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)
105 serv_calendar_init();
114 serv_netfilter_init();
122 /* serv_test_init(); */
124 serv_vandelay_init();
126 serv_fulltext_init();
131 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
134 struct CleanupFunctionHook *newfcn;
136 newfcn = (struct CleanupFunctionHook *)
137 malloc(sizeof(struct CleanupFunctionHook));
138 newfcn->next = CleanupHookTable;
139 newfcn->h_function_pointer = fcn_ptr;
140 CleanupHookTable = newfcn;
142 lprintf(CTDL_INFO, "Registered a new cleanup function\n");
146 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
148 struct CleanupFunctionHook *cur, *p;
150 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
151 /* This will also remove duplicates if any */
152 while (cur != NULL &&
153 fcn_ptr == cur->h_function_pointer) {
154 lprintf(CTDL_INFO, "Unregistered cleanup function\n");
156 if (cur == CleanupHookTable) {
157 CleanupHookTable = p;
166 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
169 struct SessionFunctionHook *newfcn;
171 newfcn = (struct SessionFunctionHook *)
172 malloc(sizeof(struct SessionFunctionHook));
173 newfcn->next = SessionHookTable;
174 newfcn->h_function_pointer = fcn_ptr;
175 newfcn->eventtype = EventType;
176 SessionHookTable = newfcn;
178 lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
183 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
185 struct SessionFunctionHook *cur, *p;
187 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
188 /* This will also remove duplicates if any */
189 while (cur != NULL &&
190 fcn_ptr == cur->h_function_pointer &&
191 EventType == cur->eventtype) {
192 lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
195 if (cur == SessionHookTable) {
196 SessionHookTable = p;
205 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
208 struct UserFunctionHook *newfcn;
210 newfcn = (struct UserFunctionHook *)
211 malloc(sizeof(struct UserFunctionHook));
212 newfcn->next = UserHookTable;
213 newfcn->h_function_pointer = fcn_ptr;
214 newfcn->eventtype = EventType;
215 UserHookTable = newfcn;
217 lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
222 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
224 struct UserFunctionHook *cur, *p;
226 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
227 /* This will also remove duplicates if any */
228 while (cur != NULL &&
229 fcn_ptr == cur->h_function_pointer &&
230 EventType == cur->eventtype) {
231 lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
234 if (cur == UserHookTable) {
244 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
248 struct MessageFunctionHook *newfcn;
250 newfcn = (struct MessageFunctionHook *)
251 malloc(sizeof(struct MessageFunctionHook));
252 newfcn->next = MessageHookTable;
253 newfcn->h_function_pointer = handler;
254 newfcn->eventtype = EventType;
255 MessageHookTable = newfcn;
257 lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
262 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
265 struct MessageFunctionHook *cur, *p;
267 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
268 /* This will also remove duplicates if any */
269 while (cur != NULL &&
270 handler == cur->h_function_pointer &&
271 EventType == cur->eventtype) {
272 lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
275 if (cur == MessageHookTable) {
276 MessageHookTable = p;
285 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
287 struct NetprocFunctionHook *newfcn;
289 newfcn = (struct NetprocFunctionHook *)
290 malloc(sizeof(struct NetprocFunctionHook));
291 newfcn->next = NetprocHookTable;
292 newfcn->h_function_pointer = handler;
293 NetprocHookTable = newfcn;
295 lprintf(CTDL_INFO, "Registered a new netproc function\n");
299 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
301 struct NetprocFunctionHook *cur, *p;
303 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
304 /* This will also remove duplicates if any */
305 while (cur != NULL &&
306 handler == cur->h_function_pointer ) {
307 lprintf(CTDL_INFO, "Unregistered netproc function\n");
309 if (cur == NetprocHookTable) {
310 NetprocHookTable = p;
319 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
321 struct DeleteFunctionHook *newfcn;
323 newfcn = (struct DeleteFunctionHook *)
324 malloc(sizeof(struct DeleteFunctionHook));
325 newfcn->next = DeleteHookTable;
326 newfcn->h_function_pointer = handler;
327 DeleteHookTable = newfcn;
329 lprintf(CTDL_INFO, "Registered a new netproc function\n");
333 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
335 struct DeleteFunctionHook *cur, *p;
337 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
338 /* This will also remove duplicates if any */
339 while (cur != NULL &&
340 handler == cur->h_function_pointer ) {
341 lprintf(CTDL_INFO, "Unregistered netproc function\n");
343 if (cur == DeleteHookTable) {
353 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
356 struct XmsgFunctionHook *newfcn;
358 newfcn = (struct XmsgFunctionHook *)
359 malloc(sizeof(struct XmsgFunctionHook));
360 newfcn->next = XmsgHookTable;
361 newfcn->order = order;
362 newfcn->h_function_pointer = fcn_ptr;
363 XmsgHookTable = newfcn;
364 lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
368 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
370 struct XmsgFunctionHook *cur, *p;
372 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
373 /* This will also remove duplicates if any */
374 while (cur != NULL &&
375 fcn_ptr == cur->h_function_pointer &&
376 order == cur->order) {
377 lprintf(CTDL_INFO, "Unregistered x-msg function "
378 "(priority %d)\n", order);
380 if (cur == XmsgHookTable) {
390 void CtdlRegisterServiceHook(int tcp_port,
392 void (*h_greeting_function) (void),
393 void (*h_command_function) (void),
394 void (*h_async_function) (void)
397 struct ServiceFunctionHook *newfcn;
400 newfcn = (struct ServiceFunctionHook *)
401 malloc(sizeof(struct ServiceFunctionHook));
402 newfcn->next = ServiceHookTable;
403 newfcn->tcp_port = tcp_port;
404 newfcn->sockpath = sockpath;
405 newfcn->h_greeting_function = h_greeting_function;
406 newfcn->h_command_function = h_command_function;
407 newfcn->h_async_function = h_async_function;
409 if (sockpath != NULL) {
410 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
411 snprintf(message, sizeof message, "Unix domain socket '%s': ", sockpath);
413 else if (tcp_port <= 0) { /* port -1 to disable */
414 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
419 newfcn->msock = ig_tcp_server(config.c_ip_addr,
421 config.c_maxsessions);
422 snprintf(message, sizeof message, "TCP port %d: ", tcp_port);
425 if (newfcn->msock > 0) {
426 ServiceHookTable = newfcn;
427 strcat(message, "registered.");
428 lprintf(CTDL_INFO, "%s\n", message);
431 strcat(message, "FAILED.");
432 lprintf(CTDL_CRIT, "%s\n", message);
438 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
439 void (*h_greeting_function) (void),
440 void (*h_command_function) (void),
441 void (*h_async_function) (void)
444 struct ServiceFunctionHook *cur, *p;
446 for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
447 /* This will also remove duplicates if any */
448 while (cur != NULL &&
449 !(sockpath && cur->sockpath &&
450 strcmp(sockpath, cur->sockpath)) &&
451 h_greeting_function == cur->h_greeting_function &&
452 h_command_function == cur->h_command_function &&
453 h_async_function == cur->h_async_function &&
454 tcp_port == cur->tcp_port) {
457 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
459 } else if (tcp_port) {
460 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
462 lprintf(CTDL_INFO, "Unregistered unknown service\n");
465 if (cur == ServiceHookTable) {
466 ServiceHookTable = p;
475 void PerformSessionHooks(int EventType)
477 struct SessionFunctionHook *fcn;
479 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
480 if (fcn->eventtype == EventType) {
481 (*fcn->h_function_pointer) ();
486 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
488 struct UserFunctionHook *fcn;
490 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
491 if (fcn->eventtype == EventType) {
492 (*fcn->h_function_pointer) (usbuf);
497 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
499 struct MessageFunctionHook *fcn;
500 int total_retval = 0;
502 /* Other code may elect to protect this message from server-side
503 * handlers; if this is the case, don't do anything.
504 lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
505 EventType, msg->cm_flags);
507 if (msg->cm_flags & CM_SKIP_HOOKS) {
508 lprintf(CTDL_DEBUG, "Skipping hooks\n");
512 /* Otherwise, run all the hooks appropriate to this event type.
514 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
515 if (fcn->eventtype == EventType) {
516 total_retval = total_retval +
517 (*fcn->h_function_pointer) (msg);
521 /* Return the sum of the return codes from the hook functions. If
522 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
523 * the save operation to abort.
530 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
532 struct NetprocFunctionHook *fcn;
533 int total_retval = 0;
535 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
536 total_retval = total_retval +
537 (*fcn->h_function_pointer) (msg, target_room);
540 /* Return the sum of the return codes from the hook functions.
541 * A nonzero return code will cause the message to *not* be imported.
547 void PerformDeleteHooks(char *room, long msgnum)
549 struct DeleteFunctionHook *fcn;
551 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
552 (*fcn->h_function_pointer) (room, msgnum);
558 int PerformXmsgHooks(char *sender, char *recp, char *msg)
560 struct XmsgFunctionHook *fcn;
564 for (p=0; p<MAX_XMSG_PRI; ++p) {
565 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
566 if (fcn->order == p) {
568 (*fcn->h_function_pointer)
572 /* Break out of the loop if a higher-priority function
573 * successfully delivered the message. This prevents duplicate
574 * deliveries to local users simultaneously signed onto
577 if (total_sent) break;