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