2 * Citadel Dynamic Loading Module
3 * Written by Brian Costello <btx@calyx.net>
5 * Copyright (c) 1987-2011 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <libcitadel.h>
19 #include "sysdep_decls.h"
20 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
22 #include "serv_extensions.h"
23 #include "ctdl_module.h"
29 * Structure defentitions for hook tables
32 HashList *LogDebugEntryTable = NULL;
34 typedef struct LogFunctionHook LogFunctionHook;
35 struct LogFunctionHook {
36 LogFunctionHook *next;
38 void (*h_function_pointer) (char *);
41 LogFunctionHook *LogHookTable = NULL;
43 typedef struct FixedOutputHook FixedOutputHook;
44 struct FixedOutputHook {
45 FixedOutputHook *next;
46 char content_type[64];
47 void (*h_function_pointer) (char *, int);
49 FixedOutputHook *FixedOutputTable = NULL;
54 * TDAPVetoHookFunctionHook extensions are used for any type of hook for which
55 * may prevent the autopurger to run for this specific data class.
56 * the function should at least LOG_INFO that it does so.
58 typedef struct TDAPVetoHookFunctionHook TDAPVetoHookFunctionHook;
59 struct TDAPVetoHookFunctionHook {
60 TDAPVetoHookFunctionHook *next;
62 int (*h_function_pointer) (StrBuf *);
65 TDAPVetoHookFunctionHook *TDAPVetoHookTable = NULL;
70 * SessionFunctionHook extensions are used for any type of hook for which
71 * the context in which it's being called (which is determined by the event
72 * type) will make it obvious for the hook function to know where to look for
75 typedef struct SessionFunctionHook SessionFunctionHook;
76 struct SessionFunctionHook {
77 SessionFunctionHook *next;
79 void (*h_function_pointer) (void);
82 SessionFunctionHook *SessionHookTable = NULL;
85 * UserFunctionHook extensions are used for any type of hook which implements
86 * an operation on a user or username (potentially) other than the one
87 * operating the current session.
89 typedef struct UserFunctionHook UserFunctionHook;
90 struct UserFunctionHook {
91 UserFunctionHook *next;
92 void (*h_function_pointer) (struct ctdluser *usbuf);
95 UserFunctionHook *UserHookTable = NULL;
98 * MessageFunctionHook extensions are used for hooks which implement handlers
99 * for various types of message operations (save, read, etc.)
101 typedef struct MessageFunctionHook MessageFunctionHook;
102 struct MessageFunctionHook {
103 MessageFunctionHook *next;
104 int (*h_function_pointer) (struct CtdlMessage *msg, recptypes *recps);
107 MessageFunctionHook *MessageHookTable = NULL;
111 * NetprocFunctionHook extensions are used for hooks which implement handlers
112 * for incoming network messages.
114 typedef struct NetprocFunctionHook NetprocFunctionHook;
115 struct NetprocFunctionHook {
116 NetprocFunctionHook *next;
117 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
119 NetprocFunctionHook *NetprocHookTable = NULL;
123 * DeleteFunctionHook extensions are used for hooks which get called when a
124 * message is about to be deleted.
126 typedef struct DeleteFunctionHook DeleteFunctionHook;
127 struct DeleteFunctionHook {
128 DeleteFunctionHook *next;
129 void (*h_function_pointer) (char *target_room, long msgnum);
131 DeleteFunctionHook *DeleteHookTable = NULL;
135 * ExpressMessageFunctionHook extensions are used for hooks which implement
136 * the sending of an instant message through various channels. Any function
137 * registered should return the number of recipients to whom the message was
138 * successfully transmitted.
140 typedef struct XmsgFunctionHook XmsgFunctionHook;
141 struct XmsgFunctionHook {
142 XmsgFunctionHook *next;
143 int (*h_function_pointer) (char *, char *, char *, char *);
146 XmsgFunctionHook *XmsgHookTable = NULL;
152 * RoomFunctionHook extensions are used for hooks which impliment room
153 * processing functions when new messages are added EG. SIEVE.
155 typedef struct RoomFunctionHook RoomFunctionHook;
156 struct RoomFunctionHook {
157 RoomFunctionHook *next;
158 int (*fcn_ptr) (struct ctdlroom *);
160 RoomFunctionHook *RoomHookTable = NULL;
164 typedef struct SearchFunctionHook SearchFunctionHook;
165 struct SearchFunctionHook {
166 SearchFunctionHook *next;
167 void (*fcn_ptr) (int *, long **, const char *);
170 SearchFunctionHook *SearchFunctionHookTable = NULL;
172 CleanupFunctionHook *CleanupHookTable = NULL;
173 CleanupFunctionHook *EVCleanupHookTable = NULL;
175 ServiceFunctionHook *ServiceHookTable = NULL;
177 typedef struct ProtoFunctionHook ProtoFunctionHook;
178 struct ProtoFunctionHook {
179 void (*handler) (char *cmdbuf);
184 HashList *ProtoHookList = NULL;
187 #define ERR_PORT (1 << 1)
190 static StrBuf *portlist = NULL;
192 static StrBuf *errormessages = NULL;
195 long DetailErrorFlags;
196 ConstStr Empty = {HKEY("")};
197 char *ErrSubject = "Startup Problems";
198 ConstStr ErrGeneral[] = {
199 {HKEY("Citadel had trouble on starting up. ")},
200 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
201 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
202 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
203 "The errors returned by the system were:\n")},
204 {HKEY("You can recheck the above if you follow this faq item:\n"
205 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
208 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
209 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
210 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
211 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
212 " and stop the program that binds these ports.\n You should eventually remove "
213 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
214 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
217 void LogPrintMessages(long err)
220 StrBuf *List, *DetailList;
221 ConstStr *Short, *Where, *Hint;
224 Message = NewStrBufPlain(NULL,
225 StrLength(portlist) + StrLength(errormessages));
227 DetailErrorFlags = DetailErrorFlags & ~err;
232 Short = &ErrPortShort;
233 Where = &ErrPortWhere;
236 DetailList = errormessages;
246 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
247 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
248 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
249 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
250 StrBufAppendBuf(Message, List, 0);
251 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
252 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
253 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
254 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
255 StrBufAppendBuf(Message, DetailList, 0);
256 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
257 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
259 MOD_syslog(LOG_EMERG, "%s", ChrPtr(Message));
260 MOD_syslog(LOG_EMERG, "%s", ErrSubject);
261 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
263 FreeStrBuf(&Message);
265 FreeStrBuf(&DetailList);
269 void AddPortError(char *Port, char *ErrorMessage)
273 DetailErrorFlags |= ERR_PORT;
275 len = StrLength(errormessages);
276 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
277 else errormessages = NewStrBuf();
278 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
281 len = StrLength(portlist);
282 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
283 else portlist = NewStrBuf();
284 StrBufAppendBufPlain(portlist, Port, -1, 0);
288 int DLoader_Exec_Cmd(char *cmdbuf)
291 ProtoFunctionHook *p;
293 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
294 p = (ProtoFunctionHook*) vP;
295 p->handler(&cmdbuf[5]);
301 long FourHash(const char *key, long length)
305 const unsigned char *ptr = (const unsigned char*)key;
307 for (i = 0; i < 4; i++, ptr ++)
317 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
320 if (LogDebugEntryTable == NULL)
321 LogDebugEntryTable = NewHash(1, NULL);
322 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
327 Put(LogDebugEntryTable, Name, Len, E, NULL);
330 void CtdlSetDebugLogFacilities(const char **Str, long n)
332 StrBuf *Token = NULL;
339 for (i=0; i < n; i++){
340 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
341 if (strcmp(Str[i], "all") == 0) {
345 Buf = NewStrBufPlain(Str[i], -1);
348 Token = NewStrBufPlain(NULL, StrLength(Buf));
349 while ((ch != StrBufNOTNULL) &&
350 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
351 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
354 LogDebugEntry *E = (LogDebugEntry*)vptr;
367 Pos = GetNewHashPos(LogDebugEntryTable, 0);
368 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
369 LogDebugEntry *E = (LogDebugEntry*)vptr;
376 void CtdlDestroyDebugTable(void)
379 DeleteHash(&LogDebugEntryTable);
382 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
384 ProtoFunctionHook *p;
386 if (ProtoHookList == NULL)
387 ProtoHookList = NewHash (1, FourHash);
390 p = (ProtoFunctionHook *)
391 malloc(sizeof(ProtoFunctionHook));
394 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
397 p->handler = handler;
401 Put(ProtoHookList, cmd, 4, p, NULL);
402 MOD_syslog(LOG_DEBUG, "Registered server command %s (%s)\n", cmd, desc);
405 void CtdlDestroyProtoHooks(void)
408 DeleteHash(&ProtoHookList);
412 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
415 CleanupFunctionHook *newfcn;
417 newfcn = (CleanupFunctionHook *)
418 malloc(sizeof(CleanupFunctionHook));
419 newfcn->next = CleanupHookTable;
420 newfcn->h_function_pointer = fcn_ptr;
421 CleanupHookTable = newfcn;
423 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
427 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
429 CleanupFunctionHook *cur, *p, *last;
431 cur = CleanupHookTable;
434 if (fcn_ptr == cur->h_function_pointer)
436 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
445 CleanupHookTable = p;
456 void CtdlDestroyCleanupHooks(void)
458 CleanupFunctionHook *cur, *p;
460 cur = CleanupHookTable;
463 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
468 CleanupHookTable = NULL;
471 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
474 CleanupFunctionHook *newfcn;
476 newfcn = (CleanupFunctionHook *)
477 malloc(sizeof(CleanupFunctionHook));
478 newfcn->next = EVCleanupHookTable;
479 newfcn->h_function_pointer = fcn_ptr;
480 EVCleanupHookTable = newfcn;
482 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
486 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
488 CleanupFunctionHook *cur, *p, *last;
490 cur = EVCleanupHookTable;
493 if (fcn_ptr == cur->h_function_pointer)
495 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
504 EVCleanupHookTable = p;
515 void CtdlDestroyEVCleanupHooks(void)
517 CleanupFunctionHook *cur, *p;
519 cur = EVCleanupHookTable;
522 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
524 cur->h_function_pointer();
528 EVCleanupHookTable = NULL;
531 void CtdlRegisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType, int Priority)
533 TDAPVetoHookFunctionHook *newfcn;
535 newfcn = (TDAPVetoHookFunctionHook *)
536 malloc(sizeof(TDAPVetoHookFunctionHook));
537 newfcn->Priority = Priority;
538 newfcn->h_function_pointer = fcn_ptr;
539 newfcn->eventtype = EventType;
541 TDAPVetoHookFunctionHook **pfcn;
542 pfcn = &TDAPVetoHookTable;
543 while ((*pfcn != NULL) &&
544 ((*pfcn)->Priority < newfcn->Priority) &&
545 ((*pfcn)->next != NULL))
546 pfcn = &(*pfcn)->next;
548 newfcn->next = *pfcn;
551 MOD_syslog(LOG_DEBUG, "Registered a new TDAP Veto function (type %d Priority %d)\n",
552 EventType, Priority);
556 void CtdlUnregisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType)
558 TDAPVetoHookFunctionHook *cur, *p, *last;
560 cur = TDAPVetoHookTable;
561 while (cur != NULL) {
562 if ((fcn_ptr == cur->h_function_pointer) &&
563 (EventType == cur->eventtype))
565 MOD_syslog(LOG_DEBUG, "Unregistered TDAP Veto function (type %d)\n",
575 TDAPVetoHookTable = p;
585 void CtdlDestroyTDAPVetoHooks(void)
587 TDAPVetoHookFunctionHook *cur, *p;
589 cur = TDAPVetoHookTable;
592 MODM_syslog(LOG_DEBUG, "Destroyed TDAP Veto function\n");
597 TDAPVetoHookTable = NULL;
601 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
603 SessionFunctionHook *newfcn;
605 newfcn = (SessionFunctionHook *)
606 malloc(sizeof(SessionFunctionHook));
607 newfcn->Priority = Priority;
608 newfcn->h_function_pointer = fcn_ptr;
609 newfcn->eventtype = EventType;
611 SessionFunctionHook **pfcn;
612 pfcn = &SessionHookTable;
613 while ((*pfcn != NULL) &&
614 ((*pfcn)->Priority < newfcn->Priority) &&
615 ((*pfcn)->next != NULL))
616 pfcn = &(*pfcn)->next;
618 newfcn->next = *pfcn;
621 MOD_syslog(LOG_DEBUG, "Registered a new session function (type %d Priority %d)\n",
622 EventType, Priority);
626 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
628 SessionFunctionHook *cur, *p, *last;
630 cur = SessionHookTable;
631 while (cur != NULL) {
632 if ((fcn_ptr == cur->h_function_pointer) &&
633 (EventType == cur->eventtype))
635 MOD_syslog(LOG_DEBUG, "Unregistered session function (type %d)\n",
645 SessionHookTable = p;
655 void CtdlDestroySessionHooks(void)
657 SessionFunctionHook *cur, *p;
659 cur = SessionHookTable;
662 MODM_syslog(LOG_DEBUG, "Destroyed session function\n");
667 SessionHookTable = NULL;
671 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
674 UserFunctionHook *newfcn;
676 newfcn = (UserFunctionHook *)
677 malloc(sizeof(UserFunctionHook));
678 newfcn->next = UserHookTable;
679 newfcn->h_function_pointer = fcn_ptr;
680 newfcn->eventtype = EventType;
681 UserHookTable = newfcn;
683 MOD_syslog(LOG_DEBUG, "Registered a new user function (type %d)\n",
688 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
690 UserFunctionHook *cur, *p, *last;
693 while (cur != NULL) {
694 if ((fcn_ptr == cur->h_function_pointer) &&
695 (EventType == cur->eventtype))
697 MOD_syslog(LOG_DEBUG, "Unregistered user function (type %d)\n",
717 void CtdlDestroyUserHooks(void)
719 UserFunctionHook *cur, *p;
724 MODM_syslog(LOG_DEBUG, "Destroyed user function \n");
729 UserHookTable = NULL;
733 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
737 MessageFunctionHook *newfcn;
739 newfcn = (MessageFunctionHook *)
740 malloc(sizeof(MessageFunctionHook));
741 newfcn->next = MessageHookTable;
742 newfcn->h_function_pointer = handler;
743 newfcn->eventtype = EventType;
744 MessageHookTable = newfcn;
746 MOD_syslog(LOG_DEBUG, "Registered a new message function (type %d)\n",
751 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
754 MessageFunctionHook *cur, *p, *last;
756 cur = MessageHookTable;
757 while (cur != NULL) {
758 if ((handler == cur->h_function_pointer) &&
759 (EventType == cur->eventtype))
761 MOD_syslog(LOG_DEBUG, "Unregistered message function (type %d)\n",
770 MessageHookTable = p;
780 void CtdlDestroyMessageHook(void)
782 MessageFunctionHook *cur, *p;
784 cur = MessageHookTable;
787 MOD_syslog(LOG_DEBUG, "Destroyed message function (type %d)\n", cur->eventtype);
792 MessageHookTable = NULL;
796 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
798 RoomFunctionHook *newfcn;
800 newfcn = (RoomFunctionHook *)
801 malloc(sizeof(RoomFunctionHook));
802 newfcn->next = RoomHookTable;
803 newfcn->fcn_ptr = fcn_ptr;
804 RoomHookTable = newfcn;
806 MODM_syslog(LOG_DEBUG, "Registered a new room function\n");
810 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
812 RoomFunctionHook *cur, *p, *last;
817 if (fcn_ptr == cur->fcn_ptr) {
818 MODM_syslog(LOG_DEBUG, "Unregistered room function\n");
838 void CtdlDestroyRoomHooks(void)
840 RoomFunctionHook *cur, *p;
845 MODM_syslog(LOG_DEBUG, "Destroyed room function\n");
850 RoomHookTable = NULL;
853 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
855 NetprocFunctionHook *newfcn;
857 newfcn = (NetprocFunctionHook *)
858 malloc(sizeof(NetprocFunctionHook));
859 newfcn->next = NetprocHookTable;
860 newfcn->h_function_pointer = handler;
861 NetprocHookTable = newfcn;
863 MODM_syslog(LOG_DEBUG, "Registered a new netproc function\n");
867 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
869 NetprocFunctionHook *cur, *p, *last;
871 cur = NetprocHookTable;
874 while (cur != NULL) {
875 if (handler == cur->h_function_pointer)
877 MODM_syslog(LOG_DEBUG, "Unregistered netproc function\n");
884 NetprocHookTable = p;
895 void CtdlDestroyNetprocHooks(void)
897 NetprocFunctionHook *cur, *p;
899 cur = NetprocHookTable;
902 MODM_syslog(LOG_DEBUG, "Destroyed netproc function\n");
907 NetprocHookTable = NULL;
911 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
913 DeleteFunctionHook *newfcn;
915 newfcn = (DeleteFunctionHook *)
916 malloc(sizeof(DeleteFunctionHook));
917 newfcn->next = DeleteHookTable;
918 newfcn->h_function_pointer = handler;
919 DeleteHookTable = newfcn;
921 MODM_syslog(LOG_DEBUG, "Registered a new delete function\n");
925 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
927 DeleteFunctionHook *cur, *p, *last;
930 cur = DeleteHookTable;
931 while (cur != NULL) {
932 if (handler == cur->h_function_pointer )
934 MODM_syslog(LOG_DEBUG, "Unregistered delete function\n");
951 void CtdlDestroyDeleteHooks(void)
953 DeleteFunctionHook *cur, *p;
955 cur = DeleteHookTable;
958 MODM_syslog(LOG_DEBUG, "Destroyed delete function\n");
963 DeleteHookTable = NULL;
969 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
971 FixedOutputHook *newfcn;
973 newfcn = (FixedOutputHook *)
974 malloc(sizeof(FixedOutputHook));
975 newfcn->next = FixedOutputTable;
976 newfcn->h_function_pointer = handler;
977 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
978 FixedOutputTable = newfcn;
980 MOD_syslog(LOG_DEBUG, "Registered a new fixed output function for %s\n", newfcn->content_type);
984 void CtdlUnregisterFixedOutputHook(char *content_type)
986 FixedOutputHook *cur, *p, *last;
989 cur = FixedOutputTable;
990 while (cur != NULL) {
991 /* This will also remove duplicates if any */
992 if (!strcasecmp(content_type, cur->content_type)) {
993 MOD_syslog(LOG_DEBUG,
994 "Unregistered fixed output function for %s\n",
1003 FixedOutputTable = p;
1015 void CtdlDestroyFixedOutputHooks(void)
1017 FixedOutputHook *cur, *p;
1019 cur = FixedOutputTable;
1022 MOD_syslog(LOG_DEBUG, "Destroyed fixed output function for %s\n", cur->content_type);
1028 FixedOutputTable = NULL;
1031 /* returns nonzero if we found a hook and used it */
1032 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
1034 FixedOutputHook *fcn;
1036 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
1037 if (!strcasecmp(content_type, fcn->content_type)) {
1038 (*fcn->h_function_pointer) (content, content_length);
1049 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1052 XmsgFunctionHook *newfcn;
1054 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
1055 newfcn->next = XmsgHookTable;
1056 newfcn->order = order;
1057 newfcn->h_function_pointer = fcn_ptr;
1058 XmsgHookTable = newfcn;
1059 MOD_syslog(LOG_DEBUG, "Registered a new x-msg function (priority %d)\n", order);
1063 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1065 XmsgFunctionHook *cur, *p, *last;
1068 cur = XmsgHookTable;
1069 while (cur != NULL) {
1070 /* This will also remove duplicates if any */
1071 if (fcn_ptr == cur->h_function_pointer &&
1072 order == cur->order) {
1073 MOD_syslog(LOG_DEBUG, "Unregistered x-msg function "
1074 "(priority %d)\n", order);
1092 void CtdlDestroyXmsgHooks(void)
1094 XmsgFunctionHook *cur, *p;
1096 cur = XmsgHookTable;
1099 MOD_syslog(LOG_DEBUG, "Destroyed x-msg function "
1100 "(priority %d)\n", cur->order);
1106 XmsgHookTable = NULL;
1110 void CtdlRegisterServiceHook(int tcp_port,
1112 void (*h_greeting_function) (void),
1113 void (*h_command_function) (void),
1114 void (*h_async_function) (void),
1115 const char *ServiceName)
1117 ServiceFunctionHook *newfcn;
1122 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1123 message = (char*) malloc (SIZ + SIZ);
1125 newfcn->next = ServiceHookTable;
1126 newfcn->tcp_port = tcp_port;
1127 newfcn->sockpath = sockpath;
1128 newfcn->h_greeting_function = h_greeting_function;
1129 newfcn->h_command_function = h_command_function;
1130 newfcn->h_async_function = h_async_function;
1131 newfcn->ServiceName = ServiceName;
1133 if (sockpath != NULL) {
1134 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
1135 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1137 else if (tcp_port <= 0) { /* port -1 to disable */
1138 MOD_syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1144 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
1146 config.c_maxsessions,
1148 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
1149 config.c_ip_addr, tcp_port, ServiceName);
1152 if (newfcn->msock > 0) {
1153 ServiceHookTable = newfcn;
1154 strcat(message, "registered.");
1155 MOD_syslog(LOG_INFO, "%s\n", message);
1158 AddPortError(message, error);
1159 strcat(message, "FAILED.");
1160 MOD_syslog(LOG_CRIT, "%s\n", message);
1167 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1168 void (*h_greeting_function) (void),
1169 void (*h_command_function) (void),
1170 void (*h_async_function) (void)
1173 ServiceFunctionHook *cur, *p, *last;
1176 cur = ServiceHookTable;
1177 while (cur != NULL) {
1178 /* This will also remove duplicates if any */
1179 if (h_greeting_function == cur->h_greeting_function &&
1180 h_command_function == cur->h_command_function &&
1181 h_async_function == cur->h_async_function &&
1182 tcp_port == cur->tcp_port &&
1183 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
1188 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1191 } else if (tcp_port) {
1192 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1194 MOD_syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1201 ServiceHookTable = p;
1212 void CtdlShutdownServiceHooks(void)
1214 /* sort of a duplicate of close_masters() but called earlier */
1215 ServiceFunctionHook *cur;
1217 cur = ServiceHookTable;
1220 if (cur->msock != -1)
1224 if (cur->sockpath != NULL){
1225 MOD_syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1228 unlink(cur->sockpath);
1230 MOD_syslog(LOG_INFO, "[%s] closing service\n",
1238 void CtdlDestroyServiceHook(void)
1241 ServiceFunctionHook *cur, *p;
1243 cur = ServiceHookTable;
1246 if (cur->msock != -1)
1253 Text = " Not closing again";
1256 if (cur->sockpath) {
1257 MOD_syslog(LOG_INFO, "%s UNIX domain socket %s\n",
1260 unlink(cur->sockpath);
1261 } else if (cur->tcp_port) {
1262 MOD_syslog(LOG_INFO, "%s TCP port %d\n", Text, cur->tcp_port);
1264 MOD_syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1270 ServiceHookTable = NULL;
1273 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1275 SearchFunctionHook *newfcn;
1277 if (!name || !fcn_ptr) {
1281 newfcn = (SearchFunctionHook *)
1282 malloc(sizeof(SearchFunctionHook));
1283 newfcn->next = SearchFunctionHookTable;
1284 newfcn->name = name;
1285 newfcn->fcn_ptr = fcn_ptr;
1286 SearchFunctionHookTable = newfcn;
1288 MOD_syslog(LOG_DEBUG, "Registered a new search function (%s)\n", name);
1291 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1293 SearchFunctionHook *cur, *p, *last;
1296 cur = SearchFunctionHookTable;
1297 while (cur != NULL) {
1299 (cur->fcn_ptr == fcn_ptr) &&
1300 name && !strcmp(name, cur->name))
1302 MOD_syslog(LOG_DEBUG, "Unregistered search function(%s)\n", name);
1308 SearchFunctionHookTable = p;
1318 void CtdlDestroySearchHooks(void)
1320 SearchFunctionHook *cur, *p;
1322 cur = SearchFunctionHookTable;
1323 SearchFunctionHookTable = NULL;
1324 while (cur != NULL) {
1331 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1333 SearchFunctionHook *fcn = NULL;
1335 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1336 if (!func_name || !strcmp(func_name, fcn->name)) {
1337 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1344 int CheckTDAPVeto (int DBType, StrBuf *ErrMsg)
1347 TDAPVetoHookFunctionHook *fcn = NULL;
1349 for (fcn = TDAPVetoHookTable; (fcn != NULL) && (Result == 0); fcn = fcn->next) {
1350 if (fcn->eventtype == DBType) {
1351 Result = (*fcn->h_function_pointer) (ErrMsg);
1357 void PerformSessionHooks(int EventType)
1359 SessionFunctionHook *fcn = NULL;
1361 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1362 if (fcn->eventtype == EventType) {
1363 if (EventType == EVT_TIMER) {
1364 pthread_setspecific(MyConKey, NULL); /* for every hook */
1366 (*fcn->h_function_pointer) ();
1371 void PerformUserHooks(ctdluser *usbuf, int EventType)
1373 UserFunctionHook *fcn = NULL;
1375 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1376 if (fcn->eventtype == EventType) {
1377 (*fcn->h_function_pointer) (usbuf);
1382 int PerformMessageHooks(struct CtdlMessage *msg, recptypes *recps, int EventType)
1384 MessageFunctionHook *fcn = NULL;
1385 int total_retval = 0;
1387 /* Other code may elect to protect this message from server-side
1388 * handlers; if this is the case, don't do anything.
1389 MOD_syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1391 if (msg->cm_flags & CM_SKIP_HOOKS) {
1392 MODM_syslog(LOG_DEBUG, "Skipping hooks\n");
1396 /* Otherwise, run all the hooks appropriate to this event type.
1398 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1399 if (fcn->eventtype == EventType) {
1400 total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
1404 /* Return the sum of the return codes from the hook functions. If
1405 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1406 * the save operation to abort.
1408 return total_retval;
1412 int PerformRoomHooks(struct ctdlroom *target_room)
1414 RoomFunctionHook *fcn;
1415 int total_retval = 0;
1417 MOD_syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1419 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1420 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1423 /* Return the sum of the return codes from the hook functions.
1425 return total_retval;
1429 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1431 NetprocFunctionHook *fcn;
1432 int total_retval = 0;
1434 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1435 total_retval = total_retval +
1436 (*fcn->h_function_pointer) (msg, target_room);
1439 /* Return the sum of the return codes from the hook functions.
1440 * A nonzero return code will cause the message to *not* be imported.
1442 return total_retval;
1446 void PerformDeleteHooks(char *room, long msgnum)
1448 DeleteFunctionHook *fcn;
1450 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1451 (*fcn->h_function_pointer) (room, msgnum);
1459 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1461 XmsgFunctionHook *fcn;
1465 for (p=0; p<MAX_XMSG_PRI; ++p) {
1466 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1467 if (fcn->order == p) {
1469 (*fcn->h_function_pointer)
1470 (sender, sender_email, recp, msg);
1473 /* Break out of the loop if a higher-priority function
1474 * successfully delivered the message. This prevents duplicate
1475 * deliveries to local users simultaneously signed onto
1478 if (total_sent) break;
1485 * Dirty hack until we impliment a hook mechanism for this
1487 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1490 CtdlStartTLS (ok_response, nosup_response, error_response);
1494 void DebugModulesEnable(const int n)
1498 CTDL_MODULE_INIT(modules)
1501 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);