1 /* modules.c - NeonServ v5.4
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
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"
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 */
49 #define Function void *
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,
64 /* 011 */ (Function) getCurrentThreadID,
66 /* 011 */ (Function) NULL,
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,
161 /* 099 */ (Function) NULL,
162 /* 100 */ (Function) NULL,
163 /* 101 */ (Function) NULL,
164 /* 102 */ (Function) NULL,
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
255 static int module_id_counter = 1;
256 static struct ModuleInfo *modules = NULL;
258 static void unregister_module_hooks(int module_id);
261 char **modulelist = get_all_fieldnames("modules");
264 struct ModuleInfo *modinfo;
265 while(modulelist[i]) {
266 sprintf(tmp, "modules.%s.enabled", modulelist[i]);
267 if(get_int_field(tmp)) {
268 modinfo = loadModule(modulelist[i]);
269 sprintf(tmp, "modules.%s.protected", modulelist[i]);
270 if(get_int_field(tmp))
271 modinfo->state |= MODINFO_STATE_PROTECTED;
278 struct ModuleInfo *loadModule(char *name) {
279 struct ModuleInfo *modinfo;
280 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
281 if(!stricmp(modinfo->name, name)) return NULL;
285 sprintf(fname, "%s.so", name);
286 void* module = dlopen(fname, RTLD_LAZY);
288 sprintf(fname, "./%s.so", name);
289 module = dlopen(fname, RTLD_LAZY);
292 sprintf(fname, ".libs/%s.so", name);
293 module = dlopen(fname, RTLD_LAZY);
296 putlog(LOGLEVEL_ERROR, "Error loading module '%s': %s not found.\n", name, fname);
299 void* initfunc = dlsym(module, "init_module");
300 void* startfunc = dlsym(module, "start_module");
301 void* loopfunc = dlsym(module, "loop_module");
302 void* stopfunc = dlsym(module, "stop_module");
303 void* modversion = dlsym(module, "modversion");
305 sprintf(fname, "%s.dll", name);
306 HMODULE module = LoadLibrary(fname);
308 putlog(LOGLEVEL_ERROR, "Error loading module '%s': %s not found.\n", name, fname);
311 FARPROC initfunc = GetProcAddress(module, "init_module");
312 FARPROC startfunc = GetProcAddress(module, "start_module");
313 FARPROC loopfunc = GetProcAddress(module, "loop_module");
314 FARPROC stopfunc = GetProcAddress(module, "stop_module");
315 FARPROC modversion = GetProcAddress(module, "modversion");
317 if(!startfunc || !loopfunc || !stopfunc || !modversion) {
318 putlog(LOGLEVEL_ERROR, "Error loading module '%s': required symbols not found.\n", name);
321 int version = ((int (*)(void)) modversion)();
322 if(version != MODULE_VERSION) {
323 putlog(LOGLEVEL_ERROR, "Error loading module '%s': version mismatch ('%d' main code, '%d' module)\n", name, MODULE_VERSION, version);
328 int module_id = module_id_counter++;
329 if((errid = ((int (*)(void **, int)) initfunc)(global_functions, module_id))) {
330 putlog(LOGLEVEL_ERROR, "Error loading module '%s': module reported error (errid: %d)\n", name, errid);
333 modinfo = malloc(sizeof(*modinfo));
335 unregister_module_hooks(module_id);
338 modinfo->name = strdup(name);
339 modinfo->module_id = module_id;
340 modinfo->module = module;
341 modinfo->startfunc = startfunc;
342 modinfo->loopfunc = loopfunc;
343 modinfo->stopfunc = stopfunc;
345 modinfo->next = modules;
351 static void closemodule(void *module) {
355 static void closemodule(HMODULE module) {
360 int ext_load_module(char *name) {
361 if(!loadModule(name)) return 0;
362 struct ModuleInfo *modinfo;
363 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
364 if(!(modinfo->state & MODINFO_STATE_STARTED)) {
365 modinfo->state |= MODINFO_STATE_STARTED;
366 ((void (*)(int)) modinfo->startfunc)(MODSTATE_STARTSTOP);
368 ((void (*)(int)) modinfo->startfunc)(MODSTATE_REBIND);
373 int ext_unload_module(char *name) {
374 struct ModuleInfo *old_modinfo, *old_prev = NULL;
375 for(old_modinfo = modules; old_modinfo; old_modinfo = old_modinfo->next) {
376 if(!stricmp(old_modinfo->name, name)) {
377 if(old_modinfo->state & MODINFO_STATE_PROTECTED) {
381 old_prev->next = old_modinfo->next;
383 modules = old_modinfo->next;
384 unregister_module_hooks(old_modinfo->module_id);
385 ((void (*)(int)) old_modinfo->stopfunc)(MODSTATE_STARTSTOP);
386 closemodule(old_modinfo->module);
387 free(old_modinfo->name);
391 old_prev = old_modinfo;
396 int ext_reload_module(char *name) {
398 struct ModuleInfo *old_modinfo, *old_prev = NULL;
399 for(old_modinfo = modules; old_modinfo; old_modinfo = old_modinfo->next) {
400 if(!stricmp(old_modinfo->name, name)) {
401 strcpy(libname, old_modinfo->name);
403 old_prev->next = old_modinfo->next;
405 modules = old_modinfo->next;
406 unregister_module_hooks(old_modinfo->module_id);
407 ((void (*)(int)) old_modinfo->stopfunc)(MODSTATE_RELOAD);
408 closemodule(old_modinfo->module);
409 free(old_modinfo->name);
413 old_prev = old_modinfo;
415 if(!loadModule(libname)) return 0;
416 struct ModuleInfo *modinfo;
417 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
418 if(!(modinfo->state & MODINFO_STATE_STARTED)) {
419 modinfo->state |= MODINFO_STATE_STARTED;
420 ((void (*)(int)) modinfo->startfunc)(MODSTATE_RELOAD);
422 ((void (*)(int)) modinfo->startfunc)(MODSTATE_REBIND);
427 struct ModuleInfo *ext_get_modules(struct ModuleInfo *last) {
428 return (last ? last->next : modules);
431 void start_modules() {
432 struct ModuleInfo *modinfo;
433 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
434 if(!(modinfo->state & MODINFO_STATE_STARTED)) {
435 modinfo->state |= MODINFO_STATE_STARTED;
436 ((void (*)(int)) modinfo->startfunc)(MODSTATE_STARTSTOP);
441 void loop_modules() {
442 struct ModuleInfo *modinfo;
443 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
444 ((void (*)(void)) modinfo->loopfunc)();
448 void stop_modules() {
449 struct ModuleInfo *modinfo, *next;
450 for(modinfo = modules; modinfo; modinfo = next) {
451 next = modinfo->next;
452 unregister_module_hooks(modinfo->module_id);
453 ((void (*)(int)) modinfo->stopfunc)(MODSTATE_STARTSTOP);
454 closemodule(modinfo->module);
461 static void unregister_module_hooks(int module_id) {
462 unregister_module_commands(module_id);
463 unregister_module_events(module_id);
464 unregister_module_timers(module_id);
467 int module_loaded(int module_id) {
468 if(!module_id) return 1;
469 struct ModuleInfo *modinfo;
470 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
471 if(modinfo->module_id == module_id)
477 char *get_module_name(int module_id) {
478 if(!module_id) return NULL;
479 struct ModuleInfo *modinfo;
480 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
481 if(modinfo->module_id == module_id)
482 return modinfo->name;