2 * This module handles shared rooms, inter-Citadel mail, and outbound
3 * mailing list processing.
5 * Copyright (c) 2000-2016 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"
75 #include "citadel_dirs.h"
79 #include "ctdl_module.h"
91 #define IFTODT(mode) (((mode) & 0170000) >> 12)
92 #define DTTOIF(dirtype) ((dirtype) << 12)
97 * Bounce a message back to the sender
99 void network_bounce(struct CtdlMessage **pMsg, char *reason)
101 struct CitContext *CCC = CC;
103 char bouncesource[SIZ];
105 recptypes *valid = NULL;
106 char force_room[ROOMNAMELEN];
107 static int serialnum = 0;
109 struct CtdlMessage *msg = *pMsg;
111 QNM_syslog(LOG_DEBUG, "entering network_bounce()");
113 if (msg == NULL) return;
115 snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, CtdlGetConfigStr("c_nodename"));
118 * Give it a fresh message ID
120 len = snprintf(buf, sizeof(buf),
125 CtdlGetConfigStr("c_fqdn"));
127 CM_SetField(msg, emessageId, buf, len);
130 * FIXME ... right now we're just sending a bounce; we really want to
131 * include the text of the bounced message.
133 if (!IsEmptyStr(reason)) {
134 CM_SetField(msg, eMesageText, reason, strlen(reason));
136 msg->cm_format_type = 0;
139 * Turn the message around
141 CM_FlushField(msg, eRecipient);
142 CM_FlushField(msg, eDestination);
144 len = snprintf(recipient, sizeof(recipient), "%s@%s",
145 msg->cm_fields[eAuthor],
146 msg->cm_fields[eNodeName]);
148 CM_SetField(msg, eAuthor, HKEY(BOUNCESOURCE));
149 CM_SetField(msg, eNodeName, CtdlGetConfigStr("c_nodename"), strlen(CtdlGetConfigStr("c_nodename")));
150 CM_SetField(msg, eMsgSubject, HKEY("Delivery Status Notification (Failure)"));
152 Netmap_AddMe(msg, HKEY("unknown_user"));
154 /* Now submit the message */
155 valid = validate_recipients(recipient, NULL, 0);
156 if (valid != NULL) if (valid->num_error != 0) {
157 free_recipients(valid);
160 if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
161 strcpy(force_room, CtdlGetConfigStr("c_aideroom"));
164 strcpy(force_room, "");
166 if ( (valid == NULL) && IsEmptyStr(force_room) ) {
167 strcpy(force_room, CtdlGetConfigStr("c_aideroom"));
169 CtdlSubmitMsg(msg, valid, force_room, 0);
172 if (valid != NULL) free_recipients(valid);
174 QNM_syslog(LOG_DEBUG, "leaving network_bounce()");
178 void ParseLastSent(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
180 RoomNetCfgLine *nptr;
181 nptr = (RoomNetCfgLine *)
182 malloc(sizeof(RoomNetCfgLine));
183 memset(nptr, 0, sizeof(RoomNetCfgLine));
184 OneRNCFG->lastsent = extract_long(LinePos, 0);
185 OneRNCFG->NetConfigs[ThisOne->C] = nptr;
188 void ParseRoomAlias(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg)
190 if (rncfg->Sender != NULL)
193 ParseGeneric(ThisOne, Line, LinePos, rncfg);
194 rncfg->Sender = NewStrBufDup(rncfg->NetConfigs[roommailalias]->Value[0]);
197 void ParseSubPendingLine(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
199 if (time(NULL) - extract_long(LinePos, 3) > EXP)
200 return; /* expired subscription... */
202 ParseGeneric(ThisOne, Line, LinePos, OneRNCFG);
204 void ParseUnSubPendingLine(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *OneRNCFG)
206 if (time(NULL) - extract_long(LinePos, 2) > EXP)
207 return; /* expired subscription... */
209 ParseGeneric(ThisOne, Line, LinePos, OneRNCFG);
213 void SerializeLastSent(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *RNCfg, RoomNetCfgLine *data)
215 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
216 StrBufAppendPrintf(OutputBuffer, "|%ld\n", RNCfg->lastsent);
219 void DeleteLastSent(const CfgLineType *ThisOne, RoomNetCfgLine **data)
225 static const RoomNetCfg SpoolCfgs [4] = {
232 static const long SpoolCfgsCopyN [4] = {
236 int HaveSpoolConfig(OneRoomNetCfg* RNCfg)
240 for (i=0; i < 4; i++) if (RNCfg->NetConfigs[SpoolCfgs[i]] == NULL) interested = 1;
244 void Netmap_AddMe(struct CtdlMessage *msg, const char *defl, long defllen)
249 /* prepend our node to the path */
250 if (CM_IsEmpty(msg, eMessagePath)) {
251 CM_SetField(msg, eMessagePath, defl, defllen);
253 node_len = strlen(CtdlGetConfigStr("c_nodename"));
256 memcpy(buf, CtdlGetConfigStr("c_nodename"), node_len);
258 buf[node_len + 1] = '\0';
259 CM_PrependToField(msg, eMessagePath, buf, node_len + 1);
262 void InspectQueuedRoom(SpoolControl **pSC,
263 RoomProcList *room_to_spool,
264 HashList *working_ignetcfg,
265 HashList *the_netmap)
267 struct CitContext *CCC = CC;
271 sc = (SpoolControl*)malloc(sizeof(SpoolControl));
272 memset(sc, 0, sizeof(SpoolControl));
273 sc->RNCfg = room_to_spool->OneRNCfg;
274 sc->lastsent = room_to_spool->lastsent;
275 sc->working_ignetcfg = working_ignetcfg;
276 sc->the_netmap = the_netmap;
279 * If the room doesn't exist, don't try to perform its networking tasks.
280 * Normally this should never happen, but once in a while maybe a room gets
281 * queued for networking and then deleted before it can happen.
283 if (CtdlGetRoom(&sc->room, room_to_spool->name) != 0) {
284 syslog(LOG_CRIT, "ERROR: cannot load <%s>", room_to_spool->name);
288 if (sc->room.QRhighest <= sc->lastsent)
290 QN_syslog(LOG_DEBUG, "nothing to do for <%s>", room_to_spool->name);
295 begin_critical_section(S_NETCONFIGS);
296 if (sc->RNCfg == NULL) {
297 sc->RNCfg = CtdlGetNetCfgForRoom(sc->room.QRnumber);
300 if (!HaveSpoolConfig(sc->RNCfg))
302 end_critical_section(S_NETCONFIGS);
304 /* nothing to do for this room... */
308 /* Now lets remember whats needed for the actual work... */
310 for (i=0; i < 4; i++)
312 aggregate_recipients(&sc->Users[SpoolCfgs[i]],
319 if (StrLength(sc->RNCfg->Sender) > 0) {
320 sc->Users[roommailalias] = NewStrBufDup(sc->RNCfg->Sender);
322 end_critical_section(S_NETCONFIGS);
329 void CalcListID(SpoolControl *sc)
334 struct CitContext *CCC = CC;
335 char filename[PATH_MAX];
336 #define MAX_LISTIDLENGTH 150
338 assoc_file_name(filename, sizeof filename, &sc->room, ctdl_info_dir);
339 fd = open(filename, 0);
344 if ((fstat(fd, &stbuf) == 0) &&
347 sc->RoomInfo = NewStrBufPlain(NULL, stbuf.st_size + 1);
348 StrBufReadBLOB(sc->RoomInfo, &fd, 0, stbuf.st_size, &err);
353 sc->ListID = NewStrBufPlain(NULL, 1024);
354 if (StrLength(sc->RoomInfo) > 0)
356 const char *Pos = NULL;
357 StrBufSipLine(sc->ListID, sc->RoomInfo, &Pos);
359 if (StrLength(sc->ListID) > MAX_LISTIDLENGTH)
361 StrBufCutAt(sc->ListID, MAX_LISTIDLENGTH, NULL);
362 StrBufAppendBufPlain(sc->ListID, HKEY("..."), 0);
364 StrBufAsciify(sc->ListID, ' ');
368 StrBufAppendBufPlain(sc->ListID, CCC->room.QRname, -1, 0);
371 StrBufAppendBufPlain(sc->ListID, HKEY("<"), 0);
372 RoomName = NewStrBufPlain (sc->room.QRname, -1);
373 StrBufAsciify(RoomName, '_');
374 StrBufReplaceChars(RoomName, ' ', '_');
376 if (StrLength(sc->Users[roommailalias]) > 0)
381 Pos = StrLength(sc->ListID);
382 StrBufAppendBuf(sc->ListID, sc->Users[roommailalias], 0);
383 AtPos = strchr(ChrPtr(sc->ListID) + Pos, '@');
387 StrBufPeek(sc->ListID, AtPos, 0, '.');
392 StrBufAppendBufPlain(sc->ListID, HKEY("room_"), 0);
393 StrBufAppendBuf(sc->ListID, RoomName, 0);
394 StrBufAppendBufPlain(sc->ListID, HKEY("."), 0);
395 StrBufAppendBufPlain(sc->ListID, CtdlGetConfigStr("c_fqdn"), -1, 0);
398 * roomname <Room-Number.list-id.fqdn>
399 * according to rfc2919.txt it only has to be a uniq identifier
400 * under the domain of the system;
401 * in general MUAs use it to calculate the reply address nowadays.
404 StrBufAppendBufPlain(sc->ListID, HKEY(">"), 0);
406 if (StrLength(sc->Users[roommailalias]) == 0)
408 sc->Users[roommailalias] = NewStrBuf();
410 StrBufAppendBufPlain(sc->Users[roommailalias], HKEY("room_"), 0);
411 StrBufAppendBuf(sc->Users[roommailalias], RoomName, 0);
412 StrBufAppendBufPlain(sc->Users[roommailalias], HKEY("@"), 0);
413 StrBufAppendBufPlain(sc->Users[roommailalias], CtdlGetConfigStr("c_fqdn"), -1, 0);
415 StrBufLowerCase(sc->Users[roommailalias]);
418 FreeStrBuf(&RoomName);
421 static time_t last_digest_delivery = 0;
424 * Batch up and send all outbound traffic from the current room
426 void network_spoolout_room(SpoolControl *sc)
428 struct CitContext *CCC = CC;
434 * If the room doesn't exist, don't try to perform its networking tasks.
435 * Normally this should never happen, but once in a while maybe a room gets
436 * queued for networking and then deleted before it can happen.
438 memcpy (&CCC->room, &sc->room, sizeof(ctdlroom));
440 syslog(LOG_INFO, "Networking started for <%s>", CCC->room.QRname);
444 /* remember where we started... */
445 lastsent = sc->lastsent;
447 /* Fetch the messages we ought to send & prepare them. */
448 CtdlForEachMessage(MSGS_GT, sc->lastsent, NULL, NULL, NULL,
449 network_spool_msg, sc);
451 if (StrLength(sc->Users[roommailalias]) > 0)
454 len = StrLength(sc->Users[roommailalias]);
455 if (len + 1 > sizeof(buf))
456 len = sizeof(buf) - 1;
457 memcpy(buf, ChrPtr(sc->Users[roommailalias]), len);
462 snprintf(buf, sizeof buf, "room_%s@%s",
463 CCC->room.QRname, CtdlGetConfigStr("c_fqdn"));
466 for (i=0; buf[i]; ++i) {
467 buf[i] = tolower(buf[i]);
468 if (isspace(buf[i])) buf[i] = '_';
472 /* If we wrote a digest, deliver it and then close it */
473 if (sc->Users[digestrecp] != NULL) {
474 time_t now = time(NULL);
475 time_t secs_today = now % (24 * 60 * 60);
478 if (last_digest_delivery != 0) {
479 delta = now - last_digest_delivery;
480 delta = (24 * 60 * 60) - delta;
483 if ((secs_today < 300) &&
486 if (sc->digestfp == NULL) {
487 sc->digestfp = create_digest_file(&sc->room, 0);
489 if (sc->digestfp != NULL) {
490 last_digest_delivery = now;
491 fprintf(sc->digestfp,
492 " -----------------------------------"
493 "------------------------------------"
495 "You are subscribed to the '%s' "
497 "To post to the list: %s\n",
498 CCC->room.QRname, buf
500 network_deliver_digest(sc); /* deliver */
501 remove_digest_file(&sc->room);
505 if (sc->digestfp != NULL) {
506 fclose(sc->digestfp);
510 /* Now rewrite the netconfig */
512 // THIS IS THE ONLY PLACE WHERE WE HAVE TO REWRITE THE NETCONFIG.
515 if (sc->lastsent != lastsent)
517 begin_critical_section(S_NETCONFIGS);
518 sc->RNCfg = CtdlGetNetCfgForRoom(sc->room.QRnumber);
520 sc->RNCfg->lastsent = sc->lastsent;
521 sc->RNCfg->changed = 1;
522 end_critical_section(S_NETCONFIGS);
528 * Check the use table. This is a list of messages which have recently
529 * arrived on the system. It is maintained and queried to prevent the same
530 * message from being entered into the database multiple times if it happens
531 * to arrive multiple times by accident.
533 int network_usetable(struct CtdlMessage *msg)
536 struct CitContext *CCC = CC;
539 /* Bail out if we can't generate a message ID */
540 if ((msg == NULL) || CM_IsEmpty(msg, emessageId))
545 /* Generate the message ID */
546 msgid = NewStrBufPlain(CM_KEY(msg, emessageId));
547 if (haschar(ChrPtr(msgid), '@') == 0) {
548 StrBufAppendBufPlain(msgid, HKEY("@"), 0);
549 if (!CM_IsEmpty(msg, eNodeName)) {
550 StrBufAppendBufPlain(msgid, CM_KEY(msg, eNodeName), 0);
558 if (CheckIfAlreadySeen("Networker Import",
562 CCC->cs_pid, 0) != 0)
574 * Process a buffer containing a single message from a single file
575 * from the inbound queue
577 void network_process_buffer(char *buffer, long size, HashList *working_ignetcfg, HashList *the_netmap, int *netmap_changed)
580 struct CitContext *CCC = CC;
582 struct CtdlMessage *msg = NULL;
585 recptypes *recp = NULL;
586 char target_room[ROOMNAMELEN];
587 struct ser_ret sermsg;
588 char filename[PATH_MAX];
590 const StrBuf *nexthop = NULL;
591 unsigned char firstbyte;
592 unsigned char lastbyte;
594 QN_syslog(LOG_DEBUG, "network_process_buffer() processing %ld bytes", size);
596 /* Validate just a little bit. First byte should be FF and * last byte should be 00. */
597 firstbyte = buffer[0];
598 lastbyte = buffer[size-1];
599 if ( (firstbyte != 255) || (lastbyte != 0) ) {
600 QN_syslog(LOG_ERR, "Corrupt message ignored. Length=%ld, firstbyte = %d, lastbyte = %d", size, firstbyte, lastbyte);
604 /* Set default target room to trash */
605 strcpy(target_room, TWITROOM);
607 /* Load the message into memory */
608 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
609 memset(msg, 0, sizeof(struct CtdlMessage));
610 msg->cm_magic = CTDLMESSAGE_MAGIC;
611 msg->cm_anon_type = buffer[1];
612 msg->cm_format_type = buffer[2];
614 for (pos = 3; pos < size; ++pos) {
616 len = strlen(buffer + pos + 1);
617 CM_SetField(msg, field, buffer + pos + 1, len);
621 /* Check for message routing */
622 if (!CM_IsEmpty(msg, eDestination)) {
623 if (strcasecmp(msg->cm_fields[eDestination], CtdlGetConfigStr("c_nodename"))) {
625 /* route the message */
626 Buf = NewStrBufPlain(CM_KEY(msg,eDestination));
627 if (CtdlIsValidNode(&nexthop,
633 Netmap_AddMe(msg, HKEY("unknown_user"));
635 /* serialize the message */
636 CtdlSerializeMessage(&sermsg, msg);
639 if (StrLength(nexthop) == 0) {
650 QN_syslog(LOG_DEBUG, "Appending to %s", filename);
651 fp = fopen(filename, "ab");
653 fwrite(sermsg.ser, sermsg.len, 1, fp);
657 QN_syslog(LOG_ERR, "%s: %s", filename, strerror(errno));
665 else { /* invalid destination node name */
669 "A message you sent could not be delivered due to an invalid destination node"
670 " name. Please check the address and try sending the message again.\n");
678 * Check to see if we already have a copy of this message, and
679 * abort its processing if so. (We used to post a warning to Aide>
680 * every time this happened, but the network is now so densely
681 * connected that it's inevitable.)
683 if (network_usetable(msg) != 0) {
688 /* Learn network topology from the path */
689 if (!CM_IsEmpty(msg, eNodeName) && !CM_IsEmpty(msg, eMessagePath)) {
690 NetworkLearnTopology(msg->cm_fields[eNodeName],
691 msg->cm_fields[eMessagePath],
696 /* Is the sending node giving us a very persuasive suggestion about
697 * which room this message should be saved in? If so, go with that.
699 if (!CM_IsEmpty(msg, eRemoteRoom)) {
700 safestrncpy(target_room, msg->cm_fields[eRemoteRoom], sizeof target_room);
703 /* Otherwise, does it have a recipient? If so, validate it... */
704 else if (!CM_IsEmpty(msg, eRecipient)) {
705 recp = validate_recipients(msg->cm_fields[eRecipient], NULL, 0);
706 if (recp != NULL) if (recp->num_error != 0) {
708 "A message you sent could not be delivered due to an invalid address.\n"
709 "Please check the address and try sending the message again.\n");
710 free_recipients(recp);
711 QNM_syslog(LOG_DEBUG, "Bouncing message due to invalid recipient address.");
714 strcpy(target_room, ""); /* no target room if mail */
717 /* Our last shot at finding a home for this message is to see if
718 * it has the eOriginalRoom (O) field (Originating room) set.
720 else if (!CM_IsEmpty(msg, eOriginalRoom)) {
721 safestrncpy(target_room, msg->cm_fields[eOriginalRoom], sizeof target_room);
724 /* Strip out fields that are only relevant during transit */
725 CM_FlushField(msg, eDestination);
726 CM_FlushField(msg, eRemoteRoom);
728 /* save the message into a room */
729 if (PerformNetprocHooks(msg, target_room) == 0) {
730 msg->cm_flags = CM_SKIP_HOOKS;
731 CtdlSubmitMsg(msg, recp, target_room, 0);
734 free_recipients(recp);
739 * Process a single message from a single file from the inbound queue
741 void network_process_message(FILE *fp,
744 HashList *working_ignetcfg,
745 HashList *the_netmap,
752 hold_pos = ftell(fp);
753 size = msgend - msgstart + 1;
754 buffer = malloc(size);
755 if (buffer != NULL) {
756 fseek(fp, msgstart, SEEK_SET);
757 if (fread(buffer, size, 1, fp) > 0) {
758 network_process_buffer(buffer,
767 fseek(fp, hold_pos, SEEK_SET);
772 * Process a single file from the inbound queue
774 void network_process_file(char *filename,
775 HashList *working_ignetcfg,
776 HashList *the_netmap,
779 struct CitContext *CCC = CC;
781 long msgstart = (-1L);
787 fp = fopen(filename, "rb");
789 QN_syslog(LOG_CRIT, "Error opening %s: %s", filename, strerror(errno));
793 fseek(fp, 0L, SEEK_END);
794 QN_syslog(LOG_INFO, "network: processing %ld bytes from %s", ftell(fp), filename);
797 /* Look for messages in the data stream and break them out */
798 while (ch = getc(fp), ch >= 0) {
801 if (msgstart >= 0L) {
803 network_process_message(fp,
818 if (msgstart >= 0L) {
819 network_process_message(fp,
829 QN_syslog(LOG_INFO, "network: processed %d messages in %s", nMessages, filename);
837 * Process anything in the inbound queue
839 void network_do_spoolin(HashList *working_ignetcfg, HashList *the_netmap, int *netmap_changed)
841 struct CitContext *CCC = CC;
844 struct dirent *filedir_entry;
846 char filename[PATH_MAX];
847 static time_t last_spoolin_mtime = 0L;
852 * Check the spoolin directory's modification time. If it hasn't
853 * been touched, we don't need to scan it.
855 if (stat(ctdl_netin_dir, &statbuf)) return;
856 if (statbuf.st_mtime == last_spoolin_mtime) {
857 QNM_syslog(LOG_DEBUG, "network: nothing in inbound queue");
860 last_spoolin_mtime = statbuf.st_mtime;
861 QNM_syslog(LOG_DEBUG, "network: processing inbound queue");
864 * Ok, there's something interesting in there, so scan it.
866 dp = opendir(ctdl_netin_dir);
867 if (dp == NULL) return;
869 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
875 while ((readdir_r(dp, d, &filedir_entry) == 0) &&
876 (filedir_entry != NULL))
878 #ifdef _DIRENT_HAVE_D_NAMLEN
879 d_namelen = filedir_entry->d_namlen;
882 d_namelen = strlen(filedir_entry->d_name);
885 #ifdef _DIRENT_HAVE_D_TYPE
886 d_type = filedir_entry->d_type;
890 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
891 continue; /* Ignore backup files... */
893 if ((d_namelen == 1) &&
894 (filedir_entry->d_name[0] == '.'))
897 if ((d_namelen == 2) &&
898 (filedir_entry->d_name[0] == '.') &&
899 (filedir_entry->d_name[1] == '.'))
902 if (d_type == DT_UNKNOWN) {
910 filedir_entry->d_name);
912 if (lstat(path, &s) == 0) {
913 d_type = IFTODT(s.st_mode);
921 case DT_LNK: /* TODO: check whether its a file or a directory */
929 network_process_file(filename,
941 * Step 1: consolidate files in the outbound queue into one file per neighbor node
942 * Step 2: delete any files in the outbound queue that were for neighbors who no longer exist.
944 void network_consolidate_spoolout(HashList *working_ignetcfg, HashList *the_netmap)
946 struct CitContext *CCC = CC;
952 struct dirent *filedir_entry;
954 char spooloutfilename[PATH_MAX];
955 char filename[PATH_MAX];
956 const StrBuf *nexthop;
964 /* Step 1: consolidate files in the outbound queue into one file per neighbor node */
965 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
966 if (d == NULL) return;
968 dp = opendir(ctdl_netout_dir);
974 NextHop = NewStrBuf();
975 memset(&IOB, 0, sizeof(IOBuffer));
976 memset(&FDIO, 0, sizeof(FDIOBuffer));
979 while ((readdir_r(dp, d, &filedir_entry) == 0) &&
980 (filedir_entry != NULL))
982 #ifdef _DIRENT_HAVE_D_NAMLEN
983 d_namelen = filedir_entry->d_namlen;
986 d_namelen = strlen(filedir_entry->d_name);
989 #ifdef _DIRENT_HAVE_D_TYPE
990 d_type = filedir_entry->d_type;
994 if (d_type == DT_DIR)
997 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
998 continue; /* Ignore backup files... */
1000 if ((d_namelen == 1) &&
1001 (filedir_entry->d_name[0] == '.'))
1004 if ((d_namelen == 2) &&
1005 (filedir_entry->d_name[0] == '.') &&
1006 (filedir_entry->d_name[1] == '.'))
1009 pch = strchr(filedir_entry->d_name, '@');
1017 filedir_entry->d_name);
1019 StrBufPlain(NextHop,
1020 filedir_entry->d_name,
1021 pch - filedir_entry->d_name);
1023 snprintf(spooloutfilename,
1024 sizeof spooloutfilename,
1029 QN_syslog(LOG_DEBUG, "Consolidate %s to %s", filename, ChrPtr(NextHop));
1030 if (CtdlNetworkTalkingTo(SKEY(NextHop), NTT_CHECK)) {
1032 QN_syslog(LOG_DEBUG, "Currently online with %s - skipping for now", ChrPtr(NextHop));
1038 const char *err = NULL;
1039 CtdlNetworkTalkingTo(SKEY(NextHop), NTT_ADD);
1041 infd = open(filename, O_RDONLY);
1045 "failed to open %s for reading due to %s; skipping.",
1046 filename, strerror(errno)
1048 CtdlNetworkTalkingTo(SKEY(NextHop), NTT_REMOVE);
1052 outfd = open(spooloutfilename,
1053 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
1057 outfd = open(spooloutfilename,
1058 O_EXCL|O_NONBLOCK|O_WRONLY,
1064 "failed to open %s for reading due to %s; skipping.",
1065 spooloutfilename, strerror(errno)
1068 CtdlNetworkTalkingTo(SKEY(NextHop), NTT_REMOVE);
1072 dsize = lseek(outfd, 0, SEEK_END);
1073 lseek(outfd, -dsize, SEEK_SET);
1075 fstat(infd, &statbuf);
1076 fsize = statbuf.st_size;
1078 fsize = lseek(infd, 0, SEEK_END);
1081 FDIOBufferInit(&FDIO, &IOB, outfd, fsize + dsize);
1082 FDIO.ChunkSendRemain = fsize;
1083 FDIO.TotalSentAlready = dsize;
1086 do {} while ((FileMoveChunked(&FDIO, &err) > 0) && (err == NULL));
1089 QN_syslog(LOG_DEBUG, "Spoolfile %s now "SIZE_T_FMT" KB", spooloutfilename, (dsize + fsize)/1024);
1093 QN_syslog(LOG_ERR, "failed to append to %s [%s]; rolling back..", spooloutfilename, strerror(errno));
1094 /* whoops partial append?? truncate spooloutfilename again! */
1095 ftruncate(outfd, dsize);
1097 FDIOBufferDelete(&FDIO);
1100 CtdlNetworkTalkingTo(SKEY(NextHop), NTT_REMOVE);
1106 FreeStrBuf(&NextHop);
1107 QN_syslog(LOG_INFO, "skipping Spoolcleanup because of %d files unprocessed.", nFailed);
1112 /* Step 2: delete any files in the outbound queue that were for neighbors who no longer exist */
1113 dp = opendir(ctdl_netout_dir);
1115 FreeStrBuf(&NextHop);
1120 while ((readdir_r(dp, d, &filedir_entry) == 0) &&
1121 (filedir_entry != NULL))
1123 #ifdef _DIRENT_HAVE_D_NAMLEN
1124 d_namelen = filedir_entry->d_namlen;
1127 d_namelen = strlen(filedir_entry->d_name);
1130 #ifdef _DIRENT_HAVE_D_TYPE
1131 d_type = filedir_entry->d_type;
1133 d_type = DT_UNKNOWN;
1135 if (d_type == DT_DIR)
1138 if ((d_namelen == 1) &&
1139 (filedir_entry->d_name[0] == '.'))
1142 if ((d_namelen == 2) &&
1143 (filedir_entry->d_name[0] == '.') &&
1144 (filedir_entry->d_name[1] == '.'))
1147 pch = strchr(filedir_entry->d_name, '@');
1148 if (pch == NULL) /* no @ in name? consolidated file. */
1151 StrBufPlain(NextHop,
1152 filedir_entry->d_name,
1153 pch - filedir_entry->d_name);
1159 filedir_entry->d_name
1162 i = CtdlIsValidNode(&nexthop,
1168 if ( (i != 0) || (StrLength(nexthop) > 0) ) {
1172 FreeStrBuf(&NextHop);
1177 void free_spoolcontrol_struct(SpoolControl **sc)
1179 free_spoolcontrol_struct_members(*sc);
1184 void free_spoolcontrol_struct_members(SpoolControl *sc)
1187 FreeStrBuf(&sc->RoomInfo);
1188 FreeStrBuf(&sc->ListID);
1189 for (i = 0; i < maxRoomNetCfg; i++)
1190 FreeStrBuf(&sc->Users[i]);
1196 * It's ok if these directories already exist. Just fail silently.
1198 void create_spool_dirs(void) {
1199 if ((mkdir(ctdl_spool_dir, 0700) != 0) && (errno != EEXIST))
1200 syslog(LOG_EMERG, "unable to create directory [%s]: %s", ctdl_spool_dir, strerror(errno));
1201 if (chown(ctdl_spool_dir, CTDLUID, (-1)) != 0)
1202 syslog(LOG_EMERG, "unable to set the access rights for [%s]: %s", ctdl_spool_dir, strerror(errno));
1203 if ((mkdir(ctdl_netin_dir, 0700) != 0) && (errno != EEXIST))
1204 syslog(LOG_EMERG, "unable to create directory [%s]: %s", ctdl_netin_dir, strerror(errno));
1205 if (chown(ctdl_netin_dir, CTDLUID, (-1)) != 0)
1206 syslog(LOG_EMERG, "unable to set the access rights for [%s]: %s", ctdl_netin_dir, strerror(errno));
1207 if ((mkdir(ctdl_nettmp_dir, 0700) != 0) && (errno != EEXIST))
1208 syslog(LOG_EMERG, "unable to create directory [%s]: %s", ctdl_nettmp_dir, strerror(errno));
1209 if (chown(ctdl_nettmp_dir, CTDLUID, (-1)) != 0)
1210 syslog(LOG_EMERG, "unable to set the access rights for [%s]: %s", ctdl_nettmp_dir, strerror(errno));
1211 if ((mkdir(ctdl_netout_dir, 0700) != 0) && (errno != EEXIST))
1212 syslog(LOG_EMERG, "unable to create directory [%s]: %s", ctdl_netout_dir, strerror(errno));
1213 if (chown(ctdl_netout_dir, CTDLUID, (-1)) != 0)
1214 syslog(LOG_EMERG, "unable to set the access rights for [%s]: %s", ctdl_netout_dir, strerror(errno));
1218 * Module entry point
1220 CTDL_MODULE_INIT(network_spool)
1224 CtdlREGISTERRoomCfgType(subpending, ParseSubPendingLine, 0, 5, SerializeGeneric, DeleteGenericCfgLine); /// todo: move this to mailinglist manager
1225 CtdlREGISTERRoomCfgType(unsubpending, ParseUnSubPendingLine, 0, 4, SerializeGeneric, DeleteGenericCfgLine); /// todo: move this to mailinglist manager
1226 CtdlREGISTERRoomCfgType(lastsent, ParseLastSent, 1, 1, SerializeLastSent, DeleteLastSent);
1227 CtdlREGISTERRoomCfgType(ignet_push_share, ParseGeneric, 0, 2, SerializeGeneric, DeleteGenericCfgLine); // [remotenode|remoteroomname (optional)]// todo: move this to the ignet client
1228 CtdlREGISTERRoomCfgType(listrecp, ParseGeneric, 0, 1, SerializeGeneric, DeleteGenericCfgLine);
1229 CtdlREGISTERRoomCfgType(digestrecp, ParseGeneric, 0, 1, SerializeGeneric, DeleteGenericCfgLine);
1230 CtdlREGISTERRoomCfgType(participate, ParseGeneric, 0, 1, SerializeGeneric, DeleteGenericCfgLine);
1231 CtdlREGISTERRoomCfgType(roommailalias, ParseRoomAlias, 0, 1, SerializeGeneric, DeleteGenericCfgLine);
1233 create_spool_dirs();
1234 //////todo CtdlRegisterCleanupHook(destroy_network_queue_room);
1236 return "network_spool";