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