2 * This module handles network mail and mailing list processing.
4 * Copyright (c) 2000-2020 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 fprintf(sc->digestfp, "\n");
273 if (!CM_IsEmpty(msg, eMsgSubject)) {
274 fprintf(sc->digestfp,
276 msg->cm_fields[eMsgSubject]);
279 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
281 safestrncpy(CC->preferred_formats,
283 sizeof CC->preferred_formats);
285 CtdlOutputPreLoadedMsg(msg,
290 StrBufTrim(CC->redirect_buffer);
291 fwrite(HKEY("\n"), 1, sc->digestfp);
292 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
293 fwrite(HKEY("\n"), 1, sc->digestfp);
295 FreeStrBuf(&CC->redirect_buffer);
297 sc->num_msgs_spooled += 1;
303 void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
305 struct CtdlMessage *msg = NULL;
308 * Process mailing list recipients
310 if (sc->Users[listrecp] == NULL)
313 /* create our own copy of the message.
314 * We're going to need to modify it
315 * in order to insert the [list name] in it, etc.
318 msg = CM_Duplicate(omsg);
321 CM_SetField(msg, eReplyTo, SKEY(sc->Users[roommailalias]));
323 /* if there is no other recipient, Set the recipient
324 * of the list message to the email address of the
327 if (CM_IsEmpty(msg, eRecipient))
329 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
332 /* Set the 'List-ID' header */
333 CM_SetField(msg, eListID, SKEY(sc->ListID));
336 /* Prepend "[List name]" to the subject */
337 ListCalculateSubject(msg);
339 /* Handle delivery */
340 network_deliver_list(msg, sc, CC->room.QRname);
345 * Deliver list messages to everyone on the list ... efficiently
347 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
352 /* Don't do this if there were no recipients! */
353 if (sc->Users[listrecp] == NULL)
356 /* Now generate the delivery instructions */
358 /* Where do we want bounces and other noise to be heard?
359 * Surely not the list members! */
360 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", CtdlGetConfigStr("c_fqdn"));
362 /* Now submit the message */
363 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
365 valid->bounce_to = strdup(bounce_to);
366 valid->envelope_from = strdup(bounce_to);
367 valid->sending_room = strdup(RoomName);
368 CtdlSubmitMsg(msg, valid, NULL, 0);
369 free_recipients(valid);
371 /* Do not call CM_Free(msg) here; the caller will free it. */
375 void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
377 struct CtdlMessage *msg = NULL;
378 int ok_to_participate = 0;
382 * Process client-side list participations for this room
384 if (sc->Users[participate] == NULL)
387 msg = CM_Duplicate(omsg);
389 /* Only send messages which originated on our own
390 * Citadel network, otherwise we'll end up sending the
391 * remote mailing list's messages back to it, which
394 ok_to_participate = 0;
396 // FIXME -- After we removed CitaNet/IGnet support , we now need a new heuristic to determine
397 // whether a message originated locally. This means the "participate" mode no longer works.
398 // We'll definitely need to refactor this when we do other federated stuff later.
400 if (ok_to_participate)
402 /* Replace the Internet email address of the
403 * actual author with the email address of the
404 * room itself, so the remote listserv doesn't
407 CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
409 valid = validate_recipients(ChrPtr(sc->Users[participate]) , NULL, 0);
411 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
412 CtdlSubmitMsg(msg, valid, "", 0);
413 free_recipients(valid);
420 * Spools out one message from the list.
422 void network_spool_msg(long msgnum, void *userdata)
424 struct CtdlMessage *msg = NULL;
425 long delete_after_send = 0; /* Set to 1 to delete after spooling */
428 sc = (SpoolControl *)userdata;
429 msg = CtdlFetchMessage(msgnum, 1, 1);
433 syslog(LOG_ERR, "netmail: failed to load Message <%ld> from disk", msgnum);
436 network_process_list(sc, msg, &delete_after_send);
437 network_process_digest(sc, msg, &delete_after_send);
438 network_process_participate(sc, msg, &delete_after_send);
442 /* update lastsent */
443 sc->lastsent = msgnum;
445 /* Delete this message if delete-after-send is set */
446 if (delete_after_send) {
447 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");