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