d2890ebaeedbd5094f0e06d99bf4bedd4dda892f
[citadel.git] / citadel / serv_extensions.c
1 /*
2  * Citadel Dynamic Loading Module
3  * Written by Brian Costello <btx@calyx.net>
4  *
5  * Copyright (c) 1987-2011 by the citadel.org team
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "sysdep.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <dirent.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <ctype.h>
31 #include <syslog.h>
32 #include <libcitadel.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "serv_extensions.h"
36 #include "sysdep_decls.h"
37 #include "msgbase.h"
38 #include "config.h"
39
40 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
41
42 #include "ctdl_module.h"
43
44 #ifndef HAVE_SNPRINTF
45 #include <stdarg.h>
46 #include "snprintf.h"
47 #endif
48
49  
50 /*
51  * Structure defentitions for hook tables
52  */
53
54
55
56 typedef struct LogFunctionHook LogFunctionHook;
57 struct LogFunctionHook {
58         LogFunctionHook *next;
59         int loglevel;
60         void (*h_function_pointer) (char *);
61 };
62 extern LogFunctionHook *LogHookTable;
63
64
65 typedef struct FixedOutputHook FixedOutputHook;
66 struct FixedOutputHook {
67         FixedOutputHook *next;
68         char content_type[64];
69         void (*h_function_pointer) (char *, int);
70 };
71 extern FixedOutputHook *FixedOutputTable;
72
73
74
75 /*
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
79  * pertinent data.
80  */
81 typedef struct SessionFunctionHook SessionFunctionHook;
82 struct SessionFunctionHook {
83         SessionFunctionHook *next;
84         void (*h_function_pointer) (void);
85         int eventtype;
86 };
87 extern SessionFunctionHook *SessionHookTable;
88
89
90 /*
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.
94  */
95 typedef struct UserFunctionHook UserFunctionHook;
96 struct UserFunctionHook {
97         UserFunctionHook *next;
98         void (*h_function_pointer) (struct ctdluser *usbuf);
99         int eventtype;
100 };
101 extern UserFunctionHook *UserHookTable;
102
103 /*
104  * MessageFunctionHook extensions are used for hooks which implement handlers
105  * for various types of message operations (save, read, etc.)
106  */
107 typedef struct MessageFunctionHook MessageFunctionHook;
108 struct MessageFunctionHook {
109         MessageFunctionHook *next;
110         int (*h_function_pointer) (struct CtdlMessage *msg);
111         int eventtype;
112 };
113 extern MessageFunctionHook *MessageHookTable;
114
115
116 /*
117  * NetprocFunctionHook extensions are used for hooks which implement handlers
118  * for incoming network messages.
119  */
120 typedef struct NetprocFunctionHook NetprocFunctionHook;
121 struct NetprocFunctionHook {
122         NetprocFunctionHook *next;
123         int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
124 };
125 extern NetprocFunctionHook *NetprocHookTable;
126
127
128 /*
129  * DeleteFunctionHook extensions are used for hooks which get called when a
130  * message is about to be deleted.
131  */
132 typedef struct DeleteFunctionHook DeleteFunctionHook;
133 struct DeleteFunctionHook {
134         DeleteFunctionHook *next;
135         void (*h_function_pointer) (char *target_room, long msgnum);
136 };
137 extern DeleteFunctionHook *DeleteHookTable;
138
139
140 /*
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.
145  */
146 typedef struct XmsgFunctionHook XmsgFunctionHook;
147 struct XmsgFunctionHook {
148         XmsgFunctionHook *next;
149         int (*h_function_pointer) (char *, char *, char *, char *);
150         int order;
151 };
152 extern XmsgFunctionHook *XmsgHookTable;
153
154
155
156
157 /*
158  * RoomFunctionHook extensions are used for hooks which impliment room
159  * processing functions when new messages are added EG. SIEVE.
160  */
161 typedef struct RoomFunctionHook RoomFunctionHook;
162 struct RoomFunctionHook {
163         RoomFunctionHook *next;
164         int (*fcn_ptr) (struct ctdlroom *);
165 };
166 extern RoomFunctionHook *RoomHookTable;
167
168
169
170 typedef struct SearchFunctionHook SearchFunctionHook;
171 struct SearchFunctionHook {
172         SearchFunctionHook *next;
173         void (*fcn_ptr) (int *, long **, const char *);
174         char *name;
175 };
176 extern SearchFunctionHook *SearchFunctionHookTable;
177
178
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;
191
192 typedef struct ProtoFunctionHook ProtoFunctionHook;
193 struct ProtoFunctionHook {
194         void (*handler) (char *cmdbuf);
195         const char *cmd;
196         const char *desc;
197 };
198
199 HashList *ProtoHookList = NULL;
200
201
202 #define ERR_PORT (1 << 1)
203
204
205 static StrBuf *portlist = NULL;
206
207 static StrBuf *errormessages = NULL;
208
209
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")}
221 };
222
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")};
230
231
232 void LogPrintMessages(long err)
233 {
234         StrBuf *Message;
235         StrBuf *List, *DetailList;
236         ConstStr *Short, *Where, *Hint; 
237
238         
239         Message = NewStrBufPlain(NULL, 
240                                  StrLength(portlist) + StrLength(errormessages));
241         
242         DetailErrorFlags = DetailErrorFlags & ~err;
243
244         switch (err)
245         {
246         case ERR_PORT:
247                 Short = &ErrPortShort;
248                 Where = &ErrPortWhere;
249                 Hint  = &ErrPortHint;
250                 List  = portlist;
251                 DetailList = errormessages;
252                 break;
253         default:
254                 Short = &Empty;
255                 Where = &Empty;
256                 Hint  = &Empty;
257                 List  = NULL;
258                 DetailList = NULL;
259         }
260
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);
273
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);
277
278         FreeStrBuf(&Message);
279         FreeStrBuf(&List);
280         FreeStrBuf(&DetailList);
281 }
282
283
284 void AddPortError(char *Port, char *ErrorMessage)
285 {
286         long len;
287
288         DetailErrorFlags |= ERR_PORT;
289
290         len = StrLength(errormessages);
291         if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
292         else errormessages = NewStrBuf();
293         StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
294
295
296         len = StrLength(portlist);
297         if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
298         else portlist = NewStrBuf();
299         StrBufAppendBufPlain(portlist, Port, -1, 0);
300 }
301
302
303 int DLoader_Exec_Cmd(char *cmdbuf)
304 {
305         void *vP;
306         ProtoFunctionHook *p;
307
308         if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
309                 p = (ProtoFunctionHook*) vP;
310                 p->handler(&cmdbuf[5]);
311                 return 1;
312         }
313         return 0;
314 }
315
316 long FourHash(const char *key, long length) 
317 {
318         int i;
319         int ret = 0;
320         const unsigned char *ptr = (const unsigned char*)key;
321
322         for (i = 0; i < 4; i++, ptr ++) 
323                 ret = (ret << 8) | 
324                         ( ((*ptr >= 'a') &&
325                            (*ptr <= 'z'))? 
326                           *ptr - 'a' + 'A': 
327                           *ptr);
328
329         return ret;
330 }
331
332 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
333 {
334         ProtoFunctionHook *p;
335
336         if (ProtoHookList == NULL)
337                 ProtoHookList = NewHash (1, FourHash);
338
339
340         p = (ProtoFunctionHook *)
341                 malloc(sizeof(ProtoFunctionHook));
342
343         if (p == NULL) {
344                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
345                 exit(EXIT_FAILURE);
346         }
347         p->handler = handler;
348         p->cmd = cmd;
349         p->desc = desc;
350
351         Put(ProtoHookList, cmd, 4, p, NULL);
352         syslog(LOG_INFO, "Registered server command %s (%s)\n", cmd, desc);
353 }
354
355 void CtdlDestroyProtoHooks(void)
356 {
357
358         DeleteHash(&ProtoHookList);
359 }
360
361
362 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
363 {
364
365         CleanupFunctionHook *newfcn;
366
367         newfcn = (CleanupFunctionHook *)
368             malloc(sizeof(CleanupFunctionHook));
369         newfcn->next = CleanupHookTable;
370         newfcn->h_function_pointer = fcn_ptr;
371         CleanupHookTable = newfcn;
372
373         syslog(LOG_INFO, "Registered a new cleanup function\n");
374 }
375
376
377 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
378 {
379         CleanupFunctionHook *cur, *p;
380
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");
386                         p = cur->next;
387                         if (cur == CleanupHookTable) {
388                                 CleanupHookTable = p;
389                         }
390                         free(cur);
391                         cur = p;
392                 }
393         }
394 }
395
396
397 void CtdlDestroyCleanupHooks(void)
398 {
399         CleanupFunctionHook *cur, *p;
400
401         cur = CleanupHookTable;
402         while (cur != NULL)
403         {
404                 syslog(LOG_INFO, "Destroyed cleanup function\n");
405                 p = cur->next;
406                 free(cur);
407                 cur = p;
408         }
409         CleanupHookTable = NULL;
410 }
411
412 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
413 {
414
415         CleanupFunctionHook *newfcn;
416
417         newfcn = (CleanupFunctionHook *)
418             malloc(sizeof(CleanupFunctionHook));
419         newfcn->next = EVCleanupHookTable;
420         newfcn->h_function_pointer = fcn_ptr;
421         EVCleanupHookTable = newfcn;
422
423         syslog(LOG_INFO, "Registered a new cleanup function\n");
424 }
425
426
427 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
428 {
429         CleanupFunctionHook *cur, *p;
430
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");
436                         p = cur->next;
437                         if (cur == EVCleanupHookTable) {
438                                 EVCleanupHookTable = p;
439                         }
440                         free(cur);
441                         cur = p;
442                 }
443         }
444 }
445
446
447 void CtdlDestroyEVCleanupHooks(void)
448 {
449         CleanupFunctionHook *cur, *p;
450
451         cur = EVCleanupHookTable;
452         while (cur != NULL)
453         {
454                 syslog(LOG_INFO, "Destroyed cleanup function\n");
455                 p = cur->next;
456                 free(cur);
457                 cur = p;
458         }
459         EVCleanupHookTable = NULL;
460 }
461
462
463 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
464 {
465
466         SessionFunctionHook *newfcn;
467
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;
474
475         syslog(LOG_INFO, "Registered a new session function (type %d)\n",
476                 EventType);
477 }
478
479
480 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
481 {
482         SessionFunctionHook *cur, *p;
483
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",
490                                         EventType);
491                         p = cur->next;
492                         if (cur == SessionHookTable) {
493                                 SessionHookTable = p;
494                         }
495                         free(cur);
496                         cur = p;
497                 }
498         }
499 }
500
501 void CtdlDestroySessionHooks(void)
502 {
503         SessionFunctionHook *cur, *p;
504
505         cur = SessionHookTable;
506         while (cur != NULL)
507         {
508                 syslog(LOG_INFO, "Destroyed session function\n");
509                 p = cur->next;
510                 free(cur);
511                 cur = p;
512         }
513         SessionHookTable = NULL;
514 }
515
516
517 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
518 {
519
520         UserFunctionHook *newfcn;
521
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;
528
529         syslog(LOG_INFO, "Registered a new user function (type %d)\n",
530                 EventType);
531 }
532
533
534 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
535 {
536         UserFunctionHook *cur, *p;
537
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",
544                                         EventType);
545                         p = cur->next;
546                         if (cur == UserHookTable) {
547                                 UserHookTable = p;
548                         }
549                         free(cur);
550                         cur = p;
551                 }
552         }
553 }
554
555 void CtdlDestroyUserHooks(void)
556 {
557         UserFunctionHook *cur, *p;
558
559         cur = UserHookTable;
560         while (cur != NULL)
561         {
562                 syslog(LOG_INFO, "Destroyed user function \n");
563                 p = cur->next;
564                 free(cur);
565                 cur = p;
566         }
567         UserHookTable = NULL;
568 }
569
570
571 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
572                                 int EventType)
573 {
574
575         MessageFunctionHook *newfcn;
576
577         newfcn = (MessageFunctionHook *)
578             malloc(sizeof(MessageFunctionHook));
579         newfcn->next = MessageHookTable;
580         newfcn->h_function_pointer = handler;
581         newfcn->eventtype = EventType;
582         MessageHookTable = newfcn;
583
584         syslog(LOG_INFO, "Registered a new message function (type %d)\n",
585                 EventType);
586 }
587
588
589 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
590                 int EventType)
591 {
592         MessageFunctionHook *cur, *p;
593
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",
600                                         EventType);
601                         p = cur->next;
602                         if (cur == MessageHookTable) {
603                                 MessageHookTable = p;
604                         }
605                         free(cur);
606                         cur = p;
607                 }
608         }
609 }
610
611 void CtdlDestroyMessageHook(void)
612 {
613         MessageFunctionHook *cur, *p;
614
615         cur = MessageHookTable; 
616         while (cur != NULL)
617         {
618                 syslog(LOG_INFO, "Destroyed message function (type %d)\n", cur->eventtype);
619                 p = cur->next;
620                 free(cur);
621                 cur = p;
622         }
623         MessageHookTable = NULL;
624 }
625
626
627 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
628 {
629         RoomFunctionHook *newfcn;
630
631         newfcn = (RoomFunctionHook *)
632             malloc(sizeof(RoomFunctionHook));
633         newfcn->next = RoomHookTable;
634         newfcn->fcn_ptr = fcn_ptr;
635         RoomHookTable = newfcn;
636
637         syslog(LOG_INFO, "Registered a new room function\n");
638 }
639
640
641 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
642 {
643         RoomFunctionHook *cur, *p;
644
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");
648                         p = cur->next;
649                         if (cur == RoomHookTable) {
650                                 RoomHookTable = p;
651                         }
652                         free(cur);
653                         cur = p;
654                 }
655         }
656 }
657
658
659 void CtdlDestroyRoomHooks(void)
660 {
661         RoomFunctionHook *cur, *p;
662
663         cur = RoomHookTable;
664         while (cur != NULL)
665         {
666                 syslog(LOG_INFO, "Destroyed room function\n");
667                 p = cur->next;
668                 free(cur);
669                 cur = p;
670         }
671         RoomHookTable = NULL;
672 }
673
674 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
675 {
676         NetprocFunctionHook *newfcn;
677
678         newfcn = (NetprocFunctionHook *)
679             malloc(sizeof(NetprocFunctionHook));
680         newfcn->next = NetprocHookTable;
681         newfcn->h_function_pointer = handler;
682         NetprocHookTable = newfcn;
683
684         syslog(LOG_INFO, "Registered a new netproc function\n");
685 }
686
687
688 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
689 {
690         NetprocFunctionHook *cur, *p;
691
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");
697                         p = cur->next;
698                         if (cur == NetprocHookTable) {
699                                 NetprocHookTable = p;
700                         }
701                         free(cur);
702                         cur = p;
703                 }
704         }
705 }
706
707 void CtdlDestroyNetprocHooks(void)
708 {
709         NetprocFunctionHook *cur, *p;
710
711         cur = NetprocHookTable;
712         while (cur != NULL)
713         {
714                 syslog(LOG_INFO, "Destroyed netproc function\n");
715                 p = cur->next;
716                 free(cur);
717                 cur = p;
718         }
719         NetprocHookTable = NULL;
720 }
721
722
723 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
724 {
725         DeleteFunctionHook *newfcn;
726
727         newfcn = (DeleteFunctionHook *)
728             malloc(sizeof(DeleteFunctionHook));
729         newfcn->next = DeleteHookTable;
730         newfcn->h_function_pointer = handler;
731         DeleteHookTable = newfcn;
732
733         syslog(LOG_INFO, "Registered a new delete function\n");
734 }
735
736
737 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
738 {
739         DeleteFunctionHook *cur, *p;
740
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");
746                         p = cur->next;
747                         if (cur == DeleteHookTable) {
748                                 DeleteHookTable = p;
749                         }
750                         free(cur);
751                         cur = p;
752                 }
753         }
754 }
755 void CtdlDestroyDeleteHooks(void)
756 {
757         DeleteFunctionHook *cur, *p;
758
759         cur = DeleteHookTable;
760         while (cur != NULL)
761         {
762                 syslog(LOG_INFO, "Destroyed delete function\n");
763                 p = cur->next;
764                 free(cur);
765                 cur = p;                
766         }
767         DeleteHookTable = NULL;
768 }
769
770
771
772
773 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
774 {
775         FixedOutputHook *newfcn;
776
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;
783
784         syslog(LOG_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
785 }
786
787
788 void CtdlUnregisterFixedOutputHook(char *content_type)
789 {
790         FixedOutputHook *cur, *p;
791
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);
796                         p = cur->next;
797                         if (cur == FixedOutputTable) {
798                                 FixedOutputTable = p;
799                         }
800                         free(cur);
801                         cur = p;
802                 }
803         }
804 }
805
806 void CtdlDestroyFixedOutputHooks(void)
807 {
808         FixedOutputHook *cur, *p;
809
810         cur = FixedOutputTable; 
811         while (cur != NULL)
812         {
813                 syslog(LOG_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
814                 p = cur->next;
815                 free(cur);
816                 cur = p;
817                 
818         }
819         FixedOutputTable = NULL;
820 }
821
822 /* returns nonzero if we found a hook and used it */
823 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
824 {
825         FixedOutputHook *fcn;
826
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);
830                         return(1);
831                 }
832         }
833         return(0);
834 }
835
836
837
838
839
840 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
841 {
842
843         XmsgFunctionHook *newfcn;
844
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);
851 }
852
853
854 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
855 {
856         XmsgFunctionHook *cur, *p;
857
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);
865                         p = cur->next;
866                         if (cur == XmsgHookTable) {
867                                 XmsgHookTable = p;
868                         }
869                         free(cur);
870                         cur = p;
871                 }
872         }
873 }
874
875 void CtdlDestroyXmsgHooks(void)
876 {
877         XmsgFunctionHook *cur, *p;
878
879         cur = XmsgHookTable;
880         while (cur != NULL)
881         {
882                 syslog(LOG_INFO, "Destroyed x-msg function "
883                         "(priority %d)\n", cur->order);
884                 p = cur->next;
885                         
886                 free(cur);
887                 cur = p;
888         }
889         XmsgHookTable = NULL;
890 }
891
892
893 void CtdlRegisterServiceHook(int tcp_port,
894                              char *sockpath,
895                              void (*h_greeting_function) (void),
896                              void (*h_command_function) (void),
897                              void (*h_async_function) (void),
898                              const char *ServiceName)
899 {
900         ServiceFunctionHook *newfcn;
901         char *message;
902         char error[SIZ];
903
904         strcpy(error, "");
905         newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
906         message = (char*) malloc (SIZ + SIZ);
907         
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;
915
916         if (sockpath != NULL) {
917                 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
918                 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
919         }
920         else if (tcp_port <= 0) {       /* port -1 to disable */
921                 syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
922                 free (message);
923                 free(newfcn);
924                 return;
925         }
926         else {
927                 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
928                                               tcp_port,
929                                               config.c_maxsessions, 
930                                               error);
931                 snprintf(message, SIZ, "TCP port %s:%d: (%s) ", 
932                          config.c_ip_addr, tcp_port, ServiceName);
933         }
934
935         if (newfcn->msock > 0) {
936                 ServiceHookTable = newfcn;
937                 strcat(message, "registered.");
938                 syslog(LOG_INFO, "%s\n", message);
939         }
940         else {
941                 AddPortError(message, error);
942                 strcat(message, "FAILED.");
943                 syslog(LOG_CRIT, "%s\n", message);
944                 free(newfcn);
945         }
946         free(message);
947 }
948
949
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)
954                         )
955 {
956         ServiceFunctionHook *cur, *p;
957
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) {
968                         close(cur->msock);
969                         if (sockpath) {
970                                 syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
971                                                 sockpath);
972                                 unlink(sockpath);
973                         } else if (tcp_port) {
974                                 syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
975                         } else {
976                                 syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
977                         }
978                         p = cur->next;
979                         if (cur == ServiceHookTable) {
980                                 ServiceHookTable = p;
981                         }
982                         free(cur);
983                         cur = p;
984                 }
985         }
986 }
987
988
989 void CtdlShutdownServiceHooks(void)
990 {
991         /* sort of a duplicate of close_masters() but called earlier */
992         ServiceFunctionHook *cur;
993
994         cur = ServiceHookTable;
995         while (cur != NULL) 
996         {
997                 if (cur->msock != -1)
998                 {
999                         close(cur->msock);
1000                         cur->msock = -1;
1001                         if (cur->sockpath != NULL){
1002                                 syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1003                                               cur->ServiceName,
1004                                               cur->sockpath);
1005                                 unlink(cur->sockpath);
1006                         } else {
1007                                 syslog(LOG_INFO, "[%s] closing service\n", 
1008                                               cur->ServiceName);
1009                         }
1010                 }
1011                 cur = cur->next;
1012         }
1013 }
1014
1015 void CtdlDestroyServiceHook(void)
1016 {
1017         ServiceFunctionHook *cur, *p;
1018
1019         cur = ServiceHookTable;
1020         while (cur != NULL)
1021         {
1022                 close(cur->msock);
1023                 if (cur->sockpath) {
1024                         syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1025                                 cur->sockpath);
1026                         unlink(cur->sockpath);
1027                 } else if (cur->tcp_port) {
1028                         syslog(LOG_INFO, "Closed TCP port %d\n", cur->tcp_port);
1029                 } else {
1030                         syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1031                 }
1032                 p = cur->next;
1033                 free(cur);
1034                 cur = p;
1035         }
1036         ServiceHookTable = NULL;
1037 }
1038
1039 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1040 {
1041         SearchFunctionHook *newfcn;
1042
1043         if (!name || !fcn_ptr) {
1044                 return;
1045         }
1046         
1047         newfcn = (SearchFunctionHook *)
1048             malloc(sizeof(SearchFunctionHook));
1049         newfcn->next = SearchFunctionHookTable;
1050         newfcn->name = name;
1051         newfcn->fcn_ptr = fcn_ptr;
1052         SearchFunctionHookTable = newfcn;
1053
1054         syslog(LOG_INFO, "Registered a new search function (%s)\n", name);
1055 }
1056
1057 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1058 {
1059         SearchFunctionHook *cur, *p;
1060         
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);
1064                         p = cur->next;
1065                         if (cur == SearchFunctionHookTable) {
1066                                 SearchFunctionHookTable = p;
1067                         }
1068                         free (cur);
1069                         cur = p;
1070                 }
1071         }
1072 }
1073
1074 void CtdlDestroySearchHooks(void)
1075 {
1076         SearchFunctionHook *cur, *p;
1077
1078         cur = SearchFunctionHookTable;
1079         SearchFunctionHookTable = NULL;
1080         while (cur != NULL) {
1081                 p = cur->next;
1082                 free(cur);
1083                 cur = p;
1084         }
1085 }
1086
1087 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1088 {
1089         SearchFunctionHook *fcn = NULL;
1090
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);
1094                         return;
1095                 }
1096         }
1097         *num_msgs = 0;
1098 }
1099
1100
1101 void PerformSessionHooks(int EventType)
1102 {
1103         SessionFunctionHook *fcn = NULL;
1104
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 */
1109                         }
1110                         (*fcn->h_function_pointer) ();
1111                 }
1112         }
1113 }
1114
1115 void PerformUserHooks(ctdluser *usbuf, int EventType)
1116 {
1117         UserFunctionHook *fcn = NULL;
1118
1119         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1120                 if (fcn->eventtype == EventType) {
1121                         (*fcn->h_function_pointer) (usbuf);
1122                 }
1123         }
1124 }
1125
1126 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
1127 {
1128         MessageFunctionHook *fcn = NULL;
1129         int total_retval = 0;
1130
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);
1134          */
1135         if (msg->cm_flags & CM_SKIP_HOOKS) {
1136                 syslog(LOG_DEBUG, "Skipping hooks\n");
1137                 return(0);
1138         }
1139
1140         /* Otherwise, run all the hooks appropriate to this event type.
1141          */
1142         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1143                 if (fcn->eventtype == EventType) {
1144                         total_retval = total_retval + (*fcn->h_function_pointer) (msg);
1145                 }
1146         }
1147
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.
1151          */
1152         return total_retval;
1153 }
1154
1155
1156 int PerformRoomHooks(struct ctdlroom *target_room)
1157 {
1158         RoomFunctionHook *fcn;
1159         int total_retval = 0;
1160
1161         syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1162
1163         for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1164                 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1165         }
1166
1167         /* Return the sum of the return codes from the hook functions.
1168          */
1169         return total_retval;
1170 }
1171
1172
1173 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1174 {
1175         NetprocFunctionHook *fcn;
1176         int total_retval = 0;
1177
1178         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1179                 total_retval = total_retval +
1180                         (*fcn->h_function_pointer) (msg, target_room);
1181         }
1182
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.
1185          */
1186         return total_retval;
1187 }
1188
1189
1190 void PerformDeleteHooks(char *room, long msgnum)
1191 {
1192         DeleteFunctionHook *fcn;
1193
1194         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1195                 (*fcn->h_function_pointer) (room, msgnum);
1196         }
1197 }
1198
1199
1200
1201
1202
1203 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1204 {
1205         XmsgFunctionHook *fcn;
1206         int total_sent = 0;
1207         int p;
1208
1209         for (p=0; p<MAX_XMSG_PRI; ++p) {
1210                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1211                         if (fcn->order == p) {
1212                                 total_sent +=
1213                                         (*fcn->h_function_pointer)
1214                                                 (sender, sender_email, recp, msg);
1215                         }
1216                 }
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
1220                  * remote services.
1221                  */
1222                 if (total_sent) break;
1223         }
1224         return total_sent;
1225 }
1226
1227
1228 /*
1229  * Dirty hack until we impliment a hook mechanism for this
1230  */
1231 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1232 {
1233 #ifdef HAVE_OPENSSL
1234         CtdlStartTLS (ok_response, nosup_response, error_response);
1235 #endif
1236 }