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"
51 * Structure defentitions for hook tables
54 typedef struct __LogDebugEntry {
60 HashList *LogDebugEntryTable = NULL;
62 typedef struct LogFunctionHook LogFunctionHook;
63 struct LogFunctionHook {
64 LogFunctionHook *next;
66 void (*h_function_pointer) (char *);
68 extern LogFunctionHook *LogHookTable;
70 typedef struct FixedOutputHook FixedOutputHook;
71 struct FixedOutputHook {
72 FixedOutputHook *next;
73 char content_type[64];
74 void (*h_function_pointer) (char *, int);
76 extern FixedOutputHook *FixedOutputTable;
81 * SessionFunctionHook extensions are used for any type of hook for which
82 * the context in which it's being called (which is determined by the event
83 * type) will make it obvious for the hook function to know where to look for
86 typedef struct SessionFunctionHook SessionFunctionHook;
87 struct SessionFunctionHook {
88 SessionFunctionHook *next;
89 void (*h_function_pointer) (void);
92 extern SessionFunctionHook *SessionHookTable;
96 * UserFunctionHook extensions are used for any type of hook which implements
97 * an operation on a user or username (potentially) other than the one
98 * operating the current session.
100 typedef struct UserFunctionHook UserFunctionHook;
101 struct UserFunctionHook {
102 UserFunctionHook *next;
103 void (*h_function_pointer) (struct ctdluser *usbuf);
106 extern UserFunctionHook *UserHookTable;
109 * MessageFunctionHook extensions are used for hooks which implement handlers
110 * for various types of message operations (save, read, etc.)
112 typedef struct MessageFunctionHook MessageFunctionHook;
113 struct MessageFunctionHook {
114 MessageFunctionHook *next;
115 int (*h_function_pointer) (struct CtdlMessage *msg);
118 extern MessageFunctionHook *MessageHookTable;
122 * NetprocFunctionHook extensions are used for hooks which implement handlers
123 * for incoming network messages.
125 typedef struct NetprocFunctionHook NetprocFunctionHook;
126 struct NetprocFunctionHook {
127 NetprocFunctionHook *next;
128 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
130 extern NetprocFunctionHook *NetprocHookTable;
134 * DeleteFunctionHook extensions are used for hooks which get called when a
135 * message is about to be deleted.
137 typedef struct DeleteFunctionHook DeleteFunctionHook;
138 struct DeleteFunctionHook {
139 DeleteFunctionHook *next;
140 void (*h_function_pointer) (char *target_room, long msgnum);
142 extern DeleteFunctionHook *DeleteHookTable;
146 * ExpressMessageFunctionHook extensions are used for hooks which implement
147 * the sending of an instant message through various channels. Any function
148 * registered should return the number of recipients to whom the message was
149 * successfully transmitted.
151 typedef struct XmsgFunctionHook XmsgFunctionHook;
152 struct XmsgFunctionHook {
153 XmsgFunctionHook *next;
154 int (*h_function_pointer) (char *, char *, char *, char *);
157 extern XmsgFunctionHook *XmsgHookTable;
163 * RoomFunctionHook extensions are used for hooks which impliment room
164 * processing functions when new messages are added EG. SIEVE.
166 typedef struct RoomFunctionHook RoomFunctionHook;
167 struct RoomFunctionHook {
168 RoomFunctionHook *next;
169 int (*fcn_ptr) (struct ctdlroom *);
171 extern RoomFunctionHook *RoomHookTable;
175 typedef struct SearchFunctionHook SearchFunctionHook;
176 struct SearchFunctionHook {
177 SearchFunctionHook *next;
178 void (*fcn_ptr) (int *, long **, const char *);
181 extern SearchFunctionHook *SearchFunctionHookTable;
184 CleanupFunctionHook *CleanupHookTable = NULL;
185 CleanupFunctionHook *EVCleanupHookTable = NULL;
186 SessionFunctionHook *SessionHookTable = NULL;
187 UserFunctionHook *UserHookTable = NULL;
188 XmsgFunctionHook *XmsgHookTable = NULL;
189 MessageFunctionHook *MessageHookTable = NULL;
190 NetprocFunctionHook *NetprocHookTable = NULL;
191 DeleteFunctionHook *DeleteHookTable = NULL;
192 ServiceFunctionHook *ServiceHookTable = NULL;
193 FixedOutputHook *FixedOutputTable = NULL;
194 RoomFunctionHook *RoomHookTable = NULL;
195 SearchFunctionHook *SearchFunctionHookTable = NULL;
197 typedef struct ProtoFunctionHook ProtoFunctionHook;
198 struct ProtoFunctionHook {
199 void (*handler) (char *cmdbuf);
204 HashList *ProtoHookList = NULL;
207 #define ERR_PORT (1 << 1)
210 static StrBuf *portlist = NULL;
212 static StrBuf *errormessages = NULL;
215 long DetailErrorFlags;
216 ConstStr Empty = {HKEY("")};
217 char *ErrSubject = "Startup Problems";
218 ConstStr ErrGeneral[] = {
219 {HKEY("Citadel had trouble on starting up. ")},
220 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
221 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
222 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
223 "The errors returned by the system were:\n")},
224 {HKEY("You can recheck the above if you follow this faq item:\n"
225 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
228 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
229 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
230 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
231 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
232 " and stop the program that binds these ports.\n You should eventually remove "
233 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
234 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
237 void LogPrintMessages(long err)
240 StrBuf *List, *DetailList;
241 ConstStr *Short, *Where, *Hint;
244 Message = NewStrBufPlain(NULL,
245 StrLength(portlist) + StrLength(errormessages));
247 DetailErrorFlags = DetailErrorFlags & ~err;
252 Short = &ErrPortShort;
253 Where = &ErrPortWhere;
256 DetailList = errormessages;
266 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
267 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
268 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
269 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
270 StrBufAppendBuf(Message, List, 0);
271 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
272 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
273 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
274 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
275 StrBufAppendBuf(Message, DetailList, 0);
276 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
277 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
279 syslog(LOG_EMERG, "%s", ChrPtr(Message));
280 syslog(LOG_EMERG, "%s", ErrSubject);
281 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
283 FreeStrBuf(&Message);
285 FreeStrBuf(&DetailList);
289 void AddPortError(char *Port, char *ErrorMessage)
293 DetailErrorFlags |= ERR_PORT;
295 len = StrLength(errormessages);
296 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
297 else errormessages = NewStrBuf();
298 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
301 len = StrLength(portlist);
302 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
303 else portlist = NewStrBuf();
304 StrBufAppendBufPlain(portlist, Port, -1, 0);
308 int DLoader_Exec_Cmd(char *cmdbuf)
311 ProtoFunctionHook *p;
313 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
314 p = (ProtoFunctionHook*) vP;
315 p->handler(&cmdbuf[5]);
321 long FourHash(const char *key, long length)
325 const unsigned char *ptr = (const unsigned char*)key;
327 for (i = 0; i < 4; i++, ptr ++)
337 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
340 if (LogDebugEntryTable == NULL)
341 LogDebugEntryTable = NewHash(1, NULL);
342 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
347 Put(LogDebugEntryTable, Name, Len, E, NULL);
350 void CtdlSetDebugLogFacilities(const char **Str, long n)
352 StrBuf *Token = NULL;
359 for (i=0; i < n; i++){
360 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
361 if (strcmp(Str[i], "all") == 0) {
365 Buf = NewStrBufPlain(Str[i], -1);
368 Token = NewStrBufPlain(NULL, StrLength(Buf));
369 while ((ch != StrBufNOTNULL) &&
370 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
371 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
374 LogDebugEntry *E = (LogDebugEntry*)vptr;
387 Pos = GetNewHashPos(LogDebugEntryTable, 0);
388 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
389 LogDebugEntry *E = (LogDebugEntry*)vptr;
396 void cmd_log_get(char *argbuf)
403 if (CtdlAccessCheck(ac_aide)) return;
405 cprintf("%d Log modules enabled:\n", LISTING_FOLLOWS);
407 Pos = GetNewHashPos(LogDebugEntryTable, 0);
409 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
410 LogDebugEntry *E = (LogDebugEntry*)vptr;
411 cprintf("%s: %d\n", ch, *E->LogP);
417 void cmd_log_set(char *argbuf)
422 char which[SIZ] = "";
424 if (CtdlAccessCheck(ac_aide)) return;
426 wlen = extract_token(which, argbuf, 0, '|', sizeof(which));
427 if (wlen < 0) wlen = 0;
428 lset = extract_int(argbuf, 1);
429 if (lset != 0) lset = 1;
430 if (GetHash(LogDebugEntryTable, which, wlen, &vptr) &&
433 LogDebugEntry *E = (LogDebugEntry*)vptr;
435 cprintf("%d %s|%d\n", CIT_OK, which, lset);
438 cprintf("%d Log setting %s not known\n",
442 void CtdlDestroyDebugTable(void)
445 DeleteHash(&LogDebugEntryTable);
448 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
450 ProtoFunctionHook *p;
452 if (ProtoHookList == NULL)
453 ProtoHookList = NewHash (1, FourHash);
456 p = (ProtoFunctionHook *)
457 malloc(sizeof(ProtoFunctionHook));
460 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
463 p->handler = handler;
467 Put(ProtoHookList, cmd, 4, p, NULL);
468 syslog(LOG_INFO, "Registered server command %s (%s)\n", cmd, desc);
471 void CtdlDestroyProtoHooks(void)
474 DeleteHash(&ProtoHookList);
478 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
481 CleanupFunctionHook *newfcn;
483 newfcn = (CleanupFunctionHook *)
484 malloc(sizeof(CleanupFunctionHook));
485 newfcn->next = CleanupHookTable;
486 newfcn->h_function_pointer = fcn_ptr;
487 CleanupHookTable = newfcn;
489 syslog(LOG_INFO, "Registered a new cleanup function\n");
493 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
495 CleanupFunctionHook *cur, *p;
497 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
498 /* This will also remove duplicates if any */
499 while (cur != NULL &&
500 fcn_ptr == cur->h_function_pointer) {
501 syslog(LOG_INFO, "Unregistered cleanup function\n");
503 if (cur == CleanupHookTable) {
504 CleanupHookTable = p;
513 void CtdlDestroyCleanupHooks(void)
515 CleanupFunctionHook *cur, *p;
517 cur = CleanupHookTable;
520 syslog(LOG_INFO, "Destroyed cleanup function\n");
525 CleanupHookTable = NULL;
528 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
531 CleanupFunctionHook *newfcn;
533 newfcn = (CleanupFunctionHook *)
534 malloc(sizeof(CleanupFunctionHook));
535 newfcn->next = EVCleanupHookTable;
536 newfcn->h_function_pointer = fcn_ptr;
537 EVCleanupHookTable = newfcn;
539 syslog(LOG_INFO, "Registered a new cleanup function\n");
543 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
545 CleanupFunctionHook *cur, *p;
547 for (cur = EVCleanupHookTable; cur != NULL; cur = cur->next) {
548 /* This will also remove duplicates if any */
549 while (cur != NULL &&
550 fcn_ptr == cur->h_function_pointer) {
551 syslog(LOG_INFO, "Unregistered cleanup function\n");
553 if (cur == EVCleanupHookTable) {
554 EVCleanupHookTable = p;
563 void CtdlDestroyEVCleanupHooks(void)
565 CleanupFunctionHook *cur, *p;
567 cur = EVCleanupHookTable;
570 syslog(LOG_INFO, "Destroyed cleanup function\n");
575 EVCleanupHookTable = NULL;
579 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
582 SessionFunctionHook *newfcn;
584 newfcn = (SessionFunctionHook *)
585 malloc(sizeof(SessionFunctionHook));
586 newfcn->next = SessionHookTable;
587 newfcn->h_function_pointer = fcn_ptr;
588 newfcn->eventtype = EventType;
589 SessionHookTable = newfcn;
591 syslog(LOG_INFO, "Registered a new session function (type %d)\n",
596 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
598 SessionFunctionHook *cur, *p;
600 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
601 /* This will also remove duplicates if any */
602 while (cur != NULL &&
603 fcn_ptr == cur->h_function_pointer &&
604 EventType == cur->eventtype) {
605 syslog(LOG_INFO, "Unregistered session function (type %d)\n",
608 if (cur == SessionHookTable) {
609 SessionHookTable = p;
617 void CtdlDestroySessionHooks(void)
619 SessionFunctionHook *cur, *p;
621 cur = SessionHookTable;
624 syslog(LOG_INFO, "Destroyed session function\n");
629 SessionHookTable = NULL;
633 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
636 UserFunctionHook *newfcn;
638 newfcn = (UserFunctionHook *)
639 malloc(sizeof(UserFunctionHook));
640 newfcn->next = UserHookTable;
641 newfcn->h_function_pointer = fcn_ptr;
642 newfcn->eventtype = EventType;
643 UserHookTable = newfcn;
645 syslog(LOG_INFO, "Registered a new user function (type %d)\n",
650 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
652 UserFunctionHook *cur, *p;
654 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
655 /* This will also remove duplicates if any */
656 while (cur != NULL &&
657 fcn_ptr == cur->h_function_pointer &&
658 EventType == cur->eventtype) {
659 syslog(LOG_INFO, "Unregistered user function (type %d)\n",
662 if (cur == UserHookTable) {
671 void CtdlDestroyUserHooks(void)
673 UserFunctionHook *cur, *p;
678 syslog(LOG_INFO, "Destroyed user function \n");
683 UserHookTable = NULL;
687 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
691 MessageFunctionHook *newfcn;
693 newfcn = (MessageFunctionHook *)
694 malloc(sizeof(MessageFunctionHook));
695 newfcn->next = MessageHookTable;
696 newfcn->h_function_pointer = handler;
697 newfcn->eventtype = EventType;
698 MessageHookTable = newfcn;
700 syslog(LOG_INFO, "Registered a new message function (type %d)\n",
705 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
708 MessageFunctionHook *cur, *p;
710 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
711 /* This will also remove duplicates if any */
712 while (cur != NULL &&
713 handler == cur->h_function_pointer &&
714 EventType == cur->eventtype) {
715 syslog(LOG_INFO, "Unregistered message function (type %d)\n",
718 if (cur == MessageHookTable) {
719 MessageHookTable = p;
727 void CtdlDestroyMessageHook(void)
729 MessageFunctionHook *cur, *p;
731 cur = MessageHookTable;
734 syslog(LOG_INFO, "Destroyed message function (type %d)\n", cur->eventtype);
739 MessageHookTable = NULL;
743 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
745 RoomFunctionHook *newfcn;
747 newfcn = (RoomFunctionHook *)
748 malloc(sizeof(RoomFunctionHook));
749 newfcn->next = RoomHookTable;
750 newfcn->fcn_ptr = fcn_ptr;
751 RoomHookTable = newfcn;
753 syslog(LOG_INFO, "Registered a new room function\n");
757 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
759 RoomFunctionHook *cur, *p;
761 for (cur = RoomHookTable; cur != NULL; cur = cur->next) {
762 while (cur != NULL && fcn_ptr == cur->fcn_ptr) {
763 syslog(LOG_INFO, "Unregistered room function\n");
765 if (cur == RoomHookTable) {
775 void CtdlDestroyRoomHooks(void)
777 RoomFunctionHook *cur, *p;
782 syslog(LOG_INFO, "Destroyed room function\n");
787 RoomHookTable = NULL;
790 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
792 NetprocFunctionHook *newfcn;
794 newfcn = (NetprocFunctionHook *)
795 malloc(sizeof(NetprocFunctionHook));
796 newfcn->next = NetprocHookTable;
797 newfcn->h_function_pointer = handler;
798 NetprocHookTable = newfcn;
800 syslog(LOG_INFO, "Registered a new netproc function\n");
804 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
806 NetprocFunctionHook *cur, *p;
808 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
809 /* This will also remove duplicates if any */
810 while (cur != NULL &&
811 handler == cur->h_function_pointer ) {
812 syslog(LOG_INFO, "Unregistered netproc function\n");
814 if (cur == NetprocHookTable) {
815 NetprocHookTable = p;
823 void CtdlDestroyNetprocHooks(void)
825 NetprocFunctionHook *cur, *p;
827 cur = NetprocHookTable;
830 syslog(LOG_INFO, "Destroyed netproc function\n");
835 NetprocHookTable = NULL;
839 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
841 DeleteFunctionHook *newfcn;
843 newfcn = (DeleteFunctionHook *)
844 malloc(sizeof(DeleteFunctionHook));
845 newfcn->next = DeleteHookTable;
846 newfcn->h_function_pointer = handler;
847 DeleteHookTable = newfcn;
849 syslog(LOG_INFO, "Registered a new delete function\n");
853 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
855 DeleteFunctionHook *cur, *p;
857 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
858 /* This will also remove duplicates if any */
859 while (cur != NULL &&
860 handler == cur->h_function_pointer ) {
861 syslog(LOG_INFO, "Unregistered delete function\n");
863 if (cur == DeleteHookTable) {
871 void CtdlDestroyDeleteHooks(void)
873 DeleteFunctionHook *cur, *p;
875 cur = DeleteHookTable;
878 syslog(LOG_INFO, "Destroyed delete function\n");
883 DeleteHookTable = NULL;
889 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
891 FixedOutputHook *newfcn;
893 newfcn = (FixedOutputHook *)
894 malloc(sizeof(FixedOutputHook));
895 newfcn->next = FixedOutputTable;
896 newfcn->h_function_pointer = handler;
897 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
898 FixedOutputTable = newfcn;
900 syslog(LOG_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
904 void CtdlUnregisterFixedOutputHook(char *content_type)
906 FixedOutputHook *cur, *p;
908 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
909 /* This will also remove duplicates if any */
910 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
911 syslog(LOG_INFO, "Unregistered fixed output function for %s\n", content_type);
913 if (cur == FixedOutputTable) {
914 FixedOutputTable = p;
922 void CtdlDestroyFixedOutputHooks(void)
924 FixedOutputHook *cur, *p;
926 cur = FixedOutputTable;
929 syslog(LOG_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
935 FixedOutputTable = NULL;
938 /* returns nonzero if we found a hook and used it */
939 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
941 FixedOutputHook *fcn;
943 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
944 if (!strcasecmp(content_type, fcn->content_type)) {
945 (*fcn->h_function_pointer) (content, content_length);
956 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
959 XmsgFunctionHook *newfcn;
961 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
962 newfcn->next = XmsgHookTable;
963 newfcn->order = order;
964 newfcn->h_function_pointer = fcn_ptr;
965 XmsgHookTable = newfcn;
966 syslog(LOG_INFO, "Registered a new x-msg function (priority %d)\n", order);
970 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
972 XmsgFunctionHook *cur, *p;
974 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
975 /* This will also remove duplicates if any */
976 while (cur != NULL &&
977 fcn_ptr == cur->h_function_pointer &&
978 order == cur->order) {
979 syslog(LOG_INFO, "Unregistered x-msg function "
980 "(priority %d)\n", order);
982 if (cur == XmsgHookTable) {
991 void CtdlDestroyXmsgHooks(void)
993 XmsgFunctionHook *cur, *p;
998 syslog(LOG_INFO, "Destroyed x-msg function "
999 "(priority %d)\n", cur->order);
1005 XmsgHookTable = NULL;
1009 void CtdlRegisterServiceHook(int tcp_port,
1011 void (*h_greeting_function) (void),
1012 void (*h_command_function) (void),
1013 void (*h_async_function) (void),
1014 const char *ServiceName)
1016 ServiceFunctionHook *newfcn;
1021 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1022 message = (char*) malloc (SIZ + SIZ);
1024 newfcn->next = ServiceHookTable;
1025 newfcn->tcp_port = tcp_port;
1026 newfcn->sockpath = sockpath;
1027 newfcn->h_greeting_function = h_greeting_function;
1028 newfcn->h_command_function = h_command_function;
1029 newfcn->h_async_function = h_async_function;
1030 newfcn->ServiceName = ServiceName;
1032 if (sockpath != NULL) {
1033 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
1034 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1036 else if (tcp_port <= 0) { /* port -1 to disable */
1037 syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1043 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
1045 config.c_maxsessions,
1047 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
1048 config.c_ip_addr, tcp_port, ServiceName);
1051 if (newfcn->msock > 0) {
1052 ServiceHookTable = newfcn;
1053 strcat(message, "registered.");
1054 syslog(LOG_INFO, "%s\n", message);
1057 AddPortError(message, error);
1058 strcat(message, "FAILED.");
1059 syslog(LOG_CRIT, "%s\n", message);
1066 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1067 void (*h_greeting_function) (void),
1068 void (*h_command_function) (void),
1069 void (*h_async_function) (void)
1072 ServiceFunctionHook *cur, *p;
1074 cur = ServiceHookTable;
1075 while (cur != NULL) {
1076 /* This will also remove duplicates if any */
1077 while (cur != NULL &&
1078 !(sockpath && cur->sockpath &&
1079 strcmp(sockpath, cur->sockpath)) &&
1080 h_greeting_function == cur->h_greeting_function &&
1081 h_command_function == cur->h_command_function &&
1082 h_async_function == cur->h_async_function &&
1083 tcp_port == cur->tcp_port) {
1086 syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1089 } else if (tcp_port) {
1090 syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1092 syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1095 if (cur == ServiceHookTable) {
1096 ServiceHookTable = p;
1105 void CtdlShutdownServiceHooks(void)
1107 /* sort of a duplicate of close_masters() but called earlier */
1108 ServiceFunctionHook *cur;
1110 cur = ServiceHookTable;
1113 if (cur->msock != -1)
1117 if (cur->sockpath != NULL){
1118 syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1121 unlink(cur->sockpath);
1123 syslog(LOG_INFO, "[%s] closing service\n",
1131 void CtdlDestroyServiceHook(void)
1133 ServiceFunctionHook *cur, *p;
1135 cur = ServiceHookTable;
1139 if (cur->sockpath) {
1140 syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1142 unlink(cur->sockpath);
1143 } else if (cur->tcp_port) {
1144 syslog(LOG_INFO, "Closed TCP port %d\n", cur->tcp_port);
1146 syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1152 ServiceHookTable = NULL;
1155 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1157 SearchFunctionHook *newfcn;
1159 if (!name || !fcn_ptr) {
1163 newfcn = (SearchFunctionHook *)
1164 malloc(sizeof(SearchFunctionHook));
1165 newfcn->next = SearchFunctionHookTable;
1166 newfcn->name = name;
1167 newfcn->fcn_ptr = fcn_ptr;
1168 SearchFunctionHookTable = newfcn;
1170 syslog(LOG_INFO, "Registered a new search function (%s)\n", name);
1173 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1175 SearchFunctionHook *cur, *p;
1177 for (cur = SearchFunctionHookTable; cur != NULL; cur = cur->next) {
1178 while (fcn_ptr && (cur->fcn_ptr == fcn_ptr) && name && !strcmp(name, cur->name)) {
1179 syslog(LOG_INFO, "Unregistered search function(%s)\n", name);
1181 if (cur == SearchFunctionHookTable) {
1182 SearchFunctionHookTable = p;
1190 void CtdlDestroySearchHooks(void)
1192 SearchFunctionHook *cur, *p;
1194 cur = SearchFunctionHookTable;
1195 SearchFunctionHookTable = NULL;
1196 while (cur != NULL) {
1203 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1205 SearchFunctionHook *fcn = NULL;
1207 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1208 if (!func_name || !strcmp(func_name, fcn->name)) {
1209 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1217 void PerformSessionHooks(int EventType)
1219 SessionFunctionHook *fcn = NULL;
1221 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1222 if (fcn->eventtype == EventType) {
1223 if (EventType == EVT_TIMER) {
1224 pthread_setspecific(MyConKey, NULL); /* for every hook */
1226 (*fcn->h_function_pointer) ();
1231 void PerformUserHooks(ctdluser *usbuf, int EventType)
1233 UserFunctionHook *fcn = NULL;
1235 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1236 if (fcn->eventtype == EventType) {
1237 (*fcn->h_function_pointer) (usbuf);
1242 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
1244 MessageFunctionHook *fcn = NULL;
1245 int total_retval = 0;
1247 /* Other code may elect to protect this message from server-side
1248 * handlers; if this is the case, don't do anything.
1249 syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1251 if (msg->cm_flags & CM_SKIP_HOOKS) {
1252 syslog(LOG_DEBUG, "Skipping hooks\n");
1256 /* Otherwise, run all the hooks appropriate to this event type.
1258 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1259 if (fcn->eventtype == EventType) {
1260 total_retval = total_retval + (*fcn->h_function_pointer) (msg);
1264 /* Return the sum of the return codes from the hook functions. If
1265 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1266 * the save operation to abort.
1268 return total_retval;
1272 int PerformRoomHooks(struct ctdlroom *target_room)
1274 RoomFunctionHook *fcn;
1275 int total_retval = 0;
1277 syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1279 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1280 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1283 /* Return the sum of the return codes from the hook functions.
1285 return total_retval;
1289 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1291 NetprocFunctionHook *fcn;
1292 int total_retval = 0;
1294 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1295 total_retval = total_retval +
1296 (*fcn->h_function_pointer) (msg, target_room);
1299 /* Return the sum of the return codes from the hook functions.
1300 * A nonzero return code will cause the message to *not* be imported.
1302 return total_retval;
1306 void PerformDeleteHooks(char *room, long msgnum)
1308 DeleteFunctionHook *fcn;
1310 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1311 (*fcn->h_function_pointer) (room, msgnum);
1319 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1321 XmsgFunctionHook *fcn;
1325 for (p=0; p<MAX_XMSG_PRI; ++p) {
1326 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1327 if (fcn->order == p) {
1329 (*fcn->h_function_pointer)
1330 (sender, sender_email, recp, msg);
1333 /* Break out of the loop if a higher-priority function
1334 * successfully delivered the message. This prevents duplicate
1335 * deliveries to local users simultaneously signed onto
1338 if (total_sent) break;
1345 * Dirty hack until we impliment a hook mechanism for this
1347 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1350 CtdlStartTLS (ok_response, nosup_response, error_response);
1355 CTDL_MODULE_INIT(modules)
1358 CtdlRegisterProtoHook(cmd_log_get, "LOGP", "Print Log-parameters");
1359 CtdlRegisterProtoHook(cmd_log_set, "LOGS", "Set Log-parameters");