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;
59 struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
61 int config_lines = num_tokens(ld->netconf, '\n');
62 for (int i=0; i<config_lines; ++i) {
63 extract_token(buf, ld->netconf, i, '\n', sizeof buf);
64 if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
65 syslog(LOG_DEBUG, "\033[32mDeliver %ld to %s\033[0m", msgnum, buf);
73 void listdeliver_sweep_room(struct ctdlroom *qrbuf, void *data) {
74 char *netconfig = NULL;
79 int number_of_messages_processed = 0;
80 int number_of_recipients = 0;
83 if (CtdlGetRoom(&CC->room, qrbuf->QRname)) {
84 syslog(LOG_DEBUG, "listdeliver: no room <%s>", qrbuf->QRname);
88 netconfig = LoadRoomNetConfigFile(qrbuf->QRnumber);
90 return; // no netconfig, no processing, no problem
93 config_lines = num_tokens(netconfig, '\n');
94 for (i=0; i<config_lines; ++i) {
95 extract_token(buf, netconfig, i, '\n', sizeof buf);
97 if (!strncasecmp(buf, "lastsent|", 9)) {
98 lastsent = atol(&buf[9]);
100 else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
101 ++number_of_recipients;
105 if (number_of_recipients > 0) {
106 syslog(LOG_DEBUG, "listdeliver: processing new messages in <%s> for <%d> recipients", qrbuf->QRname, number_of_recipients);
107 ld.netconf = netconfig;
108 number_of_messages_processed = CtdlForEachMessage(MSGS_GT, lastsent, NULL, NULL, NULL, listdeliver_do_msg, &ld);
109 syslog(LOG_DEBUG, "listdeliver: processed %d messages", number_of_messages_processed);
111 if (number_of_messages_processed > 0) {
112 syslog(LOG_DEBUG, "listdeliver: new lastsent is %ld", ld.msgnum);
113 // FIXME write lastsent back to netconfig
122 void listdeliver_sweep(void) {
123 static time_t last_run = 0L;
126 * Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
128 if ( (time(NULL) - last_run) < 900 ) {
133 * This is a simple concurrency check to make sure only one listdeliver
134 * run is done at a time. We could do this with a mutex, but since we
135 * don't really require extremely fine granularity here, we'll do it
136 * with a static variable instead.
138 if (doing_listdeliver) return;
139 doing_listdeliver = 1;
142 * Go through each room looking for mailing lists to process
144 syslog(LOG_DEBUG, "listdeliver: sweep started");
145 CtdlForEachRoom(listdeliver_sweep_room, NULL);
146 syslog(LOG_DEBUG, "listdeliver: ended");
147 last_run = time(NULL);
148 doing_listdeliver = 0;
158 CTDL_MODULE_INIT(listdeliver)
162 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
165 /* return our module name for the log */