1 /* modules.c - NeonServ v5.6
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-001 */ #include "main.h"
23 /* 002-004 */ #include "tools.h"
24 /* 005-006 */ /* deprecated */
25 /* 007-009 */ /* main.h */
26 /* 010 */ #include "log.h"
27 /* 011 */ /* main.h */
28 /* 012 */ #include "BanNode.h"
29 /* 013-019 */ #include "bots.h"
30 /* 020-025 */ #include "ChanNode.h"
31 /* 026-029 */ #include "ChanUser.h"
32 /* 030-036 */ #include "ClientSocket.h"
33 /* 037-038 */ #include "ConfigParser.h"
34 /* 039-048 */ #include "DBHelper.h"
35 /* 049 */ #include "EventLogger.h"
36 /* 050 */ #include "HandleInfoHandler.h"
37 /* 051-088 */ #include "IRCEvents.h"
38 /* 089-091 */ #include "IRCParser.h"
39 /* 092-098 */ #include "lang.h"
40 #ifdef ENABLE_MEMORY_DEBUG
41 /* 099-102 */ #include "memoryDebug.h"
43 /* 103-106 */ #include "memoryInfo.h"
44 /* 107-125 */ #include "modcmd.h"
45 /* 126-136 */ #include "ModeNode.h"
46 /* 137-142 */ #include "mysqlConn.h"
47 /* 143-149 */ #include "timeq.h"
48 /* 150-169 */ /* tools.h */
49 /* 170-180 */ #include "UserNode.h"
50 /* 181-183 */ #include "WHOHandler.h"
51 /* 184-188 */ #include "version.h"
52 /* 189 */ /* modules.h */
53 /* 190 */ /* UserNode.h */
54 /* 191-193 */ #include "ModuleFunctions.h"
55 /* 194 */ /* bots.h */
56 /* 195-196 */ /* version.h */
57 /* 197-198 */ /* IRCEvents.h */
59 #define Function void *
61 void *global_functions[] = {
62 /* 000 */ (Function) getStartTime,
63 /* 001 */ (Function) getRunningThreads,
64 /* 002 */ (Function) getCurrentSecondsOfDay,
65 /* 003 */ (Function) stricmp,
66 /* 004 */ (Function) stricmplen,
67 /* 005 */ (Function) NULL, /* deprecated */
68 /* 006 */ (Function) NULL, /* deprecated */
69 /* 007 */ (Function) restart_bot,
70 /* 008 */ (Function) stop_bot,
71 /* 009 */ (Function) reload_config,
72 /* 010 */ (Function) printf_log,
74 /* 011 */ (Function) getCurrentThreadID,
76 /* 011 */ (Function) NULL,
78 /* 012 */ (Function) getMatchingChannelBan,
79 /* 013 */ (Function) getChannelBot,
80 /* 014 */ (Function) requestOp,
81 /* 015 */ (Function) channel_ban_timeout,
82 /* 016 */ (Function) general_event_privctcp,
83 /* 017 */ (Function) set_bot_alias,
84 /* 018 */ (Function) resolve_botid,
85 /* 019 */ (Function) resolve_botalias,
86 /* 020 */ (Function) is_valid_chan,
87 /* 021 */ (Function) getAllChans,
88 /* 022 */ (Function) getChanByName,
89 /* 023 */ (Function) getChannelCount,
90 /* 024 */ (Function) getChanUserCount,
91 /* 025 */ (Function) getChanBanCount,
92 /* 026 */ (Function) isUserOnChan,
93 /* 027 */ (Function) getChanUser,
94 /* 028 */ (Function) getChannelUsers,
95 /* 029 */ (Function) getUserChannels,
96 /* 030 */ (Function) create_socket,
97 /* 031 */ (Function) connect_socket,
98 /* 032 */ (Function) close_socket,
99 /* 033 */ (Function) destroy_socket,
100 /* 034 */ (Function) write_socket,
101 /* 035 */ (Function) putsock,
102 /* 036 */ (Function) getBots,
103 /* 037 */ (Function) get_int_field,
104 /* 038 */ (Function) get_string_field,
105 /* 039 */ (Function) _loadUserSettings,
106 /* 040 */ (Function) isGodMode,
107 /* 041 */ (Function) getChanDefault,
108 /* 042 */ (Function) getChannelAccess,
109 /* 043 */ (Function) checkChannelAccess,
110 /* 044 */ (Function) _loadChannelSettings,
111 /* 045 */ (Function) isUserProtected,
112 /* 046 */ (Function) getBanAffectingMask,
113 /* 047 */ (Function) renameAccount,
114 /* 048 */ (Function) deleteUser,
115 /* 049 */ (Function) logEvent,
116 /* 050 */ (Function) lookup_authname,
117 /* 051 */ (Function) bind_join,
118 /* 052 */ (Function) unbind_join,
119 /* 053 */ (Function) bind_nick,
120 /* 054 */ (Function) unbind_nick,
121 /* 055 */ (Function) bind_part,
122 /* 056 */ (Function) unbind_part,
123 /* 057 */ (Function) bind_reload,
124 /* 058 */ (Function) unbind_reload,
125 /* 059 */ (Function) bind_kick,
126 /* 060 */ (Function) unbind_kick,
127 /* 061 */ (Function) bind_topic,
128 /* 062 */ (Function) unbind_topic,
129 /* 063 */ (Function) bind_mode,
130 /* 064 */ (Function) unbind_mode,
131 /* 065 */ (Function) bind_chanmsg,
132 /* 066 */ (Function) unbind_chanmsg,
133 /* 067 */ (Function) bind_privmsg,
134 /* 068 */ (Function) unbind_privmsg,
135 /* 069 */ (Function) bind_channotice,
136 /* 070 */ (Function) unbind_channotice,
137 /* 071 */ (Function) bind_privnotice,
138 /* 072 */ (Function) unbind_privnotice,
139 /* 073 */ (Function) bind_chanctcp,
140 /* 074 */ (Function) unbind_chanctcp,
141 /* 075 */ (Function) bind_privctcp,
142 /* 076 */ (Function) unbind_privctcp,
143 /* 077 */ (Function) bind_invite,
144 /* 078 */ (Function) unbind_invite,
145 /* 079 */ (Function) bind_raw,
146 /* 080 */ (Function) unbind_raw,
147 /* 081 */ (Function) bind_bot_ready,
148 /* 082 */ (Function) unbind_bot_ready,
149 /* 083 */ (Function) bind_registered,
150 /* 084 */ (Function) unbind_registered,
151 /* 085 */ (Function) bind_freeuser,
152 /* 086 */ (Function) unbind_freeuser,
153 /* 087 */ (Function) bind_freechan,
154 /* 088 */ (Function) unbind_freechan,
155 /* 089 */ (Function) reply,
156 /* 090 */ (Function) merge_argv,
157 /* 091 */ (Function) merge_argv_char,
158 /* 092 */ (Function) get_language_by_tag,
159 /* 093 */ (Function) get_language_by_name,
160 /* 094 */ (Function) get_default_language,
161 /* 095 */ (Function) load_language,
162 /* 096 */ (Function) register_default_language_table,
163 /* 097 */ (Function) get_language_string,
164 /* 098 */ (Function) build_language_string,
165 #ifdef ENABLE_MEMORY_DEBUG
166 /* 099 */ (Function) xmalloc,
167 /* 100 */ (Function) xcalloc,
168 /* 101 */ (Function) xstrdup,
169 /* 102 */ (Function) xfree,
171 /* 099 */ (Function) NULL,
172 /* 100 */ (Function) NULL,
173 /* 101 */ (Function) NULL,
174 /* 102 */ (Function) NULL,
176 /* 103 */ (Function) getMemoryInfoFiles,
177 /* 104 */ (Function) freeMemoryInfoFiles,
178 /* 105 */ (Function) getMemoryInfoLines,
179 /* 106 */ (Function) freeMemoryInfoLines,
180 /* 107 */ (Function) get_botwise_prefered_bot,
181 /* 108 */ (Function) register_command,
182 /* 109 */ (Function) set_trigger_callback,
183 /* 110 */ (Function) flush_trigger_cache,
184 /* 111 */ (Function) changeBotwiseChannelTrigger,
185 /* 112 */ (Function) bind_botwise_cmd_to_function,
186 /* 113 */ (Function) bind_botwise_cmd_to_command,
187 /* 114 */ (Function) unbind_botwise_cmd,
188 /* 115 */ (Function) unbind_botwise_allcmd,
189 /* 116 */ (Function) bind_botwise_set_parameters,
190 /* 117 */ (Function) bind_botwise_set_global_access,
191 /* 118 */ (Function) bind_botwise_set_channel_access,
192 /* 119 */ (Function) bind_botwise_set_bind_flags,
193 /* 120 */ (Function) find_botwise_cmd_binding,
194 /* 121 */ (Function) bind_botwise_unbound_required_functions,
195 /* 122 */ (Function) find_cmd_function,
196 /* 123 */ (Function) NULL, /* deprecated */
197 /* 124 */ (Function) register_command_alias,
198 /* 125 */ (Function) getAllBinds,
199 /* 126 */ (Function) createModeNode,
200 /* 127 */ (Function) freeModeNode,
201 /* 128 */ (Function) isModeSet,
202 /* 129 */ (Function) isModeAffected,
203 /* 130 */ (Function) getModeValue,
204 /* 131 */ (Function) getModeType,
205 /* 132 */ (Function) parseModes,
206 /* 133 */ (Function) parseModeString,
207 /* 134 */ (Function) parseMode,
208 /* 135 */ (Function) getModeString,
209 /* 136 */ (Function) getFullModeString,
210 /* 137 */ (Function) mysql_use,
211 /* 138 */ (Function) mysql_free,
212 /* 139 */ (Function) printf_mysql_query,
213 /* 140 */ (Function) printf_long_mysql_query,
214 /* 141 */ (Function) escape_string,
215 /* 142 */ (Function) get_mysql_conn,
216 /* 143 */ (Function) timeq_add,
217 /* 144 */ (Function) timeq_uadd,
218 /* 145 */ (Function) timeq_add_name,
219 /* 146 */ (Function) timeq_uadd_name,
220 /* 147 */ (Function) timeq_del,
221 /* 148 */ (Function) timeq_del_name,
222 /* 149 */ (Function) timeq_name_exists,
223 /* 150 */ (Function) match,
224 /* 151 */ (Function) table_init,
225 /* 152 */ (Function) table_add,
226 /* 153 */ (Function) table_change,
227 /* 154 */ (Function) table_change_field,
228 /* 155 */ (Function) table_set_bold,
229 /* 156 */ (Function) table_end,
230 /* 157 */ (Function) table_free,
231 /* 158 */ (Function) timeToStr,
232 /* 159 */ (Function) strToTime,
233 /* 160 */ (Function) initModeBuffer,
234 /* 161 */ (Function) modeBufferSet,
235 /* 162 */ (Function) flushModeBuffer,
236 /* 163 */ (Function) freeModeBuffer,
237 /* 164 */ (Function) is_ircmask,
238 /* 165 */ (Function) generate_banmask,
239 /* 166 */ (Function) make_banmask,
240 /* 167 */ (Function) isFakeHost,
241 /* 168 */ (Function) mask_match,
242 /* 169 */ (Function) crc32,
243 /* 170 */ (Function) is_valid_nick,
244 /* 171 */ (Function) getUserByNick,
245 /* 172 */ (Function) getUserByMask,
246 /* 173 */ (Function) countUsersWithHost,
247 /* 174 */ (Function) getAuthFakehost,
248 /* 175 */ (Function) searchUserByNick,
249 /* 176 */ (Function) getAllUsers,
250 /* 177 */ (Function) getUsersWithAuth,
251 /* 178 */ (Function) getUserCount,
252 /* 179 */ (Function) createTempUser,
253 /* 180 */ (Function) createTempUserMask,
254 /* 181 */ (Function) get_userlist,
255 /* 182 */ (Function) _get_userlist_with_invisible,
256 /* 183 */ (Function) get_userauth,
257 /* 184 */ (Function) get_compilation,
258 /* 185 */ (Function) get_creation,
259 /* 186 */ (Function) get_revision,
260 /* 187 */ (Function) get_codelines,
261 /* 188 */ (Function) get_patchlevel,
262 /* 189 */ (Function) get_module_name,
263 /* 190 */ (Function) isUserModeSet,
264 /* 191 */ (Function) module_global_cmd_register_neonbackup,
265 /* 192 */ (Function) module_global_cmd_unregister_neonbackup,
266 /* 193 */ (Function) module_neonbackup_recover_chan,
267 /* 194 */ (Function) requestInvite,
268 /* 195 */ (Function) is_stable_revision,
269 /* 196 */ (Function) get_dev_revision,
270 /* 197 */ (Function) bind_freeclient,
271 /* 198 */ (Function) unbind_freeclient
274 static int module_id_counter = 1;
275 static struct ModuleInfo *modules = NULL;
277 static void unregister_module_hooks(int module_id);
280 char **modulelist = get_all_fieldnames("modules");
281 if(!modulelist) return;
284 struct ModuleInfo *modinfo;
285 while(modulelist[i]) {
286 sprintf(tmp, "modules.%s.enabled", modulelist[i]);
287 if(get_int_field(tmp)) {
288 modinfo = loadModule(modulelist[i]);
289 sprintf(tmp, "modules.%s.protected", modulelist[i]);
290 if(get_int_field(tmp))
291 modinfo->state |= MODINFO_STATE_PROTECTED;
299 struct ModuleInfo *loadModule(char *name) {
300 struct ModuleInfo *modinfo;
301 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
302 if(!stricmp(modinfo->name, name)) return NULL;
306 sprintf(fname, "%s.so", name);
307 void* module = dlopen(fname, RTLD_LAZY);
309 sprintf(fname, "./%s.so", name);
310 module = dlopen(fname, RTLD_LAZY);
313 sprintf(fname, ".libs/%s.so", name);
314 module = dlopen(fname, RTLD_LAZY);
317 printf_log("main", LOG_ERROR, "Error loading module '%s': %s not found.\n", name, fname);
320 void* initfunc = dlsym(module, "init_module");
321 void* startfunc = dlsym(module, "start_module");
322 void* stopfunc = dlsym(module, "stop_module");
323 void* modversion = dlsym(module, "modversion");
325 sprintf(fname, "%s.dll", name);
326 HMODULE module = LoadLibrary(fname);
328 printf_log("main", LOG_ERROR, "Error loading module '%s': %s not found.\n", name, fname);
331 FARPROC initfunc = GetProcAddress(module, "init_module");
332 FARPROC startfunc = GetProcAddress(module, "start_module");
333 FARPROC stopfunc = GetProcAddress(module, "stop_module");
334 FARPROC modversion = GetProcAddress(module, "modversion");
336 if(!startfunc || !stopfunc || !modversion) {
337 printf_log("main", LOG_ERROR, "Error loading module '%s': required symbols not found.\n", name);
340 int version = ((int (*)(void)) modversion)();
341 if(version != MODULE_VERSION) {
342 printf_log("main", LOG_ERROR, "Error loading module '%s': version mismatch ('%d' main code, '%d' module)\n", name, MODULE_VERSION, version);
347 int module_id = module_id_counter++;
348 if((errid = ((int (*)(void **, int)) initfunc)(global_functions, module_id))) {
349 printf_log("main", LOG_ERROR, "Error loading module '%s': module reported error (errid: %d)\n", name, errid);
352 modinfo = malloc(sizeof(*modinfo));
354 unregister_module_hooks(module_id);
357 modinfo->name = strdup(name);
358 modinfo->module_id = module_id;
359 modinfo->module = module;
360 modinfo->startfunc = startfunc;
361 modinfo->stopfunc = stopfunc;
363 modinfo->next = modules;
365 scan_module(modinfo);
370 static void closemodule(void *module) {
374 static void closemodule(HMODULE module) {
379 int ext_load_module(char *name) {
380 if(!loadModule(name)) return 0;
381 struct ModuleInfo *modinfo;
382 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
383 if(!(modinfo->state & MODINFO_STATE_STARTED)) {
384 modinfo->state |= MODINFO_STATE_STARTED;
385 ((void (*)(int)) modinfo->startfunc)(MODSTATE_STARTSTOP);
387 ((void (*)(int)) modinfo->startfunc)(MODSTATE_REBIND);
392 int ext_unload_module(char *name) {
393 struct ModuleInfo *old_modinfo, *old_prev = NULL;
394 for(old_modinfo = modules; old_modinfo; old_modinfo = old_modinfo->next) {
395 if(!stricmp(old_modinfo->name, name)) {
396 if(old_modinfo->state & MODINFO_STATE_PROTECTED) {
400 old_prev->next = old_modinfo->next;
402 modules = old_modinfo->next;
403 unregister_module_hooks(old_modinfo->module_id);
404 ((void (*)(int)) old_modinfo->stopfunc)(MODSTATE_STARTSTOP);
405 closemodule(old_modinfo->module);
406 free_module_functions(old_modinfo);
407 free(old_modinfo->name);
411 old_prev = old_modinfo;
416 int ext_reload_module(char *name) {
418 struct ModuleInfo *old_modinfo, *old_prev = NULL;
419 for(old_modinfo = modules; old_modinfo; old_modinfo = old_modinfo->next) {
420 if(!stricmp(old_modinfo->name, name)) {
421 strcpy(libname, old_modinfo->name);
423 old_prev->next = old_modinfo->next;
425 modules = old_modinfo->next;
426 unregister_module_hooks(old_modinfo->module_id);
427 ((void (*)(int)) old_modinfo->stopfunc)(MODSTATE_RELOAD);
428 closemodule(old_modinfo->module);
429 free_module_functions(old_modinfo);
430 free(old_modinfo->name);
434 old_prev = old_modinfo;
436 if(!loadModule(libname)) return 0;
437 struct ModuleInfo *modinfo;
438 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
439 if(!(modinfo->state & MODINFO_STATE_STARTED)) {
440 modinfo->state |= MODINFO_STATE_STARTED;
441 ((void (*)(int)) modinfo->startfunc)(MODSTATE_RELOAD);
443 ((void (*)(int)) modinfo->startfunc)(MODSTATE_REBIND);
448 struct ModuleInfo *ext_get_modules(struct ModuleInfo *last) {
449 return (last ? last->next : modules);
452 void start_modules() {
453 struct ModuleInfo *modinfo;
454 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
455 if(!(modinfo->state & MODINFO_STATE_STARTED)) {
456 modinfo->state |= MODINFO_STATE_STARTED;
457 ((void (*)(int)) modinfo->startfunc)(MODSTATE_STARTSTOP);
462 void stop_modules() {
463 struct ModuleInfo *modinfo, *next;
464 for(modinfo = modules; modinfo; modinfo = next) {
465 next = modinfo->next;
466 unregister_module_hooks(modinfo->module_id);
467 ((void (*)(int)) modinfo->stopfunc)(MODSTATE_STARTSTOP);
468 closemodule(modinfo->module);
469 free_module_functions(modinfo);
476 static void unregister_module_hooks(int module_id) {
477 unregister_module_commands(module_id);
478 unregister_module_events(module_id);
479 unregister_module_timers(module_id);
483 int module_loaded(int module_id) {
484 if(!module_id) return 1;
485 struct ModuleInfo *modinfo;
486 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
487 if(modinfo->module_id == module_id)
493 char *get_module_name(int module_id) {
494 if(!module_id) return NULL;
495 struct ModuleInfo *modinfo;
496 for(modinfo = modules; modinfo; modinfo = modinfo->next) {
497 if(modinfo->module_id == module_id)
498 return modinfo->name;