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