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;
90 void (*h_function_pointer) (void);
93 extern SessionFunctionHook *SessionHookTable;
97 * UserFunctionHook extensions are used for any type of hook which implements
98 * an operation on a user or username (potentially) other than the one
99 * operating the current session.
101 typedef struct UserFunctionHook UserFunctionHook;
102 struct UserFunctionHook {
103 UserFunctionHook *next;
104 void (*h_function_pointer) (struct ctdluser *usbuf);
107 extern UserFunctionHook *UserHookTable;
110 * MessageFunctionHook extensions are used for hooks which implement handlers
111 * for various types of message operations (save, read, etc.)
113 typedef struct MessageFunctionHook MessageFunctionHook;
114 struct MessageFunctionHook {
115 MessageFunctionHook *next;
116 int (*h_function_pointer) (struct CtdlMessage *msg);
119 extern MessageFunctionHook *MessageHookTable;
123 * NetprocFunctionHook extensions are used for hooks which implement handlers
124 * for incoming network messages.
126 typedef struct NetprocFunctionHook NetprocFunctionHook;
127 struct NetprocFunctionHook {
128 NetprocFunctionHook *next;
129 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
131 extern NetprocFunctionHook *NetprocHookTable;
135 * DeleteFunctionHook extensions are used for hooks which get called when a
136 * message is about to be deleted.
138 typedef struct DeleteFunctionHook DeleteFunctionHook;
139 struct DeleteFunctionHook {
140 DeleteFunctionHook *next;
141 void (*h_function_pointer) (char *target_room, long msgnum);
143 extern DeleteFunctionHook *DeleteHookTable;
147 * ExpressMessageFunctionHook extensions are used for hooks which implement
148 * the sending of an instant message through various channels. Any function
149 * registered should return the number of recipients to whom the message was
150 * successfully transmitted.
152 typedef struct XmsgFunctionHook XmsgFunctionHook;
153 struct XmsgFunctionHook {
154 XmsgFunctionHook *next;
155 int (*h_function_pointer) (char *, char *, char *, char *);
158 extern XmsgFunctionHook *XmsgHookTable;
164 * RoomFunctionHook extensions are used for hooks which impliment room
165 * processing functions when new messages are added EG. SIEVE.
167 typedef struct RoomFunctionHook RoomFunctionHook;
168 struct RoomFunctionHook {
169 RoomFunctionHook *next;
170 int (*fcn_ptr) (struct ctdlroom *);
172 extern RoomFunctionHook *RoomHookTable;
176 typedef struct SearchFunctionHook SearchFunctionHook;
177 struct SearchFunctionHook {
178 SearchFunctionHook *next;
179 void (*fcn_ptr) (int *, long **, const char *);
182 extern SearchFunctionHook *SearchFunctionHookTable;
185 CleanupFunctionHook *CleanupHookTable = NULL;
186 CleanupFunctionHook *EVCleanupHookTable = NULL;
187 SessionFunctionHook *SessionHookTable = NULL;
188 UserFunctionHook *UserHookTable = NULL;
189 XmsgFunctionHook *XmsgHookTable = NULL;
190 MessageFunctionHook *MessageHookTable = NULL;
191 NetprocFunctionHook *NetprocHookTable = NULL;
192 DeleteFunctionHook *DeleteHookTable = NULL;
193 ServiceFunctionHook *ServiceHookTable = NULL;
194 FixedOutputHook *FixedOutputTable = NULL;
195 RoomFunctionHook *RoomHookTable = NULL;
196 SearchFunctionHook *SearchFunctionHookTable = NULL;
198 typedef struct ProtoFunctionHook ProtoFunctionHook;
199 struct ProtoFunctionHook {
200 void (*handler) (char *cmdbuf);
205 HashList *ProtoHookList = NULL;
208 #define ERR_PORT (1 << 1)
211 static StrBuf *portlist = NULL;
213 static StrBuf *errormessages = NULL;
216 long DetailErrorFlags;
217 ConstStr Empty = {HKEY("")};
218 char *ErrSubject = "Startup Problems";
219 ConstStr ErrGeneral[] = {
220 {HKEY("Citadel had trouble on starting up. ")},
221 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
222 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
223 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
224 "The errors returned by the system were:\n")},
225 {HKEY("You can recheck the above if you follow this faq item:\n"
226 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
229 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
230 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
231 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
232 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
233 " and stop the program that binds these ports.\n You should eventually remove "
234 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
235 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
238 void LogPrintMessages(long err)
241 StrBuf *List, *DetailList;
242 ConstStr *Short, *Where, *Hint;
245 Message = NewStrBufPlain(NULL,
246 StrLength(portlist) + StrLength(errormessages));
248 DetailErrorFlags = DetailErrorFlags & ~err;
253 Short = &ErrPortShort;
254 Where = &ErrPortWhere;
257 DetailList = errormessages;
267 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
268 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
269 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
270 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
271 StrBufAppendBuf(Message, List, 0);
272 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
273 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
274 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
275 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
276 StrBufAppendBuf(Message, DetailList, 0);
277 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
278 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
280 MOD_syslog(LOG_EMERG, "%s", ChrPtr(Message));
281 MOD_syslog(LOG_EMERG, "%s", ErrSubject);
282 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
284 FreeStrBuf(&Message);
286 FreeStrBuf(&DetailList);
290 void AddPortError(char *Port, char *ErrorMessage)
294 DetailErrorFlags |= ERR_PORT;
296 len = StrLength(errormessages);
297 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
298 else errormessages = NewStrBuf();
299 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
302 len = StrLength(portlist);
303 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
304 else portlist = NewStrBuf();
305 StrBufAppendBufPlain(portlist, Port, -1, 0);
309 int DLoader_Exec_Cmd(char *cmdbuf)
312 ProtoFunctionHook *p;
314 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
315 p = (ProtoFunctionHook*) vP;
316 p->handler(&cmdbuf[5]);
322 long FourHash(const char *key, long length)
326 const unsigned char *ptr = (const unsigned char*)key;
328 for (i = 0; i < 4; i++, ptr ++)
338 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
341 if (LogDebugEntryTable == NULL)
342 LogDebugEntryTable = NewHash(1, NULL);
343 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
348 Put(LogDebugEntryTable, Name, Len, E, NULL);
351 void CtdlSetDebugLogFacilities(const char **Str, long n)
353 StrBuf *Token = NULL;
360 for (i=0; i < n; i++){
361 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
362 if (strcmp(Str[i], "all") == 0) {
366 Buf = NewStrBufPlain(Str[i], -1);
369 Token = NewStrBufPlain(NULL, StrLength(Buf));
370 while ((ch != StrBufNOTNULL) &&
371 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
372 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
375 LogDebugEntry *E = (LogDebugEntry*)vptr;
388 Pos = GetNewHashPos(LogDebugEntryTable, 0);
389 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
390 LogDebugEntry *E = (LogDebugEntry*)vptr;
397 void cmd_log_get(char *argbuf)
404 if (CtdlAccessCheck(ac_aide)) return;
406 cprintf("%d Log modules enabled:\n", LISTING_FOLLOWS);
408 Pos = GetNewHashPos(LogDebugEntryTable, 0);
410 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
411 LogDebugEntry *E = (LogDebugEntry*)vptr;
412 cprintf("%s|%d\n", ch, *E->LogP);
418 void cmd_log_set(char *argbuf)
423 char which[SIZ] = "";
425 if (CtdlAccessCheck(ac_aide)) return;
427 wlen = extract_token(which, argbuf, 0, '|', sizeof(which));
428 if (wlen < 0) wlen = 0;
429 lset = extract_int(argbuf, 1);
430 if (lset != 0) lset = 1;
431 if (GetHash(LogDebugEntryTable, which, wlen, &vptr) &&
434 LogDebugEntry *E = (LogDebugEntry*)vptr;
436 cprintf("%d %s|%d\n", CIT_OK, which, lset);
439 cprintf("%d Log setting %s not known\n",
443 void CtdlDestroyDebugTable(void)
446 DeleteHash(&LogDebugEntryTable);
449 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
451 ProtoFunctionHook *p;
453 if (ProtoHookList == NULL)
454 ProtoHookList = NewHash (1, FourHash);
457 p = (ProtoFunctionHook *)
458 malloc(sizeof(ProtoFunctionHook));
461 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
464 p->handler = handler;
468 Put(ProtoHookList, cmd, 4, p, NULL);
469 MOD_syslog(LOG_DEBUG, "Registered server command %s (%s)\n", cmd, desc);
472 void CtdlDestroyProtoHooks(void)
475 DeleteHash(&ProtoHookList);
479 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
482 CleanupFunctionHook *newfcn;
484 newfcn = (CleanupFunctionHook *)
485 malloc(sizeof(CleanupFunctionHook));
486 newfcn->next = CleanupHookTable;
487 newfcn->h_function_pointer = fcn_ptr;
488 CleanupHookTable = newfcn;
490 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
494 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
496 CleanupFunctionHook *cur, *p;
498 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
499 /* This will also remove duplicates if any */
500 while (cur != NULL &&
501 fcn_ptr == cur->h_function_pointer) {
502 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
504 if (cur == CleanupHookTable) {
505 CleanupHookTable = p;
514 void CtdlDestroyCleanupHooks(void)
516 CleanupFunctionHook *cur, *p;
518 cur = CleanupHookTable;
521 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
526 CleanupHookTable = NULL;
529 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
532 CleanupFunctionHook *newfcn;
534 newfcn = (CleanupFunctionHook *)
535 malloc(sizeof(CleanupFunctionHook));
536 newfcn->next = EVCleanupHookTable;
537 newfcn->h_function_pointer = fcn_ptr;
538 EVCleanupHookTable = newfcn;
540 MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
544 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
546 CleanupFunctionHook *cur, *p;
548 for (cur = EVCleanupHookTable; cur != NULL; cur = cur->next) {
549 /* This will also remove duplicates if any */
550 while (cur != NULL &&
551 fcn_ptr == cur->h_function_pointer) {
552 MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
554 if (cur == EVCleanupHookTable) {
555 EVCleanupHookTable = p;
564 void CtdlDestroyEVCleanupHooks(void)
566 CleanupFunctionHook *cur, *p;
568 cur = EVCleanupHookTable;
571 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
576 EVCleanupHookTable = NULL;
580 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
583 SessionFunctionHook *newfcn;
585 newfcn = (SessionFunctionHook *)
586 malloc(sizeof(SessionFunctionHook));
587 newfcn->next = SessionHookTable;
588 newfcn->h_function_pointer = fcn_ptr;
589 newfcn->eventtype = EventType;
590 SessionHookTable = newfcn;
592 MOD_syslog(LOG_DEBUG, "Registered a new session function (type %d)\n",
597 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
599 SessionFunctionHook *cur, *p;
601 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
602 /* This will also remove duplicates if any */
603 while (cur != NULL &&
604 fcn_ptr == cur->h_function_pointer &&
605 EventType == cur->eventtype) {
606 MOD_syslog(LOG_DEBUG, "Unregistered session function (type %d)\n",
609 if (cur == SessionHookTable) {
610 SessionHookTable = p;
618 void CtdlDestroySessionHooks(void)
620 SessionFunctionHook *cur, *p;
622 cur = SessionHookTable;
625 MODM_syslog(LOG_DEBUG, "Destroyed session function\n");
630 SessionHookTable = NULL;
634 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
637 UserFunctionHook *newfcn;
639 newfcn = (UserFunctionHook *)
640 malloc(sizeof(UserFunctionHook));
641 newfcn->next = UserHookTable;
642 newfcn->h_function_pointer = fcn_ptr;
643 newfcn->eventtype = EventType;
644 UserHookTable = newfcn;
646 MOD_syslog(LOG_DEBUG, "Registered a new user function (type %d)\n",
651 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
653 UserFunctionHook *cur, *p;
655 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
656 /* This will also remove duplicates if any */
657 while (cur != NULL &&
658 fcn_ptr == cur->h_function_pointer &&
659 EventType == cur->eventtype) {
660 MOD_syslog(LOG_DEBUG, "Unregistered user function (type %d)\n",
663 if (cur == UserHookTable) {
672 void CtdlDestroyUserHooks(void)
674 UserFunctionHook *cur, *p;
679 MODM_syslog(LOG_DEBUG, "Destroyed user function \n");
684 UserHookTable = NULL;
688 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
692 MessageFunctionHook *newfcn;
694 newfcn = (MessageFunctionHook *)
695 malloc(sizeof(MessageFunctionHook));
696 newfcn->next = MessageHookTable;
697 newfcn->h_function_pointer = handler;
698 newfcn->eventtype = EventType;
699 MessageHookTable = newfcn;
701 MOD_syslog(LOG_DEBUG, "Registered a new message function (type %d)\n",
706 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
709 MessageFunctionHook *cur, *p;
711 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
712 /* This will also remove duplicates if any */
713 while (cur != NULL &&
714 handler == cur->h_function_pointer &&
715 EventType == cur->eventtype) {
716 MOD_syslog(LOG_DEBUG, "Unregistered message function (type %d)\n",
719 if (cur == MessageHookTable) {
720 MessageHookTable = p;
728 void CtdlDestroyMessageHook(void)
730 MessageFunctionHook *cur, *p;
732 cur = MessageHookTable;
735 MOD_syslog(LOG_DEBUG, "Destroyed message function (type %d)\n", cur->eventtype);
740 MessageHookTable = NULL;
744 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
746 RoomFunctionHook *newfcn;
748 newfcn = (RoomFunctionHook *)
749 malloc(sizeof(RoomFunctionHook));
750 newfcn->next = RoomHookTable;
751 newfcn->fcn_ptr = fcn_ptr;
752 RoomHookTable = newfcn;
754 MODM_syslog(LOG_DEBUG, "Registered a new room function\n");
758 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
760 RoomFunctionHook *cur, *p;
762 for (cur = RoomHookTable; cur != NULL; cur = cur->next) {
763 while (cur != NULL && fcn_ptr == cur->fcn_ptr) {
764 MODM_syslog(LOG_DEBUG, "Unregistered room function\n");
766 if (cur == RoomHookTable) {
776 void CtdlDestroyRoomHooks(void)
778 RoomFunctionHook *cur, *p;
783 MODM_syslog(LOG_DEBUG, "Destroyed room function\n");
788 RoomHookTable = NULL;
791 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
793 NetprocFunctionHook *newfcn;
795 newfcn = (NetprocFunctionHook *)
796 malloc(sizeof(NetprocFunctionHook));
797 newfcn->next = NetprocHookTable;
798 newfcn->h_function_pointer = handler;
799 NetprocHookTable = newfcn;
801 MODM_syslog(LOG_DEBUG, "Registered a new netproc function\n");
805 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
807 NetprocFunctionHook *cur, *p;
809 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
810 /* This will also remove duplicates if any */
811 while (cur != NULL &&
812 handler == cur->h_function_pointer ) {
813 MODM_syslog(LOG_DEBUG, "Unregistered netproc function\n");
815 if (cur == NetprocHookTable) {
816 NetprocHookTable = p;
824 void CtdlDestroyNetprocHooks(void)
826 NetprocFunctionHook *cur, *p;
828 cur = NetprocHookTable;
831 MODM_syslog(LOG_DEBUG, "Destroyed netproc function\n");
836 NetprocHookTable = NULL;
840 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
842 DeleteFunctionHook *newfcn;
844 newfcn = (DeleteFunctionHook *)
845 malloc(sizeof(DeleteFunctionHook));
846 newfcn->next = DeleteHookTable;
847 newfcn->h_function_pointer = handler;
848 DeleteHookTable = newfcn;
850 MODM_syslog(LOG_DEBUG, "Registered a new delete function\n");
854 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
856 DeleteFunctionHook *cur, *p;
858 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
859 /* This will also remove duplicates if any */
860 while (cur != NULL &&
861 handler == cur->h_function_pointer ) {
862 MODM_syslog(LOG_DEBUG, "Unregistered delete function\n");
864 if (cur == DeleteHookTable) {
872 void CtdlDestroyDeleteHooks(void)
874 DeleteFunctionHook *cur, *p;
876 cur = DeleteHookTable;
879 MODM_syslog(LOG_DEBUG, "Destroyed delete function\n");
884 DeleteHookTable = NULL;
890 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
892 FixedOutputHook *newfcn;
894 newfcn = (FixedOutputHook *)
895 malloc(sizeof(FixedOutputHook));
896 newfcn->next = FixedOutputTable;
897 newfcn->h_function_pointer = handler;
898 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
899 FixedOutputTable = newfcn;
901 MOD_syslog(LOG_DEBUG, "Registered a new fixed output function for %s\n", newfcn->content_type);
905 void CtdlUnregisterFixedOutputHook(char *content_type)
907 FixedOutputHook *cur, *p;
909 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
910 /* This will also remove duplicates if any */
911 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
912 MOD_syslog(LOG_DEBUG, "Unregistered fixed output function for %s\n", content_type);
914 if (cur == FixedOutputTable) {
915 FixedOutputTable = p;
923 void CtdlDestroyFixedOutputHooks(void)
925 FixedOutputHook *cur, *p;
927 cur = FixedOutputTable;
930 MOD_syslog(LOG_DEBUG, "Destroyed fixed output function for %s\n", cur->content_type);
936 FixedOutputTable = NULL;
939 /* returns nonzero if we found a hook and used it */
940 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
942 FixedOutputHook *fcn;
944 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
945 if (!strcasecmp(content_type, fcn->content_type)) {
946 (*fcn->h_function_pointer) (content, content_length);
957 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
960 XmsgFunctionHook *newfcn;
962 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
963 newfcn->next = XmsgHookTable;
964 newfcn->order = order;
965 newfcn->h_function_pointer = fcn_ptr;
966 XmsgHookTable = newfcn;
967 MOD_syslog(LOG_DEBUG, "Registered a new x-msg function (priority %d)\n", order);
971 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
973 XmsgFunctionHook *cur, *p;
975 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
976 /* This will also remove duplicates if any */
977 while (cur != NULL &&
978 fcn_ptr == cur->h_function_pointer &&
979 order == cur->order) {
980 MOD_syslog(LOG_DEBUG, "Unregistered x-msg function "
981 "(priority %d)\n", order);
983 if (cur == XmsgHookTable) {
992 void CtdlDestroyXmsgHooks(void)
994 XmsgFunctionHook *cur, *p;
999 MOD_syslog(LOG_DEBUG, "Destroyed x-msg function "
1000 "(priority %d)\n", cur->order);
1006 XmsgHookTable = NULL;
1010 void CtdlRegisterServiceHook(int tcp_port,
1012 void (*h_greeting_function) (void),
1013 void (*h_command_function) (void),
1014 void (*h_async_function) (void),
1015 const char *ServiceName)
1017 ServiceFunctionHook *newfcn;
1022 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1023 message = (char*) malloc (SIZ + SIZ);
1025 newfcn->next = ServiceHookTable;
1026 newfcn->tcp_port = tcp_port;
1027 newfcn->sockpath = sockpath;
1028 newfcn->h_greeting_function = h_greeting_function;
1029 newfcn->h_command_function = h_command_function;
1030 newfcn->h_async_function = h_async_function;
1031 newfcn->ServiceName = ServiceName;
1033 if (sockpath != NULL) {
1034 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
1035 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1037 else if (tcp_port <= 0) { /* port -1 to disable */
1038 MOD_syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1044 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
1046 config.c_maxsessions,
1048 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
1049 config.c_ip_addr, tcp_port, ServiceName);
1052 if (newfcn->msock > 0) {
1053 ServiceHookTable = newfcn;
1054 strcat(message, "registered.");
1055 MOD_syslog(LOG_INFO, "%s\n", message);
1058 AddPortError(message, error);
1059 strcat(message, "FAILED.");
1060 MOD_syslog(LOG_CRIT, "%s\n", message);
1067 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1068 void (*h_greeting_function) (void),
1069 void (*h_command_function) (void),
1070 void (*h_async_function) (void)
1073 ServiceFunctionHook *cur, *p;
1075 cur = ServiceHookTable;
1076 while (cur != NULL) {
1077 /* This will also remove duplicates if any */
1078 while (cur != NULL &&
1079 !(sockpath && cur->sockpath &&
1080 strcmp(sockpath, cur->sockpath)) &&
1081 h_greeting_function == cur->h_greeting_function &&
1082 h_command_function == cur->h_command_function &&
1083 h_async_function == cur->h_async_function &&
1084 tcp_port == cur->tcp_port) {
1087 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1090 } else if (tcp_port) {
1091 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1093 MOD_syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1096 if (cur == ServiceHookTable) {
1097 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 MOD_syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1122 unlink(cur->sockpath);
1124 MOD_syslog(LOG_INFO, "[%s] closing service\n",
1132 void CtdlDestroyServiceHook(void)
1134 ServiceFunctionHook *cur, *p;
1136 cur = ServiceHookTable;
1140 if (cur->sockpath) {
1141 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1143 unlink(cur->sockpath);
1144 } else if (cur->tcp_port) {
1145 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", cur->tcp_port);
1147 MOD_syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1153 ServiceHookTable = NULL;
1156 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1158 SearchFunctionHook *newfcn;
1160 if (!name || !fcn_ptr) {
1164 newfcn = (SearchFunctionHook *)
1165 malloc(sizeof(SearchFunctionHook));
1166 newfcn->next = SearchFunctionHookTable;
1167 newfcn->name = name;
1168 newfcn->fcn_ptr = fcn_ptr;
1169 SearchFunctionHookTable = newfcn;
1171 MOD_syslog(LOG_DEBUG, "Registered a new search function (%s)\n", name);
1174 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1176 SearchFunctionHook *cur, *p;
1178 for (cur = SearchFunctionHookTable; cur != NULL; cur = cur->next) {
1179 while (fcn_ptr && (cur->fcn_ptr == fcn_ptr) && name && !strcmp(name, cur->name)) {
1180 MOD_syslog(LOG_DEBUG, "Unregistered search function(%s)\n", name);
1182 if (cur == SearchFunctionHookTable) {
1183 SearchFunctionHookTable = p;
1191 void CtdlDestroySearchHooks(void)
1193 SearchFunctionHook *cur, *p;
1195 cur = SearchFunctionHookTable;
1196 SearchFunctionHookTable = NULL;
1197 while (cur != NULL) {
1204 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1206 SearchFunctionHook *fcn = NULL;
1208 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1209 if (!func_name || !strcmp(func_name, fcn->name)) {
1210 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1218 void PerformSessionHooks(int EventType)
1220 SessionFunctionHook *fcn = NULL;
1222 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1223 if (fcn->eventtype == EventType) {
1224 if (EventType == EVT_TIMER) {
1225 pthread_setspecific(MyConKey, NULL); /* for every hook */
1227 (*fcn->h_function_pointer) ();
1232 void PerformUserHooks(ctdluser *usbuf, int EventType)
1234 UserFunctionHook *fcn = NULL;
1236 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1237 if (fcn->eventtype == EventType) {
1238 (*fcn->h_function_pointer) (usbuf);
1243 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
1245 MessageFunctionHook *fcn = NULL;
1246 int total_retval = 0;
1248 /* Other code may elect to protect this message from server-side
1249 * handlers; if this is the case, don't do anything.
1250 MOD_syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1252 if (msg->cm_flags & CM_SKIP_HOOKS) {
1253 MODM_syslog(LOG_DEBUG, "Skipping hooks\n");
1257 /* Otherwise, run all the hooks appropriate to this event type.
1259 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1260 if (fcn->eventtype == EventType) {
1261 total_retval = total_retval + (*fcn->h_function_pointer) (msg);
1265 /* Return the sum of the return codes from the hook functions. If
1266 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1267 * the save operation to abort.
1269 return total_retval;
1273 int PerformRoomHooks(struct ctdlroom *target_room)
1275 RoomFunctionHook *fcn;
1276 int total_retval = 0;
1278 MOD_syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1280 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1281 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1284 /* Return the sum of the return codes from the hook functions.
1286 return total_retval;
1290 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1292 NetprocFunctionHook *fcn;
1293 int total_retval = 0;
1295 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1296 total_retval = total_retval +
1297 (*fcn->h_function_pointer) (msg, target_room);
1300 /* Return the sum of the return codes from the hook functions.
1301 * A nonzero return code will cause the message to *not* be imported.
1303 return total_retval;
1307 void PerformDeleteHooks(char *room, long msgnum)
1309 DeleteFunctionHook *fcn;
1311 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1312 (*fcn->h_function_pointer) (room, msgnum);
1320 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1322 XmsgFunctionHook *fcn;
1326 for (p=0; p<MAX_XMSG_PRI; ++p) {
1327 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1328 if (fcn->order == p) {
1330 (*fcn->h_function_pointer)
1331 (sender, sender_email, recp, msg);
1334 /* Break out of the loop if a higher-priority function
1335 * successfully delivered the message. This prevents duplicate
1336 * deliveries to local users simultaneously signed onto
1339 if (total_sent) break;
1346 * Dirty hack until we impliment a hook mechanism for this
1348 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1351 CtdlStartTLS (ok_response, nosup_response, error_response);
1355 void DebugModulesEnable(const int n)
1359 CTDL_MODULE_INIT(modules)
1362 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);
1364 CtdlRegisterProtoHook(cmd_log_get, "LOGP", "Print Log-parameters");
1365 CtdlRegisterProtoHook(cmd_log_set, "LOGS", "Set Log-parameters");