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 HashList *LogDebugEntryTable = NULL;
47 typedef struct LogFunctionHook LogFunctionHook;
48 struct LogFunctionHook {
49 LogFunctionHook *next;
51 void (*h_function_pointer) (char *);
54 LogFunctionHook *LogHookTable = NULL;
56 typedef struct FixedOutputHook FixedOutputHook;
57 struct FixedOutputHook {
58 FixedOutputHook *next;
59 char content_type[64];
60 void (*h_function_pointer) (char *, int);
62 FixedOutputHook *FixedOutputTable = NULL;
67 * TDAPVetoHookFunctionHook extensions are used for any type of hook for which
68 * may prevent the autopurger to run for this specific data class.
69 * the function should at least LOG_INFO that it does so.
71 typedef struct TDAPVetoHookFunctionHook TDAPVetoHookFunctionHook;
72 struct TDAPVetoHookFunctionHook {
73 TDAPVetoHookFunctionHook *next;
75 int (*h_function_pointer) (StrBuf *);
78 TDAPVetoHookFunctionHook *TDAPVetoHookTable = NULL;
83 * SessionFunctionHook extensions are used for any type of hook for which
84 * the context in which it's being called (which is determined by the event
85 * type) will make it obvious for the hook function to know where to look for
88 typedef struct SessionFunctionHook SessionFunctionHook;
89 struct SessionFunctionHook {
90 SessionFunctionHook *next;
92 void (*h_function_pointer) (void);
95 SessionFunctionHook *SessionHookTable = NULL;
98 * UserFunctionHook extensions are used for any type of hook which implements
99 * an operation on a user or username (potentially) other than the one
100 * operating the current session.
102 typedef struct UserFunctionHook UserFunctionHook;
103 struct UserFunctionHook {
104 UserFunctionHook *next;
105 void (*h_function_pointer) (struct ctdluser *usbuf);
108 UserFunctionHook *UserHookTable = NULL;
111 * MessageFunctionHook extensions are used for hooks which implement handlers
112 * for various types of message operations (save, read, etc.)
114 typedef struct MessageFunctionHook MessageFunctionHook;
115 struct MessageFunctionHook {
116 MessageFunctionHook *next;
117 int (*h_function_pointer) (struct CtdlMessage *msg);
120 MessageFunctionHook *MessageHookTable = NULL;
124 * NetprocFunctionHook extensions are used for hooks which implement handlers
125 * for incoming network messages.
127 typedef struct NetprocFunctionHook NetprocFunctionHook;
128 struct NetprocFunctionHook {
129 NetprocFunctionHook *next;
130 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
132 NetprocFunctionHook *NetprocHookTable = NULL;
136 * DeleteFunctionHook extensions are used for hooks which get called when a
137 * message is about to be deleted.
139 typedef struct DeleteFunctionHook DeleteFunctionHook;
140 struct DeleteFunctionHook {
141 DeleteFunctionHook *next;
142 void (*h_function_pointer) (char *target_room, long msgnum);
144 DeleteFunctionHook *DeleteHookTable = NULL;
148 * ExpressMessageFunctionHook extensions are used for hooks which implement
149 * the sending of an instant message through various channels. Any function
150 * registered should return the number of recipients to whom the message was
151 * successfully transmitted.
153 typedef struct XmsgFunctionHook XmsgFunctionHook;
154 struct XmsgFunctionHook {
155 XmsgFunctionHook *next;
156 int (*h_function_pointer) (char *, char *, char *, char *);
159 XmsgFunctionHook *XmsgHookTable = NULL;
165 * RoomFunctionHook extensions are used for hooks which impliment room
166 * processing functions when new messages are added EG. SIEVE.
168 typedef struct RoomFunctionHook RoomFunctionHook;
169 struct RoomFunctionHook {
170 RoomFunctionHook *next;
171 int (*fcn_ptr) (struct ctdlroom *);
173 RoomFunctionHook *RoomHookTable = NULL;
177 typedef struct SearchFunctionHook SearchFunctionHook;
178 struct SearchFunctionHook {
179 SearchFunctionHook *next;
180 void (*fcn_ptr) (int *, long **, const char *);
183 SearchFunctionHook *SearchFunctionHookTable = NULL;
185 CleanupFunctionHook *CleanupHookTable = NULL;
186 CleanupFunctionHook *EVCleanupHookTable = NULL;
188 ServiceFunctionHook *ServiceHookTable = NULL;
190 typedef struct ProtoFunctionHook ProtoFunctionHook;
191 struct ProtoFunctionHook {
192 void (*handler) (char *cmdbuf);
197 HashList *ProtoHookList = NULL;
200 #define ERR_PORT (1 << 1)
203 static StrBuf *portlist = NULL;
205 static StrBuf *errormessages = NULL;
208 long DetailErrorFlags;
209 ConstStr Empty = {HKEY("")};
210 char *ErrSubject = "Startup Problems";
211 ConstStr ErrGeneral[] = {
212 {HKEY("Citadel had trouble on starting up. ")},
213 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
214 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
215 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
216 "The errors returned by the system were:\n")},
217 {HKEY("You can recheck the above if you follow this faq item:\n"
218 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
221 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
222 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
223 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
224 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
225 " and stop the program that binds these ports.\n You should eventually remove "
226 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
227 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
230 void LogPrintMessages(long err)
233 StrBuf *List, *DetailList;
234 ConstStr *Short, *Where, *Hint;
237 Message = NewStrBufPlain(NULL,
238 StrLength(portlist) + StrLength(errormessages));
240 DetailErrorFlags = DetailErrorFlags & ~err;
245 Short = &ErrPortShort;
246 Where = &ErrPortWhere;
249 DetailList = errormessages;
259 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
260 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
261 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
262 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
263 StrBufAppendBuf(Message, List, 0);
264 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
265 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
266 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
267 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
268 StrBufAppendBuf(Message, DetailList, 0);
269 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
270 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
272 MOD_syslog(LOG_EMERG, "%s", ChrPtr(Message));
273 MOD_syslog(LOG_EMERG, "%s", ErrSubject);
274 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
276 FreeStrBuf(&Message);
278 FreeStrBuf(&DetailList);
282 void AddPortError(char *Port, char *ErrorMessage)
286 DetailErrorFlags |= ERR_PORT;
288 len = StrLength(errormessages);
289 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
290 else errormessages = NewStrBuf();
291 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
294 len = StrLength(portlist);
295 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
296 else portlist = NewStrBuf();
297 StrBufAppendBufPlain(portlist, Port, -1, 0);
301 int DLoader_Exec_Cmd(char *cmdbuf)
304 ProtoFunctionHook *p;
306 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
307 p = (ProtoFunctionHook*) vP;
308 p->handler(&cmdbuf[5]);
314 long FourHash(const char *key, long length)
318 const unsigned char *ptr = (const unsigned char*)key;
320 for (i = 0; i < 4; i++, ptr ++)
330 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
333 if (LogDebugEntryTable == NULL)
334 LogDebugEntryTable = NewHash(1, NULL);
335 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
340 Put(LogDebugEntryTable, Name, Len, E, NULL);
343 void CtdlSetDebugLogFacilities(const char **Str, long n)
345 StrBuf *Token = NULL;
352 for (i=0; i < n; i++){
353 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
354 if (strcmp(Str[i], "all") == 0) {
358 Buf = NewStrBufPlain(Str[i], -1);
361 Token = NewStrBufPlain(NULL, StrLength(Buf));
362 while ((ch != StrBufNOTNULL) &&
363 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
364 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
367 LogDebugEntry *E = (LogDebugEntry*)vptr;
380 Pos = GetNewHashPos(LogDebugEntryTable, 0);
381 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
382 LogDebugEntry *E = (LogDebugEntry*)vptr;
389 void CtdlDestroyDebugTable(void)
392 DeleteHash(&LogDebugEntryTable);
395 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
397 ProtoFunctionHook *p;
399 if (ProtoHookList == NULL)
400 ProtoHookList = NewHash (1, FourHash);
403 p = (ProtoFunctionHook *)
404 malloc(sizeof(ProtoFunctionHook));
407 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
410 p->handler = handler;
414 Put(ProtoHookList, cmd, 4, p, NULL);
415 MOD_syslog(LOG_DEBUG, "Registered server command %s (%s)\n", cmd, desc);
418 void CtdlDestroyProtoHooks(void)
421 DeleteHash(&ProtoHookList);
425 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
428 CleanupFunctionHook *newfcn;
430 newfcn = (CleanupFunctionHook *)
431 malloc(sizeof(CleanupFunctionHook));
432 newfcn->next = CleanupHookTable;
433 newfcn->h_function_pointer = fcn_ptr;
434 CleanupHookTable = newfcn;
436 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
440 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
442 CleanupFunctionHook *cur, *p, *last;
444 cur = CleanupHookTable;
447 if (fcn_ptr == cur->h_function_pointer)
449 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
458 CleanupHookTable = p;
469 void CtdlDestroyCleanupHooks(void)
471 CleanupFunctionHook *cur, *p;
473 cur = CleanupHookTable;
476 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
481 CleanupHookTable = NULL;
484 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
487 CleanupFunctionHook *newfcn;
489 newfcn = (CleanupFunctionHook *)
490 malloc(sizeof(CleanupFunctionHook));
491 newfcn->next = EVCleanupHookTable;
492 newfcn->h_function_pointer = fcn_ptr;
493 EVCleanupHookTable = newfcn;
495 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
499 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
501 CleanupFunctionHook *cur, *p, *last;
503 cur = EVCleanupHookTable;
506 if (fcn_ptr == cur->h_function_pointer)
508 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
517 EVCleanupHookTable = p;
528 void CtdlDestroyEVCleanupHooks(void)
530 CleanupFunctionHook *cur, *p;
532 cur = EVCleanupHookTable;
535 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
537 cur->h_function_pointer();
541 EVCleanupHookTable = NULL;
544 void CtdlRegisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType, int Priority)
546 TDAPVetoHookFunctionHook *newfcn;
548 newfcn = (TDAPVetoHookFunctionHook *)
549 malloc(sizeof(TDAPVetoHookFunctionHook));
550 newfcn->Priority = Priority;
551 newfcn->h_function_pointer = fcn_ptr;
552 newfcn->eventtype = EventType;
554 TDAPVetoHookFunctionHook **pfcn;
555 pfcn = &TDAPVetoHookTable;
556 while ((*pfcn != NULL) &&
557 ((*pfcn)->Priority < newfcn->Priority) &&
558 ((*pfcn)->next != NULL))
559 pfcn = &(*pfcn)->next;
561 newfcn->next = *pfcn;
564 MOD_syslog(LOG_DEBUG, "Registered a new TDAP Veto function (type %d Priority %d)\n",
565 EventType, Priority);
569 void CtdlUnregisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType)
571 TDAPVetoHookFunctionHook *cur, *p, *last;
573 cur = TDAPVetoHookTable;
574 while (cur != NULL) {
575 if ((fcn_ptr == cur->h_function_pointer) &&
576 (EventType == cur->eventtype))
578 MOD_syslog(LOG_DEBUG, "Unregistered TDAP Veto function (type %d)\n",
588 TDAPVetoHookTable = p;
598 void CtdlDestroyTDAPVetoHooks(void)
600 TDAPVetoHookFunctionHook *cur, *p;
602 cur = TDAPVetoHookTable;
605 MODM_syslog(LOG_DEBUG, "Destroyed TDAP Veto function\n");
610 TDAPVetoHookTable = NULL;
614 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
616 SessionFunctionHook *newfcn;
618 newfcn = (SessionFunctionHook *)
619 malloc(sizeof(SessionFunctionHook));
620 newfcn->Priority = Priority;
621 newfcn->h_function_pointer = fcn_ptr;
622 newfcn->eventtype = EventType;
624 SessionFunctionHook **pfcn;
625 pfcn = &SessionHookTable;
626 while ((*pfcn != NULL) &&
627 ((*pfcn)->Priority < newfcn->Priority) &&
628 ((*pfcn)->next != NULL))
629 pfcn = &(*pfcn)->next;
631 newfcn->next = *pfcn;
634 MOD_syslog(LOG_DEBUG, "Registered a new session function (type %d Priority %d)\n",
635 EventType, Priority);
639 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
641 SessionFunctionHook *cur, *p, *last;
643 cur = SessionHookTable;
644 while (cur != NULL) {
645 if ((fcn_ptr == cur->h_function_pointer) &&
646 (EventType == cur->eventtype))
648 MOD_syslog(LOG_DEBUG, "Unregistered session function (type %d)\n",
658 SessionHookTable = p;
668 void CtdlDestroySessionHooks(void)
670 SessionFunctionHook *cur, *p;
672 cur = SessionHookTable;
675 MODM_syslog(LOG_DEBUG, "Destroyed session function\n");
680 SessionHookTable = NULL;
684 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
687 UserFunctionHook *newfcn;
689 newfcn = (UserFunctionHook *)
690 malloc(sizeof(UserFunctionHook));
691 newfcn->next = UserHookTable;
692 newfcn->h_function_pointer = fcn_ptr;
693 newfcn->eventtype = EventType;
694 UserHookTable = newfcn;
696 MOD_syslog(LOG_DEBUG, "Registered a new user function (type %d)\n",
701 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
703 UserFunctionHook *cur, *p, *last;
706 while (cur != NULL) {
707 if ((fcn_ptr == cur->h_function_pointer) &&
708 (EventType == cur->eventtype))
710 MOD_syslog(LOG_DEBUG, "Unregistered user function (type %d)\n",
730 void CtdlDestroyUserHooks(void)
732 UserFunctionHook *cur, *p;
737 MODM_syslog(LOG_DEBUG, "Destroyed user function \n");
742 UserHookTable = NULL;
746 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
750 MessageFunctionHook *newfcn;
752 newfcn = (MessageFunctionHook *)
753 malloc(sizeof(MessageFunctionHook));
754 newfcn->next = MessageHookTable;
755 newfcn->h_function_pointer = handler;
756 newfcn->eventtype = EventType;
757 MessageHookTable = newfcn;
759 MOD_syslog(LOG_DEBUG, "Registered a new message function (type %d)\n",
764 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
767 MessageFunctionHook *cur, *p, *last;
769 cur = MessageHookTable;
770 while (cur != NULL) {
771 if ((handler == cur->h_function_pointer) &&
772 (EventType == cur->eventtype))
774 MOD_syslog(LOG_DEBUG, "Unregistered message function (type %d)\n",
783 MessageHookTable = p;
793 void CtdlDestroyMessageHook(void)
795 MessageFunctionHook *cur, *p;
797 cur = MessageHookTable;
800 MOD_syslog(LOG_DEBUG, "Destroyed message function (type %d)\n", cur->eventtype);
805 MessageHookTable = NULL;
809 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
811 RoomFunctionHook *newfcn;
813 newfcn = (RoomFunctionHook *)
814 malloc(sizeof(RoomFunctionHook));
815 newfcn->next = RoomHookTable;
816 newfcn->fcn_ptr = fcn_ptr;
817 RoomHookTable = newfcn;
819 MODM_syslog(LOG_DEBUG, "Registered a new room function\n");
823 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
825 RoomFunctionHook *cur, *p, *last;
830 if (fcn_ptr == cur->fcn_ptr) {
831 MODM_syslog(LOG_DEBUG, "Unregistered room function\n");
851 void CtdlDestroyRoomHooks(void)
853 RoomFunctionHook *cur, *p;
858 MODM_syslog(LOG_DEBUG, "Destroyed room function\n");
863 RoomHookTable = NULL;
866 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
868 NetprocFunctionHook *newfcn;
870 newfcn = (NetprocFunctionHook *)
871 malloc(sizeof(NetprocFunctionHook));
872 newfcn->next = NetprocHookTable;
873 newfcn->h_function_pointer = handler;
874 NetprocHookTable = newfcn;
876 MODM_syslog(LOG_DEBUG, "Registered a new netproc function\n");
880 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
882 NetprocFunctionHook *cur, *p, *last;
884 cur = NetprocHookTable;
887 while (cur != NULL) {
888 if (handler == cur->h_function_pointer)
890 MODM_syslog(LOG_DEBUG, "Unregistered netproc function\n");
897 NetprocHookTable = p;
908 void CtdlDestroyNetprocHooks(void)
910 NetprocFunctionHook *cur, *p;
912 cur = NetprocHookTable;
915 MODM_syslog(LOG_DEBUG, "Destroyed netproc function\n");
920 NetprocHookTable = NULL;
924 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
926 DeleteFunctionHook *newfcn;
928 newfcn = (DeleteFunctionHook *)
929 malloc(sizeof(DeleteFunctionHook));
930 newfcn->next = DeleteHookTable;
931 newfcn->h_function_pointer = handler;
932 DeleteHookTable = newfcn;
934 MODM_syslog(LOG_DEBUG, "Registered a new delete function\n");
938 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
940 DeleteFunctionHook *cur, *p, *last;
943 cur = DeleteHookTable;
944 while (cur != NULL) {
945 if (handler == cur->h_function_pointer )
947 MODM_syslog(LOG_DEBUG, "Unregistered delete function\n");
964 void CtdlDestroyDeleteHooks(void)
966 DeleteFunctionHook *cur, *p;
968 cur = DeleteHookTable;
971 MODM_syslog(LOG_DEBUG, "Destroyed delete function\n");
976 DeleteHookTable = NULL;
982 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
984 FixedOutputHook *newfcn;
986 newfcn = (FixedOutputHook *)
987 malloc(sizeof(FixedOutputHook));
988 newfcn->next = FixedOutputTable;
989 newfcn->h_function_pointer = handler;
990 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
991 FixedOutputTable = newfcn;
993 MOD_syslog(LOG_DEBUG, "Registered a new fixed output function for %s\n", newfcn->content_type);
997 void CtdlUnregisterFixedOutputHook(char *content_type)
999 FixedOutputHook *cur, *p, *last;
1002 cur = FixedOutputTable;
1003 while (cur != NULL) {
1004 /* This will also remove duplicates if any */
1005 if (!strcasecmp(content_type, cur->content_type)) {
1006 MOD_syslog(LOG_DEBUG,
1007 "Unregistered fixed output function for %s\n",
1016 FixedOutputTable = p;
1028 void CtdlDestroyFixedOutputHooks(void)
1030 FixedOutputHook *cur, *p;
1032 cur = FixedOutputTable;
1035 MOD_syslog(LOG_DEBUG, "Destroyed fixed output function for %s\n", cur->content_type);
1041 FixedOutputTable = NULL;
1044 /* returns nonzero if we found a hook and used it */
1045 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
1047 FixedOutputHook *fcn;
1049 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
1050 if (!strcasecmp(content_type, fcn->content_type)) {
1051 (*fcn->h_function_pointer) (content, content_length);
1062 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1065 XmsgFunctionHook *newfcn;
1067 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
1068 newfcn->next = XmsgHookTable;
1069 newfcn->order = order;
1070 newfcn->h_function_pointer = fcn_ptr;
1071 XmsgHookTable = newfcn;
1072 MOD_syslog(LOG_DEBUG, "Registered a new x-msg function (priority %d)\n", order);
1076 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1078 XmsgFunctionHook *cur, *p, *last;
1081 cur = XmsgHookTable;
1082 while (cur != NULL) {
1083 /* This will also remove duplicates if any */
1084 if (fcn_ptr == cur->h_function_pointer &&
1085 order == cur->order) {
1086 MOD_syslog(LOG_DEBUG, "Unregistered x-msg function "
1087 "(priority %d)\n", order);
1105 void CtdlDestroyXmsgHooks(void)
1107 XmsgFunctionHook *cur, *p;
1109 cur = XmsgHookTable;
1112 MOD_syslog(LOG_DEBUG, "Destroyed x-msg function "
1113 "(priority %d)\n", cur->order);
1119 XmsgHookTable = NULL;
1123 void CtdlRegisterServiceHook(int tcp_port,
1125 void (*h_greeting_function) (void),
1126 void (*h_command_function) (void),
1127 void (*h_async_function) (void),
1128 const char *ServiceName)
1130 ServiceFunctionHook *newfcn;
1135 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1136 message = (char*) malloc (SIZ + SIZ);
1138 newfcn->next = ServiceHookTable;
1139 newfcn->tcp_port = tcp_port;
1140 newfcn->sockpath = sockpath;
1141 newfcn->h_greeting_function = h_greeting_function;
1142 newfcn->h_command_function = h_command_function;
1143 newfcn->h_async_function = h_async_function;
1144 newfcn->ServiceName = ServiceName;
1146 if (sockpath != NULL) {
1147 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
1148 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1150 else if (tcp_port <= 0) { /* port -1 to disable */
1151 MOD_syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1157 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
1159 config.c_maxsessions,
1161 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
1162 config.c_ip_addr, tcp_port, ServiceName);
1165 if (newfcn->msock > 0) {
1166 ServiceHookTable = newfcn;
1167 strcat(message, "registered.");
1168 MOD_syslog(LOG_INFO, "%s\n", message);
1171 AddPortError(message, error);
1172 strcat(message, "FAILED.");
1173 MOD_syslog(LOG_CRIT, "%s\n", message);
1180 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1181 void (*h_greeting_function) (void),
1182 void (*h_command_function) (void),
1183 void (*h_async_function) (void)
1186 ServiceFunctionHook *cur, *p, *last;
1189 cur = ServiceHookTable;
1190 while (cur != NULL) {
1191 /* This will also remove duplicates if any */
1192 if (h_greeting_function == cur->h_greeting_function &&
1193 h_command_function == cur->h_command_function &&
1194 h_async_function == cur->h_async_function &&
1195 tcp_port == cur->tcp_port &&
1196 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
1201 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1204 } else if (tcp_port) {
1205 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1207 MOD_syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1214 ServiceHookTable = p;
1225 void CtdlShutdownServiceHooks(void)
1227 /* sort of a duplicate of close_masters() but called earlier */
1228 ServiceFunctionHook *cur;
1230 cur = ServiceHookTable;
1233 if (cur->msock != -1)
1237 if (cur->sockpath != NULL){
1238 MOD_syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1241 unlink(cur->sockpath);
1243 MOD_syslog(LOG_INFO, "[%s] closing service\n",
1251 void CtdlDestroyServiceHook(void)
1254 ServiceFunctionHook *cur, *p;
1256 cur = ServiceHookTable;
1259 if (cur->msock != -1)
1266 Text = " Not closing again";
1269 if (cur->sockpath) {
1270 MOD_syslog(LOG_INFO, "%s UNIX domain socket %s\n",
1273 unlink(cur->sockpath);
1274 } else if (cur->tcp_port) {
1275 MOD_syslog(LOG_INFO, "%s TCP port %d\n", Text, cur->tcp_port);
1277 MOD_syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1283 ServiceHookTable = NULL;
1286 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1288 SearchFunctionHook *newfcn;
1290 if (!name || !fcn_ptr) {
1294 newfcn = (SearchFunctionHook *)
1295 malloc(sizeof(SearchFunctionHook));
1296 newfcn->next = SearchFunctionHookTable;
1297 newfcn->name = name;
1298 newfcn->fcn_ptr = fcn_ptr;
1299 SearchFunctionHookTable = newfcn;
1301 MOD_syslog(LOG_DEBUG, "Registered a new search function (%s)\n", name);
1304 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1306 SearchFunctionHook *cur, *p, *last;
1309 cur = SearchFunctionHookTable;
1310 while (cur != NULL) {
1312 (cur->fcn_ptr == fcn_ptr) &&
1313 name && !strcmp(name, cur->name))
1315 MOD_syslog(LOG_DEBUG, "Unregistered search function(%s)\n", name);
1321 SearchFunctionHookTable = p;
1331 void CtdlDestroySearchHooks(void)
1333 SearchFunctionHook *cur, *p;
1335 cur = SearchFunctionHookTable;
1336 SearchFunctionHookTable = NULL;
1337 while (cur != NULL) {
1344 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1346 SearchFunctionHook *fcn = NULL;
1348 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1349 if (!func_name || !strcmp(func_name, fcn->name)) {
1350 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1357 int CheckTDAPVeto (int DBType, StrBuf *ErrMsg)
1360 TDAPVetoHookFunctionHook *fcn = NULL;
1362 for (fcn = TDAPVetoHookTable; (fcn != NULL) && (Result == 0); fcn = fcn->next) {
1363 if (fcn->eventtype == DBType) {
1364 Result = (*fcn->h_function_pointer) (ErrMsg);
1370 void PerformSessionHooks(int EventType)
1372 SessionFunctionHook *fcn = NULL;
1374 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1375 if (fcn->eventtype == EventType) {
1376 if (EventType == EVT_TIMER) {
1377 pthread_setspecific(MyConKey, NULL); /* for every hook */
1379 (*fcn->h_function_pointer) ();
1384 void PerformUserHooks(ctdluser *usbuf, int EventType)
1386 UserFunctionHook *fcn = NULL;
1388 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1389 if (fcn->eventtype == EventType) {
1390 (*fcn->h_function_pointer) (usbuf);
1395 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
1397 MessageFunctionHook *fcn = NULL;
1398 int total_retval = 0;
1400 /* Other code may elect to protect this message from server-side
1401 * handlers; if this is the case, don't do anything.
1402 MOD_syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1404 if (msg->cm_flags & CM_SKIP_HOOKS) {
1405 MODM_syslog(LOG_DEBUG, "Skipping hooks\n");
1409 /* Otherwise, run all the hooks appropriate to this event type.
1411 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1412 if (fcn->eventtype == EventType) {
1413 total_retval = total_retval + (*fcn->h_function_pointer) (msg);
1417 /* Return the sum of the return codes from the hook functions. If
1418 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1419 * the save operation to abort.
1421 return total_retval;
1425 int PerformRoomHooks(struct ctdlroom *target_room)
1427 RoomFunctionHook *fcn;
1428 int total_retval = 0;
1430 MOD_syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1432 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1433 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1436 /* Return the sum of the return codes from the hook functions.
1438 return total_retval;
1442 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1444 NetprocFunctionHook *fcn;
1445 int total_retval = 0;
1447 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1448 total_retval = total_retval +
1449 (*fcn->h_function_pointer) (msg, target_room);
1452 /* Return the sum of the return codes from the hook functions.
1453 * A nonzero return code will cause the message to *not* be imported.
1455 return total_retval;
1459 void PerformDeleteHooks(char *room, long msgnum)
1461 DeleteFunctionHook *fcn;
1463 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1464 (*fcn->h_function_pointer) (room, msgnum);
1472 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1474 XmsgFunctionHook *fcn;
1478 for (p=0; p<MAX_XMSG_PRI; ++p) {
1479 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1480 if (fcn->order == p) {
1482 (*fcn->h_function_pointer)
1483 (sender, sender_email, recp, msg);
1486 /* Break out of the loop if a higher-priority function
1487 * successfully delivered the message. This prevents duplicate
1488 * deliveries to local users simultaneously signed onto
1491 if (total_sent) break;
1498 * Dirty hack until we impliment a hook mechanism for this
1500 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1503 CtdlStartTLS (ok_response, nosup_response, error_response);
1507 void DebugModulesEnable(const int n)
1511 CTDL_MODULE_INIT(modules)
1514 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);