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