45221c514eef4a3ced49c122cadc8e8c49e14c32
[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 typedef struct __LogDebugEntry {
55         CtdlDbgFunction F;
56         const char *Name;
57         long Len;
58 } LogDebugEntry;
59 HashList *LogDebugEntryTable = NULL;
60
61 typedef struct LogFunctionHook LogFunctionHook;
62 struct LogFunctionHook {
63         LogFunctionHook *next;
64         int loglevel;
65         void (*h_function_pointer) (char *);
66 };
67 extern LogFunctionHook *LogHookTable;
68
69 typedef struct FixedOutputHook FixedOutputHook;
70 struct FixedOutputHook {
71         FixedOutputHook *next;
72         char content_type[64];
73         void (*h_function_pointer) (char *, int);
74 };
75 extern FixedOutputHook *FixedOutputTable;
76
77
78
79 /*
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
83  * pertinent data.
84  */
85 typedef struct SessionFunctionHook SessionFunctionHook;
86 struct SessionFunctionHook {
87         SessionFunctionHook *next;
88         void (*h_function_pointer) (void);
89         int eventtype;
90 };
91 extern SessionFunctionHook *SessionHookTable;
92
93
94 /*
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.
98  */
99 typedef struct UserFunctionHook UserFunctionHook;
100 struct UserFunctionHook {
101         UserFunctionHook *next;
102         void (*h_function_pointer) (struct ctdluser *usbuf);
103         int eventtype;
104 };
105 extern UserFunctionHook *UserHookTable;
106
107 /*
108  * MessageFunctionHook extensions are used for hooks which implement handlers
109  * for various types of message operations (save, read, etc.)
110  */
111 typedef struct MessageFunctionHook MessageFunctionHook;
112 struct MessageFunctionHook {
113         MessageFunctionHook *next;
114         int (*h_function_pointer) (struct CtdlMessage *msg);
115         int eventtype;
116 };
117 extern MessageFunctionHook *MessageHookTable;
118
119
120 /*
121  * NetprocFunctionHook extensions are used for hooks which implement handlers
122  * for incoming network messages.
123  */
124 typedef struct NetprocFunctionHook NetprocFunctionHook;
125 struct NetprocFunctionHook {
126         NetprocFunctionHook *next;
127         int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
128 };
129 extern NetprocFunctionHook *NetprocHookTable;
130
131
132 /*
133  * DeleteFunctionHook extensions are used for hooks which get called when a
134  * message is about to be deleted.
135  */
136 typedef struct DeleteFunctionHook DeleteFunctionHook;
137 struct DeleteFunctionHook {
138         DeleteFunctionHook *next;
139         void (*h_function_pointer) (char *target_room, long msgnum);
140 };
141 extern DeleteFunctionHook *DeleteHookTable;
142
143
144 /*
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.
149  */
150 typedef struct XmsgFunctionHook XmsgFunctionHook;
151 struct XmsgFunctionHook {
152         XmsgFunctionHook *next;
153         int (*h_function_pointer) (char *, char *, char *, char *);
154         int order;
155 };
156 extern XmsgFunctionHook *XmsgHookTable;
157
158
159
160
161 /*
162  * RoomFunctionHook extensions are used for hooks which impliment room
163  * processing functions when new messages are added EG. SIEVE.
164  */
165 typedef struct RoomFunctionHook RoomFunctionHook;
166 struct RoomFunctionHook {
167         RoomFunctionHook *next;
168         int (*fcn_ptr) (struct ctdlroom *);
169 };
170 extern RoomFunctionHook *RoomHookTable;
171
172
173
174 typedef struct SearchFunctionHook SearchFunctionHook;
175 struct SearchFunctionHook {
176         SearchFunctionHook *next;
177         void (*fcn_ptr) (int *, long **, const char *);
178         char *name;
179 };
180 extern SearchFunctionHook *SearchFunctionHookTable;
181
182
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;
195
196 typedef struct ProtoFunctionHook ProtoFunctionHook;
197 struct ProtoFunctionHook {
198         void (*handler) (char *cmdbuf);
199         const char *cmd;
200         const char *desc;
201 };
202
203 HashList *ProtoHookList = NULL;
204
205
206 #define ERR_PORT (1 << 1)
207
208
209 static StrBuf *portlist = NULL;
210
211 static StrBuf *errormessages = NULL;
212
213
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")}
225 };
226
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")};
234
235
236 void LogPrintMessages(long err)
237 {
238         StrBuf *Message;
239         StrBuf *List, *DetailList;
240         ConstStr *Short, *Where, *Hint; 
241
242         
243         Message = NewStrBufPlain(NULL, 
244                                  StrLength(portlist) + StrLength(errormessages));
245         
246         DetailErrorFlags = DetailErrorFlags & ~err;
247
248         switch (err)
249         {
250         case ERR_PORT:
251                 Short = &ErrPortShort;
252                 Where = &ErrPortWhere;
253                 Hint  = &ErrPortHint;
254                 List  = portlist;
255                 DetailList = errormessages;
256                 break;
257         default:
258                 Short = &Empty;
259                 Where = &Empty;
260                 Hint  = &Empty;
261                 List  = NULL;
262                 DetailList = NULL;
263         }
264
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);
277
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);
281
282         FreeStrBuf(&Message);
283         FreeStrBuf(&List);
284         FreeStrBuf(&DetailList);
285 }
286
287
288 void AddPortError(char *Port, char *ErrorMessage)
289 {
290         long len;
291
292         DetailErrorFlags |= ERR_PORT;
293
294         len = StrLength(errormessages);
295         if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
296         else errormessages = NewStrBuf();
297         StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
298
299
300         len = StrLength(portlist);
301         if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
302         else portlist = NewStrBuf();
303         StrBufAppendBufPlain(portlist, Port, -1, 0);
304 }
305
306
307 int DLoader_Exec_Cmd(char *cmdbuf)
308 {
309         void *vP;
310         ProtoFunctionHook *p;
311
312         if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
313                 p = (ProtoFunctionHook*) vP;
314                 p->handler(&cmdbuf[5]);
315                 return 1;
316         }
317         return 0;
318 }
319
320 long FourHash(const char *key, long length) 
321 {
322         int i;
323         int ret = 0;
324         const unsigned char *ptr = (const unsigned char*)key;
325
326         for (i = 0; i < 4; i++, ptr ++) 
327                 ret = (ret << 8) | 
328                         ( ((*ptr >= 'a') &&
329                            (*ptr <= 'z'))? 
330                           *ptr - 'a' + 'A': 
331                           *ptr);
332
333         return ret;
334 }
335 /*
336 typedef struct __LogDebugEntry {
337         CtdlDbgFunction F;
338         const char *Name;
339         long Len;
340 } LogDebugEntry;
341 HashList *LogDebugEntryTable = NULL;
342 */
343 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F)
344 {
345         LogDebugEntry *E;
346         if (LogDebugEntryTable == NULL)
347                 LogDebugEntryTable = NewHash(1, NULL);
348         E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
349         E->F = F;
350         E->Name = Name;
351         E->Len = Len;
352         Put(LogDebugEntryTable, Name, Len, E, NULL);
353         
354 }
355 void CtdlSetDebugLogFacilities(const char **Str, long n)
356 {
357         StrBuf *Token = NULL;
358         StrBuf *Buf = NULL;
359         const char *ch;
360         int i;
361         int DoAll = 0;
362         void *vptr;
363
364         for (i=0; i < n; i++){
365                 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
366                         if (strcmp(Str[i], "all") == 0) {
367                                 DoAll = 1;
368                                 continue;
369                         }
370                         Buf = NewStrBufPlain(Str[i], -1);
371                         ch = NULL;
372                         if (Token == NULL)
373                                 Token = NewStrBufPlain(NULL, StrLength(Buf));
374                         while ((ch != StrBufNOTNULL) &&
375                                StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
376                                 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) && 
377                                     (vptr != NULL))
378                                 {
379                                         LogDebugEntry *E = (LogDebugEntry*)vptr;
380                                         E->F();
381                                 }
382                         }
383                 }
384                 FreeStrBuf(&Buf);
385         }
386         FreeStrBuf(&Token);
387         if (DoAll) {
388                 long HKLen;
389                 const char *ch;
390                 HashPos *Pos;
391
392                 Pos = GetNewHashPos(LogDebugEntryTable, 0);
393                 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
394                         LogDebugEntry *E = (LogDebugEntry*)vptr;
395                         E->F();
396                 }
397
398                 DeleteHashPos(&Pos);
399         }
400 }
401 void CtdlDestroyDebugTable(void)
402 {
403
404         DeleteHash(&LogDebugEntryTable);
405 }
406
407 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
408 {
409         ProtoFunctionHook *p;
410
411         if (ProtoHookList == NULL)
412                 ProtoHookList = NewHash (1, FourHash);
413
414
415         p = (ProtoFunctionHook *)
416                 malloc(sizeof(ProtoFunctionHook));
417
418         if (p == NULL) {
419                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
420                 exit(EXIT_FAILURE);
421         }
422         p->handler = handler;
423         p->cmd = cmd;
424         p->desc = desc;
425
426         Put(ProtoHookList, cmd, 4, p, NULL);
427         syslog(LOG_INFO, "Registered server command %s (%s)\n", cmd, desc);
428 }
429
430 void CtdlDestroyProtoHooks(void)
431 {
432
433         DeleteHash(&ProtoHookList);
434 }
435
436
437 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
438 {
439
440         CleanupFunctionHook *newfcn;
441
442         newfcn = (CleanupFunctionHook *)
443             malloc(sizeof(CleanupFunctionHook));
444         newfcn->next = CleanupHookTable;
445         newfcn->h_function_pointer = fcn_ptr;
446         CleanupHookTable = newfcn;
447
448         syslog(LOG_INFO, "Registered a new cleanup function\n");
449 }
450
451
452 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
453 {
454         CleanupFunctionHook *cur, *p;
455
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");
461                         p = cur->next;
462                         if (cur == CleanupHookTable) {
463                                 CleanupHookTable = p;
464                         }
465                         free(cur);
466                         cur = p;
467                 }
468         }
469 }
470
471
472 void CtdlDestroyCleanupHooks(void)
473 {
474         CleanupFunctionHook *cur, *p;
475
476         cur = CleanupHookTable;
477         while (cur != NULL)
478         {
479                 syslog(LOG_INFO, "Destroyed cleanup function\n");
480                 p = cur->next;
481                 free(cur);
482                 cur = p;
483         }
484         CleanupHookTable = NULL;
485 }
486
487 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
488 {
489
490         CleanupFunctionHook *newfcn;
491
492         newfcn = (CleanupFunctionHook *)
493             malloc(sizeof(CleanupFunctionHook));
494         newfcn->next = EVCleanupHookTable;
495         newfcn->h_function_pointer = fcn_ptr;
496         EVCleanupHookTable = newfcn;
497
498         syslog(LOG_INFO, "Registered a new cleanup function\n");
499 }
500
501
502 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
503 {
504         CleanupFunctionHook *cur, *p;
505
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");
511                         p = cur->next;
512                         if (cur == EVCleanupHookTable) {
513                                 EVCleanupHookTable = p;
514                         }
515                         free(cur);
516                         cur = p;
517                 }
518         }
519 }
520
521
522 void CtdlDestroyEVCleanupHooks(void)
523 {
524         CleanupFunctionHook *cur, *p;
525
526         cur = EVCleanupHookTable;
527         while (cur != NULL)
528         {
529                 syslog(LOG_INFO, "Destroyed cleanup function\n");
530                 p = cur->next;
531                 free(cur);
532                 cur = p;
533         }
534         EVCleanupHookTable = NULL;
535 }
536
537
538 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
539 {
540
541         SessionFunctionHook *newfcn;
542
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;
549
550         syslog(LOG_INFO, "Registered a new session function (type %d)\n",
551                 EventType);
552 }
553
554
555 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
556 {
557         SessionFunctionHook *cur, *p;
558
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",
565                                         EventType);
566                         p = cur->next;
567                         if (cur == SessionHookTable) {
568                                 SessionHookTable = p;
569                         }
570                         free(cur);
571                         cur = p;
572                 }
573         }
574 }
575
576 void CtdlDestroySessionHooks(void)
577 {
578         SessionFunctionHook *cur, *p;
579
580         cur = SessionHookTable;
581         while (cur != NULL)
582         {
583                 syslog(LOG_INFO, "Destroyed session function\n");
584                 p = cur->next;
585                 free(cur);
586                 cur = p;
587         }
588         SessionHookTable = NULL;
589 }
590
591
592 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
593 {
594
595         UserFunctionHook *newfcn;
596
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;
603
604         syslog(LOG_INFO, "Registered a new user function (type %d)\n",
605                 EventType);
606 }
607
608
609 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
610 {
611         UserFunctionHook *cur, *p;
612
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",
619                                         EventType);
620                         p = cur->next;
621                         if (cur == UserHookTable) {
622                                 UserHookTable = p;
623                         }
624                         free(cur);
625                         cur = p;
626                 }
627         }
628 }
629
630 void CtdlDestroyUserHooks(void)
631 {
632         UserFunctionHook *cur, *p;
633
634         cur = UserHookTable;
635         while (cur != NULL)
636         {
637                 syslog(LOG_INFO, "Destroyed user function \n");
638                 p = cur->next;
639                 free(cur);
640                 cur = p;
641         }
642         UserHookTable = NULL;
643 }
644
645
646 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
647                                 int EventType)
648 {
649
650         MessageFunctionHook *newfcn;
651
652         newfcn = (MessageFunctionHook *)
653             malloc(sizeof(MessageFunctionHook));
654         newfcn->next = MessageHookTable;
655         newfcn->h_function_pointer = handler;
656         newfcn->eventtype = EventType;
657         MessageHookTable = newfcn;
658
659         syslog(LOG_INFO, "Registered a new message function (type %d)\n",
660                 EventType);
661 }
662
663
664 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
665                 int EventType)
666 {
667         MessageFunctionHook *cur, *p;
668
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",
675                                         EventType);
676                         p = cur->next;
677                         if (cur == MessageHookTable) {
678                                 MessageHookTable = p;
679                         }
680                         free(cur);
681                         cur = p;
682                 }
683         }
684 }
685
686 void CtdlDestroyMessageHook(void)
687 {
688         MessageFunctionHook *cur, *p;
689
690         cur = MessageHookTable; 
691         while (cur != NULL)
692         {
693                 syslog(LOG_INFO, "Destroyed message function (type %d)\n", cur->eventtype);
694                 p = cur->next;
695                 free(cur);
696                 cur = p;
697         }
698         MessageHookTable = NULL;
699 }
700
701
702 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
703 {
704         RoomFunctionHook *newfcn;
705
706         newfcn = (RoomFunctionHook *)
707             malloc(sizeof(RoomFunctionHook));
708         newfcn->next = RoomHookTable;
709         newfcn->fcn_ptr = fcn_ptr;
710         RoomHookTable = newfcn;
711
712         syslog(LOG_INFO, "Registered a new room function\n");
713 }
714
715
716 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
717 {
718         RoomFunctionHook *cur, *p;
719
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");
723                         p = cur->next;
724                         if (cur == RoomHookTable) {
725                                 RoomHookTable = p;
726                         }
727                         free(cur);
728                         cur = p;
729                 }
730         }
731 }
732
733
734 void CtdlDestroyRoomHooks(void)
735 {
736         RoomFunctionHook *cur, *p;
737
738         cur = RoomHookTable;
739         while (cur != NULL)
740         {
741                 syslog(LOG_INFO, "Destroyed room function\n");
742                 p = cur->next;
743                 free(cur);
744                 cur = p;
745         }
746         RoomHookTable = NULL;
747 }
748
749 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
750 {
751         NetprocFunctionHook *newfcn;
752
753         newfcn = (NetprocFunctionHook *)
754             malloc(sizeof(NetprocFunctionHook));
755         newfcn->next = NetprocHookTable;
756         newfcn->h_function_pointer = handler;
757         NetprocHookTable = newfcn;
758
759         syslog(LOG_INFO, "Registered a new netproc function\n");
760 }
761
762
763 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
764 {
765         NetprocFunctionHook *cur, *p;
766
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");
772                         p = cur->next;
773                         if (cur == NetprocHookTable) {
774                                 NetprocHookTable = p;
775                         }
776                         free(cur);
777                         cur = p;
778                 }
779         }
780 }
781
782 void CtdlDestroyNetprocHooks(void)
783 {
784         NetprocFunctionHook *cur, *p;
785
786         cur = NetprocHookTable;
787         while (cur != NULL)
788         {
789                 syslog(LOG_INFO, "Destroyed netproc function\n");
790                 p = cur->next;
791                 free(cur);
792                 cur = p;
793         }
794         NetprocHookTable = NULL;
795 }
796
797
798 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
799 {
800         DeleteFunctionHook *newfcn;
801
802         newfcn = (DeleteFunctionHook *)
803             malloc(sizeof(DeleteFunctionHook));
804         newfcn->next = DeleteHookTable;
805         newfcn->h_function_pointer = handler;
806         DeleteHookTable = newfcn;
807
808         syslog(LOG_INFO, "Registered a new delete function\n");
809 }
810
811
812 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
813 {
814         DeleteFunctionHook *cur, *p;
815
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");
821                         p = cur->next;
822                         if (cur == DeleteHookTable) {
823                                 DeleteHookTable = p;
824                         }
825                         free(cur);
826                         cur = p;
827                 }
828         }
829 }
830 void CtdlDestroyDeleteHooks(void)
831 {
832         DeleteFunctionHook *cur, *p;
833
834         cur = DeleteHookTable;
835         while (cur != NULL)
836         {
837                 syslog(LOG_INFO, "Destroyed delete function\n");
838                 p = cur->next;
839                 free(cur);
840                 cur = p;                
841         }
842         DeleteHookTable = NULL;
843 }
844
845
846
847
848 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
849 {
850         FixedOutputHook *newfcn;
851
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;
858
859         syslog(LOG_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
860 }
861
862
863 void CtdlUnregisterFixedOutputHook(char *content_type)
864 {
865         FixedOutputHook *cur, *p;
866
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);
871                         p = cur->next;
872                         if (cur == FixedOutputTable) {
873                                 FixedOutputTable = p;
874                         }
875                         free(cur);
876                         cur = p;
877                 }
878         }
879 }
880
881 void CtdlDestroyFixedOutputHooks(void)
882 {
883         FixedOutputHook *cur, *p;
884
885         cur = FixedOutputTable; 
886         while (cur != NULL)
887         {
888                 syslog(LOG_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
889                 p = cur->next;
890                 free(cur);
891                 cur = p;
892                 
893         }
894         FixedOutputTable = NULL;
895 }
896
897 /* returns nonzero if we found a hook and used it */
898 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
899 {
900         FixedOutputHook *fcn;
901
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);
905                         return(1);
906                 }
907         }
908         return(0);
909 }
910
911
912
913
914
915 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
916 {
917
918         XmsgFunctionHook *newfcn;
919
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);
926 }
927
928
929 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
930 {
931         XmsgFunctionHook *cur, *p;
932
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);
940                         p = cur->next;
941                         if (cur == XmsgHookTable) {
942                                 XmsgHookTable = p;
943                         }
944                         free(cur);
945                         cur = p;
946                 }
947         }
948 }
949
950 void CtdlDestroyXmsgHooks(void)
951 {
952         XmsgFunctionHook *cur, *p;
953
954         cur = XmsgHookTable;
955         while (cur != NULL)
956         {
957                 syslog(LOG_INFO, "Destroyed x-msg function "
958                         "(priority %d)\n", cur->order);
959                 p = cur->next;
960                         
961                 free(cur);
962                 cur = p;
963         }
964         XmsgHookTable = NULL;
965 }
966
967
968 void CtdlRegisterServiceHook(int tcp_port,
969                              char *sockpath,
970                              void (*h_greeting_function) (void),
971                              void (*h_command_function) (void),
972                              void (*h_async_function) (void),
973                              const char *ServiceName)
974 {
975         ServiceFunctionHook *newfcn;
976         char *message;
977         char error[SIZ];
978
979         strcpy(error, "");
980         newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
981         message = (char*) malloc (SIZ + SIZ);
982         
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;
990
991         if (sockpath != NULL) {
992                 newfcn->msock = ctdl_uds_server(sockpath, config.c_maxsessions, error);
993                 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
994         }
995         else if (tcp_port <= 0) {       /* port -1 to disable */
996                 syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
997                 free (message);
998                 free(newfcn);
999                 return;
1000         }
1001         else {
1002                 newfcn->msock = ctdl_tcp_server(config.c_ip_addr,
1003                                               tcp_port,
1004                                               config.c_maxsessions, 
1005                                               error);
1006                 snprintf(message, SIZ, "TCP port %s:%d: (%s) ", 
1007                          config.c_ip_addr, tcp_port, ServiceName);
1008         }
1009
1010         if (newfcn->msock > 0) {
1011                 ServiceHookTable = newfcn;
1012                 strcat(message, "registered.");
1013                 syslog(LOG_INFO, "%s\n", message);
1014         }
1015         else {
1016                 AddPortError(message, error);
1017                 strcat(message, "FAILED.");
1018                 syslog(LOG_CRIT, "%s\n", message);
1019                 free(newfcn);
1020         }
1021         free(message);
1022 }
1023
1024
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)
1029                         )
1030 {
1031         ServiceFunctionHook *cur, *p;
1032
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) {
1043                         close(cur->msock);
1044                         if (sockpath) {
1045                                 syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1046                                                 sockpath);
1047                                 unlink(sockpath);
1048                         } else if (tcp_port) {
1049                                 syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1050                         } else {
1051                                 syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1052                         }
1053                         p = cur->next;
1054                         if (cur == ServiceHookTable) {
1055                                 ServiceHookTable = p;
1056                         }
1057                         free(cur);
1058                         cur = p;
1059                 }
1060         }
1061 }
1062
1063
1064 void CtdlShutdownServiceHooks(void)
1065 {
1066         /* sort of a duplicate of close_masters() but called earlier */
1067         ServiceFunctionHook *cur;
1068
1069         cur = ServiceHookTable;
1070         while (cur != NULL) 
1071         {
1072                 if (cur->msock != -1)
1073                 {
1074                         close(cur->msock);
1075                         cur->msock = -1;
1076                         if (cur->sockpath != NULL){
1077                                 syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1078                                               cur->ServiceName,
1079                                               cur->sockpath);
1080                                 unlink(cur->sockpath);
1081                         } else {
1082                                 syslog(LOG_INFO, "[%s] closing service\n", 
1083                                               cur->ServiceName);
1084                         }
1085                 }
1086                 cur = cur->next;
1087         }
1088 }
1089
1090 void CtdlDestroyServiceHook(void)
1091 {
1092         ServiceFunctionHook *cur, *p;
1093
1094         cur = ServiceHookTable;
1095         while (cur != NULL)
1096         {
1097                 close(cur->msock);
1098                 if (cur->sockpath) {
1099                         syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1100                                 cur->sockpath);
1101                         unlink(cur->sockpath);
1102                 } else if (cur->tcp_port) {
1103                         syslog(LOG_INFO, "Closed TCP port %d\n", cur->tcp_port);
1104                 } else {
1105                         syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1106                 }
1107                 p = cur->next;
1108                 free(cur);
1109                 cur = p;
1110         }
1111         ServiceHookTable = NULL;
1112 }
1113
1114 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1115 {
1116         SearchFunctionHook *newfcn;
1117
1118         if (!name || !fcn_ptr) {
1119                 return;
1120         }
1121         
1122         newfcn = (SearchFunctionHook *)
1123             malloc(sizeof(SearchFunctionHook));
1124         newfcn->next = SearchFunctionHookTable;
1125         newfcn->name = name;
1126         newfcn->fcn_ptr = fcn_ptr;
1127         SearchFunctionHookTable = newfcn;
1128
1129         syslog(LOG_INFO, "Registered a new search function (%s)\n", name);
1130 }
1131
1132 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1133 {
1134         SearchFunctionHook *cur, *p;
1135         
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);
1139                         p = cur->next;
1140                         if (cur == SearchFunctionHookTable) {
1141                                 SearchFunctionHookTable = p;
1142                         }
1143                         free (cur);
1144                         cur = p;
1145                 }
1146         }
1147 }
1148
1149 void CtdlDestroySearchHooks(void)
1150 {
1151         SearchFunctionHook *cur, *p;
1152
1153         cur = SearchFunctionHookTable;
1154         SearchFunctionHookTable = NULL;
1155         while (cur != NULL) {
1156                 p = cur->next;
1157                 free(cur);
1158                 cur = p;
1159         }
1160 }
1161
1162 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1163 {
1164         SearchFunctionHook *fcn = NULL;
1165
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);
1169                         return;
1170                 }
1171         }
1172         *num_msgs = 0;
1173 }
1174
1175
1176 void PerformSessionHooks(int EventType)
1177 {
1178         SessionFunctionHook *fcn = NULL;
1179
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 */
1184                         }
1185                         (*fcn->h_function_pointer) ();
1186                 }
1187         }
1188 }
1189
1190 void PerformUserHooks(ctdluser *usbuf, int EventType)
1191 {
1192         UserFunctionHook *fcn = NULL;
1193
1194         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1195                 if (fcn->eventtype == EventType) {
1196                         (*fcn->h_function_pointer) (usbuf);
1197                 }
1198         }
1199 }
1200
1201 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
1202 {
1203         MessageFunctionHook *fcn = NULL;
1204         int total_retval = 0;
1205
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);
1209          */
1210         if (msg->cm_flags & CM_SKIP_HOOKS) {
1211                 syslog(LOG_DEBUG, "Skipping hooks\n");
1212                 return(0);
1213         }
1214
1215         /* Otherwise, run all the hooks appropriate to this event type.
1216          */
1217         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1218                 if (fcn->eventtype == EventType) {
1219                         total_retval = total_retval + (*fcn->h_function_pointer) (msg);
1220                 }
1221         }
1222
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.
1226          */
1227         return total_retval;
1228 }
1229
1230
1231 int PerformRoomHooks(struct ctdlroom *target_room)
1232 {
1233         RoomFunctionHook *fcn;
1234         int total_retval = 0;
1235
1236         syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1237
1238         for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1239                 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1240         }
1241
1242         /* Return the sum of the return codes from the hook functions.
1243          */
1244         return total_retval;
1245 }
1246
1247
1248 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1249 {
1250         NetprocFunctionHook *fcn;
1251         int total_retval = 0;
1252
1253         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1254                 total_retval = total_retval +
1255                         (*fcn->h_function_pointer) (msg, target_room);
1256         }
1257
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.
1260          */
1261         return total_retval;
1262 }
1263
1264
1265 void PerformDeleteHooks(char *room, long msgnum)
1266 {
1267         DeleteFunctionHook *fcn;
1268
1269         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1270                 (*fcn->h_function_pointer) (room, msgnum);
1271         }
1272 }
1273
1274
1275
1276
1277
1278 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1279 {
1280         XmsgFunctionHook *fcn;
1281         int total_sent = 0;
1282         int p;
1283
1284         for (p=0; p<MAX_XMSG_PRI; ++p) {
1285                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1286                         if (fcn->order == p) {
1287                                 total_sent +=
1288                                         (*fcn->h_function_pointer)
1289                                                 (sender, sender_email, recp, msg);
1290                         }
1291                 }
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
1295                  * remote services.
1296                  */
1297                 if (total_sent) break;
1298         }
1299         return total_sent;
1300 }
1301
1302
1303 /*
1304  * Dirty hack until we impliment a hook mechanism for this
1305  */
1306 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1307 {
1308 #ifdef HAVE_OPENSSL
1309         CtdlStartTLS (ok_response, nosup_response, error_response);
1310 #endif
1311 }