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 */
48 /* 190 */ /* UserNode.h */
49 /* 191-193 */ #include "ModuleFunctions.h"
50 /* 194 */ /* bots.h */
52 #define Function void *
54 void *global_functions[] = {
55 /* 000 */ (Function) getStartTime,
56 /* 001 */ (Function) getRunningThreads,
57 /* 002 */ (Function) exit_daemon,
58 /* 003 */ (Function) stricmp,
59 /* 004 */ (Function) stricmplen,
60 /* 005 */ (Function) restart_process,
61 /* 006 */ (Function) cleanup,
62 /* 007 */ (Function) restart_bot,
63 /* 008 */ (Function) stop_bot,
64 /* 009 */ (Function) reload_config,
65 /* 010 */ (Function) putlog,
67 /* 011 */ (Function) getCurrentThreadID,
69 /* 011 */ (Function) NULL,
71 /* 012 */ (Function) getMatchingChannelBan,
72 /* 013 */ (Function) getChannelBot,
73 /* 014 */ (Function) requestOp,
74 /* 015 */ (Function) channel_ban_timeout,
75 /* 016 */ (Function) general_event_privctcp,
76 /* 017 */ (Function) set_bot_alias,
77 /* 018 */ (Function) resolve_botid,
78 /* 019 */ (Function) resolve_botalias,
79 /* 020 */ (Function) is_valid_chan,
80 /* 021 */ (Function) getAllChans,
81 /* 022 */ (Function) getChanByName,
82 /* 023 */ (Function) getChannelCount,
83 /* 024 */ (Function) getChanUserCount,
84 /* 025 */ (Function) getChanBanCount,
85 /* 026 */ (Function) isUserOnChan,
86 /* 027 */ (Function) getChanUser,
87 /* 028 */ (Function) getChannelUsers,
88 /* 029 */ (Function) getUserChannels,
89 /* 030 */ (Function) create_socket,
90 /* 031 */ (Function) connect_socket,
91 /* 032 */ (Function) close_socket,
92 /* 033 */ (Function) disconnect_socket,
93 /* 034 */ (Function) write_socket,
94 /* 035 */ (Function) putsock,
95 /* 036 */ (Function) getBots,
96 /* 037 */ (Function) get_int_field,
97 /* 038 */ (Function) get_string_field,
98 /* 039 */ (Function) _loadUserSettings,
99 /* 040 */ (Function) isGodMode,
100 /* 041 */ (Function) getChanDefault,
101 /* 042 */ (Function) getChannelAccess,
102 /* 043 */ (Function) checkChannelAccess,
103 /* 044 */ (Function) _loadChannelSettings,
104 /* 045 */ (Function) isUserProtected,
105 /* 046 */ (Function) getBanAffectingMask,
106 /* 047 */ (Function) renameAccount,
107 /* 048 */ (Function) deleteUser,
108 /* 049 */ (Function) logEvent,
109 /* 050 */ (Function) lookup_authname,
110 /* 051 */ (Function) bind_join,
111 /* 052 */ (Function) unbind_join,
112 /* 053 */ (Function) bind_nick,
113 /* 054 */ (Function) unbind_nick,
114 /* 055 */ (Function) bind_part,
115 /* 056 */ (Function) unbind_part,
116 /* 057 */ (Function) bind_quit,
117 /* 058 */ (Function) unbind_quit,
118 /* 059 */ (Function) bind_kick,
119 /* 060 */ (Function) unbind_kick,
120 /* 061 */ (Function) bind_topic,
121 /* 062 */ (Function) unbind_topic,
122 /* 063 */ (Function) bind_mode,
123 /* 064 */ (Function) unbind_mode,
124 /* 065 */ (Function) bind_chanmsg,
125 /* 066 */ (Function) unbind_chanmsg,
126 /* 067 */ (Function) bind_privmsg,
127 /* 068 */ (Function) unbind_privmsg,
128 /* 069 */ (Function) bind_channotice,
129 /* 070 */ (Function) unbind_channotice,
130 /* 071 */ (Function) bind_privnotice,
131 /* 072 */ (Function) unbind_privnotice,
132 /* 073 */ (Function) bind_chanctcp,
133 /* 074 */ (Function) unbind_chanctcp,
134 /* 075 */ (Function) bind_privctcp,
135 /* 076 */ (Function) unbind_privctcp,
136 /* 077 */ (Function) bind_invite,
137 /* 078 */ (Function) unbind_invite,
138 /* 079 */ (Function) bind_raw,
139 /* 080 */ (Function) unbind_raw,
140 /* 081 */ (Function) bind_bot_ready,
141 /* 082 */ (Function) unbind_bot_ready,
142 /* 083 */ (Function) bind_registered,
143 /* 084 */ (Function) unbind_registered,
144 /* 085 */ (Function) bind_freeuser,
145 /* 086 */ (Function) unbind_freeuser,
146 /* 087 */ (Function) bind_freechan,
147 /* 088 */ (Function) unbind_freechan,
148 /* 089 */ (Function) reply,
149 /* 090 */ (Function) merge_argv,
150 /* 091 */ (Function) merge_argv_char,
151 /* 092 */ (Function) get_language_by_tag,
152 /* 093 */ (Function) get_language_by_name,
153 /* 094 */ (Function) get_default_language,
154 /* 095 */ (Function) load_language,
155 /* 096 */ (Function) register_default_language_table,
156 /* 097 */ (Function) get_language_string,
157 /* 098 */ (Function) build_language_string,
158 #ifdef ENABLE_MEMORY_DEBUG
159 /* 099 */ (Function) xmalloc,
160 /* 100 */ (Function) xcalloc,
161 /* 101 */ (Function) xstrdup,
162 /* 102 */ (Function) xfree,
164 /* 099 */ (Function) NULL,
165 /* 100 */ (Function) NULL,
166 /* 101 */ (Function) NULL,
167 /* 102 */ (Function) NULL,
169 /* 103 */ (Function) getMemoryInfoFiles,
170 /* 104 */ (Function) freeMemoryInfoFiles,
171 /* 105 */ (Function) getMemoryInfoLines,
172 /* 106 */ (Function) freeMemoryInfoLines,
173 /* 107 */ (Function) get_botwise_prefered_bot,
174 /* 108 */ (Function) register_command,
175 /* 109 */ (Function) set_trigger_callback,
176 /* 110 */ (Function) flush_trigger_cache,
177 /* 111 */ (Function) changeBotwiseChannelTrigger,
178 /* 112 */ (Function) bind_botwise_cmd_to_function,
179 /* 113 */ (Function) bind_botwise_cmd_to_command,
180 /* 114 */ (Function) unbind_botwise_cmd,
181 /* 115 */ (Function) unbind_botwise_allcmd,
182 /* 116 */ (Function) bind_botwise_set_parameters,
183 /* 117 */ (Function) bind_botwise_set_global_access,
184 /* 118 */ (Function) bind_botwise_set_channel_access,
185 /* 119 */ (Function) bind_botwise_set_bind_flags,
186 /* 120 */ (Function) find_botwise_cmd_binding,
187 /* 121 */ (Function) bind_botwise_unbound_required_functions,
188 /* 122 */ (Function) find_cmd_function,
189 /* 123 */ (Function) NULL, /* deprecated */
190 /* 124 */ (Function) register_command_alias,
191 /* 125 */ (Function) getAllBinds,
192 /* 126 */ (Function) createModeNode,
193 /* 127 */ (Function) freeModeNode,
194 /* 128 */ (Function) isModeSet,
195 /* 129 */ (Function) isModeAffected,
196 /* 130 */ (Function) getModeValue,
197 /* 131 */ (Function) getModeType,
198 /* 132 */ (Function) parseModes,
199 /* 133 */ (Function) parseModeString,
200 /* 134 */ (Function) parseMode,
201 /* 135 */ (Function) getModeString,
202 /* 136 */ (Function) getFullModeString,
203 /* 137 */ (Function) mysql_use,
204 /* 138 */ (Function) mysql_free,
205 /* 139 */ (Function) printf_mysql_query,
206 /* 140 */ (Function) printf_long_mysql_query,
207 /* 141 */ (Function) escape_string,
208 /* 142 */ (Function) get_mysql_conn,
209 /* 143 */ (Function) timeq_add,
210 /* 144 */ (Function) timeq_uadd,
211 /* 145 */ (Function) timeq_add_name,
212 /* 146 */ (Function) timeq_uadd_name,
213 /* 147 */ (Function) timeq_del,
214 /* 148 */ (Function) timeq_del_name,
215 /* 149 */ (Function) timeq_name_exists,
216 /* 150 */ (Function) match,
217 /* 151 */ (Function) table_init,
218 /* 152 */ (Function) table_add,
219 /* 153 */ (Function) table_change,
220 /* 154 */ (Function) table_change_field,
221 /* 155 */ (Function) table_set_bold,
222 /* 156 */ (Function) table_end,
223 /* 157 */ (Function) table_free,
224 /* 158 */ (Function) timeToStr,
225 /* 159 */ (Function) strToTime,
226 /* 160 */ (Function) initModeBuffer,
227 /* 161 */ (Function) modeBufferSet,
228 /* 162 */ (Function) flushModeBuffer,
229 /* 163 */ (Function) freeModeBuffer,
230 /* 164 */ (Function) is_ircmask,
231 /* 165 */ (Function) generate_banmask,
232 /* 166 */ (Function) make_banmask,
233 /* 167 */ (Function) isFakeHost,
234 /* 168 */ (Function) mask_match,
235 /* 169 */ (Function) crc32,
236 /* 170 */ (Function) is_valid_nick,
237 /* 171 */ (Function) getUserByNick,
238 /* 172 */ (Function) getUserByMask,
239 /* 173 */ (Function) countUsersWithHost,
240 /* 174 */ (Function) getAuthFakehost,
241 /* 175 */ (Function) searchUserByNick,
242 /* 176 */ (Function) getAllUsers,
243 /* 177 */ (Function) getUsersWithAuth,
244 /* 178 */ (Function) getUserCount,
245 /* 179 */ (Function) createTempUser,
246 /* 180 */ (Function) createTempUserMask,
247 /* 181 */ (Function) get_userlist,
248 /* 182 */ (Function) _get_userlist_with_invisible,
249 /* 183 */ (Function) get_userauth,
250 /* 184 */ (Function) get_compilation,
251 /* 185 */ (Function) get_creation,
252 /* 186 */ (Function) get_revision,
253 /* 187 */ (Function) get_codelines,
254 /* 188 */ (Function) get_patchlevel,
255 /* 189 */ (Function) get_module_name,
256 /* 190 */ (Function) isUserModeSet,
257 /* 191 */ (Function) module_global_cmd_register_neonbackup,
258 /* 192 */ (Function) module_global_cmd_unregister_neonbackup,
259 /* 193 */ (Function) module_neonbackup_recover_chan,
260 /* 194 */ (Function) requestInvite
263 static int module_id_counter = 1;
264 static struct ModuleInfo *modules = NULL;
266 static void unregister_module_hooks(int module_id);
269 char **modulelist = get_all_fieldnames("modules");
270 if(!modulelist) return;
273 struct ModuleInfo *modinfo;
274 while(modulelist[i]) {
275 sprintf(tmp, "modules.%s.enabled", modulelist[i]);
276 if(get_int_field(tmp)) {
277 modinfo = loadModule(modulelist[i]);
278 sprintf(tmp, "modules.%s.protected", modulelist[i]);
279 if(get_int_field(tmp))
280 modinfo->state |= MODINFO_STATE_PROTECTED;
288 struct ModuleInfo *loadModule(char *name) {
289 struct ModuleInfo *modinfo;
290 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
291 if(!stricmp(modinfo->name, name)) return NULL;
295 sprintf(fname, "%s.so", name);
296 void* module = dlopen(fname, RTLD_LAZY);
298 sprintf(fname, "./%s.so", name);
299 module = dlopen(fname, RTLD_LAZY);
302 sprintf(fname, ".libs/%s.so", name);
303 module = dlopen(fname, RTLD_LAZY);
306 putlog(LOGLEVEL_ERROR, "Error loading module '%s': %s not found.\n", name, fname);
309 void* initfunc = dlsym(module, "init_module");
310 void* startfunc = dlsym(module, "start_module");
311 void* loopfunc = dlsym(module, "loop_module");
312 void* stopfunc = dlsym(module, "stop_module");
313 void* modversion = dlsym(module, "modversion");
315 sprintf(fname, "%s.dll", name);
316 HMODULE module = LoadLibrary(fname);
318 putlog(LOGLEVEL_ERROR, "Error loading module '%s': %s not found.\n", name, fname);
321 FARPROC initfunc = GetProcAddress(module, "init_module");
322 FARPROC startfunc = GetProcAddress(module, "start_module");
323 FARPROC loopfunc = GetProcAddress(module, "loop_module");
324 FARPROC stopfunc = GetProcAddress(module, "stop_module");
325 FARPROC modversion = GetProcAddress(module, "modversion");
327 if(!startfunc || !loopfunc || !stopfunc || !modversion) {
328 putlog(LOGLEVEL_ERROR, "Error loading module '%s': required symbols not found.\n", name);
331 int version = ((int (*)(void)) modversion)();
332 if(version != MODULE_VERSION) {
333 putlog(LOGLEVEL_ERROR, "Error loading module '%s': version mismatch ('%d' main code, '%d' module)\n", name, MODULE_VERSION, version);
338 int module_id = module_id_counter++;
339 if((errid = ((int (*)(void **, int)) initfunc)(global_functions, module_id))) {
340 putlog(LOGLEVEL_ERROR, "Error loading module '%s': module reported error (errid: %d)\n", name, errid);
343 modinfo = malloc(sizeof(*modinfo));
345 unregister_module_hooks(module_id);
348 modinfo->name = strdup(name);
349 modinfo->module_id = module_id;
350 modinfo->module = module;
351 modinfo->startfunc = startfunc;
352 modinfo->loopfunc = loopfunc;
353 modinfo->stopfunc = stopfunc;
355 modinfo->next = modules;
357 scan_module(modinfo);
362 static void closemodule(void *module) {
366 static void closemodule(HMODULE module) {
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);
379 ((void (*)(int)) modinfo->startfunc)(MODSTATE_REBIND);
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) {
392 old_prev->next = old_modinfo->next;
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);
403 old_prev = old_modinfo;
408 int ext_reload_module(char *name) {
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);
415 old_prev->next = old_modinfo->next;
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);
426 old_prev = old_modinfo;
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);
435 ((void (*)(int)) modinfo->startfunc)(MODSTATE_REBIND);
440 struct ModuleInfo *ext_get_modules(struct ModuleInfo *last) {
441 return (last ? last->next : modules);
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);
454 void loop_modules() {
455 struct ModuleInfo *modinfo;
456 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
457 ((void (*)(void)) modinfo->loopfunc)();
461 void stop_modules() {
462 struct ModuleInfo *modinfo, *next;
463 for(modinfo = modules; modinfo; modinfo = next) {
464 next = modinfo->next;
465 unregister_module_hooks(modinfo->module_id);
466 ((void (*)(int)) modinfo->stopfunc)(MODSTATE_STARTSTOP);
467 closemodule(modinfo->module);
468 free_module_functions(modinfo);
475 static void unregister_module_hooks(int module_id) {
476 unregister_module_commands(module_id);
477 unregister_module_events(module_id);
478 unregister_module_timers(module_id);
482 int module_loaded(int module_id) {
483 if(!module_id) return 1;
484 struct ModuleInfo *modinfo;
485 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
486 if(modinfo->module_id == module_id)
492 char *get_module_name(int module_id) {
493 if(!module_id) return NULL;
494 struct ModuleInfo *modinfo;
495 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
496 if(modinfo->module_id == module_id)
497 return modinfo->name;