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"
83 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName);
85 void aggregate_recipients(StrBuf **recps, RoomNetCfg Which, OneRoomNetCfg *OneRNCfg, long nSegments)
90 struct CitContext *CCC = CC;
94 * Figure out how big a buffer we need to allocate
96 for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
97 recps_len = recps_len + StrLength(nptr->Value[0]) + 2;
100 /* Nothing todo... */
104 *recps = NewStrBufPlain(NULL, recps_len);
106 if (*recps == NULL) {
108 "Cannot allocate %ld bytes for recps...\n",
114 for (nptr = OneRNCfg->NetConfigs[Which]; nptr != NULL; nptr = nptr->next) {
115 if (nptr != OneRNCfg->NetConfigs[Which]) {
116 for (i = 0; i < nSegments; i++)
117 StrBufAppendBufPlain(*recps, HKEY(","), i);
119 StrBufAppendBuf(*recps, nptr->Value[0], 0);
120 if (Which == ignet_push_share)
122 StrBufAppendBufPlain(*recps, HKEY(","), 0);
123 StrBufAppendBuf(*recps, nptr->Value[1], 0);
129 static void ListCalculateSubject(struct CtdlMessage *msg)
131 struct CitContext *CCC = CC;
132 StrBuf *Subject, *FlatSubject;
136 if (msg->cm_fields['U'] == NULL) {
137 Subject = NewStrBufPlain(HKEY("(no subject)"));
140 Subject = NewStrBufPlain(
141 msg->cm_fields['U'], -1);
143 FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
144 StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
146 rlen = strlen(CCC->room.QRname);
147 pCh = strstr(ChrPtr(FlatSubject), CCC->room.QRname);
149 (*(pCh + rlen) != ']') ||
150 (pCh == ChrPtr(FlatSubject)) ||
155 StrBufPlain(Subject, HKEY("["));
156 StrBufAppendBufPlain(Subject,
159 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
160 StrBufAppendBuf(Subject, FlatSubject, 0);
161 /* so we can free the right one swap them */
163 Subject = FlatSubject;
165 StrBufRFC2047encode(&Subject, FlatSubject);
168 if (msg->cm_fields['U'] != NULL)
169 free (msg->cm_fields['U']);
170 msg->cm_fields['U'] = SmashStrBuf(&Subject);
172 FreeStrBuf(&FlatSubject);
176 * Deliver digest messages
178 void network_deliver_digest(SpoolControl *sc)
181 struct CtdlMessage *msg = NULL;
183 struct recptypes *valid;
186 if (sc->Users[listrecp] == NULL)
189 if (sc->num_msgs_spooled < 1) {
190 fclose(sc->digestfp);
195 msg = malloc(sizeof(struct CtdlMessage));
196 memset(msg, 0, sizeof(struct CtdlMessage));
197 msg->cm_magic = CTDLMESSAGE_MAGIC;
198 msg->cm_format_type = FMT_RFC822;
199 msg->cm_anon_type = MES_NORMAL;
201 sprintf(buf, "%ld", time(NULL));
202 msg->cm_fields['T'] = strdup(buf);
203 msg->cm_fields['A'] = strdup(CC->room.QRname);
204 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
205 msg->cm_fields['U'] = strdup(buf);
207 CtdlMsgSetCM_Fields(msg, 'F', SKEY(sc->Users[roommailalias]));
208 CtdlMsgSetCM_Fields(msg, 'R', SKEY(sc->Users[roommailalias]));
210 /* Set the 'List-ID' header */
211 CtdlMsgSetCM_Fields(msg, 'L', SKEY(sc->ListID));
214 * Go fetch the contents of the digest
216 fseek(sc->digestfp, 0L, SEEK_END);
217 msglen = ftell(sc->digestfp);
219 msg->cm_fields['M'] = malloc(msglen + 1);
220 fseek(sc->digestfp, 0L, SEEK_SET);
221 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
222 msg->cm_fields['M'][msglen] = '\0';
224 fclose(sc->digestfp);
227 /* Now generate the delivery instructions */
228 if (sc->Users[listrecp] == NULL)
231 /* Where do we want bounces and other noise to be heard?
232 *Surely not the list members! */
233 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
235 /* Now submit the message */
236 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
238 valid->bounce_to = strdup(bounce_to);
239 valid->envelope_from = strdup(bounce_to);
240 CtdlSubmitMsg(msg, valid, NULL, 0);
242 CtdlFreeMessage(msg);
243 free_recipients(valid);
247 void network_process_digest(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
250 struct CtdlMessage *msg = NULL;
253 * Process digest recipients
255 if ((sc->Users[digestrecp] == NULL)||
256 (sc->digestfp == NULL))
259 msg = CtdlDuplicateMessage(omsg);
261 fprintf(sc->digestfp,
262 " -----------------------------------"
263 "------------------------------------"
265 fprintf(sc->digestfp, "From: ");
266 if (msg->cm_fields['A'] != NULL) {
267 fprintf(sc->digestfp,
269 msg->cm_fields['A']);
271 if (msg->cm_fields['F'] != NULL) {
272 fprintf(sc->digestfp,
274 msg->cm_fields['F']);
276 else if (msg->cm_fields['N'] != NULL) {
277 fprintf(sc->digestfp,
279 msg->cm_fields['N']);
281 fprintf(sc->digestfp, "\n");
282 if (msg->cm_fields['U'] != NULL) {
283 fprintf(sc->digestfp,
285 msg->cm_fields['U']);
288 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
290 safestrncpy(CC->preferred_formats,
292 sizeof CC->preferred_formats);
294 CtdlOutputPreLoadedMsg(msg,
299 StrBufTrim(CC->redirect_buffer);
300 fwrite(HKEY("\n"), 1, sc->digestfp);
301 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
302 fwrite(HKEY("\n"), 1, sc->digestfp);
304 FreeStrBuf(&CC->redirect_buffer);
306 sc->num_msgs_spooled += 1;
307 CtdlFreeMessage(msg);
312 void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
314 struct CtdlMessage *msg = NULL;
317 * Process mailing list recipients
319 if (sc->Users[listrecp] == NULL)
322 /* create our own copy of the message.
323 * We're going to need to modify it
324 * in order to insert the [list name] in it, etc.
327 msg = CtdlDuplicateMessage(omsg);
330 CtdlMsgSetCM_Fields(msg, 'K', SKEY(sc->Users[roommailalias]));
332 /* if there is no other recipient, Set the recipient
333 * of the list message to the email address of the
336 if ((msg->cm_fields['R'] == NULL) ||
337 IsEmptyStr(msg->cm_fields['R']))
339 CtdlMsgSetCM_Fields(msg, 'R', SKEY(sc->Users[roommailalias]));
342 /* Set the 'List-ID' header */
343 CtdlMsgSetCM_Fields(msg, 'L', SKEY(sc->ListID));
346 /* Prepend "[List name]" to the subject */
347 ListCalculateSubject(msg);
349 /* Handle delivery */
350 network_deliver_list(msg, sc, CC->room.QRname);
351 CtdlFreeMessage(msg);
355 * Deliver list messages to everyone on the list ... efficiently
357 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
359 struct recptypes *valid;
362 /* Don't do this if there were no recipients! */
363 if (sc->Users[listrecp] == NULL)
366 /* Now generate the delivery instructions */
368 /* Where do we want bounces and other noise to be heard?
369 * Surely not the list members! */
370 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
372 /* Now submit the message */
373 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
375 valid->bounce_to = strdup(bounce_to);
376 valid->envelope_from = strdup(bounce_to);
377 valid->sending_room = strdup(RoomName);
378 CtdlSubmitMsg(msg, valid, NULL, 0);
379 free_recipients(valid);
381 /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
385 void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
387 struct CtdlMessage *msg = NULL;
388 int ok_to_participate = 0;
390 struct recptypes *valid;
393 * Process client-side list participations for this room
395 if (sc->Users[participate] == NULL)
398 msg = CtdlDuplicateMessage(omsg);
400 /* Only send messages which originated on our own
401 * Citadel network, otherwise we'll end up sending the
402 * remote mailing list's messages back to it, which
405 ok_to_participate = 0;
406 if (msg->cm_fields['N'] != NULL) {
407 if (!strcasecmp(msg->cm_fields['N'],
408 config.c_nodename)) {
409 ok_to_participate = 1;
412 Buf = NewStrBufPlain(msg->cm_fields['N'], -1);
413 if (CtdlIsValidNode(NULL,
416 sc->working_ignetcfg,
417 sc->the_netmap) == 0)
419 ok_to_participate = 1;
422 if (ok_to_participate)
424 /* Replace the Internet email address of the
425 * actual author with the email address of the
426 * room itself, so the remote listserv doesn't
429 CtdlMsgSetCM_Fields(msg, 'F', SKEY(sc->Users[roommailalias]));
431 valid = validate_recipients(ChrPtr(sc->Users[participate]) , NULL, 0);
433 CtdlMsgSetCM_Fields(msg, 'R', SKEY(sc->Users[roommailalias]));
434 CtdlSubmitMsg(msg, valid, "", 0);
435 free_recipients(valid);
438 CtdlFreeMessage(msg);
441 void network_process_ignetpush(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
445 const char *Pos = NULL;
446 struct CtdlMessage *msg = NULL;
447 struct CitContext *CCC = CC;
448 struct ser_ret sermsg;
450 char filename[PATH_MAX];
453 char *newpath = NULL;
459 if (sc->Users[ignet_push_share] == NULL)
463 * Process IGnet push shares
465 msg = CtdlDuplicateMessage(omsg);
467 /* Prepend our node name to the Path field whenever
468 * sending a message to another IGnet node
470 if (msg->cm_fields['P'] == NULL)
472 msg->cm_fields['P'] = strdup("username");
474 newpath_len = strlen(msg->cm_fields['P']) +
475 strlen(config.c_nodename) + 2;
476 newpath = malloc(newpath_len);
477 snprintf(newpath, newpath_len, "%s!%s",
478 config.c_nodename, msg->cm_fields['P']);
479 free(msg->cm_fields['P']);
480 msg->cm_fields['P'] = newpath;
483 * Determine if this message is set to be deleted
484 * after sending out on the network
486 if (msg->cm_fields['S'] != NULL) {
487 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
488 *delete_after_send = 1;
492 /* Now send it to every node */
493 Recipient = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
494 RemoteRoom = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
495 while ((Pos != StrBufNOTNULL) &&
496 StrBufExtract_NextToken(Recipient, sc->Users[ignet_push_share], &Pos, ','))
498 StrBufExtract_NextToken(RemoteRoom, sc->Users[ignet_push_share], &Pos, ',');
500 NewStrBufDupAppendFlush(&Buf, Recipient, NULL, 1);
502 /* Check for valid node name */
503 if (CtdlIsValidNode(NULL,
506 sc->working_ignetcfg,
507 sc->the_netmap) != 0)
510 "Invalid node <%s>\n",
516 /* Check for split horizon */
517 QN_syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields['P']);
518 bang = num_tokens(msg->cm_fields['P'], '!');
520 for (i=0; i<(bang-1); ++i) {
526 QN_syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
527 buf, ChrPtr(Recipient)) ;
528 if (!strcasecmp(buf, ChrPtr(Recipient))) {
535 " %sSending to %s\n",
540 /* Send the message */
544 * Force the message to appear in the correct
545 * room on the far end by setting the C field
548 if (msg->cm_fields['C'] != NULL) {
549 free(msg->cm_fields['C']);
551 if (StrLength(RemoteRoom) > 0) {
552 msg->cm_fields['C'] =
553 strdup(ChrPtr(RemoteRoom));
556 msg->cm_fields['C'] =
557 strdup(CC->room.QRname);
560 /* serialize it for transmission */
561 serialize_message(&sermsg, msg);
562 if (sermsg.len > 0) {
564 /* write it to a spool file */
578 fp = fopen(filename, "ab");
591 /* free the serialized version */
597 FreeStrBuf(&Recipient);
598 FreeStrBuf(&RemoteRoom);
599 CtdlFreeMessage(msg);
604 * Spools out one message from the list.
606 void network_spool_msg(long msgnum,
609 struct CitContext *CCC = CC;
610 struct CtdlMessage *msg = NULL;
611 long delete_after_send = 0; /* Set to 1 to delete after spooling */
614 sc = (SpoolControl *)userdata;
616 msg = CtdlFetchMessage(msgnum, 1);
621 "failed to load Message <%ld> from disk\n",
625 network_process_list(sc, msg, &delete_after_send);
626 network_process_digest(sc, msg, &delete_after_send);
627 network_process_participate(sc, msg, &delete_after_send);
628 network_process_ignetpush(sc, msg, &delete_after_send);
630 CtdlFreeMessage(msg);
632 /* update lastsent */
633 sc->lastsent = msgnum;
635 /* Delete this message if delete-after-send is set */
636 if (delete_after_send) {
637 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");