* rework imap tokenizer, we no longer copy the stuff around, we keep a reference...
[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;
731
732         error = NULL;
733         newfcn = (struct ServiceFunctionHook *)
734             malloc(sizeof(struct ServiceFunctionHook));
735         message = (char*) malloc (SIZ);
736         
737         newfcn->next = ServiceHookTable;
738         newfcn->tcp_port = tcp_port;
739         newfcn->sockpath = sockpath;
740         newfcn->h_greeting_function = h_greeting_function;
741         newfcn->h_command_function = h_command_function;
742         newfcn->h_async_function = h_async_function;
743         newfcn->ServiceName = ServiceName;
744
745         if (sockpath != NULL) {
746                 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions, &error);
747                 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
748         }
749         else if (tcp_port <= 0) {       /* port -1 to disable */
750                 CtdlLogPrintf(CTDL_INFO, "Service %s has been manually disabled, skipping\n", ServiceName);
751                 free (message);
752                 free(newfcn);
753                 return;
754         }
755         else {
756                 newfcn->msock = ig_tcp_server(config.c_ip_addr,
757                                               tcp_port,
758                                               config.c_maxsessions, 
759                                               &error);
760                 snprintf(message, SIZ, "TCP port %s:%d: (%s) ", 
761                          config.c_ip_addr, tcp_port, ServiceName);
762         }
763
764         if (newfcn->msock > 0) {
765                 ServiceHookTable = newfcn;
766                 strcat(message, "registered.");
767                 CtdlLogPrintf(CTDL_INFO, "%s\n", message);
768         }
769         else {
770                 AddPortError(message, error);
771                 strcat(message, "FAILED.");
772                 CtdlLogPrintf(CTDL_CRIT, "%s\n", message);
773                 free(error);
774                 free(newfcn);
775         }
776         free(message);
777 }
778
779
780 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
781                         void (*h_greeting_function) (void),
782                         void (*h_command_function) (void),
783                         void (*h_async_function) (void)
784                         )
785 {
786         struct ServiceFunctionHook *cur, *p;
787
788         for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
789                 /* This will also remove duplicates if any */
790                 while (cur != NULL &&
791                                 !(sockpath && cur->sockpath &&
792                                         strcmp(sockpath, cur->sockpath)) &&
793                                 h_greeting_function == cur->h_greeting_function &&
794                                 h_command_function == cur->h_command_function &&
795                                 h_async_function == cur->h_async_function &&
796                                 tcp_port == cur->tcp_port) {
797                         close(cur->msock);
798                         if (sockpath) {
799                                 CtdlLogPrintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
800                                                 sockpath);
801                         } else if (tcp_port) {
802                                 CtdlLogPrintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
803                         } else {
804                                 CtdlLogPrintf(CTDL_INFO, "Unregistered service \"%s\"\n", cur->ServiceName);
805                         }
806                         p = cur->next;
807                         if (cur == ServiceHookTable) {
808                                 ServiceHookTable = p;
809                         }
810                         free(cur);
811                         cur = p;
812                 }
813         }
814 }
815
816 void CtdlDestroyServiceHook(void)
817 {
818         struct ServiceFunctionHook *cur, *p;
819
820         cur = ServiceHookTable;
821         while (cur != NULL)
822         {
823                 close(cur->msock);
824                 if (cur->sockpath) {
825                         CtdlLogPrintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
826                                 cur->sockpath);
827                 } else if (cur->tcp_port) {
828                         CtdlLogPrintf(CTDL_INFO, "Closed TCP port %d\n", cur->tcp_port);
829                 } else {
830                         CtdlLogPrintf(CTDL_INFO, "Destroyed service \"%s\"\n", cur->ServiceName);
831                 }
832                 p = cur->next;
833                 free(cur);
834                 cur = p;
835         }
836         ServiceHookTable = NULL;
837 }
838
839 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
840 {
841         struct SearchFunctionHook *newfcn;
842
843         if (!name || !fcn_ptr) {
844                 return;
845         }
846         
847         newfcn = (struct SearchFunctionHook *)
848             malloc(sizeof(struct SearchFunctionHook));
849         newfcn->next = SearchFunctionHookTable;
850         newfcn->name = name;
851         newfcn->fcn_ptr = fcn_ptr;
852         SearchFunctionHookTable = newfcn;
853
854         CtdlLogPrintf(CTDL_INFO, "Registered a new search function (%s)\n", name);
855 }
856
857 void CtdlUnregisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name)
858 {
859         struct SearchFunctionHook *cur, *p;
860         
861         for (cur = SearchFunctionHookTable; cur != NULL; cur = cur->next) {
862                 while (fcn_ptr && (cur->fcn_ptr == fcn_ptr) && name && !strcmp(name, cur->name)) {
863                         CtdlLogPrintf(CTDL_INFO, "Unregistered search function(%s)\n", name);
864                         p = cur->next;
865                         if (cur == SearchFunctionHookTable) {
866                                 SearchFunctionHookTable = p;
867                         }
868                         free (cur);
869                         cur = p;
870                 }
871         }
872 }
873
874 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name)
875 {
876         struct SearchFunctionHook *fcn = NULL;
877
878         for (fcn = SearchFunctionHookTable; fcn != NULL; fcn = fcn->next) {
879                 if (!func_name || !strcmp(func_name, fcn->name)) {
880                         (*fcn->fcn_ptr) (num_msgs, search_msgs, search_string);
881                         return;
882                 }
883         }
884         *num_msgs = 0;
885 }
886
887
888 void PerformSessionHooks(int EventType)
889 {
890         struct SessionFunctionHook *fcn = NULL;
891
892         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
893                 if (fcn->eventtype == EventType) {
894                         (*fcn->h_function_pointer) ();
895                 }
896         }
897 }
898
899 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
900 {
901         struct UserFunctionHook *fcn = NULL;
902
903         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
904                 if (fcn->eventtype == EventType) {
905                         (*fcn->h_function_pointer) (usbuf);
906                 }
907         }
908 }
909
910 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
911 {
912         struct MessageFunctionHook *fcn = NULL;
913         int total_retval = 0;
914
915         /* Other code may elect to protect this message from server-side
916          * handlers; if this is the case, don't do anything.
917         CtdlLogPrintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
918                 EventType, msg->cm_flags);
919          */
920         if (msg->cm_flags & CM_SKIP_HOOKS) {
921                 CtdlLogPrintf(CTDL_DEBUG, "Skipping hooks\n");
922                 return(0);
923         }
924
925         /* Otherwise, run all the hooks appropriate to this event type.
926          */
927         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
928                 if (fcn->eventtype == EventType) {
929                         total_retval = total_retval +
930                                 (*fcn->h_function_pointer) (msg);
931                 }
932         }
933
934         /* Return the sum of the return codes from the hook functions.  If
935          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
936          * the save operation to abort.
937          */
938         return total_retval;
939 }
940
941
942 int PerformRoomHooks(struct ctdlroom *target_room)
943 {
944         struct RoomFunctionHook *fcn;
945         int total_retval = 0;
946
947         CtdlLogPrintf(CTDL_DEBUG, "Performing room hooks for <%s>\n", target_room->QRname);
948
949         for (fcn = RoomHookTable; fcn != NULL; fcn = fcn->next) {
950                 total_retval = total_retval + (*fcn->fcn_ptr) (target_room);
951         }
952
953         /* Return the sum of the return codes from the hook functions.
954          */
955         return total_retval;
956 }
957
958
959 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
960 {
961         struct NetprocFunctionHook *fcn;
962         int total_retval = 0;
963
964         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
965                 total_retval = total_retval +
966                         (*fcn->h_function_pointer) (msg, target_room);
967         }
968
969         /* Return the sum of the return codes from the hook functions.
970          * A nonzero return code will cause the message to *not* be imported.
971          */
972         return total_retval;
973 }
974
975
976 void PerformDeleteHooks(char *room, long msgnum)
977 {
978         struct DeleteFunctionHook *fcn;
979
980         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
981                 (*fcn->h_function_pointer) (room, msgnum);
982         }
983 }
984
985
986
987
988
989 int PerformXmsgHooks(char *sender, char *sender_email, char *recp, char *msg)
990 {
991         struct XmsgFunctionHook *fcn;
992         int total_sent = 0;
993         int p;
994
995         for (p=0; p<MAX_XMSG_PRI; ++p) {
996                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
997                         if (fcn->order == p) {
998                                 total_sent +=
999                                         (*fcn->h_function_pointer)
1000                                                 (sender, sender_email, recp, msg);
1001                         }
1002                 }
1003                 /* Break out of the loop if a higher-priority function
1004                  * successfully delivered the message.  This prevents duplicate
1005                  * deliveries to local users simultaneously signed onto
1006                  * remote services.
1007                  */
1008                 if (total_sent) break;
1009         }
1010         return total_sent;
1011 }
1012
1013
1014 /*
1015  * Dirty hack until we impliment a hook mechanism for this
1016  */
1017 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response)
1018 {
1019 #ifdef HAVE_OPENSSL
1020         CtdlStartTLS (ok_response, nosup_response, error_response);
1021 #endif
1022 }