2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2011 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 as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
22 * This is a fairly high-level type of critical section. It ensures that no
23 * two threads work on the netconfigs files at the same time. Since we do
24 * so many things inside these, here are the rules:
25 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
26 * 2. Do *not* perform any I/O with the client during these sections.
31 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
32 * requests that have not been confirmed will be deleted.
34 #define EXP 259200 /* three days */
46 #include <sys/types.h>
48 #if TIME_WITH_SYS_TIME
49 # include <sys/time.h>
53 # include <sys/time.h>
61 # if HAVE_SYS_SYSCALL_H
62 # include <sys/syscall.h>
69 #include <libcitadel.h>
72 #include "citserver.h"
78 #include "internet_addressing.h"
79 #include "serv_network.h"
80 #include "clientsocket.h"
82 #include "citadel_dirs.h"
90 #include "netconfig.h"
93 #include "ctdl_module.h"
97 * Deliver digest messages
99 void network_deliver_digest(SpoolControl *sc) {
102 struct CtdlMessage *msg = NULL;
105 size_t recps_len = SIZ;
106 struct recptypes *valid;
110 if (sc->num_msgs_spooled < 1) {
111 fclose(sc->digestfp);
116 msg = malloc(sizeof(struct CtdlMessage));
117 memset(msg, 0, sizeof(struct CtdlMessage));
118 msg->cm_magic = CTDLMESSAGE_MAGIC;
119 msg->cm_format_type = FMT_RFC822;
120 msg->cm_anon_type = MES_NORMAL;
122 sprintf(buf, "%ld", time(NULL));
123 msg->cm_fields['T'] = strdup(buf);
124 msg->cm_fields['A'] = strdup(CC->room.QRname);
125 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
126 msg->cm_fields['U'] = strdup(buf);
127 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
128 for (i=0; buf[i]; ++i) {
129 if (isspace(buf[i])) buf[i]='_';
130 buf[i] = tolower(buf[i]);
132 msg->cm_fields['F'] = strdup(buf);
133 msg->cm_fields['R'] = strdup(buf);
135 /* Set the 'List-ID' header */
136 msg->cm_fields['L'] = malloc(1024);
137 snprintf(msg->cm_fields['L'], 1024,
138 "%s <%ld.list-id.%s>",
145 * Go fetch the contents of the digest
147 fseek(sc->digestfp, 0L, SEEK_END);
148 msglen = ftell(sc->digestfp);
150 msg->cm_fields['M'] = malloc(msglen + 1);
151 fseek(sc->digestfp, 0L, SEEK_SET);
152 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
153 msg->cm_fields['M'][msglen] = '\0';
155 fclose(sc->digestfp);
158 /* Now generate the delivery instructions */
161 * Figure out how big a buffer we need to allocate
163 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
164 recps_len = recps_len + strlen(nptr->name) + 2;
167 recps = malloc(recps_len);
171 "Cannot allocate %ld bytes for recps...\n",
179 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
180 if (nptr != sc->digestrecps) {
183 strcat(recps, nptr->name);
186 /* Where do we want bounces and other noise to be heard?
187 *Surely not the list members! */
188 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
190 /* Now submit the message */
191 valid = validate_recipients(recps, NULL, 0);
194 valid->bounce_to = strdup(bounce_to);
195 valid->envelope_from = strdup(bounce_to);
196 CtdlSubmitMsg(msg, valid, NULL, 0);
198 CtdlFreeMessage(msg);
199 free_recipients(valid);
204 * Deliver list messages to everyone on the list ... efficiently
206 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName) {
208 size_t recps_len = SIZ;
209 struct recptypes *valid;
213 /* Don't do this if there were no recipients! */
214 if (sc->listrecps == NULL) return;
216 /* Now generate the delivery instructions */
219 * Figure out how big a buffer we need to allocate
221 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
222 recps_len = recps_len + strlen(nptr->name) + 2;
225 recps = malloc(recps_len);
229 "Cannot allocate %ld bytes for recps...\n",
237 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
238 if (nptr != sc->listrecps) {
241 strcat(recps, nptr->name);
244 /* Where do we want bounces and other noise to be heard?
245 * Surely not the list members! */
246 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
248 /* Now submit the message */
249 valid = validate_recipients(recps, NULL, 0);
252 valid->bounce_to = strdup(bounce_to);
253 valid->envelope_from = strdup(bounce_to);
254 valid->sending_room = strdup(RoomName);
255 CtdlSubmitMsg(msg, valid, NULL, 0);
256 free_recipients(valid);
258 /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
263 * Spools out one message from the list.
265 void network_spool_msg(long msgnum,
270 char *newpath = NULL;
271 struct CtdlMessage *msg = NULL;
274 struct ser_ret sermsg;
276 char filename[PATH_MAX];
280 int delete_after_send = 0; /* Set to 1 to delete after spooling */
281 int ok_to_participate = 0;
282 struct recptypes *valid;
284 sc = (SpoolControl *)userdata;
287 * Process mailing list recipients
289 if (sc->listrecps != NULL) {
290 /* Fetch the message. We're going to need to modify it
291 * in order to insert the [list name] in it, etc.
293 msg = CtdlFetchMessage(msgnum, 1);
297 StrBuf *Subject, *FlatSubject;
299 if (msg->cm_fields['K'] != NULL)
300 free(msg->cm_fields['K']);
301 if (msg->cm_fields['V'] == NULL){
302 /* local message, no enVelope */
305 StrBufAppendBufPlain(Buf,
308 StrBufAppendBufPlain(Buf, HKEY("@"), 0);
309 StrBufAppendBufPlain(Buf, config.c_fqdn, -1, 0);
311 msg->cm_fields['K'] = SmashStrBuf(&Buf);
314 msg->cm_fields['K'] =
315 strdup (msg->cm_fields['V']);
317 /* Set the 'List-ID' header */
318 if (msg->cm_fields['L'] != NULL) {
319 free(msg->cm_fields['L']);
321 msg->cm_fields['L'] = malloc(1024);
322 snprintf(msg->cm_fields['L'], 1024,
323 "%s <%ld.list-id.%s>",
329 /* Prepend "[List name]" to the subject */
330 if (msg->cm_fields['U'] == NULL) {
331 Subject = NewStrBufPlain(HKEY("(no subject)"));
334 Subject = NewStrBufPlain(
335 msg->cm_fields['U'], -1);
337 FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
338 StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
340 rlen = strlen(CC->room.QRname);
341 pCh = strstr(ChrPtr(FlatSubject), CC->room.QRname);
343 (*(pCh + rlen) != ']') ||
344 (pCh == ChrPtr(FlatSubject)) ||
349 StrBufPlain(Subject, HKEY("["));
350 StrBufAppendBufPlain(Subject,
353 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
354 StrBufAppendBuf(Subject, FlatSubject, 0);
355 /* so we can free the right one swap them */
357 Subject = FlatSubject;
359 StrBufRFC2047encode(&Subject, FlatSubject);
362 if (msg->cm_fields['U'] != NULL)
363 free (msg->cm_fields['U']);
364 msg->cm_fields['U'] = SmashStrBuf(&Subject);
366 FreeStrBuf(&FlatSubject);
368 /* else we won't modify the buffer, since the
369 * roomname is already here.
372 /* if there is no other recipient, Set the recipient
373 * of the list message to the email address of the
376 if ((msg->cm_fields['R'] == NULL) ||
377 IsEmptyStr(msg->cm_fields['R']))
379 if (msg->cm_fields['R'] != NULL)
380 free(msg->cm_fields['R']);
382 msg->cm_fields['R'] = malloc(256);
383 snprintf(msg->cm_fields['R'], 256,
384 "room_%s@%s", CC->room.QRname,
386 for (i=0; msg->cm_fields['R'][i]; ++i) {
387 if (isspace(msg->cm_fields['R'][i])) {
388 msg->cm_fields['R'][i] = '_';
393 /* Handle delivery */
394 network_deliver_list(msg, sc, CC->room.QRname);
395 CtdlFreeMessage(msg);
400 * Process digest recipients
402 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
403 msg = CtdlFetchMessage(msgnum, 1);
405 fprintf(sc->digestfp,
406 " -----------------------------------"
407 "------------------------------------"
409 fprintf(sc->digestfp, "From: ");
410 if (msg->cm_fields['A'] != NULL) {
411 fprintf(sc->digestfp,
413 msg->cm_fields['A']);
415 if (msg->cm_fields['F'] != NULL) {
416 fprintf(sc->digestfp,
418 msg->cm_fields['F']);
420 else if (msg->cm_fields['N'] != NULL) {
421 fprintf(sc->digestfp,
423 msg->cm_fields['N']);
425 fprintf(sc->digestfp, "\n");
426 if (msg->cm_fields['U'] != NULL) {
427 fprintf(sc->digestfp,
429 msg->cm_fields['U']);
432 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
434 safestrncpy(CC->preferred_formats,
436 sizeof CC->preferred_formats);
438 CtdlOutputPreLoadedMsg(msg,
443 StrBufTrim(CC->redirect_buffer);
444 fwrite(HKEY("\n"), 1, sc->digestfp);
445 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
446 fwrite(HKEY("\n"), 1, sc->digestfp);
448 FreeStrBuf(&CC->redirect_buffer);
450 sc->num_msgs_spooled += 1;
451 CtdlFreeMessage(msg);
456 * Process client-side list participations for this room
458 if (sc->participates != NULL) {
459 msg = CtdlFetchMessage(msgnum, 1);
462 /* Only send messages which originated on our own
463 * Citadel network, otherwise we'll end up sending the
464 * remote mailing list's messages back to it, which
467 ok_to_participate = 0;
468 if (msg->cm_fields['N'] != NULL) {
469 if (!strcasecmp(msg->cm_fields['N'],
470 config.c_nodename)) {
471 ok_to_participate = 1;
473 if (is_valid_node(NULL,
476 sc->working_ignetcfg,
477 sc->the_netmap) == 0)
479 ok_to_participate = 1;
482 if (ok_to_participate) {
483 if (msg->cm_fields['F'] != NULL) {
484 free(msg->cm_fields['F']);
486 msg->cm_fields['F'] = malloc(SIZ);
487 /* Replace the Internet email address of the
488 * actual author with the email address of the
489 * room itself, so the remote listserv doesn't
491 * FIXME I want to be able to pick any address
493 snprintf(msg->cm_fields['F'], SIZ,
494 "room_%s@%s", CC->room.QRname,
496 for (i=0; msg->cm_fields['F'][i]; ++i) {
497 if (isspace(msg->cm_fields['F'][i])) {
498 msg->cm_fields['F'][i] = '_';
503 * Figure out how big a buffer we need to alloc
505 for (nptr = sc->participates;
509 if (msg->cm_fields['R'] != NULL) {
510 free(msg->cm_fields['R']);
512 msg->cm_fields['R'] =
515 valid = validate_recipients(nptr->name,
518 CtdlSubmitMsg(msg, valid, "", 0);
519 free_recipients(valid);
522 CtdlFreeMessage(msg);
527 * Process IGnet push shares
529 msg = CtdlFetchMessage(msgnum, 1);
533 /* Prepend our node name to the Path field whenever
534 * sending a message to another IGnet node
536 if (msg->cm_fields['P'] == NULL) {
537 msg->cm_fields['P'] = strdup("username");
539 newpath_len = strlen(msg->cm_fields['P']) +
540 strlen(config.c_nodename) + 2;
541 newpath = malloc(newpath_len);
542 snprintf(newpath, newpath_len, "%s!%s",
543 config.c_nodename, msg->cm_fields['P']);
544 free(msg->cm_fields['P']);
545 msg->cm_fields['P'] = newpath;
548 * Determine if this message is set to be deleted
549 * after sending out on the network
551 if (msg->cm_fields['S'] != NULL) {
552 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
553 delete_after_send = 1;
557 /* Now send it to every node */
558 if (sc->ignet_push_shares != NULL)
559 for (mptr = sc->ignet_push_shares; mptr != NULL;
564 /* Check for valid node name */
565 if (is_valid_node(NULL,
567 mptr->remote_nodename,
568 sc->working_ignetcfg,
569 sc->the_netmap) != 0)
572 "Invalid node <%s>\n",
573 mptr->remote_nodename);
578 /* Check for split horizon */
579 syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields['P']);
580 bang = num_tokens(msg->cm_fields['P'], '!');
581 if (bang > 1) for (i=0; i<(bang-1); ++i) {
587 syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
588 buf, mptr->remote_nodename) ;
589 if (!strcasecmp(buf, mptr->remote_nodename)) {
591 syslog(LOG_DEBUG, "Not sending to %s\n",
592 mptr->remote_nodename);
597 mptr->remote_nodename);
601 /* Send the message */
605 * Force the message to appear in the correct
606 * room on the far end by setting the C field
609 if (msg->cm_fields['C'] != NULL) {
610 free(msg->cm_fields['C']);
612 if (!IsEmptyStr(mptr->remote_roomname)) {
613 msg->cm_fields['C'] =
614 strdup(mptr->remote_roomname);
617 msg->cm_fields['C'] =
618 strdup(CC->room.QRname);
621 /* serialize it for transmission */
622 serialize_message(&sermsg, msg);
623 if (sermsg.len > 0) {
625 /* write it to a spool file */
630 mptr->remote_nodename,
639 fp = fopen(filename, "ab");
652 /* free the serialized version */
658 CtdlFreeMessage(msg);
661 /* update lastsent */
662 sc->lastsent = msgnum;
664 /* Delete this message if delete-after-send is set */
665 if (delete_after_send) {
666 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");