Continue phase 2 of modules stuff.
[citadel.git] / citadel / serv_extensions.c
1 /*
2  * $Id$
3  *
4  * Citadel Dynamic Loading Module
5  * Written by Brian Costello <btx@calyx.net>
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <dirent.h>
15 #include <string.h>
16 #include <limits.h>
17 #include <ctype.h>
18 #include "citadel.h"
19 #include "server.h"
20 #include "serv_extensions.h"
21 #include "sysdep_decls.h"
22 #include "msgbase.h"
23 #include "tools.h"
24 #include "config.h"
25
26 #include "modules/crypto/serv_crypto.h" /* Needed until a universal crypto startup hook is implimented for CtdlStartTLS */
27
28 #ifndef HAVE_SNPRINTF
29 #include <stdarg.h>
30 #include "snprintf.h"
31 #endif
32
33 struct CleanupFunctionHook *CleanupHookTable = NULL;
34 struct SessionFunctionHook *SessionHookTable = NULL;
35 struct UserFunctionHook *UserHookTable = NULL;
36 struct XmsgFunctionHook *XmsgHookTable = NULL;
37 struct MessageFunctionHook *MessageHookTable = NULL;
38 struct NetprocFunctionHook *NetprocHookTable = NULL;
39 struct DeleteFunctionHook *DeleteHookTable = NULL;
40 struct ServiceFunctionHook *ServiceHookTable = NULL;
41 struct FixedOutputHook *FixedOutputTable = NULL;
42 struct RoomFunctionHook *RoomHookTable = NULL;
43 struct MaintenanceThreadHook *MaintenanceThreadHookTable = NULL;
44
45 struct ProtoFunctionHook {
46         void (*handler) (char *cmdbuf);
47         char *cmd;
48         char *desc;
49         struct ProtoFunctionHook *next;
50 } *ProtoHookList = NULL;
51
52
53 #define ERR_PORT (1 << 1)
54
55
56 static char *portlist = NULL;
57 static size_t nSizPort = 0;
58
59 static char *errormessages = NULL;
60 static size_t nSizErrmsg = 0;
61
62
63 static long   DetailErrorFlags;
64
65 char *ErrSubject = "Startup Problems";
66 char *ErrGeneral = "Citadel had trouble on starting up. %s This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
67 "If you don't want citadel to provide these services, turn them off in WebCit via %s%s\n\n%s\n\n"
68 "To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
69 "The errors returned by the system were:\n%s\n"
70 "You can recheck the above if you follow this faq item:\n"
71 "http://www.citadel.org/doku.php/faq:mastering_your_os:net#netstat";
72
73
74 char *ErrPortShort = "We couldn't bind all ports you configured to be provided by citadel server.";
75 char *ErrPortWhere = "Admin->System Preferences->Network.\n\nThe failed ports and sockets are: ";
76 char *ErrPortHint = "If you want citadel to provide you with that functionality, "
77 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
78 " and stop the programm, that binds these ports. You should eventually remove "
79 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n";
80
81
82 void LogPrintMessages(long err)
83 {
84         char *List, *DetailList, *Short, *Where, *Hint, *Message; 
85         int n = nSizPort + nSizErrmsg + 5;
86
87         Message = (char*) malloc(n * SIZ);
88
89         switch (err)
90         {
91         case ERR_PORT:
92                 Short = ErrPortShort;
93                 Where = ErrPortWhere;
94                 Hint  = ErrPortHint;
95                 List  = portlist;
96                 DetailList = errormessages;
97                 break;
98         default:
99                 Short = "";
100                 Where = "";
101                 Hint  = "";
102                 List  = "";
103                 DetailList = "";
104         }
105
106
107         snprintf(Message, n * SIZ, ErrGeneral, Short, Where, List, Hint, DetailList);
108
109         quickie_message("Citadel", NULL, NULL, AIDEROOM, Message, FMT_FIXED, ErrSubject);
110         if (errormessages!=NULL) free (errormessages);
111         errormessages = NULL;
112         if (portlist!=NULL) free (portlist);
113         portlist = NULL;
114         free(Message);
115 }
116
117
118
119 void AppendString(char **target, char *append, size_t *len, size_t rate)
120 {
121         size_t oLen = 0;
122         long AddLen;
123         long RelPtr = 0;
124
125         AddLen = strlen(append);
126
127         if (*len == 0)
128         {
129                 *len = rate;
130
131                 *target = (char*)malloc (*len * SIZ);
132         }
133         else 
134         {
135                 oLen = strlen(*target);
136                 RelPtr = strlen(*target);
137                 if (oLen + AddLen + 2 > *len * SIZ)
138                 {
139                         char *Buff = *target;
140                         size_t NewSiz = *len + 10;
141                         *target = malloc (NewSiz * SIZ);
142                         memcpy (*target, Buff, NewSiz * SIZ);
143                         *len = NewSiz;
144                 }
145         }
146         memcpy (*target + oLen, append, AddLen);
147         (*target)[oLen + AddLen + 1] = '\n';
148         (*target)[oLen + AddLen + 2] = '\0';
149 }
150
151 void AddPortError(char *Port, char *ErrorMessage)
152 {
153         char *pos;
154         long len;
155
156         DetailErrorFlags |= ERR_PORT;
157
158         AppendString(&errormessages, ErrorMessage, &nSizErrmsg, 10);
159         AppendString(&portlist, Port, &nSizPort, 2);
160
161         pos = strchr (portlist, ':');
162         if (pos != NULL) *pos = ';';
163         
164         len = strlen (errormessages);
165         if (nSizErrmsg * SIZ > len + 3)
166         {
167                 errormessages[len] = ';';
168                 errormessages[len+1] = ' ';
169                 errormessages[len+2] = '\0';
170         }       
171 }
172
173
174 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
175 {
176         struct ProtoFunctionHook *p;
177
178         p = (struct ProtoFunctionHook *)
179                 malloc(sizeof(struct ProtoFunctionHook));
180
181         if (p == NULL) {
182                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
183                 exit(EXIT_FAILURE);
184         }
185         p->handler = handler;
186         p->cmd = cmd;
187         p->desc = desc;
188         p->next = ProtoHookList;
189         ProtoHookList = p;
190         lprintf(CTDL_INFO, "Registered server command %s (%s)\n", cmd, desc);
191 }
192
193
194 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
195 {
196         struct ProtoFunctionHook *cur = NULL;
197         struct ProtoFunctionHook *p = NULL;
198         struct ProtoFunctionHook *lastcur = NULL;
199
200         for (cur = ProtoHookList; 
201              cur != NULL; 
202              cur = (cur != NULL)? cur->next: NULL) {
203                 /* This will also remove duplicates if any */
204                 while (cur != NULL &&
205                                 handler == cur->handler &&
206                                 !strcmp(cmd, cur->cmd)) {
207                         lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
208                                         cmd, cur->desc);
209                         p = cur->next;
210                         if (cur == ProtoHookList) {
211                                 ProtoHookList = p;
212                         }
213                         else if (lastcur != NULL)
214                         {
215                                 lastcur->next = p;
216                         }
217                         free(cur);
218                         cur = p;
219                 }
220                 lastcur = cur;
221         }
222 }
223
224 void CtdlDestroyProtoHooks(void)
225 {
226         struct ProtoFunctionHook *cur, *p;
227
228         cur = ProtoHookList; 
229         while (cur != NULL)
230         {
231                 lprintf(CTDL_INFO, "Destroyed server command %s (%s)\n",
232                         cur->cmd, cur->desc);
233                 p = cur->next;
234                 free(cur);
235                 cur = p;
236         }
237         ProtoHookList = NULL;
238 }
239
240
241 int DLoader_Exec_Cmd(char *cmdbuf)
242 {
243         struct ProtoFunctionHook *p;
244
245         for (p = ProtoHookList; p; p = p->next) {
246                 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
247                         p->handler(&cmdbuf[5]);
248                         return 1;
249                 }
250         }
251         return 0;
252 }
253
254 #if 0
255 void initialize_server_extensions(void)
256 {
257         long filter;
258
259         nSizErrmsg = 0;
260
261         /*lprintf(CTDL_INFO, "%s\n", serv_bio_init());
262         lprintf(CTDL_INFO, "%s\n", serv_calendar_init());
263         lprintf(CTDL_INFO, "%s\n", serv_notes_init());
264         lprintf(CTDL_INFO, "%s\n", serv_ldap_init());
265         lprintf(CTDL_INFO, "%s\n", serv_chat_init());
266         lprintf(CTDL_INFO, "%s\n", serv_expire_init());
267         lprintf(CTDL_INFO, "%s\n", serv_imap_init());
268         lprintf(CTDL_INFO, "%s\n", serv_upgrade_init());
269         lprintf(CTDL_INFO, "%s\n", serv_inetcfg_init());
270         lprintf(CTDL_INFO, "%s\n", serv_listsub_init());
271         lprintf(CTDL_INFO, "%s\n", serv_mrtg_init());
272         lprintf(CTDL_INFO, "%s\n", serv_netfilter_init());
273         lprintf(CTDL_INFO, "%s\n", serv_network_init());
274         lprintf(CTDL_INFO, "%s\n", serv_newuser_init());
275         lprintf(CTDL_INFO, "%s\n", serv_pas2_init());
276         lprintf(CTDL_INFO, "%s\n", serv_smtp_init());
277         lprintf(CTDL_INFO, "%s\n", serv_pop3_init());
278         lprintf(CTDL_INFO, "%s\n", serv_rwho_init());
279         lprintf(CTDL_INFO, "%s\n", serv_spam_init());*/
280         /* lprintf(CTDL_INFO, "%s\n", serv_test_init()); */
281         /*lprintf(CTDL_INFO, "%s\n", serv_vandelay_init());
282         lprintf(CTDL_INFO, "%s\n", serv_vcard_init());
283         lprintf(CTDL_INFO, "%s\n", serv_fulltext_init());
284         lprintf(CTDL_INFO, "%s\n", serv_autocompletion_init());
285         lprintf(CTDL_INFO, "%s\n", serv_postfix_tcpdict());
286         lprintf(CTDL_INFO, "%s\n", serv_sieve_init());
287         lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
288         lprintf(CTDL_INFO, "%s\n", serv_funambol_init());*/
289         for (filter = 1; filter != 0; filter = filter << 1)
290                 if ((filter & DetailErrorFlags) != 0)
291                         LogPrintMessages(filter);
292 }
293
294 #endif
295
296 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
297 {
298
299         struct CleanupFunctionHook *newfcn;
300
301         newfcn = (struct CleanupFunctionHook *)
302             malloc(sizeof(struct CleanupFunctionHook));
303         newfcn->next = CleanupHookTable;
304         newfcn->h_function_pointer = fcn_ptr;
305         CleanupHookTable = newfcn;
306
307         lprintf(CTDL_INFO, "Registered a new cleanup function\n");
308 }
309
310
311 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
312 {
313         struct CleanupFunctionHook *cur, *p;
314
315         for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
316                 /* This will also remove duplicates if any */
317                 while (cur != NULL &&
318                                 fcn_ptr == cur->h_function_pointer) {
319                         lprintf(CTDL_INFO, "Unregistered cleanup function\n");
320                         p = cur->next;
321                         if (cur == CleanupHookTable) {
322                                 CleanupHookTable = p;
323                         }
324                         free(cur);
325                         cur = p;
326                 }
327         }
328 }
329
330 void CtdlDestroyCleanupHooks(void)
331 {
332         struct CleanupFunctionHook *cur, *p;
333
334         cur = CleanupHookTable;
335         while (cur != NULL)
336         {
337                 lprintf(CTDL_INFO, "Destroyed cleanup function\n");
338                 p = cur->next;
339                 free(cur);
340                 cur = p;
341         }
342         CleanupHookTable = NULL;
343 }
344
345
346 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
347 {
348
349         struct SessionFunctionHook *newfcn;
350
351         newfcn = (struct SessionFunctionHook *)
352             malloc(sizeof(struct SessionFunctionHook));
353         newfcn->next = SessionHookTable;
354         newfcn->h_function_pointer = fcn_ptr;
355         newfcn->eventtype = EventType;
356         SessionHookTable = newfcn;
357
358         lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
359                 EventType);
360 }
361
362
363 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
364 {
365         struct SessionFunctionHook *cur, *p;
366
367         for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
368                 /* This will also remove duplicates if any */
369                 while (cur != NULL &&
370                                 fcn_ptr == cur->h_function_pointer &&
371                                 EventType == cur->eventtype) {
372                         lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
373                                         EventType);
374                         p = cur->next;
375                         if (cur == SessionHookTable) {
376                                 SessionHookTable = p;
377                         }
378                         free(cur);
379                         cur = p;
380                 }
381         }
382 }
383
384 void CtdlDestroySessionHooks(void)
385 {
386         struct SessionFunctionHook *cur, *p;
387
388         cur = SessionHookTable;
389         while (cur != NULL)
390         {
391                 lprintf(CTDL_INFO, "Destroyed session function\n");
392                 p = cur->next;
393                 free(cur);
394                 cur = p;
395         }
396         SessionHookTable = NULL;
397 }
398
399
400 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
401 {
402
403         struct UserFunctionHook *newfcn;
404
405         newfcn = (struct UserFunctionHook *)
406             malloc(sizeof(struct UserFunctionHook));
407         newfcn->next = UserHookTable;
408         newfcn->h_function_pointer = fcn_ptr;
409         newfcn->eventtype = EventType;
410         UserHookTable = newfcn;
411
412         lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
413                 EventType);
414 }
415
416
417 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
418 {
419         struct UserFunctionHook *cur, *p;
420
421         for (cur = UserHookTable; cur != NULL; cur = cur->next) {
422                 /* This will also remove duplicates if any */
423                 while (cur != NULL &&
424                                 fcn_ptr == cur->h_function_pointer &&
425                                 EventType == cur->eventtype) {
426                         lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
427                                         EventType);
428                         p = cur->next;
429                         if (cur == UserHookTable) {
430                                 UserHookTable = p;
431                         }
432                         free(cur);
433                         cur = p;
434                 }
435         }
436 }
437
438 void CtdlDestroyUserHooks(void)
439 {
440         struct UserFunctionHook *cur, *p;
441
442         cur = UserHookTable;
443         while (cur != NULL)
444         {
445                 lprintf(CTDL_INFO, "Destroyed user function \n");
446                 p = cur->next;
447                 free(cur);
448                 cur = p;
449         }
450         UserHookTable = NULL;
451 }
452
453
454 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
455                                 int EventType)
456 {
457
458         struct MessageFunctionHook *newfcn;
459
460         newfcn = (struct MessageFunctionHook *)
461             malloc(sizeof(struct MessageFunctionHook));
462         newfcn->next = MessageHookTable;
463         newfcn->h_function_pointer = handler;
464         newfcn->eventtype = EventType;
465         MessageHookTable = newfcn;
466
467         lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
468                 EventType);
469 }
470
471
472 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
473                 int EventType)
474 {
475         struct MessageFunctionHook *cur, *p;
476
477         for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
478                 /* This will also remove duplicates if any */
479                 while (cur != NULL &&
480                                 handler == cur->h_function_pointer &&
481                                 EventType == cur->eventtype) {
482                         lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
483                                         EventType);
484                         p = cur->next;
485                         if (cur == MessageHookTable) {
486                                 MessageHookTable = p;
487                         }
488                         free(cur);
489                         cur = p;
490                 }
491         }
492 }
493
494 void CtdlDestroyMessageHook(void)
495 {
496         struct MessageFunctionHook *cur, *p;
497
498         cur = MessageHookTable; 
499         while (cur != NULL)
500         {
501                 lprintf(CTDL_INFO, "Destroyed message function \n");
502                 p = cur->next;
503                 free(cur);
504                 cur = p;
505         }
506         MessageHookTable = NULL;
507 }
508
509
510 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
511 {
512         struct RoomFunctionHook *newfcn;
513
514         newfcn = (struct RoomFunctionHook *)
515             malloc(sizeof(struct RoomFunctionHook));
516         newfcn->next = RoomHookTable;
517         newfcn->fcn_ptr = fcn_ptr;
518         RoomHookTable = newfcn;
519
520         lprintf(CTDL_INFO, "Registered a new room function\n");
521 }
522
523
524 void CtdlUnregisterRoomHook(int (*fcn_ptr)(struct ctdlroom *))
525 {
526         struct RoomFunctionHook *cur, *p;
527
528         for (cur = RoomHookTable; cur != NULL; cur = cur->next) {
529                 while (cur != NULL && fcn_ptr == cur->fcn_ptr) {
530                         lprintf(CTDL_INFO, "Unregistered room function\n");
531                         p = cur->next;
532                         if (cur == RoomHookTable) {
533                                 RoomHookTable = p;
534                         }
535                         free(cur);
536                         cur = p;
537                 }
538         }
539 }
540
541
542 void CtdlDestroyRoomHooks(void)
543 {
544         struct RoomFunctionHook *cur, *p;
545
546         cur = RoomHookTable;
547         while (cur != NULL)
548         {
549                 lprintf(CTDL_INFO, "Unregistered room function\n");
550                 p = cur->next;
551                 free(cur);
552                 cur = p;
553         }
554         RoomHookTable = NULL;
555 }
556
557 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
558 {
559         struct NetprocFunctionHook *newfcn;
560
561         newfcn = (struct NetprocFunctionHook *)
562             malloc(sizeof(struct NetprocFunctionHook));
563         newfcn->next = NetprocHookTable;
564         newfcn->h_function_pointer = handler;
565         NetprocHookTable = newfcn;
566
567         lprintf(CTDL_INFO, "Registered a new netproc function\n");
568 }
569
570
571 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
572 {
573         struct NetprocFunctionHook *cur, *p;
574
575         for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
576                 /* This will also remove duplicates if any */
577                 while (cur != NULL &&
578                                 handler == cur->h_function_pointer ) {
579                         lprintf(CTDL_INFO, "Unregistered netproc function\n");
580                         p = cur->next;
581                         if (cur == NetprocHookTable) {
582                                 NetprocHookTable = p;
583                         }
584                         free(cur);
585                         cur = p;
586                 }
587         }
588 }
589
590 void CtdlDestroyNetprocHooks(void)
591 {
592         struct NetprocFunctionHook *cur, *p;
593
594         cur = NetprocHookTable;
595         while (cur != NULL)
596         {
597                 lprintf(CTDL_INFO, "Unregistered netproc function\n");
598                 p = cur->next;
599                 free(cur);
600                 cur = p;
601         }
602         NetprocHookTable = NULL;
603 }
604
605
606 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
607 {
608         struct DeleteFunctionHook *newfcn;
609
610         newfcn = (struct DeleteFunctionHook *)
611             malloc(sizeof(struct DeleteFunctionHook));
612         newfcn->next = DeleteHookTable;
613         newfcn->h_function_pointer = handler;
614         DeleteHookTable = newfcn;
615
616         lprintf(CTDL_INFO, "Registered a new netproc function\n");
617 }
618
619
620 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
621 {
622         struct DeleteFunctionHook *cur, *p;
623
624         for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
625                 /* This will also remove duplicates if any */
626                 while (cur != NULL &&
627                                 handler == cur->h_function_pointer ) {
628                         lprintf(CTDL_INFO, "Unregistered netproc function\n");
629                         p = cur->next;
630                         if (cur == DeleteHookTable) {
631                                 DeleteHookTable = p;
632                         }
633                         free(cur);
634                         cur = p;
635                 }
636         }
637 }
638 void CtdlDestroyDeleteHooks(void)
639 {
640         struct DeleteFunctionHook *cur, *p;
641
642         cur = DeleteHookTable;
643         while (cur != NULL)
644         {
645                 lprintf(CTDL_INFO, "Destroyed netproc function\n");
646                 p = cur->next;
647                 free(cur);
648                 cur = p;                
649         }
650         DeleteHookTable = NULL;
651 }
652
653
654
655
656 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
657 {
658         struct FixedOutputHook *newfcn;
659
660         newfcn = (struct FixedOutputHook *)
661             malloc(sizeof(struct FixedOutputHook));
662         newfcn->next = FixedOutputTable;
663         newfcn->h_function_pointer = handler;
664         safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
665         FixedOutputTable = newfcn;
666
667         lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
668 }
669
670
671 void CtdlUnregisterFixedOutputHook(char *content_type)
672 {
673         struct FixedOutputHook *cur, *p;
674
675         for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
676                 /* This will also remove duplicates if any */
677                 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
678                         lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
679                         p = cur->next;
680                         if (cur == FixedOutputTable) {
681                                 FixedOutputTable = p;
682                         }
683                         free(cur);
684                         cur = p;
685                 }
686         }
687 }
688
689 void CtdlDestroyFixedOutputHooks(void)
690 {
691         struct FixedOutputHook *cur, *p;
692
693         cur = FixedOutputTable; 
694         while (cur != NULL)
695         {
696                 lprintf(CTDL_INFO, "Destroyed fixed output function for %s\n", cur->content_type);
697                 p = cur->next;
698                 free(cur);
699                 cur = p;
700                 
701         }
702         FixedOutputTable = NULL;
703 }
704
705 /* returns nonzero if we found a hook and used it */
706 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
707 {
708         struct FixedOutputHook *fcn;
709
710         for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
711                 if (!strcasecmp(content_type, fcn->content_type)) {
712                         (*fcn->h_function_pointer) (content, content_length);
713                         return(1);
714                 }
715         }
716         return(0);
717 }
718
719
720
721
722
723 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
724 {
725
726         struct XmsgFunctionHook *newfcn;
727
728         newfcn = (struct XmsgFunctionHook *)
729             malloc(sizeof(struct XmsgFunctionHook));
730         newfcn->next = XmsgHookTable;
731         newfcn->order = order;
732         newfcn->h_function_pointer = fcn_ptr;
733         XmsgHookTable = newfcn;
734         lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
735 }
736
737
738 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
739 {
740         struct XmsgFunctionHook *cur, *p;
741
742         for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
743                 /* This will also remove duplicates if any */
744                 while (cur != NULL &&
745                                 fcn_ptr == cur->h_function_pointer &&
746                                 order == cur->order) {
747                         lprintf(CTDL_INFO, "Unregistered x-msg function "
748                                         "(priority %d)\n", order);
749                         p = cur->next;
750                         if (cur == XmsgHookTable) {
751                                 XmsgHookTable = p;
752                         }
753                         free(cur);
754                         cur = p;
755                 }
756         }
757 }
758
759 void CtdlDestroyXmsgHooks(void)
760 {
761         struct XmsgFunctionHook *cur, *p;
762
763         cur = XmsgHookTable;
764         while (cur != NULL)
765         {
766                 lprintf(CTDL_INFO, "Destroyed x-msg function "
767                         "(priority %d)\n", cur->order);
768                 p = cur->next;
769                         
770                 free(cur);
771                 cur = p;
772         }
773         XmsgHookTable = NULL;
774 }
775
776
777 void CtdlRegisterServiceHook(int tcp_port,
778                         char *sockpath,
779                         void (*h_greeting_function) (void),
780                         void (*h_command_function) (void),
781                         void (*h_async_function) (void)
782                         )
783 {
784         struct ServiceFunctionHook *newfcn;
785         char *message;
786         char *error;
787
788         error = NULL;
789         newfcn = (struct ServiceFunctionHook *)
790             malloc(sizeof(struct ServiceFunctionHook));
791         message = (char*) malloc (SIZ);
792         
793         newfcn->next = ServiceHookTable;
794         newfcn->tcp_port = tcp_port;
795         newfcn->sockpath = sockpath;
796         newfcn->h_greeting_function = h_greeting_function;
797         newfcn->h_command_function = h_command_function;
798         newfcn->h_async_function = h_async_function;
799
800         if (sockpath != NULL) {
801                 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions, &error);
802                 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
803         }
804         else if (tcp_port <= 0) {       /* port -1 to disable */
805                 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
806                 free (message);
807                 free(newfcn);
808                 return;
809         }
810         else {
811                 newfcn->msock = ig_tcp_server(config.c_ip_addr,
812                                               tcp_port,
813                                               config.c_maxsessions, 
814                                               &error);
815                 snprintf(message, SIZ, "TCP port %s:%d: ", 
816                          config.c_ip_addr, tcp_port);
817         }
818
819         if (newfcn->msock > 0) {
820                 ServiceHookTable = newfcn;
821                 strcat(message, "registered.");
822                 lprintf(CTDL_INFO, "%s\n", message);
823         }
824         else {
825                 AddPortError(message, error);
826                 strcat(message, "FAILED.");
827                 lprintf(CTDL_CRIT, "%s\n", message);
828                 free(error);
829                 free(newfcn);
830         }
831         free(message);
832 }
833
834
835 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
836                         void (*h_greeting_function) (void),
837                         void (*h_command_function) (void),
838                         void (*h_async_function) (void)
839                         )
840 {
841         struct ServiceFunctionHook *cur, *p;
842
843         for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
844                 /* This will also remove duplicates if any */
845                 while (cur != NULL &&
846                                 !(sockpath && cur->sockpath &&
847                                         strcmp(sockpath, cur->sockpath)) &&
848                                 h_greeting_function == cur->h_greeting_function &&
849                                 h_command_function == cur->h_command_function &&
850                                 h_async_function == cur->h_async_function &&
851                                 tcp_port == cur->tcp_port) {
852                         close(cur->msock);
853                         if (sockpath) {
854                                 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
855                                                 sockpath);
856                         } else if (tcp_port) {
857                                 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
858                         } else {
859                                 lprintf(CTDL_INFO, "Unregistered unknown service\n");
860                         }
861                         p = cur->next;
862                         if (cur == ServiceHookTable) {
863                                 ServiceHookTable = p;
864                         }
865                         free(cur);
866                         cur = p;
867                 }
868         }
869 }
870
871 void CtdlDestroyServiceHook(void)
872 {
873         struct ServiceFunctionHook *cur, *p;
874
875         cur = ServiceHookTable;
876         while (cur != NULL)
877         {
878                 close(cur->msock);
879                 if (cur->sockpath) {
880                         lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
881                                 cur->sockpath);
882                 } else if (cur->tcp_port) {
883                         lprintf(CTDL_INFO, "Closed TCP port %d\n", cur->tcp_port);
884                 } else {
885                         lprintf(CTDL_INFO, "Unregistered unknown service\n");
886                 }
887                 p = cur->next;
888                 free(cur);
889                 cur = p;
890         }
891         ServiceHookTable = NULL;
892 }
893
894
895 void PerformSessionHooks(int EventType)
896 {
897         struct SessionFunctionHook *fcn = NULL;
898
899         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
900                 if (fcn->eventtype == EventType) {
901                         (*fcn->h_function_pointer) ();
902                 }
903         }
904 }
905
906 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
907 {
908         struct UserFunctionHook *fcn = NULL;
909
910         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
911                 if (fcn->eventtype == EventType) {
912                         (*fcn->h_function_pointer) (usbuf);
913                 }
914         }
915 }
916
917 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
918 {
919         struct MessageFunctionHook *fcn = NULL;
920         int total_retval = 0;
921
922         /* Other code may elect to protect this message from server-side
923          * handlers; if this is the case, don't do anything.
924         lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
925                 EventType, msg->cm_flags);
926          */
927         if (msg->cm_flags & CM_SKIP_HOOKS) {
928                 lprintf(CTDL_DEBUG, "Skipping hooks\n");
929                 return(0);
930         }
931
932         /* Otherwise, run all the hooks appropriate to this event type.
933          */
934         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
935                 if (fcn->eventtype == EventType) {
936                         total_retval = total_retval +
937                                 (*fcn->h_function_pointer) (msg);
938                 }
939         }
940
941         /* Return the sum of the return codes from the hook functions.  If
942          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
943          * the save operation to abort.
944          */
945         return total_retval;
946 }
947
948
949 int PerformRoomHooks(struct ctdlroom *target_room)
950 {
951         struct RoomFunctionHook *fcn;
952         int total_retval = 0;
953
954         lprintf(CTDL_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
955
956         for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
957                 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
958         }
959
960         /* Return the sum of the return codes from the hook functions.
961          */
962         return total_retval;
963 }
964
965
966 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
967 {
968         struct NetprocFunctionHook *fcn;
969         int total_retval = 0;
970
971         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
972                 total_retval = total_retval +
973                         (*fcn->h_function_pointer) (msg, target_room);
974         }
975
976         /* Return the sum of the return codes from the hook functions.
977          * A nonzero return code will cause the message to *not* be imported.
978          */
979         return total_retval;
980 }
981
982
983 void PerformDeleteHooks(char *room, long msgnum)
984 {
985         struct DeleteFunctionHook *fcn;
986
987         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
988                 (*fcn->h_function_pointer) (room, msgnum);
989         }
990 }
991
992
993
994
995
996 int PerformXmsgHooks(char *sender, char *recp, char *msg)
997 {
998         struct XmsgFunctionHook *fcn;
999         int total_sent = 0;
1000         int p;
1001
1002         for (p=0; p<MAX_XMSG_PRI; ++p) {
1003                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
1004                         if (fcn->order == p) {
1005                                 total_sent +=
1006                                         (*fcn->h_function_pointer)
1007                                                 (sender, recp, msg);
1008                         }
1009                 }
1010                 /* Break out of the loop if a higher-priority function
1011                  * successfully delivered the message.  This prevents duplicate
1012                  * deliveries to local users simultaneously signed onto
1013                  * remote services.
1014                  */
1015                 if (total_sent) break;
1016         }
1017         return total_sent;
1018 }
1019
1020 void CtdlRegisterMaintenanceThread(char *name, void *(*thread_proc)(void *arg))
1021 {
1022         struct MaintenanceThreadHook *newfcn;
1023
1024         newfcn = (struct MaintenanceThreadHook *)
1025             malloc(sizeof(struct MaintenanceThreadHook));
1026         newfcn->name = name;
1027         newfcn->next = MaintenanceThreadHookTable;
1028         newfcn->fcn_ptr = thread_proc;
1029         MaintenanceThreadHookTable = newfcn;
1030
1031         lprintf(CTDL_INFO, "Registered a new maintenance thread function\n");
1032 }
1033
1034
1035 /*
1036  * Dirty hack until we impliment a hook mechanism for this
1037  */
1038 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1039 {
1040 #ifdef HAVE_OPENSSL
1041         CtdlStartTLS (ok_response, nosup_response, error_response);
1042 #endif
1043 }