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());
129 lprintf(CTDL_INFO, "%s\n", serv_postfix_tcpdict());
130 lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
135 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
138 struct CleanupFunctionHook *newfcn;
140 newfcn = (struct CleanupFunctionHook *)
141 malloc(sizeof(struct CleanupFunctionHook));
142 newfcn->next = CleanupHookTable;
143 newfcn->h_function_pointer = fcn_ptr;
144 CleanupHookTable = newfcn;
146 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
150 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
152 struct CleanupFunctionHook *cur, *p;
154 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
155 /* This will also remove duplicates if any */
156 while (cur != NULL &&
157 fcn_ptr == cur->h_function_pointer) {
158 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
160 if (cur == CleanupHookTable) {
161 CleanupHookTable = p;
170 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
173 struct SessionFunctionHook *newfcn;
175 newfcn = (struct SessionFunctionHook *)
176 malloc(sizeof(struct SessionFunctionHook));
177 newfcn->next = SessionHookTable;
178 newfcn->h_function_pointer = fcn_ptr;
179 newfcn->eventtype = EventType;
180 SessionHookTable = newfcn;
182 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
187 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
189 struct SessionFunctionHook *cur, *p;
191 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
192 /* This will also remove duplicates if any */
193 while (cur != NULL &&
194 fcn_ptr == cur->h_function_pointer &&
195 EventType == cur->eventtype) {
196 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
199 if (cur == SessionHookTable) {
200 SessionHookTable = p;
209 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
212 struct UserFunctionHook *newfcn;
214 newfcn = (struct UserFunctionHook *)
215 malloc(sizeof(struct UserFunctionHook));
216 newfcn->next = UserHookTable;
217 newfcn->h_function_pointer = fcn_ptr;
218 newfcn->eventtype = EventType;
219 UserHookTable = newfcn;
221 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
226 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
228 struct UserFunctionHook *cur, *p;
230 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
231 /* This will also remove duplicates if any */
232 while (cur != NULL &&
233 fcn_ptr == cur->h_function_pointer &&
234 EventType == cur->eventtype) {
235 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
238 if (cur == UserHookTable) {
248 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
252 struct MessageFunctionHook *newfcn;
254 newfcn = (struct MessageFunctionHook *)
255 malloc(sizeof(struct MessageFunctionHook));
256 newfcn->next = MessageHookTable;
257 newfcn->h_function_pointer = handler;
258 newfcn->eventtype = EventType;
259 MessageHookTable = newfcn;
261 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
266 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
269 struct MessageFunctionHook *cur, *p;
271 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
272 /* This will also remove duplicates if any */
273 while (cur != NULL &&
274 handler == cur->h_function_pointer &&
275 EventType == cur->eventtype) {
276 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
279 if (cur == MessageHookTable) {
280 MessageHookTable = p;
289 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
291 struct NetprocFunctionHook *newfcn;
293 newfcn = (struct NetprocFunctionHook *)
294 malloc(sizeof(struct NetprocFunctionHook));
295 newfcn->next = NetprocHookTable;
296 newfcn->h_function_pointer = handler;
297 NetprocHookTable = newfcn;
299 lprintf(CTDL_INFO, "Registered a new netproc function\n");
303 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
305 struct NetprocFunctionHook *cur, *p;
307 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
308 /* This will also remove duplicates if any */
309 while (cur != NULL &&
310 handler == cur->h_function_pointer ) {
311 lprintf(CTDL_INFO, "Unregistered netproc function\n");
313 if (cur == NetprocHookTable) {
314 NetprocHookTable = p;
323 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
325 struct DeleteFunctionHook *newfcn;
327 newfcn = (struct DeleteFunctionHook *)
328 malloc(sizeof(struct DeleteFunctionHook));
329 newfcn->next = DeleteHookTable;
330 newfcn->h_function_pointer = handler;
331 DeleteHookTable = newfcn;
333 lprintf(CTDL_INFO, "Registered a new netproc function\n");
337 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
339 struct DeleteFunctionHook *cur, *p;
341 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
342 /* This will also remove duplicates if any */
343 while (cur != NULL &&
344 handler == cur->h_function_pointer ) {
345 lprintf(CTDL_INFO, "Unregistered netproc function\n");
347 if (cur == DeleteHookTable) {
359 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
361 struct FixedOutputHook *newfcn;
363 newfcn = (struct FixedOutputHook *)
364 malloc(sizeof(struct FixedOutputHook));
365 newfcn->next = FixedOutputTable;
366 newfcn->h_function_pointer = handler;
367 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
368 FixedOutputTable = newfcn;
370 lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
374 void CtdlUnregisterFixedOutputHook(char *content_type)
376 struct FixedOutputHook *cur, *p;
378 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
379 /* This will also remove duplicates if any */
380 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
381 lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
383 if (cur == FixedOutputTable) {
384 FixedOutputTable = p;
392 /* returns nonzero if we found a hook and used it */
393 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
395 struct FixedOutputHook *fcn;
397 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
398 if (!strcasecmp(content_type, fcn->content_type)) {
399 (*fcn->h_function_pointer) (content, content_length);
410 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
413 struct XmsgFunctionHook *newfcn;
415 newfcn = (struct XmsgFunctionHook *)
416 malloc(sizeof(struct XmsgFunctionHook));
417 newfcn->next = XmsgHookTable;
418 newfcn->order = order;
419 newfcn->h_function_pointer = fcn_ptr;
420 XmsgHookTable = newfcn;
421 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
425 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
427 struct XmsgFunctionHook *cur, *p;
429 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
430 /* This will also remove duplicates if any */
431 while (cur != NULL &&
432 fcn_ptr == cur->h_function_pointer &&
433 order == cur->order) {
434 lprintf(CTDL_INFO, "Unregistered x-msg function "
435 "(priority %d)\n", order);
437 if (cur == XmsgHookTable) {
447 void CtdlRegisterServiceHook(int tcp_port,
449 void (*h_greeting_function) (void),
450 void (*h_command_function) (void),
451 void (*h_async_function) (void)
454 struct ServiceFunctionHook *newfcn;
457 newfcn = (struct ServiceFunctionHook *)
458 malloc(sizeof(struct ServiceFunctionHook));
459 newfcn->next = ServiceHookTable;
460 newfcn->tcp_port = tcp_port;
461 newfcn->sockpath = sockpath;
462 newfcn->h_greeting_function = h_greeting_function;
463 newfcn->h_command_function = h_command_function;
464 newfcn->h_async_function = h_async_function;
466 if (sockpath != NULL) {
467 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
468 snprintf(message, sizeof message, "Unix domain socket '%s': ", sockpath);
470 else if (tcp_port <= 0) { /* port -1 to disable */
471 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
476 newfcn->msock = ig_tcp_server(config.c_ip_addr,
478 config.c_maxsessions);
479 snprintf(message, sizeof message, "TCP port %d: ", tcp_port);
482 if (newfcn->msock > 0) {
483 ServiceHookTable = newfcn;
484 strcat(message, "registered.");
485 lprintf(CTDL_INFO, "%s\n", message);
488 strcat(message, "FAILED.");
489 lprintf(CTDL_CRIT, "%s\n", message);
495 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
496 void (*h_greeting_function) (void),
497 void (*h_command_function) (void),
498 void (*h_async_function) (void)
501 struct ServiceFunctionHook *cur, *p;
503 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
504 /* This will also remove duplicates if any */
505 while (cur != NULL &&
506 !(sockpath && cur->sockpath &&
507 strcmp(sockpath, cur->sockpath)) &&
508 h_greeting_function == cur->h_greeting_function &&
509 h_command_function == cur->h_command_function &&
510 h_async_function == cur->h_async_function &&
511 tcp_port == cur->tcp_port) {
514 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
516 } else if (tcp_port) {
517 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
519 lprintf(CTDL_INFO, "Unregistered unknown service\n");
522 if (cur == ServiceHookTable) {
523 ServiceHookTable = p;
532 void PerformSessionHooks(int EventType)
534 struct SessionFunctionHook *fcn;
536 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
537 if (fcn->eventtype == EventType) {
538 (*fcn->h_function_pointer) ();
543 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
545 struct UserFunctionHook *fcn;
547 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
548 if (fcn->eventtype == EventType) {
549 (*fcn->h_function_pointer) (usbuf);
554 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
556 struct MessageFunctionHook *fcn;
557 int total_retval = 0;
559 /* Other code may elect to protect this message from server-side
560 * handlers; if this is the case, don't do anything.
561 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
562 EventType, msg->cm_flags);
564 if (msg->cm_flags & CM_SKIP_HOOKS) {
565 lprintf(CTDL_DEBUG, "Skipping hooks\n");
569 /* Otherwise, run all the hooks appropriate to this event type.
571 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
572 if (fcn->eventtype == EventType) {
573 total_retval = total_retval +
574 (*fcn->h_function_pointer) (msg);
578 /* Return the sum of the return codes from the hook functions. If
579 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
580 * the save operation to abort.
587 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
589 struct NetprocFunctionHook *fcn;
590 int total_retval = 0;
592 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
593 total_retval = total_retval +
594 (*fcn->h_function_pointer) (msg, target_room);
597 /* Return the sum of the return codes from the hook functions.
598 * A nonzero return code will cause the message to *not* be imported.
604 void PerformDeleteHooks(char *room, long msgnum)
606 struct DeleteFunctionHook *fcn;
608 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
609 (*fcn->h_function_pointer) (room, msgnum);
617 int PerformXmsgHooks(char *sender, char *recp, char *msg)
619 struct XmsgFunctionHook *fcn;
623 for (p=0; p<MAX_XMSG_PRI; ++p) {
624 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
625 if (fcn->order == p) {
627 (*fcn->h_function_pointer)
631 /* Break out of the loop if a higher-priority function
632 * successfully delivered the message. This prevents duplicate
633 * deliveries to local users simultaneously signed onto
636 if (total_sent) break;