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