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"
85 * Deliver digest messages
87 void network_deliver_digest(SpoolControl *sc)
89 struct CitContext *CCC = CC;
92 struct CtdlMessage *msg = NULL;
96 size_t recps_len = SIZ;
97 struct recptypes *valid;
101 if (sc->num_msgs_spooled < 1) {
102 fclose(sc->digestfp);
107 msg = malloc(sizeof(struct CtdlMessage));
108 memset(msg, 0, sizeof(struct CtdlMessage));
109 msg->cm_magic = CTDLMESSAGE_MAGIC;
110 msg->cm_format_type = FMT_RFC822;
111 msg->cm_anon_type = MES_NORMAL;
113 sprintf(buf, "%ld", time(NULL));
114 msg->cm_fields['T'] = strdup(buf);
115 msg->cm_fields['A'] = strdup(CC->room.QRname);
116 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
117 msg->cm_fields['U'] = strdup(buf);
118 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
119 for (i=0; buf[i]; ++i) {
120 if (isspace(buf[i])) buf[i]='_';
121 buf[i] = tolower(buf[i]);
123 msg->cm_fields['F'] = strdup(buf);
124 msg->cm_fields['R'] = strdup(buf);
126 /* Set the 'List-ID' header */
127 msg->cm_fields['L'] = malloc(1024);
128 snprintf(msg->cm_fields['L'], 1024,
129 "%s <%ld.list-id.%s>",
136 * Go fetch the contents of the digest
138 fseek(sc->digestfp, 0L, SEEK_END);
139 msglen = ftell(sc->digestfp);
141 msg->cm_fields['M'] = malloc(msglen + 1);
142 fseek(sc->digestfp, 0L, SEEK_SET);
143 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
144 msg->cm_fields['M'][msglen] = '\0';
146 fclose(sc->digestfp);
149 /* Now generate the delivery instructions */
152 * Figure out how big a buffer we need to allocate
154 for (nptr = sc->RNCfg->NetConfigs[digestrecp]; nptr != NULL; nptr = nptr->next) {
155 recps_len = recps_len + StrLength(nptr->Value[0]) + 2;
158 recps = NewStrBufPlain(NULL, recps_len);
162 "Cannot allocate %ld bytes for recps...\n",
168 for (nptr = sc->RNCfg->NetConfigs[digestrecp]; nptr != NULL; nptr = nptr->next) {
169 if (nptr != sc->RNCfg->NetConfigs[digestrecp]) {
170 StrBufAppendBufPlain(recps, HKEY(","), 0);
172 StrBufAppendBuf(recps, nptr->Value[0], 0);
175 /* Where do we want bounces and other noise to be heard?
176 *Surely not the list members! */
177 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
179 /* Now submit the message */
180 precps = SmashStrBuf(&recps);
181 valid = validate_recipients(precps, NULL, 0);
184 valid->bounce_to = strdup(bounce_to);
185 valid->envelope_from = strdup(bounce_to);
186 CtdlSubmitMsg(msg, valid, NULL, 0);
188 CtdlFreeMessage(msg);
189 free_recipients(valid);
193 void network_process_digest(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
196 struct CtdlMessage *msg = NULL;
199 * Process digest recipients
201 if ((sc->RNCfg->NetConfigs[digestrecp] == NULL) ||
202 (sc->digestfp == NULL))
205 msg = CtdlDuplicateMessage(omsg);
207 fprintf(sc->digestfp,
208 " -----------------------------------"
209 "------------------------------------"
211 fprintf(sc->digestfp, "From: ");
212 if (msg->cm_fields['A'] != NULL) {
213 fprintf(sc->digestfp,
215 msg->cm_fields['A']);
217 if (msg->cm_fields['F'] != NULL) {
218 fprintf(sc->digestfp,
220 msg->cm_fields['F']);
222 else if (msg->cm_fields['N'] != NULL) {
223 fprintf(sc->digestfp,
225 msg->cm_fields['N']);
227 fprintf(sc->digestfp, "\n");
228 if (msg->cm_fields['U'] != NULL) {
229 fprintf(sc->digestfp,
231 msg->cm_fields['U']);
234 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
236 safestrncpy(CC->preferred_formats,
238 sizeof CC->preferred_formats);
240 CtdlOutputPreLoadedMsg(msg,
245 StrBufTrim(CC->redirect_buffer);
246 fwrite(HKEY("\n"), 1, sc->digestfp);
247 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
248 fwrite(HKEY("\n"), 1, sc->digestfp);
250 FreeStrBuf(&CC->redirect_buffer);
252 sc->num_msgs_spooled += 1;
253 CtdlFreeMessage(msg);
258 void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
262 StrBuf *Subject, *FlatSubject;
263 struct CtdlMessage *msg = NULL;
267 * Process mailing list recipients
269 if (sc->RNCfg->NetConfigs[listrecp] == NULL)
272 /* create our own copy of the message.
273 * We're going to need to modify it
274 * in order to insert the [list name] in it, etc.
277 msg = CtdlDuplicateMessage(omsg);
279 if (msg->cm_fields['K'] != NULL)
280 free(msg->cm_fields['K']);
281 if (msg->cm_fields['V'] == NULL){
282 /* local message, no enVelope */
285 StrBufAppendBufPlain(Buf,
288 StrBufAppendBufPlain(Buf, HKEY("@"), 0);
289 StrBufAppendBufPlain(Buf, config.c_fqdn, -1, 0);
291 msg->cm_fields['K'] = SmashStrBuf(&Buf);
294 msg->cm_fields['K'] =
295 strdup (msg->cm_fields['V']);
297 /* Set the 'List-ID' header */
298 if (msg->cm_fields['L'] != NULL) {
299 free(msg->cm_fields['L']);
301 msg->cm_fields['L'] = malloc(1024);
302 snprintf(msg->cm_fields['L'], 1024,
303 "%s <%ld.list-id.%s>",
309 /* Prepend "[List name]" to the subject */
310 if (msg->cm_fields['U'] == NULL) {
311 Subject = NewStrBufPlain(HKEY("(no subject)"));
314 Subject = NewStrBufPlain(
315 msg->cm_fields['U'], -1);
317 FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
318 StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
320 rlen = strlen(CC->room.QRname);
321 pCh = strstr(ChrPtr(FlatSubject), CC->room.QRname);
323 (*(pCh + rlen) != ']') ||
324 (pCh == ChrPtr(FlatSubject)) ||
329 StrBufPlain(Subject, HKEY("["));
330 StrBufAppendBufPlain(Subject,
333 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
334 StrBufAppendBuf(Subject, FlatSubject, 0);
335 /* so we can free the right one swap them */
337 Subject = FlatSubject;
339 StrBufRFC2047encode(&Subject, FlatSubject);
342 if (msg->cm_fields['U'] != NULL)
343 free (msg->cm_fields['U']);
344 msg->cm_fields['U'] = SmashStrBuf(&Subject);
346 FreeStrBuf(&FlatSubject);
348 /* else we won't modify the buffer, since the
349 * roomname is already here.
352 /* if there is no other recipient, Set the recipient
353 * of the list message to the email address of the
356 if ((msg->cm_fields['R'] == NULL) ||
357 IsEmptyStr(msg->cm_fields['R']))
359 if (msg->cm_fields['R'] != NULL)
360 free(msg->cm_fields['R']);
362 msg->cm_fields['R'] = malloc(256);
363 snprintf(msg->cm_fields['R'], 256,
364 "room_%s@%s", CC->room.QRname,
366 for (i=0; msg->cm_fields['R'][i]; ++i) {
367 if (isspace(msg->cm_fields['R'][i])) {
368 msg->cm_fields['R'][i] = '_';
373 /* Handle delivery */
374 // network_deliver_list(msg, sc, CC->room.QRname);
375 CtdlFreeMessage(msg);
379 * Deliver list messages to everyone on the list ... efficiently
381 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
383 struct CitContext *CCC = CC;
384 StrBuf *recps = NULL;
386 size_t recps_len = SIZ;
387 struct recptypes *valid;
388 RoomNetCfgLine *nptr;
391 /* Don't do this if there were no recipients! */
392 if (sc->RNCfg->NetConfigs[listrecp] == NULL) return;
394 /* Now generate the delivery instructions */
397 * Figure out how big a buffer we need to allocate
399 for (nptr = sc->RNCfg->NetConfigs[listrecp]; nptr != NULL; nptr = nptr->next) {
400 recps_len = recps_len + StrLength(nptr->Value[0]) + 2;
403 recps = NewStrBufPlain(NULL, recps_len);
407 "Cannot allocate %ld bytes for recps...\n",
413 for (nptr = sc->RNCfg->NetConfigs[listrecp]; nptr != NULL; nptr = nptr->next) {
414 if (nptr != sc->RNCfg->NetConfigs[listrecp]) {
415 StrBufAppendBufPlain(recps, HKEY(","), 0);
417 StrBufAppendBuf(recps, nptr->Value[0], 0);
420 /* Where do we want bounces and other noise to be heard?
421 * Surely not the list members! */
422 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
424 /* Now submit the message */
425 precps = SmashStrBuf(&recps);
426 valid = validate_recipients(precps, NULL, 0);
429 valid->bounce_to = strdup(bounce_to);
430 valid->envelope_from = strdup(bounce_to);
431 valid->sending_room = strdup(RoomName);
432 CtdlSubmitMsg(msg, valid, NULL, 0);
433 free_recipients(valid);
435 /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
439 void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
441 struct CtdlMessage *msg = NULL;
443 int ok_to_participate = 0;
445 RoomNetCfgLine *nptr;
446 struct recptypes *valid;
449 * Process client-side list participations for this room
451 if (sc->RNCfg->NetConfigs[participate] == NULL)
454 msg = CtdlDuplicateMessage(omsg);
456 /* Only send messages which originated on our own
457 * Citadel network, otherwise we'll end up sending the
458 * remote mailing list's messages back to it, which
461 ok_to_participate = 0;
462 if (msg->cm_fields['N'] != NULL) {
463 if (!strcasecmp(msg->cm_fields['N'],
464 config.c_nodename)) {
465 ok_to_participate = 1;
468 Buf = NewStrBufPlain(msg->cm_fields['N'], -1);
469 if (CtdlIsValidNode(NULL,
472 sc->working_ignetcfg,
473 sc->the_netmap) == 0)
475 ok_to_participate = 1;
478 if (ok_to_participate) {
479 if (msg->cm_fields['F'] != NULL) {
480 free(msg->cm_fields['F']);
482 msg->cm_fields['F'] = malloc(SIZ);
483 /* Replace the Internet email address of the
484 * actual author with the email address of the
485 * room itself, so the remote listserv doesn't
487 * FIXME I want to be able to pick any address
489 snprintf(msg->cm_fields['F'], SIZ,
490 "room_%s@%s", CC->room.QRname,
492 for (i=0; msg->cm_fields['F'][i]; ++i) {
493 if (isspace(msg->cm_fields['F'][i])) {
494 msg->cm_fields['F'][i] = '_';
499 * Figure out how big a buffer we need to alloc
501 for (nptr = sc->RNCfg->NetConfigs[participate];
505 if (msg->cm_fields['R'] != NULL) {
506 free(msg->cm_fields['R']);
508 msg->cm_fields['R'] =
509 strdup(ChrPtr(nptr->Value[0]));
511 valid = validate_recipients(msg->cm_fields['R'],
514 CtdlSubmitMsg(msg, valid, "", 0);
515 free_recipients(valid);
519 CtdlFreeMessage(msg);
522 void network_process_ignetpush(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
524 struct CtdlMessage *msg = NULL;
525 struct CitContext *CCC = CC;
526 struct ser_ret sermsg;
528 char filename[PATH_MAX];
531 char *newpath = NULL;
532 RoomNetCfgLine* mptr;
538 if (sc->RNCfg->NetConfigs[ignet_push_share] == NULL)
541 * Process IGnet push shares
543 msg = CtdlDuplicateMessage(omsg);
545 /* Prepend our node name to the Path field whenever
546 * sending a message to another IGnet node
548 if (msg->cm_fields['P'] == NULL)
550 msg->cm_fields['P'] = strdup("username");
552 newpath_len = strlen(msg->cm_fields['P']) +
553 strlen(config.c_nodename) + 2;
554 newpath = malloc(newpath_len);
555 snprintf(newpath, newpath_len, "%s!%s",
556 config.c_nodename, msg->cm_fields['P']);
557 free(msg->cm_fields['P']);
558 msg->cm_fields['P'] = newpath;
561 * Determine if this message is set to be deleted
562 * after sending out on the network
564 if (msg->cm_fields['S'] != NULL) {
565 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
566 *delete_after_send = 1;
570 /* Now send it to every node */
571 for (mptr = sc->RNCfg->NetConfigs[ignet_push_share];
576 NewStrBufDupAppendFlush(&Buf, mptr->Value[0], NULL, 1);
578 /* Check for valid node name */
579 if (CtdlIsValidNode(NULL,
582 sc->working_ignetcfg,
583 sc->the_netmap) != 0)
586 "Invalid node <%s>\n",
587 ChrPtr(mptr->Value[0]));
592 /* Check for split horizon */
593 QN_syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields['P']);
594 bang = num_tokens(msg->cm_fields['P'], '!');
596 for (i=0; i<(bang-1); ++i) {
602 QN_syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
603 buf, ChrPtr(mptr->Value[0])) ;
604 if (!strcasecmp(buf, ChrPtr(mptr->Value[0]))) {
613 ChrPtr(mptr->Value[0]));
616 /* Send the message */
620 * Force the message to appear in the correct
621 * room on the far end by setting the C field
624 if (msg->cm_fields['C'] != NULL) {
625 free(msg->cm_fields['C']);
627 if (StrLength(mptr->Value[0]) > 0) {
628 msg->cm_fields['C'] =
629 strdup(ChrPtr(mptr->Value[0]));
632 msg->cm_fields['C'] =
633 strdup(CC->room.QRname);
636 /* serialize it for transmission */
637 serialize_message(&sermsg, msg);
638 if (sermsg.len > 0) {
640 /* write it to a spool file */
645 ChrPtr(mptr->Value[0]),
654 fp = fopen(filename, "ab");
667 /* free the serialized version */
673 CtdlFreeMessage(msg);
678 * Spools out one message from the list.
680 void network_spool_msg(long msgnum,
683 struct CtdlMessage *msg = NULL;
684 long delete_after_send = 0; /* Set to 1 to delete after spooling */
687 sc = (SpoolControl *)userdata;
689 msg = CtdlFetchMessage(msgnum, 1);
691 network_process_list(sc, msg, &delete_after_send);
692 network_process_digest(sc, msg, &delete_after_send);
693 network_process_participate(sc, msg, &delete_after_send);
694 network_process_ignetpush(sc, msg, &delete_after_send);
696 CtdlFreeMessage(msg);
698 /* update lastsent */
699 sc->lastsent = msgnum;
701 /* Delete this message if delete-after-send is set */
702 if (delete_after_send) {
703 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");