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(CM_KEY(msg, eMsgSubject));
141 FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
142 StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
144 rlen = strlen(CCC->room.QRname);
145 pCh = strstr(ChrPtr(FlatSubject), CCC->room.QRname);
147 (*(pCh + rlen) != ']') ||
148 (pCh == ChrPtr(FlatSubject)) ||
153 StrBufPlain(Subject, HKEY("["));
154 StrBufAppendBufPlain(Subject,
157 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
158 StrBufAppendBuf(Subject, FlatSubject, 0);
159 /* so we can free the right one swap them */
161 Subject = FlatSubject;
163 StrBufRFC2047encode(&Subject, FlatSubject);
166 CM_SetAsFieldSB(msg, eMsgSubject, &Subject);
168 FreeStrBuf(&FlatSubject);
172 * Deliver digest messages
174 void network_deliver_digest(SpoolControl *sc)
176 struct CitContext *CCC = CC;
180 struct CtdlMessage *msg = NULL;
185 if (sc->Users[listrecp] == NULL)
188 if (sc->num_msgs_spooled < 1) {
189 fclose(sc->digestfp);
194 msg = malloc(sizeof(struct CtdlMessage));
195 memset(msg, 0, sizeof(struct CtdlMessage));
196 msg->cm_magic = CTDLMESSAGE_MAGIC;
197 msg->cm_format_type = FMT_RFC822;
198 msg->cm_anon_type = MES_NORMAL;
200 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
201 CM_SetField(msg, eAuthor, CCC->room.QRname, strlen(CCC->room.QRname));
202 len = snprintf(buf, sizeof buf, "[%s]", CCC->room.QRname);
203 CM_SetField(msg, eMsgSubject, buf, len);
205 CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
206 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
208 /* Set the 'List-ID' header */
209 CM_SetField(msg, eListID, SKEY(sc->ListID));
212 * Go fetch the contents of the digest
214 fseek(sc->digestfp, 0L, SEEK_END);
215 msglen = ftell(sc->digestfp);
217 pbuf = malloc(msglen + 1);
218 fseek(sc->digestfp, 0L, SEEK_SET);
219 fread(pbuf, (size_t)msglen, 1, sc->digestfp);
221 CM_SetAsField(msg, eMesageText, &pbuf, msglen);
222 fclose(sc->digestfp);
225 /* Now generate the delivery instructions */
226 if (sc->Users[listrecp] == NULL)
229 /* Where do we want bounces and other noise to be heard?
230 *Surely not the list members! */
231 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
233 /* Now submit the message */
234 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
236 valid->bounce_to = strdup(bounce_to);
237 valid->envelope_from = strdup(bounce_to);
238 CtdlSubmitMsg(msg, valid, NULL, 0);
241 free_recipients(valid);
245 void network_process_digest(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
248 struct CtdlMessage *msg = NULL;
251 * Process digest recipients
253 if ((sc->Users[digestrecp] == NULL)||
254 (sc->digestfp == NULL))
257 msg = CM_Duplicate(omsg);
259 fprintf(sc->digestfp,
260 " -----------------------------------"
261 "------------------------------------"
263 fprintf(sc->digestfp, "From: ");
264 if (!CM_IsEmpty(msg, eAuthor)) {
265 fprintf(sc->digestfp,
267 msg->cm_fields[eAuthor]);
269 if (!CM_IsEmpty(msg, erFc822Addr)) {
270 fprintf(sc->digestfp,
272 msg->cm_fields[erFc822Addr]);
274 else if (!CM_IsEmpty(msg, eNodeName)) {
275 fprintf(sc->digestfp,
277 msg->cm_fields[eNodeName]);
279 fprintf(sc->digestfp, "\n");
280 if (!CM_IsEmpty(msg, eMsgSubject)) {
281 fprintf(sc->digestfp,
283 msg->cm_fields[eMsgSubject]);
286 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
288 safestrncpy(CC->preferred_formats,
290 sizeof CC->preferred_formats);
292 CtdlOutputPreLoadedMsg(msg,
297 StrBufTrim(CC->redirect_buffer);
298 fwrite(HKEY("\n"), 1, sc->digestfp);
299 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
300 fwrite(HKEY("\n"), 1, sc->digestfp);
302 FreeStrBuf(&CC->redirect_buffer);
304 sc->num_msgs_spooled += 1;
310 void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
312 struct CtdlMessage *msg = NULL;
315 * Process mailing list recipients
317 if (sc->Users[listrecp] == NULL)
320 /* create our own copy of the message.
321 * We're going to need to modify it
322 * in order to insert the [list name] in it, etc.
325 msg = CM_Duplicate(omsg);
328 CM_SetField(msg, eListID, SKEY(sc->Users[roommailalias]));
330 /* if there is no other recipient, Set the recipient
331 * of the list message to the email address of the
334 if (CM_IsEmpty(msg, eRecipient))
336 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
339 /* Set the 'List-ID' header */
340 CM_SetField(msg, eListID, SKEY(sc->ListID));
343 /* Prepend "[List name]" to the subject */
344 ListCalculateSubject(msg);
346 /* Handle delivery */
347 network_deliver_list(msg, sc, CC->room.QRname);
352 * Deliver list messages to everyone on the list ... efficiently
354 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
359 /* Don't do this if there were no recipients! */
360 if (sc->Users[listrecp] == NULL)
363 /* Now generate the delivery instructions */
365 /* Where do we want bounces and other noise to be heard?
366 * Surely not the list members! */
367 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
369 /* Now submit the message */
370 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
372 valid->bounce_to = strdup(bounce_to);
373 valid->envelope_from = strdup(bounce_to);
374 valid->sending_room = strdup(RoomName);
375 CtdlSubmitMsg(msg, valid, NULL, 0);
376 free_recipients(valid);
378 /* Do not call CM_Free(msg) here; the caller will free it. */
382 void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
384 struct CtdlMessage *msg = NULL;
385 int ok_to_participate = 0;
390 * Process client-side list participations for this room
392 if (sc->Users[participate] == NULL)
395 msg = CM_Duplicate(omsg);
397 /* Only send messages which originated on our own
398 * Citadel network, otherwise we'll end up sending the
399 * remote mailing list's messages back to it, which
402 ok_to_participate = 0;
403 if (!CM_IsEmpty(msg, eNodeName)) {
404 if (!strcasecmp(msg->cm_fields[eNodeName],
405 config.c_nodename)) {
406 ok_to_participate = 1;
409 Buf = NewStrBufPlain(CM_KEY(msg, eNodeName));
410 if (CtdlIsValidNode(NULL,
413 sc->working_ignetcfg,
414 sc->the_netmap) == 0)
416 ok_to_participate = 1;
419 if (ok_to_participate)
421 /* Replace the Internet email address of the
422 * actual author with the email address of the
423 * room itself, so the remote listserv doesn't
426 CM_SetField(msg, erFc822Addr, SKEY(sc->Users[roommailalias]));
428 valid = validate_recipients(ChrPtr(sc->Users[participate]) , NULL, 0);
430 CM_SetField(msg, eRecipient, SKEY(sc->Users[roommailalias]));
431 CtdlSubmitMsg(msg, valid, "", 0);
432 free_recipients(valid);
438 void network_process_ignetpush(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
442 const char *Pos = NULL;
443 struct CtdlMessage *msg = NULL;
444 struct CitContext *CCC = CC;
445 struct ser_ret sermsg;
447 char filename[PATH_MAX];
454 if (sc->Users[ignet_push_share] == NULL)
458 * Process IGnet push shares
460 msg = CM_Duplicate(omsg);
462 /* Prepend our node name to the Path field whenever
463 * sending a message to another IGnet node
465 Netmap_AddMe(msg, HKEY("username"));
468 * Determine if this message is set to be deleted
469 * after sending out on the network
471 if (!CM_IsEmpty(msg, eSpecialField)) {
472 if (!strcasecmp(msg->cm_fields[eSpecialField], "CANCEL")) {
473 *delete_after_send = 1;
477 /* Now send it to every node */
478 Recipient = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
479 RemoteRoom = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
480 while ((Pos != StrBufNOTNULL) &&
481 StrBufExtract_NextToken(Recipient, sc->Users[ignet_push_share], &Pos, ','))
483 StrBufExtract_NextToken(RemoteRoom, sc->Users[ignet_push_share], &Pos, ',');
485 NewStrBufDupAppendFlush(&Buf, Recipient, NULL, 1);
487 /* Check for valid node name */
488 if (CtdlIsValidNode(NULL,
491 sc->working_ignetcfg,
492 sc->the_netmap) != 0)
495 "Invalid node <%s>\n",
501 /* Check for split horizon */
502 QN_syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields[eMessagePath]);
503 bang = num_tokens(msg->cm_fields[eMessagePath], '!');
505 for (i=0; i<(bang-1); ++i) {
507 msg->cm_fields[eMessagePath],
511 QN_syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
512 buf, ChrPtr(Recipient)) ;
513 if (!strcasecmp(buf, ChrPtr(Recipient))) {
520 " %sSending to %s\n",
525 /* Send the message */
529 * Force the message to appear in the correct
530 * room on the far end by setting the C field
533 if (StrLength(RemoteRoom) > 0) {
534 CM_SetField(msg, eRemoteRoom, SKEY(RemoteRoom));
537 CM_SetField(msg, eRemoteRoom, CCC->room.QRname, strlen(CCC->room.QRname));
540 /* serialize it for transmission */
541 CtdlSerializeMessage(&sermsg, msg);
542 if (sermsg.len > 0) {
544 /* write it to a spool file */
558 fp = fopen(filename, "ab");
571 /* free the serialized version */
577 FreeStrBuf(&Recipient);
578 FreeStrBuf(&RemoteRoom);
584 * Spools out one message from the list.
586 void network_spool_msg(long msgnum,
589 struct CitContext *CCC = CC;
590 struct CtdlMessage *msg = NULL;
591 long delete_after_send = 0; /* Set to 1 to delete after spooling */
594 sc = (SpoolControl *)userdata;
596 msg = CtdlFetchMessage(msgnum, 1);
601 "failed to load Message <%ld> from disk\n",
605 network_process_list(sc, msg, &delete_after_send);
606 network_process_digest(sc, msg, &delete_after_send);
607 network_process_participate(sc, msg, &delete_after_send);
608 network_process_ignetpush(sc, msg, &delete_after_send);
612 /* update lastsent */
613 sc->lastsent = msgnum;
615 /* Delete this message if delete-after-send is set */
616 if (delete_after_send) {
617 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");