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"
92 #include "ctdl_module.h"
96 * Deliver digest messages
98 void network_deliver_digest(SpoolControl *sc) {
101 struct CtdlMessage *msg = NULL;
104 size_t recps_len = SIZ;
105 struct recptypes *valid;
109 if (sc->num_msgs_spooled < 1) {
110 fclose(sc->digestfp);
115 msg = malloc(sizeof(struct CtdlMessage));
116 memset(msg, 0, sizeof(struct CtdlMessage));
117 msg->cm_magic = CTDLMESSAGE_MAGIC;
118 msg->cm_format_type = FMT_RFC822;
119 msg->cm_anon_type = MES_NORMAL;
121 sprintf(buf, "%ld", time(NULL));
122 msg->cm_fields['T'] = strdup(buf);
123 msg->cm_fields['A'] = strdup(CC->room.QRname);
124 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
125 msg->cm_fields['U'] = strdup(buf);
126 sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
127 for (i=0; buf[i]; ++i) {
128 if (isspace(buf[i])) buf[i]='_';
129 buf[i] = tolower(buf[i]);
131 msg->cm_fields['F'] = strdup(buf);
132 msg->cm_fields['R'] = strdup(buf);
134 /* Set the 'List-ID' header */
135 msg->cm_fields['L'] = malloc(1024);
136 snprintf(msg->cm_fields['L'], 1024,
137 "%s <%ld.list-id.%s>",
144 * Go fetch the contents of the digest
146 fseek(sc->digestfp, 0L, SEEK_END);
147 msglen = ftell(sc->digestfp);
149 msg->cm_fields['M'] = malloc(msglen + 1);
150 fseek(sc->digestfp, 0L, SEEK_SET);
151 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
152 msg->cm_fields['M'][msglen] = '\0';
154 fclose(sc->digestfp);
157 /* Now generate the delivery instructions */
160 * Figure out how big a buffer we need to allocate
162 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
163 recps_len = recps_len + strlen(nptr->name) + 2;
166 recps = malloc(recps_len);
169 syslog(LOG_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
176 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
177 if (nptr != sc->digestrecps) {
180 strcat(recps, nptr->name);
183 /* Where do we want bounces and other noise to be heard? Surely not the list members! */
184 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
186 /* Now submit the message */
187 valid = validate_recipients(recps, NULL, 0);
190 valid->bounce_to = strdup(bounce_to);
191 valid->envelope_from = strdup(bounce_to);
192 CtdlSubmitMsg(msg, valid, NULL, 0);
194 CtdlFreeMessage(msg);
195 free_recipients(valid);
200 * Deliver list messages to everyone on the list ... efficiently
202 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc) {
204 size_t recps_len = SIZ;
205 struct recptypes *valid;
209 /* Don't do this if there were no recipients! */
210 if (sc->listrecps == NULL) return;
212 /* Now generate the delivery instructions */
215 * Figure out how big a buffer we need to allocate
217 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
218 recps_len = recps_len + strlen(nptr->name) + 2;
221 recps = malloc(recps_len);
224 syslog(LOG_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
231 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
232 if (nptr != sc->listrecps) {
235 strcat(recps, nptr->name);
238 /* Where do we want bounces and other noise to be heard? Surely not the list members! */
239 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
241 /* Now submit the message */
242 valid = validate_recipients(recps, NULL, 0);
245 valid->bounce_to = strdup(bounce_to);
246 valid->envelope_from = strdup(bounce_to);
247 CtdlSubmitMsg(msg, valid, NULL, 0);
248 free_recipients(valid);
250 /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
257 * Spools out one message from the list.
259 void network_spool_msg(long msgnum,
261 char *working_ignetcfg,
266 char *newpath = NULL;
267 struct CtdlMessage *msg = NULL;
270 struct ser_ret sermsg;
272 char filename[PATH_MAX];
276 int delete_after_send = 0; /* Set to 1 to delete after spooling */
277 int ok_to_participate = 0;
278 struct recptypes *valid;
280 sc = (SpoolControl *)userdata;
283 * Process mailing list recipients
285 if (sc->listrecps != NULL) {
286 /* Fetch the message. We're going to need to modify it
287 * in order to insert the [list name] in it, etc.
289 msg = CtdlFetchMessage(msgnum, 1);
293 StrBuf *Subject, *FlatSubject;
295 if (msg->cm_fields['V'] == NULL){
296 /* local message, no enVelope */
299 StrBufAppendBufPlain(Buf, msg->cm_fields['O'], -1, 0);
300 StrBufAppendBufPlain(Buf, HKEY("@"), 0);
301 StrBufAppendBufPlain(Buf, config.c_fqdn, -1, 0);
303 msg->cm_fields['K'] = SmashStrBuf(&Buf);
306 msg->cm_fields['K'] = strdup (msg->cm_fields['V']);
308 /* Set the 'List-ID' header */
309 if (msg->cm_fields['L'] != NULL) {
310 free(msg->cm_fields['L']);
312 msg->cm_fields['L'] = malloc(1024);
313 snprintf(msg->cm_fields['L'], 1024,
314 "%s <%ld.list-id.%s>",
320 /* Prepend "[List name]" to the subject */
321 if (msg->cm_fields['U'] == NULL) {
322 Subject = NewStrBufPlain(HKEY("(no subject)"));
325 Subject = NewStrBufPlain(msg->cm_fields['U'], -1);
327 FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
328 StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
330 rlen = strlen(CC->room.QRname);
331 pCh = strstr(ChrPtr(FlatSubject), CC->room.QRname);
333 (*(pCh + rlen) != ']') ||
334 (pCh == ChrPtr(FlatSubject)) ||
339 StrBufPlain(Subject, HKEY("["));
340 StrBufAppendBufPlain(Subject, CC->room.QRname, rlen, 0);
341 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
342 StrBufAppendBuf(Subject, FlatSubject, 0);
343 tmp = Subject; Subject = FlatSubject; FlatSubject = tmp; /* so we can free the right one... */
344 StrBufRFC2047encode(&Subject, FlatSubject);
347 if (msg->cm_fields['U'] != NULL)
348 free (msg->cm_fields['U']);
349 msg->cm_fields['U'] = SmashStrBuf(&Subject);
351 FreeStrBuf(&FlatSubject);
353 /* else we won't modify the buffer, since the roomname is already here. */
355 /* Set the recipient of the list message to the
356 * email address of the room itself.
357 * FIXME ... I want to be able to pick any address
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] = '_';
372 /* Handle delivery */
373 network_deliver_list(msg, sc);
374 CtdlFreeMessage(msg);
379 * Process digest recipients
381 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
382 msg = CtdlFetchMessage(msgnum, 1);
384 fprintf(sc->digestfp, " -----------------------------------"
385 "------------------------------------"
387 fprintf(sc->digestfp, "From: ");
388 if (msg->cm_fields['A'] != NULL) {
389 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
391 if (msg->cm_fields['F'] != NULL) {
392 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
394 else if (msg->cm_fields['N'] != NULL) {
395 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
397 fprintf(sc->digestfp, "\n");
398 if (msg->cm_fields['U'] != NULL) {
399 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
402 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
404 safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
405 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
407 StrBufTrim(CC->redirect_buffer);
408 fwrite(HKEY("\n"), 1, sc->digestfp);
409 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
410 fwrite(HKEY("\n"), 1, sc->digestfp);
412 FreeStrBuf(&CC->redirect_buffer);
414 sc->num_msgs_spooled += 1;
420 * Process client-side list participations for this room
422 if (sc->participates != NULL) {
423 msg = CtdlFetchMessage(msgnum, 1);
426 /* Only send messages which originated on our own Citadel
427 * network, otherwise we'll end up sending the remote
428 * mailing list's messages back to it, which is rude...
430 ok_to_participate = 0;
431 if (msg->cm_fields['N'] != NULL) {
432 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
433 ok_to_participate = 1;
435 if (is_valid_node(NULL,
441 ok_to_participate = 1;
444 if (ok_to_participate) {
445 if (msg->cm_fields['F'] != NULL) {
446 free(msg->cm_fields['F']);
448 msg->cm_fields['F'] = malloc(SIZ);
449 /* Replace the Internet email address of the actual
450 * author with the email address of the room itself,
451 * so the remote listserv doesn't reject us.
452 * FIXME ... I want to be able to pick any address
454 snprintf(msg->cm_fields['F'], SIZ,
455 "room_%s@%s", CC->room.QRname,
457 for (i=0; msg->cm_fields['F'][i]; ++i) {
458 if (isspace(msg->cm_fields['F'][i])) {
459 msg->cm_fields['F'][i] = '_';
464 * Figure out how big a buffer we need to allocate
466 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
468 if (msg->cm_fields['R'] == NULL) {
469 free(msg->cm_fields['R']);
471 msg->cm_fields['R'] = strdup(nptr->name);
473 valid = validate_recipients(nptr->name, NULL, 0);
474 CtdlSubmitMsg(msg, valid, "", 0);
475 free_recipients(valid);
479 CtdlFreeMessage(msg);
484 * Process IGnet push shares
486 msg = CtdlFetchMessage(msgnum, 1);
490 /* Prepend our node name to the Path field whenever
491 * sending a message to another IGnet node
493 if (msg->cm_fields['P'] == NULL) {
494 msg->cm_fields['P'] = strdup("username");
496 newpath_len = strlen(msg->cm_fields['P']) +
497 strlen(config.c_nodename) + 2;
498 newpath = malloc(newpath_len);
499 snprintf(newpath, newpath_len, "%s!%s",
500 config.c_nodename, msg->cm_fields['P']);
501 free(msg->cm_fields['P']);
502 msg->cm_fields['P'] = newpath;
505 * Determine if this message is set to be deleted
506 * after sending out on the network
508 if (msg->cm_fields['S'] != NULL) {
509 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
510 delete_after_send = 1;
514 /* Now send it to every node */
515 if (sc->ignet_push_shares != NULL)
516 for (mptr = sc->ignet_push_shares; mptr != NULL;
521 /* Check for valid node name */
522 if (is_valid_node(NULL,
524 mptr->remote_nodename,
528 syslog(LOG_ERR, "Invalid node <%s>\n", mptr->remote_nodename);
532 /* Check for split horizon */
533 syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields['P']);
534 bang = num_tokens(msg->cm_fields['P'], '!');
535 if (bang > 1) for (i=0; i<(bang-1); ++i) {
536 extract_token(buf, msg->cm_fields['P'], i, '!', sizeof buf);
537 syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
538 buf, mptr->remote_nodename) ;
539 if (!strcasecmp(buf, mptr->remote_nodename)) {
541 syslog(LOG_DEBUG, "Not sending to %s\n",
542 mptr->remote_nodename);
545 syslog(LOG_DEBUG, "Sending to %s\n", mptr->remote_nodename);
549 /* Send the message */
553 * Force the message to appear in the correct room
554 * on the far end by setting the C field correctly
556 if (msg->cm_fields['C'] != NULL) {
557 free(msg->cm_fields['C']);
559 if (!IsEmptyStr(mptr->remote_roomname)) {
560 msg->cm_fields['C'] = strdup(mptr->remote_roomname);
563 msg->cm_fields['C'] = strdup(CC->room.QRname);
566 /* serialize it for transmission */
567 serialize_message(&sermsg, msg);
568 if (sermsg.len > 0) {
570 /* write it to a spool file */
571 snprintf(filename, sizeof filename,"%s/%s@%lx%x",
573 mptr->remote_nodename,
577 syslog(LOG_DEBUG, "Appending to %s\n", filename);
578 fp = fopen(filename, "ab");
585 syslog(LOG_ERR, "%s: %s\n", filename, strerror(errno));
588 /* free the serialized version */
594 CtdlFreeMessage(msg);
597 /* update lastsent */
598 sc->lastsent = msgnum;
600 /* Delete this message if delete-after-send is set */
601 if (delete_after_send) {
602 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");