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