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 {
59 HashList *LogDebugEntryTable = NULL;
61 typedef struct LogFunctionHook LogFunctionHook;
62 struct LogFunctionHook {
63 LogFunctionHook *next;
65 void (*h_function_pointer) (char *);
67 extern LogFunctionHook *LogHookTable;
69 typedef struct FixedOutputHook FixedOutputHook;
70 struct FixedOutputHook {
71 FixedOutputHook *next;
72 char content_type[64];
73 void (*h_function_pointer) (char *, int);
75 extern FixedOutputHook *FixedOutputTable;
80 * SessionFunctionHook extensions are used for any type of hook for which
81 * the context in which it's being called (which is determined by the event
82 * type) will make it obvious for the hook function to know where to look for
85 typedef struct SessionFunctionHook SessionFunctionHook;
86 struct SessionFunctionHook {
87 SessionFunctionHook *next;
88 void (*h_function_pointer) (void);
91 extern SessionFunctionHook *SessionHookTable;
95 * UserFunctionHook extensions are used for any type of hook which implements
96 * an operation on a user or username (potentially) other than the one
97 * operating the current session.
99 typedef struct UserFunctionHook UserFunctionHook;
100 struct UserFunctionHook {
101 UserFunctionHook *next;
102 void (*h_function_pointer) (struct ctdluser *usbuf);
105 extern UserFunctionHook *UserHookTable;
108 * MessageFunctionHook extensions are used for hooks which implement handlers
109 * for various types of message operations (save, read, etc.)
111 typedef struct MessageFunctionHook MessageFunctionHook;
112 struct MessageFunctionHook {
113 MessageFunctionHook *next;
114 int (*h_function_pointer) (struct CtdlMessage *msg);
117 extern MessageFunctionHook *MessageHookTable;
121 * NetprocFunctionHook extensions are used for hooks which implement handlers
122 * for incoming network messages.
124 typedef struct NetprocFunctionHook NetprocFunctionHook;
125 struct NetprocFunctionHook {
126 NetprocFunctionHook *next;
127 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
129 extern NetprocFunctionHook *NetprocHookTable;
133 * DeleteFunctionHook extensions are used for hooks which get called when a
134 * message is about to be deleted.
136 typedef struct DeleteFunctionHook DeleteFunctionHook;
137 struct DeleteFunctionHook {
138 DeleteFunctionHook *next;
139 void (*h_function_pointer) (char *target_room, long msgnum);
141 extern DeleteFunctionHook *DeleteHookTable;
145 * ExpressMessageFunctionHook extensions are used for hooks which implement
146 * the sending of an instant message through various channels. Any function
147 * registered should return the number of recipients to whom the message was
148 * successfully transmitted.
150 typedef struct XmsgFunctionHook XmsgFunctionHook;
151 struct XmsgFunctionHook {
152 XmsgFunctionHook *next;
153 int (*h_function_pointer) (char *, char *, char *, char *);
156 extern XmsgFunctionHook *XmsgHookTable;
162 * RoomFunctionHook extensions are used for hooks which impliment room
163 * processing functions when new messages are added EG. SIEVE.
165 typedef struct RoomFunctionHook RoomFunctionHook;
166 struct RoomFunctionHook {
167 RoomFunctionHook *next;
168 int (*fcn_ptr) (struct ctdlroom *);
170 extern RoomFunctionHook *RoomHookTable;
174 typedef struct SearchFunctionHook SearchFunctionHook;
175 struct SearchFunctionHook {
176 SearchFunctionHook *next;
177 void (*fcn_ptr) (int *, long **, const char *);
180 extern SearchFunctionHook *SearchFunctionHookTable;
183 CleanupFunctionHook *CleanupHookTable = NULL;
184 CleanupFunctionHook *EVCleanupHookTable = NULL;
185 SessionFunctionHook *SessionHookTable = NULL;
186 UserFunctionHook *UserHookTable = NULL;
187 XmsgFunctionHook *XmsgHookTable = NULL;
188 MessageFunctionHook *MessageHookTable = NULL;
189 NetprocFunctionHook *NetprocHookTable = NULL;
190 DeleteFunctionHook *DeleteHookTable = NULL;
191 ServiceFunctionHook *ServiceHookTable = NULL;
192 FixedOutputHook *FixedOutputTable = NULL;
193 RoomFunctionHook *RoomHookTable = NULL;
194 SearchFunctionHook *SearchFunctionHookTable = NULL;
196 typedef struct ProtoFunctionHook ProtoFunctionHook;
197 struct ProtoFunctionHook {
198 void (*handler) (char *cmdbuf);
203 HashList *ProtoHookList = NULL;
206 #define ERR_PORT (1 << 1)
209 static StrBuf *portlist = NULL;
211 static StrBuf *errormessages = NULL;
214 long DetailErrorFlags;
215 ConstStr Empty = {HKEY("")};
216 char *ErrSubject = "Startup Problems";
217 ConstStr ErrGeneral[] = {
218 {HKEY("Citadel had trouble on starting up. ")},
219 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
220 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
221 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
222 "The errors returned by the system were:\n")},
223 {HKEY("You can recheck the above if you follow this faq item:\n"
224 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
227 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
228 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
229 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
230 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
231 " and stop the program that binds these ports.\n You should eventually remove "
232 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
233 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
236 void LogPrintMessages(long err)
239 StrBuf *List, *DetailList;
240 ConstStr *Short, *Where, *Hint;
243 Message = NewStrBufPlain(NULL,
244 StrLength(portlist) + StrLength(errormessages));
246 DetailErrorFlags = DetailErrorFlags & ~err;
251 Short = &ErrPortShort;
252 Where = &ErrPortWhere;
255 DetailList = errormessages;
265 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
266 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
267 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
268 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
269 StrBufAppendBuf(Message, List, 0);
270 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
271 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
272 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
273 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
274 StrBufAppendBuf(Message, DetailList, 0);
275 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
276 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
278 syslog(LOG_EMERG, "%s", ChrPtr(Message));
279 syslog(LOG_EMERG, "%s", ErrSubject);
280 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
282 FreeStrBuf(&Message);
284 FreeStrBuf(&DetailList);
288 void AddPortError(char *Port, char *ErrorMessage)
292 DetailErrorFlags |= ERR_PORT;
294 len = StrLength(errormessages);
295 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
296 else errormessages = NewStrBuf();
297 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
300 len = StrLength(portlist);
301 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
302 else portlist = NewStrBuf();
303 StrBufAppendBufPlain(portlist, Port, -1, 0);
307 int DLoader_Exec_Cmd(char *cmdbuf)
310 ProtoFunctionHook *p;
312 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
313 p = (ProtoFunctionHook*) vP;
314 p->handler(&cmdbuf[5]);
320 long FourHash(const char *key, long length)
324 const unsigned char *ptr = (const unsigned char*)key;
326 for (i = 0; i < 4; i++, ptr ++)
336 typedef struct __LogDebugEntry {
341 HashList *LogDebugEntryTable = NULL;
343 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F)
346 if (LogDebugEntryTable == NULL)
347 LogDebugEntryTable = NewHash(1, NULL);
348 E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
352 Put(LogDebugEntryTable, Name, Len, E, NULL);
355 void CtdlSetDebugLogFacilities(const char **Str, long n)
357 StrBuf *Token = NULL;
364 for (i=0; i < n; i++){
365 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
366 if (strcmp(Str[i], "all") == 0) {
370 Buf = NewStrBufPlain(Str[i], -1);
373 Token = NewStrBufPlain(NULL, StrLength(Buf));
374 while ((ch != StrBufNOTNULL) &&
375 StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
376 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) &&
379 LogDebugEntry *E = (LogDebugEntry*)vptr;
392 Pos = GetNewHashPos(LogDebugEntryTable, 0);
393 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
394 LogDebugEntry *E = (LogDebugEntry*)vptr;
401 void CtdlDestroyDebugTable(void)
404 DeleteHash(&LogDebugEntryTable);
407 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
409 ProtoFunctionHook *p;
411 if (ProtoHookList == NULL)
412 ProtoHookList = NewHash (1, FourHash);
415 p = (ProtoFunctionHook *)
416 malloc(sizeof(ProtoFunctionHook));
419 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
422 p->handler = handler;
426 Put(ProtoHookList, cmd, 4, p, NULL);
427 syslog(LOG_INFO, "Registered server command %s (%s)\n", cmd, desc);
430 void CtdlDestroyProtoHooks(void)
433 DeleteHash(&ProtoHookList);
437 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
440 CleanupFunctionHook *newfcn;
442 newfcn = (CleanupFunctionHook *)
443 malloc(sizeof(CleanupFunctionHook));
444 newfcn->next = CleanupHookTable;
445 newfcn->h_function_pointer = fcn_ptr;
446 CleanupHookTable = newfcn;
448 syslog(LOG_INFO, "Registered a new cleanup function\n");
452 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
454 CleanupFunctionHook *cur, *p;
456 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
457 /* This will also remove duplicates if any */
458 while (cur != NULL &&
459 fcn_ptr == cur->h_function_pointer) {
460 syslog(LOG_INFO, "Unregistered cleanup function\n");
462 if (cur == CleanupHookTable) {
463 CleanupHookTable = p;
472 void CtdlDestroyCleanupHooks(void)
474 CleanupFunctionHook *cur, *p;
476 cur = CleanupHookTable;
479 syslog(LOG_INFO, "Destroyed cleanup function\n");
484 CleanupHookTable = NULL;
487 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
490 CleanupFunctionHook *newfcn;
492 newfcn = (CleanupFunctionHook *)
493 malloc(sizeof(CleanupFunctionHook));
494 newfcn->next = EVCleanupHookTable;
495 newfcn->h_function_pointer = fcn_ptr;
496 EVCleanupHookTable = newfcn;
498 syslog(LOG_INFO, "Registered a new cleanup function\n");
502 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
504 CleanupFunctionHook *cur, *p;
506 for (cur = EVCleanupHookTable; cur != NULL; cur = cur->next) {
507 /* This will also remove duplicates if any */
508 while (cur != NULL &&
509 fcn_ptr == cur->h_function_pointer) {
510 syslog(LOG_INFO, "Unregistered cleanup function\n");
512 if (cur == EVCleanupHookTable) {
513 EVCleanupHookTable = p;
522 void CtdlDestroyEVCleanupHooks(void)
524 CleanupFunctionHook *cur, *p;
526 cur = EVCleanupHookTable;
529 syslog(LOG_INFO, "Destroyed cleanup function\n");
534 EVCleanupHookTable = NULL;
538 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
541 SessionFunctionHook *newfcn;
543 newfcn = (SessionFunctionHook *)
544 malloc(sizeof(SessionFunctionHook));
545 newfcn->next = SessionHookTable;
546 newfcn->h_function_pointer = fcn_ptr;
547 newfcn->eventtype = EventType;
548 SessionHookTable = newfcn;
550 syslog(LOG_INFO, "Registered a new session function (type %d)\n",
555 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
557 SessionFunctionHook *cur, *p;
559 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
560 /* This will also remove duplicates if any */
561 while (cur != NULL &&
562 fcn_ptr == cur->h_function_pointer &&
563 EventType == cur->eventtype) {
564 syslog(LOG_INFO, "Unregistered session function (type %d)\n",
567 if (cur == SessionHookTable) {
568 SessionHookTable = p;
576 void CtdlDestroySessionHooks(void)
578 SessionFunctionHook *cur, *p;
580 cur = SessionHookTable;
583 syslog(LOG_INFO, "Destroyed session function\n");
588 SessionHookTable = NULL;
592 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
595 UserFunctionHook *newfcn;
597 newfcn = (UserFunctionHook *)
598 malloc(sizeof(UserFunctionHook));
599 newfcn->next = UserHookTable;
600 newfcn->h_function_pointer = fcn_ptr;
601 newfcn->eventtype = EventType;
602 UserHookTable = newfcn;
604 syslog(LOG_INFO, "Registered a new user function (type %d)\n",
609 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
611 UserFunctionHook *cur, *p;
613 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
614 /* This will also remove duplicates if any */
615 while (cur != NULL &&
616 fcn_ptr == cur->h_function_pointer &&
617 EventType == cur->eventtype) {
618 syslog(LOG_INFO, "Unregistered user function (type %d)\n",
621 if (cur == UserHookTable) {
630 void CtdlDestroyUserHooks(void)
632 UserFunctionHook *cur, *p;
637 syslog(LOG_INFO, "Destroyed user function \n");
642 UserHookTable = NULL;
646 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
650 MessageFunctionHook *newfcn;
652 newfcn = (MessageFunctionHook *)
653 malloc(sizeof(MessageFunctionHook));
654 newfcn->next = MessageHookTable;
655 newfcn->h_function_pointer = handler;
656 newfcn->eventtype = EventType;
657 MessageHookTable = newfcn;
659 syslog(LOG_INFO, "Registered a new message function (type %d)\n",
664 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
667 MessageFunctionHook *cur, *p;
669 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
670 /* This will also remove duplicates if any */
671 while (cur != NULL &&
672 handler == cur->h_function_pointer &&
673 EventType == cur->eventtype) {
674 syslog(LOG_INFO, "Unregistered message function (type %d)\n",
677 if (cur == MessageHookTable) {
678 MessageHookTable = p;
686 void CtdlDestroyMessageHook(void)
688 MessageFunctionHook *cur, *p;
690 cur = MessageHookTable;
693 syslog(LOG_INFO, "Destroyed message function (type %d)\n", cur->eventtype);
698 MessageHookTable = NULL;
702 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
704 RoomFunctionHook *newfcn;
706 newfcn = (RoomFunctionHook *)
707 malloc(sizeof(RoomFunctionHook));
708 newfcn->next = RoomHookTable;
709 newfcn->fcn_ptr = fcn_ptr;
710 RoomHookTable = newfcn;
712 syslog(LOG_INFO, "Registered a new room function\n");
716 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
718 RoomFunctionHook *cur, *p;
720 for (cur = RoomHookTable; cur != NULL; cur = cur->next) {
721 while (cur != NULL && fcn_ptr == cur->fcn_ptr) {
722 syslog(LOG_INFO, "Unregistered room function\n");
724 if (cur == RoomHookTable) {
734 void CtdlDestroyRoomHooks(void)
736 RoomFunctionHook *cur, *p;
741 syslog(LOG_INFO, "Destroyed room function\n");
746 RoomHookTable = NULL;
749 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
751 NetprocFunctionHook *newfcn;
753 newfcn = (NetprocFunctionHook *)
754 malloc(sizeof(NetprocFunctionHook));
755 newfcn->next = NetprocHookTable;
756 newfcn->h_function_pointer = handler;
757 NetprocHookTable = newfcn;
759 syslog(LOG_INFO, "Registered a new netproc function\n");
763 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
765 NetprocFunctionHook *cur, *p;
767 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
768 /* This will also remove duplicates if any */
769 while (cur != NULL &&
770 handler == cur->h_function_pointer ) {
771 syslog(LOG_INFO, "Unregistered netproc function\n");
773 if (cur == NetprocHookTable) {
774 NetprocHookTable = p;
782 void CtdlDestroyNetprocHooks(void)
784 NetprocFunctionHook *cur, *p;
786 cur = NetprocHookTable;
789 syslog(LOG_INFO, "Destroyed netproc function\n");
794 NetprocHookTable = NULL;
798 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
800 DeleteFunctionHook *newfcn;
802 newfcn = (DeleteFunctionHook *)
803 malloc(sizeof(DeleteFunctionHook));
804 newfcn->next = DeleteHookTable;
805 newfcn->h_function_pointer = handler;
806 DeleteHookTable = newfcn;
808 syslog(LOG_INFO, "Registered a new delete function\n");
812 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
814 DeleteFunctionHook *cur, *p;
816 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
817 /* This will also remove duplicates if any */
818 while (cur != NULL &&
819 handler == cur->h_function_pointer ) {
820 syslog(LOG_INFO, "Unregistered delete function\n");
822 if (cur == DeleteHookTable) {
830 void CtdlDestroyDeleteHooks(void)
832 DeleteFunctionHook *cur, *p;
834 cur = DeleteHookTable;
837 syslog(LOG_INFO, "Destroyed delete function\n");
842 DeleteHookTable = NULL;
848 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
850 FixedOutputHook *newfcn;
852 newfcn = (FixedOutputHook *)
853 malloc(sizeof(FixedOutputHook));
854 newfcn->next = FixedOutputTable;
855 newfcn->h_function_pointer = handler;
856 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
857 FixedOutputTable = newfcn;
859 syslog(LOG_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
863 void CtdlUnregisterFixedOutputHook(char *content_type)
865 FixedOutputHook *cur, *p;
867 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
868 /* This will also remove duplicates if any */
869 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
870 syslog(LOG_INFO, "Unregistered fixed output function for %s\n", content_type);
872 if (cur == FixedOutputTable) {
873 FixedOutputTable = p;
881 void CtdlDestroyFixedOutputHooks(void)
883 FixedOutputHook *cur, *p;
885 cur = FixedOutputTable;
888 syslog(LOG_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
894 FixedOutputTable = NULL;
897 /* returns nonzero if we found a hook and used it */
898 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
900 FixedOutputHook *fcn;
902 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
903 if (!strcasecmp(content_type, fcn->content_type)) {
904 (*fcn->h_function_pointer) (content, content_length);
915 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
918 XmsgFunctionHook *newfcn;
920 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
921 newfcn->next = XmsgHookTable;
922 newfcn->order = order;
923 newfcn->h_function_pointer = fcn_ptr;
924 XmsgHookTable = newfcn;
925 syslog(LOG_INFO, "Registered a new x-msg function (priority %d)\n", order);
929 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
931 XmsgFunctionHook *cur, *p;
933 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
934 /* This will also remove duplicates if any */
935 while (cur != NULL &&
936 fcn_ptr == cur->h_function_pointer &&
937 order == cur->order) {
938 syslog(LOG_INFO, "Unregistered x-msg function "
939 "(priority %d)\n", order);
941 if (cur == XmsgHookTable) {
950 void CtdlDestroyXmsgHooks(void)
952 XmsgFunctionHook *cur, *p;
957 syslog(LOG_INFO, "Destroyed x-msg function "
958 "(priority %d)\n", cur->order);
964 XmsgHookTable = NULL;
968 void CtdlRegisterServiceHook(int tcp_port,
970 void (*h_greeting_function) (void),
971 void (*h_command_function) (void),
972 void (*h_async_function) (void),
973 const char *ServiceName)
975 ServiceFunctionHook *newfcn;
980 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
981 message = (char*) malloc (SIZ + SIZ);
983 newfcn->next = ServiceHookTable;
984 newfcn->tcp_port = tcp_port;
985 newfcn->sockpath = sockpath;
986 newfcn->h_greeting_function = h_greeting_function;
987 newfcn->h_command_function = h_command_function;
988 newfcn->h_async_function = h_async_function;
989 newfcn->ServiceName = ServiceName;
991 if (sockpath != NULL) {
992 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
993 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
995 else if (tcp_port <= 0) { /* port -1 to disable */
996 syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1002 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
1004 config.c_maxsessions,
1006 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
1007 config.c_ip_addr, tcp_port, ServiceName);
1010 if (newfcn->msock > 0) {
1011 ServiceHookTable = newfcn;
1012 strcat(message, "registered.");
1013 syslog(LOG_INFO, "%s\n", message);
1016 AddPortError(message, error);
1017 strcat(message, "FAILED.");
1018 syslog(LOG_CRIT, "%s\n", message);
1025 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1026 void (*h_greeting_function) (void),
1027 void (*h_command_function) (void),
1028 void (*h_async_function) (void)
1031 ServiceFunctionHook *cur, *p;
1033 cur = ServiceHookTable;
1034 while (cur != NULL) {
1035 /* This will also remove duplicates if any */
1036 while (cur != NULL &&
1037 !(sockpath && cur->sockpath &&
1038 strcmp(sockpath, cur->sockpath)) &&
1039 h_greeting_function == cur->h_greeting_function &&
1040 h_command_function == cur->h_command_function &&
1041 h_async_function == cur->h_async_function &&
1042 tcp_port == cur->tcp_port) {
1045 syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1048 } else if (tcp_port) {
1049 syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1051 syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1054 if (cur == ServiceHookTable) {
1055 ServiceHookTable = p;
1064 void CtdlShutdownServiceHooks(void)
1066 /* sort of a duplicate of close_masters() but called earlier */
1067 ServiceFunctionHook *cur;
1069 cur = ServiceHookTable;
1072 if (cur->msock != -1)
1076 if (cur->sockpath != NULL){
1077 syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1080 unlink(cur->sockpath);
1082 syslog(LOG_INFO, "[%s] closing service\n",
1090 void CtdlDestroyServiceHook(void)
1092 ServiceFunctionHook *cur, *p;
1094 cur = ServiceHookTable;
1098 if (cur->sockpath) {
1099 syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1101 unlink(cur->sockpath);
1102 } else if (cur->tcp_port) {
1103 syslog(LOG_INFO, "Closed TCP port %d\n", cur->tcp_port);
1105 syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1111 ServiceHookTable = NULL;
1114 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1116 SearchFunctionHook *newfcn;
1118 if (!name || !fcn_ptr) {
1122 newfcn = (SearchFunctionHook *)
1123 malloc(sizeof(SearchFunctionHook));
1124 newfcn->next = SearchFunctionHookTable;
1125 newfcn->name = name;
1126 newfcn->fcn_ptr = fcn_ptr;
1127 SearchFunctionHookTable = newfcn;
1129 syslog(LOG_INFO, "Registered a new search function (%s)\n", name);
1132 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1134 SearchFunctionHook *cur, *p;
1136 for (cur = SearchFunctionHookTable; cur != NULL; cur = cur->next) {
1137 while (fcn_ptr && (cur->fcn_ptr == fcn_ptr) && name && !strcmp(name, cur->name)) {
1138 syslog(LOG_INFO, "Unregistered search function(%s)\n", name);
1140 if (cur == SearchFunctionHookTable) {
1141 SearchFunctionHookTable = p;
1149 void CtdlDestroySearchHooks(void)
1151 SearchFunctionHook *cur, *p;
1153 cur = SearchFunctionHookTable;
1154 SearchFunctionHookTable = NULL;
1155 while (cur != NULL) {
1162 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1164 SearchFunctionHook *fcn = NULL;
1166 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1167 if (!func_name || !strcmp(func_name, fcn->name)) {
1168 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1176 void PerformSessionHooks(int EventType)
1178 SessionFunctionHook *fcn = NULL;
1180 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1181 if (fcn->eventtype == EventType) {
1182 if (EventType == EVT_TIMER) {
1183 pthread_setspecific(MyConKey, NULL); /* for every hook */
1185 (*fcn->h_function_pointer) ();
1190 void PerformUserHooks(ctdluser *usbuf, int EventType)
1192 UserFunctionHook *fcn = NULL;
1194 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1195 if (fcn->eventtype == EventType) {
1196 (*fcn->h_function_pointer) (usbuf);
1201 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
1203 MessageFunctionHook *fcn = NULL;
1204 int total_retval = 0;
1206 /* Other code may elect to protect this message from server-side
1207 * handlers; if this is the case, don't do anything.
1208 syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1210 if (msg->cm_flags & CM_SKIP_HOOKS) {
1211 syslog(LOG_DEBUG, "Skipping hooks\n");
1215 /* Otherwise, run all the hooks appropriate to this event type.
1217 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1218 if (fcn->eventtype == EventType) {
1219 total_retval = total_retval + (*fcn->h_function_pointer) (msg);
1223 /* Return the sum of the return codes from the hook functions. If
1224 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1225 * the save operation to abort.
1227 return total_retval;
1231 int PerformRoomHooks(struct ctdlroom *target_room)
1233 RoomFunctionHook *fcn;
1234 int total_retval = 0;
1236 syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1238 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1239 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1242 /* Return the sum of the return codes from the hook functions.
1244 return total_retval;
1248 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1250 NetprocFunctionHook *fcn;
1251 int total_retval = 0;
1253 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1254 total_retval = total_retval +
1255 (*fcn->h_function_pointer) (msg, target_room);
1258 /* Return the sum of the return codes from the hook functions.
1259 * A nonzero return code will cause the message to *not* be imported.
1261 return total_retval;
1265 void PerformDeleteHooks(char *room, long msgnum)
1267 DeleteFunctionHook *fcn;
1269 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1270 (*fcn->h_function_pointer) (room, msgnum);
1278 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1280 XmsgFunctionHook *fcn;
1284 for (p=0; p<MAX_XMSG_PRI; ++p) {
1285 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1286 if (fcn->order == p) {
1288 (*fcn->h_function_pointer)
1289 (sender, sender_email, recp, msg);
1292 /* Break out of the loop if a higher-priority function
1293 * successfully delivered the message. This prevents duplicate
1294 * deliveries to local users simultaneously signed onto
1297 if (total_sent) break;
1304 * Dirty hack until we impliment a hook mechanism for this
1306 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1309 CtdlStartTLS (ok_response, nosup_response, error_response);