c36a0fb5340e7074aa7049242b7a270fbdd38fce
[citadel.git] / citadel / modules / listdeliver / serv_listdeliver.c
1 /*
2  * This module delivers messages to mailing lists.
3  *
4  * Copyright (c) 2002-2021 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *  
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <ctype.h>
21 #include <signal.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <dirent.h>
26 #include <time.h>
27 #include <sys/wait.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <libcitadel.h>
31 #include "citadel.h"
32 #include "server.h"
33 #include "citserver.h"
34 #include "support.h"
35 #include "config.h"
36 #include "user_ops.h"
37 #include "database.h"
38 #include "msgbase.h"
39 #include "internet_addressing.h"
40 #include "clientsocket.h"
41 #include "ctdl_module.h"
42
43 int doing_listdeliver = 0;
44
45
46
47 void listdeliver_sweep_room(struct ctdlroom *qrbuf, void *data) {
48         char *serialized_config = NULL;
49         long lastsent = 0;
50         char buf[SIZ];
51         int config_lines;
52         int i;
53
54         serialized_config = LoadRoomNetConfigFile(qrbuf->QRnumber);
55         if (!serialized_config) {
56                 syslog(LOG_DEBUG, "\033[31m %s has no netconfig \033[0m", qrbuf->QRname);
57                 return;
58         }
59
60         syslog(LOG_DEBUG, "\033[32m %s has a netconfig \033[0m", qrbuf->QRname);
61
62         config_lines = num_tokens(serialized_config, '\n');
63         for (i=0; i<config_lines; ++i) {
64                 extract_token(buf, serialized_config, i, '\n', sizeof buf);
65
66                 if (!strncasecmp(buf, "lastsent|", 9)) {
67                         lastsent = atol(buf[9]);
68                         syslog(LOG_DEBUG, "listdeliver: last message delivered = %ld", lastsent);
69                 }
70
71
72
73
74                 syslog(LOG_DEBUG, "%s", buf);
75         }
76
77
78         free(serialized_config);
79 }
80
81
82
83 void listdeliver_sweep(void) {
84         static time_t last_run = 0L;
85
86         /*
87          * Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
88          */
89         if ( (time(NULL) - last_run) < 900 ) {
90                 return;
91         }
92
93         /*
94          * This is a simple concurrency check to make sure only one listdeliver
95          * run is done at a time.  We could do this with a mutex, but since we
96          * don't really require extremely fine granularity here, we'll do it
97          * with a static variable instead.
98          */
99         if (doing_listdeliver) return;
100         doing_listdeliver = 1;
101
102         /*
103          * Go through each room looking for mailing lists to process
104          */
105         syslog(LOG_DEBUG, "listdeliver: sweep started");
106         CtdlForEachRoom(listdeliver_sweep_room, NULL);
107         syslog(LOG_DEBUG, "listdeliver: ended");
108         last_run = time(NULL);
109         doing_listdeliver = 0;
110 }
111
112
113
114 /*
115  * Module entry point
116  */
117 CTDL_MODULE_INIT(listdeliver)
118 {
119         if (!threading)
120         {
121                 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
122         }
123         
124         /* return our module name for the log */
125         return "listsub";
126 }