fixed renameAccount function (merging mode)
[NeonServV5.git] / src / ModuleFunctions.c
1 /* ModuleFunctions.c - NeonServ v5.6
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17 #include "ModuleFunctions.h"
18 #include "modules.h"
19 #include "ChanNode.h"
20 #ifndef WIN32
21 #include <dlfcn.h>
22 #endif
23
24 #define FUNCTION_GLOBAL_CMD_REGISTER_NEONBACKUP   0
25 #define FUNCTION_GLOBAL_CMD_UNREGISTER_NEONBACKUP 1
26 #define FUNCTION_NEONBACKUP_RECOVER_CHAN          2
27
28 #define TOTAL_FUNCTIONS                           3
29
30 struct module_function {
31     struct ModuleInfo *module;
32     void *function;
33 };
34
35 static struct module_function module_functions[TOTAL_FUNCTIONS];
36
37 void init_modulefunctions() {
38     int i;
39     for(i = 0; i < TOTAL_FUNCTIONS; i++) {
40         module_functions[i].function = NULL;
41     }
42 }
43
44 void scan_module(struct ModuleInfo *module) {
45     char *function_names[] = {
46         "global_cmd_register_neonbackup",   /* FUNCTION_GLOBAL_CMD_REGISTER_NEONBACKUP */
47         "global_cmd_unregister_neonbackup", /* FUNCTION_GLOBAL_CMD_UNREGISTER_NEONBACKUP */
48         "neonbackup_recover_chan"           /* FUNCTION_NEONBACKUP_RECOVER_CHAN */
49     };
50     int i;
51     for(i = 0; i < TOTAL_FUNCTIONS; i++) {
52         #ifndef WIN32
53         void* func = dlsym(module->module, function_names[i]);
54         #else
55         FARPROC func = GetProcAddress(module->module, function_names[i]);
56         #endif
57         if(func) {
58             module_functions[i].module = module;
59             module_functions[i].function = func;
60         }
61     }
62 }
63
64 void free_module_functions(struct ModuleInfo *module) {
65     int i;
66     for(i = 0; i < TOTAL_FUNCTIONS; i++) {
67         if(module_functions[i].module == module) {
68             module_functions[i].function = NULL;
69         }
70     }
71 }
72
73 /* function handlers */
74 void module_global_cmd_register_neonbackup(char *chan) {
75     if(!module_functions[FUNCTION_GLOBAL_CMD_REGISTER_NEONBACKUP].function)
76         return;
77     ((void (*)(char *)) module_functions[FUNCTION_GLOBAL_CMD_REGISTER_NEONBACKUP].function)(chan);
78 }
79
80 void module_global_cmd_unregister_neonbackup(char *chan) {
81     if(!module_functions[FUNCTION_GLOBAL_CMD_UNREGISTER_NEONBACKUP].function)
82         return;
83     ((void (*)(char *)) module_functions[FUNCTION_GLOBAL_CMD_UNREGISTER_NEONBACKUP].function)(chan);
84 }
85
86 void module_neonbackup_recover_chan(struct ChanNode *chan) {
87     if(!module_functions[FUNCTION_NEONBACKUP_RECOVER_CHAN].function)
88         return;
89     ((void (*)(struct ChanNode *)) module_functions[FUNCTION_NEONBACKUP_RECOVER_CHAN].function)(chan);
90 }