Write lastsent back to netconfig after sending list messages
[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 // data passed back and forth between listdeliver_do_msg() and listdeliver_sweep_room()
47 struct lddata {
48         long msgnum;            // number of most recent message processed
49         char *netconf;          // netconfig for this room (contains the recipients)
50 };
51
52
53
54 void listdeliver_do_msg(long msgnum, void *userdata) {
55         struct lddata *ld = (struct lddata *) userdata;
56         if (!ld) return;
57         char buf[SIZ];
58
59         ld->msgnum = msgnum;
60         if (msgnum <= 0) return;
61
62         struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
63         if (!TheMessage) return;
64
65
66
67         // FIXME munge the headers so it looks like it came from a mailing list
68
69
70
71         char *recipients = malloc(strlen(ld->netconf));
72         if (recipients) {
73                 recipients[0] = 0;
74
75                 int config_lines = num_tokens(ld->netconf, '\n');
76                 for (int i=0; i<config_lines; ++i) {
77                         extract_token(buf, ld->netconf, i, '\n', sizeof buf);
78                         if (!strncasecmp(buf, "listrecp|", 9)) {
79                                 if (recipients[0] != 0) {
80                                         strcat(recipients, ",");
81                                 }
82                                 strcat(recipients, &buf[9]);
83                         }
84                         if (!strncasecmp(buf, "digestrecp|", 11)) {
85                                 if (recipients[0] != 0) {
86                                         strcat(recipients, ",");
87                                 }
88                                 strcat(recipients, &buf[11]);
89                         }
90                 }
91                 syslog(LOG_DEBUG, "\033[33m%s\033[0m", recipients);
92                 struct recptypes *valid = validate_recipients(recipients, NULL, 0);
93                 if (valid) {
94                         long new_msgnum = CtdlSubmitMsg(TheMessage, valid, "");
95                         free_recipients(valid);
96                 }
97         }
98         CM_Free(TheMessage);
99 }
100
101
102 /*
103  * Sweep through one room looking for mailing list deliveries to do
104  */
105 void listdeliver_sweep_room(char *roomname) {
106         char *netconfig = NULL;
107         char *newnetconfig = NULL;
108         long lastsent = 0;
109         char buf[SIZ];
110         int config_lines;
111         int i;
112         int number_of_messages_processed = 0;
113         int number_of_recipients = 0;
114         struct lddata ld;
115
116         if (CtdlGetRoom(&CC->room, roomname)) {
117                 syslog(LOG_DEBUG, "listdeliver: no room <%s>", roomname);
118                 return;
119         }
120
121         netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
122         if (!netconfig) {
123                 return;                         // no netconfig, no processing, no problem
124         }
125
126         syslog(LOG_DEBUG, "listdeliver: sweeping %s", roomname);
127
128         config_lines = num_tokens(netconfig, '\n');
129         for (i=0; i<config_lines; ++i) {
130                 extract_token(buf, netconfig, i, '\n', sizeof buf);
131
132                 if (!strncasecmp(buf, "lastsent|", 9)) {
133                         lastsent = atol(&buf[9]);
134                 }
135                 else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
136                         ++number_of_recipients;
137                 }
138         }
139
140         if (number_of_recipients > 0) {
141                 syslog(LOG_DEBUG, "listdeliver: processing new messages in <%s> for <%d> recipients", CC->room.QRname, number_of_recipients);
142                 ld.netconf = netconfig;
143                 number_of_messages_processed = CtdlForEachMessage(MSGS_GT, lastsent, NULL, NULL, NULL, listdeliver_do_msg, &ld);
144                 syslog(LOG_DEBUG, "listdeliver: processed %d messages", number_of_messages_processed);
145         
146                 if (number_of_messages_processed > 0) {
147                         syslog(LOG_DEBUG, "listdeliver: new lastsent is %ld", ld.msgnum);
148
149                         // Update this room's netconfig with the updated lastsent
150                         netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
151                         if (!netconfig) {
152                                 netconfig = strdup("");
153                         }
154
155                         // The new netconfig begins with the new lastsent directive
156                         newnetconfig = malloc(strlen(netconfig) + 1024);
157                         sprintf(newnetconfig, "lastsent|%ld\n", ld.msgnum);
158
159                         // And then we append all of the old netconfig, minus the old lastsent.  Also omit blank lines.
160                         config_lines = num_tokens(netconfig, '\n');
161                         for (i=0; i<config_lines; ++i) {
162                                 extract_token(buf, netconfig, i, '\n', sizeof buf);
163                                 if ( (!IsEmptyStr(buf)) && (strncasecmp(buf, "lastsent|", 9)) ) {
164                                         sprintf(&newnetconfig[strlen(newnetconfig)], "%s\n", buf);
165                                 }
166                         }
167
168                         // Write the new netconfig back to disk
169                         SaveRoomNetConfigFile(CC->room.QRnumber, newnetconfig);
170                         free(newnetconfig);
171                 }
172         }
173         free(netconfig);
174 }
175
176
177 /*
178  * Callback for listdeliver_sweep()
179  * Adds one room to the queue
180  */
181 void listdeliver_queue_room(struct ctdlroom *qrbuf, void *data) {
182         Array *roomlistarr = (Array *)data;
183         array_append(roomlistarr, qrbuf->QRname);
184 }
185
186
187 /*
188  * Queue up the list of rooms so we can sweep them for mailing list delivery instructions
189  */
190 void listdeliver_sweep(void) {
191         static time_t last_run = 0L;
192         int i = 0;
193
194         /*
195          * Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
196          */
197         if ( (time(NULL) - last_run) < 900 ) {
198                 return;
199         }
200
201         /*
202          * This is a simple concurrency check to make sure only one listdeliver
203          * run is done at a time.  We could do this with a mutex, but since we
204          * don't really require extremely fine granularity here, we'll do it
205          * with a static variable instead.
206          */
207         if (doing_listdeliver) return;
208         doing_listdeliver = 1;
209
210         /*
211          * Go through each room looking for mailing lists to process
212          */
213         syslog(LOG_DEBUG, "listdeliver: sweep started");
214
215         Array *roomlistarr = array_new(ROOMNAMELEN);                    // we have to queue them
216         CtdlForEachRoom(listdeliver_queue_room, roomlistarr);           // otherwise we get multiple cursors in progress
217
218         for (i=0; i<array_len(roomlistarr); ++i) {
219                 listdeliver_sweep_room((char *)array_get_element_at(roomlistarr, i));
220         }
221
222         array_free(roomlistarr);
223         syslog(LOG_DEBUG, "listdeliver: ended");
224         last_run = time(NULL);
225         doing_listdeliver = 0;
226
227         //exit(0);
228 }
229
230
231
232 /*
233  * Module entry point
234  */
235 CTDL_MODULE_INIT(listdeliver)
236 {
237         if (!threading)
238         {
239                 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
240         }
241         
242         /* return our module name for the log */
243         return "listsub";
244 }