2 * This module delivers messages to mailing lists.
4 * Copyright (c) 2002-2021 by the citadel.org team
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.
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.
24 #include <sys/types.h>
30 #include <libcitadel.h>
33 #include "citserver.h"
39 #include "internet_addressing.h"
40 #include "clientsocket.h"
41 #include "ctdl_module.h"
43 int doing_listdeliver = 0;
46 // data passed back and forth between listdeliver_do_msg() and listdeliver_sweep_room()
48 long msgnum; // number of most recent message processed
49 char *netconf; // netconfig for this room (contains the recipients)
54 void listdeliver_do_msg(long msgnum, void *userdata) {
55 struct lddata *ld = (struct lddata *) userdata;
62 if (msgnum <= 0) return;
64 struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
65 if (!TheMessage) return;
67 // If the subject line does not contain the name of the room, add it now.
68 if (!bmstrcasestr(TheMessage->cm_fields[eMsgSubject], CC->room.QRname)) {
69 snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, TheMessage->cm_fields[eMsgSubject]);
70 CM_SetField(TheMessage, eMsgSubject, buf, strlen(buf));
73 // Reply-to: should be set 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, eReplyTo, buf, strlen(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 (int 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, NULL, 0);
108 valid->bounce_to = strdup(bounce_to);
109 valid->envelope_from = strdup(bounce_to);
110 CtdlSubmitMsg(TheMessage, valid, "");
111 free_recipients(valid);
119 * 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
194 * Callback for listdeliver_sweep()
195 * Adds one room to the queue
197 void listdeliver_queue_room(struct ctdlroom *qrbuf, void *data) {
198 Array *roomlistarr = (Array *)data;
199 array_append(roomlistarr, qrbuf->QRname);
204 * Queue up the list of rooms so we can sweep them for mailing list delivery instructions
206 void listdeliver_sweep(void) {
207 static time_t last_run = 0L;
211 * Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
213 if ( (time(NULL) - last_run) < 900 ) {
218 * This is a simple concurrency check to make sure only one listdeliver
219 * run is done at a time. We could do this with a mutex, but since we
220 * don't really require extremely fine granularity here, we'll do it
221 * with a static variable instead.
223 if (doing_listdeliver) return;
224 doing_listdeliver = 1;
227 * Go through each room looking for mailing lists to process
229 syslog(LOG_DEBUG, "listdeliver: sweep started");
231 Array *roomlistarr = array_new(ROOMNAMELEN); // we have to queue them
232 CtdlForEachRoom(listdeliver_queue_room, roomlistarr); // otherwise we get multiple cursors in progress
234 for (i=0; i<array_len(roomlistarr); ++i) {
235 listdeliver_sweep_room((char *)array_get_element_at(roomlistarr, i));
238 array_free(roomlistarr);
239 syslog(LOG_DEBUG, "listdeliver: ended");
240 last_run = time(NULL);
241 doing_listdeliver = 0;
248 CTDL_MODULE_INIT(listdeliver)
251 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
254 /* return our module name for the log */