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 long FourHash(const char *key, long length)
306 const unsigned char *ptr = (const unsigned char*)key;
308 for (i = 0; i < 4; i++, ptr ++)
318 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
321 if (LogDebugEntryTable == NULL)
322 LogDebugEntryTable = NewHash(1, NULL);
323 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
328 Put(LogDebugEntryTable, Name, Len, E, NULL);
331 void CtdlSetDebugLogFacilities(const char **Str, long n)
333 StrBuf *Token = NULL;
340 for (i=0; i < n; i++){
341 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
342 if (strcmp(Str[i], "all") == 0) {
346 Buf = NewStrBufPlain(Str[i], -1);
349 Token = NewStrBufPlain(NULL, StrLength(Buf));
350 while ((ch != StrBufNOTNULL) &&
351 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
352 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
355 LogDebugEntry *E = (LogDebugEntry*)vptr;
368 Pos = GetNewHashPos(LogDebugEntryTable, 0);
369 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
370 LogDebugEntry *E = (LogDebugEntry*)vptr;
377 void CtdlDestroyDebugTable(void)
380 DeleteHash(&LogDebugEntryTable);
383 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
385 ProtoFunctionHook *p;
387 if (ProtoHookList == NULL)
388 ProtoHookList = NewHash (1, FourHash);
391 p = (ProtoFunctionHook *)
392 malloc(sizeof(ProtoFunctionHook));
395 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
398 p->handler = handler;
402 Put(ProtoHookList, cmd, 4, p, NULL);
403 MOD_syslog(LOG_DEBUG, "Registered server command %s (%s)\n", cmd, desc);
406 void CtdlDestroyProtoHooks(void)
409 DeleteHash(&ProtoHookList);
413 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
416 CleanupFunctionHook *newfcn;
418 newfcn = (CleanupFunctionHook *)
419 malloc(sizeof(CleanupFunctionHook));
420 newfcn->next = CleanupHookTable;
421 newfcn->h_function_pointer = fcn_ptr;
422 CleanupHookTable = newfcn;
424 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
428 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
430 CleanupFunctionHook *cur, *p, *last;
432 cur = CleanupHookTable;
435 if (fcn_ptr == cur->h_function_pointer)
437 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
446 CleanupHookTable = p;
457 void CtdlDestroyCleanupHooks(void)
459 CleanupFunctionHook *cur, *p;
461 cur = CleanupHookTable;
464 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
469 CleanupHookTable = NULL;
472 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
475 CleanupFunctionHook *newfcn;
477 newfcn = (CleanupFunctionHook *)
478 malloc(sizeof(CleanupFunctionHook));
479 newfcn->next = EVCleanupHookTable;
480 newfcn->h_function_pointer = fcn_ptr;
481 EVCleanupHookTable = newfcn;
483 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
487 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
489 CleanupFunctionHook *cur, *p, *last;
491 cur = EVCleanupHookTable;
494 if (fcn_ptr == cur->h_function_pointer)
496 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
505 EVCleanupHookTable = p;
516 void CtdlDestroyEVCleanupHooks(void)
518 CleanupFunctionHook *cur, *p;
520 cur = EVCleanupHookTable;
523 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
525 cur->h_function_pointer();
529 EVCleanupHookTable = NULL;
532 void CtdlRegisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType, int Priority)
534 TDAPVetoHookFunctionHook *newfcn;
536 newfcn = (TDAPVetoHookFunctionHook *)
537 malloc(sizeof(TDAPVetoHookFunctionHook));
538 newfcn->Priority = Priority;
539 newfcn->h_function_pointer = fcn_ptr;
540 newfcn->eventtype = EventType;
542 TDAPVetoHookFunctionHook **pfcn;
543 pfcn = &TDAPVetoHookTable;
544 while ((*pfcn != NULL) &&
545 ((*pfcn)->Priority < newfcn->Priority) &&
546 ((*pfcn)->next != NULL))
547 pfcn = &(*pfcn)->next;
549 newfcn->next = *pfcn;
552 MOD_syslog(LOG_DEBUG, "Registered a new TDAP Veto function (type %d Priority %d)\n",
553 EventType, Priority);
557 void CtdlUnregisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType)
559 TDAPVetoHookFunctionHook *cur, *p, *last;
561 cur = TDAPVetoHookTable;
562 while (cur != NULL) {
563 if ((fcn_ptr == cur->h_function_pointer) &&
564 (EventType == cur->eventtype))
566 MOD_syslog(LOG_DEBUG, "Unregistered TDAP Veto function (type %d)\n",
576 TDAPVetoHookTable = p;
586 void CtdlDestroyTDAPVetoHooks(void)
588 TDAPVetoHookFunctionHook *cur, *p;
590 cur = TDAPVetoHookTable;
593 MODM_syslog(LOG_DEBUG, "Destroyed TDAP Veto function\n");
598 TDAPVetoHookTable = NULL;
602 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
604 SessionFunctionHook *newfcn;
606 newfcn = (SessionFunctionHook *)
607 malloc(sizeof(SessionFunctionHook));
608 newfcn->Priority = Priority;
609 newfcn->h_function_pointer = fcn_ptr;
610 newfcn->eventtype = EventType;
612 SessionFunctionHook **pfcn;
613 pfcn = &SessionHookTable;
614 while ((*pfcn != NULL) &&
615 ((*pfcn)->Priority < newfcn->Priority) &&
616 ((*pfcn)->next != NULL))
617 pfcn = &(*pfcn)->next;
619 newfcn->next = *pfcn;
622 MOD_syslog(LOG_DEBUG, "Registered a new session function (type %d Priority %d)\n",
623 EventType, Priority);
627 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
629 SessionFunctionHook *cur, *p, *last;
631 cur = SessionHookTable;
632 while (cur != NULL) {
633 if ((fcn_ptr == cur->h_function_pointer) &&
634 (EventType == cur->eventtype))
636 MOD_syslog(LOG_DEBUG, "Unregistered session function (type %d)\n",
646 SessionHookTable = p;
656 void CtdlDestroySessionHooks(void)
658 SessionFunctionHook *cur, *p;
660 cur = SessionHookTable;
663 MODM_syslog(LOG_DEBUG, "Destroyed session function\n");
668 SessionHookTable = NULL;
672 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
675 UserFunctionHook *newfcn;
677 newfcn = (UserFunctionHook *)
678 malloc(sizeof(UserFunctionHook));
679 newfcn->next = UserHookTable;
680 newfcn->h_function_pointer = fcn_ptr;
681 newfcn->eventtype = EventType;
682 UserHookTable = newfcn;
684 MOD_syslog(LOG_DEBUG, "Registered a new user function (type %d)\n",
689 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
691 UserFunctionHook *cur, *p, *last;
694 while (cur != NULL) {
695 if ((fcn_ptr == cur->h_function_pointer) &&
696 (EventType == cur->eventtype))
698 MOD_syslog(LOG_DEBUG, "Unregistered user function (type %d)\n",
718 void CtdlDestroyUserHooks(void)
720 UserFunctionHook *cur, *p;
725 MODM_syslog(LOG_DEBUG, "Destroyed user function \n");
730 UserHookTable = NULL;
734 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
738 MessageFunctionHook *newfcn;
740 newfcn = (MessageFunctionHook *)
741 malloc(sizeof(MessageFunctionHook));
742 newfcn->next = MessageHookTable;
743 newfcn->h_function_pointer = handler;
744 newfcn->eventtype = EventType;
745 MessageHookTable = newfcn;
747 MOD_syslog(LOG_DEBUG, "Registered a new message function (type %d)\n",
752 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
755 MessageFunctionHook *cur, *p, *last;
757 cur = MessageHookTable;
758 while (cur != NULL) {
759 if ((handler == cur->h_function_pointer) &&
760 (EventType == cur->eventtype))
762 MOD_syslog(LOG_DEBUG, "Unregistered message function (type %d)\n",
771 MessageHookTable = p;
781 void CtdlDestroyMessageHook(void)
783 MessageFunctionHook *cur, *p;
785 cur = MessageHookTable;
788 MOD_syslog(LOG_DEBUG, "Destroyed message function (type %d)\n", cur->eventtype);
793 MessageHookTable = NULL;
797 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
799 RoomFunctionHook *newfcn;
801 newfcn = (RoomFunctionHook *)
802 malloc(sizeof(RoomFunctionHook));
803 newfcn->next = RoomHookTable;
804 newfcn->fcn_ptr = fcn_ptr;
805 RoomHookTable = newfcn;
807 MODM_syslog(LOG_DEBUG, "Registered a new room function\n");
811 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
813 RoomFunctionHook *cur, *p, *last;
818 if (fcn_ptr == cur->fcn_ptr) {
819 MODM_syslog(LOG_DEBUG, "Unregistered room function\n");
839 void CtdlDestroyRoomHooks(void)
841 RoomFunctionHook *cur, *p;
846 MODM_syslog(LOG_DEBUG, "Destroyed room function\n");
851 RoomHookTable = NULL;
854 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
856 NetprocFunctionHook *newfcn;
858 newfcn = (NetprocFunctionHook *)
859 malloc(sizeof(NetprocFunctionHook));
860 newfcn->next = NetprocHookTable;
861 newfcn->h_function_pointer = handler;
862 NetprocHookTable = newfcn;
864 MODM_syslog(LOG_DEBUG, "Registered a new netproc function\n");
868 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
870 NetprocFunctionHook *cur, *p, *last;
872 cur = NetprocHookTable;
875 while (cur != NULL) {
876 if (handler == cur->h_function_pointer)
878 MODM_syslog(LOG_DEBUG, "Unregistered netproc function\n");
885 NetprocHookTable = p;
896 void CtdlDestroyNetprocHooks(void)
898 NetprocFunctionHook *cur, *p;
900 cur = NetprocHookTable;
903 MODM_syslog(LOG_DEBUG, "Destroyed netproc function\n");
908 NetprocHookTable = NULL;
912 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
914 DeleteFunctionHook *newfcn;
916 newfcn = (DeleteFunctionHook *)
917 malloc(sizeof(DeleteFunctionHook));
918 newfcn->next = DeleteHookTable;
919 newfcn->h_function_pointer = handler;
920 DeleteHookTable = newfcn;
922 MODM_syslog(LOG_DEBUG, "Registered a new delete function\n");
926 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
928 DeleteFunctionHook *cur, *p, *last;
931 cur = DeleteHookTable;
932 while (cur != NULL) {
933 if (handler == cur->h_function_pointer )
935 MODM_syslog(LOG_DEBUG, "Unregistered delete function\n");
952 void CtdlDestroyDeleteHooks(void)
954 DeleteFunctionHook *cur, *p;
956 cur = DeleteHookTable;
959 MODM_syslog(LOG_DEBUG, "Destroyed delete function\n");
964 DeleteHookTable = NULL;
970 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
972 FixedOutputHook *newfcn;
974 newfcn = (FixedOutputHook *)
975 malloc(sizeof(FixedOutputHook));
976 newfcn->next = FixedOutputTable;
977 newfcn->h_function_pointer = handler;
978 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
979 FixedOutputTable = newfcn;
981 MOD_syslog(LOG_DEBUG, "Registered a new fixed output function for %s\n", newfcn->content_type);
985 void CtdlUnregisterFixedOutputHook(char *content_type)
987 FixedOutputHook *cur, *p, *last;
990 cur = FixedOutputTable;
991 while (cur != NULL) {
992 /* This will also remove duplicates if any */
993 if (!strcasecmp(content_type, cur->content_type)) {
994 MOD_syslog(LOG_DEBUG,
995 "Unregistered fixed output function for %s\n",
1004 FixedOutputTable = p;
1016 void CtdlDestroyFixedOutputHooks(void)
1018 FixedOutputHook *cur, *p;
1020 cur = FixedOutputTable;
1023 MOD_syslog(LOG_DEBUG, "Destroyed fixed output function for %s\n", cur->content_type);
1029 FixedOutputTable = NULL;
1032 /* returns nonzero if we found a hook and used it */
1033 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
1035 FixedOutputHook *fcn;
1037 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
1038 if (!strcasecmp(content_type, fcn->content_type)) {
1039 (*fcn->h_function_pointer) (content, content_length);
1050 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1053 XmsgFunctionHook *newfcn;
1055 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
1056 newfcn->next = XmsgHookTable;
1057 newfcn->order = order;
1058 newfcn->h_function_pointer = fcn_ptr;
1059 XmsgHookTable = newfcn;
1060 MOD_syslog(LOG_DEBUG, "Registered a new x-msg function (priority %d)\n", order);
1064 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1066 XmsgFunctionHook *cur, *p, *last;
1069 cur = XmsgHookTable;
1070 while (cur != NULL) {
1071 /* This will also remove duplicates if any */
1072 if (fcn_ptr == cur->h_function_pointer &&
1073 order == cur->order) {
1074 MOD_syslog(LOG_DEBUG, "Unregistered x-msg function "
1075 "(priority %d)\n", order);
1093 void CtdlDestroyXmsgHooks(void)
1095 XmsgFunctionHook *cur, *p;
1097 cur = XmsgHookTable;
1100 MOD_syslog(LOG_DEBUG, "Destroyed x-msg function "
1101 "(priority %d)\n", cur->order);
1107 XmsgHookTable = NULL;
1111 void CtdlRegisterServiceHook(int tcp_port,
1113 void (*h_greeting_function) (void),
1114 void (*h_command_function) (void),
1115 void (*h_async_function) (void),
1116 const char *ServiceName)
1118 ServiceFunctionHook *newfcn;
1123 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1124 message = (char*) malloc (SIZ + SIZ);
1126 newfcn->next = ServiceHookTable;
1127 newfcn->tcp_port = tcp_port;
1128 newfcn->sockpath = sockpath;
1129 newfcn->h_greeting_function = h_greeting_function;
1130 newfcn->h_command_function = h_command_function;
1131 newfcn->h_async_function = h_async_function;
1132 newfcn->ServiceName = ServiceName;
1134 if (sockpath != NULL) {
1135 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"), error);
1136 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1138 else if (tcp_port <= 0) { /* port -1 to disable */
1139 MOD_syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1145 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"),
1147 CtdlGetConfigInt("c_maxsessions"),
1149 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
1150 CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
1153 if (newfcn->msock > 0) {
1154 ServiceHookTable = newfcn;
1155 strcat(message, "registered.");
1156 MOD_syslog(LOG_INFO, "%s\n", message);
1159 AddPortError(message, error);
1160 strcat(message, "FAILED.");
1161 MOD_syslog(LOG_CRIT, "%s\n", message);
1168 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1169 void (*h_greeting_function) (void),
1170 void (*h_command_function) (void),
1171 void (*h_async_function) (void)
1174 ServiceFunctionHook *cur, *p, *last;
1177 cur = ServiceHookTable;
1178 while (cur != NULL) {
1179 /* This will also remove duplicates if any */
1180 if (h_greeting_function == cur->h_greeting_function &&
1181 h_command_function == cur->h_command_function &&
1182 h_async_function == cur->h_async_function &&
1183 tcp_port == cur->tcp_port &&
1184 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
1189 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1192 } else if (tcp_port) {
1193 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1195 MOD_syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1202 ServiceHookTable = p;
1213 void CtdlShutdownServiceHooks(void)
1215 /* sort of a duplicate of close_masters() but called earlier */
1216 ServiceFunctionHook *cur;
1218 cur = ServiceHookTable;
1221 if (cur->msock != -1)
1225 if (cur->sockpath != NULL){
1226 MOD_syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1229 unlink(cur->sockpath);
1231 MOD_syslog(LOG_INFO, "[%s] closing service\n",
1239 void CtdlDestroyServiceHook(void)
1242 ServiceFunctionHook *cur, *p;
1244 cur = ServiceHookTable;
1247 if (cur->msock != -1)
1254 Text = " Not closing again";
1257 if (cur->sockpath) {
1258 MOD_syslog(LOG_INFO, "%s UNIX domain socket %s\n",
1261 unlink(cur->sockpath);
1262 } else if (cur->tcp_port) {
1263 MOD_syslog(LOG_INFO, "%s TCP port %d\n", Text, cur->tcp_port);
1265 MOD_syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1271 ServiceHookTable = NULL;
1274 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1276 SearchFunctionHook *newfcn;
1278 if (!name || !fcn_ptr) {
1282 newfcn = (SearchFunctionHook *)
1283 malloc(sizeof(SearchFunctionHook));
1284 newfcn->next = SearchFunctionHookTable;
1285 newfcn->name = name;
1286 newfcn->fcn_ptr = fcn_ptr;
1287 SearchFunctionHookTable = newfcn;
1289 MOD_syslog(LOG_DEBUG, "Registered a new search function (%s)\n", name);
1292 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1294 SearchFunctionHook *cur, *p, *last;
1297 cur = SearchFunctionHookTable;
1298 while (cur != NULL) {
1300 (cur->fcn_ptr == fcn_ptr) &&
1301 name && !strcmp(name, cur->name))
1303 MOD_syslog(LOG_DEBUG, "Unregistered search function(%s)\n", name);
1309 SearchFunctionHookTable = p;
1319 void CtdlDestroySearchHooks(void)
1321 SearchFunctionHook *cur, *p;
1323 cur = SearchFunctionHookTable;
1324 SearchFunctionHookTable = NULL;
1325 while (cur != NULL) {
1332 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1334 SearchFunctionHook *fcn = NULL;
1336 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1337 if (!func_name || !strcmp(func_name, fcn->name)) {
1338 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1345 int CheckTDAPVeto (int DBType, StrBuf *ErrMsg)
1348 TDAPVetoHookFunctionHook *fcn = NULL;
1350 for (fcn = TDAPVetoHookTable; (fcn != NULL) && (Result == 0); fcn = fcn->next) {
1351 if (fcn->eventtype == DBType) {
1352 Result = (*fcn->h_function_pointer) (ErrMsg);
1358 void PerformSessionHooks(int EventType)
1360 SessionFunctionHook *fcn = NULL;
1362 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1363 if (fcn->eventtype == EventType) {
1364 if (EventType == EVT_TIMER) {
1365 pthread_setspecific(MyConKey, NULL); /* for every hook */
1367 (*fcn->h_function_pointer) ();
1372 void PerformUserHooks(ctdluser *usbuf, int EventType)
1374 UserFunctionHook *fcn = NULL;
1376 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1377 if (fcn->eventtype == EventType) {
1378 (*fcn->h_function_pointer) (usbuf);
1383 int PerformMessageHooks(struct CtdlMessage *msg, recptypes *recps, int EventType)
1385 MessageFunctionHook *fcn = NULL;
1386 int total_retval = 0;
1388 /* Other code may elect to protect this message from server-side
1389 * handlers; if this is the case, don't do anything.
1390 MOD_syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1392 if (msg->cm_flags & CM_SKIP_HOOKS) {
1393 MODM_syslog(LOG_DEBUG, "Skipping hooks\n");
1397 /* Otherwise, run all the hooks appropriate to this event type.
1399 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1400 if (fcn->eventtype == EventType) {
1401 total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
1405 /* Return the sum of the return codes from the hook functions. If
1406 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1407 * the save operation to abort.
1409 return total_retval;
1413 int PerformRoomHooks(struct ctdlroom *target_room)
1415 RoomFunctionHook *fcn;
1416 int total_retval = 0;
1418 MOD_syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1420 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1421 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1424 /* Return the sum of the return codes from the hook functions.
1426 return total_retval;
1430 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1432 NetprocFunctionHook *fcn;
1433 int total_retval = 0;
1435 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1436 total_retval = total_retval +
1437 (*fcn->h_function_pointer) (msg, target_room);
1440 /* Return the sum of the return codes from the hook functions.
1441 * A nonzero return code will cause the message to *not* be imported.
1443 return total_retval;
1447 void PerformDeleteHooks(char *room, long msgnum)
1449 DeleteFunctionHook *fcn;
1451 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1452 (*fcn->h_function_pointer) (room, msgnum);
1460 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1462 XmsgFunctionHook *fcn;
1466 for (p=0; p<MAX_XMSG_PRI; ++p) {
1467 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1468 if (fcn->order == p) {
1470 (*fcn->h_function_pointer)
1471 (sender, sender_email, recp, msg);
1474 /* Break out of the loop if a higher-priority function
1475 * successfully delivered the message. This prevents duplicate
1476 * deliveries to local users simultaneously signed onto
1479 if (total_sent) break;
1486 * Dirty hack until we impliment a hook mechanism for this
1488 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1491 CtdlStartTLS (ok_response, nosup_response, error_response);
1495 void DebugModulesEnable(const int n)
1499 CTDL_MODULE_INIT(modules)
1502 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);