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