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