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