ctdl_tcp_server and ctdl_uds_server improved handling of error
[citadel.git] / citadel / serv_extensions.c
1 /*
2  * Citadel Extension Loader
3  * Originally written by Brian Costello <btx@calyx.net>
4  *
5  * Copyright (c) 1987-2019 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, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <sys/stat.h>
20 #include <libcitadel.h>
21 #include "sysdep_decls.h"
22 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
23 #include "serv_extensions.h"
24 #include "ctdl_module.h"
25 #include "config.h"
26
27
28 /*
29  * Structure defentitions for hook tables
30  */
31
32 typedef struct FixedOutputHook FixedOutputHook;
33 struct FixedOutputHook {
34         FixedOutputHook *next;
35         char content_type[64];
36         void (*h_function_pointer) (char *, int);
37 };
38 FixedOutputHook *FixedOutputTable = NULL;
39
40
41 /*
42  * SessionFunctionHook extensions are used for any type of hook for which
43  * the context in which it's being called (which is determined by the event
44  * type) will make it obvious for the hook function to know where to look for
45  * pertinent data.
46  */
47 typedef struct SessionFunctionHook SessionFunctionHook;
48 struct SessionFunctionHook {
49         SessionFunctionHook *next;
50         int Priority;
51         void (*h_function_pointer) (void);
52         int eventtype;
53 };
54 SessionFunctionHook *SessionHookTable = NULL;
55
56
57 /*
58  * UserFunctionHook extensions are used for any type of hook which implements
59  * an operation on a user or username (potentially) other than the one
60  * operating the current session.
61  */
62 typedef struct UserFunctionHook UserFunctionHook;
63 struct UserFunctionHook {
64         UserFunctionHook *next;
65         void (*h_function_pointer) (struct ctdluser *usbuf);
66         int eventtype;
67 };
68 UserFunctionHook *UserHookTable = NULL;
69
70
71 /*
72  * MessageFunctionHook extensions are used for hooks which implement handlers
73  * for various types of message operations (save, read, etc.)
74  */
75 typedef struct MessageFunctionHook MessageFunctionHook;
76 struct MessageFunctionHook {
77         MessageFunctionHook *next;
78         int (*h_function_pointer) (struct CtdlMessage *msg, recptypes *recps);
79         int eventtype;
80 };
81 MessageFunctionHook *MessageHookTable = NULL;
82
83
84 /*
85  * DeleteFunctionHook extensions are used for hooks which get called when a
86  * message is about to be deleted.
87  */
88 typedef struct DeleteFunctionHook DeleteFunctionHook;
89 struct DeleteFunctionHook {
90         DeleteFunctionHook *next;
91         void (*h_function_pointer) (char *target_room, long msgnum);
92 };
93 DeleteFunctionHook *DeleteHookTable = NULL;
94
95
96 /*
97  * ExpressMessageFunctionHook extensions are used for hooks which implement
98  * the sending of an instant message through various channels.  Any function
99  * registered should return the number of recipients to whom the message was
100  * successfully transmitted.
101  */
102 typedef struct XmsgFunctionHook XmsgFunctionHook;
103 struct XmsgFunctionHook {
104         XmsgFunctionHook *next;
105         int (*h_function_pointer) (char *, char *, char *, char *);
106         int order;
107 };
108 XmsgFunctionHook *XmsgHookTable = NULL;
109
110
111 /*
112  * RoomFunctionHook extensions are used for hooks which impliment room
113  * processing functions when new messages are added EG. SIEVE.
114  */
115 typedef struct RoomFunctionHook RoomFunctionHook;
116 struct RoomFunctionHook {
117         RoomFunctionHook *next;
118         int (*fcn_ptr) (struct ctdlroom *);
119 };
120 RoomFunctionHook *RoomHookTable = NULL;
121
122
123 typedef struct SearchFunctionHook SearchFunctionHook;
124 struct SearchFunctionHook {
125         SearchFunctionHook *next;
126         void (*fcn_ptr) (int *, long **, const char *);
127         char *name;
128 };
129 SearchFunctionHook *SearchFunctionHookTable = NULL;
130
131 CleanupFunctionHook *CleanupHookTable = NULL;
132 CleanupFunctionHook *EVCleanupHookTable = NULL;
133
134 ServiceFunctionHook *ServiceHookTable = NULL;
135
136 typedef struct ProtoFunctionHook ProtoFunctionHook;
137 struct ProtoFunctionHook {
138         void (*handler) (char *cmdbuf);
139         const char *cmd;
140         const char *desc;
141 };
142
143 HashList *ProtoHookList = NULL;
144
145
146 #define ERR_PORT (1 << 1)
147
148
149 static StrBuf *portlist = NULL;
150 static StrBuf *errormessages = NULL;
151
152
153 long   DetailErrorFlags;
154 ConstStr Empty = {HKEY("")};
155 char *ErrSubject = "Startup Problems";
156 ConstStr ErrGeneral[] = {
157         {HKEY("Citadel had trouble on starting up. ")},
158         {HKEY(" This means, Citadel won't be the service provider for a specific service you configured it to.\n\n"
159               "If you don't want Citadel to provide these services, turn them off in WebCit via: ")},
160         {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
161               "The errors returned by the system were:\n")},
162         {HKEY("You can recheck the above if you follow this faq item:\n"
163               "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
164 };
165
166 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by Citadel Server.\n")};
167 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
168 ConstStr ErrPortHint  = { HKEY("If you want Citadel to provide you with that functionality, "
169                                "check the output of \"netstat -lnp\" on Linux, or \"netstat -na\" on BSD"
170                                " and disable the program that binds these ports.\n")};
171
172
173 void LogPrintMessages(long err)
174 {
175         StrBuf *Message;
176         StrBuf *List, *DetailList;
177         ConstStr *Short, *Where, *Hint; 
178
179         
180         Message = NewStrBufPlain(NULL, StrLength(portlist) + StrLength(errormessages));
181         
182         DetailErrorFlags = DetailErrorFlags & ~err;
183
184         switch (err)
185         {
186         case ERR_PORT:
187                 Short = &ErrPortShort;
188                 Where = &ErrPortWhere;
189                 Hint  = &ErrPortHint;
190                 List  = portlist;
191                 DetailList = errormessages;
192                 break;
193         default:
194                 Short = &Empty;
195                 Where = &Empty;
196                 Hint  = &Empty;
197                 List  = NULL;
198                 DetailList = NULL;
199         }
200
201         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
202         StrBufAppendBufPlain(Message, CKEY(*Short), 0); 
203         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
204         StrBufAppendBufPlain(Message, CKEY(*Where), 0);
205         StrBufAppendBuf(Message, List, 0);
206         StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
207         StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
208         StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
209         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
210         StrBufAppendBuf(Message, DetailList, 0);
211         StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
212         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
213
214         syslog(LOG_EMERG, "extensions: %s", ChrPtr(Message));
215         syslog(LOG_EMERG, "extensions: %s", ErrSubject);
216         quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
217
218         FreeStrBuf(&Message);
219         FreeStrBuf(&List);
220         FreeStrBuf(&DetailList);
221 }
222
223
224 void AddPortError(char *Port, char *ErrorMessage)
225 {
226         long len;
227
228         DetailErrorFlags |= ERR_PORT;
229
230         len = StrLength(errormessages);
231         if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
232         else errormessages = NewStrBuf();
233         StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
234
235
236         len = StrLength(portlist);
237         if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
238         else portlist = NewStrBuf();
239         StrBufAppendBufPlain(portlist, Port, -1, 0);
240 }
241
242
243 int DLoader_Exec_Cmd(char *cmdbuf)
244 {
245         void *vP;
246         ProtoFunctionHook *p;
247
248         if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
249                 p = (ProtoFunctionHook*) vP;
250                 p->handler(&cmdbuf[5]);
251                 return 1;
252         }
253         return 0;
254 }
255
256
257 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
258 {
259         ProtoFunctionHook *p;
260
261         if (ProtoHookList == NULL)
262                 ProtoHookList = NewHash (1, FourHash);
263
264
265         p = (ProtoFunctionHook *)
266                 malloc(sizeof(ProtoFunctionHook));
267
268         if (p == NULL) {
269                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
270                 exit(EXIT_FAILURE);
271         }
272         p->handler = handler;
273         p->cmd = cmd;
274         p->desc = desc;
275
276         Put(ProtoHookList, cmd, 4, p, NULL);
277         syslog(LOG_DEBUG, "extensions: registered server command %s (%s)", cmd, desc);
278 }
279
280 void CtdlDestroyProtoHooks(void)
281 {
282
283         DeleteHash(&ProtoHookList);
284 }
285
286
287 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
288 {
289
290         CleanupFunctionHook *newfcn;
291
292         newfcn = (CleanupFunctionHook *)
293             malloc(sizeof(CleanupFunctionHook));
294         newfcn->next = CleanupHookTable;
295         newfcn->h_function_pointer = fcn_ptr;
296         CleanupHookTable = newfcn;
297
298         syslog(LOG_DEBUG, "extensions: registered a new cleanup function");
299 }
300
301
302 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
303 {
304         CleanupFunctionHook *cur, *p, *last;
305         last = NULL;
306         cur = CleanupHookTable;
307         while (cur != NULL)
308         {
309                 if (fcn_ptr == cur->h_function_pointer)
310                 {
311                         syslog(LOG_DEBUG, "extensions: unregistered cleanup function");
312                         p = cur->next;
313
314                         free(cur);
315                         cur = NULL;
316
317                         if (last != NULL)
318                                 last->next = p;
319                         else 
320                                 CleanupHookTable = p;
321                         cur = p;
322                 }
323                 else {
324                         last = cur;
325                         cur = cur->next;
326                 }
327         }
328 }
329
330
331 void CtdlDestroyCleanupHooks(void)
332 {
333         CleanupFunctionHook *cur, *p;
334
335         cur = CleanupHookTable;
336         while (cur != NULL)
337         {
338                 syslog(LOG_DEBUG, "extensions: destroyed cleanup function");
339                 p = cur->next;
340                 free(cur);
341                 cur = p;
342         }
343         CleanupHookTable = NULL;
344 }
345
346 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
347 {
348
349         CleanupFunctionHook *newfcn;
350
351         newfcn = (CleanupFunctionHook *)
352             malloc(sizeof(CleanupFunctionHook));
353         newfcn->next = EVCleanupHookTable;
354         newfcn->h_function_pointer = fcn_ptr;
355         EVCleanupHookTable = newfcn;
356
357         syslog(LOG_DEBUG, "extensions: registered a new cleanup function");
358 }
359
360
361 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
362 {
363         CleanupFunctionHook *cur, *p, *last;
364         last = NULL;
365         cur = EVCleanupHookTable;
366         while (cur != NULL)
367         {
368                 if (fcn_ptr == cur->h_function_pointer)
369                 {
370                         syslog(LOG_DEBUG, "extensions: unregistered cleanup function");
371                         p = cur->next;
372
373                         free(cur);
374                         cur = NULL;
375
376                         if (last != NULL)
377                                 last->next = p;
378                         else 
379                                 EVCleanupHookTable = p;
380                         cur = p;
381                 }
382                 else {
383                         last = cur;
384                         cur = cur->next;
385                 }
386         }
387 }
388
389
390 void CtdlDestroyEVCleanupHooks(void)
391 {
392         CleanupFunctionHook *cur, *p;
393
394         cur = EVCleanupHookTable;
395         while (cur != NULL)
396         {
397                 syslog(LOG_DEBUG, "extensions: destroyed cleanup function");
398                 p = cur->next;
399                 cur->h_function_pointer();
400                 free(cur);
401                 cur = p;
402         }
403         EVCleanupHookTable = NULL;
404 }
405
406
407
408
409 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
410 {
411         SessionFunctionHook *newfcn;
412
413         newfcn = (SessionFunctionHook *)
414             malloc(sizeof(SessionFunctionHook));
415         newfcn->Priority = Priority;
416         newfcn->h_function_pointer = fcn_ptr;
417         newfcn->eventtype = EventType;
418
419         SessionFunctionHook **pfcn;
420         pfcn = &SessionHookTable;
421         while ((*pfcn != NULL) && 
422                ((*pfcn)->Priority < newfcn->Priority) &&
423                ((*pfcn)->next != NULL))
424                 pfcn = &(*pfcn)->next;
425                 
426         newfcn->next = *pfcn;
427         *pfcn = newfcn;
428         
429         syslog(LOG_DEBUG, "extensions: registered a new session function (type %d Priority %d)", EventType, Priority);
430 }
431
432
433 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
434 {
435         SessionFunctionHook *cur, *p, *last;
436         last = NULL;
437         cur = SessionHookTable;
438         while  (cur != NULL) {
439                 if ((fcn_ptr == cur->h_function_pointer) &&
440                     (EventType == cur->eventtype))
441                 {
442                         syslog(LOG_DEBUG, "extensions: unregistered session function (type %d)", EventType);
443                         p = cur->next;
444
445                         free(cur);
446                         cur = NULL;
447
448                         if (last != NULL)
449                                 last->next = p;
450                         else 
451                                 SessionHookTable = p;
452                         cur = p;
453                 }
454                 else {
455                         last = cur;
456                         cur = cur->next;
457                 }
458         }
459 }
460
461 void CtdlDestroySessionHooks(void)
462 {
463         SessionFunctionHook *cur, *p;
464
465         cur = SessionHookTable;
466         while (cur != NULL)
467         {
468                 syslog(LOG_DEBUG, "extensions: destroyed session function");
469                 p = cur->next;
470                 free(cur);
471                 cur = p;
472         }
473         SessionHookTable = NULL;
474 }
475
476
477 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
478 {
479
480         UserFunctionHook *newfcn;
481
482         newfcn = (UserFunctionHook *)
483             malloc(sizeof(UserFunctionHook));
484         newfcn->next = UserHookTable;
485         newfcn->h_function_pointer = fcn_ptr;
486         newfcn->eventtype = EventType;
487         UserHookTable = newfcn;
488
489         syslog(LOG_DEBUG, "extensions: registered a new user function (type %d)",
490                    EventType);
491 }
492
493
494 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
495 {
496         UserFunctionHook *cur, *p, *last;
497         last = NULL;
498         cur = UserHookTable;
499         while (cur != NULL) {
500                 if ((fcn_ptr == cur->h_function_pointer) &&
501                     (EventType == cur->eventtype))
502                 {
503                         syslog(LOG_DEBUG, "extensions: unregistered user function (type %d)", EventType);
504                         p = cur->next;
505
506                         free(cur);
507                         cur = NULL;
508
509                         if (last != NULL)
510                                 last->next = p;
511                         else 
512                                 UserHookTable = p;
513                         cur = p;
514                 }
515                 else {
516                         last = cur;
517                         cur = cur->next;
518                 }
519         }
520 }
521
522 void CtdlDestroyUserHooks(void)
523 {
524         UserFunctionHook *cur, *p;
525
526         cur = UserHookTable;
527         while (cur != NULL)
528         {
529                 syslog(LOG_DEBUG, "extensions: destroyed user function");
530                 p = cur->next;
531                 free(cur);
532                 cur = p;
533         }
534         UserHookTable = NULL;
535 }
536
537
538 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *), int EventType)
539 {
540
541         MessageFunctionHook *newfcn;
542
543         newfcn = (MessageFunctionHook *)
544             malloc(sizeof(MessageFunctionHook));
545         newfcn->next = MessageHookTable;
546         newfcn->h_function_pointer = handler;
547         newfcn->eventtype = EventType;
548         MessageHookTable = newfcn;
549
550         syslog(LOG_DEBUG, "extensions: registered a new message function (type %d)", EventType);
551 }
552
553
554 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *), int EventType)
555 {
556         MessageFunctionHook *cur, *p, *last;
557         last = NULL;
558         cur = MessageHookTable;
559         while (cur != NULL) {
560                 if ((handler == cur->h_function_pointer) &&
561                     (EventType == cur->eventtype))
562                 {
563                         syslog(LOG_DEBUG, "extensions: unregistered message function (type %d)", EventType);
564                         p = cur->next;
565                         free(cur);
566                         cur = NULL;
567
568                         if (last != NULL)
569                                 last->next = p;
570                         else 
571                                 MessageHookTable = p;
572                         cur = p;
573                 }
574                 else {
575                         last = cur;
576                         cur = cur->next;
577                 }
578         }
579 }
580
581 void CtdlDestroyMessageHook(void)
582 {
583         MessageFunctionHook *cur, *p;
584
585         cur = MessageHookTable; 
586         while (cur != NULL)
587         {
588                 syslog(LOG_DEBUG, "extensions: destroyed message function (type %d)", cur->eventtype);
589                 p = cur->next;
590                 free(cur);
591                 cur = p;
592         }
593         MessageHookTable = NULL;
594 }
595
596
597 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
598 {
599         RoomFunctionHook *newfcn;
600
601         newfcn = (RoomFunctionHook *)
602             malloc(sizeof(RoomFunctionHook));
603         newfcn->next = RoomHookTable;
604         newfcn->fcn_ptr = fcn_ptr;
605         RoomHookTable = newfcn;
606
607         syslog(LOG_DEBUG, "extensions: registered a new room function");
608 }
609
610
611 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
612 {
613         RoomFunctionHook *cur, *p, *last;
614         last = NULL;
615         cur = RoomHookTable;
616         while (cur != NULL)
617         {
618                 if (fcn_ptr == cur->fcn_ptr) {
619                         syslog(LOG_DEBUG, "extensions: unregistered room function");
620                         p = cur->next;
621
622                         free(cur);
623                         cur = NULL;
624
625                         if (last != NULL)
626                                 last->next = p;
627                         else 
628                                 RoomHookTable = p;
629                         cur = p;
630                 }
631                 else {
632                         last = cur;
633                         cur = cur->next;
634                 }
635         }
636 }
637
638
639 void CtdlDestroyRoomHooks(void)
640 {
641         RoomFunctionHook *cur, *p;
642
643         cur = RoomHookTable;
644         while (cur != NULL)
645         {
646                 syslog(LOG_DEBUG, "extensions: destroyed room function");
647                 p = cur->next;
648                 free(cur);
649                 cur = p;
650         }
651         RoomHookTable = NULL;
652 }
653
654
655 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
656 {
657         DeleteFunctionHook *newfcn;
658
659         newfcn = (DeleteFunctionHook *)
660             malloc(sizeof(DeleteFunctionHook));
661         newfcn->next = DeleteHookTable;
662         newfcn->h_function_pointer = handler;
663         DeleteHookTable = newfcn;
664
665         syslog(LOG_DEBUG, "extensions: registered a new delete function");
666 }
667
668
669 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
670 {
671         DeleteFunctionHook *cur, *p, *last;
672
673         last = NULL;
674         cur = DeleteHookTable;
675         while (cur != NULL) {
676                 if (handler == cur->h_function_pointer )
677                 {
678                         syslog(LOG_DEBUG, "extensions: unregistered delete function");
679                         p = cur->next;
680                         free(cur);
681
682                         if (last != NULL)
683                                 last->next = p;
684                         else
685                                 DeleteHookTable = p;
686
687                         cur = p;
688                 }
689                 else {
690                         last = cur;
691                         cur = cur->next;
692                 }
693         }
694 }
695
696
697 void CtdlDestroyDeleteHooks(void)
698 {
699         DeleteFunctionHook *cur, *p;
700
701         cur = DeleteHookTable;
702         while (cur != NULL)
703         {
704                 syslog(LOG_DEBUG, "extensions: destroyed delete function");
705                 p = cur->next;
706                 free(cur);
707                 cur = p;                
708         }
709         DeleteHookTable = NULL;
710 }
711
712
713 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
714 {
715         FixedOutputHook *newfcn;
716
717         newfcn = (FixedOutputHook *)
718             malloc(sizeof(FixedOutputHook));
719         newfcn->next = FixedOutputTable;
720         newfcn->h_function_pointer = handler;
721         safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
722         FixedOutputTable = newfcn;
723
724         syslog(LOG_DEBUG, "extensions: registered a new fixed output function for %s", newfcn->content_type);
725 }
726
727
728 void CtdlUnregisterFixedOutputHook(char *content_type)
729 {
730         FixedOutputHook *cur, *p, *last;
731
732         last = NULL;
733         cur = FixedOutputTable;
734         while (cur != NULL) {
735                 /* This will also remove duplicates if any */
736                 if (!strcasecmp(content_type, cur->content_type)) {
737                         syslog(LOG_DEBUG, "extensions: unregistered fixed output function for %s", content_type);
738                         p = cur->next;
739                         free(cur);
740
741                         if (last != NULL)
742                                 last->next = p;
743                         else
744                                 FixedOutputTable = p;
745                         
746                         cur = p;
747                 }
748                 else
749                 {
750                         last = cur;
751                         cur = cur->next;
752                 }
753         }
754 }
755
756 void CtdlDestroyFixedOutputHooks(void)
757 {
758         FixedOutputHook *cur, *p;
759
760         cur = FixedOutputTable; 
761         while (cur != NULL)
762         {
763                 syslog(LOG_DEBUG, "extensions: destroyed fixed output function for %s", cur->content_type);
764                 p = cur->next;
765                 free(cur);
766                 cur = p;
767                 
768         }
769         FixedOutputTable = NULL;
770 }
771
772 /* returns nonzero if we found a hook and used it */
773 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
774 {
775         FixedOutputHook *fcn;
776
777         for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
778                 if (!strcasecmp(content_type, fcn->content_type)) {
779                         (*fcn->h_function_pointer) (content, content_length);
780                         return(1);
781                 }
782         }
783         return(0);
784 }
785
786
787 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
788 {
789
790         XmsgFunctionHook *newfcn;
791
792         newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
793         newfcn->next = XmsgHookTable;
794         newfcn->order = order;
795         newfcn->h_function_pointer = fcn_ptr;
796         XmsgHookTable = newfcn;
797         syslog(LOG_DEBUG, "extensions: registered a new x-msg function (priority %d)", order);
798 }
799
800
801 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
802 {
803         XmsgFunctionHook *cur, *p, *last;
804
805         last = NULL;
806         cur = XmsgHookTable;
807         while (cur != NULL) {
808                 /* This will also remove duplicates if any */
809                 if (fcn_ptr == cur->h_function_pointer &&
810                     order == cur->order) {
811                         syslog(LOG_DEBUG, "extensions: unregistered x-msg function (priority %d)", order);
812                         p = cur->next;
813                         free(cur);
814
815                         if (last != NULL) {
816                                 last->next = p;
817                         }
818                         else {
819                                 XmsgHookTable = p;
820                         }
821                         cur = p;
822                 }
823                 else {
824                         last = cur;
825                         cur = cur->next;
826                 }
827         }
828 }
829
830
831 void CtdlDestroyXmsgHooks(void)
832 {
833         XmsgFunctionHook *cur, *p;
834
835         cur = XmsgHookTable;
836         while (cur != NULL)
837         {
838                 syslog(LOG_DEBUG, "extensions: destroyed x-msg function (priority %d)", cur->order);
839                 p = cur->next;
840                 free(cur);
841                 cur = p;
842         }
843         XmsgHookTable = NULL;
844 }
845
846
847 void CtdlRegisterServiceHook(int tcp_port,
848                              char *sockpath,
849                              void (*h_greeting_function) (void),
850                              void (*h_command_function) (void),
851                              void (*h_async_function) (void),
852                              const char *ServiceName)
853 {
854         ServiceFunctionHook *newfcn;
855         char *message;
856
857         newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
858         message = (char*) malloc (SIZ + SIZ);
859         
860         newfcn->next = ServiceHookTable;
861         newfcn->tcp_port = tcp_port;
862         newfcn->sockpath = sockpath;
863         newfcn->h_greeting_function = h_greeting_function;
864         newfcn->h_command_function = h_command_function;
865         newfcn->h_async_function = h_async_function;
866         newfcn->ServiceName = ServiceName;
867
868         if (sockpath != NULL) {
869                 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"));
870                 snprintf(message, SIZ, "extensions: unix domain socket '%s': ", sockpath);
871         }
872         else if (tcp_port <= 0) {       /* port -1 to disable */
873                 syslog(LOG_INFO, "extensions: service %s has been manually disabled, skipping", ServiceName);
874                 free (message);
875                 free(newfcn);
876                 return;
877         }
878         else {
879                 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"), tcp_port, CtdlGetConfigInt("c_maxsessions"));
880                 snprintf(message, SIZ, "extensions: TCP port %s:%d: (%s) ", 
881                          CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
882         }
883
884         if (newfcn->msock > 0) {
885                 ServiceHookTable = newfcn;
886                 strcat(message, "registered.");
887                 syslog(LOG_INFO, "%s", message);
888         }
889         else {
890                 AddPortError(message, "failed");
891                 strcat(message, "FAILED.");
892                 syslog(LOG_ERR, "%s", message);
893                 free(newfcn);
894         }
895         free(message);
896 }
897
898
899 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
900                         void (*h_greeting_function) (void),
901                         void (*h_command_function) (void),
902                         void (*h_async_function) (void)
903                         )
904 {
905         ServiceFunctionHook *cur, *p, *last;
906
907         last = NULL;
908         cur = ServiceHookTable;
909         while (cur != NULL) {
910                 /* This will also remove duplicates if any */
911                 if (h_greeting_function == cur->h_greeting_function &&
912                     h_command_function == cur->h_command_function &&
913                     h_async_function == cur->h_async_function &&
914                     tcp_port == cur->tcp_port && 
915                     !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
916                 {
917                         if (cur->msock > 0)
918                                 close(cur->msock);
919                         if (sockpath) {
920                                 syslog(LOG_INFO, "extensions: closed UNIX domain socket %s", sockpath);
921                                 unlink(sockpath);
922                         } else if (tcp_port) {
923                                 syslog(LOG_INFO, "extensions: closed TCP port %d", tcp_port);
924                         } else {
925                                 syslog(LOG_INFO, "extensions: unregistered service \"%s\"", cur->ServiceName);
926                         }
927                         p = cur->next;
928                         free(cur);
929                         if (last != NULL)
930                                 last->next = p;
931                         else
932                                 ServiceHookTable = p;
933                         cur = p;
934                 }
935                 else {
936                         last = cur;
937                         cur = cur->next;
938                 }
939         }
940 }
941
942
943 void CtdlShutdownServiceHooks(void)
944 {
945         /* sort of a duplicate of close_masters() but called earlier */
946         ServiceFunctionHook *cur;
947
948         cur = ServiceHookTable;
949         while (cur != NULL) 
950         {
951                 if (cur->msock != -1)
952                 {
953                         close(cur->msock);
954                         cur->msock = -1;
955                         if (cur->sockpath != NULL){
956                                 syslog(LOG_INFO, "extensions: [%s] Closed UNIX domain socket %s", cur->ServiceName, cur->sockpath);
957                                 unlink(cur->sockpath);
958                         } else {
959                                 syslog(LOG_INFO, "extensions: [%s] closing service", cur->ServiceName);
960                         }
961                 }
962                 cur = cur->next;
963         }
964 }
965
966
967 void CtdlDestroyServiceHook(void)
968 {
969         const char *Text;
970         ServiceFunctionHook *cur, *p;
971
972         cur = ServiceHookTable;
973         while (cur != NULL)
974         {
975                 if (cur->msock != -1)
976                 {
977                         close(cur->msock);
978                         Text = "Closed";
979                 }
980                 else
981                 {
982                         Text = " Not closing again";
983                 }
984
985                 if (cur->sockpath) {
986                         syslog(LOG_INFO, "extensions: %s UNIX domain socket %s", Text, cur->sockpath);
987                         unlink(cur->sockpath);
988                 } else if (cur->tcp_port) {
989                         syslog(LOG_INFO, "extensions: %s TCP port %d", Text, cur->tcp_port);
990                 } else {
991                         syslog(LOG_INFO, "extensions: destroyed service \"%s\"", cur->ServiceName);
992                 }
993                 p = cur->next;
994                 free(cur);
995                 cur = p;
996         }
997         ServiceHookTable = NULL;
998 }
999
1000 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1001 {
1002         SearchFunctionHook *newfcn;
1003
1004         if (!name || !fcn_ptr) {
1005                 return;
1006         }
1007         
1008         newfcn = (SearchFunctionHook *)
1009             malloc(sizeof(SearchFunctionHook));
1010         newfcn->next = SearchFunctionHookTable;
1011         newfcn->name = name;
1012         newfcn->fcn_ptr = fcn_ptr;
1013         SearchFunctionHookTable = newfcn;
1014
1015         syslog(LOG_DEBUG, "extensions: registered a new search function (%s)", name);
1016 }
1017
1018 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1019 {
1020         SearchFunctionHook *cur, *p, *last;
1021         
1022         last = NULL;
1023         cur = SearchFunctionHookTable;
1024         while (cur != NULL) {
1025                 if (fcn_ptr &&
1026                     (cur->fcn_ptr == fcn_ptr) &&
1027                     name && !strcmp(name, cur->name))
1028                 {
1029                         syslog(LOG_DEBUG, "extensions: unregistered search function(%s)", name);
1030                         p = cur->next;
1031                         free (cur);
1032                         if (last != NULL)
1033                                 last->next = p;
1034                         else
1035                                 SearchFunctionHookTable = p;
1036                         cur = p;
1037                 }
1038                 else {
1039                         last = cur;
1040                         cur = cur->next;
1041                 }
1042         }
1043 }
1044
1045 void CtdlDestroySearchHooks(void)
1046 {
1047         SearchFunctionHook *cur, *p;
1048
1049         cur = SearchFunctionHookTable;
1050         SearchFunctionHookTable = NULL;
1051         while (cur != NULL) {
1052                 p = cur->next;
1053                 free(cur);
1054                 cur = p;
1055         }
1056 }
1057
1058 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1059 {
1060         SearchFunctionHook *fcn = NULL;
1061
1062         for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1063                 if (!func_name || !strcmp(func_name, fcn->name)) {
1064                         (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1065                         return;
1066                 }
1067         }
1068         *num_msgs = 0;
1069 }
1070
1071
1072 void PerformSessionHooks(int EventType)
1073 {
1074         SessionFunctionHook *fcn = NULL;
1075
1076         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1077                 if (fcn->eventtype == EventType) {
1078                         if (EventType == EVT_TIMER) {
1079                                 pthread_setspecific(MyConKey, NULL);    /* for every hook */
1080                         }
1081                         (*fcn->h_function_pointer) ();
1082                 }
1083         }
1084 }
1085
1086 void PerformUserHooks(ctdluser *usbuf, int EventType)
1087 {
1088         UserFunctionHook *fcn = NULL;
1089
1090         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1091                 if (fcn->eventtype == EventType) {
1092                         (*fcn->h_function_pointer) (usbuf);
1093                 }
1094         }
1095 }
1096
1097 int PerformMessageHooks(struct CtdlMessage *msg, recptypes *recps, int EventType)
1098 {
1099         MessageFunctionHook *fcn = NULL;
1100         int total_retval = 0;
1101
1102         /* Other code may elect to protect this message from server-side
1103          * handlers; if this is the case, don't do anything.
1104          */
1105         if (msg->cm_flags & CM_SKIP_HOOKS) {
1106                 return(0);
1107         }
1108
1109         /* Otherwise, run all the hooks appropriate to this event type.
1110          */
1111         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1112                 if (fcn->eventtype == EventType) {
1113                         total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
1114                 }
1115         }
1116
1117         /* Return the sum of the return codes from the hook functions.  If
1118          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1119          * the save operation to abort.
1120          */
1121         return total_retval;
1122 }
1123
1124
1125 int PerformRoomHooks(struct ctdlroom *target_room)
1126 {
1127         RoomFunctionHook *fcn;
1128         int total_retval = 0;
1129
1130         syslog(LOG_DEBUG, "extensions: performing room hooks for <%s>", target_room->QRname);
1131
1132         for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1133                 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1134         }
1135
1136         /* Return the sum of the return codes from the hook functions.
1137          */
1138         return total_retval;
1139 }
1140
1141
1142 void PerformDeleteHooks(char *room, long msgnum)
1143 {
1144         DeleteFunctionHook *fcn;
1145
1146         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1147                 (*fcn->h_function_pointer) (room, msgnum);
1148         }
1149 }
1150
1151
1152 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1153 {
1154         XmsgFunctionHook *fcn;
1155         int total_sent = 0;
1156         int p;
1157
1158         for (p=0; p<MAX_XMSG_PRI; ++p) {
1159                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1160                         if (fcn->order == p) {
1161                                 total_sent +=
1162                                         (*fcn->h_function_pointer)
1163                                                 (sender, sender_email, recp, msg);
1164                         }
1165                 }
1166                 /* Break out of the loop if a higher-priority function
1167                  * successfully delivered the message.  This prevents duplicate
1168                  * deliveries to local users simultaneously signed onto
1169                  * remote services.
1170                  */
1171                 if (total_sent) break;
1172         }
1173         return total_sent;
1174 }
1175
1176
1177 /*
1178  * Dirty hack until we impliment a hook mechanism for this
1179  */
1180 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1181 {
1182 #ifdef HAVE_OPENSSL
1183         CtdlStartTLS (ok_response, nosup_response, error_response);
1184 #endif
1185 }
1186
1187
1188 CTDL_MODULE_INIT(modules)
1189 {
1190         if (!threading) {
1191         }
1192         return "modules";
1193 }