1 // This module delivers messages to mailing lists.
3 // Copyright (c) 2002-2023 by the citadel.org team
5 // This program is open source software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License version 3.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 #include "../../sysdep.h"
22 #include <sys/types.h>
28 #include <libcitadel.h>
29 #include "../../citadel_defs.h"
30 #include "../../server.h"
31 #include "../../citserver.h"
32 #include "../../support.h"
33 #include "../../config.h"
34 #include "../../user_ops.h"
35 #include "../../database.h"
36 #include "../../msgbase.h"
37 #include "../../internet_addressing.h"
38 #include "../../clientsocket.h"
39 #include "../../ctdl_module.h"
41 int doing_listdeliver = 0;
44 // data passed back and forth between listdeliver_do_msg() and listdeliver_sweep_room()
46 long msgnum; // number of most recent message processed
47 char *netconf; // netconfig for this room (contains the recipients)
52 void listdeliver_do_msg(long msgnum, void *userdata) {
53 struct lddata *ld = (struct lddata *) userdata;
61 if (msgnum <= 0) return;
63 struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
64 if (!TheMessage) return;
66 // If the subject line does not contain the name of the room, add it now.
67 if (!bmstrcasestr(TheMessage->cm_fields[eMsgSubject], CC->room.QRname)) {
68 snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, TheMessage->cm_fields[eMsgSubject]);
69 CM_SetField(TheMessage, eMsgSubject, buf);
72 // From: should be set to the list address because doing otherwise makes DKIM parsers angry.
73 // Reply-to: should be set to the list address so that replies come to the list.
74 snprintf(buf, sizeof buf, "room_%s@%s", CC->room.QRname, CtdlGetConfigStr("c_fqdn"));
75 for (ch=buf; *ch; ++ch) {
76 if (isspace(*ch)) *ch = '_';
78 CM_SetField(TheMessage, erFc822Addr, buf);
79 CM_SetField(TheMessage, eReplyTo, buf);
81 // With that out of the way, let's figure out who this message needs to be sent to.
82 char *recipients = malloc(strlen(ld->netconf));
86 int config_lines = num_tokens(ld->netconf, '\n');
87 for (i=0; i<config_lines; ++i) {
88 extract_token(buf, ld->netconf, i, '\n', sizeof buf);
89 if (!strncasecmp(buf, "listrecp|", 9)) {
90 if (recipients[0] != 0) {
91 strcat(recipients, ",");
93 strcat(recipients, &buf[9]);
95 if (!strncasecmp(buf, "digestrecp|", 11)) {
96 if (recipients[0] != 0) {
97 strcat(recipients, ",");
99 strcat(recipients, &buf[11]);
103 // Where do we want bounces and other noise to be sent? Certainly not to the list members!
104 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", CtdlGetConfigStr("c_fqdn"));
106 // Now submit the message
107 struct recptypes *valid = validate_recipients(recipients, NULL, 0);
109 valid->bounce_to = strdup(bounce_to);
110 valid->envelope_from = strdup(bounce_to);
111 valid->sending_room = strdup(CC->room.QRname);
112 CtdlSubmitMsg(TheMessage, valid, "");
113 free_recipients(valid);
120 // Sweep through one room looking for mailing list deliveries to do
121 void listdeliver_sweep_room(char *roomname) {
122 char *netconfig = NULL;
123 char *newnetconfig = NULL;
128 int number_of_messages_processed = 0;
129 int number_of_recipients = 0;
132 if (CtdlGetRoom(&CC->room, roomname)) {
133 syslog(LOG_DEBUG, "listdeliver: no room <%s>", roomname);
137 netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
139 return; // no netconfig, no processing, no problem
142 config_lines = num_tokens(netconfig, '\n');
143 for (i=0; i<config_lines; ++i) {
144 extract_token(buf, netconfig, i, '\n', sizeof buf);
146 if (!strncasecmp(buf, "lastsent|", 9)) {
147 lastsent = atol(&buf[9]);
149 else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
150 ++number_of_recipients;
154 if (number_of_recipients > 0) {
155 syslog(LOG_DEBUG, "listdeliver: processing new messages in <%s> for <%d> recipients", CC->room.QRname, number_of_recipients);
156 ld.netconf = netconfig;
157 number_of_messages_processed = CtdlForEachMessage(MSGS_GT, lastsent, NULL, NULL, NULL, listdeliver_do_msg, &ld);
158 syslog(LOG_INFO, "listdeliver: processed <%d> messages in <%s> for <%d> recipients", number_of_messages_processed, CC->room.QRname, number_of_recipients);
160 if (number_of_messages_processed > 0) {
161 syslog(LOG_DEBUG, "listdeliver: new lastsent is %ld", ld.msgnum);
163 // Update this room's netconfig with the updated lastsent
164 begin_critical_section(S_NETCONFIGS);
165 netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
167 netconfig = strdup("");
170 // The new netconfig begins with the new lastsent directive
171 newnetconfig = malloc(strlen(netconfig) + 1024);
172 sprintf(newnetconfig, "lastsent|%ld\n", ld.msgnum);
174 // And then we append all of the old netconfig, minus the old lastsent. Also omit blank lines.
175 config_lines = num_tokens(netconfig, '\n');
176 for (i=0; i<config_lines; ++i) {
177 extract_token(buf, netconfig, i, '\n', sizeof buf);
178 if ( (!IsEmptyStr(buf)) && (strncasecmp(buf, "lastsent|", 9)) ) {
179 sprintf(&newnetconfig[strlen(newnetconfig)], "%s\n", buf);
183 // Write the new netconfig back to disk
184 SaveRoomNetConfigFile(CC->room.QRnumber, newnetconfig);
185 end_critical_section(S_NETCONFIGS);
186 free(newnetconfig); // this was the new netconfig, free it because we're done with it
189 free(netconfig); // this was the old netconfig, free it even if we didn't do anything
193 // Callback for listdeliver_sweep()
194 // Adds one room to the queue
195 void listdeliver_queue_room(struct ctdlroom *qrbuf, void *data) {
196 Array *roomlistarr = (Array *)data;
197 array_append(roomlistarr, qrbuf->QRname);
201 // Queue up the list of rooms so we can sweep them for mailing list delivery instructions
202 void listdeliver_sweep(void) {
203 static time_t last_run = 0L;
205 time_t now = time(NULL);
207 // Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
208 if ( (now - last_run) < 900 ) {
210 "listdeliver: delivery interval not yet reached; last run was %ldm%lds ago",
211 ((now - last_run) / 60),
212 ((now - last_run) % 60)
217 // This is a simple concurrency check to make sure only one listdeliver
218 // run is done at a time. We could do this with a mutex, but since we
219 // don't really require extremely fine granularity here, we'll do it
220 // with a static variable instead.
221 if (doing_listdeliver) return;
222 doing_listdeliver = 1;
224 // Go through each room looking for mailing lists to process
225 syslog(LOG_DEBUG, "listdeliver: sweep started");
227 Array *roomlistarr = array_new(ROOMNAMELEN); // we have to queue them
228 CtdlForEachRoom(listdeliver_queue_room, roomlistarr); // otherwise we get multiple cursors in progress
230 for (i=0; i<array_len(roomlistarr); ++i) {
231 listdeliver_sweep_room((char *)array_get_element_at(roomlistarr, i));
234 array_free(roomlistarr);
235 syslog(LOG_DEBUG, "listdeliver: ended");
236 last_run = time(NULL);
237 doing_listdeliver = 0;
241 // Initialization function, called from modules_init.c
242 char *ctdl_module_init_listdeliver(void) {
244 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
247 // return our module name for the log