4 * Citadel Dynamic Loading Module
5 * Written by Brian Costello <btx@calyx.net>
24 #include <sys/types.h>
32 #include "dynloader.h"
33 #include "sysdep_decls.h"
43 struct LogFunctionHook *LogHookTable = NULL;
44 struct CleanupFunctionHook *CleanupHookTable = NULL;
45 struct SessionFunctionHook *SessionHookTable = NULL;
46 struct UserFunctionHook *UserHookTable = NULL;
47 struct XmsgFunctionHook *XmsgHookTable = NULL;
48 struct MessageFunctionHook *MessageHookTable = NULL;
49 struct NetprocFunctionHook *NetprocHookTable = NULL;
50 struct ServiceFunctionHook *ServiceHookTable = NULL;
52 struct ProtoFunctionHook {
53 void (*handler) (char *cmdbuf);
56 struct ProtoFunctionHook *next;
57 } *ProtoHookList = NULL;
59 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
61 struct ProtoFunctionHook *p;
63 p = (struct ProtoFunctionHook *)
64 mallok(sizeof(struct ProtoFunctionHook));
67 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
73 p->next = ProtoHookList;
75 lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
79 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
81 struct ProtoFunctionHook *cur, *p;
83 for (cur = ProtoHookList; cur != NULL; cur = cur->next) {
84 /* This will also remove duplicates if any */
86 handler == cur->handler &&
87 !strcmp(cmd, cur->cmd)) {
88 lprintf(5, "Unregistered server command %s (%s)\n",
91 if (cur == ProtoHookList) {
101 int DLoader_Exec_Cmd(char *cmdbuf)
103 struct ProtoFunctionHook *p;
105 for (p = ProtoHookList; p; p = p->next) {
106 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
107 p->handler(&cmdbuf[5]);
114 void DLoader_Init(char *pathname)
121 char *(*h_init_fcn) (void);
124 char pathbuf[PATH_MAX];
126 if ((dir = opendir(pathname)) == NULL) {
130 while ((dptr = readdir(dir)) != NULL) {
131 if (strlen(dptr->d_name) < 4)
134 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-3], ".so"))
136 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-4], ".dll"))
140 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
141 lprintf(7, "Initializing %s...\n", pathbuf);
144 if (!(fcn_handle = dlopen(pathbuf, RTLD_LAZY)))
146 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
149 safestrncpy(dl_error, dlerror(), sizeof dl_error);
150 for (i=0; i<strlen(dl_error); ++i)
151 if (!isprint(dl_error[i]))
153 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
157 h_init_fcn = (char * (*)(void))
159 dlsym(fcn_handle, "Dynamic_Module_Init");
161 dlsym(fcn_handle, "_Dynamic_Module_Init");
164 if (dlerror() != NULL) {
165 fprintf(stderr, "DLoader_Init dlsym failed\n");
168 dl_info = h_init_fcn();
170 lprintf(3, "Loaded module: %s\n", dl_info);
176 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
179 struct LogFunctionHook *newfcn;
181 newfcn = (struct LogFunctionHook *)
182 mallok(sizeof(struct LogFunctionHook));
183 newfcn->next = LogHookTable;
184 newfcn->h_function_pointer = fcn_ptr;
185 newfcn->loglevel = loglevel;
186 LogHookTable = newfcn;
188 lprintf(5, "Registered a new logging function\n");
192 void CtdlUnregisterLogHook(void (*fcn_ptr) (char *), int loglevel)
194 struct LogFunctionHook *cur, *p;
196 for (cur = LogHookTable; cur != NULL; cur = cur->next) {
197 /* This will also remove duplicates if any */
198 while (cur != NULL &&
199 fcn_ptr == cur->h_function_pointer &&
200 loglevel == cur->loglevel) {
201 lprintf(5, "Unregistered logging function\n");
203 if (cur == LogHookTable) {
213 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
216 struct CleanupFunctionHook *newfcn;
218 newfcn = (struct CleanupFunctionHook *)
219 mallok(sizeof(struct CleanupFunctionHook));
220 newfcn->next = CleanupHookTable;
221 newfcn->h_function_pointer = fcn_ptr;
222 CleanupHookTable = newfcn;
224 lprintf(5, "Registered a new cleanup function\n");
228 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
230 struct CleanupFunctionHook *cur, *p;
232 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
233 /* This will also remove duplicates if any */
234 while (cur != NULL &&
235 fcn_ptr == cur->h_function_pointer) {
236 lprintf(5, "Unregistered cleanup function\n");
238 if (cur == CleanupHookTable) {
239 CleanupHookTable = p;
248 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
251 struct SessionFunctionHook *newfcn;
253 newfcn = (struct SessionFunctionHook *)
254 mallok(sizeof(struct SessionFunctionHook));
255 newfcn->next = SessionHookTable;
256 newfcn->h_function_pointer = fcn_ptr;
257 newfcn->eventtype = EventType;
258 SessionHookTable = newfcn;
260 lprintf(5, "Registered a new session function (type %d)\n",
265 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
267 struct SessionFunctionHook *cur, *p;
269 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
270 /* This will also remove duplicates if any */
271 while (cur != NULL &&
272 fcn_ptr == cur->h_function_pointer &&
273 EventType == cur->eventtype) {
274 lprintf(5, "Unregistered session function (type %d)\n",
277 if (cur == SessionHookTable) {
278 SessionHookTable = p;
287 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
290 struct UserFunctionHook *newfcn;
292 newfcn = (struct UserFunctionHook *)
293 mallok(sizeof(struct UserFunctionHook));
294 newfcn->next = UserHookTable;
295 newfcn->h_function_pointer = fcn_ptr;
296 newfcn->eventtype = EventType;
297 UserHookTable = newfcn;
299 lprintf(5, "Registered a new user function (type %d)\n",
304 void CtdlUnregisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
306 struct UserFunctionHook *cur, *p;
308 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
309 /* This will also remove duplicates if any */
310 while (cur != NULL &&
311 fcn_ptr == cur->h_function_pointer &&
312 EventType == cur->eventtype) {
313 lprintf(5, "Unregistered user function (type %d)\n",
316 if (cur == UserHookTable) {
326 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
330 struct MessageFunctionHook *newfcn;
332 newfcn = (struct MessageFunctionHook *)
333 mallok(sizeof(struct MessageFunctionHook));
334 newfcn->next = MessageHookTable;
335 newfcn->h_function_pointer = handler;
336 newfcn->eventtype = EventType;
337 MessageHookTable = newfcn;
339 lprintf(5, "Registered a new message function (type %d)\n",
344 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
347 struct MessageFunctionHook *cur, *p;
349 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
350 /* This will also remove duplicates if any */
351 while (cur != NULL &&
352 handler == cur->h_function_pointer &&
353 EventType == cur->eventtype) {
354 lprintf(5, "Unregistered message function (type %d)\n",
357 if (cur == MessageHookTable) {
358 MessageHookTable = p;
367 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
369 struct NetprocFunctionHook *newfcn;
371 newfcn = (struct NetprocFunctionHook *)
372 mallok(sizeof(struct NetprocFunctionHook));
373 newfcn->next = NetprocHookTable;
374 newfcn->h_function_pointer = handler;
375 NetprocHookTable = newfcn;
377 lprintf(5, "Registered a new netproc function\n");
381 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
383 struct NetprocFunctionHook *cur, *p;
385 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
386 /* This will also remove duplicates if any */
387 while (cur != NULL &&
388 handler == cur->h_function_pointer ) {
389 lprintf(5, "Unregistered netproc function\n");
391 if (cur == NetprocHookTable) {
392 NetprocHookTable = p;
401 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
404 struct XmsgFunctionHook *newfcn;
406 newfcn = (struct XmsgFunctionHook *)
407 mallok(sizeof(struct XmsgFunctionHook));
408 newfcn->next = XmsgHookTable;
409 newfcn->order = order;
410 newfcn->h_function_pointer = fcn_ptr;
411 XmsgHookTable = newfcn;
412 lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
416 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
418 struct XmsgFunctionHook *cur, *p;
420 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
421 /* This will also remove duplicates if any */
422 while (cur != NULL &&
423 fcn_ptr == cur->h_function_pointer &&
424 order == cur->order) {
425 lprintf(5, "Unregistered x-msg function "
426 "(priority %d)\n", order);
428 if (cur == XmsgHookTable) {
438 void CtdlRegisterServiceHook(int tcp_port,
440 void (*h_greeting_function) (void),
441 void (*h_command_function) (void) )
443 struct ServiceFunctionHook *newfcn;
446 newfcn = (struct ServiceFunctionHook *)
447 mallok(sizeof(struct ServiceFunctionHook));
448 newfcn->next = ServiceHookTable;
449 newfcn->tcp_port = tcp_port;
450 newfcn->sockpath = sockpath;
451 newfcn->h_greeting_function = h_greeting_function;
452 newfcn->h_command_function = h_command_function;
454 if (sockpath != NULL) {
455 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
456 sprintf(message, "Unix domain socket '%s': ", sockpath);
458 else if (tcp_port <= 0) { /* port -1 to disable */
459 lprintf(7, "Service has been manually disabled, skipping\n");
464 newfcn->msock = ig_tcp_server(tcp_port, config.c_maxsessions);
465 sprintf(message, "TCP port %d: ", tcp_port);
468 if (newfcn->msock > 0) {
469 ServiceHookTable = newfcn;
470 strcat(message, "registered.");
471 lprintf(5, "%s\n", message);
474 strcat(message, "FAILED.");
475 lprintf(2, "%s\n", message);
481 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
482 void (*h_greeting_function) (void),
483 void (*h_command_function) (void) )
485 struct ServiceFunctionHook *cur, *p;
487 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
488 /* This will also remove duplicates if any */
489 while (cur != NULL &&
490 !(sockpath && cur->sockpath &&
491 strcmp(sockpath, cur->sockpath)) &&
492 h_greeting_function == cur->h_greeting_function &&
493 h_command_function == cur->h_command_function &&
494 tcp_port == cur->tcp_port) {
497 lprintf(5, "Closed UNIX domain socket %s\n",
499 } else if (tcp_port) {
500 lprintf(5, "Closed TCP port %d\n", tcp_port);
502 lprintf(5, "Unregistered unknown service\n");
505 if (cur == ServiceHookTable) {
506 ServiceHookTable = p;
515 void PerformSessionHooks(int EventType)
517 struct SessionFunctionHook *fcn;
519 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
520 if (fcn->eventtype == EventType) {
521 (*fcn->h_function_pointer) ();
526 void PerformLogHooks(int loglevel, char *logmsg)
528 struct LogFunctionHook *fcn;
530 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
531 if (fcn->loglevel >= loglevel) {
532 (*fcn->h_function_pointer) (logmsg);
537 void PerformUserHooks(char *username, long usernum, int EventType)
539 struct UserFunctionHook *fcn;
541 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
542 if (fcn->eventtype == EventType) {
543 (*fcn->h_function_pointer) (username, usernum);
548 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
550 struct MessageFunctionHook *fcn;
551 int total_retval = 0;
553 /* Other code may elect to protect this message from server-side
554 * handlers; if this is the case, don't do anything.
555 lprintf(9, "** Event type is %d, flags are %d\n",
556 EventType, msg->cm_flags);
558 if (msg->cm_flags & CM_SKIP_HOOKS) {
559 lprintf(9, "Skipping hooks\n");
563 /* Otherwise, run all the hooks appropriate to this event type.
565 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
566 if (fcn->eventtype == EventType) {
567 total_retval = total_retval +
568 (*fcn->h_function_pointer) (msg);
572 /* Return the sum of the return codes from the hook functions. If
573 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
574 * the save operation to abort.
581 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
583 struct NetprocFunctionHook *fcn;
584 int total_retval = 0;
586 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
587 total_retval = total_retval +
588 (*fcn->h_function_pointer) (msg, target_room);
591 /* Return the sum of the return codes from the hook functions.
592 * A nonzero return code will cause the message to *not* be imported.
599 int PerformXmsgHooks(char *sender, char *recp, char *msg)
601 struct XmsgFunctionHook *fcn;
605 for (p=0; p<MAX_XMSG_PRI; ++p) {
606 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
607 if (fcn->order == p) {
609 (*fcn->h_function_pointer)
613 /* Break out of the loop if a higher-priority function
614 * successfully delivered the message. This prevents duplicate
615 * deliveries to local users simultaneously signed onto
618 if (total_sent) break;