* The scheduler can now "wake up" a session to deliver async 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 LogFunctionHook *LogHookTable = NULL;
32 struct CleanupFunctionHook *CleanupHookTable = NULL;
33 struct SessionFunctionHook *SessionHookTable = NULL;
34 struct UserFunctionHook *UserHookTable = NULL;
35 struct XmsgFunctionHook *XmsgHookTable = NULL;
36 struct MessageFunctionHook *MessageHookTable = NULL;
37 struct NetprocFunctionHook *NetprocHookTable = NULL;
38 struct DeleteFunctionHook *DeleteHookTable = NULL;
39 struct ServiceFunctionHook *ServiceHookTable = 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 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
49 {
50         struct ProtoFunctionHook *p;
51
52         p = (struct ProtoFunctionHook *)
53                 malloc(sizeof(struct ProtoFunctionHook));
54
55         if (p == NULL) {
56                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
57                 exit(EXIT_FAILURE);
58         }
59         p->handler = handler;
60         p->cmd = cmd;
61         p->desc = desc;
62         p->next = ProtoHookList;
63         ProtoHookList = p;
64         lprintf(CTDL_INFO, "Registered server command %s (%s)\n", cmd, desc);
65 }
66
67
68 void CtdlUnregisterProtoHook(void (*handler) (char *), char *cmd)
69 {
70         struct ProtoFunctionHook *cur, *p;
71
72         for (cur = ProtoHookList; cur != NULL; cur = cur->next) {
73                 /* This will also remove duplicates if any */
74                 while (cur != NULL &&
75                                 handler == cur->handler &&
76                                 !strcmp(cmd, cur->cmd)) {
77                         lprintf(CTDL_INFO, "Unregistered server command %s (%s)\n",
78                                         cmd, cur->desc);
79                         p = cur->next;
80                         if (cur == ProtoHookList) {
81                                 ProtoHookList = p;
82                         }
83                         free(cur);
84                         cur = p;
85                 }
86         }
87 }
88
89
90 int DLoader_Exec_Cmd(char *cmdbuf)
91 {
92         struct ProtoFunctionHook *p;
93
94         for (p = ProtoHookList; p; p = p->next) {
95                 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
96                         p->handler(&cmdbuf[5]);
97                         return 1;
98                 }
99         }
100         return 0;
101 }
102
103 void initialize_server_extensions(void)
104 {
105         serv_bio_init();
106         serv_calendar_init();
107         serv_ldap_init();
108         serv_chat_init();
109         serv_expire_init();
110         serv_imap_init();
111         serv_inetcfg_init();
112         serv_listsub_init();
113         serv_mrtg_init();
114         serv_netfilter_init();
115         serv_network_init();
116         serv_newuser_init();
117         serv_pas2_init();
118         serv_pop3_init();
119         serv_rwho_init();
120         serv_smtp_init();
121         serv_spam_init();
122         /* serv_test_init(); */
123         serv_upgrade_init();
124         serv_vandelay_init();
125         serv_vcard_init();
126 }
127
128
129
130 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
131 {
132
133         struct LogFunctionHook *newfcn;
134
135         newfcn = (struct LogFunctionHook *)
136             malloc(sizeof(struct LogFunctionHook));
137         newfcn->next = LogHookTable;
138         newfcn->h_function_pointer = fcn_ptr;
139         newfcn->loglevel = loglevel;
140         LogHookTable = newfcn;
141
142         lprintf(CTDL_INFO, "Registered a new logging function\n");
143 }
144
145
146 void CtdlUnregisterLogHook(void (*fcn_ptr) (char *), int loglevel)
147 {
148         struct LogFunctionHook *cur, *p;
149
150         for (cur = LogHookTable; cur != NULL; cur = cur->next) {
151                 /* This will also remove duplicates if any */
152                 while (cur != NULL &&
153                                 fcn_ptr == cur->h_function_pointer &&
154                                 loglevel == cur->loglevel) {
155                         lprintf(CTDL_INFO, "Unregistered logging function\n");
156                         p = cur->next;
157                         if (cur == LogHookTable) {
158                                 LogHookTable = p;
159                         }
160                         free(cur);
161                         cur = p;
162                 }
163         }
164 }
165
166
167 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
168 {
169
170         struct CleanupFunctionHook *newfcn;
171
172         newfcn = (struct CleanupFunctionHook *)
173             malloc(sizeof(struct CleanupFunctionHook));
174         newfcn->next = CleanupHookTable;
175         newfcn->h_function_pointer = fcn_ptr;
176         CleanupHookTable = newfcn;
177
178         lprintf(CTDL_INFO, "Registered a new cleanup function\n");
179 }
180
181
182 void CtdlUnregisterCleanupHook(void (*fcn_ptr) (void))
183 {
184         struct CleanupFunctionHook *cur, *p;
185
186         for (cur = CleanupHookTable; cur != NULL; cur = cur->next) {
187                 /* This will also remove duplicates if any */
188                 while (cur != NULL &&
189                                 fcn_ptr == cur->h_function_pointer) {
190                         lprintf(CTDL_INFO, "Unregistered cleanup function\n");
191                         p = cur->next;
192                         if (cur == CleanupHookTable) {
193                                 CleanupHookTable = p;
194                         }
195                         free(cur);
196                         cur = p;
197                 }
198         }
199 }
200
201
202 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
203 {
204
205         struct SessionFunctionHook *newfcn;
206
207         newfcn = (struct SessionFunctionHook *)
208             malloc(sizeof(struct SessionFunctionHook));
209         newfcn->next = SessionHookTable;
210         newfcn->h_function_pointer = fcn_ptr;
211         newfcn->eventtype = EventType;
212         SessionHookTable = newfcn;
213
214         lprintf(CTDL_INFO, "Registered a new session function (type %d)\n",
215                 EventType);
216 }
217
218
219 void CtdlUnregisterSessionHook(void (*fcn_ptr) (void), int EventType)
220 {
221         struct SessionFunctionHook *cur, *p;
222
223         for (cur = SessionHookTable; cur != NULL; cur = cur->next) {
224                 /* This will also remove duplicates if any */
225                 while (cur != NULL &&
226                                 fcn_ptr == cur->h_function_pointer &&
227                                 EventType == cur->eventtype) {
228                         lprintf(CTDL_INFO, "Unregistered session function (type %d)\n",
229                                         EventType);
230                         p = cur->next;
231                         if (cur == SessionHookTable) {
232                                 SessionHookTable = p;
233                         }
234                         free(cur);
235                         cur = p;
236                 }
237         }
238 }
239
240
241 void CtdlRegisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
242 {
243
244         struct UserFunctionHook *newfcn;
245
246         newfcn = (struct UserFunctionHook *)
247             malloc(sizeof(struct UserFunctionHook));
248         newfcn->next = UserHookTable;
249         newfcn->h_function_pointer = fcn_ptr;
250         newfcn->eventtype = EventType;
251         UserHookTable = newfcn;
252
253         lprintf(CTDL_INFO, "Registered a new user function (type %d)\n",
254                 EventType);
255 }
256
257
258 void CtdlUnregisterUserHook(void (*fcn_ptr) (struct ctdluser *), int EventType)
259 {
260         struct UserFunctionHook *cur, *p;
261
262         for (cur = UserHookTable; cur != NULL; cur = cur->next) {
263                 /* This will also remove duplicates if any */
264                 while (cur != NULL &&
265                                 fcn_ptr == cur->h_function_pointer &&
266                                 EventType == cur->eventtype) {
267                         lprintf(CTDL_INFO, "Unregistered user function (type %d)\n",
268                                         EventType);
269                         p = cur->next;
270                         if (cur == UserHookTable) {
271                                 UserHookTable = p;
272                         }
273                         free(cur);
274                         cur = p;
275                 }
276         }
277 }
278
279
280 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
281                                 int EventType)
282 {
283
284         struct MessageFunctionHook *newfcn;
285
286         newfcn = (struct MessageFunctionHook *)
287             malloc(sizeof(struct MessageFunctionHook));
288         newfcn->next = MessageHookTable;
289         newfcn->h_function_pointer = handler;
290         newfcn->eventtype = EventType;
291         MessageHookTable = newfcn;
292
293         lprintf(CTDL_INFO, "Registered a new message function (type %d)\n",
294                 EventType);
295 }
296
297
298 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
299                 int EventType)
300 {
301         struct MessageFunctionHook *cur, *p;
302
303         for (cur = MessageHookTable; cur != NULL; cur = cur->next) {
304                 /* This will also remove duplicates if any */
305                 while (cur != NULL &&
306                                 handler == cur->h_function_pointer &&
307                                 EventType == cur->eventtype) {
308                         lprintf(CTDL_INFO, "Unregistered message function (type %d)\n",
309                                         EventType);
310                         p = cur->next;
311                         if (cur == MessageHookTable) {
312                                 MessageHookTable = p;
313                         }
314                         free(cur);
315                         cur = p;
316                 }
317         }
318 }
319
320
321 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
322 {
323         struct NetprocFunctionHook *newfcn;
324
325         newfcn = (struct NetprocFunctionHook *)
326             malloc(sizeof(struct NetprocFunctionHook));
327         newfcn->next = NetprocHookTable;
328         newfcn->h_function_pointer = handler;
329         NetprocHookTable = newfcn;
330
331         lprintf(CTDL_INFO, "Registered a new netproc function\n");
332 }
333
334
335 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) )
336 {
337         struct NetprocFunctionHook *cur, *p;
338
339         for (cur = NetprocHookTable; cur != NULL; cur = cur->next) {
340                 /* This will also remove duplicates if any */
341                 while (cur != NULL &&
342                                 handler == cur->h_function_pointer ) {
343                         lprintf(CTDL_INFO, "Unregistered netproc function\n");
344                         p = cur->next;
345                         if (cur == NetprocHookTable) {
346                                 NetprocHookTable = p;
347                         }
348                         free(cur);
349                         cur = p;
350                 }
351         }
352 }
353
354
355 void CtdlRegisterDeleteHook(void (*handler)(char *, long) )
356 {
357         struct DeleteFunctionHook *newfcn;
358
359         newfcn = (struct DeleteFunctionHook *)
360             malloc(sizeof(struct DeleteFunctionHook));
361         newfcn->next = DeleteHookTable;
362         newfcn->h_function_pointer = handler;
363         DeleteHookTable = newfcn;
364
365         lprintf(CTDL_INFO, "Registered a new netproc function\n");
366 }
367
368
369 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) )
370 {
371         struct DeleteFunctionHook *cur, *p;
372
373         for (cur = DeleteHookTable; cur != NULL; cur = cur->next) {
374                 /* This will also remove duplicates if any */
375                 while (cur != NULL &&
376                                 handler == cur->h_function_pointer ) {
377                         lprintf(CTDL_INFO, "Unregistered netproc function\n");
378                         p = cur->next;
379                         if (cur == DeleteHookTable) {
380                                 DeleteHookTable = p;
381                         }
382                         free(cur);
383                         cur = p;
384                 }
385         }
386 }
387
388
389 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
390 {
391
392         struct XmsgFunctionHook *newfcn;
393
394         newfcn = (struct XmsgFunctionHook *)
395             malloc(sizeof(struct XmsgFunctionHook));
396         newfcn->next = XmsgHookTable;
397         newfcn->order = order;
398         newfcn->h_function_pointer = fcn_ptr;
399         XmsgHookTable = newfcn;
400         lprintf(CTDL_INFO, "Registered a new x-msg function (priority %d)\n", order);
401 }
402
403
404 void CtdlUnregisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
405 {
406         struct XmsgFunctionHook *cur, *p;
407
408         for (cur = XmsgHookTable; cur != NULL; cur = cur->next) {
409                 /* This will also remove duplicates if any */
410                 while (cur != NULL &&
411                                 fcn_ptr == cur->h_function_pointer &&
412                                 order == cur->order) {
413                         lprintf(CTDL_INFO, "Unregistered x-msg function "
414                                         "(priority %d)\n", order);
415                         p = cur->next;
416                         if (cur == XmsgHookTable) {
417                                 XmsgHookTable = p;
418                         }
419                         free(cur);
420                         cur = p;
421                 }
422         }
423 }
424
425
426 void CtdlRegisterServiceHook(int tcp_port,
427                         char *sockpath,
428                         void (*h_greeting_function) (void),
429                         void (*h_command_function) (void),
430                         void (*h_async_function) (void)
431                         )
432 {
433         struct ServiceFunctionHook *newfcn;
434         char message[SIZ];
435
436         newfcn = (struct ServiceFunctionHook *)
437             malloc(sizeof(struct ServiceFunctionHook));
438         newfcn->next = ServiceHookTable;
439         newfcn->tcp_port = tcp_port;
440         newfcn->sockpath = sockpath;
441         newfcn->h_greeting_function = h_greeting_function;
442         newfcn->h_command_function = h_command_function;
443         newfcn->h_async_function = h_async_function;
444
445         if (sockpath != NULL) {
446                 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
447                 snprintf(message, sizeof message, "Unix domain socket '%s': ", sockpath);
448         }
449         else if (tcp_port <= 0) {       /* port -1 to disable */
450                 lprintf(CTDL_INFO, "Service has been manually disabled, skipping\n");
451                 free(newfcn);
452                 return;
453         }
454         else {
455                 newfcn->msock = ig_tcp_server(tcp_port, config.c_maxsessions);
456                 snprintf(message, sizeof message, "TCP port %d: ", tcp_port);
457         }
458
459         if (newfcn->msock > 0) {
460                 ServiceHookTable = newfcn;
461                 strcat(message, "registered.");
462                 lprintf(CTDL_INFO, "%s\n", message);
463         }
464         else {
465                 strcat(message, "FAILED.");
466                 lprintf(CTDL_CRIT, "%s\n", message);
467                 free(newfcn);
468         }
469 }
470
471
472 void CtdlUnregisterServiceHook(int tcp_port, char *sockpath,
473                         void (*h_greeting_function) (void),
474                         void (*h_command_function) (void),
475                         void (*h_async_function) (void)
476                         )
477 {
478         struct ServiceFunctionHook *cur, *p;
479
480         for (cur = ServiceHookTable; cur != NULL; cur = cur->next) {
481                 /* This will also remove duplicates if any */
482                 while (cur != NULL &&
483                                 !(sockpath && cur->sockpath &&
484                                         strcmp(sockpath, cur->sockpath)) &&
485                                 h_greeting_function == cur->h_greeting_function &&
486                                 h_command_function == cur->h_command_function &&
487                                 h_async_function == cur->h_async_function &&
488                                 tcp_port == cur->tcp_port) {
489                         close(cur->msock);
490                         if (sockpath) {
491                                 lprintf(CTDL_INFO, "Closed UNIX domain socket %s\n",
492                                                 sockpath);
493                         } else if (tcp_port) {
494                                 lprintf(CTDL_INFO, "Closed TCP port %d\n", tcp_port);
495                         } else {
496                                 lprintf(CTDL_INFO, "Unregistered unknown service\n");
497                         }
498                         p = cur->next;
499                         if (cur == ServiceHookTable) {
500                                 ServiceHookTable = p;
501                         }
502                         free(cur);
503                         cur = p;
504                 }
505         }
506 }
507
508
509 void PerformSessionHooks(int EventType)
510 {
511         struct SessionFunctionHook *fcn;
512
513         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
514                 if (fcn->eventtype == EventType) {
515                         (*fcn->h_function_pointer) ();
516                 }
517         }
518 }
519
520 void PerformLogHooks(int loglevel, char *logmsg)
521 {
522         struct LogFunctionHook *fcn;
523
524         for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
525                 if (fcn->loglevel >= loglevel) {
526                         (*fcn->h_function_pointer) (logmsg);
527                 }
528         }
529 }
530
531 void PerformUserHooks(struct ctdluser *usbuf, int EventType)
532 {
533         struct UserFunctionHook *fcn;
534
535         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
536                 if (fcn->eventtype == EventType) {
537                         (*fcn->h_function_pointer) (usbuf);
538                 }
539         }
540 }
541
542 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
543 {
544         struct MessageFunctionHook *fcn;
545         int total_retval = 0;
546
547         /* Other code may elect to protect this message from server-side
548          * handlers; if this is the case, don't do anything.
549         lprintf(CTDL_DEBUG, "** Event type is %d, flags are %d\n",
550                 EventType, msg->cm_flags);
551          */
552         if (msg->cm_flags & CM_SKIP_HOOKS) {
553                 lprintf(CTDL_DEBUG, "Skipping hooks\n");
554                 return(0);
555         }
556
557         /* Otherwise, run all the hooks appropriate to this event type.
558          */
559         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
560                 if (fcn->eventtype == EventType) {
561                         total_retval = total_retval +
562                                 (*fcn->h_function_pointer) (msg);
563                 }
564         }
565
566         /* Return the sum of the return codes from the hook functions.  If
567          * this is an EVT_BEFORESAVE event, a nonzero return code will cause
568          * the save operation to abort.
569          */
570         return total_retval;
571 }
572
573
574
575 int PerformNetprocHooks(struct CtdlMessage *msg, char *target_room)
576 {
577         struct NetprocFunctionHook *fcn;
578         int total_retval = 0;
579
580         for (fcn = NetprocHookTable; fcn != NULL; fcn = fcn->next) {
581                 total_retval = total_retval +
582                         (*fcn->h_function_pointer) (msg, target_room);
583         }
584
585         /* Return the sum of the return codes from the hook functions.
586          * A nonzero return code will cause the message to *not* be imported.
587          */
588         return total_retval;
589 }
590
591
592 void PerformDeleteHooks(char *room, long msgnum)
593 {
594         struct DeleteFunctionHook *fcn;
595
596         for (fcn = DeleteHookTable; fcn != NULL; fcn = fcn->next) {
597                 (*fcn->h_function_pointer) (room, msgnum);
598         }
599 }
600
601
602
603 int PerformXmsgHooks(char *sender, char *recp, char *msg)
604 {
605         struct XmsgFunctionHook *fcn;
606         int total_sent = 0;
607         int p;
608
609         for (p=0; p<MAX_XMSG_PRI; ++p) {
610                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
611                         if (fcn->order == p) {
612                                 total_sent +=
613                                         (*fcn->h_function_pointer)
614                                                 (sender, recp, msg);
615                         }
616                 }
617                 /* Break out of the loop if a higher-priority function
618                  * successfully delivered the message.  This prevents duplicate
619                  * deliveries to local users simultaneously signed onto
620                  * remote services.
621                  */
622                 if (total_sent) break;
623         }
624         return total_sent;
625 }