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