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