2 * This module handles network mail and mailing list processing.
4 * Copyright (c) 2000-2018 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.
14 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
15 * This is a fairly high-level type of critical section. It ensures that no
16 * two threads work on the netconfigs files at the same time. Since we do
17 * so many things inside these, here are the rules:
18 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
19 * 2. Do *not* perform any I/O with the client during these sections.
24 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
25 * requests that have not been confirmed will be deleted.
27 #define EXP 259200 /* three days */
39 #include <sys/types.h>
41 #if TIME_WITH_SYS_TIME
42 # include <sys/time.h>
46 # include <sys/time.h>
54 # if HAVE_SYS_SYSCALL_H
55 # include <sys/syscall.h>
62 #include <libcitadel.h>
65 #include "citserver.h"
71 #include "internet_addressing.h"
72 #include "serv_network.h"
73 #include "clientsocket.h"
74 #include "citadel_dirs.h"
77 #include "ctdl_module.h"
81 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName);
83 void aggregate_recipients(StrBuf **recps, RoomNetCfg Which, OneRoomNetCfg *OneRNCfg, long nSegments)
91 * Figure out how big a buffer we need to allocate
93 for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
94 recps_len = recps_len + StrLength(nptr->Value[0]) + 2;
101 *recps = NewStrBufPlain(NULL, recps_len);
103 if (*recps == NULL) {
104 syslog(LOG_ERR, "netmail: cannot allocate %ld bytes for recps", (long)recps_len);
109 for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
110 if (nptr != OneRNCfg->NetConfigs[Which]) {
111 for (i = 0; i < nSegments; i++)
112 StrBufAppendBufPlain(*recps, HKEY(","), i);
114 StrBufAppendBuf(*recps, nptr->Value[0], 0);
115 if (Which == ignet_push_share)
117 StrBufAppendBufPlain(*recps, HKEY(","), 0);
118 StrBufAppendBuf(*recps, nptr->Value[1], 0);
124 static void ListCalculateSubject(struct CtdlMessage *msg)
126 struct CitContext *CCC = CC;
127 StrBuf *Subject, *FlatSubject;
131 if (CM_IsEmpty(msg, eMsgSubject)) {
132 Subject = NewStrBufPlain(HKEY("(no subject)"));
135 Subject = NewStrBufPlain(CM_KEY(msg, eMsgSubject));
137 FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
138 StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
140 rlen = strlen(CCC->room.QRname);
141 pCh = strstr(ChrPtr(FlatSubject), CCC->room.QRname);
143 (*(pCh + rlen) != ']') ||
144 (pCh == ChrPtr(FlatSubject)) ||
149 StrBufPlain(Subject, HKEY("["));
150 StrBufAppendBufPlain(Subject,
153 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
154 StrBufAppendBuf(Subject, FlatSubject, 0);
155 /* so we can free the right one swap them */
157 Subject = FlatSubject;
159 StrBufRFC2047encode(&Subject, FlatSubject);
162 CM_SetAsFieldSB(msg, eMsgSubject, &Subject);
164 FreeStrBuf(&FlatSubject);
168 * Deliver digest messages
170 void network_deliver_digest(SpoolControl *sc)
172 struct CitContext *CCC = CC;
176 struct CtdlMessage *msg = NULL;
181 if (sc->Users[digestrecp] == NULL)
184 msg = malloc(sizeof(struct CtdlMessage));
185 memset(msg, 0, sizeof(struct CtdlMessage));
186 msg->cm_magic = CTDLMESSAGE_MAGIC;
187 msg->cm_format_type = FMT_RFC822;
188 msg->cm_anon_type = MES_NORMAL;
190 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
191 CM_SetField(msg, eAuthor, CCC->room.QRname, strlen(CCC->room.QRname));
192 len = snprintf(buf, sizeof buf, "[%s]", CCC->room.QRname);
193 CM_SetField(msg, eMsgSubject, buf, len);
195 CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
196 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
198 /* Set the 'List-ID' header */
199 CM_SetField(msg, eListID, SKEY(sc->ListID));
202 * Go fetch the contents of the digest
204 fseek(sc->digestfp, 0L, SEEK_END);
205 msglen = ftell(sc->digestfp);
207 pbuf = malloc(msglen + 1);
208 fseek(sc->digestfp, 0L, SEEK_SET);
209 fread(pbuf, (size_t)msglen, 1, sc->digestfp);
211 CM_SetAsField(msg, eMesageText, &pbuf, msglen);
213 /* Now generate the delivery instructions */
215 /* Where do we want bounces and other noise to be heard?
216 * Surely not the list members! */
217 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", CtdlGetConfigStr("c_fqdn"));
219 /* Now submit the message */
220 valid = validate_recipients(ChrPtr(sc->Users[digestrecp]), NULL, 0);
222 valid->bounce_to = strdup(bounce_to);
223 valid->envelope_from = strdup(bounce_to);
224 CtdlSubmitMsg(msg, valid, NULL, 0);
227 free_recipients(valid);
231 void network_process_digest(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
234 struct CtdlMessage *msg = NULL;
236 if (sc->Users[digestrecp] == NULL)
239 /* If there are digest recipients, we have to build a digest */
240 if (sc->digestfp == NULL) {
242 sc->digestfp = create_digest_file(&sc->room, 1);
244 if (sc->digestfp == NULL)
247 sc->haveDigest = ftell(sc->digestfp) > 0;
248 if (!sc->haveDigest) {
249 fprintf(sc->digestfp, "Content-type: text/plain\n\n");
254 msg = CM_Duplicate(omsg);
257 fprintf(sc->digestfp,
258 " -----------------------------------"
259 "------------------------------------"
261 fprintf(sc->digestfp, "From: ");
262 if (!CM_IsEmpty(msg, eAuthor)) {
263 fprintf(sc->digestfp,
265 msg->cm_fields[eAuthor]);
267 if (!CM_IsEmpty(msg, erFc822Addr)) {
268 fprintf(sc->digestfp,
270 msg->cm_fields[erFc822Addr]);
272 else if (!CM_IsEmpty(msg, eNodeName)) {
273 fprintf(sc->digestfp,
275 msg->cm_fields[eNodeName]);
277 fprintf(sc->digestfp, "\n");
278 if (!CM_IsEmpty(msg, eMsgSubject)) {
279 fprintf(sc->digestfp,
281 msg->cm_fields[eMsgSubject]);
284 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
286 safestrncpy(CC->preferred_formats,
288 sizeof CC->preferred_formats);
290 CtdlOutputPreLoadedMsg(msg,
295 StrBufTrim(CC->redirect_buffer);
296 fwrite(HKEY("\n"), 1, sc->digestfp);
297 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
298 fwrite(HKEY("\n"), 1, sc->digestfp);
300 FreeStrBuf(&CC->redirect_buffer);
302 sc->num_msgs_spooled += 1;
308 void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
310 struct CtdlMessage *msg = NULL;
313 * Process mailing list recipients
315 if (sc->Users[listrecp] == NULL)
318 /* create our own copy of the message.
319 * We're going to need to modify it
320 * in order to insert the [list name] in it, etc.
323 msg = CM_Duplicate(omsg);
326 CM_SetField(msg, eReplyTo, SKEY(sc->Users[roommailalias]));
328 /* if there is no other recipient, Set the recipient
329 * of the list message to the email address of the
332 if (CM_IsEmpty(msg, eRecipient))
334 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
337 /* Set the 'List-ID' header */
338 CM_SetField(msg, eListID, SKEY(sc->ListID));
341 /* Prepend "[List name]" to the subject */
342 ListCalculateSubject(msg);
344 /* Handle delivery */
345 network_deliver_list(msg, sc, CC->room.QRname);
350 * Deliver list messages to everyone on the list ... efficiently
352 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
357 /* Don't do this if there were no recipients! */
358 if (sc->Users[listrecp] == NULL)
361 /* Now generate the delivery instructions */
363 /* Where do we want bounces and other noise to be heard?
364 * Surely not the list members! */
365 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", CtdlGetConfigStr("c_fqdn"));
367 /* Now submit the message */
368 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
370 valid->bounce_to = strdup(bounce_to);
371 valid->envelope_from = strdup(bounce_to);
372 valid->sending_room = strdup(RoomName);
373 CtdlSubmitMsg(msg, valid, NULL, 0);
374 free_recipients(valid);
376 /* Do not call CM_Free(msg) here; the caller will free it. */
380 void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
382 struct CtdlMessage *msg = NULL;
383 int ok_to_participate = 0;
387 * Process client-side list participations for this room
389 if (sc->Users[participate] == NULL)
392 msg = CM_Duplicate(omsg);
394 /* Only send messages which originated on our own
395 * Citadel network, otherwise we'll end up sending the
396 * remote mailing list's messages back to it, which
399 ok_to_participate = 0;
400 if (!CM_IsEmpty(msg, eNodeName)) {
401 if (!strcasecmp(msg->cm_fields[eNodeName], CtdlGetConfigStr("c_nodename")))
403 ok_to_participate = 1;
406 if (ok_to_participate)
408 /* Replace the Internet email address of the
409 * actual author with the email address of the
410 * room itself, so the remote listserv doesn't
413 CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
415 valid = validate_recipients(ChrPtr(sc->Users[participate]) , NULL, 0);
417 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
418 CtdlSubmitMsg(msg, valid, "", 0);
419 free_recipients(valid);
426 * Spools out one message from the list.
428 void network_spool_msg(long msgnum, void *userdata)
430 struct CtdlMessage *msg = NULL;
431 long delete_after_send = 0; /* Set to 1 to delete after spooling */
434 sc = (SpoolControl *)userdata;
435 msg = CtdlFetchMessage(msgnum, 1, 1);
439 syslog(LOG_ERR, "netmail: failed to load Message <%ld> from disk", msgnum);
442 network_process_list(sc, msg, &delete_after_send);
443 network_process_digest(sc, msg, &delete_after_send);
444 network_process_participate(sc, msg, &delete_after_send);
448 /* update lastsent */
449 sc->lastsent = msgnum;
451 /* Delete this message if delete-after-send is set */
452 if (delete_after_send) {
453 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");