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