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 "netconfig.h"
82 #include "ctdl_module.h"
86 * Deliver digest messages
88 void network_deliver_digest(SpoolControl *sc) {
89 struct CitContext *CCC = CC;
92 struct CtdlMessage *msg = NULL;
95 size_t recps_len = SIZ;
96 struct recptypes *valid;
100 if (sc->num_msgs_spooled < 1) {
101 fclose(sc->digestfp);
106 msg = malloc(sizeof(struct CtdlMessage));
107 memset(msg, 0, sizeof(struct CtdlMessage));
108 msg->cm_magic = CTDLMESSAGE_MAGIC;
109 msg->cm_format_type = FMT_RFC822;
110 msg->cm_anon_type = MES_NORMAL;
112 sprintf(buf, "%ld", time(NULL));
113 msg->cm_fields['T'] = strdup(buf);
114 msg->cm_fields['A'] = strdup(CC->room.QRname);
115 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
116 msg->cm_fields['U'] = strdup(buf);
117 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
118 for (i=0; buf[i]; ++i) {
119 if (isspace(buf[i])) buf[i]='_';
120 buf[i] = tolower(buf[i]);
122 msg->cm_fields['F'] = strdup(buf);
123 msg->cm_fields['R'] = strdup(buf);
125 /* Set the 'List-ID' header */
126 msg->cm_fields['L'] = malloc(1024);
127 snprintf(msg->cm_fields['L'], 1024,
128 "%s <%ld.list-id.%s>",
135 * Go fetch the contents of the digest
137 fseek(sc->digestfp, 0L, SEEK_END);
138 msglen = ftell(sc->digestfp);
140 msg->cm_fields['M'] = malloc(msglen + 1);
141 fseek(sc->digestfp, 0L, SEEK_SET);
142 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
143 msg->cm_fields['M'][msglen] = '\0';
145 fclose(sc->digestfp);
148 /* Now generate the delivery instructions */
151 * Figure out how big a buffer we need to allocate
153 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
154 recps_len = recps_len + strlen(nptr->name) + 2;
157 recps = malloc(recps_len);
161 "Cannot allocate %ld bytes for recps...\n",
169 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
170 if (nptr != sc->digestrecps) {
173 strcat(recps, nptr->name);
176 /* Where do we want bounces and other noise to be heard?
177 *Surely not the list members! */
178 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
180 /* Now submit the message */
181 valid = validate_recipients(recps, 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);
194 * Deliver list messages to everyone on the list ... efficiently
196 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
198 struct CitContext *CCC = CC;
200 size_t recps_len = SIZ;
201 struct recptypes *valid;
205 /* Don't do this if there were no recipients! */
206 if (sc->listrecps == NULL) return;
208 /* Now generate the delivery instructions */
211 * Figure out how big a buffer we need to allocate
213 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
214 recps_len = recps_len + strlen(nptr->name) + 2;
217 recps = malloc(recps_len);
221 "Cannot allocate %ld bytes for recps...\n",
229 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
230 if (nptr != sc->listrecps) {
233 strcat(recps, nptr->name);
236 /* Where do we want bounces and other noise to be heard?
237 * Surely not the list members! */
238 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
240 /* Now submit the message */
241 valid = validate_recipients(recps, NULL, 0);
244 valid->bounce_to = strdup(bounce_to);
245 valid->envelope_from = strdup(bounce_to);
246 valid->sending_room = strdup(RoomName);
247 CtdlSubmitMsg(msg, valid, NULL, 0);
248 free_recipients(valid);
250 /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
255 * Spools out one message from the list.
257 void network_spool_msg(long msgnum,
260 struct CitContext *CCC = CC;
264 char *newpath = NULL;
265 struct CtdlMessage *msg = NULL;
268 struct ser_ret sermsg;
270 char filename[PATH_MAX];
274 int delete_after_send = 0; /* Set to 1 to delete after spooling */
275 int ok_to_participate = 0;
276 struct recptypes *valid;
278 sc = (SpoolControl *)userdata;
281 * Process mailing list recipients
283 if (sc->listrecps != NULL) {
284 /* Fetch the message. We're going to need to modify it
285 * in order to insert the [list name] in it, etc.
287 msg = CtdlFetchMessage(msgnum, 1);
291 StrBuf *Subject, *FlatSubject;
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(CC->room.QRname);
335 pCh = strstr(ChrPtr(FlatSubject), CC->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", CC->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, CC->room.QRname);
389 CtdlFreeMessage(msg);
394 * Process digest recipients
396 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
397 msg = CtdlFetchMessage(msgnum, 1);
399 fprintf(sc->digestfp,
400 " -----------------------------------"
401 "------------------------------------"
403 fprintf(sc->digestfp, "From: ");
404 if (msg->cm_fields['A'] != NULL) {
405 fprintf(sc->digestfp,
407 msg->cm_fields['A']);
409 if (msg->cm_fields['F'] != NULL) {
410 fprintf(sc->digestfp,
412 msg->cm_fields['F']);
414 else if (msg->cm_fields['N'] != NULL) {
415 fprintf(sc->digestfp,
417 msg->cm_fields['N']);
419 fprintf(sc->digestfp, "\n");
420 if (msg->cm_fields['U'] != NULL) {
421 fprintf(sc->digestfp,
423 msg->cm_fields['U']);
426 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
428 safestrncpy(CC->preferred_formats,
430 sizeof CC->preferred_formats);
432 CtdlOutputPreLoadedMsg(msg,
437 StrBufTrim(CC->redirect_buffer);
438 fwrite(HKEY("\n"), 1, sc->digestfp);
439 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
440 fwrite(HKEY("\n"), 1, sc->digestfp);
442 FreeStrBuf(&CC->redirect_buffer);
444 sc->num_msgs_spooled += 1;
445 CtdlFreeMessage(msg);
450 * Process client-side list participations for this room
452 if (sc->participates != NULL) {
453 msg = CtdlFetchMessage(msgnum, 1);
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 (is_valid_node(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->participates;
505 if (msg->cm_fields['R'] != NULL) {
506 free(msg->cm_fields['R']);
508 msg->cm_fields['R'] =
511 valid = validate_recipients(nptr->name,
514 CtdlSubmitMsg(msg, valid, "", 0);
515 free_recipients(valid);
518 CtdlFreeMessage(msg);
523 * Process IGnet push shares
525 msg = CtdlFetchMessage(msgnum, 1);
529 /* Prepend our node name to the Path field whenever
530 * sending a message to another IGnet node
532 if (msg->cm_fields['P'] == NULL) {
533 msg->cm_fields['P'] = strdup("username");
535 newpath_len = strlen(msg->cm_fields['P']) +
536 strlen(config.c_nodename) + 2;
537 newpath = malloc(newpath_len);
538 snprintf(newpath, newpath_len, "%s!%s",
539 config.c_nodename, msg->cm_fields['P']);
540 free(msg->cm_fields['P']);
541 msg->cm_fields['P'] = newpath;
544 * Determine if this message is set to be deleted
545 * after sending out on the network
547 if (msg->cm_fields['S'] != NULL) {
548 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
549 delete_after_send = 1;
553 /* Now send it to every node */
554 if (sc->ignet_push_shares != NULL)
555 for (mptr = sc->ignet_push_shares; mptr != NULL;
560 Buf = NewStrBufPlain(mptr->remote_nodename, -1);
562 StrBufPlain(Buf, mptr->remote_nodename, -1);
563 /* Check for valid node name */
564 if (is_valid_node(NULL,
567 sc->working_ignetcfg,
568 sc->the_netmap) != 0)
571 "Invalid node <%s>\n",
572 mptr->remote_nodename);
577 /* Check for split horizon */
578 QN_syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields['P']);
579 bang = num_tokens(msg->cm_fields['P'], '!');
581 for (i=0; i<(bang-1); ++i) {
587 QN_syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
588 buf, mptr->remote_nodename) ;
589 if (!strcasecmp(buf, mptr->remote_nodename)) {
598 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, "");