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