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;
60 if (msgnum <= 0) return;
62 struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
63 if (!TheMessage) return;
67 // FIXME munge the headers so it looks like it came from a mailing list
71 char *recipients = malloc(strlen(ld->netconf));
75 int config_lines = num_tokens(ld->netconf, '\n');
76 for (int i=0; i<config_lines; ++i) {
77 extract_token(buf, ld->netconf, i, '\n', sizeof buf);
78 if (!strncasecmp(buf, "listrecp|", 9)) {
79 if (recipients[0] != 0) {
80 strcat(recipients, ",");
82 strcat(recipients, &buf[9]);
84 if (!strncasecmp(buf, "digestrecp|", 11)) {
85 if (recipients[0] != 0) {
86 strcat(recipients, ",");
88 strcat(recipients, &buf[11]);
91 syslog(LOG_DEBUG, "\033[33m%s\033[0m", recipients);
92 struct recptypes *valid = validate_recipients(recipients, NULL, 0);
94 long new_msgnum = CtdlSubmitMsg(TheMessage, valid, "");
95 free_recipients(valid);
103 * Sweep through one room looking for mailing list deliveries to do
105 void listdeliver_sweep_room(char *roomname) {
106 char *netconfig = NULL;
111 int number_of_messages_processed = 0;
112 int number_of_recipients = 0;
115 if (CtdlGetRoom(&CC->room, roomname)) {
116 syslog(LOG_DEBUG, "listdeliver: no room <%s>", roomname);
120 netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
122 return; // no netconfig, no processing, no problem
125 syslog(LOG_DEBUG, "listdeliver: sweeping %s", roomname);
127 config_lines = num_tokens(netconfig, '\n');
128 for (i=0; i<config_lines; ++i) {
129 extract_token(buf, netconfig, i, '\n', sizeof buf);
131 if (!strncasecmp(buf, "lastsent|", 9)) {
132 lastsent = atol(&buf[9]);
134 else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
135 ++number_of_recipients;
139 if (number_of_recipients > 0) {
140 syslog(LOG_DEBUG, "listdeliver: processing new messages in <%s> for <%d> recipients", CC->room.QRname, number_of_recipients);
141 ld.netconf = netconfig;
142 number_of_messages_processed = CtdlForEachMessage(MSGS_GT, lastsent, NULL, NULL, NULL, listdeliver_do_msg, &ld);
143 syslog(LOG_DEBUG, "listdeliver: processed %d messages", number_of_messages_processed);
145 if (number_of_messages_processed > 0) {
146 syslog(LOG_DEBUG, "listdeliver: new lastsent is %ld", ld.msgnum);
148 // FIXME write lastsent back to netconfig
149 syslog(LOG_DEBUG, "\033[31mBEFORE:<%s>\033[0m", netconfig);
150 syslog(LOG_DEBUG, "\033[32mAFTER:<%s>\033[0m", netconfig);
163 * Callback for listdeliver_sweep()
164 * Adds one room to the queue
166 void listdeliver_queue_room(struct ctdlroom *qrbuf, void *data) {
167 Array *roomlistarr = (Array *)data;
168 array_append(roomlistarr, qrbuf->QRname);
173 * Queue up the list of rooms so we can sweep them for mailing list delivery instructions
175 void listdeliver_sweep(void) {
176 static time_t last_run = 0L;
180 * Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
182 if ( (time(NULL) - last_run) < 900 ) {
187 * This is a simple concurrency check to make sure only one listdeliver
188 * run is done at a time. We could do this with a mutex, but since we
189 * don't really require extremely fine granularity here, we'll do it
190 * with a static variable instead.
192 if (doing_listdeliver) return;
193 doing_listdeliver = 1;
196 * Go through each room looking for mailing lists to process
198 syslog(LOG_DEBUG, "listdeliver: sweep started");
200 Array *roomlistarr = array_new(ROOMNAMELEN); // we have to queue them
201 CtdlForEachRoom(listdeliver_queue_room, roomlistarr); // otherwise we get multiple cursors in progress
203 for (i=0; i<array_len(roomlistarr); ++i) {
204 listdeliver_sweep_room((char *)array_get_element_at(roomlistarr, i));
207 array_free(roomlistarr);
208 syslog(LOG_DEBUG, "listdeliver: ended");
209 last_run = time(NULL);
210 doing_listdeliver = 0;
220 CTDL_MODULE_INIT(listdeliver)
224 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
227 /* return our module name for the log */