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.
17 #include <libcitadel.h>
19 #include "sysdep_decls.h"
20 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
22 #include "serv_extensions.h"
23 #include "ctdl_module.h"
28 int EnableMarkers = 0;
29 int EnableCtlProto = 0;
32 * Structure defentitions for hook tables
35 HashList *LogDebugEntryTable = NULL;
37 typedef struct LogFunctionHook LogFunctionHook;
38 struct LogFunctionHook {
39 LogFunctionHook *next;
41 void (*h_function_pointer) (char *);
44 LogFunctionHook *LogHookTable = NULL;
46 typedef struct FixedOutputHook FixedOutputHook;
47 struct FixedOutputHook {
48 FixedOutputHook *next;
49 char content_type[64];
50 void (*h_function_pointer) (char *, int);
52 FixedOutputHook *FixedOutputTable = NULL;
60 * SessionFunctionHook extensions are used for any type of hook for which
61 * the context in which it's being called (which is determined by the event
62 * type) will make it obvious for the hook function to know where to look for
65 typedef struct SessionFunctionHook SessionFunctionHook;
66 struct SessionFunctionHook {
67 SessionFunctionHook *next;
69 void (*h_function_pointer) (void);
72 SessionFunctionHook *SessionHookTable = NULL;
75 * UserFunctionHook extensions are used for any type of hook which implements
76 * an operation on a user or username (potentially) other than the one
77 * operating the current session.
79 typedef struct UserFunctionHook UserFunctionHook;
80 struct UserFunctionHook {
81 UserFunctionHook *next;
82 void (*h_function_pointer) (struct ctdluser *usbuf);
85 UserFunctionHook *UserHookTable = NULL;
88 * MessageFunctionHook extensions are used for hooks which implement handlers
89 * for various types of message operations (save, read, etc.)
91 typedef struct MessageFunctionHook MessageFunctionHook;
92 struct MessageFunctionHook {
93 MessageFunctionHook *next;
94 int (*h_function_pointer) (struct CtdlMessage *msg, recptypes *recps);
97 MessageFunctionHook *MessageHookTable = NULL;
101 * NetprocFunctionHook extensions are used for hooks which implement handlers
102 * for incoming network messages.
104 typedef struct NetprocFunctionHook NetprocFunctionHook;
105 struct NetprocFunctionHook {
106 NetprocFunctionHook *next;
107 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
109 NetprocFunctionHook *NetprocHookTable = NULL;
113 * DeleteFunctionHook extensions are used for hooks which get called when a
114 * message is about to be deleted.
116 typedef struct DeleteFunctionHook DeleteFunctionHook;
117 struct DeleteFunctionHook {
118 DeleteFunctionHook *next;
119 void (*h_function_pointer) (char *target_room, long msgnum);
121 DeleteFunctionHook *DeleteHookTable = NULL;
125 * ExpressMessageFunctionHook extensions are used for hooks which implement
126 * the sending of an instant message through various channels. Any function
127 * registered should return the number of recipients to whom the message was
128 * successfully transmitted.
130 typedef struct XmsgFunctionHook XmsgFunctionHook;
131 struct XmsgFunctionHook {
132 XmsgFunctionHook *next;
133 int (*h_function_pointer) (char *, char *, char *, char *);
136 XmsgFunctionHook *XmsgHookTable = NULL;
142 * RoomFunctionHook extensions are used for hooks which impliment room
143 * processing functions when new messages are added EG. SIEVE.
145 typedef struct RoomFunctionHook RoomFunctionHook;
146 struct RoomFunctionHook {
147 RoomFunctionHook *next;
148 int (*fcn_ptr) (struct ctdlroom *);
150 RoomFunctionHook *RoomHookTable = NULL;
154 typedef struct SearchFunctionHook SearchFunctionHook;
155 struct SearchFunctionHook {
156 SearchFunctionHook *next;
157 void (*fcn_ptr) (int *, long **, const char *);
160 SearchFunctionHook *SearchFunctionHookTable = NULL;
162 CleanupFunctionHook *CleanupHookTable = NULL;
163 CleanupFunctionHook *EVCleanupHookTable = NULL;
165 ServiceFunctionHook *ServiceHookTable = NULL;
167 typedef struct ProtoFunctionHook ProtoFunctionHook;
168 struct ProtoFunctionHook {
169 void (*handler) (char *cmdbuf);
174 HashList *ProtoHookList = NULL;
177 #define ERR_PORT (1 << 1)
180 static StrBuf *portlist = NULL;
182 static StrBuf *errormessages = NULL;
185 long DetailErrorFlags;
186 ConstStr Empty = {HKEY("")};
187 char *ErrSubject = "Startup Problems";
188 ConstStr ErrGeneral[] = {
189 {HKEY("Citadel had trouble on starting up. ")},
190 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
191 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
192 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
193 "The errors returned by the system were:\n")},
194 {HKEY("You can recheck the above if you follow this faq item:\n"
195 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
198 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
199 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
200 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
201 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
202 " and stop the program that binds these ports.\n You should eventually remove "
203 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
204 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
207 void LogPrintMessages(long err)
210 StrBuf *List, *DetailList;
211 ConstStr *Short, *Where, *Hint;
214 Message = NewStrBufPlain(NULL,
215 StrLength(portlist) + StrLength(errormessages));
217 DetailErrorFlags = DetailErrorFlags & ~err;
222 Short = &ErrPortShort;
223 Where = &ErrPortWhere;
226 DetailList = errormessages;
236 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
237 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
238 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
239 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
240 StrBufAppendBuf(Message, List, 0);
241 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
242 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
243 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
244 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
245 StrBufAppendBuf(Message, DetailList, 0);
246 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
247 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
249 syslog(LOG_EMERG, "extensions: %s", ChrPtr(Message));
250 syslog(LOG_EMERG, "extensions: %s", ErrSubject);
251 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
253 FreeStrBuf(&Message);
255 FreeStrBuf(&DetailList);
259 void AddPortError(char *Port, char *ErrorMessage)
263 DetailErrorFlags |= ERR_PORT;
265 len = StrLength(errormessages);
266 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
267 else errormessages = NewStrBuf();
268 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
271 len = StrLength(portlist);
272 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
273 else portlist = NewStrBuf();
274 StrBufAppendBufPlain(portlist, Port, -1, 0);
278 int DLoader_Exec_Cmd(char *cmdbuf)
281 ProtoFunctionHook *p;
283 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
284 p = (ProtoFunctionHook*) vP;
285 p->handler(&cmdbuf[5]);
291 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
294 if (LogDebugEntryTable == NULL)
295 LogDebugEntryTable = NewHash(1, NULL);
296 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
301 Put(LogDebugEntryTable, Name, Len, E, NULL);
304 void CtdlSetDebugLogFacilities(const char **Str, long n)
306 StrBuf *Token = NULL;
313 for (i=0; i < n; i++){
314 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
315 if (strcmp(Str[i], "all") == 0) {
319 Buf = NewStrBufPlain(Str[i], -1);
322 Token = NewStrBufPlain(NULL, StrLength(Buf));
323 while ((ch != StrBufNOTNULL) &&
324 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
325 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
328 LogDebugEntry *E = (LogDebugEntry*)vptr;
341 Pos = GetNewHashPos(LogDebugEntryTable, 0);
342 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
343 LogDebugEntry *E = (LogDebugEntry*)vptr;
350 void CtdlDestroyDebugTable(void)
353 DeleteHash(&LogDebugEntryTable);
356 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
358 ProtoFunctionHook *p;
360 if (ProtoHookList == NULL)
361 ProtoHookList = NewHash (1, FourHash);
364 p = (ProtoFunctionHook *)
365 malloc(sizeof(ProtoFunctionHook));
368 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
371 p->handler = handler;
375 Put(ProtoHookList, cmd, 4, p, NULL);
376 syslog(LOG_DEBUG, "extensions: registered server command %s (%s)", cmd, desc);
379 void CtdlDestroyProtoHooks(void)
382 DeleteHash(&ProtoHookList);
386 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
389 CleanupFunctionHook *newfcn;
391 newfcn = (CleanupFunctionHook *)
392 malloc(sizeof(CleanupFunctionHook));
393 newfcn->next = CleanupHookTable;
394 newfcn->h_function_pointer = fcn_ptr;
395 CleanupHookTable = newfcn;
397 syslog(LOG_DEBUG, "extensions: registered a new cleanup function");
401 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
403 CleanupFunctionHook *cur, *p, *last;
405 cur = CleanupHookTable;
408 if (fcn_ptr == cur->h_function_pointer)
410 syslog(LOG_DEBUG, "extensions: unregistered cleanup function");
419 CleanupHookTable = p;
430 void CtdlDestroyCleanupHooks(void)
432 CleanupFunctionHook *cur, *p;
434 cur = CleanupHookTable;
437 syslog(LOG_DEBUG, "extensions: destroyed cleanup function");
442 CleanupHookTable = NULL;
445 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
448 CleanupFunctionHook *newfcn;
450 newfcn = (CleanupFunctionHook *)
451 malloc(sizeof(CleanupFunctionHook));
452 newfcn->next = EVCleanupHookTable;
453 newfcn->h_function_pointer = fcn_ptr;
454 EVCleanupHookTable = newfcn;
456 syslog(LOG_DEBUG, "extensions: registered a new cleanup function");
460 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
462 CleanupFunctionHook *cur, *p, *last;
464 cur = EVCleanupHookTable;
467 if (fcn_ptr == cur->h_function_pointer)
469 syslog(LOG_DEBUG, "extensions: unregistered cleanup function");
478 EVCleanupHookTable = p;
489 void CtdlDestroyEVCleanupHooks(void)
491 CleanupFunctionHook *cur, *p;
493 cur = EVCleanupHookTable;
496 syslog(LOG_DEBUG, "extensions: destroyed cleanup function");
498 cur->h_function_pointer();
502 EVCleanupHookTable = NULL;
508 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
510 SessionFunctionHook *newfcn;
512 newfcn = (SessionFunctionHook *)
513 malloc(sizeof(SessionFunctionHook));
514 newfcn->Priority = Priority;
515 newfcn->h_function_pointer = fcn_ptr;
516 newfcn->eventtype = EventType;
518 SessionFunctionHook **pfcn;
519 pfcn = &SessionHookTable;
520 while ((*pfcn != NULL) &&
521 ((*pfcn)->Priority < newfcn->Priority) &&
522 ((*pfcn)->next != NULL))
523 pfcn = &(*pfcn)->next;
525 newfcn->next = *pfcn;
528 syslog(LOG_DEBUG, "extensions: registered a new session function (type %d Priority %d)", EventType, Priority);
532 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
534 SessionFunctionHook *cur, *p, *last;
536 cur = SessionHookTable;
537 while (cur != NULL) {
538 if ((fcn_ptr == cur->h_function_pointer) &&
539 (EventType == cur->eventtype))
541 syslog(LOG_DEBUG, "extensions: unregistered session function (type %d)", EventType);
550 SessionHookTable = p;
560 void CtdlDestroySessionHooks(void)
562 SessionFunctionHook *cur, *p;
564 cur = SessionHookTable;
567 syslog(LOG_DEBUG, "extensions: destroyed session function");
572 SessionHookTable = NULL;
576 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
579 UserFunctionHook *newfcn;
581 newfcn = (UserFunctionHook *)
582 malloc(sizeof(UserFunctionHook));
583 newfcn->next = UserHookTable;
584 newfcn->h_function_pointer = fcn_ptr;
585 newfcn->eventtype = EventType;
586 UserHookTable = newfcn;
588 syslog(LOG_DEBUG, "extensions: registered a new user function (type %d)",
593 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
595 UserFunctionHook *cur, *p, *last;
598 while (cur != NULL) {
599 if ((fcn_ptr == cur->h_function_pointer) &&
600 (EventType == cur->eventtype))
602 syslog(LOG_DEBUG, "extensions: unregistered user function (type %d)", EventType);
621 void CtdlDestroyUserHooks(void)
623 UserFunctionHook *cur, *p;
628 syslog(LOG_DEBUG, "extensions: destroyed user function");
633 UserHookTable = NULL;
637 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
641 MessageFunctionHook *newfcn;
643 newfcn = (MessageFunctionHook *)
644 malloc(sizeof(MessageFunctionHook));
645 newfcn->next = MessageHookTable;
646 newfcn->h_function_pointer = handler;
647 newfcn->eventtype = EventType;
648 MessageHookTable = newfcn;
650 syslog(LOG_DEBUG, "extensions: registered a new message function (type %d)", EventType);
654 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
657 MessageFunctionHook *cur, *p, *last;
659 cur = MessageHookTable;
660 while (cur != NULL) {
661 if ((handler == cur->h_function_pointer) &&
662 (EventType == cur->eventtype))
664 syslog(LOG_DEBUG, "extensions: unregistered message function (type %d)", EventType);
672 MessageHookTable = p;
682 void CtdlDestroyMessageHook(void)
684 MessageFunctionHook *cur, *p;
686 cur = MessageHookTable;
689 syslog(LOG_DEBUG, "extensions: destroyed message function (type %d)", cur->eventtype);
694 MessageHookTable = NULL;
698 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
700 RoomFunctionHook *newfcn;
702 newfcn = (RoomFunctionHook *)
703 malloc(sizeof(RoomFunctionHook));
704 newfcn->next = RoomHookTable;
705 newfcn->fcn_ptr = fcn_ptr;
706 RoomHookTable = newfcn;
708 syslog(LOG_DEBUG, "extensions: registered a new room function");
712 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
714 RoomFunctionHook *cur, *p, *last;
719 if (fcn_ptr == cur->fcn_ptr) {
720 syslog(LOG_DEBUG, "extensions: unregistered room function");
740 void CtdlDestroyRoomHooks(void)
742 RoomFunctionHook *cur, *p;
747 syslog(LOG_DEBUG, "extensions: destroyed room function");
752 RoomHookTable = NULL;
755 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
757 NetprocFunctionHook *newfcn;
759 newfcn = (NetprocFunctionHook *)
760 malloc(sizeof(NetprocFunctionHook));
761 newfcn->next = NetprocHookTable;
762 newfcn->h_function_pointer = handler;
763 NetprocHookTable = newfcn;
765 syslog(LOG_DEBUG, "extensions: registered a new netproc function");
769 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
771 NetprocFunctionHook *cur, *p, *last;
773 cur = NetprocHookTable;
776 while (cur != NULL) {
777 if (handler == cur->h_function_pointer)
779 syslog(LOG_DEBUG, "extensions: unregistered netproc function");
786 NetprocHookTable = p;
797 void CtdlDestroyNetprocHooks(void)
799 NetprocFunctionHook *cur, *p;
801 cur = NetprocHookTable;
804 syslog(LOG_DEBUG, "extensions: destroyed netproc function");
809 NetprocHookTable = NULL;
813 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
815 DeleteFunctionHook *newfcn;
817 newfcn = (DeleteFunctionHook *)
818 malloc(sizeof(DeleteFunctionHook));
819 newfcn->next = DeleteHookTable;
820 newfcn->h_function_pointer = handler;
821 DeleteHookTable = newfcn;
823 syslog(LOG_DEBUG, "extensions: registered a new delete function");
827 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
829 DeleteFunctionHook *cur, *p, *last;
832 cur = DeleteHookTable;
833 while (cur != NULL) {
834 if (handler == cur->h_function_pointer )
836 syslog(LOG_DEBUG, "extensions: unregistered delete function");
855 void CtdlDestroyDeleteHooks(void)
857 DeleteFunctionHook *cur, *p;
859 cur = DeleteHookTable;
862 syslog(LOG_DEBUG, "extensions: destroyed delete function");
867 DeleteHookTable = NULL;
871 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
873 FixedOutputHook *newfcn;
875 newfcn = (FixedOutputHook *)
876 malloc(sizeof(FixedOutputHook));
877 newfcn->next = FixedOutputTable;
878 newfcn->h_function_pointer = handler;
879 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
880 FixedOutputTable = newfcn;
882 syslog(LOG_DEBUG, "extensions: registered a new fixed output function for %s", newfcn->content_type);
886 void CtdlUnregisterFixedOutputHook(char *content_type)
888 FixedOutputHook *cur, *p, *last;
891 cur = FixedOutputTable;
892 while (cur != NULL) {
893 /* This will also remove duplicates if any */
894 if (!strcasecmp(content_type, cur->content_type)) {
895 syslog(LOG_DEBUG, "extensions: unregistered fixed output function for %s", content_type);
902 FixedOutputTable = p;
914 void CtdlDestroyFixedOutputHooks(void)
916 FixedOutputHook *cur, *p;
918 cur = FixedOutputTable;
921 syslog(LOG_DEBUG, "extensions: destroyed fixed output function for %s", cur->content_type);
927 FixedOutputTable = NULL;
930 /* returns nonzero if we found a hook and used it */
931 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
933 FixedOutputHook *fcn;
935 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
936 if (!strcasecmp(content_type, fcn->content_type)) {
937 (*fcn->h_function_pointer) (content, content_length);
945 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
948 XmsgFunctionHook *newfcn;
950 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
951 newfcn->next = XmsgHookTable;
952 newfcn->order = order;
953 newfcn->h_function_pointer = fcn_ptr;
954 XmsgHookTable = newfcn;
955 syslog(LOG_DEBUG, "extensions: registered a new x-msg function (priority %d)", order);
959 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
961 XmsgFunctionHook *cur, *p, *last;
965 while (cur != NULL) {
966 /* This will also remove duplicates if any */
967 if (fcn_ptr == cur->h_function_pointer &&
968 order == cur->order) {
969 syslog(LOG_DEBUG, "extensions: unregistered x-msg function (priority %d)", order);
989 void CtdlDestroyXmsgHooks(void)
991 XmsgFunctionHook *cur, *p;
996 syslog(LOG_DEBUG, "extensions: destroyed x-msg function (priority %d)", cur->order);
1001 XmsgHookTable = NULL;
1005 void CtdlRegisterServiceHook(int tcp_port,
1007 void (*h_greeting_function) (void),
1008 void (*h_command_function) (void),
1009 void (*h_async_function) (void),
1010 const char *ServiceName)
1012 ServiceFunctionHook *newfcn;
1017 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1018 message = (char*) malloc (SIZ + SIZ);
1020 newfcn->next = ServiceHookTable;
1021 newfcn->tcp_port = tcp_port;
1022 newfcn->sockpath = sockpath;
1023 newfcn->h_greeting_function = h_greeting_function;
1024 newfcn->h_command_function = h_command_function;
1025 newfcn->h_async_function = h_async_function;
1026 newfcn->ServiceName = ServiceName;
1028 if (sockpath != NULL) {
1029 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"), error);
1030 snprintf(message, SIZ, "extensions: unix domain socket '%s': ", sockpath);
1032 else if (tcp_port <= 0) { /* port -1 to disable */
1033 syslog(LOG_INFO, "extensions: service %s has been manually disabled, skipping", ServiceName);
1039 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"),
1041 CtdlGetConfigInt("c_maxsessions"),
1043 snprintf(message, SIZ, "extensions: TCP port %s:%d: (%s) ",
1044 CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
1047 if (newfcn->msock > 0) {
1048 ServiceHookTable = newfcn;
1049 strcat(message, "registered.");
1050 syslog(LOG_INFO, "%s", message);
1053 AddPortError(message, error);
1054 strcat(message, "FAILED.");
1055 syslog(LOG_ERR, "%s", message);
1062 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1063 void (*h_greeting_function) (void),
1064 void (*h_command_function) (void),
1065 void (*h_async_function) (void)
1068 ServiceFunctionHook *cur, *p, *last;
1071 cur = ServiceHookTable;
1072 while (cur != NULL) {
1073 /* This will also remove duplicates if any */
1074 if (h_greeting_function == cur->h_greeting_function &&
1075 h_command_function == cur->h_command_function &&
1076 h_async_function == cur->h_async_function &&
1077 tcp_port == cur->tcp_port &&
1078 !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
1083 syslog(LOG_INFO, "extensions: closed UNIX domain socket %s", sockpath);
1085 } else if (tcp_port) {
1086 syslog(LOG_INFO, "extensions: closed TCP port %d", tcp_port);
1088 syslog(LOG_INFO, "extensions: unregistered service \"%s\"", cur->ServiceName);
1095 ServiceHookTable = p;
1106 void CtdlShutdownServiceHooks(void)
1108 /* sort of a duplicate of close_masters() but called earlier */
1109 ServiceFunctionHook *cur;
1111 cur = ServiceHookTable;
1114 if (cur->msock != -1)
1118 if (cur->sockpath != NULL){
1119 syslog(LOG_INFO, "extensions: [%s] Closed UNIX domain socket %s", cur->ServiceName, cur->sockpath);
1120 unlink(cur->sockpath);
1122 syslog(LOG_INFO, "extensions: [%s] closing service", cur->ServiceName);
1130 void CtdlDestroyServiceHook(void)
1133 ServiceFunctionHook *cur, *p;
1135 cur = ServiceHookTable;
1138 if (cur->msock != -1)
1145 Text = " Not closing again";
1148 if (cur->sockpath) {
1149 syslog(LOG_INFO, "extensions: %s UNIX domain socket %s", Text, cur->sockpath);
1150 unlink(cur->sockpath);
1151 } else if (cur->tcp_port) {
1152 syslog(LOG_INFO, "extensions: %s TCP port %d", Text, cur->tcp_port);
1154 syslog(LOG_INFO, "extensions: destroyed service \"%s\"", cur->ServiceName);
1160 ServiceHookTable = NULL;
1163 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1165 SearchFunctionHook *newfcn;
1167 if (!name || !fcn_ptr) {
1171 newfcn = (SearchFunctionHook *)
1172 malloc(sizeof(SearchFunctionHook));
1173 newfcn->next = SearchFunctionHookTable;
1174 newfcn->name = name;
1175 newfcn->fcn_ptr = fcn_ptr;
1176 SearchFunctionHookTable = newfcn;
1178 syslog(LOG_DEBUG, "extensions: registered a new search function (%s)", name);
1181 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1183 SearchFunctionHook *cur, *p, *last;
1186 cur = SearchFunctionHookTable;
1187 while (cur != NULL) {
1189 (cur->fcn_ptr == fcn_ptr) &&
1190 name && !strcmp(name, cur->name))
1192 syslog(LOG_DEBUG, "extensions: unregistered search function(%s)", name);
1198 SearchFunctionHookTable = p;
1208 void CtdlDestroySearchHooks(void)
1210 SearchFunctionHook *cur, *p;
1212 cur = SearchFunctionHookTable;
1213 SearchFunctionHookTable = NULL;
1214 while (cur != NULL) {
1221 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1223 SearchFunctionHook *fcn = NULL;
1225 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1226 if (!func_name || !strcmp(func_name, fcn->name)) {
1227 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1235 void PerformSessionHooks(int EventType)
1237 SessionFunctionHook *fcn = NULL;
1239 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1240 if (fcn->eventtype == EventType) {
1241 if (EventType == EVT_TIMER) {
1242 pthread_setspecific(MyConKey, NULL); /* for every hook */
1244 (*fcn->h_function_pointer) ();
1249 void PerformUserHooks(ctdluser *usbuf, int EventType)
1251 UserFunctionHook *fcn = NULL;
1253 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1254 if (fcn->eventtype == EventType) {
1255 (*fcn->h_function_pointer) (usbuf);
1260 int PerformMessageHooks(struct CtdlMessage *msg, recptypes *recps, int EventType)
1262 MessageFunctionHook *fcn = NULL;
1263 int total_retval = 0;
1265 /* Other code may elect to protect this message from server-side
1266 * handlers; if this is the case, don't do anything.
1268 if (msg->cm_flags & CM_SKIP_HOOKS) {
1272 /* Otherwise, run all the hooks appropriate to this event type.
1274 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1275 if (fcn->eventtype == EventType) {
1276 total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
1280 /* Return the sum of the return codes from the hook functions. If
1281 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1282 * the save operation to abort.
1284 return total_retval;
1288 int PerformRoomHooks(struct ctdlroom *target_room)
1290 RoomFunctionHook *fcn;
1291 int total_retval = 0;
1293 syslog(LOG_DEBUG, "extensions: performing room hooks for <%s>", target_room->QRname);
1295 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1296 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1299 /* Return the sum of the return codes from the hook functions.
1301 return total_retval;
1305 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1307 NetprocFunctionHook *fcn;
1308 int total_retval = 0;
1310 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1311 total_retval = total_retval +
1312 (*fcn->h_function_pointer) (msg, target_room);
1315 /* Return the sum of the return codes from the hook functions.
1316 * A nonzero return code will cause the message to *not* be imported.
1318 return total_retval;
1322 void PerformDeleteHooks(char *room, long msgnum)
1324 DeleteFunctionHook *fcn;
1326 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1327 (*fcn->h_function_pointer) (room, msgnum);
1335 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1337 XmsgFunctionHook *fcn;
1341 for (p=0; p<MAX_XMSG_PRI; ++p) {
1342 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1343 if (fcn->order == p) {
1345 (*fcn->h_function_pointer)
1346 (sender, sender_email, recp, msg);
1349 /* Break out of the loop if a higher-priority function
1350 * successfully delivered the message. This prevents duplicate
1351 * deliveries to local users simultaneously signed onto
1354 if (total_sent) break;
1361 * Dirty hack until we impliment a hook mechanism for this
1363 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1366 CtdlStartTLS (ok_response, nosup_response, error_response);
1370 void DebugModulesEnable(const int n)
1374 void MarkersEnable(const int n)
1378 void DebugCitadelProtoEnable(const int n)
1383 CTDL_MODULE_INIT(modules)
1386 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);
1387 CtdlRegisterDebugFlagHook(HKEY("periodicmarkers"), MarkersEnable, &EnableMarkers);
1388 CtdlRegisterDebugFlagHook(HKEY("citadelprotocol"), DebugCitadelProtoEnable, &EnableCtlProto);