4 * Citadel Dynamic Loading Module
5 * Written by Brian Costello <btx@calyx.net>
13 #include <sys/types.h>
20 #include "serv_extensions.h"
21 #include "sysdep_decls.h"
31 struct CleanupFunctionHook *CleanupHookTable = NULL;
32 struct SessionFunctionHook *SessionHookTable = NULL;
33 struct UserFunctionHook *UserHookTable = NULL;
34 struct XmsgFunctionHook *XmsgHookTable = NULL;
35 struct MessageFunctionHook *MessageHookTable = NULL;
36 struct NetprocFunctionHook *NetprocHookTable = NULL;
37 struct DeleteFunctionHook *DeleteHookTable = NULL;
38 struct ServiceFunctionHook *ServiceHookTable = NULL;
40 struct ProtoFunctionHook {
41 void (*handler) (char *cmdbuf);
44 struct ProtoFunctionHook *next;
45 } *ProtoHookList = NULL;
47 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
49 struct ProtoFunctionHook *p;
51 p = (struct ProtoFunctionHook *)
52 malloc(sizeof(struct ProtoFunctionHook));
55 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
61 p->next = ProtoHookList;
63 lprintf(CTDL_INFO, "Registered server command %s (%s)\n", cmd, desc);
67 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
69 struct ProtoFunctionHook *cur, *p;
71 for (cur = ProtoHookList; cur != NULL; cur = cur->next) {
72 /* This will also remove duplicates if any */
74 handler == cur->handler &&
75 !strcmp(cmd, cur->cmd)) {
76 lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
79 if (cur == ProtoHookList) {
89 int DLoader_Exec_Cmd(char *cmdbuf)
91 struct ProtoFunctionHook *p;
93 for (p = ProtoHookList; p; p = p->next) {
94 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
95 p->handler(&cmdbuf[5]);
102 void initialize_server_extensions(void)
105 serv_calendar_init();
114 serv_netfilter_init();
122 /* serv_test_init(); */
124 serv_vandelay_init();
130 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
133 struct CleanupFunctionHook *newfcn;
135 newfcn = (struct CleanupFunctionHook *)
136 malloc(sizeof(struct CleanupFunctionHook));
137 newfcn->next = CleanupHookTable;
138 newfcn->h_function_pointer = fcn_ptr;
139 CleanupHookTable = newfcn;
141 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
145 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
147 struct CleanupFunctionHook *cur, *p;
149 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
150 /* This will also remove duplicates if any */
151 while (cur != NULL &&
152 fcn_ptr == cur->h_function_pointer) {
153 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
155 if (cur == CleanupHookTable) {
156 CleanupHookTable = p;
165 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
168 struct SessionFunctionHook *newfcn;
170 newfcn = (struct SessionFunctionHook *)
171 malloc(sizeof(struct SessionFunctionHook));
172 newfcn->next = SessionHookTable;
173 newfcn->h_function_pointer = fcn_ptr;
174 newfcn->eventtype = EventType;
175 SessionHookTable = newfcn;
177 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
182 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
184 struct SessionFunctionHook *cur, *p;
186 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
187 /* This will also remove duplicates if any */
188 while (cur != NULL &&
189 fcn_ptr == cur->h_function_pointer &&
190 EventType == cur->eventtype) {
191 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
194 if (cur == SessionHookTable) {
195 SessionHookTable = p;
204 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
207 struct UserFunctionHook *newfcn;
209 newfcn = (struct UserFunctionHook *)
210 malloc(sizeof(struct UserFunctionHook));
211 newfcn->next = UserHookTable;
212 newfcn->h_function_pointer = fcn_ptr;
213 newfcn->eventtype = EventType;
214 UserHookTable = newfcn;
216 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
221 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
223 struct UserFunctionHook *cur, *p;
225 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
226 /* This will also remove duplicates if any */
227 while (cur != NULL &&
228 fcn_ptr == cur->h_function_pointer &&
229 EventType == cur->eventtype) {
230 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
233 if (cur == UserHookTable) {
243 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
247 struct MessageFunctionHook *newfcn;
249 newfcn = (struct MessageFunctionHook *)
250 malloc(sizeof(struct MessageFunctionHook));
251 newfcn->next = MessageHookTable;
252 newfcn->h_function_pointer = handler;
253 newfcn->eventtype = EventType;
254 MessageHookTable = newfcn;
256 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
261 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
264 struct MessageFunctionHook *cur, *p;
266 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
267 /* This will also remove duplicates if any */
268 while (cur != NULL &&
269 handler == cur->h_function_pointer &&
270 EventType == cur->eventtype) {
271 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
274 if (cur == MessageHookTable) {
275 MessageHookTable = p;
284 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
286 struct NetprocFunctionHook *newfcn;
288 newfcn = (struct NetprocFunctionHook *)
289 malloc(sizeof(struct NetprocFunctionHook));
290 newfcn->next = NetprocHookTable;
291 newfcn->h_function_pointer = handler;
292 NetprocHookTable = newfcn;
294 lprintf(CTDL_INFO, "Registered a new netproc function\n");
298 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
300 struct NetprocFunctionHook *cur, *p;
302 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
303 /* This will also remove duplicates if any */
304 while (cur != NULL &&
305 handler == cur->h_function_pointer ) {
306 lprintf(CTDL_INFO, "Unregistered netproc function\n");
308 if (cur == NetprocHookTable) {
309 NetprocHookTable = p;
318 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
320 struct DeleteFunctionHook *newfcn;
322 newfcn = (struct DeleteFunctionHook *)
323 malloc(sizeof(struct DeleteFunctionHook));
324 newfcn->next = DeleteHookTable;
325 newfcn->h_function_pointer = handler;
326 DeleteHookTable = newfcn;
328 lprintf(CTDL_INFO, "Registered a new netproc function\n");
332 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
334 struct DeleteFunctionHook *cur, *p;
336 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
337 /* This will also remove duplicates if any */
338 while (cur != NULL &&
339 handler == cur->h_function_pointer ) {
340 lprintf(CTDL_INFO, "Unregistered netproc function\n");
342 if (cur == DeleteHookTable) {
352 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
355 struct XmsgFunctionHook *newfcn;
357 newfcn = (struct XmsgFunctionHook *)
358 malloc(sizeof(struct XmsgFunctionHook));
359 newfcn->next = XmsgHookTable;
360 newfcn->order = order;
361 newfcn->h_function_pointer = fcn_ptr;
362 XmsgHookTable = newfcn;
363 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
367 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
369 struct XmsgFunctionHook *cur, *p;
371 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
372 /* This will also remove duplicates if any */
373 while (cur != NULL &&
374 fcn_ptr == cur->h_function_pointer &&
375 order == cur->order) {
376 lprintf(CTDL_INFO, "Unregistered x-msg function "
377 "(priority %d)\n", order);
379 if (cur == XmsgHookTable) {
389 void CtdlRegisterServiceHook(int tcp_port,
391 void (*h_greeting_function) (void),
392 void (*h_command_function) (void),
393 void (*h_async_function) (void)
396 struct ServiceFunctionHook *newfcn;
399 newfcn = (struct ServiceFunctionHook *)
400 malloc(sizeof(struct ServiceFunctionHook));
401 newfcn->next = ServiceHookTable;
402 newfcn->tcp_port = tcp_port;
403 newfcn->sockpath = sockpath;
404 newfcn->h_greeting_function = h_greeting_function;
405 newfcn->h_command_function = h_command_function;
406 newfcn->h_async_function = h_async_function;
408 if (sockpath != NULL) {
409 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
410 snprintf(message, sizeof message, "Unix domain socket '%s': ", sockpath);
412 else if (tcp_port <= 0) { /* port -1 to disable */
413 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
418 newfcn->msock = ig_tcp_server(config.c_ip_addr,
420 config.c_maxsessions);
421 snprintf(message, sizeof message, "TCP port %d: ", tcp_port);
424 if (newfcn->msock > 0) {
425 ServiceHookTable = newfcn;
426 strcat(message, "registered.");
427 lprintf(CTDL_INFO, "%s\n", message);
430 strcat(message, "FAILED.");
431 lprintf(CTDL_CRIT, "%s\n", message);
437 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
438 void (*h_greeting_function) (void),
439 void (*h_command_function) (void),
440 void (*h_async_function) (void)
443 struct ServiceFunctionHook *cur, *p;
445 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
446 /* This will also remove duplicates if any */
447 while (cur != NULL &&
448 !(sockpath && cur->sockpath &&
449 strcmp(sockpath, cur->sockpath)) &&
450 h_greeting_function == cur->h_greeting_function &&
451 h_command_function == cur->h_command_function &&
452 h_async_function == cur->h_async_function &&
453 tcp_port == cur->tcp_port) {
456 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
458 } else if (tcp_port) {
459 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
461 lprintf(CTDL_INFO, "Unregistered unknown service\n");
464 if (cur == ServiceHookTable) {
465 ServiceHookTable = p;
474 void PerformSessionHooks(int EventType)
476 struct SessionFunctionHook *fcn;
478 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
479 if (fcn->eventtype == EventType) {
480 (*fcn->h_function_pointer) ();
485 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
487 struct UserFunctionHook *fcn;
489 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
490 if (fcn->eventtype == EventType) {
491 (*fcn->h_function_pointer) (usbuf);
496 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
498 struct MessageFunctionHook *fcn;
499 int total_retval = 0;
501 /* Other code may elect to protect this message from server-side
502 * handlers; if this is the case, don't do anything.
503 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
504 EventType, msg->cm_flags);
506 if (msg->cm_flags & CM_SKIP_HOOKS) {
507 lprintf(CTDL_DEBUG, "Skipping hooks\n");
511 /* Otherwise, run all the hooks appropriate to this event type.
513 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
514 if (fcn->eventtype == EventType) {
515 total_retval = total_retval +
516 (*fcn->h_function_pointer) (msg);
520 /* Return the sum of the return codes from the hook functions. If
521 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
522 * the save operation to abort.
529 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
531 struct NetprocFunctionHook *fcn;
532 int total_retval = 0;
534 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
535 total_retval = total_retval +
536 (*fcn->h_function_pointer) (msg, target_room);
539 /* Return the sum of the return codes from the hook functions.
540 * A nonzero return code will cause the message to *not* be imported.
546 void PerformDeleteHooks(char *room, long msgnum)
548 struct DeleteFunctionHook *fcn;
550 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
551 (*fcn->h_function_pointer) (room, msgnum);
557 int PerformXmsgHooks(char *sender, char *recp, char *msg)
559 struct XmsgFunctionHook *fcn;
563 for (p=0; p<MAX_XMSG_PRI; ++p) {
564 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
565 if (fcn->order == p) {
567 (*fcn->h_function_pointer)
571 /* Break out of the loop if a higher-priority function
572 * successfully delivered the message. This prevents duplicate
573 * deliveries to local users simultaneously signed onto
576 if (total_sent) break;