Removing things that depend on sysvinit. It's a systemd world now.
[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         char error[SIZ];
857
858         strcpy(error, "");
859         newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
860         message = (char*) malloc (SIZ + SIZ);
861         
862         newfcn->next = ServiceHookTable;
863         newfcn->tcp_port = tcp_port;
864         newfcn->sockpath = sockpath;
865         newfcn->h_greeting_function = h_greeting_function;
866         newfcn->h_command_function = h_command_function;
867         newfcn->h_async_function = h_async_function;
868         newfcn->ServiceName = ServiceName;
869
870         if (sockpath != NULL) {
871                 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"), error);
872                 snprintf(message, SIZ, "extensions: unix domain socket '%s': ", sockpath);
873         }
874         else if (tcp_port <= 0) {       /* port -1 to disable */
875                 syslog(LOG_INFO, "extensions: service %s has been manually disabled, skipping", ServiceName);
876                 free (message);
877                 free(newfcn);
878                 return;
879         }
880         else {
881                 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"),
882                                               tcp_port,
883                                               CtdlGetConfigInt("c_maxsessions"), 
884                                               error);
885                 snprintf(message, SIZ, "extensions: TCP port %s:%d: (%s) ", 
886                          CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
887         }
888
889         if (newfcn->msock > 0) {
890                 ServiceHookTable = newfcn;
891                 strcat(message, "registered.");
892                 syslog(LOG_INFO, "%s", message);
893         }
894         else {
895                 AddPortError(message, error);
896                 strcat(message, "FAILED.");
897                 syslog(LOG_ERR, "%s", message);
898                 free(newfcn);
899         }
900         free(message);
901 }
902
903
904 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
905                         void (*h_greeting_function) (void),
906                         void (*h_command_function) (void),
907                         void (*h_async_function) (void)
908                         )
909 {
910         ServiceFunctionHook *cur, *p, *last;
911
912         last = NULL;
913         cur = ServiceHookTable;
914         while (cur != NULL) {
915                 /* This will also remove duplicates if any */
916                 if (h_greeting_function == cur->h_greeting_function &&
917                     h_command_function == cur->h_command_function &&
918                     h_async_function == cur->h_async_function &&
919                     tcp_port == cur->tcp_port && 
920                     !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
921                 {
922                         if (cur->msock > 0)
923                                 close(cur->msock);
924                         if (sockpath) {
925                                 syslog(LOG_INFO, "extensions: closed UNIX domain socket %s", sockpath);
926                                 unlink(sockpath);
927                         } else if (tcp_port) {
928                                 syslog(LOG_INFO, "extensions: closed TCP port %d", tcp_port);
929                         } else {
930                                 syslog(LOG_INFO, "extensions: unregistered service \"%s\"", cur->ServiceName);
931                         }
932                         p = cur->next;
933                         free(cur);
934                         if (last != NULL)
935                                 last->next = p;
936                         else
937                                 ServiceHookTable = p;
938                         cur = p;
939                 }
940                 else {
941                         last = cur;
942                         cur = cur->next;
943                 }
944         }
945 }
946
947
948 void CtdlShutdownServiceHooks(void)
949 {
950         /* sort of a duplicate of close_masters() but called earlier */
951         ServiceFunctionHook *cur;
952
953         cur = ServiceHookTable;
954         while (cur != NULL) 
955         {
956                 if (cur->msock != -1)
957                 {
958                         close(cur->msock);
959                         cur->msock = -1;
960                         if (cur->sockpath != NULL){
961                                 syslog(LOG_INFO, "extensions: [%s] Closed UNIX domain socket %s", cur->ServiceName, cur->sockpath);
962                                 unlink(cur->sockpath);
963                         } else {
964                                 syslog(LOG_INFO, "extensions: [%s] closing service", cur->ServiceName);
965                         }
966                 }
967                 cur = cur->next;
968         }
969 }
970
971
972 void CtdlDestroyServiceHook(void)
973 {
974         const char *Text;
975         ServiceFunctionHook *cur, *p;
976
977         cur = ServiceHookTable;
978         while (cur != NULL)
979         {
980                 if (cur->msock != -1)
981                 {
982                         close(cur->msock);
983                         Text = "Closed";
984                 }
985                 else
986                 {
987                         Text = " Not closing again";
988                 }
989
990                 if (cur->sockpath) {
991                         syslog(LOG_INFO, "extensions: %s UNIX domain socket %s", Text, cur->sockpath);
992                         unlink(cur->sockpath);
993                 } else if (cur->tcp_port) {
994                         syslog(LOG_INFO, "extensions: %s TCP port %d", Text, cur->tcp_port);
995                 } else {
996                         syslog(LOG_INFO, "extensions: destroyed service \"%s\"", cur->ServiceName);
997                 }
998                 p = cur->next;
999                 free(cur);
1000                 cur = p;
1001         }
1002         ServiceHookTable = NULL;
1003 }
1004
1005 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1006 {
1007         SearchFunctionHook *newfcn;
1008
1009         if (!name || !fcn_ptr) {
1010                 return;
1011         }
1012         
1013         newfcn = (SearchFunctionHook *)
1014             malloc(sizeof(SearchFunctionHook));
1015         newfcn->next = SearchFunctionHookTable;
1016         newfcn->name = name;
1017         newfcn->fcn_ptr = fcn_ptr;
1018         SearchFunctionHookTable = newfcn;
1019
1020         syslog(LOG_DEBUG, "extensions: registered a new search function (%s)", name);
1021 }
1022
1023 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1024 {
1025         SearchFunctionHook *cur, *p, *last;
1026         
1027         last = NULL;
1028         cur = SearchFunctionHookTable;
1029         while (cur != NULL) {
1030                 if (fcn_ptr &&
1031                     (cur->fcn_ptr == fcn_ptr) &&
1032                     name && !strcmp(name, cur->name))
1033                 {
1034                         syslog(LOG_DEBUG, "extensions: unregistered search function(%s)", name);
1035                         p = cur->next;
1036                         free (cur);
1037                         if (last != NULL)
1038                                 last->next = p;
1039                         else
1040                                 SearchFunctionHookTable = p;
1041                         cur = p;
1042                 }
1043                 else {
1044                         last = cur;
1045                         cur = cur->next;
1046                 }
1047         }
1048 }
1049
1050 void CtdlDestroySearchHooks(void)
1051 {
1052         SearchFunctionHook *cur, *p;
1053
1054         cur = SearchFunctionHookTable;
1055         SearchFunctionHookTable = NULL;
1056         while (cur != NULL) {
1057                 p = cur->next;
1058                 free(cur);
1059                 cur = p;
1060         }
1061 }
1062
1063 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1064 {
1065         SearchFunctionHook *fcn = NULL;
1066
1067         for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1068                 if (!func_name || !strcmp(func_name, fcn->name)) {
1069                         (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1070                         return;
1071                 }
1072         }
1073         *num_msgs = 0;
1074 }
1075
1076
1077 void PerformSessionHooks(int EventType)
1078 {
1079         SessionFunctionHook *fcn = NULL;
1080
1081         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1082                 if (fcn->eventtype == EventType) {
1083                         if (EventType == EVT_TIMER) {
1084                                 pthread_setspecific(MyConKey, NULL);    /* for every hook */
1085                         }
1086                         (*fcn->h_function_pointer) ();
1087                 }
1088         }
1089 }
1090
1091 void PerformUserHooks(ctdluser *usbuf, int EventType)
1092 {
1093         UserFunctionHook *fcn = NULL;
1094
1095         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1096                 if (fcn->eventtype == EventType) {
1097                         (*fcn->h_function_pointer) (usbuf);
1098                 }
1099         }
1100 }
1101
1102 int PerformMessageHooks(struct CtdlMessage *msg, recptypes *recps, int EventType)
1103 {
1104         MessageFunctionHook *fcn = NULL;
1105         int total_retval = 0;
1106
1107         /* Other code may elect to protect this message from server-side
1108          * handlers; if this is the case, don't do anything.
1109          */
1110         if (msg->cm_flags & CM_SKIP_HOOKS) {
1111                 return(0);
1112         }
1113
1114         /* Otherwise, run all the hooks appropriate to this event type.
1115          */
1116         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1117                 if (fcn->eventtype == EventType) {
1118                         total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
1119                 }
1120         }
1121
1122         /* Return the sum of the return codes from the hook functions.  If
1123          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1124          * the save operation to abort.
1125          */
1126         return total_retval;
1127 }
1128
1129
1130 int PerformRoomHooks(struct ctdlroom *target_room)
1131 {
1132         RoomFunctionHook *fcn;
1133         int total_retval = 0;
1134
1135         syslog(LOG_DEBUG, "extensions: performing room hooks for <%s>", target_room->QRname);
1136
1137         for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1138                 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1139         }
1140
1141         /* Return the sum of the return codes from the hook functions.
1142          */
1143         return total_retval;
1144 }
1145
1146
1147 void PerformDeleteHooks(char *room, long msgnum)
1148 {
1149         DeleteFunctionHook *fcn;
1150
1151         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1152                 (*fcn->h_function_pointer) (room, msgnum);
1153         }
1154 }
1155
1156
1157 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1158 {
1159         XmsgFunctionHook *fcn;
1160         int total_sent = 0;
1161         int p;
1162
1163         for (p=0; p<MAX_XMSG_PRI; ++p) {
1164                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1165                         if (fcn->order == p) {
1166                                 total_sent +=
1167                                         (*fcn->h_function_pointer)
1168                                                 (sender, sender_email, recp, msg);
1169                         }
1170                 }
1171                 /* Break out of the loop if a higher-priority function
1172                  * successfully delivered the message.  This prevents duplicate
1173                  * deliveries to local users simultaneously signed onto
1174                  * remote services.
1175                  */
1176                 if (total_sent) break;
1177         }
1178         return total_sent;
1179 }
1180
1181
1182 /*
1183  * Dirty hack until we impliment a hook mechanism for this
1184  */
1185 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1186 {
1187 #ifdef HAVE_OPENSSL
1188         CtdlStartTLS (ok_response, nosup_response, error_response);
1189 #endif
1190 }
1191
1192
1193 CTDL_MODULE_INIT(modules)
1194 {
1195         if (!threading) {
1196         }
1197         return "modules";
1198 }