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