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