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_sieve_init());
131 lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
136 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
139 struct CleanupFunctionHook *newfcn;
141 newfcn = (struct CleanupFunctionHook *)
142 malloc(sizeof(struct CleanupFunctionHook));
143 newfcn->next = CleanupHookTable;
144 newfcn->h_function_pointer = fcn_ptr;
145 CleanupHookTable = newfcn;
147 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
151 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
153 struct CleanupFunctionHook *cur, *p;
155 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
156 /* This will also remove duplicates if any */
157 while (cur != NULL &&
158 fcn_ptr == cur->h_function_pointer) {
159 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
161 if (cur == CleanupHookTable) {
162 CleanupHookTable = p;
171 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
174 struct SessionFunctionHook *newfcn;
176 newfcn = (struct SessionFunctionHook *)
177 malloc(sizeof(struct SessionFunctionHook));
178 newfcn->next = SessionHookTable;
179 newfcn->h_function_pointer = fcn_ptr;
180 newfcn->eventtype = EventType;
181 SessionHookTable = newfcn;
183 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
188 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
190 struct SessionFunctionHook *cur, *p;
192 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
193 /* This will also remove duplicates if any */
194 while (cur != NULL &&
195 fcn_ptr == cur->h_function_pointer &&
196 EventType == cur->eventtype) {
197 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
200 if (cur == SessionHookTable) {
201 SessionHookTable = p;
210 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
213 struct UserFunctionHook *newfcn;
215 newfcn = (struct UserFunctionHook *)
216 malloc(sizeof(struct UserFunctionHook));
217 newfcn->next = UserHookTable;
218 newfcn->h_function_pointer = fcn_ptr;
219 newfcn->eventtype = EventType;
220 UserHookTable = newfcn;
222 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
227 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
229 struct UserFunctionHook *cur, *p;
231 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
232 /* This will also remove duplicates if any */
233 while (cur != NULL &&
234 fcn_ptr == cur->h_function_pointer &&
235 EventType == cur->eventtype) {
236 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
239 if (cur == UserHookTable) {
249 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
253 struct MessageFunctionHook *newfcn;
255 newfcn = (struct MessageFunctionHook *)
256 malloc(sizeof(struct MessageFunctionHook));
257 newfcn->next = MessageHookTable;
258 newfcn->h_function_pointer = handler;
259 newfcn->eventtype = EventType;
260 MessageHookTable = newfcn;
262 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
267 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
270 struct MessageFunctionHook *cur, *p;
272 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
273 /* This will also remove duplicates if any */
274 while (cur != NULL &&
275 handler == cur->h_function_pointer &&
276 EventType == cur->eventtype) {
277 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
280 if (cur == MessageHookTable) {
281 MessageHookTable = p;
290 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
292 struct NetprocFunctionHook *newfcn;
294 newfcn = (struct NetprocFunctionHook *)
295 malloc(sizeof(struct NetprocFunctionHook));
296 newfcn->next = NetprocHookTable;
297 newfcn->h_function_pointer = handler;
298 NetprocHookTable = newfcn;
300 lprintf(CTDL_INFO, "Registered a new netproc function\n");
304 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
306 struct NetprocFunctionHook *cur, *p;
308 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
309 /* This will also remove duplicates if any */
310 while (cur != NULL &&
311 handler == cur->h_function_pointer ) {
312 lprintf(CTDL_INFO, "Unregistered netproc function\n");
314 if (cur == NetprocHookTable) {
315 NetprocHookTable = p;
324 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
326 struct DeleteFunctionHook *newfcn;
328 newfcn = (struct DeleteFunctionHook *)
329 malloc(sizeof(struct DeleteFunctionHook));
330 newfcn->next = DeleteHookTable;
331 newfcn->h_function_pointer = handler;
332 DeleteHookTable = newfcn;
334 lprintf(CTDL_INFO, "Registered a new netproc function\n");
338 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
340 struct DeleteFunctionHook *cur, *p;
342 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
343 /* This will also remove duplicates if any */
344 while (cur != NULL &&
345 handler == cur->h_function_pointer ) {
346 lprintf(CTDL_INFO, "Unregistered netproc function\n");
348 if (cur == DeleteHookTable) {
360 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
362 struct FixedOutputHook *newfcn;
364 newfcn = (struct FixedOutputHook *)
365 malloc(sizeof(struct FixedOutputHook));
366 newfcn->next = FixedOutputTable;
367 newfcn->h_function_pointer = handler;
368 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
369 FixedOutputTable = newfcn;
371 lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
375 void CtdlUnregisterFixedOutputHook(char *content_type)
377 struct FixedOutputHook *cur, *p;
379 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
380 /* This will also remove duplicates if any */
381 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
382 lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
384 if (cur == FixedOutputTable) {
385 FixedOutputTable = p;
393 /* returns nonzero if we found a hook and used it */
394 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
396 struct FixedOutputHook *fcn;
398 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
399 if (!strcasecmp(content_type, fcn->content_type)) {
400 (*fcn->h_function_pointer) (content, content_length);
411 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
414 struct XmsgFunctionHook *newfcn;
416 newfcn = (struct XmsgFunctionHook *)
417 malloc(sizeof(struct XmsgFunctionHook));
418 newfcn->next = XmsgHookTable;
419 newfcn->order = order;
420 newfcn->h_function_pointer = fcn_ptr;
421 XmsgHookTable = newfcn;
422 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
426 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
428 struct XmsgFunctionHook *cur, *p;
430 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
431 /* This will also remove duplicates if any */
432 while (cur != NULL &&
433 fcn_ptr == cur->h_function_pointer &&
434 order == cur->order) {
435 lprintf(CTDL_INFO, "Unregistered x-msg function "
436 "(priority %d)\n", order);
438 if (cur == XmsgHookTable) {
448 void CtdlRegisterServiceHook(int tcp_port,
450 void (*h_greeting_function) (void),
451 void (*h_command_function) (void),
452 void (*h_async_function) (void)
455 struct ServiceFunctionHook *newfcn;
458 newfcn = (struct ServiceFunctionHook *)
459 malloc(sizeof(struct ServiceFunctionHook));
460 newfcn->next = ServiceHookTable;
461 newfcn->tcp_port = tcp_port;
462 newfcn->sockpath = sockpath;
463 newfcn->h_greeting_function = h_greeting_function;
464 newfcn->h_command_function = h_command_function;
465 newfcn->h_async_function = h_async_function;
467 if (sockpath != NULL) {
468 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
469 snprintf(message, sizeof message, "Unix domain socket '%s': ", sockpath);
471 else if (tcp_port <= 0) { /* port -1 to disable */
472 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
477 newfcn->msock = ig_tcp_server(config.c_ip_addr,
479 config.c_maxsessions);
480 snprintf(message, sizeof message, "TCP port %d: ", tcp_port);
483 if (newfcn->msock > 0) {
484 ServiceHookTable = newfcn;
485 strcat(message, "registered.");
486 lprintf(CTDL_INFO, "%s\n", message);
489 strcat(message, "FAILED.");
490 lprintf(CTDL_CRIT, "%s\n", message);
496 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
497 void (*h_greeting_function) (void),
498 void (*h_command_function) (void),
499 void (*h_async_function) (void)
502 struct ServiceFunctionHook *cur, *p;
504 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
505 /* This will also remove duplicates if any */
506 while (cur != NULL &&
507 !(sockpath && cur->sockpath &&
508 strcmp(sockpath, cur->sockpath)) &&
509 h_greeting_function == cur->h_greeting_function &&
510 h_command_function == cur->h_command_function &&
511 h_async_function == cur->h_async_function &&
512 tcp_port == cur->tcp_port) {
515 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
517 } else if (tcp_port) {
518 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
520 lprintf(CTDL_INFO, "Unregistered unknown service\n");
523 if (cur == ServiceHookTable) {
524 ServiceHookTable = p;
533 void PerformSessionHooks(int EventType)
535 struct SessionFunctionHook *fcn;
537 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
538 if (fcn->eventtype == EventType) {
539 (*fcn->h_function_pointer) ();
544 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
546 struct UserFunctionHook *fcn;
548 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
549 if (fcn->eventtype == EventType) {
550 (*fcn->h_function_pointer) (usbuf);
555 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
557 struct MessageFunctionHook *fcn;
558 int total_retval = 0;
560 /* Other code may elect to protect this message from server-side
561 * handlers; if this is the case, don't do anything.
562 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
563 EventType, msg->cm_flags);
565 if (msg->cm_flags & CM_SKIP_HOOKS) {
566 lprintf(CTDL_DEBUG, "Skipping hooks\n");
570 /* Otherwise, run all the hooks appropriate to this event type.
572 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
573 if (fcn->eventtype == EventType) {
574 total_retval = total_retval +
575 (*fcn->h_function_pointer) (msg);
579 /* Return the sum of the return codes from the hook functions. If
580 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
581 * the save operation to abort.
588 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
590 struct NetprocFunctionHook *fcn;
591 int total_retval = 0;
593 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
594 total_retval = total_retval +
595 (*fcn->h_function_pointer) (msg, target_room);
598 /* Return the sum of the return codes from the hook functions.
599 * A nonzero return code will cause the message to *not* be imported.
605 void PerformDeleteHooks(char *room, long msgnum)
607 struct DeleteFunctionHook *fcn;
609 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
610 (*fcn->h_function_pointer) (room, msgnum);
618 int PerformXmsgHooks(char *sender, char *recp, char *msg)
620 struct XmsgFunctionHook *fcn;
624 for (p=0; p<MAX_XMSG_PRI; ++p) {
625 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
626 if (fcn->order == p) {
628 (*fcn->h_function_pointer)
632 /* Break out of the loop if a higher-priority function
633 * successfully delivered the message. This prevents duplicate
634 * deliveries to local users simultaneously signed onto
637 if (total_sent) break;