* added message subject to all those tiny messages
[citadel.git] / citadel / serv_extensions.c
1 /*
2  * $Id$
3  *
4  * Citadel Dynamic Loading Module
5  * Written by Brian Costello <btx@calyx.net>
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <dirent.h>
15 #include <string.h>
16 #include <limits.h>
17 #include <ctype.h>
18 #include "citadel.h"
19 #include "server.h"
20 #include "serv_extensions.h"
21 #include "sysdep_decls.h"
22 #include "msgbase.h"
23 #include "tools.h"
24 #include "config.h"
25
26 #ifndef HAVE_SNPRINTF
27 #include <stdarg.h>
28 #include "snprintf.h"
29 #endif
30
31 struct CleanupFunctionHook *CleanupHookTable = NULL;
32 struct SessionFunctionHook *SessionHookTable = NULL;
33 struct UserFunctionHook *UserHookTable = NULL;
34 struct XmsgFunctionHook *XmsgHookTable = NULL;
35 struct MessageFunctionHook *MessageHookTable = NULL;
36 struct NetprocFunctionHook *NetprocHookTable = NULL;
37 struct DeleteFunctionHook *DeleteHookTable = NULL;
38 struct ServiceFunctionHook *ServiceHookTable = NULL;
39 struct FixedOutputHook *FixedOutputTable = NULL;
40
41 struct ProtoFunctionHook {
42         void (*handler) (char *cmdbuf);
43         char *cmd;
44         char *desc;
45         struct ProtoFunctionHook *next;
46 } *ProtoHookList = NULL;
47
48
49 #define ERR_PORT (1 << 1)
50
51
52 static char *portlist = NULL;
53 static size_t nSizPort = 0;
54
55 static char *errormessages = NULL;
56 static size_t nSizErrmsg = 0;
57
58
59 static long   DetailErrorFlags;
60
61
62 char *ErrSubject = "Startup Problems";
63 char *ErrGeneral = "Citadel had trouble on starting up. %s This means, citadel won't be the service provider for a specific service you configured it to.\n\n"
64 "If you don't want citadel to provide these services, turn them off in WebCit via %s%s\n\n%s\n\n"
65 "To make both ways actualy take place restart the citserver with \"sendcommand down\"\n\n"
66 "The errors returned by the system were:\n%s\n";
67
68
69 char *ErrPortShort = "We couldn't bind all ports you configured to be provided by citadel server.";
70 char *ErrPortWhere = "Admin->System Preferences->Network.\n\nThe failed ports and sockets are: ";
71 char *ErrPortHint = "If you want citadel to provide you with that functionality, "
72 "check the output of \"netstat -lnp\" on linux Servers or \"netstat -na\" on *BSD"
73 " and stop the programm, that binds these ports. You should eventually remove "
74 " their initscripts in /etc/init.d so that you won't get this trouble once more.\n";
75
76
77 void LogPrintMessages(long err)
78 {
79         char *List, *DetailList, *Short, *Where, *Hint, *Message; 
80         int n = nSizPort + nSizErrmsg + 5;
81
82         Message = (char*) malloc(n * SIZ);
83
84         switch (err)
85         {
86         case ERR_PORT:
87                 Short = ErrPortShort;
88                 Where = ErrPortWhere;
89                 Hint  = ErrPortHint;
90                 List  = portlist;
91                 DetailList = errormessages;
92                 break;
93         default:
94                 Short = "";
95                 Where = "";
96                 Hint  = "";
97                 List  = "";
98                 DetailList = "";
99         }
100
101
102         snprintf(Message, n * SIZ, ErrGeneral, Short, Where, List, Hint, DetailList);
103
104         quickie_message("Citadel", NULL, NULL, AIDEROOM, Message, FMT_FIXED, ErrSubject);
105         free(Message);
106 }
107
108
109
110 void AppendString(char **target, char *append, size_t *len, size_t rate)
111 {
112         size_t oLen = 0;
113         long AddLen;
114         long RelPtr = 0;
115
116         AddLen = strlen(append);
117
118         if (*len == 0)
119         {
120                 *len = rate;
121
122                 *target = (char*)malloc (*len * SIZ);
123         }
124         else 
125         {
126                 oLen = strlen(*target);
127                 RelPtr = strlen(*target);
128                 if (oLen + AddLen + 2 > *len * SIZ)
129                 {
130                         char *Buff = *target;
131                         size_t NewSiz = *len + 10;
132                         *target = malloc (NewSiz * SIZ);
133                         memcpy (*target, Buff, NewSiz * SIZ);
134                         *len = NewSiz;
135                 }
136         }
137         memcpy (*target + oLen, append, AddLen);
138         (*target)[AddLen + 1] = '\n';
139         (*target)[AddLen + 2] = '\0';
140 }
141
142 void AddPortError(char *Port, char *ErrorMessage)
143 {
144         DetailErrorFlags |= ERR_PORT;
145
146         AppendString(&errormessages, ErrorMessage, &nSizErrmsg, 10);
147         AppendString(&portlist, Port, &nSizPort, 2);
148 }
149
150
151 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
152 {
153         struct ProtoFunctionHook *p;
154
155         p = (struct ProtoFunctionHook *)
156                 malloc(sizeof(struct ProtoFunctionHook));
157
158         if (p == NULL) {
159                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
160                 exit(EXIT_FAILURE);
161         }
162         p->handler = handler;
163         p->cmd = cmd;
164         p->desc = desc;
165         p->next = ProtoHookList;
166         ProtoHookList = p;
167         lprintf(CTDL_INFO, "Registered server command %s (%s)\n", cmd, desc);
168 }
169
170
171 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
172 {
173         struct ProtoFunctionHook *cur, *p;
174
175         for (cur = ProtoHookList; cur != NULL; cur = cur->next) {
176                 /* This will also remove duplicates if any */
177                 while (cur != NULL &&
178                                 handler == cur->handler &&
179                                 !strcmp(cmd, cur->cmd)) {
180                         lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
181                                         cmd, cur->desc);
182                         p = cur->next;
183                         if (cur == ProtoHookList) {
184                                 ProtoHookList = p;
185                         }
186                         free(cur);
187                         cur = p;
188                 }
189         }
190 }
191
192
193 int DLoader_Exec_Cmd(char *cmdbuf)
194 {
195         struct ProtoFunctionHook *p;
196
197         for (p = ProtoHookList; p; p = p->next) {
198                 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
199                         p->handler(&cmdbuf[5]);
200                         return 1;
201                 }
202         }
203         return 0;
204 }
205
206 void initialize_server_extensions(void)
207 {
208         long filter;
209
210         lprintf(CTDL_INFO, "%s\n", serv_bio_init());
211         lprintf(CTDL_INFO, "%s\n", serv_calendar_init());
212         lprintf(CTDL_INFO, "%s\n", serv_notes_init());
213         lprintf(CTDL_INFO, "%s\n", serv_ldap_init());
214         lprintf(CTDL_INFO, "%s\n", serv_chat_init());
215         lprintf(CTDL_INFO, "%s\n", serv_expire_init());
216         lprintf(CTDL_INFO, "%s\n", serv_imap_init());
217         lprintf(CTDL_INFO, "%s\n", serv_upgrade_init());
218         lprintf(CTDL_INFO, "%s\n", serv_inetcfg_init());
219         lprintf(CTDL_INFO, "%s\n", serv_listsub_init());
220         lprintf(CTDL_INFO, "%s\n", serv_mrtg_init());
221         lprintf(CTDL_INFO, "%s\n", serv_netfilter_init());
222         lprintf(CTDL_INFO, "%s\n", serv_network_init());
223         lprintf(CTDL_INFO, "%s\n", serv_newuser_init());
224         lprintf(CTDL_INFO, "%s\n", serv_pas2_init());
225         lprintf(CTDL_INFO, "%s\n", serv_smtp_init());
226         lprintf(CTDL_INFO, "%s\n", serv_pop3_init());
227         lprintf(CTDL_INFO, "%s\n", serv_rwho_init());
228         lprintf(CTDL_INFO, "%s\n", serv_spam_init());
229         /* lprintf(CTDL_INFO, "%s\n", serv_test_init()); */
230         lprintf(CTDL_INFO, "%s\n", serv_vandelay_init());
231         lprintf(CTDL_INFO, "%s\n", serv_vcard_init());
232         lprintf(CTDL_INFO, "%s\n", serv_fulltext_init());
233         lprintf(CTDL_INFO, "%s\n", serv_autocompletion_init());
234         lprintf(CTDL_INFO, "%s\n", serv_postfix_tcpdict());
235         lprintf(CTDL_INFO, "%s\n", serv_sieve_init());
236         lprintf(CTDL_INFO, "%s\n", serv_managesieve_init());
237
238         for (filter = 1; filter != 0; filter = filter << 1)
239                 if ((filter & DetailErrorFlags) != 0)
240                         LogPrintMessages(filter);
241 }
242
243
244
245 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
246 {
247
248         struct CleanupFunctionHook *newfcn;
249
250         newfcn = (struct CleanupFunctionHook *)
251             malloc(sizeof(struct CleanupFunctionHook));
252         newfcn->next = CleanupHookTable;
253         newfcn->h_function_pointer = fcn_ptr;
254         CleanupHookTable = newfcn;
255
256         lprintf(CTDL_INFO, "Registered a new cleanup function\n");
257 }
258
259
260 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
261 {
262         struct CleanupFunctionHook *cur, *p;
263
264         for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
265                 /* This will also remove duplicates if any */
266                 while (cur != NULL &&
267                                 fcn_ptr == cur->h_function_pointer) {
268                         lprintf(CTDL_INFO, "Unregistered cleanup function\n");
269                         p = cur->next;
270                         if (cur == CleanupHookTable) {
271                                 CleanupHookTable = p;
272                         }
273                         free(cur);
274                         cur = p;
275                 }
276         }
277 }
278
279
280 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
281 {
282
283         struct SessionFunctionHook *newfcn;
284
285         newfcn = (struct SessionFunctionHook *)
286             malloc(sizeof(struct SessionFunctionHook));
287         newfcn->next = SessionHookTable;
288         newfcn->h_function_pointer = fcn_ptr;
289         newfcn->eventtype = EventType;
290         SessionHookTable = newfcn;
291
292         lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
293                 EventType);
294 }
295
296
297 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
298 {
299         struct SessionFunctionHook *cur, *p;
300
301         for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
302                 /* This will also remove duplicates if any */
303                 while (cur != NULL &&
304                                 fcn_ptr == cur->h_function_pointer &&
305                                 EventType == cur->eventtype) {
306                         lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
307                                         EventType);
308                         p = cur->next;
309                         if (cur == SessionHookTable) {
310                                 SessionHookTable = p;
311                         }
312                         free(cur);
313                         cur = p;
314                 }
315         }
316 }
317
318
319 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
320 {
321
322         struct UserFunctionHook *newfcn;
323
324         newfcn = (struct UserFunctionHook *)
325             malloc(sizeof(struct UserFunctionHook));
326         newfcn->next = UserHookTable;
327         newfcn->h_function_pointer = fcn_ptr;
328         newfcn->eventtype = EventType;
329         UserHookTable = newfcn;
330
331         lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
332                 EventType);
333 }
334
335
336 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
337 {
338         struct UserFunctionHook *cur, *p;
339
340         for (cur = UserHookTable; cur != NULL; cur = cur->next) {
341                 /* This will also remove duplicates if any */
342                 while (cur != NULL &&
343                                 fcn_ptr == cur->h_function_pointer &&
344                                 EventType == cur->eventtype) {
345                         lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
346                                         EventType);
347                         p = cur->next;
348                         if (cur == UserHookTable) {
349                                 UserHookTable = p;
350                         }
351                         free(cur);
352                         cur = p;
353                 }
354         }
355 }
356
357
358 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
359                                 int EventType)
360 {
361
362         struct MessageFunctionHook *newfcn;
363
364         newfcn = (struct MessageFunctionHook *)
365             malloc(sizeof(struct MessageFunctionHook));
366         newfcn->next = MessageHookTable;
367         newfcn->h_function_pointer = handler;
368         newfcn->eventtype = EventType;
369         MessageHookTable = newfcn;
370
371         lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
372                 EventType);
373 }
374
375
376 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
377                 int EventType)
378 {
379         struct MessageFunctionHook *cur, *p;
380
381         for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
382                 /* This will also remove duplicates if any */
383                 while (cur != NULL &&
384                                 handler == cur->h_function_pointer &&
385                                 EventType == cur->eventtype) {
386                         lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
387                                         EventType);
388                         p = cur->next;
389                         if (cur == MessageHookTable) {
390                                 MessageHookTable = p;
391                         }
392                         free(cur);
393                         cur = p;
394                 }
395         }
396 }
397
398
399 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
400 {
401         struct NetprocFunctionHook *newfcn;
402
403         newfcn = (struct NetprocFunctionHook *)
404             malloc(sizeof(struct NetprocFunctionHook));
405         newfcn->next = NetprocHookTable;
406         newfcn->h_function_pointer = handler;
407         NetprocHookTable = newfcn;
408
409         lprintf(CTDL_INFO, "Registered a new netproc function\n");
410 }
411
412
413 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
414 {
415         struct NetprocFunctionHook *cur, *p;
416
417         for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
418                 /* This will also remove duplicates if any */
419                 while (cur != NULL &&
420                                 handler == cur->h_function_pointer ) {
421                         lprintf(CTDL_INFO, "Unregistered netproc function\n");
422                         p = cur->next;
423                         if (cur == NetprocHookTable) {
424                                 NetprocHookTable = p;
425                         }
426                         free(cur);
427                         cur = p;
428                 }
429         }
430 }
431
432
433 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
434 {
435         struct DeleteFunctionHook *newfcn;
436
437         newfcn = (struct DeleteFunctionHook *)
438             malloc(sizeof(struct DeleteFunctionHook));
439         newfcn->next = DeleteHookTable;
440         newfcn->h_function_pointer = handler;
441         DeleteHookTable = newfcn;
442
443         lprintf(CTDL_INFO, "Registered a new netproc function\n");
444 }
445
446
447 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
448 {
449         struct DeleteFunctionHook *cur, *p;
450
451         for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
452                 /* This will also remove duplicates if any */
453                 while (cur != NULL &&
454                                 handler == cur->h_function_pointer ) {
455                         lprintf(CTDL_INFO, "Unregistered netproc function\n");
456                         p = cur->next;
457                         if (cur == DeleteHookTable) {
458                                 DeleteHookTable = p;
459                         }
460                         free(cur);
461                         cur = p;
462                 }
463         }
464 }
465
466
467
468
469 void CtdlRegisterFixedOutputHook(char *content_type, void (*handler)(char *, int) )
470 {
471         struct FixedOutputHook *newfcn;
472
473         newfcn = (struct FixedOutputHook *)
474             malloc(sizeof(struct FixedOutputHook));
475         newfcn->next = FixedOutputTable;
476         newfcn->h_function_pointer = handler;
477         safestrncpy(newfcn->content_type, content_type, sizeof newfcn->content_type);
478         FixedOutputTable = newfcn;
479
480         lprintf(CTDL_INFO, "Registered a new fixed output function for %s\n", newfcn->content_type);
481 }
482
483
484 void CtdlUnregisterFixedOutputHook(char *content_type)
485 {
486         struct FixedOutputHook *cur, *p;
487
488         for (cur = FixedOutputTable; cur != NULL; cur = cur->next) {
489                 /* This will also remove duplicates if any */
490                 while (cur != NULL && (!strcasecmp(content_type, cur->content_type))) {
491                         lprintf(CTDL_INFO, "Unregistered fixed output function for %s\n", content_type);
492                         p = cur->next;
493                         if (cur == FixedOutputTable) {
494                                 FixedOutputTable = p;
495                         }
496                         free(cur);
497                         cur = p;
498                 }
499         }
500 }
501
502 /* returns nonzero if we found a hook and used it */
503 int PerformFixedOutputHooks(char *content_type, char *content, int content_length)
504 {
505         struct FixedOutputHook *fcn;
506
507         for (fcn = FixedOutputTable; fcn != NULL; fcn = fcn->next) {
508                 if (!strcasecmp(content_type, fcn->content_type)) {
509                         (*fcn->h_function_pointer) (content, content_length);
510                         return(1);
511                 }
512         }
513         return(0);
514 }
515
516
517
518
519
520 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
521 {
522
523         struct XmsgFunctionHook *newfcn;
524
525         newfcn = (struct XmsgFunctionHook *)
526             malloc(sizeof(struct XmsgFunctionHook));
527         newfcn->next = XmsgHookTable;
528         newfcn->order = order;
529         newfcn->h_function_pointer = fcn_ptr;
530         XmsgHookTable = newfcn;
531         lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
532 }
533
534
535 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
536 {
537         struct XmsgFunctionHook *cur, *p;
538
539         for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
540                 /* This will also remove duplicates if any */
541                 while (cur != NULL &&
542                                 fcn_ptr == cur->h_function_pointer &&
543                                 order == cur->order) {
544                         lprintf(CTDL_INFO, "Unregistered x-msg function "
545                                         "(priority %d)\n", order);
546                         p = cur->next;
547                         if (cur == XmsgHookTable) {
548                                 XmsgHookTable = p;
549                         }
550                         free(cur);
551                         cur = p;
552                 }
553         }
554 }
555
556
557 void CtdlRegisterServiceHook(int tcp_port,
558                         char *sockpath,
559                         void (*h_greeting_function) (void),
560                         void (*h_command_function) (void),
561                         void (*h_async_function) (void)
562                         )
563 {
564         struct ServiceFunctionHook *newfcn;
565         char *message;
566         char *error;
567
568         newfcn = (struct ServiceFunctionHook *)
569             malloc(sizeof(struct ServiceFunctionHook));
570         message = (char*) malloc (SIZ);
571         
572         newfcn->next = ServiceHookTable;
573         newfcn->tcp_port = tcp_port;
574         newfcn->sockpath = sockpath;
575         newfcn->h_greeting_function = h_greeting_function;
576         newfcn->h_command_function = h_command_function;
577         newfcn->h_async_function = h_async_function;
578
579         if (sockpath != NULL) {
580                 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions, &error);
581                 snprintf(message, SIZ, "Unix domain socket '%s': ", sockpath);
582         }
583         else if (tcp_port <= 0) {       /* port -1 to disable */
584                 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
585                 free(newfcn);
586                 return;
587         }
588         else {
589                 newfcn->msock = ig_tcp_server(config.c_ip_addr,
590                                               tcp_port,
591                                               config.c_maxsessions, 
592                                               &error);
593                 snprintf(message, SIZ, "TCP port %d: ", tcp_port);
594         }
595
596         if (newfcn->msock > 0) {
597                 ServiceHookTable = newfcn;
598                 strcat(message, "registered.");
599                 lprintf(CTDL_INFO, "%s\n", message);
600         }
601         else {
602                 AddPortError(message, error);
603                 strcat(message, "FAILED.");
604                 lprintf(CTDL_CRIT, "%s\n", message);
605                 free(error);
606                 free(newfcn);
607         }
608         free(message);
609 }
610
611
612 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
613                         void (*h_greeting_function) (void),
614                         void (*h_command_function) (void),
615                         void (*h_async_function) (void)
616                         )
617 {
618         struct ServiceFunctionHook *cur, *p;
619
620         for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
621                 /* This will also remove duplicates if any */
622                 while (cur != NULL &&
623                                 !(sockpath && cur->sockpath &&
624                                         strcmp(sockpath, cur->sockpath)) &&
625                                 h_greeting_function == cur->h_greeting_function &&
626                                 h_command_function == cur->h_command_function &&
627                                 h_async_function == cur->h_async_function &&
628                                 tcp_port == cur->tcp_port) {
629                         close(cur->msock);
630                         if (sockpath) {
631                                 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
632                                                 sockpath);
633                         } else if (tcp_port) {
634                                 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
635                         } else {
636                                 lprintf(CTDL_INFO, "Unregistered unknown service\n");
637                         }
638                         p = cur->next;
639                         if (cur == ServiceHookTable) {
640                                 ServiceHookTable = p;
641                         }
642                         free(cur);
643                         cur = p;
644                 }
645         }
646 }
647
648
649 void PerformSessionHooks(int EventType)
650 {
651         struct SessionFunctionHook *fcn;
652
653         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
654                 if (fcn->eventtype == EventType) {
655                         (*fcn->h_function_pointer) ();
656                 }
657         }
658 }
659
660 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
661 {
662         struct UserFunctionHook *fcn;
663
664         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
665                 if (fcn->eventtype == EventType) {
666                         (*fcn->h_function_pointer) (usbuf);
667                 }
668         }
669 }
670
671 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
672 {
673         struct MessageFunctionHook *fcn;
674         int total_retval = 0;
675
676         /* Other code may elect to protect this message from server-side
677          * handlers; if this is the case, don't do anything.
678         lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
679                 EventType, msg->cm_flags);
680          */
681         if (msg->cm_flags & CM_SKIP_HOOKS) {
682                 lprintf(CTDL_DEBUG, "Skipping hooks\n");
683                 return(0);
684         }
685
686         /* Otherwise, run all the hooks appropriate to this event type.
687          */
688         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
689                 if (fcn->eventtype == EventType) {
690                         total_retval = total_retval +
691                                 (*fcn->h_function_pointer) (msg);
692                 }
693         }
694
695         /* Return the sum of the return codes from the hook functions.  If
696          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
697          * the save operation to abort.
698          */
699         return total_retval;
700 }
701
702
703
704 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
705 {
706         struct NetprocFunctionHook *fcn;
707         int total_retval = 0;
708
709         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
710                 total_retval = total_retval +
711                         (*fcn->h_function_pointer) (msg, target_room);
712         }
713
714         /* Return the sum of the return codes from the hook functions.
715          * A nonzero return code will cause the message to *not* be imported.
716          */
717         return total_retval;
718 }
719
720
721 void PerformDeleteHooks(char *room, long msgnum)
722 {
723         struct DeleteFunctionHook *fcn;
724
725         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
726                 (*fcn->h_function_pointer) (room, msgnum);
727         }
728 }
729
730
731
732
733
734 int PerformXmsgHooks(char *sender, char *recp, char *msg)
735 {
736         struct XmsgFunctionHook *fcn;
737         int total_sent = 0;
738         int p;
739
740         for (p=0; p<MAX_XMSG_PRI; ++p) {
741                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
742                         if (fcn->order == p) {
743                                 total_sent +=
744                                         (*fcn->h_function_pointer)
745                                                 (sender, recp, msg);
746                         }
747                 }
748                 /* Break out of the loop if a higher-priority function
749                  * successfully delivered the message.  This prevents duplicate
750                  * deliveries to local users simultaneously signed onto
751                  * remote services.
752                  */
753                 if (total_sent) break;
754         }
755         return total_sent;
756 }