]> code.citadel.org Git - citadel.git/blob - citadel/modules/listdeliver/serv_listdeliver.c
6cce15731b903ecdd45e14c7f098f0b08e3ae25a
[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         char *ch;
59         char bounce_to[256];
60         int i = 0;
61
62         ld->msgnum = msgnum;
63         if (msgnum <= 0) return;
64
65         struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
66         if (!TheMessage) return;
67
68         // If the subject line does not contain the name of the room, add it now.
69         if (!bmstrcasestr(TheMessage->cm_fields[eMsgSubject], CC->room.QRname)) {
70                 snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, TheMessage->cm_fields[eMsgSubject]);
71                 CM_SetField(TheMessage, eMsgSubject, buf, strlen(buf));
72         }
73
74         // From: should be set to the list address because doing otherwise makes DKIM parsers angry.
75         // Reply-to: should be set to the list address so that replies come to the list.
76         snprintf(buf, sizeof buf, "room_%s@%s", CC->room.QRname, CtdlGetConfigStr("c_fqdn"));
77         for (ch=buf; *ch; ++ch) {
78                 if (isspace(*ch)) *ch = '_';
79         }
80         CM_SetField(TheMessage, erFc822Addr, buf, strlen(buf));
81         CM_SetField(TheMessage, eReplyTo, buf, strlen(buf));
82
83         // With that out of the way, let's figure out who this message needs to be sent to.
84         char *recipients = malloc(strlen(ld->netconf));
85         if (recipients) {
86                 recipients[0] = 0;
87
88                 int config_lines = num_tokens(ld->netconf, '\n');
89                 for (i=0; i<config_lines; ++i) {
90                         extract_token(buf, ld->netconf, i, '\n', sizeof buf);
91                         if (!strncasecmp(buf, "listrecp|", 9)) {
92                                 if (recipients[0] != 0) {
93                                         strcat(recipients, ",");
94                                 }
95                                 strcat(recipients, &buf[9]);
96                         }
97                         if (!strncasecmp(buf, "digestrecp|", 11)) {
98                                 if (recipients[0] != 0) {
99                                         strcat(recipients, ",");
100                                 }
101                                 strcat(recipients, &buf[11]);
102                         }
103                 }
104
105                 // Where do we want bounces and other noise to be sent?  Certainly not to the list members!
106                 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", CtdlGetConfigStr("c_fqdn"));
107
108                 // Now submit the message
109                 struct recptypes *valid = validate_recipients(recipients, NULL, 0);
110                 if (valid) {
111                         valid->bounce_to = strdup(bounce_to);
112                         valid->envelope_from = strdup(bounce_to);
113                         CtdlSubmitMsg(TheMessage, valid, "");
114                         free_recipients(valid);
115                 }
116         }
117         CM_Free(TheMessage);
118 }
119
120
121 /*
122  * Sweep through one room looking for mailing list deliveries to do
123  */
124 void listdeliver_sweep_room(char *roomname) {
125         char *netconfig = NULL;
126         char *newnetconfig = NULL;
127         long lastsent = 0;
128         char buf[SIZ];
129         int config_lines;
130         int i;
131         int number_of_messages_processed = 0;
132         int number_of_recipients = 0;
133         struct lddata ld;
134
135         if (CtdlGetRoom(&CC->room, roomname)) {
136                 syslog(LOG_DEBUG, "listdeliver: no room <%s>", roomname);
137                 return;
138         }
139
140         netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
141         if (!netconfig) {
142                 return;                         // no netconfig, no processing, no problem
143         }
144
145         config_lines = num_tokens(netconfig, '\n');
146         for (i=0; i<config_lines; ++i) {
147                 extract_token(buf, netconfig, i, '\n', sizeof buf);
148
149                 if (!strncasecmp(buf, "lastsent|", 9)) {
150                         lastsent = atol(&buf[9]);
151                 }
152                 else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
153                         ++number_of_recipients;
154                 }
155         }
156
157         if (number_of_recipients > 0) {
158                 syslog(LOG_DEBUG, "listdeliver: processing new messages in <%s> for <%d> recipients", CC->room.QRname, number_of_recipients);
159                 ld.netconf = netconfig;
160                 number_of_messages_processed = CtdlForEachMessage(MSGS_GT, lastsent, NULL, NULL, NULL, listdeliver_do_msg, &ld);
161                 syslog(LOG_INFO, "listdeliver: processed <%d> messages in <%s> for <%d> recipients", number_of_messages_processed, CC->room.QRname, number_of_recipients);
162         
163                 if (number_of_messages_processed > 0) {
164                         syslog(LOG_DEBUG, "listdeliver: new lastsent is %ld", ld.msgnum);
165
166                         // Update this room's netconfig with the updated lastsent
167                         begin_critical_section(S_NETCONFIGS);
168                         netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
169                         if (!netconfig) {
170                                 netconfig = strdup("");
171                         }
172
173                         // The new netconfig begins with the new lastsent directive
174                         newnetconfig = malloc(strlen(netconfig) + 1024);
175                         sprintf(newnetconfig, "lastsent|%ld\n", ld.msgnum);
176
177                         // And then we append all of the old netconfig, minus the old lastsent.  Also omit blank lines.
178                         config_lines = num_tokens(netconfig, '\n');
179                         for (i=0; i<config_lines; ++i) {
180                                 extract_token(buf, netconfig, i, '\n', sizeof buf);
181                                 if ( (!IsEmptyStr(buf)) && (strncasecmp(buf, "lastsent|", 9)) ) {
182                                         sprintf(&newnetconfig[strlen(newnetconfig)], "%s\n", buf);
183                                 }
184                         }
185
186                         // Write the new netconfig back to disk
187                         SaveRoomNetConfigFile(CC->room.QRnumber, newnetconfig);
188                         end_critical_section(S_NETCONFIGS);
189                         free(newnetconfig);     // this was the new netconfig, free it because we're done with it
190                 }
191         }
192         free(netconfig);                        // this was the old netconfig, free it even if we didn't do anything
193 }
194
195
196 /*
197  * Callback for listdeliver_sweep()
198  * Adds one room to the queue
199  */
200 void listdeliver_queue_room(struct ctdlroom *qrbuf, void *data) {
201         Array *roomlistarr = (Array *)data;
202         array_append(roomlistarr, qrbuf->QRname);
203 }
204
205
206 /*
207  * Queue up the list of rooms so we can sweep them for mailing list delivery instructions
208  */
209 void listdeliver_sweep(void) {
210         static time_t last_run = 0L;
211         int i = 0;
212         time_t now = time(NULL);
213
214         /*
215          * Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
216          */
217         if ( (now - last_run) < 900 ) {
218                 syslog(LOG_DEBUG,
219                         "listdeliver: delivery interval not yet reached; last run was %ldm%lds ago",
220                         ((now - last_run) / 60),
221                         ((now - last_run) % 60)
222                 );
223                 return;
224         }
225
226         /*
227          * This is a simple concurrency check to make sure only one listdeliver
228          * run is done at a time.  We could do this with a mutex, but since we
229          * don't really require extremely fine granularity here, we'll do it
230          * with a static variable instead.
231          */
232         if (doing_listdeliver) return;
233         doing_listdeliver = 1;
234
235         /*
236          * Go through each room looking for mailing lists to process
237          */
238         syslog(LOG_DEBUG, "listdeliver: sweep started");
239
240         Array *roomlistarr = array_new(ROOMNAMELEN);                    // we have to queue them
241         CtdlForEachRoom(listdeliver_queue_room, roomlistarr);           // otherwise we get multiple cursors in progress
242
243         for (i=0; i<array_len(roomlistarr); ++i) {
244                 listdeliver_sweep_room((char *)array_get_element_at(roomlistarr, i));
245         }
246
247         array_free(roomlistarr);
248         syslog(LOG_DEBUG, "listdeliver: ended");
249         last_run = time(NULL);
250         doing_listdeliver = 0;
251 }
252
253
254 /*
255  * Module entry point
256  */
257 CTDL_MODULE_INIT(listdeliver)
258 {
259         if (!threading) {
260                 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
261         }
262         
263         /* return our module name for the log */
264         return "listsub";
265 }