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