]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd. | |
3 | * modules.c: A module loader. | |
4 | * | |
5 | * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center | |
6 | * Copyright (C) 1996-2002 Hybrid Development Team | |
7 | * Copyright (C) 2002-2005 ircd-ratbox development team | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License as published by | |
11 | * the Free Software Foundation; either version 2 of the License, or | |
12 | * (at your option) any later version. | |
13 | * | |
14 | * This program is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
22 | * USA | |
212380e3 AC |
23 | */ |
24 | ||
25 | #include "stdinc.h" | |
212380e3 | 26 | #include "modules.h" |
4016731b | 27 | #include "logger.h" |
212380e3 AC |
28 | #include "ircd.h" |
29 | #include "client.h" | |
30 | #include "send.h" | |
31 | #include "s_conf.h" | |
32 | #include "s_newconf.h" | |
33 | #include "numeric.h" | |
34 | #include "parse.h" | |
35 | #include "ircd_defs.h" | |
4562c604 | 36 | #include "match.h" |
15feac53 | 37 | #include "s_serv.h" |
8e9c6a75 | 38 | #include "capability.h" |
212380e3 | 39 | |
f272e7ab AC |
40 | #include <ltdl.h> |
41 | ||
9abdcf1c EM |
42 | #ifndef LT_MODULE_EXT |
43 | # error "Charybdis requires loadable module support." | |
44 | #endif | |
45 | ||
212380e3 AC |
46 | struct module **modlist = NULL; |
47 | ||
48 | static const char *core_module_table[] = { | |
431a1a27 | 49 | "m_ban", |
212380e3 AC |
50 | "m_die", |
51 | "m_error", | |
52 | "m_join", | |
53 | "m_kick", | |
54 | "m_kill", | |
55 | "m_message", | |
56 | "m_mode", | |
78946542 | 57 | "m_modules", |
212380e3 AC |
58 | "m_nick", |
59 | "m_part", | |
60 | "m_quit", | |
61 | "m_server", | |
212380e3 AC |
62 | "m_squit", |
63 | NULL | |
64 | }; | |
65 | ||
81204be8 EM |
66 | #define MOD_WARN_DELTA (90 * 86400) /* time in seconds, 86400 seconds in a day */ |
67 | ||
212380e3 AC |
68 | #define MODS_INCREMENT 10 |
69 | int num_mods = 0; | |
70 | int max_mods = MODS_INCREMENT; | |
71 | ||
330fc5c1 | 72 | static rb_dlink_list mod_paths; |
212380e3 | 73 | |
212380e3 AC |
74 | void |
75 | modules_init(void) | |
76 | { | |
f272e7ab AC |
77 | if(lt_dlinit()) |
78 | { | |
79 | ilog(L_MAIN, "lt_dlinit failed"); | |
80 | exit(0); | |
81 | } | |
82 | ||
212380e3 | 83 | /* Add the default paths we look in to the module system --nenolod */ |
4d8cfacd AC |
84 | mod_add_path(ircd_paths[IRCD_PATH_MODULES]); |
85 | mod_add_path(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]); | |
212380e3 AC |
86 | } |
87 | ||
88 | /* mod_find_path() | |
89 | * | |
90 | * input - path | |
91 | * output - none | |
92 | * side effects - returns a module path from path | |
93 | */ | |
26c6ac3d | 94 | static char * |
212380e3 AC |
95 | mod_find_path(const char *path) |
96 | { | |
330fc5c1 | 97 | rb_dlink_node *ptr; |
26c6ac3d | 98 | char *mpath; |
212380e3 | 99 | |
5cefa1d6 | 100 | RB_DLINK_FOREACH(ptr, mod_paths.head) |
212380e3 AC |
101 | { |
102 | mpath = ptr->data; | |
103 | ||
26c6ac3d | 104 | if(!strcmp(path, mpath)) |
212380e3 AC |
105 | return mpath; |
106 | } | |
107 | ||
108 | return NULL; | |
109 | } | |
110 | ||
111 | /* mod_add_path | |
112 | * | |
113 | * input - path | |
55abcbb2 | 114 | * ouput - |
212380e3 AC |
115 | * side effects - adds path to list |
116 | */ | |
117 | void | |
118 | mod_add_path(const char *path) | |
119 | { | |
26c6ac3d | 120 | char *pathst; |
212380e3 AC |
121 | |
122 | if(mod_find_path(path)) | |
123 | return; | |
124 | ||
26c6ac3d | 125 | pathst = rb_strdup(path); |
330fc5c1 | 126 | rb_dlinkAddAlloc(pathst, &mod_paths); |
212380e3 AC |
127 | } |
128 | ||
129 | /* mod_clear_paths() | |
130 | * | |
131 | * input - | |
132 | * output - | |
133 | * side effects - clear the lists of paths | |
134 | */ | |
135 | void | |
136 | mod_clear_paths(void) | |
137 | { | |
637c4932 | 138 | rb_dlink_node *ptr, *next_ptr; |
212380e3 | 139 | |
637c4932 | 140 | RB_DLINK_FOREACH_SAFE(ptr, next_ptr, mod_paths.head) |
212380e3 | 141 | { |
637c4932 | 142 | rb_free(ptr->data); |
30106156 | 143 | rb_free_rb_dlink_node(ptr); |
212380e3 AC |
144 | } |
145 | ||
146 | mod_paths.head = mod_paths.tail = NULL; | |
147 | mod_paths.length = 0; | |
148 | } | |
149 | ||
212380e3 AC |
150 | /* findmodule_byname |
151 | * | |
aa483e55 EM |
152 | * input - module to load |
153 | * output - index of module on success, -1 on failure | |
154 | * side effects - none | |
212380e3 | 155 | */ |
212380e3 AC |
156 | int |
157 | findmodule_byname(const char *name) | |
158 | { | |
159 | int i; | |
c9108ea0 AC |
160 | char name_ext[PATH_MAX + 1]; |
161 | ||
dd92c99b AC |
162 | rb_strlcpy(name_ext, name, sizeof name_ext); |
163 | rb_strlcat(name_ext, LT_MODULE_EXT, sizeof name_ext); | |
212380e3 AC |
164 | |
165 | for (i = 0; i < num_mods; i++) | |
166 | { | |
167 | if(!irccmp(modlist[i]->name, name)) | |
168 | return i; | |
c9108ea0 AC |
169 | |
170 | if(!irccmp(modlist[i]->name, name_ext)) | |
171 | return i; | |
212380e3 | 172 | } |
c9108ea0 | 173 | |
212380e3 AC |
174 | return -1; |
175 | } | |
176 | ||
177 | /* load_all_modules() | |
178 | * | |
179 | * input - | |
180 | * output - | |
181 | * side effects - | |
182 | */ | |
183 | void | |
aa483e55 | 184 | load_all_modules(bool warn) |
212380e3 AC |
185 | { |
186 | DIR *system_module_dir = NULL; | |
187 | struct dirent *ldirent = NULL; | |
188 | char module_fq_name[PATH_MAX + 1]; | |
1db8a313 EM |
189 | size_t module_ext_len = strlen(LT_MODULE_EXT); |
190 | ||
212380e3 AC |
191 | modules_init(); |
192 | ||
1e170010 | 193 | modlist = (struct module **) rb_malloc(sizeof(struct module *) * (MODS_INCREMENT)); |
212380e3 AC |
194 | |
195 | max_mods = MODS_INCREMENT; | |
196 | ||
4d8cfacd | 197 | system_module_dir = opendir(ircd_paths[IRCD_PATH_AUTOLOAD_MODULES]); |
212380e3 AC |
198 | |
199 | if(system_module_dir == NULL) | |
200 | { | |
4d8cfacd | 201 | ilog(L_MAIN, "Could not load modules from %s: %s", ircd_paths[IRCD_PATH_AUTOLOAD_MODULES], strerror(errno)); |
212380e3 AC |
202 | return; |
203 | } | |
204 | ||
205 | while ((ldirent = readdir(system_module_dir)) != NULL) | |
206 | { | |
66031753 | 207 | size_t len = strlen(ldirent->d_name); |
9abdcf1c | 208 | |
66031753 | 209 | if(len > module_ext_len && |
f956cb0f | 210 | rb_strncasecmp(ldirent->d_name + (len - module_ext_len), LT_MODULE_EXT, module_ext_len) == 0) |
5203cba5 | 211 | { |
66031753 EM |
212 | (void) snprintf(module_fq_name, sizeof(module_fq_name), "%s%c%s", |
213 | ircd_paths[IRCD_PATH_AUTOLOAD_MODULES], RB_PATH_SEPARATOR, ldirent->d_name); | |
aa483e55 | 214 | (void) load_a_module(module_fq_name, warn, MAPI_ORIGIN_CORE, false); |
5203cba5 | 215 | } |
212380e3 AC |
216 | |
217 | } | |
218 | (void) closedir(system_module_dir); | |
219 | } | |
220 | ||
221 | /* load_core_modules() | |
222 | * | |
223 | * input - | |
224 | * output - | |
225 | * side effects - core modules are loaded, if any fail, kill ircd | |
226 | */ | |
227 | void | |
aa483e55 | 228 | load_core_modules(bool warn) |
212380e3 | 229 | { |
d74fa5b5 | 230 | char module_name[PATH_MAX]; |
212380e3 AC |
231 | int i; |
232 | ||
233 | ||
234 | for (i = 0; core_module_table[i]; i++) | |
235 | { | |
aa483e55 EM |
236 | snprintf(module_name, sizeof(module_name), "%s%c%s", ircd_paths[IRCD_PATH_MODULES], RB_PATH_SEPARATOR, |
237 | core_module_table[i]); | |
212380e3 | 238 | |
aa483e55 | 239 | if(load_a_module(module_name, warn, MAPI_ORIGIN_CORE, true) == false) |
212380e3 AC |
240 | { |
241 | ilog(L_MAIN, | |
9abdcf1c EM |
242 | "Error loading core module %s: terminating ircd", |
243 | core_module_table[i]); | |
212380e3 AC |
244 | exit(0); |
245 | } | |
246 | } | |
247 | } | |
248 | ||
249 | /* load_one_module() | |
250 | * | |
251 | * input - | |
252 | * output - | |
253 | * side effects - | |
254 | */ | |
aa483e55 EM |
255 | bool |
256 | load_one_module(const char *path, int origin, bool coremodule) | |
212380e3 | 257 | { |
d74fa5b5 | 258 | char modpath[PATH_MAX]; |
330fc5c1 | 259 | rb_dlink_node *pathst; |
212380e3 | 260 | |
503727d1 | 261 | if (server_state_foreground) |
55abcbb2 | 262 | inotice("loading module %s ...", path); |
212380e3 | 263 | |
aa483e55 | 264 | if(coremodule) |
216d70e9 | 265 | origin = MAPI_ORIGIN_CORE; |
216d70e9 | 266 | |
5cefa1d6 | 267 | RB_DLINK_FOREACH(pathst, mod_paths.head) |
212380e3 | 268 | { |
aa483e55 EM |
269 | struct stat statbuf; |
270 | const char *mpath = pathst->data; | |
212380e3 | 271 | |
aa483e55 | 272 | snprintf(modpath, sizeof(modpath), "%s%c%s%s", mpath, RB_PATH_SEPARATOR, path, LT_MODULE_EXT); |
212380e3 AC |
273 | if((strstr(modpath, "../") == NULL) && (strstr(modpath, "/..") == NULL)) |
274 | { | |
aa483e55 | 275 | if(stat(modpath, &statbuf) == 0 && S_ISREG(statbuf.st_mode)) |
212380e3 | 276 | { |
aa483e55 EM |
277 | /* Regular files only please */ |
278 | return load_a_module(modpath, true, origin, coremodule); | |
212380e3 AC |
279 | } |
280 | ||
281 | } | |
282 | } | |
283 | ||
284 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Cannot locate module %s", path); | |
aa483e55 | 285 | return false; |
212380e3 AC |
286 | } |
287 | ||
212380e3 AC |
288 | static void increase_modlist(void); |
289 | ||
290 | #define MODS_INCREMENT 10 | |
291 | ||
292 | static char unknown_ver[] = "<unknown>"; | |
8e9c6a75 | 293 | static char unknown_description[] = "<none>"; |
212380e3 | 294 | |
212380e3 AC |
295 | /* unload_one_module() |
296 | * | |
297 | * inputs - name of module to unload | |
aa483e55 EM |
298 | * - true to say modules unloaded, false to not |
299 | * output - true if successful, false if error | |
212380e3 AC |
300 | * side effects - module is unloaded |
301 | */ | |
aa483e55 EM |
302 | bool |
303 | unload_one_module(const char *name, bool warn) | |
212380e3 AC |
304 | { |
305 | int modindex; | |
306 | ||
307 | if((modindex = findmodule_byname(name)) == -1) | |
aa483e55 | 308 | return false; |
212380e3 AC |
309 | |
310 | /* | |
311 | ** XXX - The type system in C does not allow direct conversion between | |
312 | ** data and function pointers, but as it happens, most C compilers will | |
55abcbb2 KB |
313 | ** safely do this, however it is a theoretical overlow to cast as we |
314 | ** must do here. I have library functions to take care of this, but | |
315 | ** despite being more "correct" for the C language, this is more | |
212380e3 AC |
316 | ** practical. Removing the abuse of the ability to cast ANY pointer |
317 | ** to and from an integer value here will break some compilers. | |
318 | ** -jmallett | |
319 | */ | |
320 | /* Left the comment in but the code isn't here any more -larne */ | |
321 | switch (modlist[modindex]->mapi_version) | |
322 | { | |
323 | case 1: | |
324 | { | |
325 | struct mapi_mheader_av1 *mheader = modlist[modindex]->mapi_header; | |
326 | if(mheader->mapi_command_list) | |
327 | { | |
328 | struct Message **m; | |
329 | for (m = mheader->mapi_command_list; *m; ++m) | |
330 | mod_del_cmd(*m); | |
331 | } | |
332 | ||
333 | /* hook events are never removed, we simply lose the | |
334 | * ability to call them --fl | |
335 | */ | |
336 | if(mheader->mapi_hfn_list) | |
337 | { | |
338 | mapi_hfn_list_av1 *m; | |
339 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
340 | remove_hook(m->hapi_name, m->fn); | |
341 | } | |
342 | ||
343 | if(mheader->mapi_unregister) | |
344 | mheader->mapi_unregister(); | |
345 | break; | |
346 | } | |
8e9c6a75 EM |
347 | case 2: |
348 | { | |
349 | struct mapi_mheader_av2 *mheader = modlist[modindex]->mapi_header; | |
350 | ||
351 | /* XXX duplicate code :( */ | |
352 | if(mheader->mapi_command_list) | |
353 | { | |
354 | struct Message **m; | |
355 | for (m = mheader->mapi_command_list; *m; ++m) | |
356 | mod_del_cmd(*m); | |
357 | } | |
358 | ||
359 | /* hook events are never removed, we simply lose the | |
360 | * ability to call them --fl | |
361 | */ | |
362 | if(mheader->mapi_hfn_list) | |
363 | { | |
364 | mapi_hfn_list_av1 *m; | |
365 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
366 | remove_hook(m->hapi_name, m->fn); | |
367 | } | |
368 | ||
369 | if(mheader->mapi_unregister) | |
370 | mheader->mapi_unregister(); | |
371 | ||
372 | if(mheader->mapi_cap_list) | |
373 | { | |
374 | mapi_cap_list_av2 *m; | |
375 | for (m = mheader->mapi_cap_list; m->cap_name; ++m) | |
376 | { | |
377 | struct CapabilityIndex *idx; | |
378 | ||
978b7232 | 379 | switch (m->cap_index) |
8e9c6a75 EM |
380 | { |
381 | case MAPI_CAP_CLIENT: | |
382 | idx = cli_capindex; | |
383 | break; | |
384 | case MAPI_CAP_SERVER: | |
385 | idx = serv_capindex; | |
386 | break; | |
387 | default: | |
388 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
389 | "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s", | |
390 | m->cap_index, m->cap_name, modlist[modindex]->name); | |
391 | ilog(L_MAIN, | |
392 | "Unknown/unsupported CAP index found of type %d on capability %s when unloading %s", | |
393 | m->cap_index, m->cap_name, modlist[modindex]->name); | |
394 | continue; | |
395 | } | |
396 | ||
3256156a EM |
397 | if (m->cap_id != NULL) |
398 | { | |
399 | capability_orphan(idx, m->cap_name); | |
400 | sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * DEL :%s", me.name, m->cap_name); | |
401 | } | |
8e9c6a75 EM |
402 | } |
403 | } | |
404 | } | |
212380e3 AC |
405 | default: |
406 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
407 | "Unknown/unsupported MAPI version %d when unloading %s!", | |
408 | modlist[modindex]->mapi_version, modlist[modindex]->name); | |
409 | ilog(L_MAIN, "Unknown/unsupported MAPI version %d when unloading %s!", | |
410 | modlist[modindex]->mapi_version, modlist[modindex]->name); | |
411 | break; | |
412 | } | |
413 | ||
f272e7ab | 414 | lt_dlclose(modlist[modindex]->address); |
212380e3 | 415 | |
637c4932 | 416 | rb_free(modlist[modindex]->name); |
55d5f797 | 417 | rb_free(modlist[modindex]); |
f54e1a8f | 418 | memmove(&modlist[modindex], &modlist[modindex + 1], |
1e170010 | 419 | sizeof(struct module *) * ((num_mods - 1) - modindex)); |
212380e3 AC |
420 | |
421 | if(num_mods != 0) | |
422 | num_mods--; | |
423 | ||
aa483e55 | 424 | if(warn) |
212380e3 AC |
425 | { |
426 | ilog(L_MAIN, "Module %s unloaded", name); | |
427 | sendto_realops_snomask(SNO_GENERAL, L_ALL, "Module %s unloaded", name); | |
428 | } | |
429 | ||
aa483e55 | 430 | return true; |
212380e3 AC |
431 | } |
432 | ||
212380e3 AC |
433 | /* |
434 | * load_a_module() | |
435 | * | |
aa483e55 EM |
436 | * inputs - path name of module, bool to notice, int of origin, bool if core |
437 | * output - false if error true if success | |
212380e3 AC |
438 | * side effects - loads a module if successful |
439 | */ | |
aa483e55 EM |
440 | bool |
441 | load_a_module(const char *path, bool warn, int origin, bool core) | |
212380e3 | 442 | { |
f272e7ab | 443 | lt_dlhandle tmpptr; |
0a87075b | 444 | char *mod_displayname, *c; |
8e9c6a75 | 445 | const char *ver, *description = NULL; |
0a87075b | 446 | size_t module_ext_len = strlen(LT_MODULE_EXT); |
212380e3 AC |
447 | |
448 | int *mapi_version; | |
449 | ||
0a87075b EM |
450 | mod_displayname = rb_basename(path); |
451 | ||
452 | /* Trim off the ending for the display name if we have to */ | |
453 | if((c = rb_strcasestr(mod_displayname, LT_MODULE_EXT)) != NULL) | |
454 | *c = '\0'; | |
212380e3 | 455 | |
9abdcf1c | 456 | tmpptr = lt_dlopenext(path); |
212380e3 AC |
457 | |
458 | if(tmpptr == NULL) | |
459 | { | |
f272e7ab | 460 | const char *err = lt_dlerror(); |
212380e3 AC |
461 | |
462 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
0a87075b EM |
463 | "Error loading module %s: %s", mod_displayname, err); |
464 | ilog(L_MAIN, "Error loading module %s: %s", mod_displayname, err); | |
465 | rb_free(mod_displayname); | |
aa483e55 | 466 | return false; |
212380e3 AC |
467 | } |
468 | ||
212380e3 AC |
469 | /* |
470 | * _mheader is actually a struct mapi_mheader_*, but mapi_version | |
471 | * is always the first member of this structure, so we treate it | |
472 | * as a single int in order to determine the API version. | |
473 | * -larne. | |
474 | */ | |
f272e7ab | 475 | mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "_mheader"); |
212380e3 | 476 | if((mapi_version == NULL |
f272e7ab | 477 | && (mapi_version = (int *) (uintptr_t) lt_dlsym(tmpptr, "__mheader")) == NULL) |
212380e3 AC |
478 | || MAPI_MAGIC(*mapi_version) != MAPI_MAGIC_HDR) |
479 | { | |
480 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
481 | "Data format error: module %s has no MAPI header.", | |
0a87075b EM |
482 | mod_displayname); |
483 | ilog(L_MAIN, "Data format error: module %s has no MAPI header.", mod_displayname); | |
f272e7ab | 484 | (void) lt_dlclose(tmpptr); |
0a87075b | 485 | rb_free(mod_displayname); |
aa483e55 | 486 | return false; |
212380e3 AC |
487 | } |
488 | ||
489 | switch (MAPI_VERSION(*mapi_version)) | |
490 | { | |
491 | case 1: | |
492 | { | |
29c92cf9 | 493 | struct mapi_mheader_av1 *mheader = (struct mapi_mheader_av1 *)(void *)mapi_version; /* see above */ |
212380e3 AC |
494 | if(mheader->mapi_register && (mheader->mapi_register() == -1)) |
495 | { | |
496 | ilog(L_MAIN, "Module %s indicated failure during load.", | |
0a87075b | 497 | mod_displayname); |
212380e3 AC |
498 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
499 | "Module %s indicated failure during load.", | |
0a87075b | 500 | mod_displayname); |
f272e7ab | 501 | lt_dlclose(tmpptr); |
0a87075b | 502 | rb_free(mod_displayname); |
aa483e55 | 503 | return false; |
212380e3 AC |
504 | } |
505 | if(mheader->mapi_command_list) | |
506 | { | |
507 | struct Message **m; | |
508 | for (m = mheader->mapi_command_list; *m; ++m) | |
509 | mod_add_cmd(*m); | |
510 | } | |
511 | ||
512 | if(mheader->mapi_hook_list) | |
513 | { | |
514 | mapi_hlist_av1 *m; | |
515 | for (m = mheader->mapi_hook_list; m->hapi_name; ++m) | |
516 | *m->hapi_id = register_hook(m->hapi_name); | |
517 | } | |
518 | ||
519 | if(mheader->mapi_hfn_list) | |
520 | { | |
521 | mapi_hfn_list_av1 *m; | |
522 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
523 | add_hook(m->hapi_name, m->fn); | |
524 | } | |
525 | ||
526 | ver = mheader->mapi_module_version; | |
527 | break; | |
528 | } | |
8e9c6a75 EM |
529 | case 2: |
530 | { | |
531 | struct mapi_mheader_av2 *mheader = (struct mapi_mheader_av2 *)(void *)mapi_version; /* see above */ | |
212380e3 | 532 | |
8e9c6a75 | 533 | /* XXX duplicated code :( */ |
5eb3d7a7 | 534 | if(mheader->mapi_register && (mheader->mapi_register() == -1)) |
8e9c6a75 EM |
535 | { |
536 | ilog(L_MAIN, "Module %s indicated failure during load.", | |
0a87075b | 537 | mod_displayname); |
8e9c6a75 EM |
538 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
539 | "Module %s indicated failure during load.", | |
0a87075b | 540 | mod_displayname); |
8e9c6a75 | 541 | lt_dlclose(tmpptr); |
0a87075b | 542 | rb_free(mod_displayname); |
aa483e55 | 543 | return false; |
8e9c6a75 | 544 | } |
81204be8 EM |
545 | |
546 | /* Basic date code checks | |
547 | * | |
548 | * Don't make them fatal, but do complain about differences within a certain time frame. | |
549 | * Later on if there are major API changes we can add fatal checks. | |
550 | * -- Elizafox | |
551 | */ | |
552 | if(mheader->mapi_datecode != datecode && mheader->mapi_datecode > 0) | |
553 | { | |
3089f59c | 554 | long int delta = datecode - mheader->mapi_datecode; |
81204be8 EM |
555 | if (delta > MOD_WARN_DELTA) |
556 | { | |
557 | delta /= 86400; | |
881acf00 | 558 | iwarn("Module %s build date is out of sync with ircd build date by %ld days, expect problems", |
0a87075b | 559 | mod_displayname, delta); |
81204be8 EM |
560 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
561 | "Module %s build date is out of sync with ircd build date by %ld days, expect problems", | |
0a87075b | 562 | mod_displayname, delta); |
81204be8 EM |
563 | } |
564 | } | |
565 | ||
8e9c6a75 EM |
566 | if(mheader->mapi_command_list) |
567 | { | |
568 | struct Message **m; | |
569 | for (m = mheader->mapi_command_list; *m; ++m) | |
570 | mod_add_cmd(*m); | |
571 | } | |
572 | ||
573 | if(mheader->mapi_hook_list) | |
574 | { | |
575 | mapi_hlist_av1 *m; | |
576 | for (m = mheader->mapi_hook_list; m->hapi_name; ++m) | |
577 | *m->hapi_id = register_hook(m->hapi_name); | |
578 | } | |
579 | ||
580 | if(mheader->mapi_hfn_list) | |
581 | { | |
582 | mapi_hfn_list_av1 *m; | |
583 | for (m = mheader->mapi_hfn_list; m->hapi_name; ++m) | |
584 | add_hook(m->hapi_name, m->fn); | |
585 | } | |
586 | ||
587 | /* New in MAPI v2 - version replacement */ | |
588 | ver = mheader->mapi_module_version ? mheader->mapi_module_version : ircd_version; | |
589 | description = mheader->mapi_module_description; | |
590 | ||
591 | if(mheader->mapi_cap_list) | |
592 | { | |
593 | mapi_cap_list_av2 *m; | |
594 | for (m = mheader->mapi_cap_list; m->cap_name; ++m) | |
595 | { | |
596 | struct CapabilityIndex *idx; | |
597 | int result; | |
598 | ||
978b7232 | 599 | switch (m->cap_index) |
8e9c6a75 EM |
600 | { |
601 | case MAPI_CAP_CLIENT: | |
602 | idx = cli_capindex; | |
603 | break; | |
604 | case MAPI_CAP_SERVER: | |
605 | idx = serv_capindex; | |
606 | break; | |
607 | default: | |
608 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
609 | "Unknown/unsupported CAP index found of type %d on capability %s when loading %s", | |
0a87075b | 610 | m->cap_index, m->cap_name, mod_displayname); |
8e9c6a75 EM |
611 | ilog(L_MAIN, |
612 | "Unknown/unsupported CAP index found of type %d on capability %s when loading %s", | |
0a87075b | 613 | m->cap_index, m->cap_name, mod_displayname); |
8e9c6a75 EM |
614 | continue; |
615 | } | |
616 | ||
617 | result = capability_put(idx, m->cap_name, m->cap_ownerdata); | |
618 | if (m->cap_id != NULL) | |
3256156a | 619 | { |
8e9c6a75 | 620 | *(m->cap_id) = result; |
3256156a EM |
621 | sendto_local_clients_with_capability(CLICAP_CAP_NOTIFY, ":%s CAP * ADD :%s", me.name, m->cap_name); |
622 | } | |
8e9c6a75 EM |
623 | } |
624 | } | |
625 | } | |
0e5bf029 EM |
626 | |
627 | break; | |
212380e3 AC |
628 | default: |
629 | ilog(L_MAIN, "Module %s has unknown/unsupported MAPI version %d.", | |
0a87075b | 630 | mod_displayname, MAPI_VERSION(*mapi_version)); |
212380e3 AC |
631 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
632 | "Module %s has unknown/unsupported MAPI version %d.", | |
0a87075b | 633 | mod_displayname, *mapi_version); |
f272e7ab | 634 | lt_dlclose(tmpptr); |
0a87075b | 635 | rb_free(mod_displayname); |
aa483e55 | 636 | return false; |
212380e3 AC |
637 | } |
638 | ||
639 | if(ver == NULL) | |
640 | ver = unknown_ver; | |
641 | ||
8e9c6a75 EM |
642 | if(description == NULL) |
643 | description = unknown_description; | |
644 | ||
212380e3 AC |
645 | increase_modlist(); |
646 | ||
eddc2ab6 | 647 | modlist[num_mods] = rb_malloc(sizeof(struct module)); |
212380e3 AC |
648 | modlist[num_mods]->address = tmpptr; |
649 | modlist[num_mods]->version = ver; | |
0eb7d9c0 | 650 | modlist[num_mods]->description = description; |
212380e3 | 651 | modlist[num_mods]->core = core; |
0a87075b | 652 | modlist[num_mods]->name = rb_strdup(mod_displayname); |
212380e3 AC |
653 | modlist[num_mods]->mapi_header = mapi_version; |
654 | modlist[num_mods]->mapi_version = MAPI_VERSION(*mapi_version); | |
c63aeb44 | 655 | modlist[num_mods]->origin = origin; |
212380e3 AC |
656 | num_mods++; |
657 | ||
aa483e55 | 658 | if(warn) |
212380e3 | 659 | { |
c63aeb44 EM |
660 | const char *o; |
661 | ||
978b7232 | 662 | switch (origin) |
c63aeb44 | 663 | { |
c63aeb44 EM |
664 | case MAPI_ORIGIN_EXTENSION: |
665 | o = "extension"; | |
666 | break; | |
667 | case MAPI_ORIGIN_CORE: | |
668 | o = "core"; | |
669 | break; | |
670 | default: | |
671 | o = "unknown"; | |
672 | break; | |
673 | } | |
674 | ||
212380e3 | 675 | sendto_realops_snomask(SNO_GENERAL, L_ALL, |
02831b6f | 676 | "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p", |
0a87075b | 677 | mod_displayname, ver, MAPI_VERSION(*mapi_version), o, description, |
02831b6f AC |
678 | (void *) tmpptr); |
679 | ilog(L_MAIN, "Module %s [version: %s; MAPI version: %d; origin: %s; description: \"%s\"] loaded at %p", | |
0a87075b | 680 | mod_displayname, ver, MAPI_VERSION(*mapi_version), o, description, (void *) tmpptr); |
212380e3 | 681 | } |
0a87075b | 682 | rb_free(mod_displayname); |
aa483e55 | 683 | return true; |
212380e3 AC |
684 | } |
685 | ||
686 | /* | |
687 | * increase_modlist | |
688 | * | |
689 | * inputs - NONE | |
690 | * output - NONE | |
691 | * side effects - expand the size of modlist if necessary | |
692 | */ | |
693 | static void | |
694 | increase_modlist(void) | |
695 | { | |
696 | struct module **new_modlist = NULL; | |
697 | ||
698 | if((num_mods + 1) < max_mods) | |
699 | return; | |
700 | ||
1e170010 | 701 | new_modlist = (struct module **) rb_malloc(sizeof(struct module *) * |
212380e3 | 702 | (max_mods + MODS_INCREMENT)); |
1e170010 | 703 | memcpy((void *) new_modlist, (void *) modlist, sizeof(struct module *) * num_mods); |
212380e3 | 704 | |
637c4932 | 705 | rb_free(modlist); |
212380e3 AC |
706 | modlist = new_modlist; |
707 | max_mods += MODS_INCREMENT; | |
708 | } |