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)
104 lprintf(CTDL_INFO, "%s\n", serv_bio_init());
105 lprintf(CTDL_INFO, "%s\n", serv_calendar_init());
106 lprintf(CTDL_INFO, "%s\n", serv_notes_init());
107 lprintf(CTDL_INFO, "%s\n", serv_ldap_init());
108 lprintf(CTDL_INFO, "%s\n", serv_chat_init());
109 lprintf(CTDL_INFO, "%s\n", serv_expire_init());
110 lprintf(CTDL_INFO, "%s\n", serv_imap_init());
111 lprintf(CTDL_INFO, "%s\n", serv_inetcfg_init());
112 lprintf(CTDL_INFO, "%s\n", serv_listsub_init());
113 lprintf(CTDL_INFO, "%s\n", serv_mrtg_init());
114 lprintf(CTDL_INFO, "%s\n", serv_netfilter_init());
115 lprintf(CTDL_INFO, "%s\n", serv_network_init());
116 lprintf(CTDL_INFO, "%s\n", serv_newuser_init());
117 lprintf(CTDL_INFO, "%s\n", serv_pas2_init());
118 lprintf(CTDL_INFO, "%s\n", serv_pop3_init());
119 lprintf(CTDL_INFO, "%s\n", serv_rwho_init());
120 lprintf(CTDL_INFO, "%s\n", serv_smtp_init());
121 lprintf(CTDL_INFO, "%s\n", serv_spam_init());
122 /* lprintf(CTDL_INFO, "%s\n", serv_test_init()); */
123 lprintf(CTDL_INFO, "%s\n", serv_upgrade_init());
124 lprintf(CTDL_INFO, "%s\n", serv_vandelay_init());
125 lprintf(CTDL_INFO, "%s\n", serv_vcard_init());
126 lprintf(CTDL_INFO, "%s\n", serv_fulltext_init());
127 lprintf(CTDL_INFO, "%s\n", serv_autocompletion_init());
132 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
135 struct CleanupFunctionHook *newfcn;
137 newfcn = (struct CleanupFunctionHook *)
138 malloc(sizeof(struct CleanupFunctionHook));
139 newfcn->next = CleanupHookTable;
140 newfcn->h_function_pointer = fcn_ptr;
141 CleanupHookTable = newfcn;
143 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
147 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
149 struct CleanupFunctionHook *cur, *p;
151 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
152 /* This will also remove duplicates if any */
153 while (cur != NULL &&
154 fcn_ptr == cur->h_function_pointer) {
155 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
157 if (cur == CleanupHookTable) {
158 CleanupHookTable = p;
167 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
170 struct SessionFunctionHook *newfcn;
172 newfcn = (struct SessionFunctionHook *)
173 malloc(sizeof(struct SessionFunctionHook));
174 newfcn->next = SessionHookTable;
175 newfcn->h_function_pointer = fcn_ptr;
176 newfcn->eventtype = EventType;
177 SessionHookTable = newfcn;
179 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
184 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
186 struct SessionFunctionHook *cur, *p;
188 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
189 /* This will also remove duplicates if any */
190 while (cur != NULL &&
191 fcn_ptr == cur->h_function_pointer &&
192 EventType == cur->eventtype) {
193 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
196 if (cur == SessionHookTable) {
197 SessionHookTable = p;
206 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
209 struct UserFunctionHook *newfcn;
211 newfcn = (struct UserFunctionHook *)
212 malloc(sizeof(struct UserFunctionHook));
213 newfcn->next = UserHookTable;
214 newfcn->h_function_pointer = fcn_ptr;
215 newfcn->eventtype = EventType;
216 UserHookTable = newfcn;
218 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
223 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
225 struct UserFunctionHook *cur, *p;
227 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
228 /* This will also remove duplicates if any */
229 while (cur != NULL &&
230 fcn_ptr == cur->h_function_pointer &&
231 EventType == cur->eventtype) {
232 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
235 if (cur == UserHookTable) {
245 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
249 struct MessageFunctionHook *newfcn;
251 newfcn = (struct MessageFunctionHook *)
252 malloc(sizeof(struct MessageFunctionHook));
253 newfcn->next = MessageHookTable;
254 newfcn->h_function_pointer = handler;
255 newfcn->eventtype = EventType;
256 MessageHookTable = newfcn;
258 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
263 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
266 struct MessageFunctionHook *cur, *p;
268 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
269 /* This will also remove duplicates if any */
270 while (cur != NULL &&
271 handler == cur->h_function_pointer &&
272 EventType == cur->eventtype) {
273 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
276 if (cur == MessageHookTable) {
277 MessageHookTable = p;
286 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
288 struct NetprocFunctionHook *newfcn;
290 newfcn = (struct NetprocFunctionHook *)
291 malloc(sizeof(struct NetprocFunctionHook));
292 newfcn->next = NetprocHookTable;
293 newfcn->h_function_pointer = handler;
294 NetprocHookTable = newfcn;
296 lprintf(CTDL_INFO, "Registered a new netproc function\n");
300 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
302 struct NetprocFunctionHook *cur, *p;
304 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
305 /* This will also remove duplicates if any */
306 while (cur != NULL &&
307 handler == cur->h_function_pointer ) {
308 lprintf(CTDL_INFO, "Unregistered netproc function\n");
310 if (cur == NetprocHookTable) {
311 NetprocHookTable = p;
320 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
322 struct DeleteFunctionHook *newfcn;
324 newfcn = (struct DeleteFunctionHook *)
325 malloc(sizeof(struct DeleteFunctionHook));
326 newfcn->next = DeleteHookTable;
327 newfcn->h_function_pointer = handler;
328 DeleteHookTable = newfcn;
330 lprintf(CTDL_INFO, "Registered a new netproc function\n");
334 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
336 struct DeleteFunctionHook *cur, *p;
338 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
339 /* This will also remove duplicates if any */
340 while (cur != NULL &&
341 handler == cur->h_function_pointer ) {
342 lprintf(CTDL_INFO, "Unregistered netproc function\n");
344 if (cur == DeleteHookTable) {
354 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
357 struct XmsgFunctionHook *newfcn;
359 newfcn = (struct XmsgFunctionHook *)
360 malloc(sizeof(struct XmsgFunctionHook));
361 newfcn->next = XmsgHookTable;
362 newfcn->order = order;
363 newfcn->h_function_pointer = fcn_ptr;
364 XmsgHookTable = newfcn;
365 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
369 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
371 struct XmsgFunctionHook *cur, *p;
373 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
374 /* This will also remove duplicates if any */
375 while (cur != NULL &&
376 fcn_ptr == cur->h_function_pointer &&
377 order == cur->order) {
378 lprintf(CTDL_INFO, "Unregistered x-msg function "
379 "(priority %d)\n", order);
381 if (cur == XmsgHookTable) {
391 void CtdlRegisterServiceHook(int tcp_port,
393 void (*h_greeting_function) (void),
394 void (*h_command_function) (void),
395 void (*h_async_function) (void)
398 struct ServiceFunctionHook *newfcn;
401 newfcn = (struct ServiceFunctionHook *)
402 malloc(sizeof(struct ServiceFunctionHook));
403 newfcn->next = ServiceHookTable;
404 newfcn->tcp_port = tcp_port;
405 newfcn->sockpath = sockpath;
406 newfcn->h_greeting_function = h_greeting_function;
407 newfcn->h_command_function = h_command_function;
408 newfcn->h_async_function = h_async_function;
410 if (sockpath != NULL) {
411 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
412 snprintf(message, sizeof message, "Unix domain socket '%s': ", sockpath);
414 else if (tcp_port <= 0) { /* port -1 to disable */
415 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
420 newfcn->msock = ig_tcp_server(config.c_ip_addr,
422 config.c_maxsessions);
423 snprintf(message, sizeof message, "TCP port %d: ", tcp_port);
426 if (newfcn->msock > 0) {
427 ServiceHookTable = newfcn;
428 strcat(message, "registered.");
429 lprintf(CTDL_INFO, "%s\n", message);
432 strcat(message, "FAILED.");
433 lprintf(CTDL_CRIT, "%s\n", message);
439 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
440 void (*h_greeting_function) (void),
441 void (*h_command_function) (void),
442 void (*h_async_function) (void)
445 struct ServiceFunctionHook *cur, *p;
447 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
448 /* This will also remove duplicates if any */
449 while (cur != NULL &&
450 !(sockpath && cur->sockpath &&
451 strcmp(sockpath, cur->sockpath)) &&
452 h_greeting_function == cur->h_greeting_function &&
453 h_command_function == cur->h_command_function &&
454 h_async_function == cur->h_async_function &&
455 tcp_port == cur->tcp_port) {
458 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
460 } else if (tcp_port) {
461 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
463 lprintf(CTDL_INFO, "Unregistered unknown service\n");
466 if (cur == ServiceHookTable) {
467 ServiceHookTable = p;
476 void PerformSessionHooks(int EventType)
478 struct SessionFunctionHook *fcn;
480 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
481 if (fcn->eventtype == EventType) {
482 (*fcn->h_function_pointer) ();
487 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
489 struct UserFunctionHook *fcn;
491 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
492 if (fcn->eventtype == EventType) {
493 (*fcn->h_function_pointer) (usbuf);
498 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
500 struct MessageFunctionHook *fcn;
501 int total_retval = 0;
503 /* Other code may elect to protect this message from server-side
504 * handlers; if this is the case, don't do anything.
505 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
506 EventType, msg->cm_flags);
508 if (msg->cm_flags & CM_SKIP_HOOKS) {
509 lprintf(CTDL_DEBUG, "Skipping hooks\n");
513 /* Otherwise, run all the hooks appropriate to this event type.
515 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
516 if (fcn->eventtype == EventType) {
517 total_retval = total_retval +
518 (*fcn->h_function_pointer) (msg);
522 /* Return the sum of the return codes from the hook functions. If
523 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
524 * the save operation to abort.
531 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
533 struct NetprocFunctionHook *fcn;
534 int total_retval = 0;
536 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
537 total_retval = total_retval +
538 (*fcn->h_function_pointer) (msg, target_room);
541 /* Return the sum of the return codes from the hook functions.
542 * A nonzero return code will cause the message to *not* be imported.
548 void PerformDeleteHooks(char *room, long msgnum)
550 struct DeleteFunctionHook *fcn;
552 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
553 (*fcn->h_function_pointer) (room, msgnum);
559 int PerformXmsgHooks(char *sender, char *recp, char *msg)
561 struct XmsgFunctionHook *fcn;
565 for (p=0; p<MAX_XMSG_PRI; ++p) {
566 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
567 if (fcn->order == p) {
569 (*fcn->h_function_pointer)
573 /* Break out of the loop if a higher-priority function
574 * successfully delivered the message. This prevents duplicate
575 * deliveries to local users simultaneously signed onto
578 if (total_sent) break;