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;
39 struct FixedOutputHook *FixedOutputTable = NULL;
41 struct ProtoFunctionHook {
42 void (*handler) (char *cmdbuf);
45 struct ProtoFunctionHook *next;
46 } *ProtoHookList = NULL;
48 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
50 struct ProtoFunctionHook *p;
52 p = (struct ProtoFunctionHook *)
53 malloc(sizeof(struct ProtoFunctionHook));
56 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
62 p->next = ProtoHookList;
64 lprintf(CTDL_INFO, "Registered server command %s (%s)\n", cmd, desc);
68 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
70 struct ProtoFunctionHook *cur, *p;
72 for (cur = ProtoHookList; cur != NULL; cur = cur->next) {
73 /* This will also remove duplicates if any */
75 handler == cur->handler &&
76 !strcmp(cmd, cur->cmd)) {
77 lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
80 if (cur == ProtoHookList) {
90 int DLoader_Exec_Cmd(char *cmdbuf)
92 struct ProtoFunctionHook *p;
94 for (p = ProtoHookList; p; p = p->next) {
95 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
96 p->handler(&cmdbuf[5]);
103 void initialize_server_extensions(void)
105 lprintf(CTDL_INFO, "%s\n", serv_bio_init());
106 lprintf(CTDL_INFO, "%s\n", serv_calendar_init());
107 lprintf(CTDL_INFO, "%s\n", serv_notes_init());
108 lprintf(CTDL_INFO, "%s\n", serv_ldap_init());
109 lprintf(CTDL_INFO, "%s\n", serv_chat_init());
110 lprintf(CTDL_INFO, "%s\n", serv_expire_init());
111 lprintf(CTDL_INFO, "%s\n", serv_imap_init());
112 lprintf(CTDL_INFO, "%s\n", serv_upgrade_init());
113 lprintf(CTDL_INFO, "%s\n", serv_inetcfg_init());
114 lprintf(CTDL_INFO, "%s\n", serv_listsub_init());
115 lprintf(CTDL_INFO, "%s\n", serv_mrtg_init());
116 lprintf(CTDL_INFO, "%s\n", serv_netfilter_init());
117 lprintf(CTDL_INFO, "%s\n", serv_network_init());
118 lprintf(CTDL_INFO, "%s\n", serv_newuser_init());
119 lprintf(CTDL_INFO, "%s\n", serv_pas2_init());
120 lprintf(CTDL_INFO, "%s\n", serv_pop3_init());
121 lprintf(CTDL_INFO, "%s\n", serv_rwho_init());
122 lprintf(CTDL_INFO, "%s\n", serv_smtp_init());
123 lprintf(CTDL_INFO, "%s\n", serv_spam_init());
124 /* lprintf(CTDL_INFO, "%s\n", serv_test_init()); */
125 lprintf(CTDL_INFO, "%s\n", serv_vandelay_init());
126 lprintf(CTDL_INFO, "%s\n", serv_vcard_init());
127 lprintf(CTDL_INFO, "%s\n", serv_fulltext_init());
128 lprintf(CTDL_INFO, "%s\n", serv_autocompletion_init());
133 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
136 struct CleanupFunctionHook *newfcn;
138 newfcn = (struct CleanupFunctionHook *)
139 malloc(sizeof(struct CleanupFunctionHook));
140 newfcn->next = CleanupHookTable;
141 newfcn->h_function_pointer = fcn_ptr;
142 CleanupHookTable = newfcn;
144 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
148 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
150 struct CleanupFunctionHook *cur, *p;
152 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
153 /* This will also remove duplicates if any */
154 while (cur != NULL &&
155 fcn_ptr == cur->h_function_pointer) {
156 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
158 if (cur == CleanupHookTable) {
159 CleanupHookTable = p;
168 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
171 struct SessionFunctionHook *newfcn;
173 newfcn = (struct SessionFunctionHook *)
174 malloc(sizeof(struct SessionFunctionHook));
175 newfcn->next = SessionHookTable;
176 newfcn->h_function_pointer = fcn_ptr;
177 newfcn->eventtype = EventType;
178 SessionHookTable = newfcn;
180 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
185 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
187 struct SessionFunctionHook *cur, *p;
189 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
190 /* This will also remove duplicates if any */
191 while (cur != NULL &&
192 fcn_ptr == cur->h_function_pointer &&
193 EventType == cur->eventtype) {
194 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
197 if (cur == SessionHookTable) {
198 SessionHookTable = p;
207 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
210 struct UserFunctionHook *newfcn;
212 newfcn = (struct UserFunctionHook *)
213 malloc(sizeof(struct UserFunctionHook));
214 newfcn->next = UserHookTable;
215 newfcn->h_function_pointer = fcn_ptr;
216 newfcn->eventtype = EventType;
217 UserHookTable = newfcn;
219 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
224 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
226 struct UserFunctionHook *cur, *p;
228 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
229 /* This will also remove duplicates if any */
230 while (cur != NULL &&
231 fcn_ptr == cur->h_function_pointer &&
232 EventType == cur->eventtype) {
233 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
236 if (cur == UserHookTable) {
246 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
250 struct MessageFunctionHook *newfcn;
252 newfcn = (struct MessageFunctionHook *)
253 malloc(sizeof(struct MessageFunctionHook));
254 newfcn->next = MessageHookTable;
255 newfcn->h_function_pointer = handler;
256 newfcn->eventtype = EventType;
257 MessageHookTable = newfcn;
259 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
264 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
267 struct MessageFunctionHook *cur, *p;
269 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
270 /* This will also remove duplicates if any */
271 while (cur != NULL &&
272 handler == cur->h_function_pointer &&
273 EventType == cur->eventtype) {
274 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
277 if (cur == MessageHookTable) {
278 MessageHookTable = p;
287 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
289 struct NetprocFunctionHook *newfcn;
291 newfcn = (struct NetprocFunctionHook *)
292 malloc(sizeof(struct NetprocFunctionHook));
293 newfcn->next = NetprocHookTable;
294 newfcn->h_function_pointer = handler;
295 NetprocHookTable = newfcn;
297 lprintf(CTDL_INFO, "Registered a new netproc function\n");
301 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
303 struct NetprocFunctionHook *cur, *p;
305 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
306 /* This will also remove duplicates if any */
307 while (cur != NULL &&
308 handler == cur->h_function_pointer ) {
309 lprintf(CTDL_INFO, "Unregistered netproc function\n");
311 if (cur == NetprocHookTable) {
312 NetprocHookTable = p;
321 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
323 struct DeleteFunctionHook *newfcn;
325 newfcn = (struct DeleteFunctionHook *)
326 malloc(sizeof(struct DeleteFunctionHook));
327 newfcn->next = DeleteHookTable;
328 newfcn->h_function_pointer = handler;
329 DeleteHookTable = newfcn;
331 lprintf(CTDL_INFO, "Registered a new netproc function\n");
335 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
337 struct DeleteFunctionHook *cur, *p;
339 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
340 /* This will also remove duplicates if any */
341 while (cur != NULL &&
342 handler == cur->h_function_pointer ) {
343 lprintf(CTDL_INFO, "Unregistered netproc function\n");
345 if (cur == DeleteHookTable) {
357 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
359 struct FixedOutputHook *newfcn;
361 newfcn = (struct FixedOutputHook *)
362 malloc(sizeof(struct FixedOutputHook));
363 newfcn->next = FixedOutputTable;
364 newfcn->h_function_pointer = handler;
365 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
366 FixedOutputTable = newfcn;
368 lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
372 void CtdlUnregisterFixedOutputHook(char *content_type)
374 struct FixedOutputHook *cur, *p;
376 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
377 /* This will also remove duplicates if any */
378 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
379 lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
381 if (cur == FixedOutputTable) {
382 FixedOutputTable = p;
390 /* returns nonzero if we found a hook and used it */
391 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
393 struct FixedOutputHook *fcn;
395 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
396 if (!strcasecmp(content_type, fcn->content_type)) {
397 (*fcn->h_function_pointer) (content, content_length);
408 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
411 struct XmsgFunctionHook *newfcn;
413 newfcn = (struct XmsgFunctionHook *)
414 malloc(sizeof(struct XmsgFunctionHook));
415 newfcn->next = XmsgHookTable;
416 newfcn->order = order;
417 newfcn->h_function_pointer = fcn_ptr;
418 XmsgHookTable = newfcn;
419 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
423 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
425 struct XmsgFunctionHook *cur, *p;
427 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
428 /* This will also remove duplicates if any */
429 while (cur != NULL &&
430 fcn_ptr == cur->h_function_pointer &&
431 order == cur->order) {
432 lprintf(CTDL_INFO, "Unregistered x-msg function "
433 "(priority %d)\n", order);
435 if (cur == XmsgHookTable) {
445 void CtdlRegisterServiceHook(int tcp_port,
447 void (*h_greeting_function) (void),
448 void (*h_command_function) (void),
449 void (*h_async_function) (void)
452 struct ServiceFunctionHook *newfcn;
455 newfcn = (struct ServiceFunctionHook *)
456 malloc(sizeof(struct ServiceFunctionHook));
457 newfcn->next = ServiceHookTable;
458 newfcn->tcp_port = tcp_port;
459 newfcn->sockpath = sockpath;
460 newfcn->h_greeting_function = h_greeting_function;
461 newfcn->h_command_function = h_command_function;
462 newfcn->h_async_function = h_async_function;
464 if (sockpath != NULL) {
465 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
466 snprintf(message, sizeof message, "Unix domain socket '%s': ", sockpath);
468 else if (tcp_port <= 0) { /* port -1 to disable */
469 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
474 newfcn->msock = ig_tcp_server(config.c_ip_addr,
476 config.c_maxsessions);
477 snprintf(message, sizeof message, "TCP port %d: ", tcp_port);
480 if (newfcn->msock > 0) {
481 ServiceHookTable = newfcn;
482 strcat(message, "registered.");
483 lprintf(CTDL_INFO, "%s\n", message);
486 strcat(message, "FAILED.");
487 lprintf(CTDL_CRIT, "%s\n", message);
493 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
494 void (*h_greeting_function) (void),
495 void (*h_command_function) (void),
496 void (*h_async_function) (void)
499 struct ServiceFunctionHook *cur, *p;
501 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
502 /* This will also remove duplicates if any */
503 while (cur != NULL &&
504 !(sockpath && cur->sockpath &&
505 strcmp(sockpath, cur->sockpath)) &&
506 h_greeting_function == cur->h_greeting_function &&
507 h_command_function == cur->h_command_function &&
508 h_async_function == cur->h_async_function &&
509 tcp_port == cur->tcp_port) {
512 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
514 } else if (tcp_port) {
515 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
517 lprintf(CTDL_INFO, "Unregistered unknown service\n");
520 if (cur == ServiceHookTable) {
521 ServiceHookTable = p;
530 void PerformSessionHooks(int EventType)
532 struct SessionFunctionHook *fcn;
534 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
535 if (fcn->eventtype == EventType) {
536 (*fcn->h_function_pointer) ();
541 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
543 struct UserFunctionHook *fcn;
545 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
546 if (fcn->eventtype == EventType) {
547 (*fcn->h_function_pointer) (usbuf);
552 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
554 struct MessageFunctionHook *fcn;
555 int total_retval = 0;
557 /* Other code may elect to protect this message from server-side
558 * handlers; if this is the case, don't do anything.
559 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
560 EventType, msg->cm_flags);
562 if (msg->cm_flags & CM_SKIP_HOOKS) {
563 lprintf(CTDL_DEBUG, "Skipping hooks\n");
567 /* Otherwise, run all the hooks appropriate to this event type.
569 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
570 if (fcn->eventtype == EventType) {
571 total_retval = total_retval +
572 (*fcn->h_function_pointer) (msg);
576 /* Return the sum of the return codes from the hook functions. If
577 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
578 * the save operation to abort.
585 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
587 struct NetprocFunctionHook *fcn;
588 int total_retval = 0;
590 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
591 total_retval = total_retval +
592 (*fcn->h_function_pointer) (msg, target_room);
595 /* Return the sum of the return codes from the hook functions.
596 * A nonzero return code will cause the message to *not* be imported.
602 void PerformDeleteHooks(char *room, long msgnum)
604 struct DeleteFunctionHook *fcn;
606 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
607 (*fcn->h_function_pointer) (room, msgnum);
615 int PerformXmsgHooks(char *sender, char *recp, char *msg)
617 struct XmsgFunctionHook *fcn;
621 for (p=0; p<MAX_XMSG_PRI; ++p) {
622 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
623 if (fcn->order == p) {
625 (*fcn->h_function_pointer)
629 /* Break out of the loop if a higher-priority function
630 * successfully delivered the message. This prevents duplicate
631 * deliveries to local users simultaneously signed onto
634 if (total_sent) break;