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