9984e23cf23c2308610be2c7205f5e6448b4b240
[citadel.git] / citadel / serv_extensions.c
1 /*
2  * Citadel Extension Loader
3  * Written by Brian Costello <btx@calyx.net>
4  *
5  * Copyright (c) 1987-2015 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 <stdio.h>
17 #include <libcitadel.h>
18
19 #include "sysdep_decls.h"
20 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
21
22 #include "serv_extensions.h"
23 #include "ctdl_module.h"
24 #include "config.h"
25
26
27 int DebugModules = 0;
28  
29 /*
30  * Structure defentitions for hook tables
31  */
32
33 HashList *LogDebugEntryTable = NULL;
34
35 typedef struct LogFunctionHook LogFunctionHook;
36 struct LogFunctionHook {
37         LogFunctionHook *next;
38         int loglevel;
39         void (*h_function_pointer) (char *);
40 };
41
42 LogFunctionHook *LogHookTable = NULL;
43
44 typedef struct FixedOutputHook FixedOutputHook;
45 struct FixedOutputHook {
46         FixedOutputHook *next;
47         char content_type[64];
48         void (*h_function_pointer) (char *, int);
49 };
50 FixedOutputHook *FixedOutputTable = NULL;
51
52
53
54 /*
55  * TDAPVetoHookFunctionHook extensions are used for any type of hook for which
56  * may prevent the autopurger to run for this specific data class.
57  * the function should at least LOG_INFO that it does so.
58  */
59 typedef struct TDAPVetoHookFunctionHook TDAPVetoHookFunctionHook;
60 struct TDAPVetoHookFunctionHook {
61         TDAPVetoHookFunctionHook *next;
62         int Priority;
63         int (*h_function_pointer) (StrBuf *);
64         int eventtype;
65 };
66 TDAPVetoHookFunctionHook *TDAPVetoHookTable = NULL;
67
68
69
70 /*
71  * SessionFunctionHook extensions are used for any type of hook for which
72  * the context in which it's being called (which is determined by the event
73  * type) will make it obvious for the hook function to know where to look for
74  * pertinent data.
75  */
76 typedef struct SessionFunctionHook SessionFunctionHook;
77 struct SessionFunctionHook {
78         SessionFunctionHook *next;
79         int Priority;
80         void (*h_function_pointer) (void);
81         int eventtype;
82 };
83 SessionFunctionHook *SessionHookTable = NULL;
84
85 /*
86  * UserFunctionHook extensions are used for any type of hook which implements
87  * an operation on a user or username (potentially) other than the one
88  * operating the current session.
89  */
90 typedef struct UserFunctionHook UserFunctionHook;
91 struct UserFunctionHook {
92         UserFunctionHook *next;
93         void (*h_function_pointer) (struct ctdluser *usbuf);
94         int eventtype;
95 };
96 UserFunctionHook *UserHookTable = NULL;
97
98 /*
99  * MessageFunctionHook extensions are used for hooks which implement handlers
100  * for various types of message operations (save, read, etc.)
101  */
102 typedef struct MessageFunctionHook MessageFunctionHook;
103 struct MessageFunctionHook {
104         MessageFunctionHook *next;
105         int (*h_function_pointer) (struct CtdlMessage *msg, recptypes *recps);
106         int eventtype;
107 };
108 MessageFunctionHook *MessageHookTable = NULL;
109
110
111 /*
112  * NetprocFunctionHook extensions are used for hooks which implement handlers
113  * for incoming network messages.
114  */
115 typedef struct NetprocFunctionHook NetprocFunctionHook;
116 struct NetprocFunctionHook {
117         NetprocFunctionHook *next;
118         int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
119 };
120 NetprocFunctionHook *NetprocHookTable = NULL;
121
122
123 /*
124  * DeleteFunctionHook extensions are used for hooks which get called when a
125  * message is about to be deleted.
126  */
127 typedef struct DeleteFunctionHook DeleteFunctionHook;
128 struct DeleteFunctionHook {
129         DeleteFunctionHook *next;
130         void (*h_function_pointer) (char *target_room, long msgnum);
131 };
132 DeleteFunctionHook *DeleteHookTable = NULL;
133
134
135 /*
136  * ExpressMessageFunctionHook extensions are used for hooks which implement
137  * the sending of an instant message through various channels.  Any function
138  * registered should return the number of recipients to whom the message was
139  * successfully transmitted.
140  */
141 typedef struct XmsgFunctionHook XmsgFunctionHook;
142 struct XmsgFunctionHook {
143         XmsgFunctionHook *next;
144         int (*h_function_pointer) (char *, char *, char *, char *);
145         int order;
146 };
147 XmsgFunctionHook *XmsgHookTable = NULL;
148
149
150
151
152 /*
153  * RoomFunctionHook extensions are used for hooks which impliment room
154  * processing functions when new messages are added EG. SIEVE.
155  */
156 typedef struct RoomFunctionHook RoomFunctionHook;
157 struct RoomFunctionHook {
158         RoomFunctionHook *next;
159         int (*fcn_ptr) (struct ctdlroom *);
160 };
161 RoomFunctionHook *RoomHookTable = NULL;
162
163
164
165 typedef struct SearchFunctionHook SearchFunctionHook;
166 struct SearchFunctionHook {
167         SearchFunctionHook *next;
168         void (*fcn_ptr) (int *, long **, const char *);
169         char *name;
170 };
171 SearchFunctionHook *SearchFunctionHookTable = NULL;
172
173 CleanupFunctionHook *CleanupHookTable = NULL;
174 CleanupFunctionHook *EVCleanupHookTable = NULL;
175
176 ServiceFunctionHook *ServiceHookTable = NULL;
177
178 typedef struct ProtoFunctionHook ProtoFunctionHook;
179 struct ProtoFunctionHook {
180         void (*handler) (char *cmdbuf);
181         const char *cmd;
182         const char *desc;
183 };
184
185 HashList *ProtoHookList = NULL;
186
187
188 #define ERR_PORT (1 << 1)
189
190
191 static StrBuf *portlist = NULL;
192
193 static StrBuf *errormessages = NULL;
194
195
196 long   DetailErrorFlags;
197 ConstStr Empty = {HKEY("")};
198 char *ErrSubject = "Startup Problems";
199 ConstStr ErrGeneral[] = {
200         {HKEY("Citadel had trouble on starting up. ")},
201         {HKEY(" This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
202               "If you don't want citadel to provide these services, turn them off in WebCit via: ")},
203         {HKEY("To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
204               "The errors returned by the system were:\n")},
205         {HKEY("You can recheck the above if you follow this faq item:\n"
206               "http://www.citadel.org/doku.php?id=faq:mastering_your_os:net#netstat")}
207 };
208
209 ConstStr ErrPortShort = { HKEY("We couldn't bind all ports you configured to be provided by citadel server.\n")};
210 ConstStr ErrPortWhere = { HKEY("\"Admin->System Preferences->Network\".\n\nThe failed ports and sockets are: ")};
211 ConstStr ErrPortHint  = { HKEY("If you want citadel to provide you with that functionality, "
212                                "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
213                                " and stop the program that binds these ports.\n You should eventually remove "
214                                " their initscripts in /etc/init.d so that you won't get this trouble once more.\n"
215                                " After that goto \"Administration -> Shutdown Citadel\" to make Citadel restart & retry to bind this port.\n")};
216
217
218 void LogPrintMessages(long err)
219 {
220         StrBuf *Message;
221         StrBuf *List, *DetailList;
222         ConstStr *Short, *Where, *Hint; 
223
224         
225         Message = NewStrBufPlain(NULL, 
226                                  StrLength(portlist) + StrLength(errormessages));
227         
228         DetailErrorFlags = DetailErrorFlags & ~err;
229
230         switch (err)
231         {
232         case ERR_PORT:
233                 Short = &ErrPortShort;
234                 Where = &ErrPortWhere;
235                 Hint  = &ErrPortHint;
236                 List  = portlist;
237                 DetailList = errormessages;
238                 break;
239         default:
240                 Short = &Empty;
241                 Where = &Empty;
242                 Hint  = &Empty;
243                 List  = NULL;
244                 DetailList = NULL;
245         }
246
247         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[0]), 0);
248         StrBufAppendBufPlain(Message, CKEY(*Short), 0); 
249         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[1]), 0);
250         StrBufAppendBufPlain(Message, CKEY(*Where), 0);
251         StrBufAppendBuf(Message, List, 0);
252         StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
253         StrBufAppendBufPlain(Message, CKEY(*Hint), 0);
254         StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
255         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[2]), 0);
256         StrBufAppendBuf(Message, DetailList, 0);
257         StrBufAppendBufPlain(Message, HKEY("\n\n"), 0);
258         StrBufAppendBufPlain(Message, CKEY(ErrGeneral[3]), 0);
259
260         MOD_syslog(LOG_EMERG, "%s", ChrPtr(Message));
261         MOD_syslog(LOG_EMERG, "%s", ErrSubject);
262         quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(Message), FMT_FIXED, ErrSubject);
263
264         FreeStrBuf(&Message);
265         FreeStrBuf(&List);
266         FreeStrBuf(&DetailList);
267 }
268
269
270 void AddPortError(char *Port, char *ErrorMessage)
271 {
272         long len;
273
274         DetailErrorFlags |= ERR_PORT;
275
276         len = StrLength(errormessages);
277         if (len > 0) StrBufAppendBufPlain(errormessages, HKEY("; "), 0);
278         else errormessages = NewStrBuf();
279         StrBufAppendBufPlain(errormessages, ErrorMessage, -1, 0);
280
281
282         len = StrLength(portlist);
283         if (len > 0) StrBufAppendBufPlain(portlist, HKEY(";"), 0);
284         else portlist = NewStrBuf();
285         StrBufAppendBufPlain(portlist, Port, -1, 0);
286 }
287
288
289 int DLoader_Exec_Cmd(char *cmdbuf)
290 {
291         void *vP;
292         ProtoFunctionHook *p;
293
294         if (GetHash(ProtoHookList, cmdbuf, 4, &vP) && (vP != NULL)) {
295                 p = (ProtoFunctionHook*) vP;
296                 p->handler(&cmdbuf[5]);
297                 return 1;
298         }
299         return 0;
300 }
301
302 void CtdlRegisterDebugFlagHook(const char *Name, long Len, CtdlDbgFunction F, const int *LogP)
303 {
304         LogDebugEntry *E;
305         if (LogDebugEntryTable == NULL)
306                 LogDebugEntryTable = NewHash(1, NULL);
307         E = (LogDebugEntry*) malloc(sizeof(LogDebugEntry));
308         E->F = F;
309         E->Name = Name;
310         E->Len = Len;
311         E->LogP = LogP;
312         Put(LogDebugEntryTable, Name, Len, E, NULL);
313         
314 }
315 void CtdlSetDebugLogFacilities(const char **Str, long n)
316 {
317         StrBuf *Token = NULL;
318         StrBuf *Buf = NULL;
319         const char *ch;
320         int i;
321         int DoAll = 0;
322         void *vptr;
323
324         for (i=0; i < n; i++){
325                 if ((Str[i] != NULL) && !IsEmptyStr(Str[i])) {
326                         if (strcmp(Str[i], "all") == 0) {
327                                 DoAll = 1;
328                                 continue;
329                         }
330                         Buf = NewStrBufPlain(Str[i], -1);
331                         ch = NULL;
332                         if (Token == NULL)
333                                 Token = NewStrBufPlain(NULL, StrLength(Buf));
334                         while ((ch != StrBufNOTNULL) &&
335                                StrBufExtract_NextToken(Token, Buf, &ch, ',')) {
336                                 if (GetHash(LogDebugEntryTable, SKEY(Token), &vptr) && 
337                                     (vptr != NULL))
338                                 {
339                                         LogDebugEntry *E = (LogDebugEntry*)vptr;
340                                         E->F(1);
341                                 }
342                         }
343                 }
344                 FreeStrBuf(&Buf);
345         }
346         FreeStrBuf(&Token);
347         if (DoAll) {
348                 long HKLen;
349                 const char *ch;
350                 HashPos *Pos;
351
352                 Pos = GetNewHashPos(LogDebugEntryTable, 0);
353                 while (GetNextHashPos(LogDebugEntryTable, Pos, &HKLen, &ch, &vptr)) {
354                         LogDebugEntry *E = (LogDebugEntry*)vptr;
355                         E->F(1);
356                 }
357
358                 DeleteHashPos(&Pos);
359         }
360 }
361 void CtdlDestroyDebugTable(void)
362 {
363
364         DeleteHash(&LogDebugEntryTable);
365 }
366
367 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
368 {
369         ProtoFunctionHook *p;
370
371         if (ProtoHookList == NULL)
372                 ProtoHookList = NewHash (1, FourHash);
373
374
375         p = (ProtoFunctionHook *)
376                 malloc(sizeof(ProtoFunctionHook));
377
378         if (p == NULL) {
379                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
380                 exit(EXIT_FAILURE);
381         }
382         p->handler = handler;
383         p->cmd = cmd;
384         p->desc = desc;
385
386         Put(ProtoHookList, cmd, 4, p, NULL);
387         MOD_syslog(LOG_DEBUG, "Registered server command %s (%s)\n", cmd, desc);
388 }
389
390 void CtdlDestroyProtoHooks(void)
391 {
392
393         DeleteHash(&ProtoHookList);
394 }
395
396
397 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
398 {
399
400         CleanupFunctionHook *newfcn;
401
402         newfcn = (CleanupFunctionHook *)
403             malloc(sizeof(CleanupFunctionHook));
404         newfcn->next = CleanupHookTable;
405         newfcn->h_function_pointer = fcn_ptr;
406         CleanupHookTable = newfcn;
407
408         MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
409 }
410
411
412 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
413 {
414         CleanupFunctionHook *cur, *p, *last;
415         last = NULL;
416         cur = CleanupHookTable;
417         while (cur != NULL)
418         {
419                 if (fcn_ptr == cur->h_function_pointer)
420                 {
421                         MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
422                         p = cur->next;
423
424                         free(cur);
425                         cur = NULL;
426
427                         if (last != NULL)
428                                 last->next = p;
429                         else 
430                                 CleanupHookTable = p;
431                         cur = p;
432                 }
433                 else {
434                         last = cur;
435                         cur = cur->next;
436                 }
437         }
438 }
439
440
441 void CtdlDestroyCleanupHooks(void)
442 {
443         CleanupFunctionHook *cur, *p;
444
445         cur = CleanupHookTable;
446         while (cur != NULL)
447         {
448                 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
449                 p = cur->next;
450                 free(cur);
451                 cur = p;
452         }
453         CleanupHookTable = NULL;
454 }
455
456 void CtdlRegisterEVCleanupHook(void (*fcn_ptr) (void))
457 {
458
459         CleanupFunctionHook *newfcn;
460
461         newfcn = (CleanupFunctionHook *)
462             malloc(sizeof(CleanupFunctionHook));
463         newfcn->next = EVCleanupHookTable;
464         newfcn->h_function_pointer = fcn_ptr;
465         EVCleanupHookTable = newfcn;
466
467         MODM_syslog(LOG_DEBUG, "Registered a new cleanup function\n");
468 }
469
470
471 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr) (void))
472 {
473         CleanupFunctionHook *cur, *p, *last;
474         last = NULL;
475         cur = EVCleanupHookTable;
476         while (cur != NULL)
477         {
478                 if (fcn_ptr == cur->h_function_pointer)
479                 {
480                         MODM_syslog(LOG_DEBUG, "Unregistered cleanup function\n");
481                         p = cur->next;
482
483                         free(cur);
484                         cur = NULL;
485
486                         if (last != NULL)
487                                 last->next = p;
488                         else 
489                                 EVCleanupHookTable = p;
490                         cur = p;
491                 }
492                 else {
493                         last = cur;
494                         cur = cur->next;
495                 }
496         }
497 }
498
499
500 void CtdlDestroyEVCleanupHooks(void)
501 {
502         CleanupFunctionHook *cur, *p;
503
504         cur = EVCleanupHookTable;
505         while (cur != NULL)
506         {
507                 MODM_syslog(LOG_DEBUG, "Destroyed cleanup function\n");
508                 p = cur->next;
509                 cur->h_function_pointer();
510                 free(cur);
511                 cur = p;
512         }
513         EVCleanupHookTable = NULL;
514 }
515
516 void CtdlRegisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType, int Priority)
517 {
518         TDAPVetoHookFunctionHook *newfcn;
519
520         newfcn = (TDAPVetoHookFunctionHook *)
521             malloc(sizeof(TDAPVetoHookFunctionHook));
522         newfcn->Priority = Priority;
523         newfcn->h_function_pointer = fcn_ptr;
524         newfcn->eventtype = EventType;
525
526         TDAPVetoHookFunctionHook **pfcn;
527         pfcn = &TDAPVetoHookTable;
528         while ((*pfcn != NULL) && 
529                ((*pfcn)->Priority < newfcn->Priority) &&
530                ((*pfcn)->next != NULL))
531                 pfcn = &(*pfcn)->next;
532                 
533         newfcn->next = *pfcn;
534         *pfcn = newfcn;
535         
536         MOD_syslog(LOG_DEBUG, "Registered a new TDAP Veto function (type %d Priority %d)\n",
537                    EventType, Priority);
538 }
539
540
541 void CtdlUnregisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType)
542 {
543         TDAPVetoHookFunctionHook *cur, *p, *last;
544         last = NULL;
545         cur = TDAPVetoHookTable;
546         while  (cur != NULL) {
547                 if ((fcn_ptr == cur->h_function_pointer) &&
548                     (EventType == cur->eventtype))
549                 {
550                         MOD_syslog(LOG_DEBUG, "Unregistered TDAP Veto function (type %d)\n",
551                                    EventType);
552                         p = cur->next;
553
554                         free(cur);
555                         cur = NULL;
556
557                         if (last != NULL)
558                                 last->next = p;
559                         else 
560                                 TDAPVetoHookTable = p;
561                         cur = p;
562                 }
563                 else {
564                         last = cur;
565                         cur = cur->next;
566                 }
567         }
568 }
569
570 void CtdlDestroyTDAPVetoHooks(void)
571 {
572         TDAPVetoHookFunctionHook *cur, *p;
573
574         cur = TDAPVetoHookTable;
575         while (cur != NULL)
576         {
577                 MODM_syslog(LOG_DEBUG, "Destroyed TDAP Veto function\n");
578                 p = cur->next;
579                 free(cur);
580                 cur = p;
581         }
582         TDAPVetoHookTable = NULL;
583 }
584
585
586 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType, int Priority)
587 {
588         SessionFunctionHook *newfcn;
589
590         newfcn = (SessionFunctionHook *)
591             malloc(sizeof(SessionFunctionHook));
592         newfcn->Priority = Priority;
593         newfcn->h_function_pointer = fcn_ptr;
594         newfcn->eventtype = EventType;
595
596         SessionFunctionHook **pfcn;
597         pfcn = &SessionHookTable;
598         while ((*pfcn != NULL) && 
599                ((*pfcn)->Priority < newfcn->Priority) &&
600                ((*pfcn)->next != NULL))
601                 pfcn = &(*pfcn)->next;
602                 
603         newfcn->next = *pfcn;
604         *pfcn = newfcn;
605         
606         MOD_syslog(LOG_DEBUG, "Registered a new session function (type %d Priority %d)\n",
607                    EventType, Priority);
608 }
609
610
611 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
612 {
613         SessionFunctionHook *cur, *p, *last;
614         last = NULL;
615         cur = SessionHookTable;
616         while  (cur != NULL) {
617                 if ((fcn_ptr == cur->h_function_pointer) &&
618                     (EventType == cur->eventtype))
619                 {
620                         MOD_syslog(LOG_DEBUG, "Unregistered session function (type %d)\n",
621                                    EventType);
622                         p = cur->next;
623
624                         free(cur);
625                         cur = NULL;
626
627                         if (last != NULL)
628                                 last->next = p;
629                         else 
630                                 SessionHookTable = p;
631                         cur = p;
632                 }
633                 else {
634                         last = cur;
635                         cur = cur->next;
636                 }
637         }
638 }
639
640 void CtdlDestroySessionHooks(void)
641 {
642         SessionFunctionHook *cur, *p;
643
644         cur = SessionHookTable;
645         while (cur != NULL)
646         {
647                 MODM_syslog(LOG_DEBUG, "Destroyed session function\n");
648                 p = cur->next;
649                 free(cur);
650                 cur = p;
651         }
652         SessionHookTable = NULL;
653 }
654
655
656 void CtdlRegisterUserHook(void (*fcn_ptr) (ctdluser *), int EventType)
657 {
658
659         UserFunctionHook *newfcn;
660
661         newfcn = (UserFunctionHook *)
662             malloc(sizeof(UserFunctionHook));
663         newfcn->next = UserHookTable;
664         newfcn->h_function_pointer = fcn_ptr;
665         newfcn->eventtype = EventType;
666         UserHookTable = newfcn;
667
668         MOD_syslog(LOG_DEBUG, "Registered a new user function (type %d)\n",
669                    EventType);
670 }
671
672
673 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
674 {
675         UserFunctionHook *cur, *p, *last;
676         last = NULL;
677         cur = UserHookTable;
678         while (cur != NULL) {
679                 if ((fcn_ptr == cur->h_function_pointer) &&
680                     (EventType == cur->eventtype))
681                 {
682                         MOD_syslog(LOG_DEBUG, "Unregistered user function (type %d)\n",
683                                    EventType);
684                         p = cur->next;
685
686                         free(cur);
687                         cur = NULL;
688
689                         if (last != NULL)
690                                 last->next = p;
691                         else 
692                                 UserHookTable = p;
693                         cur = p;
694                 }
695                 else {
696                         last = cur;
697                         cur = cur->next;
698                 }
699         }
700 }
701
702 void CtdlDestroyUserHooks(void)
703 {
704         UserFunctionHook *cur, *p;
705
706         cur = UserHookTable;
707         while (cur != NULL)
708         {
709                 MODM_syslog(LOG_DEBUG, "Destroyed user function \n");
710                 p = cur->next;
711                 free(cur);
712                 cur = p;
713         }
714         UserHookTable = NULL;
715 }
716
717
718 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
719                                 int EventType)
720 {
721
722         MessageFunctionHook *newfcn;
723
724         newfcn = (MessageFunctionHook *)
725             malloc(sizeof(MessageFunctionHook));
726         newfcn->next = MessageHookTable;
727         newfcn->h_function_pointer = handler;
728         newfcn->eventtype = EventType;
729         MessageHookTable = newfcn;
730
731         MOD_syslog(LOG_DEBUG, "Registered a new message function (type %d)\n",
732                    EventType);
733 }
734
735
736 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
737                 int EventType)
738 {
739         MessageFunctionHook *cur, *p, *last;
740         last = NULL;
741         cur = MessageHookTable;
742         while (cur != NULL) {
743                 if ((handler == cur->h_function_pointer) &&
744                     (EventType == cur->eventtype))
745                 {
746                         MOD_syslog(LOG_DEBUG, "Unregistered message function (type %d)\n",
747                                    EventType);
748                         p = cur->next;
749                         free(cur);
750                         cur = NULL;
751
752                         if (last != NULL)
753                                 last->next = p;
754                         else 
755                                 MessageHookTable = p;
756                         cur = p;
757                 }
758                 else {
759                         last = cur;
760                         cur = cur->next;
761                 }
762         }
763 }
764
765 void CtdlDestroyMessageHook(void)
766 {
767         MessageFunctionHook *cur, *p;
768
769         cur = MessageHookTable; 
770         while (cur != NULL)
771         {
772                 MOD_syslog(LOG_DEBUG, "Destroyed message function (type %d)\n", cur->eventtype);
773                 p = cur->next;
774                 free(cur);
775                 cur = p;
776         }
777         MessageHookTable = NULL;
778 }
779
780
781 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
782 {
783         RoomFunctionHook *newfcn;
784
785         newfcn = (RoomFunctionHook *)
786             malloc(sizeof(RoomFunctionHook));
787         newfcn->next = RoomHookTable;
788         newfcn->fcn_ptr = fcn_ptr;
789         RoomHookTable = newfcn;
790
791         MODM_syslog(LOG_DEBUG, "Registered a new room function\n");
792 }
793
794
795 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
796 {
797         RoomFunctionHook *cur, *p, *last;
798         last = NULL;
799         cur = RoomHookTable;
800         while (cur != NULL)
801         {
802                 if (fcn_ptr == cur->fcn_ptr) {
803                         MODM_syslog(LOG_DEBUG, "Unregistered room function\n");
804                         p = cur->next;
805
806                         free(cur);
807                         cur = NULL;
808
809                         if (last != NULL)
810                                 last->next = p;
811                         else 
812                                 RoomHookTable = p;
813                         cur = p;
814                 }
815                 else {
816                         last = cur;
817                         cur = cur->next;
818                 }
819         }
820 }
821
822
823 void CtdlDestroyRoomHooks(void)
824 {
825         RoomFunctionHook *cur, *p;
826
827         cur = RoomHookTable;
828         while (cur != NULL)
829         {
830                 MODM_syslog(LOG_DEBUG, "Destroyed room function\n");
831                 p = cur->next;
832                 free(cur);
833                 cur = p;
834         }
835         RoomHookTable = NULL;
836 }
837
838 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
839 {
840         NetprocFunctionHook *newfcn;
841
842         newfcn = (NetprocFunctionHook *)
843             malloc(sizeof(NetprocFunctionHook));
844         newfcn->next = NetprocHookTable;
845         newfcn->h_function_pointer = handler;
846         NetprocHookTable = newfcn;
847
848         MODM_syslog(LOG_DEBUG, "Registered a new netproc function\n");
849 }
850
851
852 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
853 {
854         NetprocFunctionHook *cur, *p, *last;
855
856         cur = NetprocHookTable;
857         last = NULL;
858
859         while (cur != NULL) {
860                 if (handler == cur->h_function_pointer)
861                 {
862                         MODM_syslog(LOG_DEBUG, "Unregistered netproc function\n");
863                         p = cur->next;
864                         free(cur);
865                         if (last != NULL) {
866                                 last->next = p;
867                         }
868                         else {
869                                 NetprocHookTable = p;
870                         }
871                         cur = p;
872                 }
873                 else {
874                         last = cur;
875                         cur = cur->next;
876                 }
877         }
878 }
879
880 void CtdlDestroyNetprocHooks(void)
881 {
882         NetprocFunctionHook *cur, *p;
883
884         cur = NetprocHookTable;
885         while (cur != NULL)
886         {
887                 MODM_syslog(LOG_DEBUG, "Destroyed netproc function\n");
888                 p = cur->next;
889                 free(cur);
890                 cur = p;
891         }
892         NetprocHookTable = NULL;
893 }
894
895
896 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
897 {
898         DeleteFunctionHook *newfcn;
899
900         newfcn = (DeleteFunctionHook *)
901             malloc(sizeof(DeleteFunctionHook));
902         newfcn->next = DeleteHookTable;
903         newfcn->h_function_pointer = handler;
904         DeleteHookTable = newfcn;
905
906         MODM_syslog(LOG_DEBUG, "Registered a new delete function\n");
907 }
908
909
910 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
911 {
912         DeleteFunctionHook *cur, *p, *last;
913
914         last = NULL;
915         cur = DeleteHookTable;
916         while (cur != NULL) {
917                 if (handler == cur->h_function_pointer )
918                 {
919                         MODM_syslog(LOG_DEBUG, "Unregistered delete function\n");
920                         p = cur->next;
921                         free(cur);
922
923                         if (last != NULL)
924                                 last->next = p;
925                         else
926                                 DeleteHookTable = p;
927
928                         cur = p;
929                 }
930                 else {
931                         last = cur;
932                         cur = cur->next;
933                 }
934         }
935 }
936 void CtdlDestroyDeleteHooks(void)
937 {
938         DeleteFunctionHook *cur, *p;
939
940         cur = DeleteHookTable;
941         while (cur != NULL)
942         {
943                 MODM_syslog(LOG_DEBUG, "Destroyed delete function\n");
944                 p = cur->next;
945                 free(cur);
946                 cur = p;                
947         }
948         DeleteHookTable = NULL;
949 }
950
951
952
953
954 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
955 {
956         FixedOutputHook *newfcn;
957
958         newfcn = (FixedOutputHook *)
959             malloc(sizeof(FixedOutputHook));
960         newfcn->next = FixedOutputTable;
961         newfcn->h_function_pointer = handler;
962         safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
963         FixedOutputTable = newfcn;
964
965         MOD_syslog(LOG_DEBUG, "Registered a new fixed output function for %s\n", newfcn->content_type);
966 }
967
968
969 void CtdlUnregisterFixedOutputHook(char *content_type)
970 {
971         FixedOutputHook *cur, *p, *last;
972
973         last = NULL;
974         cur = FixedOutputTable;
975         while (cur != NULL) {
976                 /* This will also remove duplicates if any */
977                 if (!strcasecmp(content_type, cur->content_type)) {
978                         MOD_syslog(LOG_DEBUG,
979                                    "Unregistered fixed output function for %s\n",
980                                    content_type);
981
982                         p = cur->next;
983                         free(cur);
984
985                         if (last != NULL)
986                                 last->next = p;
987                         else
988                                 FixedOutputTable = p;
989                         
990                         cur = p;
991                 }
992                 else
993                 {
994                         last = cur;
995                         cur = cur->next;
996                 }
997         }
998 }
999
1000 void CtdlDestroyFixedOutputHooks(void)
1001 {
1002         FixedOutputHook *cur, *p;
1003
1004         cur = FixedOutputTable; 
1005         while (cur != NULL)
1006         {
1007                 MOD_syslog(LOG_DEBUG, "Destroyed fixed output function for %s\n", cur->content_type);
1008                 p = cur->next;
1009                 free(cur);
1010                 cur = p;
1011                 
1012         }
1013         FixedOutputTable = NULL;
1014 }
1015
1016 /* returns nonzero if we found a hook and used it */
1017 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
1018 {
1019         FixedOutputHook *fcn;
1020
1021         for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
1022                 if (!strcasecmp(content_type, fcn->content_type)) {
1023                         (*fcn->h_function_pointer) (content, content_length);
1024                         return(1);
1025                 }
1026         }
1027         return(0);
1028 }
1029
1030
1031
1032
1033
1034 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1035 {
1036
1037         XmsgFunctionHook *newfcn;
1038
1039         newfcn = (XmsgFunctionHook *) malloc(sizeof(XmsgFunctionHook));
1040         newfcn->next = XmsgHookTable;
1041         newfcn->order = order;
1042         newfcn->h_function_pointer = fcn_ptr;
1043         XmsgHookTable = newfcn;
1044         MOD_syslog(LOG_DEBUG, "Registered a new x-msg function (priority %d)\n", order);
1045 }
1046
1047
1048 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *, char *), int order)
1049 {
1050         XmsgFunctionHook *cur, *p, *last;
1051
1052         last = NULL;
1053         cur = XmsgHookTable;
1054         while (cur != NULL) {
1055                 /* This will also remove duplicates if any */
1056                 if (fcn_ptr == cur->h_function_pointer &&
1057                     order == cur->order) {
1058                         MOD_syslog(LOG_DEBUG, "Unregistered x-msg function "
1059                                    "(priority %d)\n", order);
1060                         p = cur->next;
1061                         free(cur);
1062
1063                         if (last != NULL)
1064                                 last->next = p;
1065                         else
1066                                 XmsgHookTable = p;
1067                         
1068                         cur = p;
1069                 }
1070                 else {
1071                         last = cur;
1072                         cur = cur->next;
1073                 }
1074         }
1075 }
1076
1077 void CtdlDestroyXmsgHooks(void)
1078 {
1079         XmsgFunctionHook *cur, *p;
1080
1081         cur = XmsgHookTable;
1082         while (cur != NULL)
1083         {
1084                 MOD_syslog(LOG_DEBUG, "Destroyed x-msg function "
1085                         "(priority %d)\n", cur->order);
1086                 p = cur->next;
1087                         
1088                 free(cur);
1089                 cur = p;
1090         }
1091         XmsgHookTable = NULL;
1092 }
1093
1094
1095 void CtdlRegisterServiceHook(int tcp_port,
1096                              char *sockpath,
1097                              void (*h_greeting_function) (void),
1098                              void (*h_command_function) (void),
1099                              void (*h_async_function) (void),
1100                              const char *ServiceName)
1101 {
1102         ServiceFunctionHook *newfcn;
1103         char *message;
1104         char error[SIZ];
1105
1106         strcpy(error, "");
1107         newfcn = (ServiceFunctionHook *) malloc(sizeof(ServiceFunctionHook));
1108         message = (char*) malloc (SIZ + SIZ);
1109         
1110         newfcn->next = ServiceHookTable;
1111         newfcn->tcp_port = tcp_port;
1112         newfcn->sockpath = sockpath;
1113         newfcn->h_greeting_function = h_greeting_function;
1114         newfcn->h_command_function = h_command_function;
1115         newfcn->h_async_function = h_async_function;
1116         newfcn->ServiceName = ServiceName;
1117
1118         if (sockpath != NULL) {
1119                 newfcn->msock = ctdl_uds_server(sockpath, CtdlGetConfigInt("c_maxsessions"), error);
1120                 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
1121         }
1122         else if (tcp_port <= 0) {       /* port -1 to disable */
1123                 MOD_syslog(LOG_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
1124                 free (message);
1125                 free(newfcn);
1126                 return;
1127         }
1128         else {
1129                 newfcn->msock = ctdl_tcp_server(CtdlGetConfigStr("c_ip_addr"),
1130                                               tcp_port,
1131                                               CtdlGetConfigInt("c_maxsessions"), 
1132                                               error);
1133                 snprintf(message, SIZ, "TCP port %s:%d: (%s) ", 
1134                          CtdlGetConfigStr("c_ip_addr"), tcp_port, ServiceName);
1135         }
1136
1137         if (newfcn->msock > 0) {
1138                 ServiceHookTable = newfcn;
1139                 strcat(message, "registered.");
1140                 MOD_syslog(LOG_INFO, "%s\n", message);
1141         }
1142         else {
1143                 AddPortError(message, error);
1144                 strcat(message, "FAILED.");
1145                 MOD_syslog(LOG_CRIT, "%s\n", message);
1146                 free(newfcn);
1147         }
1148         free(message);
1149 }
1150
1151
1152 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
1153                         void (*h_greeting_function) (void),
1154                         void (*h_command_function) (void),
1155                         void (*h_async_function) (void)
1156                         )
1157 {
1158         ServiceFunctionHook *cur, *p, *last;
1159
1160         last = NULL;
1161         cur = ServiceHookTable;
1162         while (cur != NULL) {
1163                 /* This will also remove duplicates if any */
1164                 if (h_greeting_function == cur->h_greeting_function &&
1165                     h_command_function == cur->h_command_function &&
1166                     h_async_function == cur->h_async_function &&
1167                     tcp_port == cur->tcp_port && 
1168                     !(sockpath && cur->sockpath && strcmp(sockpath, cur->sockpath)) )
1169                 {
1170                         if (cur->msock > 0)
1171                                 close(cur->msock);
1172                         if (sockpath) {
1173                                 MOD_syslog(LOG_INFO, "Closed UNIX domain socket %s\n",
1174                                            sockpath);
1175                                 unlink(sockpath);
1176                         } else if (tcp_port) {
1177                                 MOD_syslog(LOG_INFO, "Closed TCP port %d\n", tcp_port);
1178                         } else {
1179                                 MOD_syslog(LOG_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
1180                         }
1181                         p = cur->next;
1182                         free(cur);
1183                         if (last != NULL)
1184                                 last->next = p;
1185                         else
1186                                 ServiceHookTable = p;
1187                         cur = p;
1188                 }
1189                 else {
1190                         last = cur;
1191                         cur = cur->next;
1192                 }
1193         }
1194 }
1195
1196
1197 void CtdlShutdownServiceHooks(void)
1198 {
1199         /* sort of a duplicate of close_masters() but called earlier */
1200         ServiceFunctionHook *cur;
1201
1202         cur = ServiceHookTable;
1203         while (cur != NULL) 
1204         {
1205                 if (cur->msock != -1)
1206                 {
1207                         close(cur->msock);
1208                         cur->msock = -1;
1209                         if (cur->sockpath != NULL){
1210                                 MOD_syslog(LOG_INFO, "[%s] Closed UNIX domain socket %s\n",
1211                                            cur->ServiceName,
1212                                            cur->sockpath);
1213                                 unlink(cur->sockpath);
1214                         } else {
1215                                 MOD_syslog(LOG_INFO, "[%s] closing service\n", 
1216                                            cur->ServiceName);
1217                         }
1218                 }
1219                 cur = cur->next;
1220         }
1221 }
1222
1223 void CtdlDestroyServiceHook(void)
1224 {
1225         const char *Text;
1226         ServiceFunctionHook *cur, *p;
1227
1228         cur = ServiceHookTable;
1229         while (cur != NULL)
1230         {
1231                 if (cur->msock != -1)
1232                 {
1233                         close(cur->msock);
1234                         Text = "Closed";
1235                 }
1236                 else
1237                 {
1238                         Text = " Not closing again";
1239                 }
1240
1241                 if (cur->sockpath) {
1242                         MOD_syslog(LOG_INFO, "%s UNIX domain socket %s\n",
1243                                    Text,
1244                                    cur->sockpath);
1245                         unlink(cur->sockpath);
1246                 } else if (cur->tcp_port) {
1247                         MOD_syslog(LOG_INFO, "%s TCP port %d\n", Text, cur->tcp_port);
1248                 } else {
1249                         MOD_syslog(LOG_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
1250                 }
1251                 p = cur->next;
1252                 free(cur);
1253                 cur = p;
1254         }
1255         ServiceHookTable = NULL;
1256 }
1257
1258 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1259 {
1260         SearchFunctionHook *newfcn;
1261
1262         if (!name || !fcn_ptr) {
1263                 return;
1264         }
1265         
1266         newfcn = (SearchFunctionHook *)
1267             malloc(sizeof(SearchFunctionHook));
1268         newfcn->next = SearchFunctionHookTable;
1269         newfcn->name = name;
1270         newfcn->fcn_ptr = fcn_ptr;
1271         SearchFunctionHookTable = newfcn;
1272
1273         MOD_syslog(LOG_DEBUG, "Registered a new search function (%s)\n", name);
1274 }
1275
1276 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
1277 {
1278         SearchFunctionHook *cur, *p, *last;
1279         
1280         last = NULL;
1281         cur = SearchFunctionHookTable;
1282         while (cur != NULL) {
1283                 if (fcn_ptr &&
1284                     (cur->fcn_ptr == fcn_ptr) &&
1285                     name && !strcmp(name, cur->name))
1286                 {
1287                         MOD_syslog(LOG_DEBUG, "Unregistered search function(%s)\n", name);
1288                         p = cur->next;
1289                         free (cur);
1290                         if (last != NULL)
1291                                 last->next = p;
1292                         else
1293                                 SearchFunctionHookTable = p;
1294                         cur = p;
1295                 }
1296                 else {
1297                         last = cur;
1298                         cur = cur->next;
1299                 }
1300         }
1301 }
1302
1303 void CtdlDestroySearchHooks(void)
1304 {
1305         SearchFunctionHook *cur, *p;
1306
1307         cur = SearchFunctionHookTable;
1308         SearchFunctionHookTable = NULL;
1309         while (cur != NULL) {
1310                 p = cur->next;
1311                 free(cur);
1312                 cur = p;
1313         }
1314 }
1315
1316 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
1317 {
1318         SearchFunctionHook *fcn = NULL;
1319
1320         for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
1321                 if (!func_name || !strcmp(func_name, fcn->name)) {
1322                         (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
1323                         return;
1324                 }
1325         }
1326         *num_msgs = 0;
1327 }
1328
1329 int CheckTDAPVeto (int DBType, StrBuf *ErrMsg)
1330 {
1331         int Result = 0;
1332         TDAPVetoHookFunctionHook *fcn = NULL;
1333
1334         for (fcn = TDAPVetoHookTable; (fcn != NULL) && (Result == 0); fcn = fcn->next) {
1335                 if (fcn->eventtype == DBType) {
1336                         Result = (*fcn->h_function_pointer) (ErrMsg);
1337                 }
1338         }
1339         return Result;
1340 }
1341
1342 void PerformSessionHooks(int EventType)
1343 {
1344         SessionFunctionHook *fcn = NULL;
1345
1346         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
1347                 if (fcn->eventtype == EventType) {
1348                         if (EventType == EVT_TIMER) {
1349                                 pthread_setspecific(MyConKey, NULL);    /* for every hook */
1350                         }
1351                         (*fcn->h_function_pointer) ();
1352                 }
1353         }
1354 }
1355
1356 void PerformUserHooks(ctdluser *usbuf, int EventType)
1357 {
1358         UserFunctionHook *fcn = NULL;
1359
1360         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
1361                 if (fcn->eventtype == EventType) {
1362                         (*fcn->h_function_pointer) (usbuf);
1363                 }
1364         }
1365 }
1366
1367 int PerformMessageHooks(struct CtdlMessage *msg, recptypes *recps, int EventType)
1368 {
1369         MessageFunctionHook *fcn = NULL;
1370         int total_retval = 0;
1371
1372         /* Other code may elect to protect this message from server-side
1373          * handlers; if this is the case, don't do anything.
1374         MOD_syslog(LOG_DEBUG, "** Event type is %d, flags are %d\n", EventType, msg->cm_flags);
1375          */
1376         if (msg->cm_flags & CM_SKIP_HOOKS) {
1377                 MODM_syslog(LOG_DEBUG, "Skipping hooks\n");
1378                 return(0);
1379         }
1380
1381         /* Otherwise, run all the hooks appropriate to this event type.
1382          */
1383         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
1384                 if (fcn->eventtype == EventType) {
1385                         total_retval = total_retval + (*fcn->h_function_pointer) (msg, recps);
1386                 }
1387         }
1388
1389         /* Return the sum of the return codes from the hook functions.  If
1390          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
1391          * the save operation to abort.
1392          */
1393         return total_retval;
1394 }
1395
1396
1397 int PerformRoomHooks(struct ctdlroom *target_room)
1398 {
1399         RoomFunctionHook *fcn;
1400         int total_retval = 0;
1401
1402         MOD_syslog(LOG_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
1403
1404         for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
1405                 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
1406         }
1407
1408         /* Return the sum of the return codes from the hook functions.
1409          */
1410         return total_retval;
1411 }
1412
1413
1414 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
1415 {
1416         NetprocFunctionHook *fcn;
1417         int total_retval = 0;
1418
1419         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
1420                 total_retval = total_retval +
1421                         (*fcn->h_function_pointer) (msg, target_room);
1422         }
1423
1424         /* Return the sum of the return codes from the hook functions.
1425          * A nonzero return code will cause the message to *not* be imported.
1426          */
1427         return total_retval;
1428 }
1429
1430
1431 void PerformDeleteHooks(char *room, long msgnum)
1432 {
1433         DeleteFunctionHook *fcn;
1434
1435         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
1436                 (*fcn->h_function_pointer) (room, msgnum);
1437         }
1438 }
1439
1440
1441
1442
1443
1444 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
1445 {
1446         XmsgFunctionHook *fcn;
1447         int total_sent = 0;
1448         int p;
1449
1450         for (p=0; p<MAX_XMSG_PRI; ++p) {
1451                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1452                         if (fcn->order == p) {
1453                                 total_sent +=
1454                                         (*fcn->h_function_pointer)
1455                                                 (sender, sender_email, recp, msg);
1456                         }
1457                 }
1458                 /* Break out of the loop if a higher-priority function
1459                  * successfully delivered the message.  This prevents duplicate
1460                  * deliveries to local users simultaneously signed onto
1461                  * remote services.
1462                  */
1463                 if (total_sent) break;
1464         }
1465         return total_sent;
1466 }
1467
1468
1469 /*
1470  * Dirty hack until we impliment a hook mechanism for this
1471  */
1472 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1473 {
1474 #ifdef HAVE_OPENSSL
1475         CtdlStartTLS (ok_response, nosup_response, error_response);
1476 #endif
1477 }
1478
1479 void DebugModulesEnable(const int n)
1480 {
1481         DebugModules = n;
1482 }
1483 CTDL_MODULE_INIT(modules)
1484 {
1485         if (!threading) {
1486                 CtdlRegisterDebugFlagHook(HKEY("modules"), DebugModulesEnable, &DebugModules);
1487         }
1488         return "modules";
1489 }