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);
123 static void ListCalculateSubject(struct CtdlMessage *msg)
125 struct CitContext *CCC = CC;
126 StrBuf *Subject, *FlatSubject;
130 if (msg->cm_fields['U'] == NULL) {
131 Subject = NewStrBufPlain(HKEY("(no subject)"));
134 Subject = NewStrBufPlain(
135 msg->cm_fields['U'], -1);
137 FlatSubject = NewStrBufPlain(NULL, StrLength(Subject));
138 StrBuf_RFC822_to_Utf8(FlatSubject, Subject, NULL, NULL);
140 rlen = strlen(CCC->room.QRname);
141 pCh = strstr(ChrPtr(FlatSubject), CCC->room.QRname);
143 (*(pCh + rlen) != ']') ||
144 (pCh == ChrPtr(FlatSubject)) ||
149 StrBufPlain(Subject, HKEY("["));
150 StrBufAppendBufPlain(Subject,
153 StrBufAppendBufPlain(Subject, HKEY("] "), 0);
154 StrBufAppendBuf(Subject, FlatSubject, 0);
155 /* so we can free the right one swap them */
157 Subject = FlatSubject;
159 StrBufRFC2047encode(&Subject, FlatSubject);
162 if (msg->cm_fields['U'] != NULL)
163 free (msg->cm_fields['U']);
164 msg->cm_fields['U'] = SmashStrBuf(&Subject);
166 FreeStrBuf(&FlatSubject);
170 * Deliver digest messages
172 void network_deliver_digest(SpoolControl *sc)
175 struct CtdlMessage *msg = NULL;
177 struct recptypes *valid;
180 if (sc->Users[listrecp] == NULL)
183 if (sc->num_msgs_spooled < 1) {
184 fclose(sc->digestfp);
189 msg = malloc(sizeof(struct CtdlMessage));
190 memset(msg, 0, sizeof(struct CtdlMessage));
191 msg->cm_magic = CTDLMESSAGE_MAGIC;
192 msg->cm_format_type = FMT_RFC822;
193 msg->cm_anon_type = MES_NORMAL;
195 sprintf(buf, "%ld", time(NULL));
196 msg->cm_fields['T'] = strdup(buf);
197 msg->cm_fields['A'] = strdup(CC->room.QRname);
198 snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
199 msg->cm_fields['U'] = strdup(buf);
201 CtdlMsgSetCM_Fields(msg, 'F', SKEY(sc->Users[roommailalias]));
202 CtdlMsgSetCM_Fields(msg, 'R', SKEY(sc->Users[roommailalias]));
204 /* Set the 'List-ID' header */
205 CtdlMsgSetCM_Fields(msg, 'L', SKEY(sc->ListID));
208 * Go fetch the contents of the digest
210 fseek(sc->digestfp, 0L, SEEK_END);
211 msglen = ftell(sc->digestfp);
213 msg->cm_fields['M'] = malloc(msglen + 1);
214 fseek(sc->digestfp, 0L, SEEK_SET);
215 fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
216 msg->cm_fields['M'][msglen] = '\0';
218 fclose(sc->digestfp);
221 /* Now generate the delivery instructions */
222 if (sc->Users[listrecp] == NULL)
225 /* Where do we want bounces and other noise to be heard?
226 *Surely not the list members! */
227 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
229 /* Now submit the message */
230 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
232 valid->bounce_to = strdup(bounce_to);
233 valid->envelope_from = strdup(bounce_to);
234 CtdlSubmitMsg(msg, valid, NULL, 0);
236 CtdlFreeMessage(msg);
237 free_recipients(valid);
241 void network_process_digest(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
244 struct CtdlMessage *msg = NULL;
247 * Process digest recipients
249 if ((sc->Users[digestrecp] == NULL)||
250 (sc->digestfp == NULL))
253 msg = CtdlDuplicateMessage(omsg);
255 fprintf(sc->digestfp,
256 " -----------------------------------"
257 "------------------------------------"
259 fprintf(sc->digestfp, "From: ");
260 if (msg->cm_fields['A'] != NULL) {
261 fprintf(sc->digestfp,
263 msg->cm_fields['A']);
265 if (msg->cm_fields['F'] != NULL) {
266 fprintf(sc->digestfp,
268 msg->cm_fields['F']);
270 else if (msg->cm_fields['N'] != NULL) {
271 fprintf(sc->digestfp,
273 msg->cm_fields['N']);
275 fprintf(sc->digestfp, "\n");
276 if (msg->cm_fields['U'] != NULL) {
277 fprintf(sc->digestfp,
279 msg->cm_fields['U']);
282 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
284 safestrncpy(CC->preferred_formats,
286 sizeof CC->preferred_formats);
288 CtdlOutputPreLoadedMsg(msg,
293 StrBufTrim(CC->redirect_buffer);
294 fwrite(HKEY("\n"), 1, sc->digestfp);
295 fwrite(SKEY(CC->redirect_buffer), 1, sc->digestfp);
296 fwrite(HKEY("\n"), 1, sc->digestfp);
298 FreeStrBuf(&CC->redirect_buffer);
300 sc->num_msgs_spooled += 1;
301 CtdlFreeMessage(msg);
306 void network_process_list(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
308 struct CtdlMessage *msg = NULL;
311 * Process mailing list recipients
313 if (sc->Users[listrecp] == NULL)
316 /* create our own copy of the message.
317 * We're going to need to modify it
318 * in order to insert the [list name] in it, etc.
321 msg = CtdlDuplicateMessage(omsg);
324 CtdlMsgSetCM_Fields(msg, 'K', SKEY(sc->Users[roommailalias]));
326 /* if there is no other recipient, Set the recipient
327 * of the list message to the email address of the
330 if ((msg->cm_fields['R'] == NULL) ||
331 IsEmptyStr(msg->cm_fields['R']))
333 CtdlMsgSetCM_Fields(msg, 'R', SKEY(sc->Users[roommailalias]));
336 /* Set the 'List-ID' header */
337 CtdlMsgSetCM_Fields(msg, 'L', SKEY(sc->ListID));
340 /* Prepend "[List name]" to the subject */
341 ListCalculateSubject(msg);
343 /* Handle delivery */
344 network_deliver_list(msg, sc, CC->room.QRname);
345 CtdlFreeMessage(msg);
349 * Deliver list messages to everyone on the list ... efficiently
351 void network_deliver_list(struct CtdlMessage *msg, SpoolControl *sc, const char *RoomName)
353 struct recptypes *valid;
356 /* Don't do this if there were no recipients! */
357 if (sc->Users[listrecp] == NULL)
360 /* Now generate the delivery instructions */
362 /* Where do we want bounces and other noise to be heard?
363 * Surely not the list members! */
364 snprintf(bounce_to, sizeof bounce_to, "room_aide@%s", config.c_fqdn);
366 /* Now submit the message */
367 valid = validate_recipients(ChrPtr(sc->Users[listrecp]), NULL, 0);
369 valid->bounce_to = strdup(bounce_to);
370 valid->envelope_from = strdup(bounce_to);
371 valid->sending_room = strdup(RoomName);
372 CtdlSubmitMsg(msg, valid, NULL, 0);
373 free_recipients(valid);
375 /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
379 void network_process_participate(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
381 struct CtdlMessage *msg = NULL;
382 int ok_to_participate = 0;
384 struct recptypes *valid;
387 * Process client-side list participations for this room
389 if (sc->Users[participate] == NULL)
392 msg = CtdlDuplicateMessage(omsg);
394 /* Only send messages which originated on our own
395 * Citadel network, otherwise we'll end up sending the
396 * remote mailing list's messages back to it, which
399 ok_to_participate = 0;
400 if (msg->cm_fields['N'] != NULL) {
401 if (!strcasecmp(msg->cm_fields['N'],
402 config.c_nodename)) {
403 ok_to_participate = 1;
406 Buf = NewStrBufPlain(msg->cm_fields['N'], -1);
407 if (CtdlIsValidNode(NULL,
410 sc->working_ignetcfg,
411 sc->the_netmap) == 0)
413 ok_to_participate = 1;
416 if (ok_to_participate)
418 /* Replace the Internet email address of the
419 * actual author with the email address of the
420 * room itself, so the remote listserv doesn't
423 CtdlMsgSetCM_Fields(msg, 'F', SKEY(sc->Users[roommailalias]));
425 valid = validate_recipients(ChrPtr(sc->Users[participate]) , NULL, 0);
427 CtdlMsgSetCM_Fields(msg, 'R', SKEY(sc->Users[roommailalias]));
428 CtdlSubmitMsg(msg, valid, "", 0);
429 free_recipients(valid);
432 CtdlFreeMessage(msg);
435 void network_process_ignetpush(SpoolControl *sc, struct CtdlMessage *omsg, long *delete_after_send)
439 const char *Pos = NULL;
440 struct CtdlMessage *msg = NULL;
441 struct CitContext *CCC = CC;
442 struct ser_ret sermsg;
444 char filename[PATH_MAX];
447 char *newpath = NULL;
453 if (sc->Users[ignet_push_share] == NULL)
457 * Process IGnet push shares
459 msg = CtdlDuplicateMessage(omsg);
461 /* Prepend our node name to the Path field whenever
462 * sending a message to another IGnet node
464 if (msg->cm_fields['P'] == NULL)
466 msg->cm_fields['P'] = strdup("username");
468 newpath_len = strlen(msg->cm_fields['P']) +
469 strlen(config.c_nodename) + 2;
470 newpath = malloc(newpath_len);
471 snprintf(newpath, newpath_len, "%s!%s",
472 config.c_nodename, msg->cm_fields['P']);
473 free(msg->cm_fields['P']);
474 msg->cm_fields['P'] = newpath;
477 * Determine if this message is set to be deleted
478 * after sending out on the network
480 if (msg->cm_fields['S'] != NULL) {
481 if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
482 *delete_after_send = 1;
486 /* Now send it to every node */
487 Recipient = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
488 RemoteRoom = NewStrBufPlain(NULL, StrLength(sc->Users[ignet_push_share]));
489 while ((Pos != StrBufNOTNULL) &&
490 StrBufExtract_NextToken(Recipient, sc->Users[ignet_push_share], &Pos, ','))
492 StrBufExtract_NextToken(RemoteRoom, sc->Users[ignet_push_share], &Pos, ',');
494 NewStrBufDupAppendFlush(&Buf, Recipient, NULL, 1);
496 /* Check for valid node name */
497 if (CtdlIsValidNode(NULL,
500 sc->working_ignetcfg,
501 sc->the_netmap) != 0)
504 "Invalid node <%s>\n",
510 /* Check for split horizon */
511 QN_syslog(LOG_DEBUG, "Path is %s\n", msg->cm_fields['P']);
512 bang = num_tokens(msg->cm_fields['P'], '!');
514 for (i=0; i<(bang-1); ++i) {
520 QN_syslog(LOG_DEBUG, "Compare <%s> to <%s>\n",
521 buf, ChrPtr(Recipient)) ;
522 if (!strcasecmp(buf, ChrPtr(Recipient))) {
529 " %sSending to %s\n",
534 /* Send the message */
538 * Force the message to appear in the correct
539 * room on the far end by setting the C field
542 if (msg->cm_fields['C'] != NULL) {
543 free(msg->cm_fields['C']);
545 if (StrLength(RemoteRoom) > 0) {
546 msg->cm_fields['C'] =
547 strdup(ChrPtr(RemoteRoom));
550 msg->cm_fields['C'] =
551 strdup(CC->room.QRname);
554 /* serialize it for transmission */
555 serialize_message(&sermsg, msg);
556 if (sermsg.len > 0) {
558 /* write it to a spool file */
572 fp = fopen(filename, "ab");
585 /* free the serialized version */
591 FreeStrBuf(&Recipient);
592 FreeStrBuf(&RemoteRoom);
593 CtdlFreeMessage(msg);
598 * Spools out one message from the list.
600 void network_spool_msg(long msgnum,
603 struct CitContext *CCC = CC;
604 struct CtdlMessage *msg = NULL;
605 long delete_after_send = 0; /* Set to 1 to delete after spooling */
608 sc = (SpoolControl *)userdata;
610 msg = CtdlFetchMessage(msgnum, 1);
615 "failed to load Message <%ld> from disk\n",
619 network_process_list(sc, msg, &delete_after_send);
620 network_process_digest(sc, msg, &delete_after_send);
621 network_process_participate(sc, msg, &delete_after_send);
622 network_process_ignetpush(sc, msg, &delete_after_send);
624 CtdlFreeMessage(msg);
626 /* update lastsent */
627 sc->lastsent = msgnum;
629 /* Delete this message if delete-after-send is set */
630 if (delete_after_send) {
631 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");