search modules locally
[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
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, "./%s.so", name);
287         module = dlopen(fname, RTLD_LAZY);
288     }
289     if(!module) {
290         sprintf(fname, ".libs/%s.so", name);
291         module = dlopen(fname, RTLD_LAZY);
292     }
293     if(!module) {
294         putlog(LOGLEVEL_ERROR, "Error loading module '%s': %s not found.\n", name, fname);
295         return NULL;
296     }
297     void* initfunc = dlsym(module, "init_module");
298     void* startfunc = dlsym(module, "start_module");
299     void* loopfunc = dlsym(module, "loop_module");
300     void* stopfunc = dlsym(module, "stop_module");
301     void* modversion = dlsym(module, "modversion");
302     #else
303     sprintf(fname, "%s.dll", name);
304     HMODULE module = LoadLibrary(fname);
305     if(!module) {
306         putlog(LOGLEVEL_ERROR, "Error loading module '%s': %s not found.\n", name, fname);
307         return NULL;
308     }
309     FARPROC initfunc = GetProcAddress(module, "init_module");
310     FARPROC startfunc = GetProcAddress(module, "start_module");
311     FARPROC loopfunc = GetProcAddress(module, "loop_module");
312     FARPROC stopfunc = GetProcAddress(module, "stop_module");
313     FARPROC modversion = GetProcAddress(module, "modversion");
314     #endif
315     if(!startfunc || !loopfunc || !stopfunc || !modversion) {
316         putlog(LOGLEVEL_ERROR, "Error loading module '%s': required symbols not found.\n", name);
317         return NULL;
318     }
319     int version = ((int (*)(void)) modversion)();
320     if(version != MODULE_VERSION) {
321         putlog(LOGLEVEL_ERROR, "Error loading module '%s': version mismatch ('%d' main code, '%d' module)\n", name, MODULE_VERSION, version);
322         return NULL;
323     }
324     //start module
325     int errid;
326     int module_id = module_id_counter++;
327     if((errid = ((int (*)(void **, int)) initfunc)(global_functions, module_id))) {
328         putlog(LOGLEVEL_ERROR, "Error loading module '%s': module reported error (errid: %d)\n", name, errid);
329         return NULL;
330     }
331     modinfo = malloc(sizeof(*modinfo));
332     if(!modinfo) {
333         unregister_module_hooks(module_id);
334         return NULL;
335     }
336     modinfo->name = strdup(name);
337     modinfo->module_id = module_id;
338     modinfo->module = module;
339     modinfo->startfunc = startfunc;
340     modinfo->loopfunc = loopfunc;
341     modinfo->stopfunc = stopfunc;
342     modinfo->state = 0;
343     modinfo->next = modules;
344     modules = modinfo;
345     return modinfo;
346 }
347
348 #ifndef WIN32
349 static void closemodule(void *module) {
350     dlclose(module);
351 }
352 #else
353 static void closemodule(HMODULE module) {
354     FreeLibrary(module);
355 }
356 #endif
357
358 int ext_load_module(char *name) {
359     if(!loadModule(name)) return 0;
360     struct ModuleInfo *modinfo;
361     for(modinfo = modules; modinfo; modinfo = modinfo->next) {
362         if(!(modinfo->state & MODINFO_STATE_STARTED)) {
363             modinfo->state |= MODINFO_STATE_STARTED;
364             ((void (*)(int)) modinfo->startfunc)(MODSTATE_STARTSTOP);
365         } else
366             ((void (*)(int)) modinfo->startfunc)(MODSTATE_REBIND);
367     }
368     return 1;
369 }
370
371 int ext_unload_module(char *name) {
372     struct ModuleInfo *old_modinfo, *old_prev = NULL;
373     for(old_modinfo = modules; old_modinfo; old_modinfo = old_modinfo->next) {
374         if(!stricmp(old_modinfo->name, name)) {
375             if(old_modinfo->state & MODINFO_STATE_PROTECTED) {
376                 return 0;
377             }
378             if(old_prev)
379                 old_prev->next = old_modinfo->next;
380             else
381                 modules = old_modinfo->next;
382             unregister_module_hooks(old_modinfo->module_id);
383             ((void (*)(int)) old_modinfo->stopfunc)(MODSTATE_STARTSTOP);
384             closemodule(old_modinfo->module);
385             free(old_modinfo->name);
386             free(old_modinfo);
387             return 1;
388         } else
389             old_prev = old_modinfo;
390     }
391     return 0;
392 }
393
394 int ext_reload_module(char *name) {
395     char libname[256];
396     struct ModuleInfo *old_modinfo, *old_prev = NULL;
397     for(old_modinfo = modules; old_modinfo; old_modinfo = old_modinfo->next) {
398         if(!stricmp(old_modinfo->name, name)) {
399             strcpy(libname, old_modinfo->name);
400             if(old_prev)
401                 old_prev->next = old_modinfo->next;
402             else
403                 modules = old_modinfo->next;
404             unregister_module_hooks(old_modinfo->module_id);
405             ((void (*)(int)) old_modinfo->stopfunc)(MODSTATE_RELOAD);
406             closemodule(old_modinfo->module);
407             free(old_modinfo->name);
408             free(old_modinfo);
409             break;
410         } else
411             old_prev = old_modinfo;
412     }
413     if(!loadModule(libname)) return 0;
414     struct ModuleInfo *modinfo;
415     for(modinfo = modules; modinfo; modinfo = modinfo->next) {
416         if(!(modinfo->state & MODINFO_STATE_STARTED)) {
417             modinfo->state |= MODINFO_STATE_STARTED;
418             ((void (*)(int)) modinfo->startfunc)(MODSTATE_RELOAD);
419         } else
420             ((void (*)(int)) modinfo->startfunc)(MODSTATE_REBIND);
421     }
422     return 1;
423 }
424
425 struct ModuleInfo *ext_get_modules(struct ModuleInfo *last) {
426     return (last ? last->next : modules);
427 }
428
429 void start_modules() {
430     struct ModuleInfo *modinfo;
431     for(modinfo = modules; modinfo; modinfo = modinfo->next) {
432         if(!(modinfo->state & MODINFO_STATE_STARTED)) {
433             modinfo->state |= MODINFO_STATE_STARTED;
434             ((void (*)(int)) modinfo->startfunc)(MODSTATE_STARTSTOP);
435         }
436     }
437 }
438
439 void loop_modules() {
440     struct ModuleInfo *modinfo;
441     for(modinfo = modules; modinfo; modinfo = modinfo->next) {
442         ((void (*)(void)) modinfo->loopfunc)();
443     }
444 }
445
446 void stop_modules() {
447     struct ModuleInfo *modinfo, *next;
448     for(modinfo = modules; modinfo; modinfo = next) {
449         next = modinfo->next;
450         unregister_module_hooks(modinfo->module_id);
451         ((void (*)(int)) modinfo->stopfunc)(MODSTATE_STARTSTOP);
452         closemodule(modinfo->module);
453         free(modinfo->name);
454         free(modinfo);
455     }
456     modules = NULL;
457 }
458
459 static void unregister_module_hooks(int module_id) {
460     unregister_module_commands(module_id);
461     unregister_module_events(module_id);
462     unregister_module_timers(module_id);
463 }
464
465 int module_loaded(int module_id) {
466     if(!module_id) return 1;
467     struct ModuleInfo *modinfo;
468     for(modinfo = modules; modinfo; modinfo = modinfo->next) {
469         if(modinfo->module_id == module_id)
470             return 1;
471     }
472     return 0;
473 }