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 as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
32 #include <libcitadel.h>
35 #include "serv_extensions.h"
36 #include "sysdep_decls.h"
40 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
42 #include "ctdl_module.h"
52 * Structure defentitions for hook tables
55 typedef struct __LogDebugEntry {
61 HashList *LogDebugEntryTable = NULL;
63 typedef struct LogFunctionHook LogFunctionHook;
64 struct LogFunctionHook {
65 LogFunctionHook *next;
67 void (*h_function_pointer) (char *);
69 extern LogFunctionHook *LogHookTable;
71 typedef struct FixedOutputHook FixedOutputHook;
72 struct FixedOutputHook {
73 FixedOutputHook *next;
74 char content_type[64];
75 void (*h_function_pointer) (char *, int);
77 extern FixedOutputHook *FixedOutputTable;
82 * SessionFunctionHook extensions are used for any type of hook for which
83 * the context in which it's being called (which is determined by the event
84 * type) will make it obvious for the hook function to know where to look for
87 typedef struct SessionFunctionHook SessionFunctionHook;
88 struct SessionFunctionHook {
89 SessionFunctionHook *next;
91 void (*h_function_pointer) (void);
94 extern SessionFunctionHook *SessionHookTable;
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 extern UserFunctionHook *UserHookTable;
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 extern MessageFunctionHook *MessageHookTable;
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 extern NetprocFunctionHook *NetprocHookTable;
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 extern DeleteFunctionHook *DeleteHookTable;
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 extern XmsgFunctionHook *XmsgHookTable;
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 extern RoomFunctionHook *RoomHookTable;
177 typedef struct SearchFunctionHook SearchFunctionHook;
178 struct SearchFunctionHook {
179 SearchFunctionHook *next;
180 void (*fcn_ptr) (int *, long **, const char *);
183 extern SearchFunctionHook *SearchFunctionHookTable;
186 CleanupFunctionHook *CleanupHookTable = NULL;
187 CleanupFunctionHook *EVCleanupHookTable = NULL;
188 SessionFunctionHook *SessionHookTable = NULL;
189 UserFunctionHook *UserHookTable = NULL;
190 XmsgFunctionHook *XmsgHookTable = NULL;
191 MessageFunctionHook *MessageHookTable = NULL;
192 NetprocFunctionHook *NetprocHookTable = NULL;
193 DeleteFunctionHook *DeleteHookTable = NULL;
194 ServiceFunctionHook *ServiceHookTable = NULL;
195 FixedOutputHook *FixedOutputTable = NULL;
196 RoomFunctionHook *RoomHookTable = NULL;
197 SearchFunctionHook *SearchFunctionHookTable = NULL;
199 typedef struct ProtoFunctionHook ProtoFunctionHook;
200 struct ProtoFunctionHook {
201 void (*handler) (char *cmdbuf);
206 HashList *ProtoHookList = NULL;
209 #define ERR_PORT (1 << 1)
212 static StrBuf *portlist = NULL;
214 static StrBuf *errormessages = NULL;
217 long DetailErrorFlags;
218 ConstStr Empty = {HKEY("")};
219 char *ErrSubject = "Startup Problems";
220 ConstStr ErrGeneral[] = {
221 {HKEY("Citadel had trouble on starting up. ")},
222 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
223 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
224 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
225 "The errors returned by the system were:\n")},
226 {HKEY("You can recheck the above if you follow this faq item:\n"
227 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
230 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
231 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
232 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
233 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
234 " and stop the program that binds these ports.\n You should eventually remove "
235 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
236 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
239 void LogPrintMessages(long err)
242 StrBuf *List, *DetailList;
243 ConstStr *Short, *Where, *Hint;
246 Message = NewStrBufPlain(NULL,
247 StrLength(portlist) + StrLength(errormessages));
249 DetailErrorFlags = DetailErrorFlags & ~err;
254 Short = &ErrPortShort;
255 Where = &ErrPortWhere;
258 DetailList = errormessages;
268 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
269 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
270 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
271 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
272 StrBufAppendBuf(Message, List, 0);
273 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
274 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
275 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
276 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
277 StrBufAppendBuf(Message, DetailList, 0);
278 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
279 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
281 MOD_syslog(LOG_EMERG, "%s", ChrPtr(Message));
282 MOD_syslog(LOG_EMERG, "%s", ErrSubject);
283 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
285 FreeStrBuf(&Message);
287 FreeStrBuf(&DetailList);
291 void AddPortError(char *Port, char *ErrorMessage)
295 DetailErrorFlags |= ERR_PORT;
297 len = StrLength(errormessages);
298 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
299 else errormessages = NewStrBuf();
300 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
303 len = StrLength(portlist);
304 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
305 else portlist = NewStrBuf();
306 StrBufAppendBufPlain(portlist, Port, -1, 0);
310 int DLoader_Exec_Cmd(char *cmdbuf)
313 ProtoFunctionHook *p;
315 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
316 p = (ProtoFunctionHook*) vP;
317 p->handler(&cmdbuf[5]);
323 long FourHash(const char *key, long length)
327 const unsigned char *ptr = (const unsigned char*)key;
329 for (i = 0; i < 4; i++, ptr ++)
339 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
342 if (LogDebugEntryTable == NULL)
343 LogDebugEntryTable = NewHash(1, NULL);
344 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
349 Put(LogDebugEntryTable, Name, Len, E, NULL);
352 void CtdlSetDebugLogFacilities(const char **Str, long n)
354 StrBuf *Token = NULL;
361 for (i=0; i < n; i++){
362 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
363 if (strcmp(Str[i], "all") == 0) {
367 Buf = NewStrBufPlain(Str[i], -1);
370 Token = NewStrBufPlain(NULL, StrLength(Buf));
371 while ((ch != StrBufNOTNULL) &&
372 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
373 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
376 LogDebugEntry *E = (LogDebugEntry*)vptr;
389 Pos = GetNewHashPos(LogDebugEntryTable, 0);
390 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
391 LogDebugEntry *E = (LogDebugEntry*)vptr;
398 void cmd_log_get(char *argbuf)
405 if (CtdlAccessCheck(ac_aide)) return;
407 cprintf("%d Log modules enabled:\n", LISTING_FOLLOWS);
409 Pos = GetNewHashPos(LogDebugEntryTable, 0);
411 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
412 LogDebugEntry *E = (LogDebugEntry*)vptr;
413 cprintf("%s|%d\n", ch, *E->LogP);
419 void cmd_log_set(char *argbuf)
424 char which[SIZ] = "";
426 if (CtdlAccessCheck(ac_aide)) return;
428 wlen = extract_token(which, argbuf, 0, '|', sizeof(which));
429 if (wlen < 0) wlen = 0;
430 lset = extract_int(argbuf, 1);
431 if (lset != 0) lset = 1;
432 if (GetHash(LogDebugEntryTable, which, wlen, &vptr) &&
435 LogDebugEntry *E = (LogDebugEntry*)vptr;
437 cprintf("%d %s|%d\n", CIT_OK, which, lset);
440 cprintf("%d Log setting %s not known\n",
444 void CtdlDestroyDebugTable(void)
447 DeleteHash(&LogDebugEntryTable);
450 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
452 ProtoFunctionHook *p;
454 if (ProtoHookList == NULL)
455 ProtoHookList = NewHash (1, FourHash);
458 p = (ProtoFunctionHook *)
459 malloc(sizeof(ProtoFunctionHook));
462 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
465 p->handler = handler;
469 Put(ProtoHookList, cmd, 4, p, NULL);
470 MOD_syslog(LOG_DEBUG, "Registered server command %s (%s)\n", cmd, desc);
473 void CtdlDestroyProtoHooks(void)
476 DeleteHash(&ProtoHookList);
480 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
483 CleanupFunctionHook *newfcn;
485 newfcn = (CleanupFunctionHook *)
486 malloc(sizeof(CleanupFunctionHook));
487 newfcn->next = CleanupHookTable;
488 newfcn->h_function_pointer = fcn_ptr;
489 CleanupHookTable = newfcn;
491 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
495 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
497 CleanupFunctionHook *cur, *p, *last;
499 cur = CleanupHookTable;
502 if (fcn_ptr == cur->h_function_pointer)
504 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
513 CleanupHookTable = p;
524 void CtdlDestroyCleanupHooks(void)
526 CleanupFunctionHook *cur, *p;
528 cur = CleanupHookTable;
531 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
536 CleanupHookTable = NULL;
539 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
542 CleanupFunctionHook *newfcn;
544 newfcn = (CleanupFunctionHook *)
545 malloc(sizeof(CleanupFunctionHook));
546 newfcn->next = EVCleanupHookTable;
547 newfcn->h_function_pointer = fcn_ptr;
548 EVCleanupHookTable = newfcn;
550 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
554 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
556 CleanupFunctionHook *cur, *p, *last;
558 cur = EVCleanupHookTable;
561 if (fcn_ptr == cur->h_function_pointer)
563 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
572 EVCleanupHookTable = p;
583 void CtdlDestroyEVCleanupHooks(void)
585 CleanupFunctionHook *cur, *p;
587 cur = EVCleanupHookTable;
590 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
595 EVCleanupHookTable = NULL;
599 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
601 SessionFunctionHook *newfcn;
603 newfcn = (SessionFunctionHook *)
604 malloc(sizeof(SessionFunctionHook));
605 newfcn->Priority = Priority;
606 newfcn->h_function_pointer = fcn_ptr;
607 newfcn->eventtype = EventType;
609 SessionFunctionHook **pfcn;
610 pfcn = &SessionHookTable;
611 while ((*pfcn != NULL) &&
612 ((*pfcn)->Priority < newfcn->Priority) &&
613 ((*pfcn)->next != NULL))
614 pfcn = &(*pfcn)->next;
616 newfcn->next = *pfcn;
619 MOD_syslog(LOG_DEBUG, "Registered a new session function (type %d Priority %d)\n",
620 EventType, Priority);
624 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
626 SessionFunctionHook *cur, *p, *last;
628 cur = SessionHookTable;
629 while (cur != NULL) {
630 if ((fcn_ptr == cur->h_function_pointer) &&
631 (EventType == cur->eventtype))
633 MOD_syslog(LOG_DEBUG, "Unregistered session function (type %d)\n",
643 SessionHookTable = p;
653 void CtdlDestroySessionHooks(void)
655 SessionFunctionHook *cur, *p;
657 cur = SessionHookTable;
660 MODM_syslog(LOG_DEBUG, "Destroyed session function\n");
665 SessionHookTable = NULL;
669 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
672 UserFunctionHook *newfcn;
674 newfcn = (UserFunctionHook *)
675 malloc(sizeof(UserFunctionHook));
676 newfcn->next = UserHookTable;
677 newfcn->h_function_pointer = fcn_ptr;
678 newfcn->eventtype = EventType;
679 UserHookTable = newfcn;
681 MOD_syslog(LOG_DEBUG, "Registered a new user function (type %d)\n",
686 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
688 UserFunctionHook *cur, *p, *last;
691 while (cur != NULL) {
692 if ((fcn_ptr == cur->h_function_pointer) &&
693 (EventType == cur->eventtype))
695 MOD_syslog(LOG_DEBUG, "Unregistered user function (type %d)\n",
715 void CtdlDestroyUserHooks(void)
717 UserFunctionHook *cur, *p;
722 MODM_syslog(LOG_DEBUG, "Destroyed user function \n");
727 UserHookTable = NULL;
731 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
735 MessageFunctionHook *newfcn;
737 newfcn = (MessageFunctionHook *)
738 malloc(sizeof(MessageFunctionHook));
739 newfcn->next = MessageHookTable;
740 newfcn->h_function_pointer = handler;
741 newfcn->eventtype = EventType;
742 MessageHookTable = newfcn;
744 MOD_syslog(LOG_DEBUG, "Registered a new message function (type %d)\n",
749 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
752 MessageFunctionHook *cur, *p, *last;
754 cur = MessageHookTable;
755 while (cur != NULL) {
756 if ((handler == cur->h_function_pointer) &&
757 (EventType == cur->eventtype))
759 MOD_syslog(LOG_DEBUG, "Unregistered message function (type %d)\n",
768 MessageHookTable = p;
778 void CtdlDestroyMessageHook(void)
780 MessageFunctionHook *cur, *p;
782 cur = MessageHookTable;
785 MOD_syslog(LOG_DEBUG, "Destroyed message function (type %d)\n", cur->eventtype);
790 MessageHookTable = NULL;
794 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
796 RoomFunctionHook *newfcn;
798 newfcn = (RoomFunctionHook *)
799 malloc(sizeof(RoomFunctionHook));
800 newfcn->next = RoomHookTable;
801 newfcn->fcn_ptr = fcn_ptr;
802 RoomHookTable = newfcn;
804 MODM_syslog(LOG_DEBUG, "Registered a new room function\n");
808 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
810 RoomFunctionHook *cur, *p, *last;
815 if (fcn_ptr == cur->fcn_ptr) {
816 MODM_syslog(LOG_DEBUG, "Unregistered room function\n");
836 void CtdlDestroyRoomHooks(void)
838 RoomFunctionHook *cur, *p;
843 MODM_syslog(LOG_DEBUG, "Destroyed room function\n");
848 RoomHookTable = NULL;
851 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
853 NetprocFunctionHook *newfcn;
855 newfcn = (NetprocFunctionHook *)
856 malloc(sizeof(NetprocFunctionHook));
857 newfcn->next = NetprocHookTable;
858 newfcn->h_function_pointer = handler;
859 NetprocHookTable = newfcn;
861 MODM_syslog(LOG_DEBUG, "Registered a new netproc function\n");
865 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
867 NetprocFunctionHook *cur, *p, *last;
869 cur = NetprocHookTable;
872 while (cur != NULL) {
873 if (handler == cur->h_function_pointer)
875 MODM_syslog(LOG_DEBUG, "Unregistered netproc function\n");
882 NetprocHookTable = p;
893 void CtdlDestroyNetprocHooks(void)
895 NetprocFunctionHook *cur, *p;
897 cur = NetprocHookTable;
900 MODM_syslog(LOG_DEBUG, "Destroyed netproc function\n");
905 NetprocHookTable = NULL;
909 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
911 DeleteFunctionHook *newfcn;
913 newfcn = (DeleteFunctionHook *)
914 malloc(sizeof(DeleteFunctionHook));
915 newfcn->next = DeleteHookTable;
916 newfcn->h_function_pointer = handler;
917 DeleteHookTable = newfcn;
919 MODM_syslog(LOG_DEBUG, "Registered a new delete function\n");
923 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
925 DeleteFunctionHook *cur, *p, *last;
928 cur = DeleteHookTable;
929 while (cur != NULL) {
930 if (handler == cur->h_function_pointer )
932 MODM_syslog(LOG_DEBUG, "Unregistered delete function\n");
949 void CtdlDestroyDeleteHooks(void)
951 DeleteFunctionHook *cur, *p;
953 cur = DeleteHookTable;
956 MODM_syslog(LOG_DEBUG, "Destroyed delete function\n");
961 DeleteHookTable = NULL;
967 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
969 FixedOutputHook *newfcn;
971 newfcn = (FixedOutputHook *)
972 malloc(sizeof(FixedOutputHook));
973 newfcn->next = FixedOutputTable;
974 newfcn->h_function_pointer = handler;
975 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
976 FixedOutputTable = newfcn;
978 MOD_syslog(LOG_DEBUG, "Registered a new fixed output function for %s\n", newfcn->content_type);
982 void CtdlUnregisterFixedOutputHook(char *content_type)
984 FixedOutputHook *cur, *p, *last;
987 cur = FixedOutputTable;
988 while (cur != NULL) {
989 /* This will also remove duplicates if any */
990 if (!strcasecmp(content_type, cur->content_type)) {
991 MOD_syslog(LOG_DEBUG,
992 "Unregistered fixed output function for %s\n",
1001 FixedOutputTable = p;
1013 void CtdlDestroyFixedOutputHooks(void)
1015 FixedOutputHook *cur, *p;
1017 cur = FixedOutputTable;
1020 MOD_syslog(LOG_DEBUG, "Destroyed fixed output function for %s\n", cur->content_type);
1026 FixedOutputTable = NULL;
1029 /* returns nonzero if we found a hook and used it */
1030 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
1032 FixedOutputHook *fcn;
1034 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
1035 if (!strcasecmp(content_type, fcn->content_type)) {
1036 (*fcn->h_function_pointer) (content, content_length);
1047 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1050 XmsgFunctionHook *newfcn;
1052 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
1053 newfcn->next = XmsgHookTable;
1054 newfcn->order = order;
1055 newfcn->h_function_pointer = fcn_ptr;
1056 XmsgHookTable = newfcn;
1057 MOD_syslog(LOG_DEBUG, "Registered a new x-msg function (priority %d)\n", order);
1061 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1063 XmsgFunctionHook *cur, *p, *last;
1066 cur = XmsgHookTable;
1067 while (cur != NULL) {
1068 /* This will also remove duplicates if any */
1069 if (fcn_ptr == cur->h_function_pointer &&
1070 order == cur->order) {
1071 MOD_syslog(LOG_DEBUG, "Unregistered x-msg function "
1072 "(priority %d)\n", order);
1090 void CtdlDestroyXmsgHooks(void)
1092 XmsgFunctionHook *cur, *p;
1094 cur = XmsgHookTable;
1097 MOD_syslog(LOG_DEBUG, "Destroyed x-msg function "
1098 "(priority %d)\n", cur->order);
1104 XmsgHookTable = NULL;
1108 void CtdlRegisterServiceHook(int tcp_port,
1110 void (*h_greeting_function) (void),
1111 void (*h_command_function) (void),
1112 void (*h_async_function) (void),
1113 const char *ServiceName)
1115 ServiceFunctionHook *newfcn;
1120 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1121 message = (char*) malloc (SIZ + SIZ);
1123 newfcn->next = ServiceHookTable;
1124 newfcn->tcp_port = tcp_port;
1125 newfcn->sockpath = sockpath;
1126 newfcn->h_greeting_function = h_greeting_function;
1127 newfcn->h_command_function = h_command_function;
1128 newfcn->h_async_function = h_async_function;
1129 newfcn->ServiceName = ServiceName;
1131 if (sockpath != NULL) {
1132 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
1133 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1135 else if (tcp_port <= 0) { /* port -1 to disable */
1136 MOD_syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1142 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
1144 config.c_maxsessions,
1146 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
1147 config.c_ip_addr, tcp_port, ServiceName);
1150 if (newfcn->msock > 0) {
1151 ServiceHookTable = newfcn;
1152 strcat(message, "registered.");
1153 MOD_syslog(LOG_INFO, "%s\n", message);
1156 AddPortError(message, error);
1157 strcat(message, "FAILED.");
1158 MOD_syslog(LOG_CRIT, "%s\n", message);
1165 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1166 void (*h_greeting_function) (void),
1167 void (*h_command_function) (void),
1168 void (*h_async_function) (void)
1171 ServiceFunctionHook *cur, *p, *last;
1174 cur = ServiceHookTable;
1175 while (cur != NULL) {
1176 /* This will also remove duplicates if any */
1177 if (h_greeting_function == cur->h_greeting_function &&
1178 h_command_function == cur->h_command_function &&
1179 h_async_function == cur->h_async_function &&
1180 tcp_port == cur->tcp_port &&
1181 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
1186 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1189 } else if (tcp_port) {
1190 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1192 MOD_syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1199 ServiceHookTable = p;
1210 void CtdlShutdownServiceHooks(void)
1212 /* sort of a duplicate of close_masters() but called earlier */
1213 ServiceFunctionHook *cur;
1215 cur = ServiceHookTable;
1218 if (cur->msock != -1)
1222 if (cur->sockpath != NULL){
1223 MOD_syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1226 unlink(cur->sockpath);
1228 MOD_syslog(LOG_INFO, "[%s] closing service\n",
1236 void CtdlDestroyServiceHook(void)
1238 ServiceFunctionHook *cur, *p;
1240 cur = ServiceHookTable;
1244 if (cur->sockpath) {
1245 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1247 unlink(cur->sockpath);
1248 } else if (cur->tcp_port) {
1249 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", cur->tcp_port);
1251 MOD_syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1257 ServiceHookTable = NULL;
1260 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1262 SearchFunctionHook *newfcn;
1264 if (!name || !fcn_ptr) {
1268 newfcn = (SearchFunctionHook *)
1269 malloc(sizeof(SearchFunctionHook));
1270 newfcn->next = SearchFunctionHookTable;
1271 newfcn->name = name;
1272 newfcn->fcn_ptr = fcn_ptr;
1273 SearchFunctionHookTable = newfcn;
1275 MOD_syslog(LOG_DEBUG, "Registered a new search function (%s)\n", name);
1278 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1280 SearchFunctionHook *cur, *p, *last;
1283 cur = SearchFunctionHookTable;
1284 while (cur != NULL) {
1286 (cur->fcn_ptr == fcn_ptr) &&
1287 name && !strcmp(name, cur->name))
1289 MOD_syslog(LOG_DEBUG, "Unregistered search function(%s)\n", name);
1295 SearchFunctionHookTable = p;
1305 void CtdlDestroySearchHooks(void)
1307 SearchFunctionHook *cur, *p;
1309 cur = SearchFunctionHookTable;
1310 SearchFunctionHookTable = NULL;
1311 while (cur != NULL) {
1318 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1320 SearchFunctionHook *fcn = NULL;
1322 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1323 if (!func_name || !strcmp(func_name, fcn->name)) {
1324 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1332 void PerformSessionHooks(int EventType)
1334 SessionFunctionHook *fcn = NULL;
1336 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1337 if (fcn->eventtype == EventType) {
1338 if (EventType == EVT_TIMER) {
1339 pthread_setspecific(MyConKey, NULL); /* for every hook */
1341 (*fcn->h_function_pointer) ();
1346 void PerformUserHooks(ctdluser *usbuf, int EventType)
1348 UserFunctionHook *fcn = NULL;
1350 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1351 if (fcn->eventtype == EventType) {
1352 (*fcn->h_function_pointer) (usbuf);
1357 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
1359 MessageFunctionHook *fcn = NULL;
1360 int total_retval = 0;
1362 /* Other code may elect to protect this message from server-side
1363 * handlers; if this is the case, don't do anything.
1364 MOD_syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1366 if (msg->cm_flags & CM_SKIP_HOOKS) {
1367 MODM_syslog(LOG_DEBUG, "Skipping hooks\n");
1371 /* Otherwise, run all the hooks appropriate to this event type.
1373 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1374 if (fcn->eventtype == EventType) {
1375 total_retval = total_retval + (*fcn->h_function_pointer) (msg);
1379 /* Return the sum of the return codes from the hook functions. If
1380 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1381 * the save operation to abort.
1383 return total_retval;
1387 int PerformRoomHooks(struct ctdlroom *target_room)
1389 RoomFunctionHook *fcn;
1390 int total_retval = 0;
1392 MOD_syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1394 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1395 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1398 /* Return the sum of the return codes from the hook functions.
1400 return total_retval;
1404 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1406 NetprocFunctionHook *fcn;
1407 int total_retval = 0;
1409 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1410 total_retval = total_retval +
1411 (*fcn->h_function_pointer) (msg, target_room);
1414 /* Return the sum of the return codes from the hook functions.
1415 * A nonzero return code will cause the message to *not* be imported.
1417 return total_retval;
1421 void PerformDeleteHooks(char *room, long msgnum)
1423 DeleteFunctionHook *fcn;
1425 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1426 (*fcn->h_function_pointer) (room, msgnum);
1434 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1436 XmsgFunctionHook *fcn;
1440 for (p=0; p<MAX_XMSG_PRI; ++p) {
1441 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1442 if (fcn->order == p) {
1444 (*fcn->h_function_pointer)
1445 (sender, sender_email, recp, msg);
1448 /* Break out of the loop if a higher-priority function
1449 * successfully delivered the message. This prevents duplicate
1450 * deliveries to local users simultaneously signed onto
1453 if (total_sent) break;
1460 * Dirty hack until we impliment a hook mechanism for this
1462 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1465 CtdlStartTLS (ok_response, nosup_response, error_response);
1469 void DebugModulesEnable(const int n)
1473 CTDL_MODULE_INIT(modules)
1476 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);
1478 CtdlRegisterProtoHook(cmd_log_get, "LOGP", "Print Log-parameters");
1479 CtdlRegisterProtoHook(cmd_log_set, "LOGS", "Set Log-parameters");