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.
5 #include "../../sysdep.h"
14 #include <sys/types.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"
33 int doing_listdeliver = 0;
35 // data passed back and forth between listdeliver_do_msg() and listdeliver_sweep_room()
37 long msgnum; // number of most recent message processed
38 char *netconf; // netconfig for this room (contains the recipients)
42 void listdeliver_do_msg(long msgnum, void *userdata) {
43 struct lddata *ld = (struct lddata *) userdata;
51 if (msgnum <= 0) return;
53 struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
54 if (!TheMessage) return;
56 // FIXME add the list unsubscribe instructions directly to the message text. Do it right here.
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);
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 = '_';
70 CM_SetField(TheMessage, erFc822Addr, buf);
71 CM_SetField(TheMessage, eReplyTo, buf);
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.
76 strcat(buf, CC->room.QRname);
78 CM_SetField(TheMessage, eRecipient, buf);
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));
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, ",");
92 strcat(recipients, &buf[9]);
94 if (!strncasecmp(buf, "digestrecp|", 11)) {
95 if (recipients[0] != 0) {
96 strcat(recipients, ",");
98 strcat(recipients, &buf[11]);
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"));
105 // Now submit the message
106 struct recptypes *valid = validate_recipients(recipients, 0);
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);
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;
127 int number_of_messages_processed = 0;
128 int number_of_recipients = 0;
131 if (CtdlGetRoom(&CC->room, roomname)) {
132 syslog(LOG_DEBUG, "listdeliver: no room <%s>", roomname);
136 netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
138 return; // no netconfig, no processing, no problem
141 config_lines = num_tokens(netconfig, '\n');
142 for (i=0; i<config_lines; ++i) {
143 extract_token(buf, netconfig, i, '\n', sizeof buf);
145 if (!strncasecmp(buf, "lastsent|", 9)) {
146 lastsent = atol(&buf[9]);
148 else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
149 ++number_of_recipients;
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);
159 if (number_of_messages_processed > 0) {
160 syslog(LOG_DEBUG, "listdeliver: new lastsent is %ld", ld.msgnum);
162 // Update this room's netconfig with the updated lastsent
163 begin_critical_section(S_NETCONFIGS);
164 netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
166 netconfig = strdup("");
169 // The new netconfig begins with the new lastsent directive
170 newnetconfig = malloc(strlen(netconfig) + 1024);
171 sprintf(newnetconfig, "lastsent|%ld\n", ld.msgnum);
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);
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
188 free(netconfig); // this was the old netconfig, free it even if we didn't do anything
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);
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;
204 time_t now = time(NULL);
206 // Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
207 if ( (now - last_run) < 900 ) {
209 "listdeliver: delivery interval not yet reached; last run was %ldm%lds ago",
210 ((now - last_run) / 60),
211 ((now - last_run) % 60)
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;
223 // Go through each room looking for mailing lists to process
224 syslog(LOG_DEBUG, "listdeliver: sweep started");
226 Array *roomlistarr = array_new(ROOMNAMELEN); // we have to queue them
227 CtdlForEachRoom(listdeliver_queue_room, roomlistarr); // otherwise we get multiple cursors in progress
229 for (i=0; i<array_len(roomlistarr); ++i) {
230 listdeliver_sweep_room((char *)array_get_element_at(roomlistarr, i));
233 array_free(roomlistarr);
234 syslog(LOG_DEBUG, "listdeliver: ended");
235 last_run = time(NULL);
236 doing_listdeliver = 0;
240 // Initialization function, called from modules_init.c
241 char *ctdl_module_init_listdeliver(void) {
243 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
246 // return our module name for the log
247 return "listdeliver";