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