fixed WIN32 compatibility
[NeonServV5.git] / src / modules.c
1 /* modules.c - NeonServ v5.3
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 "modules.h"
18 #ifndef WIN32
19 #include <dlfcn.h>
20 #endif
21
22 /* 000-011 */ #include "main.h"
23 /* 012     */ #include "BanNode.h"
24 /* 013-019 */ #include "bots.h"
25 /* 020-025 */ #include "ChanNode.h"
26 /* 026-029 */ #include "ChanUser.h"
27 /* 030-036 */ #include "ClientSocket.h"
28 /* 037-038 */ #include "ConfigParser.h"
29 /* 039-048 */ #include "DBHelper.h"
30 /* 049     */ #include "EventLogger.h"
31 /* 050     */ #include "HandleInfoHandler.h"
32 /* 051-088 */ #include "IRCEvents.h"
33 /* 089-091 */ #include "IRCParser.h"
34 /* 092-098 */ #include "lang.h"
35 #ifdef ENABLE_MEMORY_DEBUG
36 /* 099-102 */ #include "memoryDebug.h"
37 #endif
38 /* 103-106 */ #include "memoryInfo.h"
39 /* 107-125 */ #include "modcmd.h"
40 /* 126-136 */ #include "ModeNode.h"
41 /* 137-142 */ #include "mysqlConn.h"
42 /* 143-149 */ #include "timeq.h"
43 /* 150-169 */ #include "tools.h"
44 /* 170-180 */ #include "UserNode.h"
45 /* 181-183 */ #include "WHOHandler.h"
46 /* 184-188 */ #include "version.h"
47
48 #define Function void *
49
50 void *global_functions[] = {
51 /* 000 */ (Function) getStartTime,
52 /* 001 */ (Function) getRunningThreads,
53 /* 002 */ (Function) exit_daemon,
54 /* 003 */ (Function) stricmp,
55 /* 004 */ (Function) stricmplen,
56 /* 005 */ (Function) restart_process,
57 /* 006 */ (Function) cleanup,
58 /* 007 */ (Function) restart_bot,
59 /* 008 */ (Function) stop_bot,
60 /* 009 */ (Function) reload_config,
61 /* 010 */ (Function) putlog,
62 #ifdef HAVE_THREADS
63 /* 011 */ (Function) getCurrentThreadID,
64 #else
65 /* 011 */ (Function) NULL,
66 #endif
67 /* 012 */ (Function) getMatchingChannelBan,
68 /* 013 */ (Function) getChannelBot,
69 /* 014 */ (Function) requestOp,
70 /* 015 */ (Function) channel_ban_timeout,
71 /* 016 */ (Function) general_event_privctcp,
72 /* 017 */ (Function) set_bot_alias,
73 /* 018 */ (Function) resolve_botid,
74 /* 019 */ (Function) resolve_botalias,
75 /* 020 */ (Function) is_valid_chan,
76 /* 021 */ (Function) getAllChans,
77 /* 022 */ (Function) getChanByName,
78 /* 023 */ (Function) getChannelCount,
79 /* 024 */ (Function) getChanUserCount,
80 /* 025 */ (Function) getChanBanCount,
81 /* 026 */ (Function) isUserOnChan,
82 /* 027 */ (Function) getChanUser,
83 /* 028 */ (Function) getChannelUsers,
84 /* 029 */ (Function) getUserChannels,
85 /* 030 */ (Function) create_socket,
86 /* 031 */ (Function) connect_socket,
87 /* 032 */ (Function) close_socket,
88 /* 033 */ (Function) disconnect_socket,
89 /* 034 */ (Function) write_socket,
90 /* 035 */ (Function) putsock,
91 /* 036 */ (Function) getBots,
92 /* 037 */ (Function) get_int_field,
93 /* 038 */ (Function) get_string_field,
94 /* 039 */ (Function) _loadUserSettings,
95 /* 040 */ (Function) isGodMode,
96 /* 041 */ (Function) getChanDefault,
97 /* 042 */ (Function) getChannelAccess,
98 /* 043 */ (Function) checkChannelAccess,
99 /* 044 */ (Function) _loadChannelSettings,
100 /* 045 */ (Function) isUserProtected,
101 /* 046 */ (Function) getBanAffectingMask,
102 /* 047 */ (Function) renameAccount,
103 /* 048 */ (Function) deleteUser,
104 /* 049 */ (Function) logEvent,
105 /* 050 */ (Function) lookup_authname,
106 /* 051 */ (Function) bind_join,
107 /* 052 */ (Function) unbind_join,
108 /* 053 */ (Function) bind_nick,
109 /* 054 */ (Function) unbind_nick,
110 /* 055 */ (Function) bind_part,
111 /* 056 */ (Function) unbind_part,
112 /* 057 */ (Function) bind_quit,
113 /* 058 */ (Function) unbind_quit,
114 /* 059 */ (Function) bind_kick,
115 /* 060 */ (Function) unbind_kick,
116 /* 061 */ (Function) bind_topic,
117 /* 062 */ (Function) unbind_topic,
118 /* 063 */ (Function) bind_mode,
119 /* 064 */ (Function) unbind_mode,
120 /* 065 */ (Function) bind_chanmsg,
121 /* 066 */ (Function) unbind_chanmsg,
122 /* 067 */ (Function) bind_privmsg,
123 /* 068 */ (Function) unbind_privmsg,
124 /* 069 */ (Function) bind_channotice,
125 /* 070 */ (Function) unbind_channotice,
126 /* 071 */ (Function) bind_privnotice,
127 /* 072 */ (Function) unbind_privnotice,
128 /* 073 */ (Function) bind_chanctcp,
129 /* 074 */ (Function) unbind_chanctcp,
130 /* 075 */ (Function) bind_privctcp,
131 /* 076 */ (Function) unbind_privctcp,
132 /* 077 */ (Function) bind_invite,
133 /* 078 */ (Function) unbind_invite,
134 /* 079 */ (Function) bind_raw,
135 /* 080 */ (Function) unbind_raw,
136 /* 081 */ (Function) bind_bot_ready,
137 /* 082 */ (Function) unbind_bot_ready,
138 /* 083 */ (Function) bind_registered,
139 /* 084 */ (Function) unbind_registered,
140 /* 085 */ (Function) bind_freeuser,
141 /* 086 */ (Function) unbind_freeuser,
142 /* 087 */ (Function) bind_freechan,
143 /* 088 */ (Function) unbind_freechan,
144 /* 089 */ (Function) reply,
145 /* 090 */ (Function) merge_argv,
146 /* 091 */ (Function) merge_argv_char,
147 /* 092 */ (Function) get_language_by_tag,
148 /* 093 */ (Function) get_language_by_name,
149 /* 094 */ (Function) get_default_language,
150 /* 095 */ (Function) load_language,
151 /* 096 */ (Function) register_default_language_table,
152 /* 097 */ (Function) get_language_string,
153 /* 098 */ (Function) build_language_string,
154 #ifdef ENABLE_MEMORY_DEBUG
155 /* 099 */ (Function) xmalloc,
156 /* 100 */ (Function) xcalloc,
157 /* 101 */ (Function) xstrdup,
158 /* 102 */ (Function) xfree,
159 #else
160 /* 099 */ (Function) NULL,
161 /* 100 */ (Function) NULL,
162 /* 101 */ (Function) NULL,
163 /* 102 */ (Function) NULL,
164 #endif
165 /* 103 */ (Function) getMemoryInfoFiles,
166 /* 104 */ (Function) freeMemoryInfoFiles,
167 /* 105 */ (Function) getMemoryInfoLines,
168 /* 106 */ (Function) freeMemoryInfoLines,
169 /* 107 */ (Function) get_botwise_prefered_bot,
170 /* 108 */ (Function) register_command,
171 /* 109 */ (Function) set_trigger_callback,
172 /* 110 */ (Function) flush_trigger_cache,
173 /* 111 */ (Function) changeBotwiseChannelTrigger,
174 /* 112 */ (Function) bind_botwise_cmd_to_function,
175 /* 113 */ (Function) bind_botwise_cmd_to_command,
176 /* 114 */ (Function) unbind_botwise_cmd,
177 /* 115 */ (Function) unbind_botwise_allcmd,
178 /* 116 */ (Function) bind_botwise_set_parameters,
179 /* 117 */ (Function) bind_botwise_set_global_access,
180 /* 118 */ (Function) bind_botwise_set_channel_access,
181 /* 119 */ (Function) bind_botwise_set_bind_flags,
182 /* 120 */ (Function) find_botwise_cmd_binding,
183 /* 121 */ (Function) bind_botwise_unbound_required_functions,
184 /* 122 */ (Function) find_cmd_function,
185 /* 123 */ (Function) getTextBot,
186 /* 124 */ (Function) register_command_alias,
187 /* 125 */ (Function) getAllBinds,
188 /* 126 */ (Function) createModeNode,
189 /* 127 */ (Function) freeModeNode,
190 /* 128 */ (Function) isModeSet,
191 /* 129 */ (Function) isModeAffected,
192 /* 130 */ (Function) getModeValue,
193 /* 131 */ (Function) getModeType,
194 /* 132 */ (Function) parseModes,
195 /* 133 */ (Function) parseModeString,
196 /* 134 */ (Function) parseMode,
197 /* 135 */ (Function) getModeString,
198 /* 136 */ (Function) getFullModeString,
199 /* 137 */ (Function) mysql_use,
200 /* 138 */ (Function) mysql_free,
201 /* 139 */ (Function) printf_mysql_query,
202 /* 140 */ (Function) printf_long_mysql_query,
203 /* 141 */ (Function) escape_string,
204 /* 142 */ (Function) get_mysql_conn,
205 /* 143 */ (Function) timeq_add,
206 /* 144 */ (Function) timeq_uadd,
207 /* 145 */ (Function) timeq_add_name,
208 /* 146 */ (Function) timeq_uadd_name,
209 /* 147 */ (Function) timeq_del,
210 /* 148 */ (Function) timeq_del_name,
211 /* 149 */ (Function) timeq_name_exists,
212 /* 150 */ (Function) match,
213 /* 151 */ (Function) table_init,
214 /* 152 */ (Function) table_add,
215 /* 153 */ (Function) table_change,
216 /* 154 */ (Function) table_change_field,
217 /* 155 */ (Function) table_set_bold,
218 /* 156 */ (Function) table_end,
219 /* 157 */ (Function) table_free,
220 /* 158 */ (Function) timeToStr,
221 /* 159 */ (Function) strToTime,
222 /* 160 */ (Function) initModeBuffer,
223 /* 161 */ (Function) modeBufferSet,
224 /* 162 */ (Function) flushModeBuffer,
225 /* 163 */ (Function) freeModeBuffer,
226 /* 164 */ (Function) is_ircmask,
227 /* 165 */ (Function) generate_banmask,
228 /* 166 */ (Function) make_banmask,
229 /* 167 */ (Function) isFakeHost,
230 /* 168 */ (Function) mask_match,
231 /* 169 */ (Function) crc32,
232 /* 170 */ (Function) is_valid_nick,
233 /* 171 */ (Function) getUserByNick,
234 /* 172 */ (Function) getUserByMask,
235 /* 173 */ (Function) countUsersWithHost,
236 /* 174 */ (Function) getAuthFakehost,
237 /* 175 */ (Function) searchUserByNick,
238 /* 176 */ (Function) getAllUsers,
239 /* 177 */ (Function) getUsersWithAuth,
240 /* 178 */ (Function) getUserCount,
241 /* 179 */ (Function) createTempUser,
242 /* 180 */ (Function) createTempUserMask,
243 /* 181 */ (Function) get_userlist,
244 /* 182 */ (Function) _get_userlist_with_invisible,
245 /* 183 */ (Function) get_userauth,
246 /* 184 */ (Function) get_compilation,
247 /* 185 */ (Function) get_creation,
248 /* 186 */ (Function) get_revision,
249 /* 187 */ (Function) get_codelines,
250 /* 188 */ (Function) get_patchlevel
251 };
252
253 static int module_id_counter = 1;
254 static struct ModuleInfo *modules = NULL;
255
256 static void unregister_module_hooks(int module_id);
257
258 void loadModules() {
259     char **modulelist = get_all_fieldnames("modules");
260     int i = 0;
261     char tmp[MAXLEN];
262     struct ModuleInfo *modinfo;
263     while(modulelist[i]) {
264         sprintf(tmp, "modules.%s.enabled", modulelist[i]);
265         if(get_int_field(tmp)) {
266             modinfo = loadModule(modulelist[i]);
267             sprintf(tmp, "modules.%s.protected", modulelist[i]);
268             if(get_int_field(tmp))
269                 modinfo->state |= MODINFO_STATE_PROTECTED;
270         }
271         i++;
272     }
273     start_modules();
274 }
275
276 struct ModuleInfo *loadModule(char *name) {
277     struct ModuleInfo *modinfo;
278     for(modinfo = modules; modinfo; modinfo = modinfo->next) {
279         if(!stricmp(modinfo->name, name)) return NULL;
280     }
281     char fname[256];
282     #ifndef WIN32
283     sprintf(fname, "%s.so", name);
284     void* module = dlopen(fname, RTLD_LAZY);
285     if(!module) {
286         sprintf(fname, ".libs/%s.so", name);
287         module = dlopen(fname, RTLD_LAZY);
288         if(!module) {
289             putlog(LOGLEVEL_ERROR, "Error loading module '%s': %s not found.\n", name, fname);
290             return NULL;
291         }
292     }
293     void* initfunc = dlsym(module, "init_module");
294     void* startfunc = dlsym(module, "start_module");
295     void* loopfunc = dlsym(module, "loop_module");
296     void* stopfunc = dlsym(module, "stop_module");
297     void* modversion = dlsym(module, "modversion");
298     #else
299     sprintf(fname, "%s.dll", name);
300     HMODULE module = LoadLibrary(fname);
301     if(!module) {
302         putlog(LOGLEVEL_ERROR, "Error loading module '%s': %s not found.\n", name, fname);
303         return NULL;
304     }
305     FARPROC initfunc = GetProcAddress(module, "init_module");
306     FARPROC startfunc = GetProcAddress(module, "start_module");
307     FARPROC loopfunc = GetProcAddress(module, "loop_module");
308     FARPROC stopfunc = GetProcAddress(module, "stop_module");
309     FARPROC modversion = GetProcAddress(module, "modversion");
310     #endif
311     if(!startfunc || !loopfunc || !stopfunc || !modversion) {
312         putlog(LOGLEVEL_ERROR, "Error loading module '%s': required symbols not found.\n", name);
313         return NULL;
314     }
315     int version = ((int (*)(void)) modversion)();
316     if(version != MODULE_VERSION) {
317         putlog(LOGLEVEL_ERROR, "Error loading module '%s': version mismatch ('%d' main code, '%d' module)\n", name, MODULE_VERSION, version);
318         return NULL;
319     }
320     //start module
321     int errid;
322     int module_id = module_id_counter++;
323     if((errid = ((int (*)(void **, int)) initfunc)(global_functions, module_id))) {
324         putlog(LOGLEVEL_ERROR, "Error loading module '%s': module reported error (errid: %d)\n", name, errid);
325         return NULL;
326     }
327     modinfo = malloc(sizeof(*modinfo));
328     if(!modinfo) {
329         unregister_module_hooks(module_id);
330         return NULL;
331     }
332     modinfo->name = strdup(name);
333     modinfo->module_id = module_id;
334     modinfo->module = module;
335     modinfo->startfunc = startfunc;
336     modinfo->loopfunc = loopfunc;
337     modinfo->stopfunc = stopfunc;
338     modinfo->state = 0;
339     modinfo->next = modules;
340     modules = modinfo;
341     return modinfo;
342 }
343
344 #ifndef WIN32
345 static void closemodule(void *module) {
346     dlclose(module);
347 }
348 #else
349 static void closemodule(HMODULE module) {
350     FreeLibrary(module);
351 }
352 #endif
353
354 int ext_load_module(char *name) {
355     if(!loadModule(name)) return 0;
356     struct ModuleInfo *modinfo;
357     for(modinfo = modules; modinfo; modinfo = modinfo->next) {
358         if(!(modinfo->state & MODINFO_STATE_STARTED)) {
359             modinfo->state |= MODINFO_STATE_STARTED;
360             ((void (*)(int)) modinfo->startfunc)(MODSTATE_STARTSTOP);
361         } else
362             ((void (*)(int)) modinfo->startfunc)(MODSTATE_REBIND);
363     }
364     return 1;
365 }
366
367 int ext_unload_module(char *name) {
368     struct ModuleInfo *old_modinfo, *old_prev = NULL;
369     for(old_modinfo = modules; old_modinfo; old_modinfo = old_modinfo->next) {
370         if(!stricmp(old_modinfo->name, name)) {
371             if(old_modinfo->state & MODINFO_STATE_PROTECTED) {
372                 return 0;
373             }
374             if(old_prev)
375                 old_prev->next = old_modinfo->next;
376             else
377                 modules = old_modinfo->next;
378             unregister_module_hooks(old_modinfo->module_id);
379             ((void (*)(int)) old_modinfo->stopfunc)(MODSTATE_STARTSTOP);
380             closemodule(old_modinfo->module);
381             free(old_modinfo->name);
382             free(old_modinfo);
383             return 1;
384         } else
385             old_prev = old_modinfo;
386     }
387     return 0;
388 }
389
390 int ext_reload_module(char *name) {
391     char libname[256];
392     struct ModuleInfo *old_modinfo, *old_prev = NULL;
393     for(old_modinfo = modules; old_modinfo; old_modinfo = old_modinfo->next) {
394         if(!stricmp(old_modinfo->name, name)) {
395             strcpy(libname, old_modinfo->name);
396             if(old_prev)
397                 old_prev->next = old_modinfo->next;
398             else
399                 modules = old_modinfo->next;
400             unregister_module_hooks(old_modinfo->module_id);
401             ((void (*)(int)) old_modinfo->stopfunc)(MODSTATE_RELOAD);
402             closemodule(old_modinfo->module);
403             free(old_modinfo->name);
404             free(old_modinfo);
405             break;
406         } else
407             old_prev = old_modinfo;
408     }
409     if(!loadModule(libname)) return 0;
410     struct ModuleInfo *modinfo;
411     for(modinfo = modules; modinfo; modinfo = modinfo->next) {
412         if(!(modinfo->state & MODINFO_STATE_STARTED)) {
413             modinfo->state |= MODINFO_STATE_STARTED;
414             ((void (*)(int)) modinfo->startfunc)(MODSTATE_RELOAD);
415         } else
416             ((void (*)(int)) modinfo->startfunc)(MODSTATE_REBIND);
417     }
418     return 1;
419 }
420
421 struct ModuleInfo *ext_get_modules(struct ModuleInfo *last) {
422     return (last ? last->next : modules);
423 }
424
425 void start_modules() {
426     struct ModuleInfo *modinfo;
427     for(modinfo = modules; modinfo; modinfo = modinfo->next) {
428         if(!(modinfo->state & MODINFO_STATE_STARTED)) {
429             modinfo->state |= MODINFO_STATE_STARTED;
430             ((void (*)(int)) modinfo->startfunc)(MODSTATE_STARTSTOP);
431         }
432     }
433 }
434
435 void loop_modules() {
436     struct ModuleInfo *modinfo;
437     for(modinfo = modules; modinfo; modinfo = modinfo->next) {
438         ((void (*)(void)) modinfo->loopfunc)();
439     }
440 }
441
442 void stop_modules() {
443     struct ModuleInfo *modinfo, *next;
444     for(modinfo = modules; modinfo; modinfo = next) {
445         next = modinfo->next;
446         unregister_module_hooks(modinfo->module_id);
447         ((void (*)(int)) modinfo->stopfunc)(MODSTATE_STARTSTOP);
448         closemodule(modinfo->module);
449         free(modinfo->name);
450         free(modinfo);
451     }
452     modules = NULL;
453 }
454
455 static void unregister_module_hooks(int module_id) {
456     unregister_module_commands(module_id);
457     unregister_module_events(module_id);
458     unregister_module_timers(module_id);
459 }
460
461 int module_loaded(int module_id) {
462     if(!module_id) return 1;
463     struct ModuleInfo *modinfo;
464     for(modinfo = modules; modinfo; modinfo = modinfo->next) {
465         if(modinfo->module_id == module_id)
466             return 1;
467     }
468     return 0;
469 }