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