]> code.citadel.org Git - citadel.git/blob - citadel/server/modules/listdeliver/serv_listdeliver.c
Remove preprocessor tests for OpenSSL. It's a requirement.
[citadel.git] / citadel / server / modules / listdeliver / serv_listdeliver.c
1 // This module delivers messages to mailing lists.
2 // Copyright (c) 2002-2024 by the citadel.org team (Art Cancro et al.)
3 // This program is open source software.  Use, duplication, or disclosure are subject to the GNU General Public License v3.
4
5 #include "../../sysdep.h"
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <ctype.h>
11 #include <signal.h>
12 #include <pwd.h>
13 #include <errno.h>
14 #include <sys/types.h>
15 #include <dirent.h>
16 #include <time.h>
17 #include <sys/wait.h>
18 #include <string.h>
19 #include <limits.h>
20 #include <libcitadel.h>
21 #include "../../citadel_defs.h"
22 #include "../../server.h"
23 #include "../../citserver.h"
24 #include "../../support.h"
25 #include "../../config.h"
26 #include "../../user_ops.h"
27 #include "../../database.h"
28 #include "../../msgbase.h"
29 #include "../../internet_addressing.h"
30 #include "../../clientsocket.h"
31 #include "../../ctdl_module.h"
32
33 int doing_listdeliver = 0;
34
35 // data passed back and forth between listdeliver_do_msg() and listdeliver_sweep_room()
36 struct lddata {
37         long msgnum;            // number of most recent message processed
38         char *netconf;          // netconfig for this room (contains the recipients)
39 };
40
41
42 void listdeliver_do_msg(long msgnum, void *userdata) {
43         struct lddata *ld = (struct lddata *) userdata;
44         if (!ld) return;
45         char buf[SIZ];
46         char *ch;
47         char bounce_to[256];
48         int i = 0;
49
50         ld->msgnum = msgnum;
51         if (msgnum <= 0) return;
52
53         struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
54         if (!TheMessage) return;
55
56         // FIXME add the list unsubscribe instructions directly to the message text.  Do it right here.
57
58         // If the subject line does not contain the name of the room, add it now.
59         if (!bmstrcasestr(TheMessage->cm_fields[eMsgSubject], CC->room.QRname)) {
60                 snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, TheMessage->cm_fields[eMsgSubject]);
61                 CM_SetField(TheMessage, eMsgSubject, buf);
62         }
63
64         // From: should be set to the list address because doing otherwise makes DKIM parsers angry.
65         // Reply-to: should be set to the list address so that replies come to the list.
66         snprintf(buf, sizeof buf, "room_%s@%s", CC->room.QRname, CtdlGetConfigStr("c_fqdn"));
67         for (ch=buf; *ch; ++ch) {
68                 if (isspace(*ch)) *ch = '_';
69         }
70         CM_SetField(TheMessage, erFc822Addr, buf);
71         CM_SetField(TheMessage, eReplyTo, buf);
72
73         // To: likewise needs to have something in it, definitely not the name of an actual mailing list member.
74         // Let's use the address and name of the room.
75         strcat(buf, " (");
76         strcat(buf, CC->room.QRname);
77         strcat(buf, " )");
78         CM_SetField(TheMessage, eRecipient, buf);
79
80         // With that out of the way, let's figure out who this message needs to be sent to.
81         char *recipients = malloc(strlen(ld->netconf));
82         if (recipients) {
83                 recipients[0] = 0;
84
85                 int config_lines = num_tokens(ld->netconf, '\n');
86                 for (i=0; i<config_lines; ++i) {
87                         extract_token(buf, ld->netconf, i, '\n', sizeof buf);
88                         if (!strncasecmp(buf, "listrecp|", 9)) {
89                                 if (recipients[0] != 0) {
90                                         strcat(recipients, ",");
91                                 }
92                                 strcat(recipients, &buf[9]);
93                         }
94                         if (!strncasecmp(buf, "digestrecp|", 11)) {
95                                 if (recipients[0] != 0) {
96                                         strcat(recipients, ",");
97                                 }
98                                 strcat(recipients, &buf[11]);
99                         }
100                 }
101
102                 // Where do we want bounces and other noise to be sent?  Certainly not to the list members!
103                 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", CtdlGetConfigStr("c_fqdn"));
104
105                 // Now submit the message
106                 struct recptypes *valid = validate_recipients(recipients, 0);
107                 if (valid) {
108                         valid->bounce_to = strdup(bounce_to);
109                         valid->envelope_from = strdup(bounce_to);
110                         valid->sending_room = strdup(CC->room.QRname);
111                         CtdlSubmitMsg(TheMessage, valid, "");
112                         free_recipients(valid);
113                 }
114         }
115         CM_Free(TheMessage);
116 }
117
118
119 // Sweep through one room looking for mailing list deliveries to do
120 void listdeliver_sweep_room(char *roomname) {
121         char *netconfig = NULL;
122         char *newnetconfig = NULL;
123         long lastsent = 0;
124         char buf[SIZ];
125         int config_lines;
126         int i;
127         int number_of_messages_processed = 0;
128         int number_of_recipients = 0;
129         struct lddata ld;
130
131         if (CtdlGetRoom(&CC->room, roomname)) {
132                 syslog(LOG_DEBUG, "listdeliver: no room <%s>", roomname);
133                 return;
134         }
135
136         netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
137         if (!netconfig) {
138                 return;                         // no netconfig, no processing, no problem
139         }
140
141         config_lines = num_tokens(netconfig, '\n');
142         for (i=0; i<config_lines; ++i) {
143                 extract_token(buf, netconfig, i, '\n', sizeof buf);
144
145                 if (!strncasecmp(buf, "lastsent|", 9)) {
146                         lastsent = atol(&buf[9]);
147                 }
148                 else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
149                         ++number_of_recipients;
150                 }
151         }
152
153         if (number_of_recipients > 0) {
154                 syslog(LOG_DEBUG, "listdeliver: processing new messages in <%s> for <%d> recipients", CC->room.QRname, number_of_recipients);
155                 ld.netconf = netconfig;
156                 number_of_messages_processed = CtdlForEachMessage(MSGS_GT, lastsent, NULL, NULL, NULL, listdeliver_do_msg, &ld);
157                 syslog(LOG_INFO, "listdeliver: processed <%d> messages in <%s> for <%d> recipients", number_of_messages_processed, CC->room.QRname, number_of_recipients);
158         
159                 if (number_of_messages_processed > 0) {
160                         syslog(LOG_DEBUG, "listdeliver: new lastsent is %ld", ld.msgnum);
161
162                         // Update this room's netconfig with the updated lastsent
163                         begin_critical_section(S_NETCONFIGS);
164                         netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
165                         if (!netconfig) {
166                                 netconfig = strdup("");
167                         }
168
169                         // The new netconfig begins with the new lastsent directive
170                         newnetconfig = malloc(strlen(netconfig) + 1024);
171                         sprintf(newnetconfig, "lastsent|%ld\n", ld.msgnum);
172
173                         // And then we append all of the old netconfig, minus the old lastsent.  Also omit blank lines.
174                         config_lines = num_tokens(netconfig, '\n');
175                         for (i=0; i<config_lines; ++i) {
176                                 extract_token(buf, netconfig, i, '\n', sizeof buf);
177                                 if ( (!IsEmptyStr(buf)) && (strncasecmp(buf, "lastsent|", 9)) ) {
178                                         sprintf(&newnetconfig[strlen(newnetconfig)], "%s\n", buf);
179                                 }
180                         }
181
182                         // Write the new netconfig back to disk
183                         SaveRoomNetConfigFile(CC->room.QRnumber, newnetconfig);
184                         end_critical_section(S_NETCONFIGS);
185                         free(newnetconfig);     // this was the new netconfig, free it because we're done with it
186                 }
187         }
188         free(netconfig);                        // this was the old netconfig, free it even if we didn't do anything
189 }
190
191
192 // Callback for listdeliver_sweep()
193 // Adds one room to the queue
194 void listdeliver_queue_room(struct ctdlroom *qrbuf, void *data) {
195         Array *roomlistarr = (Array *)data;
196         array_append(roomlistarr, qrbuf->QRname);
197 }
198
199
200 // Queue up the list of rooms so we can sweep them for mailing list delivery instructions
201 void listdeliver_sweep(void) {
202         static time_t last_run = 0L;
203         int i = 0;
204         time_t now = time(NULL);
205
206         // Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
207         if ( (now - last_run) < 900 ) {
208                 syslog(LOG_DEBUG,
209                         "listdeliver: delivery interval not yet reached; last run was %ldm%lds ago",
210                         ((now - last_run) / 60),
211                         ((now - last_run) % 60)
212                 );
213                 return;
214         }
215
216         // This is a simple concurrency check to make sure only one listdeliver
217         // run is done at a time.  We could do this with a mutex, but since we
218         // don't really require extremely fine granularity here, we'll do it
219         // with a static variable instead.
220         if (doing_listdeliver) return;
221         doing_listdeliver = 1;
222
223         // Go through each room looking for mailing lists to process
224         syslog(LOG_DEBUG, "listdeliver: sweep started");
225
226         Array *roomlistarr = array_new(ROOMNAMELEN);                    // we have to queue them
227         CtdlForEachRoom(listdeliver_queue_room, roomlistarr);           // otherwise we get multiple cursors in progress
228
229         for (i=0; i<array_len(roomlistarr); ++i) {
230                 listdeliver_sweep_room((char *)array_get_element_at(roomlistarr, i));
231         }
232
233         array_free(roomlistarr);
234         syslog(LOG_DEBUG, "listdeliver: ended");
235         last_run = time(NULL);
236         doing_listdeliver = 0;
237 }
238
239
240 // Initialization function, called from modules_init.c
241 char *ctdl_module_init_listdeliver(void) {
242         if (!threading) {
243                 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
244         }
245         
246         // return our module name for the log
247         return "listdeliver";
248 }