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