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());
131 lprintf(CTDL_INFO, "%s\n", "FIXME do serv_libsieve_init() here");
133 lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
138 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
141 struct CleanupFunctionHook *newfcn;
143 newfcn = (struct CleanupFunctionHook *)
144 malloc(sizeof(struct CleanupFunctionHook));
145 newfcn->next = CleanupHookTable;
146 newfcn->h_function_pointer = fcn_ptr;
147 CleanupHookTable = newfcn;
149 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
153 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
155 struct CleanupFunctionHook *cur, *p;
157 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
158 /* This will also remove duplicates if any */
159 while (cur != NULL &&
160 fcn_ptr == cur->h_function_pointer) {
161 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
163 if (cur == CleanupHookTable) {
164 CleanupHookTable = p;
173 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
176 struct SessionFunctionHook *newfcn;
178 newfcn = (struct SessionFunctionHook *)
179 malloc(sizeof(struct SessionFunctionHook));
180 newfcn->next = SessionHookTable;
181 newfcn->h_function_pointer = fcn_ptr;
182 newfcn->eventtype = EventType;
183 SessionHookTable = newfcn;
185 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
190 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
192 struct SessionFunctionHook *cur, *p;
194 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
195 /* This will also remove duplicates if any */
196 while (cur != NULL &&
197 fcn_ptr == cur->h_function_pointer &&
198 EventType == cur->eventtype) {
199 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
202 if (cur == SessionHookTable) {
203 SessionHookTable = p;
212 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
215 struct UserFunctionHook *newfcn;
217 newfcn = (struct UserFunctionHook *)
218 malloc(sizeof(struct UserFunctionHook));
219 newfcn->next = UserHookTable;
220 newfcn->h_function_pointer = fcn_ptr;
221 newfcn->eventtype = EventType;
222 UserHookTable = newfcn;
224 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
229 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
231 struct UserFunctionHook *cur, *p;
233 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
234 /* This will also remove duplicates if any */
235 while (cur != NULL &&
236 fcn_ptr == cur->h_function_pointer &&
237 EventType == cur->eventtype) {
238 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
241 if (cur == UserHookTable) {
251 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
255 struct MessageFunctionHook *newfcn;
257 newfcn = (struct MessageFunctionHook *)
258 malloc(sizeof(struct MessageFunctionHook));
259 newfcn->next = MessageHookTable;
260 newfcn->h_function_pointer = handler;
261 newfcn->eventtype = EventType;
262 MessageHookTable = newfcn;
264 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
269 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
272 struct MessageFunctionHook *cur, *p;
274 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
275 /* This will also remove duplicates if any */
276 while (cur != NULL &&
277 handler == cur->h_function_pointer &&
278 EventType == cur->eventtype) {
279 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
282 if (cur == MessageHookTable) {
283 MessageHookTable = p;
292 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
294 struct NetprocFunctionHook *newfcn;
296 newfcn = (struct NetprocFunctionHook *)
297 malloc(sizeof(struct NetprocFunctionHook));
298 newfcn->next = NetprocHookTable;
299 newfcn->h_function_pointer = handler;
300 NetprocHookTable = newfcn;
302 lprintf(CTDL_INFO, "Registered a new netproc function\n");
306 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
308 struct NetprocFunctionHook *cur, *p;
310 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
311 /* This will also remove duplicates if any */
312 while (cur != NULL &&
313 handler == cur->h_function_pointer ) {
314 lprintf(CTDL_INFO, "Unregistered netproc function\n");
316 if (cur == NetprocHookTable) {
317 NetprocHookTable = p;
326 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
328 struct DeleteFunctionHook *newfcn;
330 newfcn = (struct DeleteFunctionHook *)
331 malloc(sizeof(struct DeleteFunctionHook));
332 newfcn->next = DeleteHookTable;
333 newfcn->h_function_pointer = handler;
334 DeleteHookTable = newfcn;
336 lprintf(CTDL_INFO, "Registered a new netproc function\n");
340 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
342 struct DeleteFunctionHook *cur, *p;
344 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
345 /* This will also remove duplicates if any */
346 while (cur != NULL &&
347 handler == cur->h_function_pointer ) {
348 lprintf(CTDL_INFO, "Unregistered netproc function\n");
350 if (cur == DeleteHookTable) {
362 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
364 struct FixedOutputHook *newfcn;
366 newfcn = (struct FixedOutputHook *)
367 malloc(sizeof(struct FixedOutputHook));
368 newfcn->next = FixedOutputTable;
369 newfcn->h_function_pointer = handler;
370 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
371 FixedOutputTable = newfcn;
373 lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
377 void CtdlUnregisterFixedOutputHook(char *content_type)
379 struct FixedOutputHook *cur, *p;
381 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
382 /* This will also remove duplicates if any */
383 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
384 lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
386 if (cur == FixedOutputTable) {
387 FixedOutputTable = p;
395 /* returns nonzero if we found a hook and used it */
396 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
398 struct FixedOutputHook *fcn;
400 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
401 if (!strcasecmp(content_type, fcn->content_type)) {
402 (*fcn->h_function_pointer) (content, content_length);
413 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
416 struct XmsgFunctionHook *newfcn;
418 newfcn = (struct XmsgFunctionHook *)
419 malloc(sizeof(struct XmsgFunctionHook));
420 newfcn->next = XmsgHookTable;
421 newfcn->order = order;
422 newfcn->h_function_pointer = fcn_ptr;
423 XmsgHookTable = newfcn;
424 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
428 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
430 struct XmsgFunctionHook *cur, *p;
432 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
433 /* This will also remove duplicates if any */
434 while (cur != NULL &&
435 fcn_ptr == cur->h_function_pointer &&
436 order == cur->order) {
437 lprintf(CTDL_INFO, "Unregistered x-msg function "
438 "(priority %d)\n", order);
440 if (cur == XmsgHookTable) {
450 void CtdlRegisterServiceHook(int tcp_port,
452 void (*h_greeting_function) (void),
453 void (*h_command_function) (void),
454 void (*h_async_function) (void)
457 struct ServiceFunctionHook *newfcn;
460 newfcn = (struct ServiceFunctionHook *)
461 malloc(sizeof(struct ServiceFunctionHook));
462 newfcn->next = ServiceHookTable;
463 newfcn->tcp_port = tcp_port;
464 newfcn->sockpath = sockpath;
465 newfcn->h_greeting_function = h_greeting_function;
466 newfcn->h_command_function = h_command_function;
467 newfcn->h_async_function = h_async_function;
469 if (sockpath != NULL) {
470 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
471 snprintf(message, sizeof message, "Unix domain socket '%s': ", sockpath);
473 else if (tcp_port <= 0) { /* port -1 to disable */
474 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
479 newfcn->msock = ig_tcp_server(config.c_ip_addr,
481 config.c_maxsessions);
482 snprintf(message, sizeof message, "TCP port %d: ", tcp_port);
485 if (newfcn->msock > 0) {
486 ServiceHookTable = newfcn;
487 strcat(message, "registered.");
488 lprintf(CTDL_INFO, "%s\n", message);
491 strcat(message, "FAILED.");
492 lprintf(CTDL_CRIT, "%s\n", message);
498 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
499 void (*h_greeting_function) (void),
500 void (*h_command_function) (void),
501 void (*h_async_function) (void)
504 struct ServiceFunctionHook *cur, *p;
506 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
507 /* This will also remove duplicates if any */
508 while (cur != NULL &&
509 !(sockpath && cur->sockpath &&
510 strcmp(sockpath, cur->sockpath)) &&
511 h_greeting_function == cur->h_greeting_function &&
512 h_command_function == cur->h_command_function &&
513 h_async_function == cur->h_async_function &&
514 tcp_port == cur->tcp_port) {
517 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
519 } else if (tcp_port) {
520 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
522 lprintf(CTDL_INFO, "Unregistered unknown service\n");
525 if (cur == ServiceHookTable) {
526 ServiceHookTable = p;
535 void PerformSessionHooks(int EventType)
537 struct SessionFunctionHook *fcn;
539 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
540 if (fcn->eventtype == EventType) {
541 (*fcn->h_function_pointer) ();
546 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
548 struct UserFunctionHook *fcn;
550 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
551 if (fcn->eventtype == EventType) {
552 (*fcn->h_function_pointer) (usbuf);
557 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
559 struct MessageFunctionHook *fcn;
560 int total_retval = 0;
562 /* Other code may elect to protect this message from server-side
563 * handlers; if this is the case, don't do anything.
564 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
565 EventType, msg->cm_flags);
567 if (msg->cm_flags & CM_SKIP_HOOKS) {
568 lprintf(CTDL_DEBUG, "Skipping hooks\n");
572 /* Otherwise, run all the hooks appropriate to this event type.
574 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
575 if (fcn->eventtype == EventType) {
576 total_retval = total_retval +
577 (*fcn->h_function_pointer) (msg);
581 /* Return the sum of the return codes from the hook functions. If
582 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
583 * the save operation to abort.
590 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
592 struct NetprocFunctionHook *fcn;
593 int total_retval = 0;
595 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
596 total_retval = total_retval +
597 (*fcn->h_function_pointer) (msg, target_room);
600 /* Return the sum of the return codes from the hook functions.
601 * A nonzero return code will cause the message to *not* be imported.
607 void PerformDeleteHooks(char *room, long msgnum)
609 struct DeleteFunctionHook *fcn;
611 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
612 (*fcn->h_function_pointer) (room, msgnum);
620 int PerformXmsgHooks(char *sender, char *recp, char *msg)
622 struct XmsgFunctionHook *fcn;
626 for (p=0; p<MAX_XMSG_PRI; ++p) {
627 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
628 if (fcn->order == p) {
630 (*fcn->h_function_pointer)
634 /* Break out of the loop if a higher-priority function
635 * successfully delivered the message. This prevents duplicate
636 * deliveries to local users simultaneously signed onto
639 if (total_sent) break;