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"
76 #include "citadel_dirs.h"
79 #include "ctdl_module.h"
83 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName);
85 void aggregate_recipients(StrBuf **recps, RoomNetCfg Which, OneRoomNetCfg *OneRNCfg)
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 StrBufAppendBufPlain(*recps, HKEY(","), 0);
117 StrBufAppendBuf(*recps, nptr->Value[0], 0);
122 * Deliver digest messages
124 void network_deliver_digest(SpoolControl *sc)
128 struct CtdlMessage *msg = NULL;
130 StrBuf *recps = NULL;
131 struct recptypes *valid;
134 if (sc->Users[listrecp] == NULL)
137 if (sc->num_msgs_spooled < 1) {
138 fclose(sc->digestfp);
143 msg = malloc(sizeof(struct CtdlMessage));
144 memset(msg, 0, sizeof(struct CtdlMessage));
145 msg->cm_magic = CTDLMESSAGE_MAGIC;
146 msg->cm_format_type = FMT_RFC822;
147 msg->cm_anon_type = MES_NORMAL;
149 sprintf(buf, "%ld", time(NULL));
150 msg->cm_fields['T'] = strdup(buf);
151 msg->cm_fields['A'] = strdup(CC->room.QRname);
152 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
153 msg->cm_fields['U'] = strdup(buf);
154 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
155 for (i=0; buf[i]; ++i) {
156 if (isspace(buf[i])) buf[i]='_';
157 buf[i] = tolower(buf[i]);
159 msg->cm_fields['F'] = strdup(buf);
160 msg->cm_fields['R'] = strdup(buf);
162 /* Set the 'List-ID' header */
163 msg->cm_fields['L'] = malloc(1024);
164 snprintf(msg->cm_fields['L'], 1024,
165 "%s <%ld.list-id.%s>",
172 * Go fetch the contents of the digest
174 fseek(sc->digestfp, 0L, SEEK_END);
175 msglen = ftell(sc->digestfp);
177 msg->cm_fields['M'] = malloc(msglen + 1);
178 fseek(sc->digestfp, 0L, SEEK_SET);
179 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
180 msg->cm_fields['M'][msglen] = '\0';
182 fclose(sc->digestfp);
185 /* Now generate the delivery instructions */
186 if (sc->Users[listrecp] == NULL)
188 aggregate_recipients(&recps, digestrecp, sc->RNCfg);
190 /* Where do we want bounces and other noise to be heard?
191 *Surely not the list members! */
192 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
194 /* Now submit the message */
195 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
197 valid->bounce_to = strdup(bounce_to);
198 valid->envelope_from = strdup(bounce_to);
199 CtdlSubmitMsg(msg, valid, NULL, 0);
201 CtdlFreeMessage(msg);
202 free_recipients(valid);
206 void network_process_digest(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
209 struct CtdlMessage *msg = NULL;
212 * Process digest recipients
214 if ((sc->Users[digestrecp] == NULL)||
215 (sc->digestfp == NULL))
218 msg = CtdlDuplicateMessage(omsg);
220 fprintf(sc->digestfp,
221 " -----------------------------------"
222 "------------------------------------"
224 fprintf(sc->digestfp, "From: ");
225 if (msg->cm_fields['A'] != NULL) {
226 fprintf(sc->digestfp,
228 msg->cm_fields['A']);
230 if (msg->cm_fields['F'] != NULL) {
231 fprintf(sc->digestfp,
233 msg->cm_fields['F']);
235 else if (msg->cm_fields['N'] != NULL) {
236 fprintf(sc->digestfp,
238 msg->cm_fields['N']);
240 fprintf(sc->digestfp, "\n");
241 if (msg->cm_fields['U'] != NULL) {
242 fprintf(sc->digestfp,
244 msg->cm_fields['U']);
247 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
249 safestrncpy(CC->preferred_formats,
251 sizeof CC->preferred_formats);
253 CtdlOutputPreLoadedMsg(msg,
258 StrBufTrim(CC->redirect_buffer);
259 fwrite(HKEY("\n"), 1, sc->digestfp);
260 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
261 fwrite(HKEY("\n"), 1, sc->digestfp);
263 FreeStrBuf(&CC->redirect_buffer);
265 sc->num_msgs_spooled += 1;
266 CtdlFreeMessage(msg);
271 void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
273 struct CitContext *CCC = CC;
276 StrBuf *Subject, *FlatSubject;
277 struct CtdlMessage *msg = NULL;
281 * Process mailing list recipients
283 if (sc->Users[listrecp] == NULL)
286 /* create our own copy of the message.
287 * We're going to need to modify it
288 * in order to insert the [list name] in it, etc.
291 msg = CtdlDuplicateMessage(omsg);
293 if (msg->cm_fields['K'] != NULL)
294 free(msg->cm_fields['K']);
295 if (msg->cm_fields['V'] == NULL){
296 /* local message, no enVelope */
299 StrBufAppendBufPlain(Buf,
302 StrBufAppendBufPlain(Buf, HKEY("@"), 0);
303 StrBufAppendBufPlain(Buf, config.c_fqdn, -1, 0);
305 msg->cm_fields['K'] = SmashStrBuf(&Buf);
308 msg->cm_fields['K'] =
309 strdup (msg->cm_fields['V']);
311 /* Set the 'List-ID' header */
312 if (msg->cm_fields['L'] != NULL) {
313 free(msg->cm_fields['L']);
315 msg->cm_fields['L'] = malloc(1024);
316 snprintf(msg->cm_fields['L'], 1024,
317 "%s <%ld.list-id.%s>",
323 /* Prepend "[List name]" to the subject */
324 if (msg->cm_fields['U'] == NULL) {
325 Subject = NewStrBufPlain(HKEY("(no subject)"));
328 Subject = NewStrBufPlain(
329 msg->cm_fields['U'], -1);
331 FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
332 StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
334 rlen = strlen(CCC->room.QRname);
335 pCh = strstr(ChrPtr(FlatSubject), CCC->room.QRname);
337 (*(pCh + rlen) != ']') ||
338 (pCh == ChrPtr(FlatSubject)) ||
343 StrBufPlain(Subject, HKEY("["));
344 StrBufAppendBufPlain(Subject,
347 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
348 StrBufAppendBuf(Subject, FlatSubject, 0);
349 /* so we can free the right one swap them */
351 Subject = FlatSubject;
353 StrBufRFC2047encode(&Subject, FlatSubject);
356 if (msg->cm_fields['U'] != NULL)
357 free (msg->cm_fields['U']);
358 msg->cm_fields['U'] = SmashStrBuf(&Subject);
360 FreeStrBuf(&FlatSubject);
362 /* else we won't modify the buffer, since the
363 * roomname is already here.
366 /* if there is no other recipient, Set the recipient
367 * of the list message to the email address of the
370 if ((msg->cm_fields['R'] == NULL) ||
371 IsEmptyStr(msg->cm_fields['R']))
373 if (msg->cm_fields['R'] != NULL)
374 free(msg->cm_fields['R']);
376 msg->cm_fields['R'] = malloc(256);
377 snprintf(msg->cm_fields['R'], 256,
378 "room_%s@%s", CCC->room.QRname,
380 for (i=0; msg->cm_fields['R'][i]; ++i) {
381 if (isspace(msg->cm_fields['R'][i])) {
382 msg->cm_fields['R'][i] = '_';
387 /* Handle delivery */
388 network_deliver_list(msg, sc, CCC->room.QRname);
389 CtdlFreeMessage(msg);
393 * Deliver list messages to everyone on the list ... efficiently
395 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
397 struct recptypes *valid;
400 /* Don't do this if there were no recipients! */
401 if (sc->Users[listrecp] == NULL)
404 /* Now generate the delivery instructions */
406 /* Where do we want bounces and other noise to be heard?
407 * Surely not the list members! */
408 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
410 /* Now submit the message */
411 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
413 valid->bounce_to = strdup(bounce_to);
414 valid->envelope_from = strdup(bounce_to);
415 valid->sending_room = strdup(RoomName);
416 CtdlSubmitMsg(msg, valid, NULL, 0);
417 free_recipients(valid);
419 /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
423 void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
425 struct CtdlMessage *msg = NULL;
427 int ok_to_participate = 0;
429 struct recptypes *valid;
432 * Process client-side list participations for this room
434 if (sc->Users[participate] == NULL)
437 msg = CtdlDuplicateMessage(omsg);
439 /* Only send messages which originated on our own
440 * Citadel network, otherwise we'll end up sending the
441 * remote mailing list's messages back to it, which
444 ok_to_participate = 0;
445 if (msg->cm_fields['N'] != NULL) {
446 if (!strcasecmp(msg->cm_fields['N'],
447 config.c_nodename)) {
448 ok_to_participate = 1;
451 Buf = NewStrBufPlain(msg->cm_fields['N'], -1);
452 if (CtdlIsValidNode(NULL,
455 sc->working_ignetcfg,
456 sc->the_netmap) == 0)
458 ok_to_participate = 1;
461 if (ok_to_participate)
463 if (msg->cm_fields['F'] != NULL) {
464 free(msg->cm_fields['F']);
466 msg->cm_fields['F'] = malloc(SIZ);
467 /* Replace the Internet email address of the
468 * actual author with the email address of the
469 * room itself, so the remote listserv doesn't
472 if (sc->Users[roommailalias] != NULL)
473 snprintf(msg->cm_fields['F'], SIZ,
474 "%s", ChrPtr(sc->Users[roommailalias]));
476 snprintf(msg->cm_fields['F'], SIZ,
477 "room_%s@%s", CC->room.QRname,
480 for (i=0; msg->cm_fields['F'][i]; ++i) {
481 if (isspace(msg->cm_fields['F'][i])) {
482 msg->cm_fields['F'][i] = '_';
486 valid = validate_recipients(ChrPtr(sc->Users[participate]) , NULL, 0);
488 if (msg->cm_fields['R'] != NULL) {
489 free(msg->cm_fields['R']);
490 }/* TODO: check whether 'R' is set appropriate later. */
492 CtdlSubmitMsg(msg, valid, "", 0);
493 free_recipients(valid);
496 CtdlFreeMessage(msg);
499 void network_process_ignetpush(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
502 const char *Pos = NULL;
503 struct CtdlMessage *msg = NULL;
504 struct CitContext *CCC = CC;
505 struct ser_ret sermsg;
507 char filename[PATH_MAX];
510 char *newpath = NULL;
516 if (sc->Users[ignet_push_share] == NULL)
520 * Process IGnet push shares
522 msg = CtdlDuplicateMessage(omsg);
524 /* Prepend our node name to the Path field whenever
525 * sending a message to another IGnet node
527 if (msg->cm_fields['P'] == NULL)
529 msg->cm_fields['P'] = strdup("username");
531 newpath_len = strlen(msg->cm_fields['P']) +
532 strlen(config.c_nodename) + 2;
533 newpath = malloc(newpath_len);
534 snprintf(newpath, newpath_len, "%s!%s",
535 config.c_nodename, msg->cm_fields['P']);
536 free(msg->cm_fields['P']);
537 msg->cm_fields['P'] = newpath;
540 * Determine if this message is set to be deleted
541 * after sending out on the network
543 if (msg->cm_fields['S'] != NULL) {
544 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
545 *delete_after_send = 1;
549 /* Now send it to every node */
550 Recipient = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
551 while ((Pos != StrBufNOTNULL) &&
552 StrBufExtract_NextToken(Recipient, sc->Users[ignet_push_share], &Pos, '|'))
555 NewStrBufDupAppendFlush(&Buf, Recipient, NULL, 1);
557 /* Check for valid node name */
558 if (CtdlIsValidNode(NULL,
561 sc->working_ignetcfg,
562 sc->the_netmap) != 0)
565 "Invalid node <%s>\n",
571 /* Check for split horizon */
572 QN_syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields['P']);
573 bang = num_tokens(msg->cm_fields['P'], '!');
575 for (i=0; i<(bang-1); ++i) {
581 QN_syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
582 buf, ChrPtr(Recipient)) ;
583 if (!strcasecmp(buf, ChrPtr(Recipient))) {
595 /* Send the message */
599 * Force the message to appear in the correct
600 * room on the far end by setting the C field
603 if (msg->cm_fields['C'] != NULL) {
604 free(msg->cm_fields['C']);
606 if (StrLength(Recipient) > 0) {
607 msg->cm_fields['C'] =
608 strdup(ChrPtr(Recipient));
611 msg->cm_fields['C'] =
612 strdup(CC->room.QRname);
615 /* serialize it for transmission */
616 serialize_message(&sermsg, msg);
617 if (sermsg.len > 0) {
619 /* write it to a spool file */
633 fp = fopen(filename, "ab");
646 /* free the serialized version */
652 FreeStrBuf(&Recipient);
653 CtdlFreeMessage(msg);
658 * Spools out one message from the list.
660 void network_spool_msg(long msgnum,
663 struct CtdlMessage *msg = NULL;
664 long delete_after_send = 0; /* Set to 1 to delete after spooling */
667 sc = (SpoolControl *)userdata;
669 msg = CtdlFetchMessage(msgnum, 1);
671 network_process_list(sc, msg, &delete_after_send);
672 network_process_digest(sc, msg, &delete_after_send);
673 network_process_participate(sc, msg, &delete_after_send);
674 network_process_ignetpush(sc, msg, &delete_after_send);
676 CtdlFreeMessage(msg);
678 /* update lastsent */
679 sc->lastsent = msgnum;
681 /* Delete this message if delete-after-send is set */
682 if (delete_after_send) {
683 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");