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