2 * Citadel Extension Loader
3 * Written by Brian Costello <btx@calyx.net>
5 * Copyright (c) 1987-2015 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"
28 int EnableMarkers = 0;
29 int EnableCtlProto = 0;
32 * Structure defentitions for hook tables
35 HashList *LogDebugEntryTable = NULL;
37 typedef struct LogFunctionHook LogFunctionHook;
38 struct LogFunctionHook {
39 LogFunctionHook *next;
41 void (*h_function_pointer) (char *);
44 LogFunctionHook *LogHookTable = NULL;
46 typedef struct FixedOutputHook FixedOutputHook;
47 struct FixedOutputHook {
48 FixedOutputHook *next;
49 char content_type[64];
50 void (*h_function_pointer) (char *, int);
52 FixedOutputHook *FixedOutputTable = NULL;
57 * TDAPVetoHookFunctionHook extensions are used for any type of hook for which
58 * may prevent the autopurger to run for this specific data class.
59 * the function should at least LOG_INFO that it does so.
61 typedef struct TDAPVetoHookFunctionHook TDAPVetoHookFunctionHook;
62 struct TDAPVetoHookFunctionHook {
63 TDAPVetoHookFunctionHook *next;
65 int (*h_function_pointer) (StrBuf *);
68 TDAPVetoHookFunctionHook *TDAPVetoHookTable = NULL;
73 * SessionFunctionHook extensions are used for any type of hook for which
74 * the context in which it's being called (which is determined by the event
75 * type) will make it obvious for the hook function to know where to look for
78 typedef struct SessionFunctionHook SessionFunctionHook;
79 struct SessionFunctionHook {
80 SessionFunctionHook *next;
82 void (*h_function_pointer) (void);
85 SessionFunctionHook *SessionHookTable = NULL;
88 * UserFunctionHook extensions are used for any type of hook which implements
89 * an operation on a user or username (potentially) other than the one
90 * operating the current session.
92 typedef struct UserFunctionHook UserFunctionHook;
93 struct UserFunctionHook {
94 UserFunctionHook *next;
95 void (*h_function_pointer) (struct ctdluser *usbuf);
98 UserFunctionHook *UserHookTable = NULL;
101 * MessageFunctionHook extensions are used for hooks which implement handlers
102 * for various types of message operations (save, read, etc.)
104 typedef struct MessageFunctionHook MessageFunctionHook;
105 struct MessageFunctionHook {
106 MessageFunctionHook *next;
107 int (*h_function_pointer) (struct CtdlMessage *msg, recptypes *recps);
110 MessageFunctionHook *MessageHookTable = NULL;
114 * NetprocFunctionHook extensions are used for hooks which implement handlers
115 * for incoming network messages.
117 typedef struct NetprocFunctionHook NetprocFunctionHook;
118 struct NetprocFunctionHook {
119 NetprocFunctionHook *next;
120 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
122 NetprocFunctionHook *NetprocHookTable = NULL;
126 * DeleteFunctionHook extensions are used for hooks which get called when a
127 * message is about to be deleted.
129 typedef struct DeleteFunctionHook DeleteFunctionHook;
130 struct DeleteFunctionHook {
131 DeleteFunctionHook *next;
132 void (*h_function_pointer) (char *target_room, long msgnum);
134 DeleteFunctionHook *DeleteHookTable = NULL;
138 * ExpressMessageFunctionHook extensions are used for hooks which implement
139 * the sending of an instant message through various channels. Any function
140 * registered should return the number of recipients to whom the message was
141 * successfully transmitted.
143 typedef struct XmsgFunctionHook XmsgFunctionHook;
144 struct XmsgFunctionHook {
145 XmsgFunctionHook *next;
146 int (*h_function_pointer) (char *, char *, char *, char *);
149 XmsgFunctionHook *XmsgHookTable = NULL;
155 * RoomFunctionHook extensions are used for hooks which impliment room
156 * processing functions when new messages are added EG. SIEVE.
158 typedef struct RoomFunctionHook RoomFunctionHook;
159 struct RoomFunctionHook {
160 RoomFunctionHook *next;
161 int (*fcn_ptr) (struct ctdlroom *);
163 RoomFunctionHook *RoomHookTable = NULL;
167 typedef struct SearchFunctionHook SearchFunctionHook;
168 struct SearchFunctionHook {
169 SearchFunctionHook *next;
170 void (*fcn_ptr) (int *, long **, const char *);
173 SearchFunctionHook *SearchFunctionHookTable = NULL;
175 CleanupFunctionHook *CleanupHookTable = NULL;
176 CleanupFunctionHook *EVCleanupHookTable = NULL;
178 ServiceFunctionHook *ServiceHookTable = NULL;
180 typedef struct ProtoFunctionHook ProtoFunctionHook;
181 struct ProtoFunctionHook {
182 void (*handler) (char *cmdbuf);
187 HashList *ProtoHookList = NULL;
190 #define ERR_PORT (1 << 1)
193 static StrBuf *portlist = NULL;
195 static StrBuf *errormessages = NULL;
198 long DetailErrorFlags;
199 ConstStr Empty = {HKEY("")};
200 char *ErrSubject = "Startup Problems";
201 ConstStr ErrGeneral[] = {
202 {HKEY("Citadel had trouble on starting up. ")},
203 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
204 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
205 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
206 "The errors returned by the system were:\n")},
207 {HKEY("You can recheck the above if you follow this faq item:\n"
208 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
211 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
212 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
213 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
214 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
215 " and stop the program that binds these ports.\n You should eventually remove "
216 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
217 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
220 void LogPrintMessages(long err)
223 StrBuf *List, *DetailList;
224 ConstStr *Short, *Where, *Hint;
227 Message = NewStrBufPlain(NULL,
228 StrLength(portlist) + StrLength(errormessages));
230 DetailErrorFlags = DetailErrorFlags & ~err;
235 Short = &ErrPortShort;
236 Where = &ErrPortWhere;
239 DetailList = errormessages;
249 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
250 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
251 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
252 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
253 StrBufAppendBuf(Message, List, 0);
254 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
255 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
256 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
257 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
258 StrBufAppendBuf(Message, DetailList, 0);
259 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
260 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
262 MOD_syslog(LOG_EMERG, "%s", ChrPtr(Message));
263 MOD_syslog(LOG_EMERG, "%s", ErrSubject);
264 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
266 FreeStrBuf(&Message);
268 FreeStrBuf(&DetailList);
272 void AddPortError(char *Port, char *ErrorMessage)
276 DetailErrorFlags |= ERR_PORT;
278 len = StrLength(errormessages);
279 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
280 else errormessages = NewStrBuf();
281 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
284 len = StrLength(portlist);
285 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
286 else portlist = NewStrBuf();
287 StrBufAppendBufPlain(portlist, Port, -1, 0);
291 int DLoader_Exec_Cmd(char *cmdbuf)
294 ProtoFunctionHook *p;
296 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
297 p = (ProtoFunctionHook*) vP;
298 p->handler(&cmdbuf[5]);
304 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
307 if (LogDebugEntryTable == NULL)
308 LogDebugEntryTable = NewHash(1, NULL);
309 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
314 Put(LogDebugEntryTable, Name, Len, E, NULL);
317 void CtdlSetDebugLogFacilities(const char **Str, long n)
319 StrBuf *Token = NULL;
326 for (i=0; i < n; i++){
327 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
328 if (strcmp(Str[i], "all") == 0) {
332 Buf = NewStrBufPlain(Str[i], -1);
335 Token = NewStrBufPlain(NULL, StrLength(Buf));
336 while ((ch != StrBufNOTNULL) &&
337 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
338 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
341 LogDebugEntry *E = (LogDebugEntry*)vptr;
354 Pos = GetNewHashPos(LogDebugEntryTable, 0);
355 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
356 LogDebugEntry *E = (LogDebugEntry*)vptr;
363 void CtdlDestroyDebugTable(void)
366 DeleteHash(&LogDebugEntryTable);
369 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
371 ProtoFunctionHook *p;
373 if (ProtoHookList == NULL)
374 ProtoHookList = NewHash (1, FourHash);
377 p = (ProtoFunctionHook *)
378 malloc(sizeof(ProtoFunctionHook));
381 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
384 p->handler = handler;
388 Put(ProtoHookList, cmd, 4, p, NULL);
389 MOD_syslog(LOG_DEBUG, "Registered server command %s (%s)\n", cmd, desc);
392 void CtdlDestroyProtoHooks(void)
395 DeleteHash(&ProtoHookList);
399 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
402 CleanupFunctionHook *newfcn;
404 newfcn = (CleanupFunctionHook *)
405 malloc(sizeof(CleanupFunctionHook));
406 newfcn->next = CleanupHookTable;
407 newfcn->h_function_pointer = fcn_ptr;
408 CleanupHookTable = newfcn;
410 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
414 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
416 CleanupFunctionHook *cur, *p, *last;
418 cur = CleanupHookTable;
421 if (fcn_ptr == cur->h_function_pointer)
423 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
432 CleanupHookTable = p;
443 void CtdlDestroyCleanupHooks(void)
445 CleanupFunctionHook *cur, *p;
447 cur = CleanupHookTable;
450 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
455 CleanupHookTable = NULL;
458 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
461 CleanupFunctionHook *newfcn;
463 newfcn = (CleanupFunctionHook *)
464 malloc(sizeof(CleanupFunctionHook));
465 newfcn->next = EVCleanupHookTable;
466 newfcn->h_function_pointer = fcn_ptr;
467 EVCleanupHookTable = newfcn;
469 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
473 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
475 CleanupFunctionHook *cur, *p, *last;
477 cur = EVCleanupHookTable;
480 if (fcn_ptr == cur->h_function_pointer)
482 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
491 EVCleanupHookTable = p;
502 void CtdlDestroyEVCleanupHooks(void)
504 CleanupFunctionHook *cur, *p;
506 cur = EVCleanupHookTable;
509 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
511 cur->h_function_pointer();
515 EVCleanupHookTable = NULL;
518 void CtdlRegisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType, int Priority)
520 TDAPVetoHookFunctionHook *newfcn;
522 newfcn = (TDAPVetoHookFunctionHook *)
523 malloc(sizeof(TDAPVetoHookFunctionHook));
524 newfcn->Priority = Priority;
525 newfcn->h_function_pointer = fcn_ptr;
526 newfcn->eventtype = EventType;
528 TDAPVetoHookFunctionHook **pfcn;
529 pfcn = &TDAPVetoHookTable;
530 while ((*pfcn != NULL) &&
531 ((*pfcn)->Priority < newfcn->Priority) &&
532 ((*pfcn)->next != NULL))
533 pfcn = &(*pfcn)->next;
535 newfcn->next = *pfcn;
538 MOD_syslog(LOG_DEBUG, "Registered a new TDAP Veto function (type %d Priority %d)\n",
539 EventType, Priority);
543 void CtdlUnregisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType)
545 TDAPVetoHookFunctionHook *cur, *p, *last;
547 cur = TDAPVetoHookTable;
548 while (cur != NULL) {
549 if ((fcn_ptr == cur->h_function_pointer) &&
550 (EventType == cur->eventtype))
552 MOD_syslog(LOG_DEBUG, "Unregistered TDAP Veto function (type %d)\n",
562 TDAPVetoHookTable = p;
572 void CtdlDestroyTDAPVetoHooks(void)
574 TDAPVetoHookFunctionHook *cur, *p;
576 cur = TDAPVetoHookTable;
579 MODM_syslog(LOG_DEBUG, "Destroyed TDAP Veto function\n");
584 TDAPVetoHookTable = NULL;
588 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
590 SessionFunctionHook *newfcn;
592 newfcn = (SessionFunctionHook *)
593 malloc(sizeof(SessionFunctionHook));
594 newfcn->Priority = Priority;
595 newfcn->h_function_pointer = fcn_ptr;
596 newfcn->eventtype = EventType;
598 SessionFunctionHook **pfcn;
599 pfcn = &SessionHookTable;
600 while ((*pfcn != NULL) &&
601 ((*pfcn)->Priority < newfcn->Priority) &&
602 ((*pfcn)->next != NULL))
603 pfcn = &(*pfcn)->next;
605 newfcn->next = *pfcn;
608 MOD_syslog(LOG_DEBUG, "Registered a new session function (type %d Priority %d)\n",
609 EventType, Priority);
613 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
615 SessionFunctionHook *cur, *p, *last;
617 cur = SessionHookTable;
618 while (cur != NULL) {
619 if ((fcn_ptr == cur->h_function_pointer) &&
620 (EventType == cur->eventtype))
622 MOD_syslog(LOG_DEBUG, "Unregistered session function (type %d)\n",
632 SessionHookTable = p;
642 void CtdlDestroySessionHooks(void)
644 SessionFunctionHook *cur, *p;
646 cur = SessionHookTable;
649 MODM_syslog(LOG_DEBUG, "Destroyed session function\n");
654 SessionHookTable = NULL;
658 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
661 UserFunctionHook *newfcn;
663 newfcn = (UserFunctionHook *)
664 malloc(sizeof(UserFunctionHook));
665 newfcn->next = UserHookTable;
666 newfcn->h_function_pointer = fcn_ptr;
667 newfcn->eventtype = EventType;
668 UserHookTable = newfcn;
670 MOD_syslog(LOG_DEBUG, "Registered a new user function (type %d)\n",
675 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
677 UserFunctionHook *cur, *p, *last;
680 while (cur != NULL) {
681 if ((fcn_ptr == cur->h_function_pointer) &&
682 (EventType == cur->eventtype))
684 MOD_syslog(LOG_DEBUG, "Unregistered user function (type %d)\n",
704 void CtdlDestroyUserHooks(void)
706 UserFunctionHook *cur, *p;
711 MODM_syslog(LOG_DEBUG, "Destroyed user function \n");
716 UserHookTable = NULL;
720 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
724 MessageFunctionHook *newfcn;
726 newfcn = (MessageFunctionHook *)
727 malloc(sizeof(MessageFunctionHook));
728 newfcn->next = MessageHookTable;
729 newfcn->h_function_pointer = handler;
730 newfcn->eventtype = EventType;
731 MessageHookTable = newfcn;
733 MOD_syslog(LOG_DEBUG, "Registered a new message function (type %d)\n",
738 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
741 MessageFunctionHook *cur, *p, *last;
743 cur = MessageHookTable;
744 while (cur != NULL) {
745 if ((handler == cur->h_function_pointer) &&
746 (EventType == cur->eventtype))
748 MOD_syslog(LOG_DEBUG, "Unregistered message function (type %d)\n",
757 MessageHookTable = p;
767 void CtdlDestroyMessageHook(void)
769 MessageFunctionHook *cur, *p;
771 cur = MessageHookTable;
774 MOD_syslog(LOG_DEBUG, "Destroyed message function (type %d)\n", cur->eventtype);
779 MessageHookTable = NULL;
783 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
785 RoomFunctionHook *newfcn;
787 newfcn = (RoomFunctionHook *)
788 malloc(sizeof(RoomFunctionHook));
789 newfcn->next = RoomHookTable;
790 newfcn->fcn_ptr = fcn_ptr;
791 RoomHookTable = newfcn;
793 MODM_syslog(LOG_DEBUG, "Registered a new room function\n");
797 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
799 RoomFunctionHook *cur, *p, *last;
804 if (fcn_ptr == cur->fcn_ptr) {
805 MODM_syslog(LOG_DEBUG, "Unregistered room function\n");
825 void CtdlDestroyRoomHooks(void)
827 RoomFunctionHook *cur, *p;
832 MODM_syslog(LOG_DEBUG, "Destroyed room function\n");
837 RoomHookTable = NULL;
840 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
842 NetprocFunctionHook *newfcn;
844 newfcn = (NetprocFunctionHook *)
845 malloc(sizeof(NetprocFunctionHook));
846 newfcn->next = NetprocHookTable;
847 newfcn->h_function_pointer = handler;
848 NetprocHookTable = newfcn;
850 MODM_syslog(LOG_DEBUG, "Registered a new netproc function\n");
854 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
856 NetprocFunctionHook *cur, *p, *last;
858 cur = NetprocHookTable;
861 while (cur != NULL) {
862 if (handler == cur->h_function_pointer)
864 MODM_syslog(LOG_DEBUG, "Unregistered netproc function\n");
871 NetprocHookTable = p;
882 void CtdlDestroyNetprocHooks(void)
884 NetprocFunctionHook *cur, *p;
886 cur = NetprocHookTable;
889 MODM_syslog(LOG_DEBUG, "Destroyed netproc function\n");
894 NetprocHookTable = NULL;
898 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
900 DeleteFunctionHook *newfcn;
902 newfcn = (DeleteFunctionHook *)
903 malloc(sizeof(DeleteFunctionHook));
904 newfcn->next = DeleteHookTable;
905 newfcn->h_function_pointer = handler;
906 DeleteHookTable = newfcn;
908 MODM_syslog(LOG_DEBUG, "Registered a new delete function\n");
912 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
914 DeleteFunctionHook *cur, *p, *last;
917 cur = DeleteHookTable;
918 while (cur != NULL) {
919 if (handler == cur->h_function_pointer )
921 MODM_syslog(LOG_DEBUG, "Unregistered delete function\n");
938 void CtdlDestroyDeleteHooks(void)
940 DeleteFunctionHook *cur, *p;
942 cur = DeleteHookTable;
945 MODM_syslog(LOG_DEBUG, "Destroyed delete function\n");
950 DeleteHookTable = NULL;
956 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
958 FixedOutputHook *newfcn;
960 newfcn = (FixedOutputHook *)
961 malloc(sizeof(FixedOutputHook));
962 newfcn->next = FixedOutputTable;
963 newfcn->h_function_pointer = handler;
964 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
965 FixedOutputTable = newfcn;
967 MOD_syslog(LOG_DEBUG, "Registered a new fixed output function for %s\n", newfcn->content_type);
971 void CtdlUnregisterFixedOutputHook(char *content_type)
973 FixedOutputHook *cur, *p, *last;
976 cur = FixedOutputTable;
977 while (cur != NULL) {
978 /* This will also remove duplicates if any */
979 if (!strcasecmp(content_type, cur->content_type)) {
980 MOD_syslog(LOG_DEBUG,
981 "Unregistered fixed output function for %s\n",
990 FixedOutputTable = p;
1002 void CtdlDestroyFixedOutputHooks(void)
1004 FixedOutputHook *cur, *p;
1006 cur = FixedOutputTable;
1009 MOD_syslog(LOG_DEBUG, "Destroyed fixed output function for %s\n", cur->content_type);
1015 FixedOutputTable = NULL;
1018 /* returns nonzero if we found a hook and used it */
1019 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
1021 FixedOutputHook *fcn;
1023 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
1024 if (!strcasecmp(content_type, fcn->content_type)) {
1025 (*fcn->h_function_pointer) (content, content_length);
1036 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1039 XmsgFunctionHook *newfcn;
1041 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
1042 newfcn->next = XmsgHookTable;
1043 newfcn->order = order;
1044 newfcn->h_function_pointer = fcn_ptr;
1045 XmsgHookTable = newfcn;
1046 MOD_syslog(LOG_DEBUG, "Registered a new x-msg function (priority %d)\n", order);
1050 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1052 XmsgFunctionHook *cur, *p, *last;
1055 cur = XmsgHookTable;
1056 while (cur != NULL) {
1057 /* This will also remove duplicates if any */
1058 if (fcn_ptr == cur->h_function_pointer &&
1059 order == cur->order) {
1060 MOD_syslog(LOG_DEBUG, "Unregistered x-msg function "
1061 "(priority %d)\n", order);
1079 void CtdlDestroyXmsgHooks(void)
1081 XmsgFunctionHook *cur, *p;
1083 cur = XmsgHookTable;
1086 MOD_syslog(LOG_DEBUG, "Destroyed x-msg function "
1087 "(priority %d)\n", cur->order);
1093 XmsgHookTable = NULL;
1097 void CtdlRegisterServiceHook(int tcp_port,
1099 void (*h_greeting_function) (void),
1100 void (*h_command_function) (void),
1101 void (*h_async_function) (void),
1102 const char *ServiceName)
1104 ServiceFunctionHook *newfcn;
1109 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1110 message = (char*) malloc (SIZ + SIZ);
1112 newfcn->next = ServiceHookTable;
1113 newfcn->tcp_port = tcp_port;
1114 newfcn->sockpath = sockpath;
1115 newfcn->h_greeting_function = h_greeting_function;
1116 newfcn->h_command_function = h_command_function;
1117 newfcn->h_async_function = h_async_function;
1118 newfcn->ServiceName = ServiceName;
1120 if (sockpath != NULL) {
1121 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"), error);
1122 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1124 else if (tcp_port <= 0) { /* port -1 to disable */
1125 MOD_syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1131 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"),
1133 CtdlGetConfigInt("c_maxsessions"),
1135 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
1136 CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
1139 if (newfcn->msock > 0) {
1140 ServiceHookTable = newfcn;
1141 strcat(message, "registered.");
1142 MOD_syslog(LOG_INFO, "%s\n", message);
1145 AddPortError(message, error);
1146 strcat(message, "FAILED.");
1147 MOD_syslog(LOG_CRIT, "%s\n", message);
1154 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1155 void (*h_greeting_function) (void),
1156 void (*h_command_function) (void),
1157 void (*h_async_function) (void)
1160 ServiceFunctionHook *cur, *p, *last;
1163 cur = ServiceHookTable;
1164 while (cur != NULL) {
1165 /* This will also remove duplicates if any */
1166 if (h_greeting_function == cur->h_greeting_function &&
1167 h_command_function == cur->h_command_function &&
1168 h_async_function == cur->h_async_function &&
1169 tcp_port == cur->tcp_port &&
1170 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
1175 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1178 } else if (tcp_port) {
1179 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1181 MOD_syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1188 ServiceHookTable = p;
1199 void CtdlShutdownServiceHooks(void)
1201 /* sort of a duplicate of close_masters() but called earlier */
1202 ServiceFunctionHook *cur;
1204 cur = ServiceHookTable;
1207 if (cur->msock != -1)
1211 if (cur->sockpath != NULL){
1212 MOD_syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1215 unlink(cur->sockpath);
1217 MOD_syslog(LOG_INFO, "[%s] closing service\n",
1225 void CtdlDestroyServiceHook(void)
1228 ServiceFunctionHook *cur, *p;
1230 cur = ServiceHookTable;
1233 if (cur->msock != -1)
1240 Text = " Not closing again";
1243 if (cur->sockpath) {
1244 MOD_syslog(LOG_INFO, "%s UNIX domain socket %s\n",
1247 unlink(cur->sockpath);
1248 } else if (cur->tcp_port) {
1249 MOD_syslog(LOG_INFO, "%s TCP port %d\n", Text, cur->tcp_port);
1251 MOD_syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1257 ServiceHookTable = NULL;
1260 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1262 SearchFunctionHook *newfcn;
1264 if (!name || !fcn_ptr) {
1268 newfcn = (SearchFunctionHook *)
1269 malloc(sizeof(SearchFunctionHook));
1270 newfcn->next = SearchFunctionHookTable;
1271 newfcn->name = name;
1272 newfcn->fcn_ptr = fcn_ptr;
1273 SearchFunctionHookTable = newfcn;
1275 MOD_syslog(LOG_DEBUG, "Registered a new search function (%s)\n", name);
1278 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1280 SearchFunctionHook *cur, *p, *last;
1283 cur = SearchFunctionHookTable;
1284 while (cur != NULL) {
1286 (cur->fcn_ptr == fcn_ptr) &&
1287 name && !strcmp(name, cur->name))
1289 MOD_syslog(LOG_DEBUG, "Unregistered search function(%s)\n", name);
1295 SearchFunctionHookTable = p;
1305 void CtdlDestroySearchHooks(void)
1307 SearchFunctionHook *cur, *p;
1309 cur = SearchFunctionHookTable;
1310 SearchFunctionHookTable = NULL;
1311 while (cur != NULL) {
1318 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1320 SearchFunctionHook *fcn = NULL;
1322 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1323 if (!func_name || !strcmp(func_name, fcn->name)) {
1324 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1331 int CheckTDAPVeto (int DBType, StrBuf *ErrMsg)
1334 TDAPVetoHookFunctionHook *fcn = NULL;
1336 for (fcn = TDAPVetoHookTable; (fcn != NULL) && (Result == 0); fcn = fcn->next) {
1337 if (fcn->eventtype == DBType) {
1338 Result = (*fcn->h_function_pointer) (ErrMsg);
1344 void PerformSessionHooks(int EventType)
1346 SessionFunctionHook *fcn = NULL;
1348 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1349 if (fcn->eventtype == EventType) {
1350 if (EventType == EVT_TIMER) {
1351 pthread_setspecific(MyConKey, NULL); /* for every hook */
1353 (*fcn->h_function_pointer) ();
1358 void PerformUserHooks(ctdluser *usbuf, int EventType)
1360 UserFunctionHook *fcn = NULL;
1362 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1363 if (fcn->eventtype == EventType) {
1364 (*fcn->h_function_pointer) (usbuf);
1369 int PerformMessageHooks(struct CtdlMessage *msg, recptypes *recps, int EventType)
1371 MessageFunctionHook *fcn = NULL;
1372 int total_retval = 0;
1374 /* Other code may elect to protect this message from server-side
1375 * handlers; if this is the case, don't do anything.
1376 MOD_syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1378 if (msg->cm_flags & CM_SKIP_HOOKS) {
1379 MODM_syslog(LOG_DEBUG, "Skipping hooks\n");
1383 /* Otherwise, run all the hooks appropriate to this event type.
1385 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1386 if (fcn->eventtype == EventType) {
1387 total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
1391 /* Return the sum of the return codes from the hook functions. If
1392 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1393 * the save operation to abort.
1395 return total_retval;
1399 int PerformRoomHooks(struct ctdlroom *target_room)
1401 RoomFunctionHook *fcn;
1402 int total_retval = 0;
1404 MOD_syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1406 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1407 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1410 /* Return the sum of the return codes from the hook functions.
1412 return total_retval;
1416 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1418 NetprocFunctionHook *fcn;
1419 int total_retval = 0;
1421 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1422 total_retval = total_retval +
1423 (*fcn->h_function_pointer) (msg, target_room);
1426 /* Return the sum of the return codes from the hook functions.
1427 * A nonzero return code will cause the message to *not* be imported.
1429 return total_retval;
1433 void PerformDeleteHooks(char *room, long msgnum)
1435 DeleteFunctionHook *fcn;
1437 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1438 (*fcn->h_function_pointer) (room, msgnum);
1446 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1448 XmsgFunctionHook *fcn;
1452 for (p=0; p<MAX_XMSG_PRI; ++p) {
1453 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1454 if (fcn->order == p) {
1456 (*fcn->h_function_pointer)
1457 (sender, sender_email, recp, msg);
1460 /* Break out of the loop if a higher-priority function
1461 * successfully delivered the message. This prevents duplicate
1462 * deliveries to local users simultaneously signed onto
1465 if (total_sent) break;
1472 * Dirty hack until we impliment a hook mechanism for this
1474 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1477 CtdlStartTLS (ok_response, nosup_response, error_response);
1481 void DebugModulesEnable(const int n)
1485 void MarkersEnable(const int n)
1489 void DebugCitadelProtoEnable(const int n)
1494 CTDL_MODULE_INIT(modules)
1497 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);
1498 CtdlRegisterDebugFlagHook(HKEY("periodicmarkers"), MarkersEnable, &EnableMarkers);
1499 CtdlRegisterDebugFlagHook(HKEY("citadelprotocol"), DebugCitadelProtoEnable, &EnableCtlProto);