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