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"
46 * Structure defentitions for hook tables
49 typedef struct __LogDebugEntry {
55 HashList *LogDebugEntryTable = NULL;
57 typedef struct LogFunctionHook LogFunctionHook;
58 struct LogFunctionHook {
59 LogFunctionHook *next;
61 void (*h_function_pointer) (char *);
63 extern LogFunctionHook *LogHookTable;
65 typedef struct FixedOutputHook FixedOutputHook;
66 struct FixedOutputHook {
67 FixedOutputHook *next;
68 char content_type[64];
69 void (*h_function_pointer) (char *, int);
71 extern FixedOutputHook *FixedOutputTable;
76 * SessionFunctionHook extensions are used for any type of hook for which
77 * the context in which it's being called (which is determined by the event
78 * type) will make it obvious for the hook function to know where to look for
81 typedef struct SessionFunctionHook SessionFunctionHook;
82 struct SessionFunctionHook {
83 SessionFunctionHook *next;
85 void (*h_function_pointer) (void);
88 extern SessionFunctionHook *SessionHookTable;
92 * UserFunctionHook extensions are used for any type of hook which implements
93 * an operation on a user or username (potentially) other than the one
94 * operating the current session.
96 typedef struct UserFunctionHook UserFunctionHook;
97 struct UserFunctionHook {
98 UserFunctionHook *next;
99 void (*h_function_pointer) (struct ctdluser *usbuf);
102 extern UserFunctionHook *UserHookTable;
105 * MessageFunctionHook extensions are used for hooks which implement handlers
106 * for various types of message operations (save, read, etc.)
108 typedef struct MessageFunctionHook MessageFunctionHook;
109 struct MessageFunctionHook {
110 MessageFunctionHook *next;
111 int (*h_function_pointer) (struct CtdlMessage *msg);
114 extern MessageFunctionHook *MessageHookTable;
118 * NetprocFunctionHook extensions are used for hooks which implement handlers
119 * for incoming network messages.
121 typedef struct NetprocFunctionHook NetprocFunctionHook;
122 struct NetprocFunctionHook {
123 NetprocFunctionHook *next;
124 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
126 extern NetprocFunctionHook *NetprocHookTable;
130 * DeleteFunctionHook extensions are used for hooks which get called when a
131 * message is about to be deleted.
133 typedef struct DeleteFunctionHook DeleteFunctionHook;
134 struct DeleteFunctionHook {
135 DeleteFunctionHook *next;
136 void (*h_function_pointer) (char *target_room, long msgnum);
138 extern DeleteFunctionHook *DeleteHookTable;
142 * ExpressMessageFunctionHook extensions are used for hooks which implement
143 * the sending of an instant message through various channels. Any function
144 * registered should return the number of recipients to whom the message was
145 * successfully transmitted.
147 typedef struct XmsgFunctionHook XmsgFunctionHook;
148 struct XmsgFunctionHook {
149 XmsgFunctionHook *next;
150 int (*h_function_pointer) (char *, char *, char *, char *);
153 extern XmsgFunctionHook *XmsgHookTable;
159 * RoomFunctionHook extensions are used for hooks which impliment room
160 * processing functions when new messages are added EG. SIEVE.
162 typedef struct RoomFunctionHook RoomFunctionHook;
163 struct RoomFunctionHook {
164 RoomFunctionHook *next;
165 int (*fcn_ptr) (struct ctdlroom *);
167 extern RoomFunctionHook *RoomHookTable;
171 typedef struct SearchFunctionHook SearchFunctionHook;
172 struct SearchFunctionHook {
173 SearchFunctionHook *next;
174 void (*fcn_ptr) (int *, long **, const char *);
177 extern SearchFunctionHook *SearchFunctionHookTable;
180 CleanupFunctionHook *CleanupHookTable = NULL;
181 CleanupFunctionHook *EVCleanupHookTable = NULL;
182 SessionFunctionHook *SessionHookTable = NULL;
183 UserFunctionHook *UserHookTable = NULL;
184 XmsgFunctionHook *XmsgHookTable = NULL;
185 MessageFunctionHook *MessageHookTable = NULL;
186 NetprocFunctionHook *NetprocHookTable = NULL;
187 DeleteFunctionHook *DeleteHookTable = NULL;
188 ServiceFunctionHook *ServiceHookTable = NULL;
189 FixedOutputHook *FixedOutputTable = NULL;
190 RoomFunctionHook *RoomHookTable = NULL;
191 SearchFunctionHook *SearchFunctionHookTable = NULL;
193 typedef struct ProtoFunctionHook ProtoFunctionHook;
194 struct ProtoFunctionHook {
195 void (*handler) (char *cmdbuf);
200 HashList *ProtoHookList = NULL;
203 #define ERR_PORT (1 << 1)
206 static StrBuf *portlist = NULL;
208 static StrBuf *errormessages = NULL;
211 long DetailErrorFlags;
212 ConstStr Empty = {HKEY("")};
213 char *ErrSubject = "Startup Problems";
214 ConstStr ErrGeneral[] = {
215 {HKEY("Citadel had trouble on starting up. ")},
216 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
217 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
218 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
219 "The errors returned by the system were:\n")},
220 {HKEY("You can recheck the above if you follow this faq item:\n"
221 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
224 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
225 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
226 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
227 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
228 " and stop the program that binds these ports.\n You should eventually remove "
229 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
230 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
233 void LogPrintMessages(long err)
236 StrBuf *List, *DetailList;
237 ConstStr *Short, *Where, *Hint;
240 Message = NewStrBufPlain(NULL,
241 StrLength(portlist) + StrLength(errormessages));
243 DetailErrorFlags = DetailErrorFlags & ~err;
248 Short = &ErrPortShort;
249 Where = &ErrPortWhere;
252 DetailList = errormessages;
262 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
263 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
264 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
265 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
266 StrBufAppendBuf(Message, List, 0);
267 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
268 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
269 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
270 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
271 StrBufAppendBuf(Message, DetailList, 0);
272 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
273 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
275 MOD_syslog(LOG_EMERG, "%s", ChrPtr(Message));
276 MOD_syslog(LOG_EMERG, "%s", ErrSubject);
277 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
279 FreeStrBuf(&Message);
281 FreeStrBuf(&DetailList);
285 void AddPortError(char *Port, char *ErrorMessage)
289 DetailErrorFlags |= ERR_PORT;
291 len = StrLength(errormessages);
292 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
293 else errormessages = NewStrBuf();
294 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
297 len = StrLength(portlist);
298 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
299 else portlist = NewStrBuf();
300 StrBufAppendBufPlain(portlist, Port, -1, 0);
304 int DLoader_Exec_Cmd(char *cmdbuf)
307 ProtoFunctionHook *p;
309 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
310 p = (ProtoFunctionHook*) vP;
311 p->handler(&cmdbuf[5]);
317 long FourHash(const char *key, long length)
321 const unsigned char *ptr = (const unsigned char*)key;
323 for (i = 0; i < 4; i++, ptr ++)
333 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
336 if (LogDebugEntryTable == NULL)
337 LogDebugEntryTable = NewHash(1, NULL);
338 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
343 Put(LogDebugEntryTable, Name, Len, E, NULL);
346 void CtdlSetDebugLogFacilities(const char **Str, long n)
348 StrBuf *Token = NULL;
355 for (i=0; i < n; i++){
356 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
357 if (strcmp(Str[i], "all") == 0) {
361 Buf = NewStrBufPlain(Str[i], -1);
364 Token = NewStrBufPlain(NULL, StrLength(Buf));
365 while ((ch != StrBufNOTNULL) &&
366 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
367 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
370 LogDebugEntry *E = (LogDebugEntry*)vptr;
383 Pos = GetNewHashPos(LogDebugEntryTable, 0);
384 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
385 LogDebugEntry *E = (LogDebugEntry*)vptr;
392 void cmd_log_get(char *argbuf)
399 if (CtdlAccessCheck(ac_aide)) return;
401 cprintf("%d Log modules enabled:\n", LISTING_FOLLOWS);
403 Pos = GetNewHashPos(LogDebugEntryTable, 0);
405 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
406 LogDebugEntry *E = (LogDebugEntry*)vptr;
407 cprintf("%s|%d\n", ch, *E->LogP);
413 void cmd_log_set(char *argbuf)
418 char which[SIZ] = "";
420 if (CtdlAccessCheck(ac_aide)) return;
422 wlen = extract_token(which, argbuf, 0, '|', sizeof(which));
423 if (wlen < 0) wlen = 0;
424 lset = extract_int(argbuf, 1);
425 if (lset != 0) lset = 1;
426 if (GetHash(LogDebugEntryTable, which, wlen, &vptr) &&
429 LogDebugEntry *E = (LogDebugEntry*)vptr;
431 cprintf("%d %s|%d\n", CIT_OK, which, lset);
434 cprintf("%d Log setting %s not known\n",
438 void CtdlDestroyDebugTable(void)
441 DeleteHash(&LogDebugEntryTable);
444 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
446 ProtoFunctionHook *p;
448 if (ProtoHookList == NULL)
449 ProtoHookList = NewHash (1, FourHash);
452 p = (ProtoFunctionHook *)
453 malloc(sizeof(ProtoFunctionHook));
456 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
459 p->handler = handler;
463 Put(ProtoHookList, cmd, 4, p, NULL);
464 MOD_syslog(LOG_DEBUG, "Registered server command %s (%s)\n", cmd, desc);
467 void CtdlDestroyProtoHooks(void)
470 DeleteHash(&ProtoHookList);
474 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
477 CleanupFunctionHook *newfcn;
479 newfcn = (CleanupFunctionHook *)
480 malloc(sizeof(CleanupFunctionHook));
481 newfcn->next = CleanupHookTable;
482 newfcn->h_function_pointer = fcn_ptr;
483 CleanupHookTable = newfcn;
485 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
489 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
491 CleanupFunctionHook *cur, *p, *last;
493 cur = CleanupHookTable;
496 if (fcn_ptr == cur->h_function_pointer)
498 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
507 CleanupHookTable = p;
518 void CtdlDestroyCleanupHooks(void)
520 CleanupFunctionHook *cur, *p;
522 cur = CleanupHookTable;
525 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
530 CleanupHookTable = NULL;
533 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
536 CleanupFunctionHook *newfcn;
538 newfcn = (CleanupFunctionHook *)
539 malloc(sizeof(CleanupFunctionHook));
540 newfcn->next = EVCleanupHookTable;
541 newfcn->h_function_pointer = fcn_ptr;
542 EVCleanupHookTable = newfcn;
544 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
548 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
550 CleanupFunctionHook *cur, *p, *last;
552 cur = EVCleanupHookTable;
555 if (fcn_ptr == cur->h_function_pointer)
557 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
566 EVCleanupHookTable = p;
577 void CtdlDestroyEVCleanupHooks(void)
579 CleanupFunctionHook *cur, *p;
581 cur = EVCleanupHookTable;
584 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
589 EVCleanupHookTable = NULL;
593 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
595 SessionFunctionHook *newfcn;
597 newfcn = (SessionFunctionHook *)
598 malloc(sizeof(SessionFunctionHook));
599 newfcn->Priority = Priority;
600 newfcn->h_function_pointer = fcn_ptr;
601 newfcn->eventtype = EventType;
603 SessionFunctionHook **pfcn;
604 pfcn = &SessionHookTable;
605 while ((*pfcn != NULL) &&
606 ((*pfcn)->Priority < newfcn->Priority) &&
607 ((*pfcn)->next != NULL))
608 pfcn = &(*pfcn)->next;
610 newfcn->next = *pfcn;
613 MOD_syslog(LOG_DEBUG, "Registered a new session function (type %d Priority %d)\n",
614 EventType, Priority);
618 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
620 SessionFunctionHook *cur, *p, *last;
622 cur = SessionHookTable;
623 while (cur != NULL) {
624 if ((fcn_ptr == cur->h_function_pointer) &&
625 (EventType == cur->eventtype))
627 MOD_syslog(LOG_DEBUG, "Unregistered session function (type %d)\n",
637 SessionHookTable = p;
647 void CtdlDestroySessionHooks(void)
649 SessionFunctionHook *cur, *p;
651 cur = SessionHookTable;
654 MODM_syslog(LOG_DEBUG, "Destroyed session function\n");
659 SessionHookTable = NULL;
663 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
666 UserFunctionHook *newfcn;
668 newfcn = (UserFunctionHook *)
669 malloc(sizeof(UserFunctionHook));
670 newfcn->next = UserHookTable;
671 newfcn->h_function_pointer = fcn_ptr;
672 newfcn->eventtype = EventType;
673 UserHookTable = newfcn;
675 MOD_syslog(LOG_DEBUG, "Registered a new user function (type %d)\n",
680 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
682 UserFunctionHook *cur, *p, *last;
685 while (cur != NULL) {
686 if ((fcn_ptr == cur->h_function_pointer) &&
687 (EventType == cur->eventtype))
689 MOD_syslog(LOG_DEBUG, "Unregistered user function (type %d)\n",
709 void CtdlDestroyUserHooks(void)
711 UserFunctionHook *cur, *p;
716 MODM_syslog(LOG_DEBUG, "Destroyed user function \n");
721 UserHookTable = NULL;
725 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
729 MessageFunctionHook *newfcn;
731 newfcn = (MessageFunctionHook *)
732 malloc(sizeof(MessageFunctionHook));
733 newfcn->next = MessageHookTable;
734 newfcn->h_function_pointer = handler;
735 newfcn->eventtype = EventType;
736 MessageHookTable = newfcn;
738 MOD_syslog(LOG_DEBUG, "Registered a new message function (type %d)\n",
743 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
746 MessageFunctionHook *cur, *p, *last;
748 cur = MessageHookTable;
749 while (cur != NULL) {
750 if ((handler == cur->h_function_pointer) &&
751 (EventType == cur->eventtype))
753 MOD_syslog(LOG_DEBUG, "Unregistered message function (type %d)\n",
762 MessageHookTable = p;
772 void CtdlDestroyMessageHook(void)
774 MessageFunctionHook *cur, *p;
776 cur = MessageHookTable;
779 MOD_syslog(LOG_DEBUG, "Destroyed message function (type %d)\n", cur->eventtype);
784 MessageHookTable = NULL;
788 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
790 RoomFunctionHook *newfcn;
792 newfcn = (RoomFunctionHook *)
793 malloc(sizeof(RoomFunctionHook));
794 newfcn->next = RoomHookTable;
795 newfcn->fcn_ptr = fcn_ptr;
796 RoomHookTable = newfcn;
798 MODM_syslog(LOG_DEBUG, "Registered a new room function\n");
802 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
804 RoomFunctionHook *cur, *p, *last;
809 if (fcn_ptr == cur->fcn_ptr) {
810 MODM_syslog(LOG_DEBUG, "Unregistered room function\n");
830 void CtdlDestroyRoomHooks(void)
832 RoomFunctionHook *cur, *p;
837 MODM_syslog(LOG_DEBUG, "Destroyed room function\n");
842 RoomHookTable = NULL;
845 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
847 NetprocFunctionHook *newfcn;
849 newfcn = (NetprocFunctionHook *)
850 malloc(sizeof(NetprocFunctionHook));
851 newfcn->next = NetprocHookTable;
852 newfcn->h_function_pointer = handler;
853 NetprocHookTable = newfcn;
855 MODM_syslog(LOG_DEBUG, "Registered a new netproc function\n");
859 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
861 NetprocFunctionHook *cur, *p, *last;
863 cur = NetprocHookTable;
866 while (cur != NULL) {
867 if (handler == cur->h_function_pointer)
869 MODM_syslog(LOG_DEBUG, "Unregistered netproc function\n");
876 NetprocHookTable = p;
887 void CtdlDestroyNetprocHooks(void)
889 NetprocFunctionHook *cur, *p;
891 cur = NetprocHookTable;
894 MODM_syslog(LOG_DEBUG, "Destroyed netproc function\n");
899 NetprocHookTable = NULL;
903 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
905 DeleteFunctionHook *newfcn;
907 newfcn = (DeleteFunctionHook *)
908 malloc(sizeof(DeleteFunctionHook));
909 newfcn->next = DeleteHookTable;
910 newfcn->h_function_pointer = handler;
911 DeleteHookTable = newfcn;
913 MODM_syslog(LOG_DEBUG, "Registered a new delete function\n");
917 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
919 DeleteFunctionHook *cur, *p, *last;
922 cur = DeleteHookTable;
923 while (cur != NULL) {
924 if (handler == cur->h_function_pointer )
926 MODM_syslog(LOG_DEBUG, "Unregistered delete function\n");
943 void CtdlDestroyDeleteHooks(void)
945 DeleteFunctionHook *cur, *p;
947 cur = DeleteHookTable;
950 MODM_syslog(LOG_DEBUG, "Destroyed delete function\n");
955 DeleteHookTable = NULL;
961 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
963 FixedOutputHook *newfcn;
965 newfcn = (FixedOutputHook *)
966 malloc(sizeof(FixedOutputHook));
967 newfcn->next = FixedOutputTable;
968 newfcn->h_function_pointer = handler;
969 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
970 FixedOutputTable = newfcn;
972 MOD_syslog(LOG_DEBUG, "Registered a new fixed output function for %s\n", newfcn->content_type);
976 void CtdlUnregisterFixedOutputHook(char *content_type)
978 FixedOutputHook *cur, *p, *last;
981 cur = FixedOutputTable;
982 while (cur != NULL) {
983 /* This will also remove duplicates if any */
984 if (!strcasecmp(content_type, cur->content_type)) {
985 MOD_syslog(LOG_DEBUG,
986 "Unregistered fixed output function for %s\n",
995 FixedOutputTable = p;
1007 void CtdlDestroyFixedOutputHooks(void)
1009 FixedOutputHook *cur, *p;
1011 cur = FixedOutputTable;
1014 MOD_syslog(LOG_DEBUG, "Destroyed fixed output function for %s\n", cur->content_type);
1020 FixedOutputTable = NULL;
1023 /* returns nonzero if we found a hook and used it */
1024 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
1026 FixedOutputHook *fcn;
1028 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
1029 if (!strcasecmp(content_type, fcn->content_type)) {
1030 (*fcn->h_function_pointer) (content, content_length);
1041 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1044 XmsgFunctionHook *newfcn;
1046 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
1047 newfcn->next = XmsgHookTable;
1048 newfcn->order = order;
1049 newfcn->h_function_pointer = fcn_ptr;
1050 XmsgHookTable = newfcn;
1051 MOD_syslog(LOG_DEBUG, "Registered a new x-msg function (priority %d)\n", order);
1055 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1057 XmsgFunctionHook *cur, *p, *last;
1060 cur = XmsgHookTable;
1061 while (cur != NULL) {
1062 /* This will also remove duplicates if any */
1063 if (fcn_ptr == cur->h_function_pointer &&
1064 order == cur->order) {
1065 MOD_syslog(LOG_DEBUG, "Unregistered x-msg function "
1066 "(priority %d)\n", order);
1084 void CtdlDestroyXmsgHooks(void)
1086 XmsgFunctionHook *cur, *p;
1088 cur = XmsgHookTable;
1091 MOD_syslog(LOG_DEBUG, "Destroyed x-msg function "
1092 "(priority %d)\n", cur->order);
1098 XmsgHookTable = NULL;
1102 void CtdlRegisterServiceHook(int tcp_port,
1104 void (*h_greeting_function) (void),
1105 void (*h_command_function) (void),
1106 void (*h_async_function) (void),
1107 const char *ServiceName)
1109 ServiceFunctionHook *newfcn;
1114 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1115 message = (char*) malloc (SIZ + SIZ);
1117 newfcn->next = ServiceHookTable;
1118 newfcn->tcp_port = tcp_port;
1119 newfcn->sockpath = sockpath;
1120 newfcn->h_greeting_function = h_greeting_function;
1121 newfcn->h_command_function = h_command_function;
1122 newfcn->h_async_function = h_async_function;
1123 newfcn->ServiceName = ServiceName;
1125 if (sockpath != NULL) {
1126 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
1127 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1129 else if (tcp_port <= 0) { /* port -1 to disable */
1130 MOD_syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1136 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
1138 config.c_maxsessions,
1140 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
1141 config.c_ip_addr, tcp_port, ServiceName);
1144 if (newfcn->msock > 0) {
1145 ServiceHookTable = newfcn;
1146 strcat(message, "registered.");
1147 MOD_syslog(LOG_INFO, "%s\n", message);
1150 AddPortError(message, error);
1151 strcat(message, "FAILED.");
1152 MOD_syslog(LOG_CRIT, "%s\n", message);
1159 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1160 void (*h_greeting_function) (void),
1161 void (*h_command_function) (void),
1162 void (*h_async_function) (void)
1165 ServiceFunctionHook *cur, *p, *last;
1168 cur = ServiceHookTable;
1169 while (cur != NULL) {
1170 /* This will also remove duplicates if any */
1171 if (h_greeting_function == cur->h_greeting_function &&
1172 h_command_function == cur->h_command_function &&
1173 h_async_function == cur->h_async_function &&
1174 tcp_port == cur->tcp_port &&
1175 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
1180 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1183 } else if (tcp_port) {
1184 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1186 MOD_syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1193 ServiceHookTable = p;
1204 void CtdlShutdownServiceHooks(void)
1206 /* sort of a duplicate of close_masters() but called earlier */
1207 ServiceFunctionHook *cur;
1209 cur = ServiceHookTable;
1212 if (cur->msock != -1)
1216 if (cur->sockpath != NULL){
1217 MOD_syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1220 unlink(cur->sockpath);
1222 MOD_syslog(LOG_INFO, "[%s] closing service\n",
1230 void CtdlDestroyServiceHook(void)
1232 ServiceFunctionHook *cur, *p;
1234 cur = ServiceHookTable;
1238 if (cur->sockpath) {
1239 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1241 unlink(cur->sockpath);
1242 } else if (cur->tcp_port) {
1243 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", cur->tcp_port);
1245 MOD_syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1251 ServiceHookTable = NULL;
1254 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1256 SearchFunctionHook *newfcn;
1258 if (!name || !fcn_ptr) {
1262 newfcn = (SearchFunctionHook *)
1263 malloc(sizeof(SearchFunctionHook));
1264 newfcn->next = SearchFunctionHookTable;
1265 newfcn->name = name;
1266 newfcn->fcn_ptr = fcn_ptr;
1267 SearchFunctionHookTable = newfcn;
1269 MOD_syslog(LOG_DEBUG, "Registered a new search function (%s)\n", name);
1272 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1274 SearchFunctionHook *cur, *p, *last;
1277 cur = SearchFunctionHookTable;
1278 while (cur != NULL) {
1280 (cur->fcn_ptr == fcn_ptr) &&
1281 name && !strcmp(name, cur->name))
1283 MOD_syslog(LOG_DEBUG, "Unregistered search function(%s)\n", name);
1289 SearchFunctionHookTable = p;
1299 void CtdlDestroySearchHooks(void)
1301 SearchFunctionHook *cur, *p;
1303 cur = SearchFunctionHookTable;
1304 SearchFunctionHookTable = NULL;
1305 while (cur != NULL) {
1312 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1314 SearchFunctionHook *fcn = NULL;
1316 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1317 if (!func_name || !strcmp(func_name, fcn->name)) {
1318 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1326 void PerformSessionHooks(int EventType)
1328 SessionFunctionHook *fcn = NULL;
1330 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1331 if (fcn->eventtype == EventType) {
1332 if (EventType == EVT_TIMER) {
1333 pthread_setspecific(MyConKey, NULL); /* for every hook */
1335 (*fcn->h_function_pointer) ();
1340 void PerformUserHooks(ctdluser *usbuf, int EventType)
1342 UserFunctionHook *fcn = NULL;
1344 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1345 if (fcn->eventtype == EventType) {
1346 (*fcn->h_function_pointer) (usbuf);
1351 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
1353 MessageFunctionHook *fcn = NULL;
1354 int total_retval = 0;
1356 /* Other code may elect to protect this message from server-side
1357 * handlers; if this is the case, don't do anything.
1358 MOD_syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1360 if (msg->cm_flags & CM_SKIP_HOOKS) {
1361 MODM_syslog(LOG_DEBUG, "Skipping hooks\n");
1365 /* Otherwise, run all the hooks appropriate to this event type.
1367 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1368 if (fcn->eventtype == EventType) {
1369 total_retval = total_retval + (*fcn->h_function_pointer) (msg);
1373 /* Return the sum of the return codes from the hook functions. If
1374 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1375 * the save operation to abort.
1377 return total_retval;
1381 int PerformRoomHooks(struct ctdlroom *target_room)
1383 RoomFunctionHook *fcn;
1384 int total_retval = 0;
1386 MOD_syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1388 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1389 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1392 /* Return the sum of the return codes from the hook functions.
1394 return total_retval;
1398 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1400 NetprocFunctionHook *fcn;
1401 int total_retval = 0;
1403 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1404 total_retval = total_retval +
1405 (*fcn->h_function_pointer) (msg, target_room);
1408 /* Return the sum of the return codes from the hook functions.
1409 * A nonzero return code will cause the message to *not* be imported.
1411 return total_retval;
1415 void PerformDeleteHooks(char *room, long msgnum)
1417 DeleteFunctionHook *fcn;
1419 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1420 (*fcn->h_function_pointer) (room, msgnum);
1428 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1430 XmsgFunctionHook *fcn;
1434 for (p=0; p<MAX_XMSG_PRI; ++p) {
1435 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1436 if (fcn->order == p) {
1438 (*fcn->h_function_pointer)
1439 (sender, sender_email, recp, msg);
1442 /* Break out of the loop if a higher-priority function
1443 * successfully delivered the message. This prevents duplicate
1444 * deliveries to local users simultaneously signed onto
1447 if (total_sent) break;
1454 * Dirty hack until we impliment a hook mechanism for this
1456 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1459 CtdlStartTLS (ok_response, nosup_response, error_response);
1463 void DebugModulesEnable(const int n)
1467 CTDL_MODULE_INIT(modules)
1470 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);
1472 CtdlRegisterProtoHook(cmd_log_get, "LOGP", "Print Log-parameters");
1473 CtdlRegisterProtoHook(cmd_log_set, "LOGS", "Set Log-parameters");