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);
170 syslog(LOG_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
177 for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
178 if (nptr != sc->digestrecps) {
181 strcat(recps, nptr->name);
184 /* Where do we want bounces and other noise to be heard? Surely not the list members! */
185 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
187 /* Now submit the message */
188 valid = validate_recipients(recps, NULL, 0);
191 valid->bounce_to = strdup(bounce_to);
192 valid->envelope_from = strdup(bounce_to);
193 CtdlSubmitMsg(msg, valid, NULL, 0);
195 CtdlFreeMessage(msg);
196 free_recipients(valid);
201 * Deliver list messages to everyone on the list ... efficiently
203 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc) {
205 size_t recps_len = SIZ;
206 struct recptypes *valid;
210 /* Don't do this if there were no recipients! */
211 if (sc->listrecps == NULL) return;
213 /* Now generate the delivery instructions */
216 * Figure out how big a buffer we need to allocate
218 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
219 recps_len = recps_len + strlen(nptr->name) + 2;
222 recps = malloc(recps_len);
225 syslog(LOG_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
232 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
233 if (nptr != sc->listrecps) {
236 strcat(recps, nptr->name);
239 /* Where do we want bounces and other noise to be heard? Surely not the list members! */
240 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
242 /* Now submit the message */
243 valid = validate_recipients(recps, NULL, 0);
246 valid->bounce_to = strdup(bounce_to);
247 valid->envelope_from = strdup(bounce_to);
248 CtdlSubmitMsg(msg, valid, NULL, 0);
249 free_recipients(valid);
251 /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
258 * Spools out one message from the list.
260 void network_spool_msg(long msgnum,
265 char *newpath = NULL;
266 struct CtdlMessage *msg = NULL;
269 struct ser_ret sermsg;
271 char filename[PATH_MAX];
275 int delete_after_send = 0; /* Set to 1 to delete after spooling */
276 int ok_to_participate = 0;
277 struct recptypes *valid;
279 sc = (SpoolControl *)userdata;
282 * Process mailing list recipients
284 if (sc->listrecps != NULL) {
285 /* Fetch the message. We're going to need to modify it
286 * in order to insert the [list name] in it, etc.
288 msg = CtdlFetchMessage(msgnum, 1);
292 StrBuf *Subject, *FlatSubject;
294 if (msg->cm_fields['V'] == NULL){
295 /* local message, no enVelope */
298 StrBufAppendBufPlain(Buf, msg->cm_fields['O'], -1, 0);
299 StrBufAppendBufPlain(Buf, HKEY("@"), 0);
300 StrBufAppendBufPlain(Buf, config.c_fqdn, -1, 0);
302 msg->cm_fields['K'] = SmashStrBuf(&Buf);
305 msg->cm_fields['K'] = strdup (msg->cm_fields['V']);
307 /* Set the 'List-ID' header */
308 if (msg->cm_fields['L'] != NULL) {
309 free(msg->cm_fields['L']);
311 msg->cm_fields['L'] = malloc(1024);
312 snprintf(msg->cm_fields['L'], 1024,
313 "%s <%ld.list-id.%s>",
319 /* Prepend "[List name]" to the subject */
320 if (msg->cm_fields['U'] == NULL) {
321 Subject = NewStrBufPlain(HKEY("(no subject)"));
324 Subject = NewStrBufPlain(msg->cm_fields['U'], -1);
326 FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
327 StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
329 rlen = strlen(CC->room.QRname);
330 pCh = strstr(ChrPtr(FlatSubject), CC->room.QRname);
332 (*(pCh + rlen) != ']') ||
333 (pCh == ChrPtr(FlatSubject)) ||
338 StrBufPlain(Subject, HKEY("["));
339 StrBufAppendBufPlain(Subject, CC->room.QRname, rlen, 0);
340 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
341 StrBufAppendBuf(Subject, FlatSubject, 0);
342 tmp = Subject; Subject = FlatSubject; FlatSubject = tmp; /* so we can free the right one... */
343 StrBufRFC2047encode(&Subject, FlatSubject);
346 if (msg->cm_fields['U'] != NULL)
347 free (msg->cm_fields['U']);
348 msg->cm_fields['U'] = SmashStrBuf(&Subject);
350 FreeStrBuf(&FlatSubject);
352 /* else we won't modify the buffer, since the roomname is already here. */
354 /* Set the recipient of the list message to the
355 * email address of the room itself.
356 * FIXME ... I want to be able to pick any address
358 if (msg->cm_fields['R'] != NULL) {
359 free(msg->cm_fields['R']);
361 msg->cm_fields['R'] = malloc(256);
362 snprintf(msg->cm_fields['R'], 256,
363 "room_%s@%s", CC->room.QRname,
365 for (i=0; msg->cm_fields['R'][i]; ++i) {
366 if (isspace(msg->cm_fields['R'][i])) {
367 msg->cm_fields['R'][i] = '_';
371 /* Handle delivery */
372 network_deliver_list(msg, sc);
373 CtdlFreeMessage(msg);
378 * Process digest recipients
380 if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
381 msg = CtdlFetchMessage(msgnum, 1);
383 fprintf(sc->digestfp, " -----------------------------------"
384 "------------------------------------"
386 fprintf(sc->digestfp, "From: ");
387 if (msg->cm_fields['A'] != NULL) {
388 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
390 if (msg->cm_fields['F'] != NULL) {
391 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
393 else if (msg->cm_fields['N'] != NULL) {
394 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
396 fprintf(sc->digestfp, "\n");
397 if (msg->cm_fields['U'] != NULL) {
398 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
401 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
403 safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
404 CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0, 0);
406 StrBufTrim(CC->redirect_buffer);
407 fwrite(HKEY("\n"), 1, sc->digestfp);
408 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
409 fwrite(HKEY("\n"), 1, sc->digestfp);
411 FreeStrBuf(&CC->redirect_buffer);
413 sc->num_msgs_spooled += 1;
419 * Process client-side list participations for this room
421 if (sc->participates != NULL) {
422 msg = CtdlFetchMessage(msgnum, 1);
425 /* Only send messages which originated on our own Citadel
426 * network, otherwise we'll end up sending the remote
427 * mailing list's messages back to it, which is rude...
429 ok_to_participate = 0;
430 if (msg->cm_fields['N'] != NULL) {
431 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
432 ok_to_participate = 1;
434 if (is_valid_node(NULL,
437 sc->working_ignetcfg,
438 sc->the_netmap) == 0)
440 ok_to_participate = 1;
443 if (ok_to_participate) {
444 if (msg->cm_fields['F'] != NULL) {
445 free(msg->cm_fields['F']);
447 msg->cm_fields['F'] = malloc(SIZ);
448 /* Replace the Internet email address of the actual
449 * author with the email address of the room itself,
450 * so the remote listserv doesn't reject us.
451 * FIXME ... I want to be able to pick any address
453 snprintf(msg->cm_fields['F'], SIZ,
454 "room_%s@%s", CC->room.QRname,
456 for (i=0; msg->cm_fields['F'][i]; ++i) {
457 if (isspace(msg->cm_fields['F'][i])) {
458 msg->cm_fields['F'][i] = '_';
463 * Figure out how big a buffer we need to allocate
465 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
467 if (msg->cm_fields['R'] == NULL) {
468 free(msg->cm_fields['R']);
470 msg->cm_fields['R'] = strdup(nptr->name);
472 valid = validate_recipients(nptr->name, NULL, 0);
473 CtdlSubmitMsg(msg, valid, "", 0);
474 free_recipients(valid);
478 CtdlFreeMessage(msg);
483 * Process IGnet push shares
485 msg = CtdlFetchMessage(msgnum, 1);
489 /* Prepend our node name to the Path field whenever
490 * sending a message to another IGnet node
492 if (msg->cm_fields['P'] == NULL) {
493 msg->cm_fields['P'] = strdup("username");
495 newpath_len = strlen(msg->cm_fields['P']) +
496 strlen(config.c_nodename) + 2;
497 newpath = malloc(newpath_len);
498 snprintf(newpath, newpath_len, "%s!%s",
499 config.c_nodename, msg->cm_fields['P']);
500 free(msg->cm_fields['P']);
501 msg->cm_fields['P'] = newpath;
504 * Determine if this message is set to be deleted
505 * after sending out on the network
507 if (msg->cm_fields['S'] != NULL) {
508 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
509 delete_after_send = 1;
513 /* Now send it to every node */
514 if (sc->ignet_push_shares != NULL)
515 for (mptr = sc->ignet_push_shares; mptr != NULL;
520 /* Check for valid node name */
521 if (is_valid_node(NULL,
523 mptr->remote_nodename,
524 sc->working_ignetcfg,
525 sc->the_netmap) != 0)
527 syslog(LOG_ERR, "Invalid node <%s>\n", mptr->remote_nodename);
531 /* Check for split horizon */
532 syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields['P']);
533 bang = num_tokens(msg->cm_fields['P'], '!');
534 if (bang > 1) for (i=0; i<(bang-1); ++i) {
535 extract_token(buf, msg->cm_fields['P'], i, '!', sizeof buf);
536 syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
537 buf, mptr->remote_nodename) ;
538 if (!strcasecmp(buf, mptr->remote_nodename)) {
540 syslog(LOG_DEBUG, "Not sending to %s\n",
541 mptr->remote_nodename);
544 syslog(LOG_DEBUG, "Sending to %s\n", mptr->remote_nodename);
548 /* Send the message */
552 * Force the message to appear in the correct room
553 * on the far end by setting the C field correctly
555 if (msg->cm_fields['C'] != NULL) {
556 free(msg->cm_fields['C']);
558 if (!IsEmptyStr(mptr->remote_roomname)) {
559 msg->cm_fields['C'] = strdup(mptr->remote_roomname);
562 msg->cm_fields['C'] = strdup(CC->room.QRname);
565 /* serialize it for transmission */
566 serialize_message(&sermsg, msg);
567 if (sermsg.len > 0) {
569 /* write it to a spool file */
570 snprintf(filename, sizeof filename,"%s/%s@%lx%x",
572 mptr->remote_nodename,
576 syslog(LOG_DEBUG, "Appending to %s\n", filename);
577 fp = fopen(filename, "ab");
584 syslog(LOG_ERR, "%s: %s\n", filename, strerror(errno));
587 /* free the serialized version */
593 CtdlFreeMessage(msg);
596 /* update lastsent */
597 sc->lastsent = msgnum;
599 /* Delete this message if delete-after-send is set */
600 if (delete_after_send) {
601 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");