2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2012 by the citadel.org team
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License, version 3.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
16 * This is a fairly high-level type of critical section. It ensures that no
17 * two threads work on the netconfigs files at the same time. Since we do
18 * so many things inside these, here are the rules:
19 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
20 * 2. Do *not* perform any I/O with the client during these sections.
25 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
26 * requests that have not been confirmed will be deleted.
28 #define EXP 259200 /* three days */
40 #include <sys/types.h>
42 #if TIME_WITH_SYS_TIME
43 # include <sys/time.h>
47 # include <sys/time.h>
55 # if HAVE_SYS_SYSCALL_H
56 # include <sys/syscall.h>
63 #include <libcitadel.h>
66 #include "citserver.h"
72 #include "internet_addressing.h"
73 #include "serv_network.h"
74 #include "clientsocket.h"
75 #include "citadel_dirs.h"
78 #include "ctdl_module.h"
82 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName);
84 void aggregate_recipients(StrBuf **recps, RoomNetCfg Which, OneRoomNetCfg *OneRNCfg, long nSegments)
89 struct CitContext *CCC = CC;
93 * Figure out how big a buffer we need to allocate
95 for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
96 recps_len = recps_len + StrLength(nptr->Value[0]) + 2;
103 *recps = NewStrBufPlain(NULL, recps_len);
105 if (*recps == NULL) {
107 "Cannot allocate %ld bytes for recps...\n",
113 for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
114 if (nptr != OneRNCfg->NetConfigs[Which]) {
115 for (i = 0; i < nSegments; i++)
116 StrBufAppendBufPlain(*recps, HKEY(","), i);
118 StrBufAppendBuf(*recps, nptr->Value[0], 0);
119 if (Which == ignet_push_share)
121 StrBufAppendBufPlain(*recps, HKEY(","), 0);
122 StrBufAppendBuf(*recps, nptr->Value[1], 0);
128 static void ListCalculateSubject(struct CtdlMessage *msg)
130 struct CitContext *CCC = CC;
131 StrBuf *Subject, *FlatSubject;
135 if (CM_IsEmpty(msg, eMsgSubject)) {
136 Subject = NewStrBufPlain(HKEY("(no subject)"));
139 Subject = NewStrBufPlain(
140 msg->cm_fields[eMsgSubject], -1);
142 FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
143 StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
145 rlen = strlen(CCC->room.QRname);
146 pCh = strstr(ChrPtr(FlatSubject), CCC->room.QRname);
148 (*(pCh + rlen) != ']') ||
149 (pCh == ChrPtr(FlatSubject)) ||
154 StrBufPlain(Subject, HKEY("["));
155 StrBufAppendBufPlain(Subject,
158 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
159 StrBufAppendBuf(Subject, FlatSubject, 0);
160 /* so we can free the right one swap them */
162 Subject = FlatSubject;
164 StrBufRFC2047encode(&Subject, FlatSubject);
167 CM_SetAsFieldSB(msg, eMsgSubject, &Subject);
169 FreeStrBuf(&FlatSubject);
173 * Deliver digest messages
175 void network_deliver_digest(SpoolControl *sc)
177 struct CitContext *CCC = CC;
181 struct CtdlMessage *msg = NULL;
186 if (sc->Users[listrecp] == NULL)
189 if (sc->num_msgs_spooled < 1) {
190 fclose(sc->digestfp);
195 msg = malloc(sizeof(struct CtdlMessage));
196 memset(msg, 0, sizeof(struct CtdlMessage));
197 msg->cm_magic = CTDLMESSAGE_MAGIC;
198 msg->cm_format_type = FMT_RFC822;
199 msg->cm_anon_type = MES_NORMAL;
201 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
202 CM_SetField(msg, eAuthor, CCC->room.QRname, strlen(CCC->room.QRname));
203 len = snprintf(buf, sizeof buf, "[%s]", CCC->room.QRname);
204 CM_SetField(msg, eMsgSubject, buf, len);
206 CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
207 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
209 /* Set the 'List-ID' header */
210 CM_SetField(msg, eListID, SKEY(sc->ListID));
213 * Go fetch the contents of the digest
215 fseek(sc->digestfp, 0L, SEEK_END);
216 msglen = ftell(sc->digestfp);
218 pbuf = malloc(msglen + 1);
219 fseek(sc->digestfp, 0L, SEEK_SET);
220 fread(pbuf, (size_t)msglen, 1, sc->digestfp);
222 CM_SetAsField(msg, eMesageText, &pbuf, msglen);
223 fclose(sc->digestfp);
226 /* Now generate the delivery instructions */
227 if (sc->Users[listrecp] == NULL)
230 /* Where do we want bounces and other noise to be heard?
231 *Surely not the list members! */
232 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
234 /* Now submit the message */
235 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
237 valid->bounce_to = strdup(bounce_to);
238 valid->envelope_from = strdup(bounce_to);
239 CtdlSubmitMsg(msg, valid, NULL, 0);
242 free_recipients(valid);
246 void network_process_digest(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
249 struct CtdlMessage *msg = NULL;
252 * Process digest recipients
254 if ((sc->Users[digestrecp] == NULL)||
255 (sc->digestfp == NULL))
258 msg = CM_Duplicate(omsg);
260 fprintf(sc->digestfp,
261 " -----------------------------------"
262 "------------------------------------"
264 fprintf(sc->digestfp, "From: ");
265 if (!CM_IsEmpty(msg, eAuthor)) {
266 fprintf(sc->digestfp,
268 msg->cm_fields[eAuthor]);
270 if (!CM_IsEmpty(msg, erFc822Addr)) {
271 fprintf(sc->digestfp,
273 msg->cm_fields[erFc822Addr]);
275 else if (!CM_IsEmpty(msg, eNodeName)) {
276 fprintf(sc->digestfp,
278 msg->cm_fields[eNodeName]);
280 fprintf(sc->digestfp, "\n");
281 if (!CM_IsEmpty(msg, eMsgSubject)) {
282 fprintf(sc->digestfp,
284 msg->cm_fields[eMsgSubject]);
287 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
289 safestrncpy(CC->preferred_formats,
291 sizeof CC->preferred_formats);
293 CtdlOutputPreLoadedMsg(msg,
298 StrBufTrim(CC->redirect_buffer);
299 fwrite(HKEY("\n"), 1, sc->digestfp);
300 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
301 fwrite(HKEY("\n"), 1, sc->digestfp);
303 FreeStrBuf(&CC->redirect_buffer);
305 sc->num_msgs_spooled += 1;
311 void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
313 struct CtdlMessage *msg = NULL;
316 * Process mailing list recipients
318 if (sc->Users[listrecp] == NULL)
321 /* create our own copy of the message.
322 * We're going to need to modify it
323 * in order to insert the [list name] in it, etc.
326 msg = CM_Duplicate(omsg);
329 CM_SetField(msg, eListID, SKEY(sc->Users[roommailalias]));
331 /* if there is no other recipient, Set the recipient
332 * of the list message to the email address of the
335 if (CM_IsEmpty(msg, eRecipient))
337 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
340 /* Set the 'List-ID' header */
341 CM_SetField(msg, eListID, SKEY(sc->ListID));
344 /* Prepend "[List name]" to the subject */
345 ListCalculateSubject(msg);
347 /* Handle delivery */
348 network_deliver_list(msg, sc, CC->room.QRname);
353 * Deliver list messages to everyone on the list ... efficiently
355 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
360 /* Don't do this if there were no recipients! */
361 if (sc->Users[listrecp] == NULL)
364 /* Now generate the delivery instructions */
366 /* Where do we want bounces and other noise to be heard?
367 * Surely not the list members! */
368 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
370 /* Now submit the message */
371 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
373 valid->bounce_to = strdup(bounce_to);
374 valid->envelope_from = strdup(bounce_to);
375 valid->sending_room = strdup(RoomName);
376 CtdlSubmitMsg(msg, valid, NULL, 0);
377 free_recipients(valid);
379 /* Do not call CM_Free(msg) here; the caller will free it. */
383 void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
385 struct CtdlMessage *msg = NULL;
386 int ok_to_participate = 0;
391 * Process client-side list participations for this room
393 if (sc->Users[participate] == NULL)
396 msg = CM_Duplicate(omsg);
398 /* Only send messages which originated on our own
399 * Citadel network, otherwise we'll end up sending the
400 * remote mailing list's messages back to it, which
403 ok_to_participate = 0;
404 if (!CM_IsEmpty(msg, eNodeName)) {
405 if (!strcasecmp(msg->cm_fields[eNodeName],
406 config.c_nodename)) {
407 ok_to_participate = 1;
410 Buf = NewStrBufPlain(msg->cm_fields[eNodeName], -1);
411 if (CtdlIsValidNode(NULL,
414 sc->working_ignetcfg,
415 sc->the_netmap) == 0)
417 ok_to_participate = 1;
420 if (ok_to_participate)
422 /* Replace the Internet email address of the
423 * actual author with the email address of the
424 * room itself, so the remote listserv doesn't
427 CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
429 valid = validate_recipients(ChrPtr(sc->Users[participate]) , NULL, 0);
431 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
432 CtdlSubmitMsg(msg, valid, "", 0);
433 free_recipients(valid);
439 void network_process_ignetpush(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
443 const char *Pos = NULL;
444 struct CtdlMessage *msg = NULL;
445 struct CitContext *CCC = CC;
446 struct ser_ret sermsg;
448 char filename[PATH_MAX];
455 if (sc->Users[ignet_push_share] == NULL)
459 * Process IGnet push shares
461 msg = CM_Duplicate(omsg);
463 /* Prepend our node name to the Path field whenever
464 * sending a message to another IGnet node
466 Netmap_AddMe(msg, HKEY("username"));
469 * Determine if this message is set to be deleted
470 * after sending out on the network
472 if (!CM_IsEmpty(msg, eSpecialField)) {
473 if (!strcasecmp(msg->cm_fields[eSpecialField], "CANCEL")) {
474 *delete_after_send = 1;
478 /* Now send it to every node */
479 Recipient = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
480 RemoteRoom = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
481 while ((Pos != StrBufNOTNULL) &&
482 StrBufExtract_NextToken(Recipient, sc->Users[ignet_push_share], &Pos, ','))
484 StrBufExtract_NextToken(RemoteRoom, sc->Users[ignet_push_share], &Pos, ',');
486 NewStrBufDupAppendFlush(&Buf, Recipient, NULL, 1);
488 /* Check for valid node name */
489 if (CtdlIsValidNode(NULL,
492 sc->working_ignetcfg,
493 sc->the_netmap) != 0)
496 "Invalid node <%s>\n",
502 /* Check for split horizon */
503 QN_syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields[eMessagePath]);
504 bang = num_tokens(msg->cm_fields[eMessagePath], '!');
506 for (i=0; i<(bang-1); ++i) {
508 msg->cm_fields[eMessagePath],
512 QN_syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
513 buf, ChrPtr(Recipient)) ;
514 if (!strcasecmp(buf, ChrPtr(Recipient))) {
521 " %sSending to %s\n",
526 /* Send the message */
530 * Force the message to appear in the correct
531 * room on the far end by setting the C field
534 if (StrLength(RemoteRoom) > 0) {
535 CM_SetField(msg, eRemoteRoom, SKEY(RemoteRoom));
538 CM_SetField(msg, eRemoteRoom, CCC->room.QRname, strlen(CCC->room.QRname));
541 /* serialize it for transmission */
542 CtdlSerializeMessage(&sermsg, msg);
543 if (sermsg.len > 0) {
545 /* write it to a spool file */
559 fp = fopen(filename, "ab");
572 /* free the serialized version */
578 FreeStrBuf(&Recipient);
579 FreeStrBuf(&RemoteRoom);
585 * Spools out one message from the list.
587 void network_spool_msg(long msgnum,
590 struct CitContext *CCC = CC;
591 struct CtdlMessage *msg = NULL;
592 long delete_after_send = 0; /* Set to 1 to delete after spooling */
595 sc = (SpoolControl *)userdata;
597 msg = CtdlFetchMessage(msgnum, 1);
602 "failed to load Message <%ld> from disk\n",
606 network_process_list(sc, msg, &delete_after_send);
607 network_process_digest(sc, msg, &delete_after_send);
608 network_process_participate(sc, msg, &delete_after_send);
609 network_process_ignetpush(sc, msg, &delete_after_send);
613 /* update lastsent */
614 sc->lastsent = msgnum;
616 /* Delete this message if delete-after-send is set */
617 if (delete_after_send) {
618 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");