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"
30 * Structure defentitions for hook tables
33 HashList *LogDebugEntryTable = NULL;
35 typedef struct LogFunctionHook LogFunctionHook;
36 struct LogFunctionHook {
37 LogFunctionHook *next;
39 void (*h_function_pointer) (char *);
42 LogFunctionHook *LogHookTable = NULL;
44 typedef struct FixedOutputHook FixedOutputHook;
45 struct FixedOutputHook {
46 FixedOutputHook *next;
47 char content_type[64];
48 void (*h_function_pointer) (char *, int);
50 FixedOutputHook *FixedOutputTable = NULL;
55 * TDAPVetoHookFunctionHook extensions are used for any type of hook for which
56 * may prevent the autopurger to run for this specific data class.
57 * the function should at least LOG_INFO that it does so.
59 typedef struct TDAPVetoHookFunctionHook TDAPVetoHookFunctionHook;
60 struct TDAPVetoHookFunctionHook {
61 TDAPVetoHookFunctionHook *next;
63 int (*h_function_pointer) (StrBuf *);
66 TDAPVetoHookFunctionHook *TDAPVetoHookTable = NULL;
71 * SessionFunctionHook extensions are used for any type of hook for which
72 * the context in which it's being called (which is determined by the event
73 * type) will make it obvious for the hook function to know where to look for
76 typedef struct SessionFunctionHook SessionFunctionHook;
77 struct SessionFunctionHook {
78 SessionFunctionHook *next;
80 void (*h_function_pointer) (void);
83 SessionFunctionHook *SessionHookTable = NULL;
86 * UserFunctionHook extensions are used for any type of hook which implements
87 * an operation on a user or username (potentially) other than the one
88 * operating the current session.
90 typedef struct UserFunctionHook UserFunctionHook;
91 struct UserFunctionHook {
92 UserFunctionHook *next;
93 void (*h_function_pointer) (struct ctdluser *usbuf);
96 UserFunctionHook *UserHookTable = NULL;
99 * MessageFunctionHook extensions are used for hooks which implement handlers
100 * for various types of message operations (save, read, etc.)
102 typedef struct MessageFunctionHook MessageFunctionHook;
103 struct MessageFunctionHook {
104 MessageFunctionHook *next;
105 int (*h_function_pointer) (struct CtdlMessage *msg, recptypes *recps);
108 MessageFunctionHook *MessageHookTable = NULL;
112 * NetprocFunctionHook extensions are used for hooks which implement handlers
113 * for incoming network messages.
115 typedef struct NetprocFunctionHook NetprocFunctionHook;
116 struct NetprocFunctionHook {
117 NetprocFunctionHook *next;
118 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
120 NetprocFunctionHook *NetprocHookTable = NULL;
124 * DeleteFunctionHook extensions are used for hooks which get called when a
125 * message is about to be deleted.
127 typedef struct DeleteFunctionHook DeleteFunctionHook;
128 struct DeleteFunctionHook {
129 DeleteFunctionHook *next;
130 void (*h_function_pointer) (char *target_room, long msgnum);
132 DeleteFunctionHook *DeleteHookTable = NULL;
136 * ExpressMessageFunctionHook extensions are used for hooks which implement
137 * the sending of an instant message through various channels. Any function
138 * registered should return the number of recipients to whom the message was
139 * successfully transmitted.
141 typedef struct XmsgFunctionHook XmsgFunctionHook;
142 struct XmsgFunctionHook {
143 XmsgFunctionHook *next;
144 int (*h_function_pointer) (char *, char *, char *, char *);
147 XmsgFunctionHook *XmsgHookTable = NULL;
153 * RoomFunctionHook extensions are used for hooks which impliment room
154 * processing functions when new messages are added EG. SIEVE.
156 typedef struct RoomFunctionHook RoomFunctionHook;
157 struct RoomFunctionHook {
158 RoomFunctionHook *next;
159 int (*fcn_ptr) (struct ctdlroom *);
161 RoomFunctionHook *RoomHookTable = NULL;
165 typedef struct SearchFunctionHook SearchFunctionHook;
166 struct SearchFunctionHook {
167 SearchFunctionHook *next;
168 void (*fcn_ptr) (int *, long **, const char *);
171 SearchFunctionHook *SearchFunctionHookTable = NULL;
173 CleanupFunctionHook *CleanupHookTable = NULL;
174 CleanupFunctionHook *EVCleanupHookTable = NULL;
176 ServiceFunctionHook *ServiceHookTable = NULL;
178 typedef struct ProtoFunctionHook ProtoFunctionHook;
179 struct ProtoFunctionHook {
180 void (*handler) (char *cmdbuf);
185 HashList *ProtoHookList = NULL;
188 #define ERR_PORT (1 << 1)
191 static StrBuf *portlist = NULL;
193 static StrBuf *errormessages = NULL;
196 long DetailErrorFlags;
197 ConstStr Empty = {HKEY("")};
198 char *ErrSubject = "Startup Problems";
199 ConstStr ErrGeneral[] = {
200 {HKEY("Citadel had trouble on starting up. ")},
201 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
202 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
203 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
204 "The errors returned by the system were:\n")},
205 {HKEY("You can recheck the above if you follow this faq item:\n"
206 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
209 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
210 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
211 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
212 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
213 " and stop the program that binds these ports.\n You should eventually remove "
214 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
215 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
218 void LogPrintMessages(long err)
221 StrBuf *List, *DetailList;
222 ConstStr *Short, *Where, *Hint;
225 Message = NewStrBufPlain(NULL,
226 StrLength(portlist) + StrLength(errormessages));
228 DetailErrorFlags = DetailErrorFlags & ~err;
233 Short = &ErrPortShort;
234 Where = &ErrPortWhere;
237 DetailList = errormessages;
247 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
248 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
249 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
250 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
251 StrBufAppendBuf(Message, List, 0);
252 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
253 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
254 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
255 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
256 StrBufAppendBuf(Message, DetailList, 0);
257 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
258 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
260 MOD_syslog(LOG_EMERG, "%s", ChrPtr(Message));
261 MOD_syslog(LOG_EMERG, "%s", ErrSubject);
262 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
264 FreeStrBuf(&Message);
266 FreeStrBuf(&DetailList);
270 void AddPortError(char *Port, char *ErrorMessage)
274 DetailErrorFlags |= ERR_PORT;
276 len = StrLength(errormessages);
277 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
278 else errormessages = NewStrBuf();
279 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
282 len = StrLength(portlist);
283 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
284 else portlist = NewStrBuf();
285 StrBufAppendBufPlain(portlist, Port, -1, 0);
289 int DLoader_Exec_Cmd(char *cmdbuf)
292 ProtoFunctionHook *p;
294 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
295 p = (ProtoFunctionHook*) vP;
296 p->handler(&cmdbuf[5]);
302 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
305 if (LogDebugEntryTable == NULL)
306 LogDebugEntryTable = NewHash(1, NULL);
307 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
312 Put(LogDebugEntryTable, Name, Len, E, NULL);
315 void CtdlSetDebugLogFacilities(const char **Str, long n)
317 StrBuf *Token = NULL;
324 for (i=0; i < n; i++){
325 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
326 if (strcmp(Str[i], "all") == 0) {
330 Buf = NewStrBufPlain(Str[i], -1);
333 Token = NewStrBufPlain(NULL, StrLength(Buf));
334 while ((ch != StrBufNOTNULL) &&
335 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
336 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
339 LogDebugEntry *E = (LogDebugEntry*)vptr;
352 Pos = GetNewHashPos(LogDebugEntryTable, 0);
353 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
354 LogDebugEntry *E = (LogDebugEntry*)vptr;
361 void CtdlDestroyDebugTable(void)
364 DeleteHash(&LogDebugEntryTable);
367 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
369 ProtoFunctionHook *p;
371 if (ProtoHookList == NULL)
372 ProtoHookList = NewHash (1, FourHash);
375 p = (ProtoFunctionHook *)
376 malloc(sizeof(ProtoFunctionHook));
379 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
382 p->handler = handler;
386 Put(ProtoHookList, cmd, 4, p, NULL);
387 MOD_syslog(LOG_DEBUG, "Registered server command %s (%s)\n", cmd, desc);
390 void CtdlDestroyProtoHooks(void)
393 DeleteHash(&ProtoHookList);
397 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
400 CleanupFunctionHook *newfcn;
402 newfcn = (CleanupFunctionHook *)
403 malloc(sizeof(CleanupFunctionHook));
404 newfcn->next = CleanupHookTable;
405 newfcn->h_function_pointer = fcn_ptr;
406 CleanupHookTable = newfcn;
408 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
412 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
414 CleanupFunctionHook *cur, *p, *last;
416 cur = CleanupHookTable;
419 if (fcn_ptr == cur->h_function_pointer)
421 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
430 CleanupHookTable = p;
441 void CtdlDestroyCleanupHooks(void)
443 CleanupFunctionHook *cur, *p;
445 cur = CleanupHookTable;
448 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
453 CleanupHookTable = NULL;
456 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
459 CleanupFunctionHook *newfcn;
461 newfcn = (CleanupFunctionHook *)
462 malloc(sizeof(CleanupFunctionHook));
463 newfcn->next = EVCleanupHookTable;
464 newfcn->h_function_pointer = fcn_ptr;
465 EVCleanupHookTable = newfcn;
467 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
471 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
473 CleanupFunctionHook *cur, *p, *last;
475 cur = EVCleanupHookTable;
478 if (fcn_ptr == cur->h_function_pointer)
480 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
489 EVCleanupHookTable = p;
500 void CtdlDestroyEVCleanupHooks(void)
502 CleanupFunctionHook *cur, *p;
504 cur = EVCleanupHookTable;
507 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
509 cur->h_function_pointer();
513 EVCleanupHookTable = NULL;
516 void CtdlRegisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType, int Priority)
518 TDAPVetoHookFunctionHook *newfcn;
520 newfcn = (TDAPVetoHookFunctionHook *)
521 malloc(sizeof(TDAPVetoHookFunctionHook));
522 newfcn->Priority = Priority;
523 newfcn->h_function_pointer = fcn_ptr;
524 newfcn->eventtype = EventType;
526 TDAPVetoHookFunctionHook **pfcn;
527 pfcn = &TDAPVetoHookTable;
528 while ((*pfcn != NULL) &&
529 ((*pfcn)->Priority < newfcn->Priority) &&
530 ((*pfcn)->next != NULL))
531 pfcn = &(*pfcn)->next;
533 newfcn->next = *pfcn;
536 MOD_syslog(LOG_DEBUG, "Registered a new TDAP Veto function (type %d Priority %d)\n",
537 EventType, Priority);
541 void CtdlUnregisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType)
543 TDAPVetoHookFunctionHook *cur, *p, *last;
545 cur = TDAPVetoHookTable;
546 while (cur != NULL) {
547 if ((fcn_ptr == cur->h_function_pointer) &&
548 (EventType == cur->eventtype))
550 MOD_syslog(LOG_DEBUG, "Unregistered TDAP Veto function (type %d)\n",
560 TDAPVetoHookTable = p;
570 void CtdlDestroyTDAPVetoHooks(void)
572 TDAPVetoHookFunctionHook *cur, *p;
574 cur = TDAPVetoHookTable;
577 MODM_syslog(LOG_DEBUG, "Destroyed TDAP Veto function\n");
582 TDAPVetoHookTable = NULL;
586 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
588 SessionFunctionHook *newfcn;
590 newfcn = (SessionFunctionHook *)
591 malloc(sizeof(SessionFunctionHook));
592 newfcn->Priority = Priority;
593 newfcn->h_function_pointer = fcn_ptr;
594 newfcn->eventtype = EventType;
596 SessionFunctionHook **pfcn;
597 pfcn = &SessionHookTable;
598 while ((*pfcn != NULL) &&
599 ((*pfcn)->Priority < newfcn->Priority) &&
600 ((*pfcn)->next != NULL))
601 pfcn = &(*pfcn)->next;
603 newfcn->next = *pfcn;
606 MOD_syslog(LOG_DEBUG, "Registered a new session function (type %d Priority %d)\n",
607 EventType, Priority);
611 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
613 SessionFunctionHook *cur, *p, *last;
615 cur = SessionHookTable;
616 while (cur != NULL) {
617 if ((fcn_ptr == cur->h_function_pointer) &&
618 (EventType == cur->eventtype))
620 MOD_syslog(LOG_DEBUG, "Unregistered session function (type %d)\n",
630 SessionHookTable = p;
640 void CtdlDestroySessionHooks(void)
642 SessionFunctionHook *cur, *p;
644 cur = SessionHookTable;
647 MODM_syslog(LOG_DEBUG, "Destroyed session function\n");
652 SessionHookTable = NULL;
656 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
659 UserFunctionHook *newfcn;
661 newfcn = (UserFunctionHook *)
662 malloc(sizeof(UserFunctionHook));
663 newfcn->next = UserHookTable;
664 newfcn->h_function_pointer = fcn_ptr;
665 newfcn->eventtype = EventType;
666 UserHookTable = newfcn;
668 MOD_syslog(LOG_DEBUG, "Registered a new user function (type %d)\n",
673 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
675 UserFunctionHook *cur, *p, *last;
678 while (cur != NULL) {
679 if ((fcn_ptr == cur->h_function_pointer) &&
680 (EventType == cur->eventtype))
682 MOD_syslog(LOG_DEBUG, "Unregistered user function (type %d)\n",
702 void CtdlDestroyUserHooks(void)
704 UserFunctionHook *cur, *p;
709 MODM_syslog(LOG_DEBUG, "Destroyed user function \n");
714 UserHookTable = NULL;
718 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
722 MessageFunctionHook *newfcn;
724 newfcn = (MessageFunctionHook *)
725 malloc(sizeof(MessageFunctionHook));
726 newfcn->next = MessageHookTable;
727 newfcn->h_function_pointer = handler;
728 newfcn->eventtype = EventType;
729 MessageHookTable = newfcn;
731 MOD_syslog(LOG_DEBUG, "Registered a new message function (type %d)\n",
736 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
739 MessageFunctionHook *cur, *p, *last;
741 cur = MessageHookTable;
742 while (cur != NULL) {
743 if ((handler == cur->h_function_pointer) &&
744 (EventType == cur->eventtype))
746 MOD_syslog(LOG_DEBUG, "Unregistered message function (type %d)\n",
755 MessageHookTable = p;
765 void CtdlDestroyMessageHook(void)
767 MessageFunctionHook *cur, *p;
769 cur = MessageHookTable;
772 MOD_syslog(LOG_DEBUG, "Destroyed message function (type %d)\n", cur->eventtype);
777 MessageHookTable = NULL;
781 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
783 RoomFunctionHook *newfcn;
785 newfcn = (RoomFunctionHook *)
786 malloc(sizeof(RoomFunctionHook));
787 newfcn->next = RoomHookTable;
788 newfcn->fcn_ptr = fcn_ptr;
789 RoomHookTable = newfcn;
791 MODM_syslog(LOG_DEBUG, "Registered a new room function\n");
795 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
797 RoomFunctionHook *cur, *p, *last;
802 if (fcn_ptr == cur->fcn_ptr) {
803 MODM_syslog(LOG_DEBUG, "Unregistered room function\n");
823 void CtdlDestroyRoomHooks(void)
825 RoomFunctionHook *cur, *p;
830 MODM_syslog(LOG_DEBUG, "Destroyed room function\n");
835 RoomHookTable = NULL;
838 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
840 NetprocFunctionHook *newfcn;
842 newfcn = (NetprocFunctionHook *)
843 malloc(sizeof(NetprocFunctionHook));
844 newfcn->next = NetprocHookTable;
845 newfcn->h_function_pointer = handler;
846 NetprocHookTable = newfcn;
848 MODM_syslog(LOG_DEBUG, "Registered a new netproc function\n");
852 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
854 NetprocFunctionHook *cur, *p, *last;
856 cur = NetprocHookTable;
859 while (cur != NULL) {
860 if (handler == cur->h_function_pointer)
862 MODM_syslog(LOG_DEBUG, "Unregistered netproc function\n");
869 NetprocHookTable = p;
880 void CtdlDestroyNetprocHooks(void)
882 NetprocFunctionHook *cur, *p;
884 cur = NetprocHookTable;
887 MODM_syslog(LOG_DEBUG, "Destroyed netproc function\n");
892 NetprocHookTable = NULL;
896 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
898 DeleteFunctionHook *newfcn;
900 newfcn = (DeleteFunctionHook *)
901 malloc(sizeof(DeleteFunctionHook));
902 newfcn->next = DeleteHookTable;
903 newfcn->h_function_pointer = handler;
904 DeleteHookTable = newfcn;
906 MODM_syslog(LOG_DEBUG, "Registered a new delete function\n");
910 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
912 DeleteFunctionHook *cur, *p, *last;
915 cur = DeleteHookTable;
916 while (cur != NULL) {
917 if (handler == cur->h_function_pointer )
919 MODM_syslog(LOG_DEBUG, "Unregistered delete function\n");
936 void CtdlDestroyDeleteHooks(void)
938 DeleteFunctionHook *cur, *p;
940 cur = DeleteHookTable;
943 MODM_syslog(LOG_DEBUG, "Destroyed delete function\n");
948 DeleteHookTable = NULL;
954 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
956 FixedOutputHook *newfcn;
958 newfcn = (FixedOutputHook *)
959 malloc(sizeof(FixedOutputHook));
960 newfcn->next = FixedOutputTable;
961 newfcn->h_function_pointer = handler;
962 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
963 FixedOutputTable = newfcn;
965 MOD_syslog(LOG_DEBUG, "Registered a new fixed output function for %s\n", newfcn->content_type);
969 void CtdlUnregisterFixedOutputHook(char *content_type)
971 FixedOutputHook *cur, *p, *last;
974 cur = FixedOutputTable;
975 while (cur != NULL) {
976 /* This will also remove duplicates if any */
977 if (!strcasecmp(content_type, cur->content_type)) {
978 MOD_syslog(LOG_DEBUG,
979 "Unregistered fixed output function for %s\n",
988 FixedOutputTable = p;
1000 void CtdlDestroyFixedOutputHooks(void)
1002 FixedOutputHook *cur, *p;
1004 cur = FixedOutputTable;
1007 MOD_syslog(LOG_DEBUG, "Destroyed fixed output function for %s\n", cur->content_type);
1013 FixedOutputTable = NULL;
1016 /* returns nonzero if we found a hook and used it */
1017 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
1019 FixedOutputHook *fcn;
1021 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
1022 if (!strcasecmp(content_type, fcn->content_type)) {
1023 (*fcn->h_function_pointer) (content, content_length);
1034 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1037 XmsgFunctionHook *newfcn;
1039 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
1040 newfcn->next = XmsgHookTable;
1041 newfcn->order = order;
1042 newfcn->h_function_pointer = fcn_ptr;
1043 XmsgHookTable = newfcn;
1044 MOD_syslog(LOG_DEBUG, "Registered a new x-msg function (priority %d)\n", order);
1048 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1050 XmsgFunctionHook *cur, *p, *last;
1053 cur = XmsgHookTable;
1054 while (cur != NULL) {
1055 /* This will also remove duplicates if any */
1056 if (fcn_ptr == cur->h_function_pointer &&
1057 order == cur->order) {
1058 MOD_syslog(LOG_DEBUG, "Unregistered x-msg function "
1059 "(priority %d)\n", order);
1077 void CtdlDestroyXmsgHooks(void)
1079 XmsgFunctionHook *cur, *p;
1081 cur = XmsgHookTable;
1084 MOD_syslog(LOG_DEBUG, "Destroyed x-msg function "
1085 "(priority %d)\n", cur->order);
1091 XmsgHookTable = NULL;
1095 void CtdlRegisterServiceHook(int tcp_port,
1097 void (*h_greeting_function) (void),
1098 void (*h_command_function) (void),
1099 void (*h_async_function) (void),
1100 const char *ServiceName)
1102 ServiceFunctionHook *newfcn;
1107 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1108 message = (char*) malloc (SIZ + SIZ);
1110 newfcn->next = ServiceHookTable;
1111 newfcn->tcp_port = tcp_port;
1112 newfcn->sockpath = sockpath;
1113 newfcn->h_greeting_function = h_greeting_function;
1114 newfcn->h_command_function = h_command_function;
1115 newfcn->h_async_function = h_async_function;
1116 newfcn->ServiceName = ServiceName;
1118 if (sockpath != NULL) {
1119 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"), error);
1120 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1122 else if (tcp_port <= 0) { /* port -1 to disable */
1123 MOD_syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1129 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"),
1131 CtdlGetConfigInt("c_maxsessions"),
1133 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
1134 CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
1137 if (newfcn->msock > 0) {
1138 ServiceHookTable = newfcn;
1139 strcat(message, "registered.");
1140 MOD_syslog(LOG_INFO, "%s\n", message);
1143 AddPortError(message, error);
1144 strcat(message, "FAILED.");
1145 MOD_syslog(LOG_CRIT, "%s\n", message);
1152 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1153 void (*h_greeting_function) (void),
1154 void (*h_command_function) (void),
1155 void (*h_async_function) (void)
1158 ServiceFunctionHook *cur, *p, *last;
1161 cur = ServiceHookTable;
1162 while (cur != NULL) {
1163 /* This will also remove duplicates if any */
1164 if (h_greeting_function == cur->h_greeting_function &&
1165 h_command_function == cur->h_command_function &&
1166 h_async_function == cur->h_async_function &&
1167 tcp_port == cur->tcp_port &&
1168 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
1173 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1176 } else if (tcp_port) {
1177 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1179 MOD_syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1186 ServiceHookTable = p;
1197 void CtdlShutdownServiceHooks(void)
1199 /* sort of a duplicate of close_masters() but called earlier */
1200 ServiceFunctionHook *cur;
1202 cur = ServiceHookTable;
1205 if (cur->msock != -1)
1209 if (cur->sockpath != NULL){
1210 MOD_syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1213 unlink(cur->sockpath);
1215 MOD_syslog(LOG_INFO, "[%s] closing service\n",
1223 void CtdlDestroyServiceHook(void)
1226 ServiceFunctionHook *cur, *p;
1228 cur = ServiceHookTable;
1231 if (cur->msock != -1)
1238 Text = " Not closing again";
1241 if (cur->sockpath) {
1242 MOD_syslog(LOG_INFO, "%s UNIX domain socket %s\n",
1245 unlink(cur->sockpath);
1246 } else if (cur->tcp_port) {
1247 MOD_syslog(LOG_INFO, "%s TCP port %d\n", Text, cur->tcp_port);
1249 MOD_syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1255 ServiceHookTable = NULL;
1258 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1260 SearchFunctionHook *newfcn;
1262 if (!name || !fcn_ptr) {
1266 newfcn = (SearchFunctionHook *)
1267 malloc(sizeof(SearchFunctionHook));
1268 newfcn->next = SearchFunctionHookTable;
1269 newfcn->name = name;
1270 newfcn->fcn_ptr = fcn_ptr;
1271 SearchFunctionHookTable = newfcn;
1273 MOD_syslog(LOG_DEBUG, "Registered a new search function (%s)\n", name);
1276 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1278 SearchFunctionHook *cur, *p, *last;
1281 cur = SearchFunctionHookTable;
1282 while (cur != NULL) {
1284 (cur->fcn_ptr == fcn_ptr) &&
1285 name && !strcmp(name, cur->name))
1287 MOD_syslog(LOG_DEBUG, "Unregistered search function(%s)\n", name);
1293 SearchFunctionHookTable = p;
1303 void CtdlDestroySearchHooks(void)
1305 SearchFunctionHook *cur, *p;
1307 cur = SearchFunctionHookTable;
1308 SearchFunctionHookTable = NULL;
1309 while (cur != NULL) {
1316 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1318 SearchFunctionHook *fcn = NULL;
1320 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1321 if (!func_name || !strcmp(func_name, fcn->name)) {
1322 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1329 int CheckTDAPVeto (int DBType, StrBuf *ErrMsg)
1332 TDAPVetoHookFunctionHook *fcn = NULL;
1334 for (fcn = TDAPVetoHookTable; (fcn != NULL) && (Result == 0); fcn = fcn->next) {
1335 if (fcn->eventtype == DBType) {
1336 Result = (*fcn->h_function_pointer) (ErrMsg);
1342 void PerformSessionHooks(int EventType)
1344 SessionFunctionHook *fcn = NULL;
1346 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1347 if (fcn->eventtype == EventType) {
1348 if (EventType == EVT_TIMER) {
1349 pthread_setspecific(MyConKey, NULL); /* for every hook */
1351 (*fcn->h_function_pointer) ();
1356 void PerformUserHooks(ctdluser *usbuf, int EventType)
1358 UserFunctionHook *fcn = NULL;
1360 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1361 if (fcn->eventtype == EventType) {
1362 (*fcn->h_function_pointer) (usbuf);
1367 int PerformMessageHooks(struct CtdlMessage *msg, recptypes *recps, int EventType)
1369 MessageFunctionHook *fcn = NULL;
1370 int total_retval = 0;
1372 /* Other code may elect to protect this message from server-side
1373 * handlers; if this is the case, don't do anything.
1374 MOD_syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1376 if (msg->cm_flags & CM_SKIP_HOOKS) {
1377 MODM_syslog(LOG_DEBUG, "Skipping hooks\n");
1381 /* Otherwise, run all the hooks appropriate to this event type.
1383 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1384 if (fcn->eventtype == EventType) {
1385 total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
1389 /* Return the sum of the return codes from the hook functions. If
1390 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1391 * the save operation to abort.
1393 return total_retval;
1397 int PerformRoomHooks(struct ctdlroom *target_room)
1399 RoomFunctionHook *fcn;
1400 int total_retval = 0;
1402 MOD_syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1404 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1405 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1408 /* Return the sum of the return codes from the hook functions.
1410 return total_retval;
1414 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1416 NetprocFunctionHook *fcn;
1417 int total_retval = 0;
1419 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1420 total_retval = total_retval +
1421 (*fcn->h_function_pointer) (msg, target_room);
1424 /* Return the sum of the return codes from the hook functions.
1425 * A nonzero return code will cause the message to *not* be imported.
1427 return total_retval;
1431 void PerformDeleteHooks(char *room, long msgnum)
1433 DeleteFunctionHook *fcn;
1435 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1436 (*fcn->h_function_pointer) (room, msgnum);
1444 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1446 XmsgFunctionHook *fcn;
1450 for (p=0; p<MAX_XMSG_PRI; ++p) {
1451 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1452 if (fcn->order == p) {
1454 (*fcn->h_function_pointer)
1455 (sender, sender_email, recp, msg);
1458 /* Break out of the loop if a higher-priority function
1459 * successfully delivered the message. This prevents duplicate
1460 * deliveries to local users simultaneously signed onto
1463 if (total_sent) break;
1470 * Dirty hack until we impliment a hook mechanism for this
1472 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1475 CtdlStartTLS (ok_response, nosup_response, error_response);
1479 void DebugModulesEnable(const int n)
1483 CTDL_MODULE_INIT(modules)
1486 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);