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