2 * Citadel Extension Loader
3 * Written by Brian Costello <btx@calyx.net>
5 * Copyright (c) 1987-2017 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 <libcitadel.h>
21 #include "sysdep_decls.h"
22 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
23 #include "serv_extensions.h"
24 #include "ctdl_module.h"
29 int EnableMarkers = 0;
30 int EnableCtlProto = 0;
33 * Structure defentitions for hook tables
36 HashList *LogDebugEntryTable = NULL;
38 typedef struct LogFunctionHook LogFunctionHook;
39 struct LogFunctionHook {
40 LogFunctionHook *next;
42 void (*h_function_pointer) (char *);
45 LogFunctionHook *LogHookTable = NULL;
47 typedef struct FixedOutputHook FixedOutputHook;
48 struct FixedOutputHook {
49 FixedOutputHook *next;
50 char content_type[64];
51 void (*h_function_pointer) (char *, int);
53 FixedOutputHook *FixedOutputTable = NULL;
61 * SessionFunctionHook extensions are used for any type of hook for which
62 * the context in which it's being called (which is determined by the event
63 * type) will make it obvious for the hook function to know where to look for
66 typedef struct SessionFunctionHook SessionFunctionHook;
67 struct SessionFunctionHook {
68 SessionFunctionHook *next;
70 void (*h_function_pointer) (void);
73 SessionFunctionHook *SessionHookTable = NULL;
76 * UserFunctionHook extensions are used for any type of hook which implements
77 * an operation on a user or username (potentially) other than the one
78 * operating the current session.
80 typedef struct UserFunctionHook UserFunctionHook;
81 struct UserFunctionHook {
82 UserFunctionHook *next;
83 void (*h_function_pointer) (struct ctdluser *usbuf);
86 UserFunctionHook *UserHookTable = NULL;
89 * MessageFunctionHook extensions are used for hooks which implement handlers
90 * for various types of message operations (save, read, etc.)
92 typedef struct MessageFunctionHook MessageFunctionHook;
93 struct MessageFunctionHook {
94 MessageFunctionHook *next;
95 int (*h_function_pointer) (struct CtdlMessage *msg, recptypes *recps);
98 MessageFunctionHook *MessageHookTable = NULL;
102 * NetprocFunctionHook extensions are used for hooks which implement handlers
103 * for incoming network messages.
105 typedef struct NetprocFunctionHook NetprocFunctionHook;
106 struct NetprocFunctionHook {
107 NetprocFunctionHook *next;
108 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
110 NetprocFunctionHook *NetprocHookTable = NULL;
114 * DeleteFunctionHook extensions are used for hooks which get called when a
115 * message is about to be deleted.
117 typedef struct DeleteFunctionHook DeleteFunctionHook;
118 struct DeleteFunctionHook {
119 DeleteFunctionHook *next;
120 void (*h_function_pointer) (char *target_room, long msgnum);
122 DeleteFunctionHook *DeleteHookTable = NULL;
126 * ExpressMessageFunctionHook extensions are used for hooks which implement
127 * the sending of an instant message through various channels. Any function
128 * registered should return the number of recipients to whom the message was
129 * successfully transmitted.
131 typedef struct XmsgFunctionHook XmsgFunctionHook;
132 struct XmsgFunctionHook {
133 XmsgFunctionHook *next;
134 int (*h_function_pointer) (char *, char *, char *, char *);
137 XmsgFunctionHook *XmsgHookTable = NULL;
143 * RoomFunctionHook extensions are used for hooks which impliment room
144 * processing functions when new messages are added EG. SIEVE.
146 typedef struct RoomFunctionHook RoomFunctionHook;
147 struct RoomFunctionHook {
148 RoomFunctionHook *next;
149 int (*fcn_ptr) (struct ctdlroom *);
151 RoomFunctionHook *RoomHookTable = NULL;
155 typedef struct SearchFunctionHook SearchFunctionHook;
156 struct SearchFunctionHook {
157 SearchFunctionHook *next;
158 void (*fcn_ptr) (int *, long **, const char *);
161 SearchFunctionHook *SearchFunctionHookTable = NULL;
163 CleanupFunctionHook *CleanupHookTable = NULL;
164 CleanupFunctionHook *EVCleanupHookTable = NULL;
166 ServiceFunctionHook *ServiceHookTable = NULL;
168 typedef struct ProtoFunctionHook ProtoFunctionHook;
169 struct ProtoFunctionHook {
170 void (*handler) (char *cmdbuf);
175 HashList *ProtoHookList = NULL;
178 #define ERR_PORT (1 << 1)
181 static StrBuf *portlist = NULL;
183 static StrBuf *errormessages = NULL;
186 long DetailErrorFlags;
187 ConstStr Empty = {HKEY("")};
188 char *ErrSubject = "Startup Problems";
189 ConstStr ErrGeneral[] = {
190 {HKEY("Citadel had trouble on starting up. ")},
191 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
192 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
193 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
194 "The errors returned by the system were:\n")},
195 {HKEY("You can recheck the above if you follow this faq item:\n"
196 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
199 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
200 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
201 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
202 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
203 " and stop the program that binds these ports.\n You should eventually remove "
204 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
205 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
208 void LogPrintMessages(long err)
211 StrBuf *List, *DetailList;
212 ConstStr *Short, *Where, *Hint;
215 Message = NewStrBufPlain(NULL,
216 StrLength(portlist) + StrLength(errormessages));
218 DetailErrorFlags = DetailErrorFlags & ~err;
223 Short = &ErrPortShort;
224 Where = &ErrPortWhere;
227 DetailList = errormessages;
237 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
238 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
239 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
240 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
241 StrBufAppendBuf(Message, List, 0);
242 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
243 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
244 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
245 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
246 StrBufAppendBuf(Message, DetailList, 0);
247 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
248 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
250 syslog(LOG_EMERG, "extensions: %s", ChrPtr(Message));
251 syslog(LOG_EMERG, "extensions: %s", ErrSubject);
252 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
254 FreeStrBuf(&Message);
256 FreeStrBuf(&DetailList);
260 void AddPortError(char *Port, char *ErrorMessage)
264 DetailErrorFlags |= ERR_PORT;
266 len = StrLength(errormessages);
267 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
268 else errormessages = NewStrBuf();
269 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
272 len = StrLength(portlist);
273 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
274 else portlist = NewStrBuf();
275 StrBufAppendBufPlain(portlist, Port, -1, 0);
279 int DLoader_Exec_Cmd(char *cmdbuf)
282 ProtoFunctionHook *p;
284 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
285 p = (ProtoFunctionHook*) vP;
286 p->handler(&cmdbuf[5]);
292 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
295 if (LogDebugEntryTable == NULL)
296 LogDebugEntryTable = NewHash(1, NULL);
297 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
302 Put(LogDebugEntryTable, Name, Len, E, NULL);
305 void CtdlSetDebugLogFacilities(const char **Str, long n)
307 StrBuf *Token = NULL;
314 for (i=0; i < n; i++){
315 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
316 if (strcmp(Str[i], "all") == 0) {
320 Buf = NewStrBufPlain(Str[i], -1);
323 Token = NewStrBufPlain(NULL, StrLength(Buf));
324 while ((ch != StrBufNOTNULL) &&
325 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
326 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
329 LogDebugEntry *E = (LogDebugEntry*)vptr;
342 Pos = GetNewHashPos(LogDebugEntryTable, 0);
343 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
344 LogDebugEntry *E = (LogDebugEntry*)vptr;
351 void CtdlDestroyDebugTable(void)
354 DeleteHash(&LogDebugEntryTable);
357 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
359 ProtoFunctionHook *p;
361 if (ProtoHookList == NULL)
362 ProtoHookList = NewHash (1, FourHash);
365 p = (ProtoFunctionHook *)
366 malloc(sizeof(ProtoFunctionHook));
369 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
372 p->handler = handler;
376 Put(ProtoHookList, cmd, 4, p, NULL);
377 syslog(LOG_DEBUG, "extensions: registered server command %s (%s)", cmd, desc);
380 void CtdlDestroyProtoHooks(void)
383 DeleteHash(&ProtoHookList);
387 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
390 CleanupFunctionHook *newfcn;
392 newfcn = (CleanupFunctionHook *)
393 malloc(sizeof(CleanupFunctionHook));
394 newfcn->next = CleanupHookTable;
395 newfcn->h_function_pointer = fcn_ptr;
396 CleanupHookTable = newfcn;
398 syslog(LOG_DEBUG, "extensions: registered a new cleanup function");
402 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
404 CleanupFunctionHook *cur, *p, *last;
406 cur = CleanupHookTable;
409 if (fcn_ptr == cur->h_function_pointer)
411 syslog(LOG_DEBUG, "extensions: unregistered cleanup function");
420 CleanupHookTable = p;
431 void CtdlDestroyCleanupHooks(void)
433 CleanupFunctionHook *cur, *p;
435 cur = CleanupHookTable;
438 syslog(LOG_DEBUG, "extensions: destroyed cleanup function");
443 CleanupHookTable = NULL;
446 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
449 CleanupFunctionHook *newfcn;
451 newfcn = (CleanupFunctionHook *)
452 malloc(sizeof(CleanupFunctionHook));
453 newfcn->next = EVCleanupHookTable;
454 newfcn->h_function_pointer = fcn_ptr;
455 EVCleanupHookTable = newfcn;
457 syslog(LOG_DEBUG, "extensions: registered a new cleanup function");
461 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
463 CleanupFunctionHook *cur, *p, *last;
465 cur = EVCleanupHookTable;
468 if (fcn_ptr == cur->h_function_pointer)
470 syslog(LOG_DEBUG, "extensions: unregistered cleanup function");
479 EVCleanupHookTable = p;
490 void CtdlDestroyEVCleanupHooks(void)
492 CleanupFunctionHook *cur, *p;
494 cur = EVCleanupHookTable;
497 syslog(LOG_DEBUG, "extensions: destroyed cleanup function");
499 cur->h_function_pointer();
503 EVCleanupHookTable = NULL;
509 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
511 SessionFunctionHook *newfcn;
513 newfcn = (SessionFunctionHook *)
514 malloc(sizeof(SessionFunctionHook));
515 newfcn->Priority = Priority;
516 newfcn->h_function_pointer = fcn_ptr;
517 newfcn->eventtype = EventType;
519 SessionFunctionHook **pfcn;
520 pfcn = &SessionHookTable;
521 while ((*pfcn != NULL) &&
522 ((*pfcn)->Priority < newfcn->Priority) &&
523 ((*pfcn)->next != NULL))
524 pfcn = &(*pfcn)->next;
526 newfcn->next = *pfcn;
529 syslog(LOG_DEBUG, "extensions: registered a new session function (type %d Priority %d)", EventType, Priority);
533 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
535 SessionFunctionHook *cur, *p, *last;
537 cur = SessionHookTable;
538 while (cur != NULL) {
539 if ((fcn_ptr == cur->h_function_pointer) &&
540 (EventType == cur->eventtype))
542 syslog(LOG_DEBUG, "extensions: unregistered session function (type %d)", EventType);
551 SessionHookTable = p;
561 void CtdlDestroySessionHooks(void)
563 SessionFunctionHook *cur, *p;
565 cur = SessionHookTable;
568 syslog(LOG_DEBUG, "extensions: destroyed session function");
573 SessionHookTable = NULL;
577 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
580 UserFunctionHook *newfcn;
582 newfcn = (UserFunctionHook *)
583 malloc(sizeof(UserFunctionHook));
584 newfcn->next = UserHookTable;
585 newfcn->h_function_pointer = fcn_ptr;
586 newfcn->eventtype = EventType;
587 UserHookTable = newfcn;
589 syslog(LOG_DEBUG, "extensions: registered a new user function (type %d)",
594 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
596 UserFunctionHook *cur, *p, *last;
599 while (cur != NULL) {
600 if ((fcn_ptr == cur->h_function_pointer) &&
601 (EventType == cur->eventtype))
603 syslog(LOG_DEBUG, "extensions: unregistered user function (type %d)", EventType);
622 void CtdlDestroyUserHooks(void)
624 UserFunctionHook *cur, *p;
629 syslog(LOG_DEBUG, "extensions: destroyed user function");
634 UserHookTable = NULL;
638 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
642 MessageFunctionHook *newfcn;
644 newfcn = (MessageFunctionHook *)
645 malloc(sizeof(MessageFunctionHook));
646 newfcn->next = MessageHookTable;
647 newfcn->h_function_pointer = handler;
648 newfcn->eventtype = EventType;
649 MessageHookTable = newfcn;
651 syslog(LOG_DEBUG, "extensions: registered a new message function (type %d)", EventType);
655 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
658 MessageFunctionHook *cur, *p, *last;
660 cur = MessageHookTable;
661 while (cur != NULL) {
662 if ((handler == cur->h_function_pointer) &&
663 (EventType == cur->eventtype))
665 syslog(LOG_DEBUG, "extensions: unregistered message function (type %d)", EventType);
673 MessageHookTable = p;
683 void CtdlDestroyMessageHook(void)
685 MessageFunctionHook *cur, *p;
687 cur = MessageHookTable;
690 syslog(LOG_DEBUG, "extensions: destroyed message function (type %d)", cur->eventtype);
695 MessageHookTable = NULL;
699 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
701 RoomFunctionHook *newfcn;
703 newfcn = (RoomFunctionHook *)
704 malloc(sizeof(RoomFunctionHook));
705 newfcn->next = RoomHookTable;
706 newfcn->fcn_ptr = fcn_ptr;
707 RoomHookTable = newfcn;
709 syslog(LOG_DEBUG, "extensions: registered a new room function");
713 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
715 RoomFunctionHook *cur, *p, *last;
720 if (fcn_ptr == cur->fcn_ptr) {
721 syslog(LOG_DEBUG, "extensions: unregistered room function");
741 void CtdlDestroyRoomHooks(void)
743 RoomFunctionHook *cur, *p;
748 syslog(LOG_DEBUG, "extensions: destroyed room function");
753 RoomHookTable = NULL;
756 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
758 NetprocFunctionHook *newfcn;
760 newfcn = (NetprocFunctionHook *)
761 malloc(sizeof(NetprocFunctionHook));
762 newfcn->next = NetprocHookTable;
763 newfcn->h_function_pointer = handler;
764 NetprocHookTable = newfcn;
766 syslog(LOG_DEBUG, "extensions: registered a new netproc function");
770 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
772 NetprocFunctionHook *cur, *p, *last;
774 cur = NetprocHookTable;
777 while (cur != NULL) {
778 if (handler == cur->h_function_pointer)
780 syslog(LOG_DEBUG, "extensions: unregistered netproc function");
787 NetprocHookTable = p;
798 void CtdlDestroyNetprocHooks(void)
800 NetprocFunctionHook *cur, *p;
802 cur = NetprocHookTable;
805 syslog(LOG_DEBUG, "extensions: destroyed netproc function");
810 NetprocHookTable = NULL;
814 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
816 DeleteFunctionHook *newfcn;
818 newfcn = (DeleteFunctionHook *)
819 malloc(sizeof(DeleteFunctionHook));
820 newfcn->next = DeleteHookTable;
821 newfcn->h_function_pointer = handler;
822 DeleteHookTable = newfcn;
824 syslog(LOG_DEBUG, "extensions: registered a new delete function");
828 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
830 DeleteFunctionHook *cur, *p, *last;
833 cur = DeleteHookTable;
834 while (cur != NULL) {
835 if (handler == cur->h_function_pointer )
837 syslog(LOG_DEBUG, "extensions: unregistered delete function");
856 void CtdlDestroyDeleteHooks(void)
858 DeleteFunctionHook *cur, *p;
860 cur = DeleteHookTable;
863 syslog(LOG_DEBUG, "extensions: destroyed delete function");
868 DeleteHookTable = NULL;
872 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
874 FixedOutputHook *newfcn;
876 newfcn = (FixedOutputHook *)
877 malloc(sizeof(FixedOutputHook));
878 newfcn->next = FixedOutputTable;
879 newfcn->h_function_pointer = handler;
880 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
881 FixedOutputTable = newfcn;
883 syslog(LOG_DEBUG, "extensions: registered a new fixed output function for %s", newfcn->content_type);
887 void CtdlUnregisterFixedOutputHook(char *content_type)
889 FixedOutputHook *cur, *p, *last;
892 cur = FixedOutputTable;
893 while (cur != NULL) {
894 /* This will also remove duplicates if any */
895 if (!strcasecmp(content_type, cur->content_type)) {
896 syslog(LOG_DEBUG, "extensions: unregistered fixed output function for %s", content_type);
903 FixedOutputTable = p;
915 void CtdlDestroyFixedOutputHooks(void)
917 FixedOutputHook *cur, *p;
919 cur = FixedOutputTable;
922 syslog(LOG_DEBUG, "extensions: destroyed fixed output function for %s", cur->content_type);
928 FixedOutputTable = NULL;
931 /* returns nonzero if we found a hook and used it */
932 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
934 FixedOutputHook *fcn;
936 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
937 if (!strcasecmp(content_type, fcn->content_type)) {
938 (*fcn->h_function_pointer) (content, content_length);
946 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
949 XmsgFunctionHook *newfcn;
951 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
952 newfcn->next = XmsgHookTable;
953 newfcn->order = order;
954 newfcn->h_function_pointer = fcn_ptr;
955 XmsgHookTable = newfcn;
956 syslog(LOG_DEBUG, "extensions: registered a new x-msg function (priority %d)", order);
960 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
962 XmsgFunctionHook *cur, *p, *last;
966 while (cur != NULL) {
967 /* This will also remove duplicates if any */
968 if (fcn_ptr == cur->h_function_pointer &&
969 order == cur->order) {
970 syslog(LOG_DEBUG, "extensions: unregistered x-msg function (priority %d)", order);
990 void CtdlDestroyXmsgHooks(void)
992 XmsgFunctionHook *cur, *p;
997 syslog(LOG_DEBUG, "extensions: destroyed x-msg function (priority %d)", cur->order);
1002 XmsgHookTable = NULL;
1006 void CtdlRegisterServiceHook(int tcp_port,
1008 void (*h_greeting_function) (void),
1009 void (*h_command_function) (void),
1010 void (*h_async_function) (void),
1011 const char *ServiceName)
1013 ServiceFunctionHook *newfcn;
1018 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1019 message = (char*) malloc (SIZ + SIZ);
1021 newfcn->next = ServiceHookTable;
1022 newfcn->tcp_port = tcp_port;
1023 newfcn->sockpath = sockpath;
1024 newfcn->h_greeting_function = h_greeting_function;
1025 newfcn->h_command_function = h_command_function;
1026 newfcn->h_async_function = h_async_function;
1027 newfcn->ServiceName = ServiceName;
1029 if (sockpath != NULL) {
1030 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"), error);
1031 snprintf(message, SIZ, "extensions: unix domain socket '%s': ", sockpath);
1033 else if (tcp_port <= 0) { /* port -1 to disable */
1034 syslog(LOG_INFO, "extensions: service %s has been manually disabled, skipping", ServiceName);
1040 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"),
1042 CtdlGetConfigInt("c_maxsessions"),
1044 snprintf(message, SIZ, "extensions: TCP port %s:%d: (%s) ",
1045 CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
1048 if (newfcn->msock > 0) {
1049 ServiceHookTable = newfcn;
1050 strcat(message, "registered.");
1051 syslog(LOG_INFO, "%s", message);
1054 AddPortError(message, error);
1055 strcat(message, "FAILED.");
1056 syslog(LOG_ERR, "%s", message);
1063 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1064 void (*h_greeting_function) (void),
1065 void (*h_command_function) (void),
1066 void (*h_async_function) (void)
1069 ServiceFunctionHook *cur, *p, *last;
1072 cur = ServiceHookTable;
1073 while (cur != NULL) {
1074 /* This will also remove duplicates if any */
1075 if (h_greeting_function == cur->h_greeting_function &&
1076 h_command_function == cur->h_command_function &&
1077 h_async_function == cur->h_async_function &&
1078 tcp_port == cur->tcp_port &&
1079 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
1084 syslog(LOG_INFO, "extensions: closed UNIX domain socket %s", sockpath);
1086 } else if (tcp_port) {
1087 syslog(LOG_INFO, "extensions: closed TCP port %d", tcp_port);
1089 syslog(LOG_INFO, "extensions: unregistered service \"%s\"", cur->ServiceName);
1096 ServiceHookTable = p;
1107 void CtdlShutdownServiceHooks(void)
1109 /* sort of a duplicate of close_masters() but called earlier */
1110 ServiceFunctionHook *cur;
1112 cur = ServiceHookTable;
1115 if (cur->msock != -1)
1119 if (cur->sockpath != NULL){
1120 syslog(LOG_INFO, "extensions: [%s] Closed UNIX domain socket %s", cur->ServiceName, cur->sockpath);
1121 unlink(cur->sockpath);
1123 syslog(LOG_INFO, "extensions: [%s] closing service", cur->ServiceName);
1131 void CtdlDestroyServiceHook(void)
1134 ServiceFunctionHook *cur, *p;
1136 cur = ServiceHookTable;
1139 if (cur->msock != -1)
1146 Text = " Not closing again";
1149 if (cur->sockpath) {
1150 syslog(LOG_INFO, "extensions: %s UNIX domain socket %s", Text, cur->sockpath);
1151 unlink(cur->sockpath);
1152 } else if (cur->tcp_port) {
1153 syslog(LOG_INFO, "extensions: %s TCP port %d", Text, cur->tcp_port);
1155 syslog(LOG_INFO, "extensions: destroyed service \"%s\"", cur->ServiceName);
1161 ServiceHookTable = NULL;
1164 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1166 SearchFunctionHook *newfcn;
1168 if (!name || !fcn_ptr) {
1172 newfcn = (SearchFunctionHook *)
1173 malloc(sizeof(SearchFunctionHook));
1174 newfcn->next = SearchFunctionHookTable;
1175 newfcn->name = name;
1176 newfcn->fcn_ptr = fcn_ptr;
1177 SearchFunctionHookTable = newfcn;
1179 syslog(LOG_DEBUG, "extensions: registered a new search function (%s)", name);
1182 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1184 SearchFunctionHook *cur, *p, *last;
1187 cur = SearchFunctionHookTable;
1188 while (cur != NULL) {
1190 (cur->fcn_ptr == fcn_ptr) &&
1191 name && !strcmp(name, cur->name))
1193 syslog(LOG_DEBUG, "extensions: unregistered search function(%s)", name);
1199 SearchFunctionHookTable = p;
1209 void CtdlDestroySearchHooks(void)
1211 SearchFunctionHook *cur, *p;
1213 cur = SearchFunctionHookTable;
1214 SearchFunctionHookTable = NULL;
1215 while (cur != NULL) {
1222 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1224 SearchFunctionHook *fcn = NULL;
1226 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1227 if (!func_name || !strcmp(func_name, fcn->name)) {
1228 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1236 void PerformSessionHooks(int EventType)
1238 SessionFunctionHook *fcn = NULL;
1240 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1241 if (fcn->eventtype == EventType) {
1242 if (EventType == EVT_TIMER) {
1243 pthread_setspecific(MyConKey, NULL); /* for every hook */
1245 (*fcn->h_function_pointer) ();
1250 void PerformUserHooks(ctdluser *usbuf, int EventType)
1252 UserFunctionHook *fcn = NULL;
1254 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1255 if (fcn->eventtype == EventType) {
1256 (*fcn->h_function_pointer) (usbuf);
1261 int PerformMessageHooks(struct CtdlMessage *msg, recptypes *recps, int EventType)
1263 MessageFunctionHook *fcn = NULL;
1264 int total_retval = 0;
1266 /* Other code may elect to protect this message from server-side
1267 * handlers; if this is the case, don't do anything.
1269 if (msg->cm_flags & CM_SKIP_HOOKS) {
1273 /* Otherwise, run all the hooks appropriate to this event type.
1275 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1276 if (fcn->eventtype == EventType) {
1277 total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
1281 /* Return the sum of the return codes from the hook functions. If
1282 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1283 * the save operation to abort.
1285 return total_retval;
1289 int PerformRoomHooks(struct ctdlroom *target_room)
1291 RoomFunctionHook *fcn;
1292 int total_retval = 0;
1294 syslog(LOG_DEBUG, "extensions: performing room hooks for <%s>", target_room->QRname);
1296 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1297 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1300 /* Return the sum of the return codes from the hook functions.
1302 return total_retval;
1306 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1308 NetprocFunctionHook *fcn;
1309 int total_retval = 0;
1311 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1312 total_retval = total_retval +
1313 (*fcn->h_function_pointer) (msg, target_room);
1316 /* Return the sum of the return codes from the hook functions.
1317 * A nonzero return code will cause the message to *not* be imported.
1319 return total_retval;
1323 void PerformDeleteHooks(char *room, long msgnum)
1325 DeleteFunctionHook *fcn;
1327 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1328 (*fcn->h_function_pointer) (room, msgnum);
1336 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1338 XmsgFunctionHook *fcn;
1342 for (p=0; p<MAX_XMSG_PRI; ++p) {
1343 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1344 if (fcn->order == p) {
1346 (*fcn->h_function_pointer)
1347 (sender, sender_email, recp, msg);
1350 /* Break out of the loop if a higher-priority function
1351 * successfully delivered the message. This prevents duplicate
1352 * deliveries to local users simultaneously signed onto
1355 if (total_sent) break;
1362 * Dirty hack until we impliment a hook mechanism for this
1364 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1367 CtdlStartTLS (ok_response, nosup_response, error_response);
1371 void DebugModulesEnable(const int n)
1375 void MarkersEnable(const int n)
1379 void DebugCitadelProtoEnable(const int n)
1384 CTDL_MODULE_INIT(modules)
1387 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);
1388 CtdlRegisterDebugFlagHook(HKEY("periodicmarkers"), MarkersEnable, &EnableMarkers);
1389 CtdlRegisterDebugFlagHook(HKEY("citadelprotocol"), DebugCitadelProtoEnable, &EnableCtlProto);