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, long nSegments)
90 struct CitContext *CCC = CC;
94 * Figure out how big a buffer we need to allocate
96 for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
97 recps_len = recps_len + StrLength(nptr->Value[0]) + 2;
100 /* Nothing todo... */
104 *recps = NewStrBufPlain(NULL, recps_len);
106 if (*recps == NULL) {
108 "Cannot allocate %ld bytes for recps...\n",
114 for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
115 if (nptr != OneRNCfg->NetConfigs[Which]) {
116 for (i = 0; i < nSegments; i++)
117 StrBufAppendBufPlain(*recps, HKEY(","), i);
119 StrBufAppendBuf(*recps, nptr->Value[0], 0);
124 * Deliver digest messages
126 void network_deliver_digest(SpoolControl *sc)
130 struct CtdlMessage *msg = NULL;
132 struct recptypes *valid;
135 if (sc->Users[listrecp] == NULL)
138 if (sc->num_msgs_spooled < 1) {
139 fclose(sc->digestfp);
144 msg = malloc(sizeof(struct CtdlMessage));
145 memset(msg, 0, sizeof(struct CtdlMessage));
146 msg->cm_magic = CTDLMESSAGE_MAGIC;
147 msg->cm_format_type = FMT_RFC822;
148 msg->cm_anon_type = MES_NORMAL;
150 sprintf(buf, "%ld", time(NULL));
151 msg->cm_fields['T'] = strdup(buf);
152 msg->cm_fields['A'] = strdup(CC->room.QRname);
153 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
154 msg->cm_fields['U'] = strdup(buf);
155 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
156 for (i=0; buf[i]; ++i) {
157 if (isspace(buf[i])) buf[i]='_';
158 buf[i] = tolower(buf[i]);
160 msg->cm_fields['F'] = strdup(buf);
161 msg->cm_fields['R'] = strdup(buf);
163 /* Set the 'List-ID' header */
164 msg->cm_fields['L'] = malloc(1024);
165 snprintf(msg->cm_fields['L'], 1024,
166 "%s <%ld.list-id.%s>",
173 * Go fetch the contents of the digest
175 fseek(sc->digestfp, 0L, SEEK_END);
176 msglen = ftell(sc->digestfp);
178 msg->cm_fields['M'] = malloc(msglen + 1);
179 fseek(sc->digestfp, 0L, SEEK_SET);
180 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
181 msg->cm_fields['M'][msglen] = '\0';
183 fclose(sc->digestfp);
186 /* Now generate the delivery instructions */
187 if (sc->Users[listrecp] == NULL)
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)
503 const char *Pos = NULL;
504 struct CtdlMessage *msg = NULL;
505 struct CitContext *CCC = CC;
506 struct ser_ret sermsg;
508 char filename[PATH_MAX];
511 char *newpath = NULL;
517 if (sc->Users[ignet_push_share] == NULL)
521 * Process IGnet push shares
523 msg = CtdlDuplicateMessage(omsg);
525 /* Prepend our node name to the Path field whenever
526 * sending a message to another IGnet node
528 if (msg->cm_fields['P'] == NULL)
530 msg->cm_fields['P'] = strdup("username");
532 newpath_len = strlen(msg->cm_fields['P']) +
533 strlen(config.c_nodename) + 2;
534 newpath = malloc(newpath_len);
535 snprintf(newpath, newpath_len, "%s!%s",
536 config.c_nodename, msg->cm_fields['P']);
537 free(msg->cm_fields['P']);
538 msg->cm_fields['P'] = newpath;
541 * Determine if this message is set to be deleted
542 * after sending out on the network
544 if (msg->cm_fields['S'] != NULL) {
545 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
546 *delete_after_send = 1;
550 /* Now send it to every node */
551 Recipient = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
552 RemoteRoom = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
553 while ((Pos != StrBufNOTNULL) &&
554 StrBufExtract_NextToken(Recipient, sc->Users[ignet_push_share], &Pos, ','))
556 StrBufExtract_NextToken(RemoteRoom, sc->Users[ignet_push_share], &Pos, ',');
558 NewStrBufDupAppendFlush(&Buf, Recipient, NULL, 1);
560 /* Check for valid node name */
561 if (CtdlIsValidNode(NULL,
564 sc->working_ignetcfg,
565 sc->the_netmap) != 0)
568 "Invalid node <%s>\n",
574 /* Check for split horizon */
575 QN_syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields['P']);
576 bang = num_tokens(msg->cm_fields['P'], '!');
578 for (i=0; i<(bang-1); ++i) {
584 QN_syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
585 buf, ChrPtr(Recipient)) ;
586 if (!strcasecmp(buf, ChrPtr(Recipient))) {
598 /* Send the message */
602 * Force the message to appear in the correct
603 * room on the far end by setting the C field
606 if (msg->cm_fields['C'] != NULL) {
607 free(msg->cm_fields['C']);
609 if (StrLength(RemoteRoom) > 0) {
610 msg->cm_fields['C'] =
611 strdup(ChrPtr(RemoteRoom));
614 msg->cm_fields['C'] =
615 strdup(CC->room.QRname);
618 /* serialize it for transmission */
619 serialize_message(&sermsg, msg);
620 if (sermsg.len > 0) {
622 /* write it to a spool file */
636 fp = fopen(filename, "ab");
649 /* free the serialized version */
655 FreeStrBuf(&Recipient);
656 FreeStrBuf(&RemoteRoom);
657 CtdlFreeMessage(msg);
662 * Spools out one message from the list.
664 void network_spool_msg(long msgnum,
667 struct CtdlMessage *msg = NULL;
668 long delete_after_send = 0; /* Set to 1 to delete after spooling */
671 sc = (SpoolControl *)userdata;
673 msg = CtdlFetchMessage(msgnum, 1);
675 network_process_list(sc, msg, &delete_after_send);
676 network_process_digest(sc, msg, &delete_after_send);
677 network_process_participate(sc, msg, &delete_after_send);
678 network_process_ignetpush(sc, msg, &delete_after_send);
680 CtdlFreeMessage(msg);
682 /* update lastsent */
683 sc->lastsent = msgnum;
685 /* Delete this message if delete-after-send is set */
686 if (delete_after_send) {
687 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");