citadel
About: Citadel is an advanced messaging and collaboration system for groupware and BBS applications (preferred OS: Linux).
  Fossies Dox: citadel.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
serv_listdeliver.c
Go to the documentation of this file.
1// This module delivers messages to mailing lists.
2//
3// Copyright (c) 2002-2022 by the citadel.org team
4//
5// This program is open source software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License version 3.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12
13#include "../../sysdep.h"
14#include <stdlib.h>
15#include <unistd.h>
16#include <stdio.h>
17#include <fcntl.h>
18#include <ctype.h>
19#include <signal.h>
20#include <pwd.h>
21#include <errno.h>
22#include <sys/types.h>
23#include <dirent.h>
24#include <time.h>
25#include <sys/wait.h>
26#include <string.h>
27#include <limits.h>
28#include <libcitadel.h>
29#include "../../citadel.h"
30#include "../../server.h"
31#include "../../citserver.h"
32#include "../../support.h"
33#include "../../config.h"
34#include "../../user_ops.h"
35#include "../../database.h"
36#include "../../msgbase.h"
37#include "../../internet_addressing.h"
38#include "../../clientsocket.h"
39#include "../../ctdl_module.h"
40
42
43
44// data passed back and forth between listdeliver_do_msg() and listdeliver_sweep_room()
45struct lddata {
46 long msgnum; // number of most recent message processed
47 char *netconf; // netconfig for this room (contains the recipients)
48};
49
50
51
52void listdeliver_do_msg(long msgnum, void *userdata) {
53 struct lddata *ld = (struct lddata *) userdata;
54 if (!ld) return;
55 char buf[SIZ];
56 char *ch;
57 char bounce_to[256];
58 int i = 0;
59
60 ld->msgnum = msgnum;
61 if (msgnum <= 0) return;
62
63 struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
64 if (!TheMessage) return;
65
66 // If the subject line does not contain the name of the room, add it now.
67 if (!bmstrcasestr(TheMessage->cm_fields[eMsgSubject], CC->room.QRname)) {
68 snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, TheMessage->cm_fields[eMsgSubject]);
69 CM_SetField(TheMessage, eMsgSubject, buf, strlen(buf));
70 }
71
72 // From: should be set to the list address because doing otherwise makes DKIM parsers angry.
73 // Reply-to: should be set to the list address 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 = '_';
77 }
78 CM_SetField(TheMessage, erFc822Addr, buf, strlen(buf));
79 CM_SetField(TheMessage, eReplyTo, buf, strlen(buf));
80
81 // With that out of the way, let's figure out who this message needs to be sent to.
82 char *recipients = malloc(strlen(ld->netconf));
83 if (recipients) {
84 recipients[0] = 0;
85
86 int config_lines = num_tokens(ld->netconf, '\n');
87 for (i=0; i<config_lines; ++i) {
88 extract_token(buf, ld->netconf, i, '\n', sizeof buf);
89 if (!strncasecmp(buf, "listrecp|", 9)) {
90 if (recipients[0] != 0) {
91 strcat(recipients, ",");
92 }
93 strcat(recipients, &buf[9]);
94 }
95 if (!strncasecmp(buf, "digestrecp|", 11)) {
96 if (recipients[0] != 0) {
97 strcat(recipients, ",");
98 }
99 strcat(recipients, &buf[11]);
100 }
101 }
102
103 // Where do we want bounces and other noise to be sent? Certainly not to the list members!
104 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", CtdlGetConfigStr("c_fqdn"));
105
106 // Now submit the message
107 struct recptypes *valid = validate_recipients(recipients, NULL, 0);
108 if (valid) {
109 valid->bounce_to = strdup(bounce_to);
110 valid->envelope_from = strdup(bounce_to);
111 valid->sending_room = strdup(CC->room.QRname);
112 CtdlSubmitMsg(TheMessage, valid, "");
113 free_recipients(valid);
114 }
115 }
116 CM_Free(TheMessage);
117}
118
119
120// Sweep through one room looking for mailing list deliveries to do
121void listdeliver_sweep_room(char *roomname) {
122 char *netconfig = NULL;
123 char *newnetconfig = NULL;
124 long lastsent = 0;
125 char buf[SIZ];
126 int config_lines;
127 int i;
128 int number_of_messages_processed = 0;
129 int number_of_recipients = 0;
130 struct lddata ld;
131
132 if (CtdlGetRoom(&CC->room, roomname)) {
133 syslog(LOG_DEBUG, "listdeliver: no room <%s>", roomname);
134 return;
135 }
136
137 netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
138 if (!netconfig) {
139 return; // no netconfig, no processing, no problem
140 }
141
142 config_lines = num_tokens(netconfig, '\n');
143 for (i=0; i<config_lines; ++i) {
144 extract_token(buf, netconfig, i, '\n', sizeof buf);
145
146 if (!strncasecmp(buf, "lastsent|", 9)) {
147 lastsent = atol(&buf[9]);
148 }
149 else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
150 ++number_of_recipients;
151 }
152 }
153
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);
159
160 if (number_of_messages_processed > 0) {
161 syslog(LOG_DEBUG, "listdeliver: new lastsent is %ld", ld.msgnum);
162
163 // Update this room's netconfig with the updated lastsent
165 netconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
166 if (!netconfig) {
167 netconfig = strdup("");
168 }
169
170 // The new netconfig begins with the new lastsent directive
171 newnetconfig = malloc(strlen(netconfig) + 1024);
172 sprintf(newnetconfig, "lastsent|%ld\n", ld.msgnum);
173
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);
180 }
181 }
182
183 // Write the new netconfig back to disk
184 SaveRoomNetConfigFile(CC->room.QRnumber, newnetconfig);
186 free(newnetconfig); // this was the new netconfig, free it because we're done with it
187 }
188 }
189 free(netconfig); // this was the old netconfig, free it even if we didn't do anything
190}
191
192
193// Callback for listdeliver_sweep()
194// Adds one room to the queue
195void listdeliver_queue_room(struct ctdlroom *qrbuf, void *data) {
196 Array *roomlistarr = (Array *)data;
197 array_append(roomlistarr, qrbuf->QRname);
198}
199
200
201// Queue up the list of rooms so we can sweep them for mailing list delivery instructions
203 static time_t last_run = 0L;
204 int i = 0;
205 time_t now = time(NULL);
206
207 // Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
208 if ( (now - last_run) < 900 ) {
209 syslog(LOG_DEBUG,
210 "listdeliver: delivery interval not yet reached; last run was %ldm%lds ago",
211 ((now - last_run) / 60),
212 ((now - last_run) % 60)
213 );
214 return;
215 }
216
217 // This is a simple concurrency check to make sure only one listdeliver
218 // run is done at a time. We could do this with a mutex, but since we
219 // don't really require extremely fine granularity here, we'll do it
220 // with a static variable instead.
221 if (doing_listdeliver) return;
223
224 // Go through each room looking for mailing lists to process
225 syslog(LOG_DEBUG, "listdeliver: sweep started");
226
227 Array *roomlistarr = array_new(ROOMNAMELEN); // we have to queue them
228 CtdlForEachRoom(listdeliver_queue_room, roomlistarr); // otherwise we get multiple cursors in progress
229
230 for (i=0; i<array_len(roomlistarr); ++i) {
231 listdeliver_sweep_room((char *)array_get_element_at(roomlistarr, i));
232 }
233
234 array_free(roomlistarr);
235 syslog(LOG_DEBUG, "listdeliver: ended");
236 last_run = time(NULL);
238}
239
240
241// Initialization function, called from modules_init.c
243 if (!threading) {
245 }
246
247 // return our module name for the log
248 return "listsub";
249}
#define ROOMNAMELEN
Definition: citadel.h:51
char * CtdlGetConfigStr(char *key)
Definition: config.c:363
#define CC
Definition: context.h:140
void SaveRoomNetConfigFile(long roomnum, const char *raw_netconfig)
Definition: netconfig.c:36
int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name)
Definition: room_ops.c:309
char * LoadRoomNetConfigFile(long roomnum)
Definition: netconfig.c:61
void CtdlRegisterSessionHook(void(*fcn_ptr)(void), int EventType, int Priority)
int threading
Definition: modules_init.c:24
#define PRIO_AGGR
Definition: ctdl_module.h:76
void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data)
Definition: room_ops.c:542
struct recptypes * validate_recipients(char *supplied_recipients, const char *RemoteIdentifier, int Flags)
void free_recipients(struct recptypes *valid)
void CM_Free(struct CtdlMessage *msg)
Definition: msgbase.c:305
int CtdlForEachMessage(int mode, long ref, char *search_string, char *content_type, struct CtdlMessage *compare, ForEachMsgCallback CallBack, void *userdata)
Definition: msgbase.c:621
struct CtdlMessage * CtdlFetchMessage(long msgnum, int with_body)
Definition: msgbase.c:1128
long CtdlSubmitMsg(struct CtdlMessage *msg, struct recptypes *recps, const char *force)
Definition: msgbase.c:2588
void CM_SetField(struct CtdlMessage *Msg, eMsgField which, const char *buf, long length)
Definition: msgbase.c:137
@ MSGS_GT
Definition: msgbase.h:11
void * malloc(unsigned)
void free(void *)
void listdeliver_sweep(void)
void listdeliver_queue_room(struct ctdlroom *qrbuf, void *data)
char * ctdl_module_init_listdeliver(void)
void listdeliver_do_msg(long msgnum, void *userdata)
void listdeliver_sweep_room(char *roomname)
int doing_listdeliver
struct ctdlroom qrbuf
Definition: serv_migrate.c:497
time_t last_run
@ erFc822Addr
Definition: server.h:310
@ eReplyTo
Definition: server.h:313
@ eMsgSubject
Definition: server.h:320
@ S_NETCONFIGS
Definition: server.h:145
#define EVT_TIMER
Definition: server.h:224
char * cm_fields[256]
Definition: server.h:37
char QRname[128]
Definition: citadel.h:94
char * netconf
char * bounce_to
Definition: server.h:59
char * sending_room
Definition: server.h:61
char * envelope_from
Definition: server.h:60
#define SIZ
Definition: sysconfig.h:33
void begin_critical_section(int which_one)
Definition: threads.c:62
void end_critical_section(int which_one)
Definition: threads.c:80