Added an elastic string buffer class to libcitadel. Why do I have a feeling I'm...
[citadel.git] / citadel / modules / listdeliver / serv_listdeliver.c
1 /*
2  * This module delivers messages to mailing lists.
3  *
4  * Copyright (c) 2002-2021 by the citadel.org team
5  *
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.
8  *  
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.
13  */
14
15 #include "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <ctype.h>
21 #include <signal.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <dirent.h>
26 #include <time.h>
27 #include <sys/wait.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <libcitadel.h>
31 #include "citadel.h"
32 #include "server.h"
33 #include "citserver.h"
34 #include "support.h"
35 #include "config.h"
36 #include "user_ops.h"
37 #include "database.h"
38 #include "msgbase.h"
39 #include "internet_addressing.h"
40 #include "clientsocket.h"
41 #include "ctdl_module.h"
42
43 int doing_listdeliver = 0;
44
45
46 // data passed back and forth between listdeliver_do_msg() and listdeliver_sweep_room()
47 struct lddata {
48         long msgnum;            // number of most recent message processed
49         char *netconf;          // netconfig for this room (contains the recipients)
50 };
51
52
53
54 void listdeliver_do_msg(long msgnum, void *userdata) {
55         struct lddata *ld = (struct lddata *) userdata;
56         if (!ld) return;
57         char buf[SIZ];
58
59         ld->msgnum = msgnum;
60         if (msgnum <= 0) return;
61
62         struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
63         if (!TheMessage) return;
64
65         char *recipients = malloc(strlen(ld->netconf));
66         if (recipients) {
67                 recipients[0] = 0;
68
69                 int config_lines = num_tokens(ld->netconf, '\n');
70                 for (int i=0; i<config_lines; ++i) {
71                         extract_token(buf, ld->netconf, i, '\n', sizeof buf);
72                         if (!strncasecmp(buf, "listrecp|", 9)) {
73                                 if (recipients[0] != 0) {
74                                         strcat(recipients, ",");
75                                 }
76                                 strcat(recipients, &buf[9]);
77                         }
78                         if (!strncasecmp(buf, "digestrecp|", 11)) {
79                                 if (recipients[0] != 0) {
80                                         strcat(recipients, ",");
81                                 }
82                                 strcat(recipients, &buf[11]);
83                         }
84                 }
85                 syslog(LOG_DEBUG, "\033[33m%s\033[0m", recipients);
86                 struct recptypes *valid = validate_recipients(recipients, NULL, 0);
87                 if (valid) {
88                         long new_msgnum = CtdlSubmitMsg(TheMessage, valid, "");
89                         free_recipients(valid);
90                 }
91         }
92         CM_Free(TheMessage);
93 }
94
95
96 void listdeliver_sweep_room(struct ctdlroom *qrbuf, void *data) {
97         char *netconfig = NULL;
98         long lastsent = 0;
99         char buf[SIZ];
100         int config_lines;
101         int i;
102         int number_of_messages_processed = 0;
103         int number_of_recipients = 0;
104         struct lddata ld;
105
106         if (CtdlGetRoom(&CC->room, qrbuf->QRname)) {
107                 syslog(LOG_DEBUG, "listdeliver: no room <%s>", qrbuf->QRname);
108                 return;
109         }
110
111         netconfig = LoadRoomNetConfigFile(qrbuf->QRnumber);
112         if (!netconfig) {
113                 return;                         // no netconfig, no processing, no problem
114         }
115
116         config_lines = num_tokens(netconfig, '\n');
117         for (i=0; i<config_lines; ++i) {
118                 extract_token(buf, netconfig, i, '\n', sizeof buf);
119
120                 if (!strncasecmp(buf, "lastsent|", 9)) {
121                         lastsent = atol(&buf[9]);
122                 }
123                 else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
124                         ++number_of_recipients;
125                 }
126         }
127
128         if (number_of_recipients > 0) {
129                 syslog(LOG_DEBUG, "listdeliver: processing new messages in <%s> for <%d> recipients", qrbuf->QRname, number_of_recipients);
130                 ld.netconf = netconfig;
131                 number_of_messages_processed = CtdlForEachMessage(MSGS_GT, lastsent, NULL, NULL, NULL, listdeliver_do_msg, &ld);
132                 syslog(LOG_DEBUG, "listdeliver: processed %d messages", number_of_messages_processed);
133         
134                 if (number_of_messages_processed > 0) {
135                         syslog(LOG_DEBUG, "listdeliver: new lastsent is %ld", ld.msgnum);
136
137                         // FIXME write lastsent back to netconfig
138                         syslog(LOG_DEBUG, "\033[31mBEFORE:<%s>\033[0m", netconfig);
139                         syslog(LOG_DEBUG, "\033[32mAFTER:<%s>\033[0m", netconfig);
140
141
142
143
144
145                 }
146         }
147
148         free(netconfig);
149 }
150
151
152
153 void listdeliver_sweep(void) {
154         static time_t last_run = 0L;
155
156         /*
157          * Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
158          */
159         if ( (time(NULL) - last_run) < 900 ) {
160                 return;
161         }
162
163         /*
164          * This is a simple concurrency check to make sure only one listdeliver
165          * run is done at a time.  We could do this with a mutex, but since we
166          * don't really require extremely fine granularity here, we'll do it
167          * with a static variable instead.
168          */
169         if (doing_listdeliver) return;
170         doing_listdeliver = 1;
171
172         /*
173          * Go through each room looking for mailing lists to process
174          */
175         syslog(LOG_DEBUG, "listdeliver: sweep started");
176         CtdlForEachRoom(listdeliver_sweep_room, NULL);
177         syslog(LOG_DEBUG, "listdeliver: ended");
178         last_run = time(NULL);
179         doing_listdeliver = 0;
180
181         exit(0);
182 }
183
184
185
186 /*
187  * Module entry point
188  */
189 CTDL_MODULE_INIT(listdeliver)
190 {
191         if (!threading)
192         {
193                 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
194         }
195         
196         /* return our module name for the log */
197         return "listsub";
198 }