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 "netconfig.h"
82 #include "ctdl_module.h"
85 HashList *CfgTypeHash = NULL;
86 typedef struct CfgLineType CfgLineType;
87 typedef void (*CfgLineParser)(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, SpoolControl *sc);
88 typedef void (*CfgLineSerializer)(const CfgLineType *ThisOne, StrBuf *OuptputBuffer, SpoolControl *sc, namelist *data);
89 typedef void (*CfgLineDeAllocator)(const CfgLineType *ThisOne, namelist **data);
93 CfgLineSerializer Serializer;
94 CfgLineDeAllocator DeAllocator;
98 #define REGISTERRoomCfgType(a, p, uniq, s, d) RegisterRoomCfgType(#a, sizeof(#a) - 1, a, p, uniq, s, d);
100 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, CfgLineSerializer s, CfgLineDeAllocator d)
104 pCfg = (CfgLineType*) malloc(sizeof(CfgLineType));
106 pCfg->Serializer = s;
108 pCfg->Str.Key = Name;
110 pCfg->IsSingleLine = uniq;
112 if (CfgTypeHash == NULL)
113 CfgTypeHash = NewHash(1, NULL);
114 Put(CfgTypeHash, Name, len, pCfg, NULL);
117 const CfgLineType *GetCfgTypeByStr(const char *Key, long len)
121 if (GetHash(CfgTypeHash, Key, len, &pv) && (pv != NULL))
123 return (const CfgLineType *) pv;
131 const CfgLineType *GetCfgTypeByEnum(RoomNetCfg eCfg, HashPos *It)
138 RewindHashPos(CfgTypeHash, It, 1);
139 while (GetNextHashPos(CfgTypeHash, It, &len, &Key, &pv) && (pv != NULL))
141 pCfg = (CfgLineType*) pv;
147 void ParseDefault(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, SpoolControl *sc)
152 malloc(sizeof(namelist));
153 nptr->next = sc->NetConfigs[ThisOne->C];
154 nptr->Value = NewStrBufPlain(LinePos, StrLength(Line) - ( LinePos - ChrPtr(Line)) );
155 sc->NetConfigs[ThisOne->C] = nptr;
158 void ParseLastSent(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, SpoolControl *sc)
160 sc->lastsent = extract_long(LinePos, 0);
162 void ParseIgnetPushShare(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, SpoolControl *sc)
165 extract_token(nodename, LinePos, 0, '|', sizeof nodename);
166 extract_token(roomname, LinePos, 1, '|', sizeof roomname);
167 mptr = (maplist *) malloc(sizeof(maplist));
168 mptr->next = sc->ignet_push_shares;
169 strcpy(mptr->remote_nodename, nodename);
170 strcpy(mptr->remote_roomname, roomname);
171 sc->ignet_push_shares = mptr;
175 void ParseRoomAlias(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, SpoolControl *sc)
178 if (sc->sender != NULL)
179 continue; / * just one alowed... * /
180 extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
185 void ParseSubPendingLine(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, SpoolControl *sc)
188 if (time(NULL) - extract_long(LinePos, 3) > EXP) {
196 void ParseUnSubPendingLine(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, SpoolControl *sc)
198 ///int skipthisline = 0;
199 if (time(NULL) - extract_long(LinePos, 2) > EXP) {
205 void DeleteGenericCfgLine(const CfgLineType *ThisOne, namelist **data)
207 FreeStrBuf(&(*data)->Value);
211 int read_spoolcontrol_file(SpoolControl **scc, char *filename)
214 const char *ErrStr = NULL;
216 const CfgLineType *pCfg;
221 fd = open(filename, O_NONBLOCK|O_RDONLY);
226 sc = malloc(sizeof(SpoolControl));
227 memset(sc, 0, sizeof(SpoolControl));
230 while (StrBufTCP_read_line(Line, &fd, 0, &ErrStr) >= 0) {
231 if (StrLength(Line) == 0)
234 InStr = NewStrBufPlain(NULL, StrLength(Line));
235 StrBufExtract_NextToken(InStr, Line, &Pos, '|');
237 pCfg = GetCfgTypeByStr(SKEY(InStr));
240 pCfg->Parser(pCfg, Line, Pos, sc);
244 if (sc->misc == NULL)
246 sc->misc = NewStrBufDup(Line);
250 if(StrLength(sc->misc) > 0)
251 StrBufAppendBufPlain(sc->misc, HKEY("\n"), 0);
252 StrBufAppendBuf(sc->misc, Line, 0);
263 void SerializeLastSent(const CfgLineType *ThisOne, StrBuf *OutputBuffer, SpoolControl *sc, namelist *data)
265 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
266 StrBufAppendPrintf(OutputBuffer, "|%ld\n", sc->lastsent);
269 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, SpoolControl *sc, namelist *data)
271 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
272 StrBufAppendBuf(OutputBuffer, data->Value, 0);
273 StrBufAppendBufPlain(OutputBuffer, HKEY("\n"), 0);
275 void SerializeIgnetPushShare(const CfgLineType *ThisOne, StrBuf *OutputBuffer, SpoolControl *sc, namelist *data)
277 StrBufAppendBufPlain(OutputBuffer, CKEY(ThisOne->Str), 0);
279 StrBufAppendPrintf(Cfg, "ignet_push_share|%s", sc->ignet_push_shares->remote_nodename);
280 if (!IsEmptyStr(sc->ignet_push_shares->remote_roomname)) {
281 StrBufAppendPrintf(Cfg, "|%s", sc->ignet_push_shares->remote_roomname);
283 StrBufAppendPrintf(Cfg, "\n");
284 mptr = sc->ignet_push_shares->next;
285 free(sc->ignet_push_shares);
286 sc->ignet_push_shares = mptr;
288 StrBufAppendBuf(OutputBuffer, data->Value, 0);
289 StrBufAppendBufPlain(OutputBuffer, HKEY("\n"), 0);
292 int save_spoolcontrol_file(SpoolControl *sc, char *filename)
296 char tempfilename[PATH_MAX];
301 long reltid; /* if we don't have SYS_gettid, use "random" value */
306 len = strlen(filename);
307 memcpy(tempfilename, filename, len + 1);
310 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
311 reltid = syscall(SYS_gettid);
313 gettimeofday(&tv, NULL);
314 /* Promote to time_t; types differ on some OSes (like darwin) */
315 unixtime = tv.tv_sec;
317 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
319 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
321 if ((TmpFD < 0) || (errno != 0)) {
322 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
323 filename, strerror(errno));
324 unlink(tempfilename);
328 CfgIt = GetNewHashPos(CfgTypeHash, 1);
329 fchown(TmpFD, config.c_ctdluid, 0);
330 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
332 const CfgLineType *pCfg;
333 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
334 if (pCfg->IsSingleLine)
336 pCfg->Serializer(pCfg, OutBuffer, sc, NULL);
340 namelist *pName = sc->NetConfigs[pCfg->C];
341 while (pName != NULL)
343 pCfg->Serializer(pCfg, OutBuffer, sc, pName);
351 DeleteHashPos(&CfgIt);
354 if (sc->misc != NULL) {
355 StrBufAppendBuf(OutBuffer, sc->misc, 0);
358 rc = write(TmpFD, ChrPtr(OutBuffer), StrLength(OutBuffer));
359 if ((rc >=0 ) && (rc == StrLength(Cfg)))
362 rename(tempfilename, filename);
367 "unable to write %s; [%s]; not enough space on the disk?\n",
371 unlink(tempfilename);
374 FreeStrBuf(&OutBuffer);
382 void free_spoolcontrol_struct(SpoolControl **scc)
389 CfgIt = GetNewHashPos(CfgTypeHash, 1);
390 for (eCfg = subpending; eCfg < maxRoomNetCfg; eCfg ++)
392 const CfgLineType *pCfg;
393 namelist *pNext, *pName;
395 pCfg = GetCfgTypeByEnum(eCfg, CfgIt);
396 pName= sc->NetConfigs[pCfg->C];
397 while (pName != NULL)
400 pCfg->DeAllocator(pCfg, &pName);
404 DeleteHashPos(&CfgIt);
406 FreeStrBuf(&sc->Sender);
407 FreeStrBuf(&sc->RoomInfo);
408 FreeStrBuf(&sc->misc);
414 int writenfree_spoolcontrol_file(SpoolControl **scc, char *filename)
416 char tempfilename[PATH_MAX];
419 namelist *nptr = NULL;
420 maplist *mptr = NULL;
424 long reltid; /* if we don't have SYS_gettid, use "random" value */
428 len = strlen(filename);
429 memcpy(tempfilename, filename, len + 1);
432 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
433 reltid = syscall(SYS_gettid);
435 gettimeofday(&tv, NULL);
436 /* Promote to time_t; types differ on some OSes (like darwin) */
437 unixtime = tv.tv_sec;
439 sprintf(tempfilename + len, ".%ld-%ld", reltid, unixtime);
442 TmpFD = open(tempfilename, O_CREAT|O_EXCL|O_RDWR, S_IRUSR|S_IWUSR);
444 if ((TmpFD < 0) || (errno != 0)) {
445 syslog(LOG_CRIT, "ERROR: cannot open %s: %s\n",
446 filename, strerror(errno));
447 free_spoolcontrol_struct(scc);
448 unlink(tempfilename);
451 fchown(TmpFD, config.c_ctdluid, 0);
452 StrBufAppendPrintf(Cfg, "lastsent|%ld\n", sc->lastsent);
454 /* Write out the listrecps while freeing from memory at the
455 * same time. Am I clever or what? :)
457 while (sc->listrecps != NULL) {
458 StrBufAppendPrintf(Cfg, "listrecp|%s\n", sc->listrecps->name);
459 nptr = sc->listrecps->next;
461 sc->listrecps = nptr;
463 /* Do the same for digestrecps */
464 while (sc->digestrecps != NULL) {
465 StrBufAppendPrintf(Cfg, "digestrecp|%s\n", sc->digestrecps->name);
466 nptr = sc->digestrecps->next;
467 free(sc->digestrecps);
468 sc->digestrecps = nptr;
470 /* Do the same for participates */
471 while (sc->participates != NULL) {
472 StrBufAppendPrintf(Cfg, "participate|%s\n", sc->participates->name);
473 nptr = sc->participates->next;
474 free(sc->participates);
475 sc->participates = nptr;
477 while (sc->ignet_push_shares != NULL) {
478 StrBufAppendPrintf(Cfg, "ignet_push_share|%s", sc->ignet_push_shares->remote_nodename);
479 if (!IsEmptyStr(sc->ignet_push_shares->remote_roomname)) {
480 StrBufAppendPrintf(Cfg, "|%s", sc->ignet_push_shares->remote_roomname);
482 StrBufAppendPrintf(Cfg, "\n");
483 mptr = sc->ignet_push_shares->next;
484 free(sc->ignet_push_shares);
485 sc->ignet_push_shares = mptr;
487 if (sc->misc != NULL) {
488 StrBufAppendBufPlain(Cfg, sc->misc, -1, 0);
492 rc = write(TmpFD, ChrPtr(Cfg), StrLength(Cfg));
493 if ((rc >=0 ) && (rc == StrLength(Cfg)))
496 rename(tempfilename, filename);
500 "unable to write %s; [%s]; not enough space on the disk?\n",
504 unlink(tempfilename);
513 int is_recipient(SpoolControl *sc, const char *Name)
515 const RoomNetCfg RecipientCfgs[] = {
527 while (RecipientCfgs[i] != maxRoomNetCfg)
529 nptr = sc->NetConfigs[RecipientCfgs[i]];
533 if ((StrLength(nptr->Value) == len) &&
534 (!strcmp(Name, ChrPtr(nptr->Value))))
546 * Batch up and send all outbound traffic from the current room
548 void network_spoolout_room(RoomProcList *room_to_spool,
549 HashList *working_ignetcfg,
550 HashList *the_netmap)
553 char filename[PATH_MAX];
558 * If the room doesn't exist, don't try to perform its networking tasks.
559 * Normally this should never happen, but once in a while maybe a room gets
560 * queued for networking and then deleted before it can happen.
562 if (CtdlGetRoom(&CC->room, room_to_spool->name) != 0) {
563 syslog(LOG_CRIT, "ERROR: cannot load <%s>\n", room_to_spool->name);
567 assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
568 begin_critical_section(S_NETCONFIGS);
570 /* Only do net processing for rooms that have netconfigs */
571 if (!read_spoolcontrol_file(&sc, filename))
573 end_critical_section(S_NETCONFIGS);
576 syslog(LOG_INFO, "Networking started for <%s>\n", CC->room.QRname);
578 sc->working_ignetcfg = working_ignetcfg;
579 sc->the_netmap = the_netmap;
581 /* If there are digest recipients, we have to build a digest */
582 if (sc->NetConfigs[digestrecp] != NULL) {
583 sc->digestfp = tmpfile();
584 fprintf(sc->digestfp, "Content-type: text/plain\n\n");
587 /* Do something useful */
588 CtdlForEachMessage(MSGS_GT, sc->lastsent, NULL, NULL, NULL,
589 network_spool_msg, sc);
591 /* If we wrote a digest, deliver it and then close it */
592 snprintf(buf, sizeof buf, "room_%s@%s",
593 CC->room.QRname, config.c_fqdn);
594 for (i=0; buf[i]; ++i) {
595 buf[i] = tolower(buf[i]);
596 if (isspace(buf[i])) buf[i] = '_';
598 if (sc->digestfp != NULL) {
599 fprintf(sc->digestfp, " -----------------------------------"
600 "------------------------------------"
602 "You are subscribed to the '%s' "
604 "To post to the list: %s\n",
607 network_deliver_digest(sc); /* deliver and close */
610 /* Now rewrite the config file */
611 //// todo writenfree_spoolcontrol_file(&sc, filename);
612 end_critical_section(S_NETCONFIGS);
616 * Process a buffer containing a single message from a single file
617 * from the inbound queue
619 void network_process_buffer(char *buffer, long size, HashList *working_ignetcfg, HashList *the_netmap, int *netmap_changed)
621 struct CitContext *CCC = CC;
623 struct CtdlMessage *msg = NULL;
626 struct recptypes *recp = NULL;
627 char target_room[ROOMNAMELEN];
628 struct ser_ret sermsg;
629 char *oldpath = NULL;
630 char filename[PATH_MAX];
632 const StrBuf *nexthop = NULL;
633 unsigned char firstbyte;
634 unsigned char lastbyte;
636 QN_syslog(LOG_DEBUG, "network_process_buffer() processing %ld bytes\n", size);
638 /* Validate just a little bit. First byte should be FF and * last byte should be 00. */
639 firstbyte = buffer[0];
640 lastbyte = buffer[size-1];
641 if ( (firstbyte != 255) || (lastbyte != 0) ) {
642 QN_syslog(LOG_ERR, "Corrupt message ignored. Length=%ld, firstbyte = %d, lastbyte = %d\n",
643 size, firstbyte, lastbyte);
647 /* Set default target room to trash */
648 strcpy(target_room, TWITROOM);
650 /* Load the message into memory */
651 msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
652 memset(msg, 0, sizeof(struct CtdlMessage));
653 msg->cm_magic = CTDLMESSAGE_MAGIC;
654 msg->cm_anon_type = buffer[1];
655 msg->cm_format_type = buffer[2];
657 for (pos = 3; pos < size; ++pos) {
659 msg->cm_fields[field] = strdup(&buffer[pos+1]);
660 pos = pos + strlen(&buffer[(int)pos]);
663 /* Check for message routing */
664 if (msg->cm_fields['D'] != NULL) {
665 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
667 /* route the message */
668 Buf = NewStrBufPlain(msg->cm_fields['D'], -1);
669 if (is_valid_node(&nexthop,
675 /* prepend our node to the path */
676 if (msg->cm_fields['P'] != NULL) {
677 oldpath = msg->cm_fields['P'];
678 msg->cm_fields['P'] = NULL;
681 oldpath = strdup("unknown_user");
683 size = strlen(oldpath) + SIZ;
684 msg->cm_fields['P'] = malloc(size);
685 snprintf(msg->cm_fields['P'], size, "%s!%s",
686 config.c_nodename, oldpath);
689 /* serialize the message */
690 serialize_message(&sermsg, msg);
693 if (StrLength(nexthop) == 0) {
704 QN_syslog(LOG_DEBUG, "Appending to %s\n", filename);
705 fp = fopen(filename, "ab");
707 fwrite(sermsg.ser, sermsg.len, 1, fp);
711 QN_syslog(LOG_ERR, "%s: %s\n", filename, strerror(errno));
714 CtdlFreeMessage(msg);
719 else { /* invalid destination node name */
723 "A message you sent could not be delivered due to an invalid destination node"
724 " name. Please check the address and try sending the message again.\n");
733 * Check to see if we already have a copy of this message, and
734 * abort its processing if so. (We used to post a warning to Aide>
735 * every time this happened, but the network is now so densely
736 * connected that it's inevitable.)
738 if (network_usetable(msg) != 0) {
739 CtdlFreeMessage(msg);
743 /* Learn network topology from the path */
744 if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
745 network_learn_topology(msg->cm_fields['N'],
751 /* Is the sending node giving us a very persuasive suggestion about
752 * which room this message should be saved in? If so, go with that.
754 if (msg->cm_fields['C'] != NULL) {
755 safestrncpy(target_room, msg->cm_fields['C'], sizeof target_room);
758 /* Otherwise, does it have a recipient? If so, validate it... */
759 else if (msg->cm_fields['R'] != NULL) {
760 recp = validate_recipients(msg->cm_fields['R'], NULL, 0);
761 if (recp != NULL) if (recp->num_error != 0) {
763 "A message you sent could not be delivered due to an invalid address.\n"
764 "Please check the address and try sending the message again.\n");
766 free_recipients(recp);
767 QNM_syslog(LOG_DEBUG, "Bouncing message due to invalid recipient address.\n");
770 strcpy(target_room, ""); /* no target room if mail */
773 /* Our last shot at finding a home for this message is to see if
774 * it has the O field (Originating room) set.
776 else if (msg->cm_fields['O'] != NULL) {
777 safestrncpy(target_room, msg->cm_fields['O'], sizeof target_room);
780 /* Strip out fields that are only relevant during transit */
781 if (msg->cm_fields['D'] != NULL) {
782 free(msg->cm_fields['D']);
783 msg->cm_fields['D'] = NULL;
785 if (msg->cm_fields['C'] != NULL) {
786 free(msg->cm_fields['C']);
787 msg->cm_fields['C'] = NULL;
790 /* save the message into a room */
791 if (PerformNetprocHooks(msg, target_room) == 0) {
792 msg->cm_flags = CM_SKIP_HOOKS;
793 CtdlSubmitMsg(msg, recp, target_room, 0);
795 CtdlFreeMessage(msg);
796 free_recipients(recp);
801 * Process a single message from a single file from the inbound queue
803 void network_process_message(FILE *fp,
806 HashList *working_ignetcfg,
807 HashList *the_netmap,
814 hold_pos = ftell(fp);
815 size = msgend - msgstart + 1;
816 buffer = malloc(size);
817 if (buffer != NULL) {
818 fseek(fp, msgstart, SEEK_SET);
819 if (fread(buffer, size, 1, fp) > 0) {
820 network_process_buffer(buffer,
829 fseek(fp, hold_pos, SEEK_SET);
834 * Process a single file from the inbound queue
836 void network_process_file(char *filename,
837 HashList *working_ignetcfg,
838 HashList *the_netmap,
841 struct CitContext *CCC = CC;
843 long msgstart = (-1L);
849 fp = fopen(filename, "rb");
851 QN_syslog(LOG_CRIT, "Error opening %s: %s\n", filename, strerror(errno));
855 fseek(fp, 0L, SEEK_END);
856 QN_syslog(LOG_INFO, "network: processing %ld bytes from %s\n", ftell(fp), filename);
859 /* Look for messages in the data stream and break them out */
860 while (ch = getc(fp), ch >= 0) {
863 if (msgstart >= 0L) {
865 network_process_message(fp,
879 if (msgstart >= 0L) {
880 network_process_message(fp,
894 * Process anything in the inbound queue
896 void network_do_spoolin(HashList *working_ignetcfg, HashList *the_netmap, int *netmap_changed)
898 struct CitContext *CCC = CC;
902 char filename[PATH_MAX];
903 static time_t last_spoolin_mtime = 0L;
906 * Check the spoolin directory's modification time. If it hasn't
907 * been touched, we don't need to scan it.
909 if (stat(ctdl_netin_dir, &statbuf)) return;
910 if (statbuf.st_mtime == last_spoolin_mtime) {
911 QNM_syslog(LOG_DEBUG, "network: nothing in inbound queue\n");
914 last_spoolin_mtime = statbuf.st_mtime;
915 QNM_syslog(LOG_DEBUG, "network: processing inbound queue\n");
918 * Ok, there's something interesting in there, so scan it.
920 dp = opendir(ctdl_netin_dir);
921 if (dp == NULL) return;
923 while (d = readdir(dp), d != NULL) {
924 if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
931 network_process_file(filename,
942 * Step 1: consolidate files in the outbound queue into one file per neighbor node
943 * Step 2: delete any files in the outbound queue that were for neighbors who no longer exist.
945 void network_consolidate_spoolout(HashList *working_ignetcfg, HashList *the_netmap)
947 struct CitContext *CCC = CC;
953 struct dirent *filedir_entry;
955 char spooloutfilename[PATH_MAX];
956 char filename[PATH_MAX];
957 const StrBuf *nexthop;
963 /* Step 1: consolidate files in the outbound queue into one file per neighbor node */
964 d = (struct dirent *)malloc(offsetof(struct dirent, d_name) + PATH_MAX + 1);
965 if (d == NULL) return;
967 dp = opendir(ctdl_netout_dir);
973 NextHop = NewStrBuf();
974 memset(&IOB, 0, sizeof(IOBuffer));
975 memset(&FDIO, 0, sizeof(FDIOBuffer));
978 while ((readdir_r(dp, d, &filedir_entry) == 0) &&
979 (filedir_entry != NULL))
981 #ifdef _DIRENT_HAVE_D_NAMELEN
982 d_namelen = filedir_entry->d_namelen;
991 #define IFTODT(mode) (((mode) & 0170000) >> 12)
992 #define DTTOIF(dirtype) ((dirtype) << 12)
994 d_namelen = strlen(filedir_entry->d_name);
996 if ((d_namelen > 1) && filedir_entry->d_name[d_namelen - 1] == '~')
997 continue; /* Ignore backup files... */
999 if ((d_namelen == 1) &&
1000 (filedir_entry->d_name[0] == '.'))
1003 if ((d_namelen == 2) &&
1004 (filedir_entry->d_name[0] == '.') &&
1005 (filedir_entry->d_name[1] == '.'))
1008 pch = strchr(filedir_entry->d_name, '@');
1016 filedir_entry->d_name);
1018 StrBufPlain(NextHop,
1019 filedir_entry->d_name,
1020 pch - filedir_entry->d_name);
1022 snprintf(spooloutfilename,
1023 sizeof spooloutfilename,
1028 QN_syslog(LOG_DEBUG, "Consolidate %s to %s\n", filename, ChrPtr(NextHop));
1029 if (network_talking_to(SKEY(NextHop), NTT_CHECK)) {
1031 QN_syslog(LOG_DEBUG,
1032 "Currently online with %s - skipping for now\n",
1040 const char *err = NULL;
1041 network_talking_to(SKEY(NextHop), NTT_ADD);
1043 infd = open(filename, O_RDONLY);
1047 "failed to open %s for reading due to %s; skipping.\n",
1048 filename, strerror(errno)
1050 network_talking_to(SKEY(NextHop), NTT_REMOVE);
1054 outfd = open(spooloutfilename,
1055 O_EXCL|O_CREAT|O_NONBLOCK|O_WRONLY,
1059 outfd = open(spooloutfilename,
1060 O_EXCL|O_NONBLOCK|O_WRONLY,
1066 "failed to open %s for reading due to %s; skipping.\n",
1067 spooloutfilename, strerror(errno)
1070 network_talking_to(SKEY(NextHop), NTT_REMOVE);
1074 dsize = lseek(outfd, 0, SEEK_END);
1075 lseek(outfd, -dsize, SEEK_SET);
1077 fstat(infd, &statbuf);
1078 fsize = statbuf.st_size;
1080 fsize = lseek(infd, 0, SEEK_END);
1083 FDIOBufferInit(&FDIO, &IOB, outfd, fsize + dsize);
1084 FDIO.ChunkSendRemain = fsize;
1085 FDIO.TotalSentAlready = dsize;
1088 do {} while ((FileMoveChunked(&FDIO, &err) > 0) && (err == NULL));
1095 "failed to append to %s [%s]; rolling back..\n",
1096 spooloutfilename, strerror(errno)
1098 /* whoops partial append?? truncate spooloutfilename again! */
1099 ftruncate(outfd, dsize);
1101 FDIOBufferDelete(&FDIO);
1104 network_talking_to(SKEY(NextHop), NTT_REMOVE);
1110 FreeStrBuf(&NextHop);
1112 "skipping Spoolcleanup because of %d files unprocessed.\n",
1119 /* Step 2: delete any files in the outbound queue that were for neighbors who no longer exist */
1120 dp = opendir(ctdl_netout_dir);
1122 FreeStrBuf(&NextHop);
1127 while ((readdir_r(dp, d, &filedir_entry) == 0) &&
1128 (filedir_entry != NULL))
1130 #ifdef _DIRENT_HAVE_D_NAMELEN
1131 d_namelen = filedir_entry->d_namelen;
1132 d_type = filedir_entry->d_type;
1136 #define DT_UNKNOWN 0
1141 #define IFTODT(mode) (((mode) & 0170000) >> 12)
1142 #define DTTOIF(dirtype) ((dirtype) << 12)
1144 d_namelen = strlen(filedir_entry->d_name);
1146 if ((d_namelen == 1) &&
1147 (filedir_entry->d_name[0] == '.'))
1150 if ((d_namelen == 2) &&
1151 (filedir_entry->d_name[0] == '.') &&
1152 (filedir_entry->d_name[1] == '.'))
1155 pch = strchr(filedir_entry->d_name, '@');
1156 if (pch == NULL) /* no @ in name? consolidated file. */
1159 StrBufPlain(NextHop,
1160 filedir_entry->d_name,
1161 pch - filedir_entry->d_name);
1167 filedir_entry->d_name
1170 i = is_valid_node(&nexthop,
1176 if ( (i != 0) || (StrLength(nexthop) > 0) ) {
1180 FreeStrBuf(&NextHop);
1189 * It's ok if these directories already exist. Just fail silently.
1191 void create_spool_dirs(void) {
1192 if ((mkdir(ctdl_spool_dir, 0700) != 0) && (errno != EEXIST))
1193 syslog(LOG_EMERG, "unable to create directory [%s]: %s", ctdl_spool_dir, strerror(errno));
1194 if (chown(ctdl_spool_dir, CTDLUID, (-1)) != 0)
1195 syslog(LOG_EMERG, "unable to set the access rights for [%s]: %s", ctdl_spool_dir, strerror(errno));
1196 if ((mkdir(ctdl_netin_dir, 0700) != 0) && (errno != EEXIST))
1197 syslog(LOG_EMERG, "unable to create directory [%s]: %s", ctdl_netin_dir, strerror(errno));
1198 if (chown(ctdl_netin_dir, CTDLUID, (-1)) != 0)
1199 syslog(LOG_EMERG, "unable to set the access rights for [%s]: %s", ctdl_netin_dir, strerror(errno));
1200 if ((mkdir(ctdl_nettmp_dir, 0700) != 0) && (errno != EEXIST))
1201 syslog(LOG_EMERG, "unable to create directory [%s]: %s", ctdl_nettmp_dir, strerror(errno));
1202 if (chown(ctdl_nettmp_dir, CTDLUID, (-1)) != 0)
1203 syslog(LOG_EMERG, "unable to set the access rights for [%s]: %s", ctdl_nettmp_dir, strerror(errno));
1204 if ((mkdir(ctdl_netout_dir, 0700) != 0) && (errno != EEXIST))
1205 syslog(LOG_EMERG, "unable to create directory [%s]: %s", ctdl_netout_dir, strerror(errno));
1206 if (chown(ctdl_netout_dir, CTDLUID, (-1)) != 0)
1207 syslog(LOG_EMERG, "unable to set the access rights for [%s]: %s", ctdl_netout_dir, strerror(errno));
1211 * Module entry point
1213 CTDL_MODULE_INIT(network_spool)
1217 // REGISTERRoomCfgType(subpending, ParseSubPendingLine, 0, SerializeSubPendingLine, DeleteSubPendingLine); /// todo: move this to mailinglist manager
1218 // REGISTERRoomCfgType(unsubpending, ParseUnSubPendingLine0, SerializeUnSubPendingLine, DeleteUnSubPendingLine); /// todo: move this to mailinglist manager
1219 // REGISTERRoomCfgType(lastsent, ParseLastSent, 1, SerializeLastSent, DeleteLastSent);
1220 /// REGISTERRoomCfgType(ignet_push_share, ParseIgnetPushShare, 0, SerializeIgnetPushShare, DeleteIgnetPushShare); // todo: move this to the ignet client
1221 REGISTERRoomCfgType(listrecp, ParseDefault, 0, SerializeGeneric, DeleteGenericCfgLine);
1222 REGISTERRoomCfgType(digestrecp, ParseDefault, 0, SerializeGeneric, DeleteGenericCfgLine);
1223 REGISTERRoomCfgType(pop3client, ParseDefault, 0, SerializeGeneric, DeleteGenericCfgLine);/// todo: implement pop3 specific parser
1224 REGISTERRoomCfgType(rssclient, ParseDefault, 0, SerializeGeneric, DeleteGenericCfgLine); /// todo: implement rss specific parser
1225 REGISTERRoomCfgType(participate, ParseDefault, 0, SerializeGeneric, DeleteGenericCfgLine);
1226 REGISTERRoomCfgType(roommailalias, ParseRoomAlias, 0, SerializeGeneric, DeleteGenericCfgLine);
1228 create_spool_dirs();
1229 //////todo CtdlRegisterCleanupHook(destroy_network_queue_room);
1231 return "network_spool";