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
56 typedef struct LogFunctionHook LogFunctionHook;
57 struct LogFunctionHook {
58 LogFunctionHook *next;
60 void (*h_function_pointer) (char *);
62 extern LogFunctionHook *LogHookTable;
65 typedef struct FixedOutputHook FixedOutputHook;
66 struct FixedOutputHook {
67 FixedOutputHook *next;
68 char content_type[64];
69 void (*h_function_pointer) (char *, int);
71 extern FixedOutputHook *FixedOutputTable;
76 * SessionFunctionHook extensions are used for any type of hook for which
77 * the context in which it's being called (which is determined by the event
78 * type) will make it obvious for the hook function to know where to look for
81 typedef struct SessionFunctionHook SessionFunctionHook;
82 struct SessionFunctionHook {
83 SessionFunctionHook *next;
84 void (*h_function_pointer) (void);
87 extern SessionFunctionHook *SessionHookTable;
91 * UserFunctionHook extensions are used for any type of hook which implements
92 * an operation on a user or username (potentially) other than the one
93 * operating the current session.
95 typedef struct UserFunctionHook UserFunctionHook;
96 struct UserFunctionHook {
97 UserFunctionHook *next;
98 void (*h_function_pointer) (struct ctdluser *usbuf);
101 extern UserFunctionHook *UserHookTable;
104 * MessageFunctionHook extensions are used for hooks which implement handlers
105 * for various types of message operations (save, read, etc.)
107 typedef struct MessageFunctionHook MessageFunctionHook;
108 struct MessageFunctionHook {
109 MessageFunctionHook *next;
110 int (*h_function_pointer) (struct CtdlMessage *msg);
113 extern MessageFunctionHook *MessageHookTable;
117 * NetprocFunctionHook extensions are used for hooks which implement handlers
118 * for incoming network messages.
120 typedef struct NetprocFunctionHook NetprocFunctionHook;
121 struct NetprocFunctionHook {
122 NetprocFunctionHook *next;
123 int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
125 extern NetprocFunctionHook *NetprocHookTable;
129 * DeleteFunctionHook extensions are used for hooks which get called when a
130 * message is about to be deleted.
132 typedef struct DeleteFunctionHook DeleteFunctionHook;
133 struct DeleteFunctionHook {
134 DeleteFunctionHook *next;
135 void (*h_function_pointer) (char *target_room, long msgnum);
137 extern DeleteFunctionHook *DeleteHookTable;
141 * ExpressMessageFunctionHook extensions are used for hooks which implement
142 * the sending of an instant message through various channels. Any function
143 * registered should return the number of recipients to whom the message was
144 * successfully transmitted.
146 typedef struct XmsgFunctionHook XmsgFunctionHook;
147 struct XmsgFunctionHook {
148 XmsgFunctionHook *next;
149 int (*h_function_pointer) (char *, char *, char *, char *);
152 extern XmsgFunctionHook *XmsgHookTable;
158 * RoomFunctionHook extensions are used for hooks which impliment room
159 * processing functions when new messages are added EG. SIEVE.
161 typedef struct RoomFunctionHook RoomFunctionHook;
162 struct RoomFunctionHook {
163 RoomFunctionHook *next;
164 int (*fcn_ptr) (struct ctdlroom *);
166 extern RoomFunctionHook *RoomHookTable;
170 typedef struct SearchFunctionHook SearchFunctionHook;
171 struct SearchFunctionHook {
172 SearchFunctionHook *next;
173 void (*fcn_ptr) (int *, long **, const char *);
176 extern SearchFunctionHook *SearchFunctionHookTable;
179 CleanupFunctionHook *CleanupHookTable = NULL;
180 CleanupFunctionHook *EVCleanupHookTable = NULL;
181 SessionFunctionHook *SessionHookTable = NULL;
182 UserFunctionHook *UserHookTable = NULL;
183 XmsgFunctionHook *XmsgHookTable = NULL;
184 MessageFunctionHook *MessageHookTable = NULL;
185 NetprocFunctionHook *NetprocHookTable = NULL;
186 DeleteFunctionHook *DeleteHookTable = NULL;
187 ServiceFunctionHook *ServiceHookTable = NULL;
188 FixedOutputHook *FixedOutputTable = NULL;
189 RoomFunctionHook *RoomHookTable = NULL;
190 SearchFunctionHook *SearchFunctionHookTable = NULL;
192 typedef struct ProtoFunctionHook ProtoFunctionHook;
193 struct ProtoFunctionHook {
194 void (*handler) (char *cmdbuf);
199 HashList *ProtoHookList = NULL;
202 #define ERR_PORT (1 << 1)
205 static StrBuf *portlist = NULL;
207 static StrBuf *errormessages = NULL;
210 long DetailErrorFlags;
211 ConstStr Empty = {HKEY("")};
212 char *ErrSubject = "Startup Problems";
213 ConstStr ErrGeneral[] = {
214 {HKEY("Citadel had trouble on starting up. ")},
215 {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
216 "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
217 {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
218 "The errors returned by the system were:\n")},
219 {HKEY("You can recheck the above if you follow this faq item:\n"
220 "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
223 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
224 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
225 ConstStr ErrPortHint = { HKEY("If you want citadel to provide you with that functionality, "
226 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
227 " and stop the program that binds these ports.\n You should eventually remove "
228 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
229 " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
232 void LogPrintMessages(long err)
235 StrBuf *List, *DetailList;
236 ConstStr *Short, *Where, *Hint;
239 Message = NewStrBufPlain(NULL,
240 StrLength(portlist) + StrLength(errormessages));
242 DetailErrorFlags = DetailErrorFlags & ~err;
247 Short = &ErrPortShort;
248 Where = &ErrPortWhere;
251 DetailList = errormessages;
261 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
262 StrBufAppendBufPlain(Message, CKEY(*Short), 0);
263 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
264 StrBufAppendBufPlain(Message, CKEY(*Where), 0);
265 StrBufAppendBuf(Message, List, 0);
266 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
267 StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
268 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
269 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
270 StrBufAppendBuf(Message, DetailList, 0);
271 StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
272 StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
274 syslog(LOG_EMERG, "%s", ChrPtr(Message));
275 syslog(LOG_EMERG, "%s", ErrSubject);
276 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
278 FreeStrBuf(&Message);
280 FreeStrBuf(&DetailList);
284 void AddPortError(char *Port, char *ErrorMessage)
288 DetailErrorFlags |= ERR_PORT;
290 len = StrLength(errormessages);
291 if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
292 else errormessages = NewStrBuf();
293 StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
296 len = StrLength(portlist);
297 if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
298 else portlist = NewStrBuf();
299 StrBufAppendBufPlain(portlist, Port, -1, 0);
303 int DLoader_Exec_Cmd(char *cmdbuf)
306 ProtoFunctionHook *p;
308 if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
309 p = (ProtoFunctionHook*) vP;
310 p->handler(&cmdbuf[5]);
316 long FourHash(const char *key, long length)
320 const unsigned char *ptr = (const unsigned char*)key;
322 for (i = 0; i < 4; i++, ptr ++)
332 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
334 ProtoFunctionHook *p;
336 if (ProtoHookList == NULL)
337 ProtoHookList = NewHash (1, FourHash);
340 p = (ProtoFunctionHook *)
341 malloc(sizeof(ProtoFunctionHook));
344 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
347 p->handler = handler;
351 Put(ProtoHookList, cmd, 4, p, NULL);
352 syslog(LOG_INFO, "Registered server command %s (%s)\n", cmd, desc);
355 void CtdlDestroyProtoHooks(void)
358 DeleteHash(&ProtoHookList);
362 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
365 CleanupFunctionHook *newfcn;
367 newfcn = (CleanupFunctionHook *)
368 malloc(sizeof(CleanupFunctionHook));
369 newfcn->next = CleanupHookTable;
370 newfcn->h_function_pointer = fcn_ptr;
371 CleanupHookTable = newfcn;
373 syslog(LOG_INFO, "Registered a new cleanup function\n");
377 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
379 CleanupFunctionHook *cur, *p;
381 for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
382 /* This will also remove duplicates if any */
383 while (cur != NULL &&
384 fcn_ptr == cur->h_function_pointer) {
385 syslog(LOG_INFO, "Unregistered cleanup function\n");
387 if (cur == CleanupHookTable) {
388 CleanupHookTable = p;
397 void CtdlDestroyCleanupHooks(void)
399 CleanupFunctionHook *cur, *p;
401 cur = CleanupHookTable;
404 syslog(LOG_INFO, "Destroyed cleanup function\n");
409 CleanupHookTable = NULL;
412 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
415 CleanupFunctionHook *newfcn;
417 newfcn = (CleanupFunctionHook *)
418 malloc(sizeof(CleanupFunctionHook));
419 newfcn->next = EVCleanupHookTable;
420 newfcn->h_function_pointer = fcn_ptr;
421 EVCleanupHookTable = newfcn;
423 syslog(LOG_INFO, "Registered a new cleanup function\n");
427 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
429 CleanupFunctionHook *cur, *p;
431 for (cur = EVCleanupHookTable; cur != NULL; cur = cur->next) {
432 /* This will also remove duplicates if any */
433 while (cur != NULL &&
434 fcn_ptr == cur->h_function_pointer) {
435 syslog(LOG_INFO, "Unregistered cleanup function\n");
437 if (cur == EVCleanupHookTable) {
438 EVCleanupHookTable = p;
447 void CtdlDestroyEVCleanupHooks(void)
449 CleanupFunctionHook *cur, *p;
451 cur = EVCleanupHookTable;
454 syslog(LOG_INFO, "Destroyed cleanup function\n");
459 EVCleanupHookTable = NULL;
463 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
466 SessionFunctionHook *newfcn;
468 newfcn = (SessionFunctionHook *)
469 malloc(sizeof(SessionFunctionHook));
470 newfcn->next = SessionHookTable;
471 newfcn->h_function_pointer = fcn_ptr;
472 newfcn->eventtype = EventType;
473 SessionHookTable = newfcn;
475 syslog(LOG_INFO, "Registered a new session function (type %d)\n",
480 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
482 SessionFunctionHook *cur, *p;
484 for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
485 /* This will also remove duplicates if any */
486 while (cur != NULL &&
487 fcn_ptr == cur->h_function_pointer &&
488 EventType == cur->eventtype) {
489 syslog(LOG_INFO, "Unregistered session function (type %d)\n",
492 if (cur == SessionHookTable) {
493 SessionHookTable = p;
501 void CtdlDestroySessionHooks(void)
503 SessionFunctionHook *cur, *p;
505 cur = SessionHookTable;
508 syslog(LOG_INFO, "Destroyed session function\n");
513 SessionHookTable = NULL;
517 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
520 UserFunctionHook *newfcn;
522 newfcn = (UserFunctionHook *)
523 malloc(sizeof(UserFunctionHook));
524 newfcn->next = UserHookTable;
525 newfcn->h_function_pointer = fcn_ptr;
526 newfcn->eventtype = EventType;
527 UserHookTable = newfcn;
529 syslog(LOG_INFO, "Registered a new user function (type %d)\n",
534 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
536 UserFunctionHook *cur, *p;
538 for (cur = UserHookTable; cur != NULL; cur = cur->next) {
539 /* This will also remove duplicates if any */
540 while (cur != NULL &&
541 fcn_ptr == cur->h_function_pointer &&
542 EventType == cur->eventtype) {
543 syslog(LOG_INFO, "Unregistered user function (type %d)\n",
546 if (cur == UserHookTable) {
555 void CtdlDestroyUserHooks(void)
557 UserFunctionHook *cur, *p;
562 syslog(LOG_INFO, "Destroyed user function \n");
567 UserHookTable = NULL;
571 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
575 MessageFunctionHook *newfcn;
577 newfcn = (MessageFunctionHook *)
578 malloc(sizeof(MessageFunctionHook));
579 newfcn->next = MessageHookTable;
580 newfcn->h_function_pointer = handler;
581 newfcn->eventtype = EventType;
582 MessageHookTable = newfcn;
584 syslog(LOG_INFO, "Registered a new message function (type %d)\n",
589 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
592 MessageFunctionHook *cur, *p;
594 for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
595 /* This will also remove duplicates if any */
596 while (cur != NULL &&
597 handler == cur->h_function_pointer &&
598 EventType == cur->eventtype) {
599 syslog(LOG_INFO, "Unregistered message function (type %d)\n",
602 if (cur == MessageHookTable) {
603 MessageHookTable = p;
611 void CtdlDestroyMessageHook(void)
613 MessageFunctionHook *cur, *p;
615 cur = MessageHookTable;
618 syslog(LOG_INFO, "Destroyed message function (type %d)\n", cur->eventtype);
623 MessageHookTable = NULL;
627 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
629 RoomFunctionHook *newfcn;
631 newfcn = (RoomFunctionHook *)
632 malloc(sizeof(RoomFunctionHook));
633 newfcn->next = RoomHookTable;
634 newfcn->fcn_ptr = fcn_ptr;
635 RoomHookTable = newfcn;
637 syslog(LOG_INFO, "Registered a new room function\n");
641 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
643 RoomFunctionHook *cur, *p;
645 for (cur = RoomHookTable; cur != NULL; cur = cur->next) {
646 while (cur != NULL && fcn_ptr == cur->fcn_ptr) {
647 syslog(LOG_INFO, "Unregistered room function\n");
649 if (cur == RoomHookTable) {
659 void CtdlDestroyRoomHooks(void)
661 RoomFunctionHook *cur, *p;
666 syslog(LOG_INFO, "Destroyed room function\n");
671 RoomHookTable = NULL;
674 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
676 NetprocFunctionHook *newfcn;
678 newfcn = (NetprocFunctionHook *)
679 malloc(sizeof(NetprocFunctionHook));
680 newfcn->next = NetprocHookTable;
681 newfcn->h_function_pointer = handler;
682 NetprocHookTable = newfcn;
684 syslog(LOG_INFO, "Registered a new netproc function\n");
688 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
690 NetprocFunctionHook *cur, *p;
692 for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
693 /* This will also remove duplicates if any */
694 while (cur != NULL &&
695 handler == cur->h_function_pointer ) {
696 syslog(LOG_INFO, "Unregistered netproc function\n");
698 if (cur == NetprocHookTable) {
699 NetprocHookTable = p;
707 void CtdlDestroyNetprocHooks(void)
709 NetprocFunctionHook *cur, *p;
711 cur = NetprocHookTable;
714 syslog(LOG_INFO, "Destroyed netproc function\n");
719 NetprocHookTable = NULL;
723 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
725 DeleteFunctionHook *newfcn;
727 newfcn = (DeleteFunctionHook *)
728 malloc(sizeof(DeleteFunctionHook));
729 newfcn->next = DeleteHookTable;
730 newfcn->h_function_pointer = handler;
731 DeleteHookTable = newfcn;
733 syslog(LOG_INFO, "Registered a new delete function\n");
737 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
739 DeleteFunctionHook *cur, *p;
741 for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
742 /* This will also remove duplicates if any */
743 while (cur != NULL &&
744 handler == cur->h_function_pointer ) {
745 syslog(LOG_INFO, "Unregistered delete function\n");
747 if (cur == DeleteHookTable) {
755 void CtdlDestroyDeleteHooks(void)
757 DeleteFunctionHook *cur, *p;
759 cur = DeleteHookTable;
762 syslog(LOG_INFO, "Destroyed delete function\n");
767 DeleteHookTable = NULL;
773 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
775 FixedOutputHook *newfcn;
777 newfcn = (FixedOutputHook *)
778 malloc(sizeof(FixedOutputHook));
779 newfcn->next = FixedOutputTable;
780 newfcn->h_function_pointer = handler;
781 safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
782 FixedOutputTable = newfcn;
784 syslog(LOG_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
788 void CtdlUnregisterFixedOutputHook(char *content_type)
790 FixedOutputHook *cur, *p;
792 for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
793 /* This will also remove duplicates if any */
794 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
795 syslog(LOG_INFO, "Unregistered fixed output function for %s\n", content_type);
797 if (cur == FixedOutputTable) {
798 FixedOutputTable = p;
806 void CtdlDestroyFixedOutputHooks(void)
808 FixedOutputHook *cur, *p;
810 cur = FixedOutputTable;
813 syslog(LOG_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
819 FixedOutputTable = NULL;
822 /* returns nonzero if we found a hook and used it */
823 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
825 FixedOutputHook *fcn;
827 for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
828 if (!strcasecmp(content_type, fcn->content_type)) {
829 (*fcn->h_function_pointer) (content, content_length);
840 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
843 XmsgFunctionHook *newfcn;
845 newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
846 newfcn->next = XmsgHookTable;
847 newfcn->order = order;
848 newfcn->h_function_pointer = fcn_ptr;
849 XmsgHookTable = newfcn;
850 syslog(LOG_INFO, "Registered a new x-msg function (priority %d)\n", order);
854 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
856 XmsgFunctionHook *cur, *p;
858 for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
859 /* This will also remove duplicates if any */
860 while (cur != NULL &&
861 fcn_ptr == cur->h_function_pointer &&
862 order == cur->order) {
863 syslog(LOG_INFO, "Unregistered x-msg function "
864 "(priority %d)\n", order);
866 if (cur == XmsgHookTable) {
875 void CtdlDestroyXmsgHooks(void)
877 XmsgFunctionHook *cur, *p;
882 syslog(LOG_INFO, "Destroyed x-msg function "
883 "(priority %d)\n", cur->order);
889 XmsgHookTable = NULL;
893 void CtdlRegisterServiceHook(int tcp_port,
895 void (*h_greeting_function) (void),
896 void (*h_command_function) (void),
897 void (*h_async_function) (void),
898 const char *ServiceName)
900 ServiceFunctionHook *newfcn;
905 newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
906 message = (char*) malloc (SIZ + SIZ);
908 newfcn->next = ServiceHookTable;
909 newfcn->tcp_port = tcp_port;
910 newfcn->sockpath = sockpath;
911 newfcn->h_greeting_function = h_greeting_function;
912 newfcn->h_command_function = h_command_function;
913 newfcn->h_async_function = h_async_function;
914 newfcn->ServiceName = ServiceName;
916 if (sockpath != NULL) {
917 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
918 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
920 else if (tcp_port <= 0) { /* port -1 to disable */
921 syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
927 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
929 config.c_maxsessions,
931 snprintf(message, SIZ, "TCP port %s:%d: (%s) ",
932 config.c_ip_addr, tcp_port, ServiceName);
935 if (newfcn->msock > 0) {
936 ServiceHookTable = newfcn;
937 strcat(message, "registered.");
938 syslog(LOG_INFO, "%s\n", message);
941 AddPortError(message, error);
942 strcat(message, "FAILED.");
943 syslog(LOG_CRIT, "%s\n", message);
950 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
951 void (*h_greeting_function) (void),
952 void (*h_command_function) (void),
953 void (*h_async_function) (void)
956 ServiceFunctionHook *cur, *p;
958 cur = ServiceHookTable;
959 while (cur != NULL) {
960 /* This will also remove duplicates if any */
961 while (cur != NULL &&
962 !(sockpath && cur->sockpath &&
963 strcmp(sockpath, cur->sockpath)) &&
964 h_greeting_function == cur->h_greeting_function &&
965 h_command_function == cur->h_command_function &&
966 h_async_function == cur->h_async_function &&
967 tcp_port == cur->tcp_port) {
970 syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
973 } else if (tcp_port) {
974 syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
976 syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
979 if (cur == ServiceHookTable) {
980 ServiceHookTable = p;
989 void CtdlShutdownServiceHooks(void)
991 /* sort of a duplicate of close_masters() but called earlier */
992 ServiceFunctionHook *cur;
994 cur = ServiceHookTable;
997 if (cur->msock != -1)
1001 if (cur->sockpath != NULL){
1002 syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1005 unlink(cur->sockpath);
1007 syslog(LOG_INFO, "[%s] closing service\n",
1015 void CtdlDestroyServiceHook(void)
1017 ServiceFunctionHook *cur, *p;
1019 cur = ServiceHookTable;
1023 if (cur->sockpath) {
1024 syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1026 unlink(cur->sockpath);
1027 } else if (cur->tcp_port) {
1028 syslog(LOG_INFO, "Closed TCP port %d\n", cur->tcp_port);
1030 syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1036 ServiceHookTable = NULL;
1039 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1041 SearchFunctionHook *newfcn;
1043 if (!name || !fcn_ptr) {
1047 newfcn = (SearchFunctionHook *)
1048 malloc(sizeof(SearchFunctionHook));
1049 newfcn->next = SearchFunctionHookTable;
1050 newfcn->name = name;
1051 newfcn->fcn_ptr = fcn_ptr;
1052 SearchFunctionHookTable = newfcn;
1054 syslog(LOG_INFO, "Registered a new search function (%s)\n", name);
1057 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1059 SearchFunctionHook *cur, *p;
1061 for (cur = SearchFunctionHookTable; cur != NULL; cur = cur->next) {
1062 while (fcn_ptr && (cur->fcn_ptr == fcn_ptr) && name && !strcmp(name, cur->name)) {
1063 syslog(LOG_INFO, "Unregistered search function(%s)\n", name);
1065 if (cur == SearchFunctionHookTable) {
1066 SearchFunctionHookTable = p;
1074 void CtdlDestroySearchHooks(void)
1076 SearchFunctionHook *cur, *p;
1078 cur = SearchFunctionHookTable;
1079 SearchFunctionHookTable = NULL;
1080 while (cur != NULL) {
1087 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1089 SearchFunctionHook *fcn = NULL;
1091 for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1092 if (!func_name || !strcmp(func_name, fcn->name)) {
1093 (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1101 void PerformSessionHooks(int EventType)
1103 SessionFunctionHook *fcn = NULL;
1105 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1106 if (fcn->eventtype == EventType) {
1107 if (EventType == EVT_TIMER) {
1108 pthread_setspecific(MyConKey, NULL); /* for every hook */
1110 (*fcn->h_function_pointer) ();
1115 void PerformUserHooks(ctdluser *usbuf, int EventType)
1117 UserFunctionHook *fcn = NULL;
1119 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1120 if (fcn->eventtype == EventType) {
1121 (*fcn->h_function_pointer) (usbuf);
1126 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
1128 MessageFunctionHook *fcn = NULL;
1129 int total_retval = 0;
1131 /* Other code may elect to protect this message from server-side
1132 * handlers; if this is the case, don't do anything.
1133 syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1135 if (msg->cm_flags & CM_SKIP_HOOKS) {
1136 syslog(LOG_DEBUG, "Skipping hooks\n");
1140 /* Otherwise, run all the hooks appropriate to this event type.
1142 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1143 if (fcn->eventtype == EventType) {
1144 total_retval = total_retval + (*fcn->h_function_pointer) (msg);
1148 /* Return the sum of the return codes from the hook functions. If
1149 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1150 * the save operation to abort.
1152 return total_retval;
1156 int PerformRoomHooks(struct ctdlroom *target_room)
1158 RoomFunctionHook *fcn;
1159 int total_retval = 0;
1161 syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1163 for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1164 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1167 /* Return the sum of the return codes from the hook functions.
1169 return total_retval;
1173 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1175 NetprocFunctionHook *fcn;
1176 int total_retval = 0;
1178 for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1179 total_retval = total_retval +
1180 (*fcn->h_function_pointer) (msg, target_room);
1183 /* Return the sum of the return codes from the hook functions.
1184 * A nonzero return code will cause the message to *not* be imported.
1186 return total_retval;
1190 void PerformDeleteHooks(char *room, long msgnum)
1192 DeleteFunctionHook *fcn;
1194 for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1195 (*fcn->h_function_pointer) (room, msgnum);
1203 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1205 XmsgFunctionHook *fcn;
1209 for (p=0; p<MAX_XMSG_PRI; ++p) {
1210 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1211 if (fcn->order == p) {
1213 (*fcn->h_function_pointer)
1214 (sender, sender_email, recp, msg);
1217 /* Break out of the loop if a higher-priority function
1218 * successfully delivered the message. This prevents duplicate
1219 * deliveries to local users simultaneously signed onto
1222 if (total_sent) break;
1229 * Dirty hack until we impliment a hook mechanism for this
1231 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1234 CtdlStartTLS (ok_response, nosup_response, error_response);