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