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.
20 #include <sys/types.h>
26 #include <libcitadel.h>
29 #include "serv_extensions.h"
30 #include "sysdep_decls.h"
34 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
36 #include "ctdl_module.h"
42 * Structure defentitions for hook tables
45 typedef struct __LogDebugEntry {
51 HashList *LogDebugEntryTable = NULL;
53 typedef struct LogFunctionHook LogFunctionHook;
54 struct LogFunctionHook {
55 LogFunctionHook *next;
57 void (*h_function_pointer) (char *);
60 LogFunctionHook *LogHookTable = NULL;
62 typedef struct FixedOutputHook FixedOutputHook;
63 struct FixedOutputHook {
64 FixedOutputHook *next;
65 char content_type[64];
66 void (*h_function_pointer) (char *, int);
68 FixedOutputHook *FixedOutputTable = NULL;
73 * TDAPVetoHookFunctionHook extensions are used for any type of hook for which
74 * may prevent the autopurger to run for this specific data class.
75 * the function should at least LOG_INFO that it does so.
77 typedef struct TDAPVetoHookFunctionHook TDAPVetoHookFunctionHook;
78 struct TDAPVetoHookFunctionHook {
79 TDAPVetoHookFunctionHook *next;
81 int (*h_function_pointer) (StrBuf *);
84 TDAPVetoHookFunctionHook *TDAPVetoHookTable = NULL;
89 * SessionFunctionHook extensions are used for any type of hook for which
90 * the context in which it's being called (which is determined by the event
91 * type) will make it obvious for the hook function to know where to look for
94 typedef struct SessionFunctionHook SessionFunctionHook;
95 struct SessionFunctionHook {
96 SessionFunctionHook *next;
98 void (*h_function_pointer) (void);
101 SessionFunctionHook *SessionHookTable = NULL;
104 * UserFunctionHook extensions are used for any type of hook which implements
105 * an operation on a user or username (potentially) other than the one
106 * operating the current session.
108 typedef struct UserFunctionHook UserFunctionHook;
109 struct UserFunctionHook {
110 UserFunctionHook *next;
111 void (*h_function_pointer) (struct ctdluser *usbuf);
114 UserFunctionHook *UserHookTable = NULL;
117 * MessageFunctionHook extensions are used for hooks which implement handlers
118 * for various types of message operations (save, read, etc.)
120 typedef struct MessageFunctionHook MessageFunctionHook;
121 struct MessageFunctionHook {
122 MessageFunctionHook *next;
123 int (*h_function_pointer) (struct CtdlMessage *msg);
126 MessageFunctionHook *MessageHookTable = NULL;
130 * NetprocFunctionHook extensions are used for hooks which implement handlers
131 * for incoming network messages.
133 typedef struct NetprocFunctionHook NetprocFunctionHook;
134 struct NetprocFunctionHook {
135 NetprocFunctionHook *next;
136 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
138 NetprocFunctionHook *NetprocHookTable = NULL;
142 * DeleteFunctionHook extensions are used for hooks which get called when a
143 * message is about to be deleted.
145 typedef struct DeleteFunctionHook DeleteFunctionHook;
146 struct DeleteFunctionHook {
147 DeleteFunctionHook *next;
148 void (*h_function_pointer) (char *target_room, long msgnum);
150 DeleteFunctionHook *DeleteHookTable = NULL;
154 * ExpressMessageFunctionHook extensions are used for hooks which implement
155 * the sending of an instant message through various channels. Any function
156 * registered should return the number of recipients to whom the message was
157 * successfully transmitted.
159 typedef struct XmsgFunctionHook XmsgFunctionHook;
160 struct XmsgFunctionHook {
161 XmsgFunctionHook *next;
162 int (*h_function_pointer) (char *, char *, char *, char *);
165 XmsgFunctionHook *XmsgHookTable = NULL;
171 * RoomFunctionHook extensions are used for hooks which impliment room
172 * processing functions when new messages are added EG. SIEVE.
174 typedef struct RoomFunctionHook RoomFunctionHook;
175 struct RoomFunctionHook {
176 RoomFunctionHook *next;
177 int (*fcn_ptr) (struct ctdlroom *);
179 RoomFunctionHook *RoomHookTable = NULL;
183 typedef struct SearchFunctionHook SearchFunctionHook;
184 struct SearchFunctionHook {
185 SearchFunctionHook *next;
186 void (*fcn_ptr) (int *, long **, const char *);
189 SearchFunctionHook *SearchFunctionHookTable = NULL;
191 CleanupFunctionHook *CleanupHookTable = NULL;
192 CleanupFunctionHook *EVCleanupHookTable = NULL;
194 ServiceFunctionHook *ServiceHookTable = NULL;
196 typedef struct ProtoFunctionHook ProtoFunctionHook;
197 struct ProtoFunctionHook {
198 void (*handler) (char *cmdbuf);
203 HashList *ProtoHookList = NULL;
206 #define ERR_PORT (1 << 1)
209 static StrBuf *portlist = NULL;
211 static StrBuf *errormessages = NULL;
214 long DetailErrorFlags;
215 ConstStr Empty = {HKEY("")};
216 char *ErrSubject = "Startup Problems";
217 ConstStr ErrGeneral[] = {
218 {HKEY("Citadel had trouble on starting up. ")},
219 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
220 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
221 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
222 "The errors returned by the system were:\n")},
223 {HKEY("You can recheck the above if you follow this faq item:\n"
224 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
227 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
228 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
229 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
230 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
231 " and stop the program that binds these ports.\n You should eventually remove "
232 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
233 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
236 void LogPrintMessages(long err)
239 StrBuf *List, *DetailList;
240 ConstStr *Short, *Where, *Hint;
243 Message = NewStrBufPlain(NULL,
244 StrLength(portlist) + StrLength(errormessages));
246 DetailErrorFlags = DetailErrorFlags & ~err;
251 Short = &ErrPortShort;
252 Where = &ErrPortWhere;
255 DetailList = errormessages;
265 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
266 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
267 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
268 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
269 StrBufAppendBuf(Message, List, 0);
270 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
271 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
272 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
273 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
274 StrBufAppendBuf(Message, DetailList, 0);
275 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
276 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
278 MOD_syslog(LOG_EMERG, "%s", ChrPtr(Message));
279 MOD_syslog(LOG_EMERG, "%s", ErrSubject);
280 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
282 FreeStrBuf(&Message);
284 FreeStrBuf(&DetailList);
288 void AddPortError(char *Port, char *ErrorMessage)
292 DetailErrorFlags |= ERR_PORT;
294 len = StrLength(errormessages);
295 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
296 else errormessages = NewStrBuf();
297 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
300 len = StrLength(portlist);
301 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
302 else portlist = NewStrBuf();
303 StrBufAppendBufPlain(portlist, Port, -1, 0);
307 int DLoader_Exec_Cmd(char *cmdbuf)
310 ProtoFunctionHook *p;
312 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
313 p = (ProtoFunctionHook*) vP;
314 p->handler(&cmdbuf[5]);
320 long FourHash(const char *key, long length)
324 const unsigned char *ptr = (const unsigned char*)key;
326 for (i = 0; i < 4; i++, ptr ++)
336 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
339 if (LogDebugEntryTable == NULL)
340 LogDebugEntryTable = NewHash(1, NULL);
341 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
346 Put(LogDebugEntryTable, Name, Len, E, NULL);
349 void CtdlSetDebugLogFacilities(const char **Str, long n)
351 StrBuf *Token = NULL;
358 for (i=0; i < n; i++){
359 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
360 if (strcmp(Str[i], "all") == 0) {
364 Buf = NewStrBufPlain(Str[i], -1);
367 Token = NewStrBufPlain(NULL, StrLength(Buf));
368 while ((ch != StrBufNOTNULL) &&
369 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
370 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
373 LogDebugEntry *E = (LogDebugEntry*)vptr;
386 Pos = GetNewHashPos(LogDebugEntryTable, 0);
387 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
388 LogDebugEntry *E = (LogDebugEntry*)vptr;
395 void cmd_log_get(char *argbuf)
402 if (CtdlAccessCheck(ac_aide)) return;
404 cprintf("%d Log modules enabled:\n", LISTING_FOLLOWS);
406 Pos = GetNewHashPos(LogDebugEntryTable, 0);
408 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
409 LogDebugEntry *E = (LogDebugEntry*)vptr;
410 cprintf("%s|%d\n", ch, *E->LogP);
416 void cmd_log_set(char *argbuf)
421 char which[SIZ] = "";
423 if (CtdlAccessCheck(ac_aide)) return;
425 wlen = extract_token(which, argbuf, 0, '|', sizeof(which));
426 if (wlen < 0) wlen = 0;
427 lset = extract_int(argbuf, 1);
428 if (lset != 0) lset = 1;
429 if (GetHash(LogDebugEntryTable, which, wlen, &vptr) &&
432 LogDebugEntry *E = (LogDebugEntry*)vptr;
434 cprintf("%d %s|%d\n", CIT_OK, which, lset);
437 cprintf("%d Log setting %s not known\n",
441 void CtdlDestroyDebugTable(void)
444 DeleteHash(&LogDebugEntryTable);
447 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
449 ProtoFunctionHook *p;
451 if (ProtoHookList == NULL)
452 ProtoHookList = NewHash (1, FourHash);
455 p = (ProtoFunctionHook *)
456 malloc(sizeof(ProtoFunctionHook));
459 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
462 p->handler = handler;
466 Put(ProtoHookList, cmd, 4, p, NULL);
467 MOD_syslog(LOG_DEBUG, "Registered server command %s (%s)\n", cmd, desc);
470 void CtdlDestroyProtoHooks(void)
473 DeleteHash(&ProtoHookList);
477 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
480 CleanupFunctionHook *newfcn;
482 newfcn = (CleanupFunctionHook *)
483 malloc(sizeof(CleanupFunctionHook));
484 newfcn->next = CleanupHookTable;
485 newfcn->h_function_pointer = fcn_ptr;
486 CleanupHookTable = newfcn;
488 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
492 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
494 CleanupFunctionHook *cur, *p, *last;
496 cur = CleanupHookTable;
499 if (fcn_ptr == cur->h_function_pointer)
501 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
510 CleanupHookTable = p;
521 void CtdlDestroyCleanupHooks(void)
523 CleanupFunctionHook *cur, *p;
525 cur = CleanupHookTable;
528 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
533 CleanupHookTable = NULL;
536 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
539 CleanupFunctionHook *newfcn;
541 newfcn = (CleanupFunctionHook *)
542 malloc(sizeof(CleanupFunctionHook));
543 newfcn->next = EVCleanupHookTable;
544 newfcn->h_function_pointer = fcn_ptr;
545 EVCleanupHookTable = newfcn;
547 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
551 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
553 CleanupFunctionHook *cur, *p, *last;
555 cur = EVCleanupHookTable;
558 if (fcn_ptr == cur->h_function_pointer)
560 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
569 EVCleanupHookTable = p;
580 void CtdlDestroyEVCleanupHooks(void)
582 CleanupFunctionHook *cur, *p;
584 cur = EVCleanupHookTable;
587 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
589 cur->h_function_pointer();
593 EVCleanupHookTable = NULL;
596 void CtdlRegisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType, int Priority)
598 TDAPVetoHookFunctionHook *newfcn;
600 newfcn = (TDAPVetoHookFunctionHook *)
601 malloc(sizeof(TDAPVetoHookFunctionHook));
602 newfcn->Priority = Priority;
603 newfcn->h_function_pointer = fcn_ptr;
604 newfcn->eventtype = EventType;
606 TDAPVetoHookFunctionHook **pfcn;
607 pfcn = &TDAPVetoHookTable;
608 while ((*pfcn != NULL) &&
609 ((*pfcn)->Priority < newfcn->Priority) &&
610 ((*pfcn)->next != NULL))
611 pfcn = &(*pfcn)->next;
613 newfcn->next = *pfcn;
616 MOD_syslog(LOG_DEBUG, "Registered a new TDAP Veto function (type %d Priority %d)\n",
617 EventType, Priority);
621 void CtdlUnregisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType)
623 TDAPVetoHookFunctionHook *cur, *p, *last;
625 cur = TDAPVetoHookTable;
626 while (cur != NULL) {
627 if ((fcn_ptr == cur->h_function_pointer) &&
628 (EventType == cur->eventtype))
630 MOD_syslog(LOG_DEBUG, "Unregistered TDAP Veto function (type %d)\n",
640 TDAPVetoHookTable = p;
650 void CtdlDestroyTDAPVetoHooks(void)
652 TDAPVetoHookFunctionHook *cur, *p;
654 cur = TDAPVetoHookTable;
657 MODM_syslog(LOG_DEBUG, "Destroyed TDAP Veto function\n");
662 TDAPVetoHookTable = NULL;
666 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
668 SessionFunctionHook *newfcn;
670 newfcn = (SessionFunctionHook *)
671 malloc(sizeof(SessionFunctionHook));
672 newfcn->Priority = Priority;
673 newfcn->h_function_pointer = fcn_ptr;
674 newfcn->eventtype = EventType;
676 SessionFunctionHook **pfcn;
677 pfcn = &SessionHookTable;
678 while ((*pfcn != NULL) &&
679 ((*pfcn)->Priority < newfcn->Priority) &&
680 ((*pfcn)->next != NULL))
681 pfcn = &(*pfcn)->next;
683 newfcn->next = *pfcn;
686 MOD_syslog(LOG_DEBUG, "Registered a new session function (type %d Priority %d)\n",
687 EventType, Priority);
691 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
693 SessionFunctionHook *cur, *p, *last;
695 cur = SessionHookTable;
696 while (cur != NULL) {
697 if ((fcn_ptr == cur->h_function_pointer) &&
698 (EventType == cur->eventtype))
700 MOD_syslog(LOG_DEBUG, "Unregistered session function (type %d)\n",
710 SessionHookTable = p;
720 void CtdlDestroySessionHooks(void)
722 SessionFunctionHook *cur, *p;
724 cur = SessionHookTable;
727 MODM_syslog(LOG_DEBUG, "Destroyed session function\n");
732 SessionHookTable = NULL;
736 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
739 UserFunctionHook *newfcn;
741 newfcn = (UserFunctionHook *)
742 malloc(sizeof(UserFunctionHook));
743 newfcn->next = UserHookTable;
744 newfcn->h_function_pointer = fcn_ptr;
745 newfcn->eventtype = EventType;
746 UserHookTable = newfcn;
748 MOD_syslog(LOG_DEBUG, "Registered a new user function (type %d)\n",
753 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
755 UserFunctionHook *cur, *p, *last;
758 while (cur != NULL) {
759 if ((fcn_ptr == cur->h_function_pointer) &&
760 (EventType == cur->eventtype))
762 MOD_syslog(LOG_DEBUG, "Unregistered user function (type %d)\n",
782 void CtdlDestroyUserHooks(void)
784 UserFunctionHook *cur, *p;
789 MODM_syslog(LOG_DEBUG, "Destroyed user function \n");
794 UserHookTable = NULL;
798 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
802 MessageFunctionHook *newfcn;
804 newfcn = (MessageFunctionHook *)
805 malloc(sizeof(MessageFunctionHook));
806 newfcn->next = MessageHookTable;
807 newfcn->h_function_pointer = handler;
808 newfcn->eventtype = EventType;
809 MessageHookTable = newfcn;
811 MOD_syslog(LOG_DEBUG, "Registered a new message function (type %d)\n",
816 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
819 MessageFunctionHook *cur, *p, *last;
821 cur = MessageHookTable;
822 while (cur != NULL) {
823 if ((handler == cur->h_function_pointer) &&
824 (EventType == cur->eventtype))
826 MOD_syslog(LOG_DEBUG, "Unregistered message function (type %d)\n",
835 MessageHookTable = p;
845 void CtdlDestroyMessageHook(void)
847 MessageFunctionHook *cur, *p;
849 cur = MessageHookTable;
852 MOD_syslog(LOG_DEBUG, "Destroyed message function (type %d)\n", cur->eventtype);
857 MessageHookTable = NULL;
861 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
863 RoomFunctionHook *newfcn;
865 newfcn = (RoomFunctionHook *)
866 malloc(sizeof(RoomFunctionHook));
867 newfcn->next = RoomHookTable;
868 newfcn->fcn_ptr = fcn_ptr;
869 RoomHookTable = newfcn;
871 MODM_syslog(LOG_DEBUG, "Registered a new room function\n");
875 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
877 RoomFunctionHook *cur, *p, *last;
882 if (fcn_ptr == cur->fcn_ptr) {
883 MODM_syslog(LOG_DEBUG, "Unregistered room function\n");
903 void CtdlDestroyRoomHooks(void)
905 RoomFunctionHook *cur, *p;
910 MODM_syslog(LOG_DEBUG, "Destroyed room function\n");
915 RoomHookTable = NULL;
918 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
920 NetprocFunctionHook *newfcn;
922 newfcn = (NetprocFunctionHook *)
923 malloc(sizeof(NetprocFunctionHook));
924 newfcn->next = NetprocHookTable;
925 newfcn->h_function_pointer = handler;
926 NetprocHookTable = newfcn;
928 MODM_syslog(LOG_DEBUG, "Registered a new netproc function\n");
932 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
934 NetprocFunctionHook *cur, *p, *last;
936 cur = NetprocHookTable;
939 while (cur != NULL) {
940 if (handler == cur->h_function_pointer)
942 MODM_syslog(LOG_DEBUG, "Unregistered netproc function\n");
949 NetprocHookTable = p;
960 void CtdlDestroyNetprocHooks(void)
962 NetprocFunctionHook *cur, *p;
964 cur = NetprocHookTable;
967 MODM_syslog(LOG_DEBUG, "Destroyed netproc function\n");
972 NetprocHookTable = NULL;
976 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
978 DeleteFunctionHook *newfcn;
980 newfcn = (DeleteFunctionHook *)
981 malloc(sizeof(DeleteFunctionHook));
982 newfcn->next = DeleteHookTable;
983 newfcn->h_function_pointer = handler;
984 DeleteHookTable = newfcn;
986 MODM_syslog(LOG_DEBUG, "Registered a new delete function\n");
990 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
992 DeleteFunctionHook *cur, *p, *last;
995 cur = DeleteHookTable;
996 while (cur != NULL) {
997 if (handler == cur->h_function_pointer )
999 MODM_syslog(LOG_DEBUG, "Unregistered delete function\n");
1006 DeleteHookTable = p;
1016 void CtdlDestroyDeleteHooks(void)
1018 DeleteFunctionHook *cur, *p;
1020 cur = DeleteHookTable;
1023 MODM_syslog(LOG_DEBUG, "Destroyed delete function\n");
1028 DeleteHookTable = NULL;
1034 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
1036 FixedOutputHook *newfcn;
1038 newfcn = (FixedOutputHook *)
1039 malloc(sizeof(FixedOutputHook));
1040 newfcn->next = FixedOutputTable;
1041 newfcn->h_function_pointer = handler;
1042 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
1043 FixedOutputTable = newfcn;
1045 MOD_syslog(LOG_DEBUG, "Registered a new fixed output function for %s\n", newfcn->content_type);
1049 void CtdlUnregisterFixedOutputHook(char *content_type)
1051 FixedOutputHook *cur, *p, *last;
1054 cur = FixedOutputTable;
1055 while (cur != NULL) {
1056 /* This will also remove duplicates if any */
1057 if (!strcasecmp(content_type, cur->content_type)) {
1058 MOD_syslog(LOG_DEBUG,
1059 "Unregistered fixed output function for %s\n",
1068 FixedOutputTable = p;
1080 void CtdlDestroyFixedOutputHooks(void)
1082 FixedOutputHook *cur, *p;
1084 cur = FixedOutputTable;
1087 MOD_syslog(LOG_DEBUG, "Destroyed fixed output function for %s\n", cur->content_type);
1093 FixedOutputTable = NULL;
1096 /* returns nonzero if we found a hook and used it */
1097 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
1099 FixedOutputHook *fcn;
1101 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
1102 if (!strcasecmp(content_type, fcn->content_type)) {
1103 (*fcn->h_function_pointer) (content, content_length);
1114 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1117 XmsgFunctionHook *newfcn;
1119 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
1120 newfcn->next = XmsgHookTable;
1121 newfcn->order = order;
1122 newfcn->h_function_pointer = fcn_ptr;
1123 XmsgHookTable = newfcn;
1124 MOD_syslog(LOG_DEBUG, "Registered a new x-msg function (priority %d)\n", order);
1128 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1130 XmsgFunctionHook *cur, *p, *last;
1133 cur = XmsgHookTable;
1134 while (cur != NULL) {
1135 /* This will also remove duplicates if any */
1136 if (fcn_ptr == cur->h_function_pointer &&
1137 order == cur->order) {
1138 MOD_syslog(LOG_DEBUG, "Unregistered x-msg function "
1139 "(priority %d)\n", order);
1157 void CtdlDestroyXmsgHooks(void)
1159 XmsgFunctionHook *cur, *p;
1161 cur = XmsgHookTable;
1164 MOD_syslog(LOG_DEBUG, "Destroyed x-msg function "
1165 "(priority %d)\n", cur->order);
1171 XmsgHookTable = NULL;
1175 void CtdlRegisterServiceHook(int tcp_port,
1177 void (*h_greeting_function) (void),
1178 void (*h_command_function) (void),
1179 void (*h_async_function) (void),
1180 const char *ServiceName)
1182 ServiceFunctionHook *newfcn;
1187 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1188 message = (char*) malloc (SIZ + SIZ);
1190 newfcn->next = ServiceHookTable;
1191 newfcn->tcp_port = tcp_port;
1192 newfcn->sockpath = sockpath;
1193 newfcn->h_greeting_function = h_greeting_function;
1194 newfcn->h_command_function = h_command_function;
1195 newfcn->h_async_function = h_async_function;
1196 newfcn->ServiceName = ServiceName;
1198 if (sockpath != NULL) {
1199 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
1200 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1202 else if (tcp_port <= 0) { /* port -1 to disable */
1203 MOD_syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1209 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
1211 config.c_maxsessions,
1213 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
1214 config.c_ip_addr, tcp_port, ServiceName);
1217 if (newfcn->msock > 0) {
1218 ServiceHookTable = newfcn;
1219 strcat(message, "registered.");
1220 MOD_syslog(LOG_INFO, "%s\n", message);
1223 AddPortError(message, error);
1224 strcat(message, "FAILED.");
1225 MOD_syslog(LOG_CRIT, "%s\n", message);
1232 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1233 void (*h_greeting_function) (void),
1234 void (*h_command_function) (void),
1235 void (*h_async_function) (void)
1238 ServiceFunctionHook *cur, *p, *last;
1241 cur = ServiceHookTable;
1242 while (cur != NULL) {
1243 /* This will also remove duplicates if any */
1244 if (h_greeting_function == cur->h_greeting_function &&
1245 h_command_function == cur->h_command_function &&
1246 h_async_function == cur->h_async_function &&
1247 tcp_port == cur->tcp_port &&
1248 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
1253 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1256 } else if (tcp_port) {
1257 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1259 MOD_syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1266 ServiceHookTable = p;
1277 void CtdlShutdownServiceHooks(void)
1279 /* sort of a duplicate of close_masters() but called earlier */
1280 ServiceFunctionHook *cur;
1282 cur = ServiceHookTable;
1285 if (cur->msock != -1)
1289 if (cur->sockpath != NULL){
1290 MOD_syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1293 unlink(cur->sockpath);
1295 MOD_syslog(LOG_INFO, "[%s] closing service\n",
1303 void CtdlDestroyServiceHook(void)
1306 ServiceFunctionHook *cur, *p;
1308 cur = ServiceHookTable;
1311 if (cur->msock != -1)
1318 Text = " Not closing again";
1321 if (cur->sockpath) {
1322 MOD_syslog(LOG_INFO, "%s UNIX domain socket %s\n",
1325 unlink(cur->sockpath);
1326 } else if (cur->tcp_port) {
1327 MOD_syslog(LOG_INFO, "%s TCP port %d\n", Text, cur->tcp_port);
1329 MOD_syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1335 ServiceHookTable = NULL;
1338 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1340 SearchFunctionHook *newfcn;
1342 if (!name || !fcn_ptr) {
1346 newfcn = (SearchFunctionHook *)
1347 malloc(sizeof(SearchFunctionHook));
1348 newfcn->next = SearchFunctionHookTable;
1349 newfcn->name = name;
1350 newfcn->fcn_ptr = fcn_ptr;
1351 SearchFunctionHookTable = newfcn;
1353 MOD_syslog(LOG_DEBUG, "Registered a new search function (%s)\n", name);
1356 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1358 SearchFunctionHook *cur, *p, *last;
1361 cur = SearchFunctionHookTable;
1362 while (cur != NULL) {
1364 (cur->fcn_ptr == fcn_ptr) &&
1365 name && !strcmp(name, cur->name))
1367 MOD_syslog(LOG_DEBUG, "Unregistered search function(%s)\n", name);
1373 SearchFunctionHookTable = p;
1383 void CtdlDestroySearchHooks(void)
1385 SearchFunctionHook *cur, *p;
1387 cur = SearchFunctionHookTable;
1388 SearchFunctionHookTable = NULL;
1389 while (cur != NULL) {
1396 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1398 SearchFunctionHook *fcn = NULL;
1400 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1401 if (!func_name || !strcmp(func_name, fcn->name)) {
1402 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1409 int CheckTDAPVeto (int DBType, StrBuf *ErrMsg)
1412 TDAPVetoHookFunctionHook *fcn = NULL;
1414 for (fcn = TDAPVetoHookTable; (fcn != NULL) && (Result == 0); fcn = fcn->next) {
1415 if (fcn->eventtype == DBType) {
1416 Result = (*fcn->h_function_pointer) (ErrMsg);
1422 void PerformSessionHooks(int EventType)
1424 SessionFunctionHook *fcn = NULL;
1426 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1427 if (fcn->eventtype == EventType) {
1428 if (EventType == EVT_TIMER) {
1429 pthread_setspecific(MyConKey, NULL); /* for every hook */
1431 (*fcn->h_function_pointer) ();
1436 void PerformUserHooks(ctdluser *usbuf, int EventType)
1438 UserFunctionHook *fcn = NULL;
1440 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1441 if (fcn->eventtype == EventType) {
1442 (*fcn->h_function_pointer) (usbuf);
1447 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
1449 MessageFunctionHook *fcn = NULL;
1450 int total_retval = 0;
1452 /* Other code may elect to protect this message from server-side
1453 * handlers; if this is the case, don't do anything.
1454 MOD_syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1456 if (msg->cm_flags & CM_SKIP_HOOKS) {
1457 MODM_syslog(LOG_DEBUG, "Skipping hooks\n");
1461 /* Otherwise, run all the hooks appropriate to this event type.
1463 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1464 if (fcn->eventtype == EventType) {
1465 total_retval = total_retval + (*fcn->h_function_pointer) (msg);
1469 /* Return the sum of the return codes from the hook functions. If
1470 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1471 * the save operation to abort.
1473 return total_retval;
1477 int PerformRoomHooks(struct ctdlroom *target_room)
1479 RoomFunctionHook *fcn;
1480 int total_retval = 0;
1482 MOD_syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1484 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1485 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1488 /* Return the sum of the return codes from the hook functions.
1490 return total_retval;
1494 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1496 NetprocFunctionHook *fcn;
1497 int total_retval = 0;
1499 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1500 total_retval = total_retval +
1501 (*fcn->h_function_pointer) (msg, target_room);
1504 /* Return the sum of the return codes from the hook functions.
1505 * A nonzero return code will cause the message to *not* be imported.
1507 return total_retval;
1511 void PerformDeleteHooks(char *room, long msgnum)
1513 DeleteFunctionHook *fcn;
1515 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1516 (*fcn->h_function_pointer) (room, msgnum);
1524 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1526 XmsgFunctionHook *fcn;
1530 for (p=0; p<MAX_XMSG_PRI; ++p) {
1531 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1532 if (fcn->order == p) {
1534 (*fcn->h_function_pointer)
1535 (sender, sender_email, recp, msg);
1538 /* Break out of the loop if a higher-priority function
1539 * successfully delivered the message. This prevents duplicate
1540 * deliveries to local users simultaneously signed onto
1543 if (total_sent) break;
1550 * Dirty hack until we impliment a hook mechanism for this
1552 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1555 CtdlStartTLS (ok_response, nosup_response, error_response);
1559 void DebugModulesEnable(const int n)
1563 CTDL_MODULE_INIT(modules)
1566 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);
1568 CtdlRegisterProtoHook(cmd_log_get, "LOGP", "Print Log-parameters");
1569 CtdlRegisterProtoHook(cmd_log_set, "LOGS", "Set Log-parameters");