2016-02-10 05:30:59 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// ENiGMA½
|
2016-02-17 05:11:55 +00:00
|
|
|
let MessageScanTossModule = require('../msg_scan_toss_module.js').MessageScanTossModule;
|
|
|
|
let Config = require('../config.js').config;
|
2016-02-21 00:57:38 +00:00
|
|
|
let ftnMailPacket = require('../ftn_mail_packet.js');
|
2016-02-17 05:11:55 +00:00
|
|
|
let ftnUtil = require('../ftn_util.js');
|
|
|
|
let Address = require('../ftn_address.js');
|
|
|
|
let Log = require('../logger.js').log;
|
2016-02-24 04:56:22 +00:00
|
|
|
let ArchiveUtil = require('../archive_util.js');
|
2016-02-29 05:04:03 +00:00
|
|
|
let msgDb = require('../database.js').dbs.message;
|
|
|
|
let Message = require('../message.js');
|
2016-02-16 00:56:05 +00:00
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
let moment = require('moment');
|
|
|
|
let _ = require('lodash');
|
2016-02-21 00:57:38 +00:00
|
|
|
let paths = require('path');
|
|
|
|
let mkdirp = require('mkdirp');
|
2016-02-24 04:56:22 +00:00
|
|
|
let async = require('async');
|
|
|
|
let fs = require('fs');
|
2016-02-29 05:04:03 +00:00
|
|
|
let later = require('later');
|
2016-03-04 05:54:32 +00:00
|
|
|
let temp = require('temp').track(); // track() cleans up temp dir/files for us
|
2016-03-09 05:30:04 +00:00
|
|
|
let assert = require('assert');
|
2016-02-10 05:30:59 +00:00
|
|
|
|
|
|
|
exports.moduleInfo = {
|
2016-02-29 05:04:03 +00:00
|
|
|
name : 'FTN BSO',
|
|
|
|
desc : 'BSO style message scanner/tosser for FTN networks',
|
2016-02-10 05:30:59 +00:00
|
|
|
author : 'NuSkooler',
|
|
|
|
};
|
|
|
|
|
2016-02-21 00:57:38 +00:00
|
|
|
/*
|
|
|
|
:TODO:
|
|
|
|
* Add bundle timer (arcmail)
|
|
|
|
* Queue until time elapses / fixed time interval
|
|
|
|
* Pakcets append until >= max byte size
|
|
|
|
* [if arch type is not empty): Packets -> bundle until max byte size -> repeat process
|
|
|
|
* NetMail needs explicit isNetMail() check
|
|
|
|
* NetMail filename / location / etc. is still unknown - need to post on groups & get real answers
|
|
|
|
*/
|
|
|
|
|
2016-02-10 05:30:59 +00:00
|
|
|
exports.getModule = FTNMessageScanTossModule;
|
|
|
|
|
2016-02-29 05:04:03 +00:00
|
|
|
const SCHEDULE_REGEXP = /(?:^|or )?(@watch\:|@immediate)([^\0]+)?$/;
|
|
|
|
|
2016-02-10 05:30:59 +00:00
|
|
|
function FTNMessageScanTossModule() {
|
|
|
|
MessageScanTossModule.call(this);
|
2016-02-29 05:04:03 +00:00
|
|
|
|
|
|
|
let self = this;
|
2016-02-10 05:30:59 +00:00
|
|
|
|
2016-02-24 04:56:22 +00:00
|
|
|
this.archUtil = new ArchiveUtil();
|
|
|
|
this.archUtil.init();
|
2016-02-29 05:04:03 +00:00
|
|
|
|
2016-02-24 04:56:22 +00:00
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
if(_.has(Config, 'scannerTossers.ftn_bso')) {
|
2016-02-21 00:57:38 +00:00
|
|
|
this.moduleConfig = Config.scannerTossers.ftn_bso;
|
2016-02-29 05:04:03 +00:00
|
|
|
|
|
|
|
|
2016-02-21 00:57:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-01 05:32:51 +00:00
|
|
|
this.getDefaultNetworkName = function() {
|
|
|
|
if(this.moduleConfig.defaultNetwork) {
|
|
|
|
return this.moduleConfig.defaultNetwork;
|
|
|
|
}
|
|
|
|
|
|
|
|
const networkNames = Object.keys(Config.messageNetworks.ftn.networks);
|
|
|
|
if(1 === networkNames.length) {
|
|
|
|
return networkNames[0];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-21 00:57:38 +00:00
|
|
|
this.isDefaultDomainZone = function(networkName, address) {
|
2016-03-01 05:32:51 +00:00
|
|
|
const defaultNetworkName = this.getDefaultNetworkName();
|
|
|
|
return(networkName === defaultNetworkName && address.zone === this.moduleConfig.defaultZone);
|
2016-03-04 05:54:32 +00:00
|
|
|
};
|
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
this.getNetworkNameByAddress = function(remoteAddress) {
|
2016-03-04 05:54:32 +00:00
|
|
|
return _.findKey(Config.messageNetworks.ftn.networks, network => {
|
2016-03-09 05:30:04 +00:00
|
|
|
const localAddress = Address.fromString(network.localAddress);
|
|
|
|
return !_.isUndefined(localAddress) && localAddress.isEqual(remoteAddress);
|
2016-03-04 05:54:32 +00:00
|
|
|
});
|
|
|
|
};
|
2016-02-21 00:57:38 +00:00
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
this.getNetworkNameByAddressPattern = function(remoteAddressPattern) {
|
|
|
|
return _.findKey(Config.messageNetworks.ftn.networks, network => {
|
|
|
|
const localAddress = Address.fromString(network.localAddress);
|
|
|
|
return !_.isUndefined(localAddress) && localAddress.isPatternMatch(remoteAddressPattern);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getLocalAreaTagByFtnAreaTag = function(ftnAreaTag) {
|
|
|
|
return _.findKey(Config.messageNetworks.ftn.areas, areaConf => {
|
|
|
|
return areaConf.tag === ftnAreaTag;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
this.getSeenByAddresses = function(messageSeenBy) {
|
|
|
|
if(!_.isArray(messageSeenBy)) {
|
|
|
|
messageSeenBy = [ messageSeenBy ];
|
|
|
|
}
|
|
|
|
|
|
|
|
let seenByAddrs = [];
|
|
|
|
messageSeenBy.forEach(sb => {
|
|
|
|
seenByAddrs = seenByAddrs.concat(ftnUtil.parseAbbreviatedNetNodeList(sb));
|
|
|
|
});
|
|
|
|
return seenByAddrs;
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
|
|
|
this.messageHasValidMSGID = function(msg) {
|
|
|
|
return _.isString(msg.meta.FtnKludge.MSGID) && msg.meta.FtnKludge.MSGID.length > 0;
|
|
|
|
};
|
|
|
|
|
2016-02-24 04:56:22 +00:00
|
|
|
this.getOutgoingPacketDir = function(networkName, destAddress) {
|
2016-02-21 00:57:38 +00:00
|
|
|
let dir = this.moduleConfig.paths.outbound;
|
2016-02-24 04:56:22 +00:00
|
|
|
if(!this.isDefaultDomainZone(networkName, destAddress)) {
|
|
|
|
const hexZone = `000${destAddress.zone.toString(16)}`.substr(-3);
|
2016-02-21 00:57:38 +00:00
|
|
|
dir = paths.join(dir, `${networkName.toLowerCase()}.${hexZone}`);
|
|
|
|
}
|
|
|
|
return dir;
|
|
|
|
};
|
|
|
|
|
2016-02-29 05:04:03 +00:00
|
|
|
this.getOutgoingPacketFileName = function(basePath, messageId, isTemp) {
|
2016-02-21 00:57:38 +00:00
|
|
|
//
|
|
|
|
// Generating an outgoing packet file name comes with a few issues:
|
|
|
|
// * We must use DOS 8.3 filenames due to legacy systems that receive
|
|
|
|
// the packet not understanding LFNs
|
|
|
|
// * We need uniqueness; This is especially important with packets that
|
|
|
|
// end up in bundles and on the receiving/remote system where conflicts
|
|
|
|
// with other systems could also occur
|
|
|
|
//
|
|
|
|
// There are a lot of systems in use here for the name:
|
|
|
|
// * HEX CRC16/32 of data
|
|
|
|
// * HEX UNIX timestamp
|
|
|
|
// * Mystic at least at one point, used Hex8(day of month + seconds past midnight + hundredths of second)
|
|
|
|
// See https://groups.google.com/forum/#!searchin/alt.bbs.mystic/netmail$20filename/alt.bbs.mystic/m1xLnY8i1pU/YnG2excdl6MJ
|
|
|
|
// * SBBSEcho uses DDHHMMSS - see https://github.com/ftnapps/pkg-sbbs/blob/master/docs/fidonet.txt
|
|
|
|
// * We already have a system for 8-character serial number gernation that is
|
|
|
|
// used for e.g. in FTS-0009.001 MSGIDs... let's use that!
|
|
|
|
//
|
2016-02-29 05:04:03 +00:00
|
|
|
const name = ftnUtil.getMessageSerialNumber(messageId);
|
2016-02-21 00:57:38 +00:00
|
|
|
const ext = (true === isTemp) ? 'pk_' : 'pkt';
|
|
|
|
return paths.join(basePath, `${name}.${ext}`);
|
|
|
|
};
|
2016-02-10 05:30:59 +00:00
|
|
|
|
2016-02-24 04:56:22 +00:00
|
|
|
this.getOutgoingFlowFileName = function(basePath, destAddress, exportType, extSuffix) {
|
|
|
|
if(destAddress.point) {
|
|
|
|
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// Use |destAddress| nnnnNNNN.??? where nnnn is dest net and NNNN is dest
|
|
|
|
// node. This seems to match what Mystic does
|
|
|
|
//
|
|
|
|
return `${Math.abs(destAddress.net)}${Math.abs(destAddress.node)}.${exportType[1]}${extSuffix}`;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getOutgoingBundleFileName = function(basePath, sourceAddress, destAddress, cb) {
|
|
|
|
//
|
|
|
|
// Base filename is constructed as such:
|
|
|
|
// * If this |destAddress| is *not* a point address, we use NNNNnnnn where
|
|
|
|
// NNNN is 0 padded hex of dest net - source net and and nnnn is 0 padded
|
|
|
|
// hex of dest node - source node.
|
|
|
|
// * If |destAddress| is a point, NNNN becomes 0000 and nnnn becomes 'p' +
|
|
|
|
// 3 digit 0 padded hex point
|
|
|
|
//
|
|
|
|
// Extension is dd? where dd is Su...Mo and ? is 0...Z as collisions arise
|
|
|
|
//
|
|
|
|
var basename;
|
|
|
|
if(destAddress.point) {
|
|
|
|
const pointHex = `000${destAddress.point}`.substr(-3);
|
|
|
|
basename = `0000p${pointHex}`;
|
|
|
|
} else {
|
|
|
|
basename =
|
|
|
|
`0000${Math.abs(sourceAddress.net - destAddress.net).toString(16)}`.substr(-4) +
|
|
|
|
`0000${Math.abs(sourceAddress.node - destAddress.node).toString(16)}`.substr(-4);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// We need to now find the first entry that does not exist starting
|
|
|
|
// with dd0 to ddz
|
|
|
|
//
|
|
|
|
const EXT_SUFFIXES = '0123456789abcdefghijklmnopqrstuvwxyz'.split('');
|
|
|
|
let fileName = `${basename}.${moment().format('dd').toLowerCase()}`;
|
|
|
|
async.detectSeries(EXT_SUFFIXES, (suffix, callback) => {
|
|
|
|
const checkFileName = fileName + suffix;
|
2016-03-10 05:32:00 +00:00
|
|
|
fs.stat(paths.join(basePath, checkFileName), err => {
|
2016-02-24 04:56:22 +00:00
|
|
|
callback((err && 'ENOENT' === err.code) ? true : false);
|
|
|
|
});
|
|
|
|
}, finalSuffix => {
|
|
|
|
if(finalSuffix) {
|
|
|
|
cb(null, paths.join(basePath, fileName + finalSuffix));
|
|
|
|
} else {
|
|
|
|
cb(new Error('Could not acquire a bundle filename!'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2016-02-29 05:04:03 +00:00
|
|
|
|
2016-02-21 00:57:38 +00:00
|
|
|
this.prepareMessage = function(message, options) {
|
2016-02-16 00:56:05 +00:00
|
|
|
//
|
|
|
|
// Set various FTN kludges/etc.
|
|
|
|
//
|
|
|
|
message.meta.FtnProperty = message.meta.FtnProperty || {};
|
2016-02-21 00:57:38 +00:00
|
|
|
message.meta.FtnKludge = message.meta.FtnKludge || {};
|
|
|
|
|
|
|
|
message.meta.FtnProperty.ftn_orig_node = options.network.localAddress.node;
|
2016-02-24 04:56:22 +00:00
|
|
|
message.meta.FtnProperty.ftn_dest_node = options.destAddress.node;
|
2016-02-21 00:57:38 +00:00
|
|
|
message.meta.FtnProperty.ftn_orig_network = options.network.localAddress.net;
|
2016-02-24 04:56:22 +00:00
|
|
|
message.meta.FtnProperty.ftn_dest_network = options.destAddress.net;
|
2016-02-16 00:56:05 +00:00
|
|
|
// :TODO: attr1 & 2
|
|
|
|
message.meta.FtnProperty.ftn_cost = 0;
|
2016-02-21 00:57:38 +00:00
|
|
|
|
|
|
|
message.meta.FtnProperty.ftn_tear_line = ftnUtil.getTearLine();
|
2016-02-16 00:56:05 +00:00
|
|
|
|
2016-02-21 00:57:38 +00:00
|
|
|
// :TODO: Need an explicit isNetMail() check
|
2016-03-10 05:32:00 +00:00
|
|
|
let ftnAttribute =
|
|
|
|
ftnMailPacket.Packet.Attribute.Local; // message from our system
|
2016-02-24 04:56:22 +00:00
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
if(message.isPrivate()) {
|
2016-02-24 04:56:22 +00:00
|
|
|
ftnAttribute |= ftnMailPacket.Packet.Attribute.Private;
|
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
//
|
|
|
|
// NetMail messages need a FRL-1005.001 "Via" line
|
|
|
|
// http://ftsc.org/docs/frl-1005.001
|
|
|
|
//
|
2016-02-21 00:57:38 +00:00
|
|
|
if(_.isString(message.meta.FtnKludge.Via)) {
|
|
|
|
message.meta.FtnKludge.Via = [ message.meta.FtnKludge.Via ];
|
2016-02-17 05:11:55 +00:00
|
|
|
}
|
2016-02-21 00:57:38 +00:00
|
|
|
message.meta.FtnKludge.Via = message.meta.FtnKludge.Via || [];
|
|
|
|
message.meta.FtnKludge.Via.push(ftnUtil.getVia(options.network.localAddress));
|
2016-02-16 00:56:05 +00:00
|
|
|
} else {
|
2016-02-21 00:57:38 +00:00
|
|
|
//
|
|
|
|
// EchoMail requires some additional properties & kludges
|
2016-03-10 05:32:00 +00:00
|
|
|
//
|
2016-02-21 00:57:38 +00:00
|
|
|
message.meta.FtnProperty.ftn_origin = ftnUtil.getOrigin(options.network.localAddress);
|
|
|
|
message.meta.FtnProperty.ftn_area = Config.messageNetworks.ftn.areas[message.areaTag].tag;
|
|
|
|
|
|
|
|
//
|
|
|
|
// When exporting messages, we should create/update SEEN-BY
|
|
|
|
// with remote address(s) we are exporting to.
|
|
|
|
//
|
2016-03-09 05:30:04 +00:00
|
|
|
const seenByAdditions =
|
|
|
|
[ `${options.network.localAddress.net}/${options.network.localAddress.node}` ].concat(Config.messageNetworks.ftn.areas[message.areaTag].uplinks);
|
2016-02-21 00:57:38 +00:00
|
|
|
message.meta.FtnProperty.ftn_seen_by =
|
2016-02-24 06:38:05 +00:00
|
|
|
ftnUtil.getUpdatedSeenByEntries(message.meta.FtnProperty.ftn_seen_by, seenByAdditions);
|
2016-02-16 00:56:05 +00:00
|
|
|
|
2016-02-21 00:57:38 +00:00
|
|
|
//
|
|
|
|
// And create/update PATH for ourself
|
|
|
|
//
|
|
|
|
message.meta.FtnKludge.PATH =
|
|
|
|
ftnUtil.getUpdatedPathEntries(message.meta.FtnKludge.PATH, options.network.localAddress);
|
|
|
|
}
|
|
|
|
|
2016-02-24 04:56:22 +00:00
|
|
|
message.meta.FtnProperty.ftn_attr_flags = ftnAttribute;
|
|
|
|
|
2016-02-16 00:56:05 +00:00
|
|
|
//
|
2016-02-21 00:57:38 +00:00
|
|
|
// Additional kludges
|
2016-03-09 05:30:04 +00:00
|
|
|
//
|
|
|
|
// Check for existence of MSGID as we may already have stored it from a previous
|
|
|
|
// export that failed to finish
|
2016-02-21 00:57:38 +00:00
|
|
|
//
|
2016-03-09 05:30:04 +00:00
|
|
|
if(!message.meta.FtnKludge.MSGID) {
|
|
|
|
message.meta.FtnKludge.MSGID = ftnUtil.getMessageIdentifier(message, options.network.localAddress);
|
|
|
|
}
|
|
|
|
|
2016-02-21 00:57:38 +00:00
|
|
|
message.meta.FtnKludge.TZUTC = ftnUtil.getUTCTimeZoneOffset();
|
|
|
|
|
|
|
|
if(!message.meta.FtnKludge.PID) {
|
|
|
|
message.meta.FtnKludge.PID = ftnUtil.getProductIdentifier();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!message.meta.FtnKludge.TID) {
|
|
|
|
// :TODO: Create TID!!
|
|
|
|
//message.meta.FtnKludge.TID =
|
|
|
|
}
|
|
|
|
|
2016-02-16 00:56:05 +00:00
|
|
|
//
|
2016-02-21 00:57:38 +00:00
|
|
|
// Determine CHRS and actual internal encoding name
|
|
|
|
// Try to preserve anything already here
|
|
|
|
let encoding = options.nodeConfig.encoding || 'utf8';
|
|
|
|
if(message.meta.FtnKludge.CHRS) {
|
|
|
|
const encFromChars = ftnUtil.getEncodingFromCharacterSetIdentifier(message.meta.FtnKludge.CHRS);
|
|
|
|
if(encFromChars) {
|
|
|
|
encoding = encFromChars;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
options.encoding = encoding; // save for later
|
|
|
|
message.meta.FtnKludge.CHRS = ftnUtil.getCharacterSetIdentifierByEncoding(encoding);
|
|
|
|
// :TODO: FLAGS kludge?
|
|
|
|
// :TODO: Add REPLY kludge if appropriate
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// :TODO: change to something like isAreaConfigValid
|
|
|
|
// check paths, Addresses, etc.
|
2016-02-29 05:04:03 +00:00
|
|
|
this.isAreaConfigValid = function(areaConfig) {
|
2016-02-21 00:57:38 +00:00
|
|
|
if(!_.isString(areaConfig.tag) || !_.isString(areaConfig.network)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(_.isString(areaConfig.uplinks)) {
|
|
|
|
areaConfig.uplinks = areaConfig.uplinks.split(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
return (_.isArray(areaConfig.uplinks));
|
2016-02-16 00:56:05 +00:00
|
|
|
};
|
2016-02-29 05:04:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
this.hasValidConfiguration = function() {
|
|
|
|
if(!_.has(this, 'moduleConfig.nodes') || !_.has(Config, 'messageNetworks.ftn.areas')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:32:51 +00:00
|
|
|
// :TODO: need to check more!
|
|
|
|
|
2016-02-29 05:04:03 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.parseScheduleString = function(schedStr) {
|
2016-03-04 05:54:32 +00:00
|
|
|
if(!schedStr) {
|
|
|
|
return; // nothing to parse!
|
|
|
|
}
|
|
|
|
|
2016-02-29 05:04:03 +00:00
|
|
|
let schedule = {};
|
|
|
|
|
|
|
|
const m = SCHEDULE_REGEXP.exec(schedStr);
|
|
|
|
if(m) {
|
|
|
|
schedStr = schedStr.substr(0, m.index).trim();
|
|
|
|
|
|
|
|
if('@watch:' === m[1]) {
|
|
|
|
schedule.watchFile = m[2];
|
|
|
|
} else if('@immediate' === m[1]) {
|
|
|
|
schedule.immediate = true;
|
|
|
|
}
|
|
|
|
}
|
2016-02-16 00:56:05 +00:00
|
|
|
|
2016-02-29 05:04:03 +00:00
|
|
|
if(schedStr.length > 0) {
|
|
|
|
const sched = later.parse.text(schedStr);
|
|
|
|
if(-1 === sched.error) {
|
|
|
|
schedule.sched = sched;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// return undefined if we couldn't parse out anything useful
|
|
|
|
if(!_.isEmpty(schedule)) {
|
|
|
|
return schedule;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getAreaLastScanId = function(areaTag, cb) {
|
|
|
|
const sql =
|
|
|
|
`SELECT area_tag, message_id
|
|
|
|
FROM message_area_last_scan
|
|
|
|
WHERE scan_toss = "ftn_bso" AND area_tag = ?
|
|
|
|
LIMIT 1;`;
|
|
|
|
|
|
|
|
msgDb.get(sql, [ areaTag ], (err, row) => {
|
|
|
|
cb(err, row ? row.message_id : 0);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-02-29 05:35:43 +00:00
|
|
|
this.setAreaLastScanId = function(areaTag, lastScanId, cb) {
|
|
|
|
const sql =
|
|
|
|
`REPLACE INTO message_area_last_scan (scan_toss, area_tag, message_id)
|
|
|
|
VALUES ("ftn_bso", ?, ?);`;
|
|
|
|
|
|
|
|
msgDb.run(sql, [ areaTag, lastScanId ], err => {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-02-29 05:04:03 +00:00
|
|
|
this.getNodeConfigKeyForUplink = function(uplink) {
|
|
|
|
// :TODO: sort by least # of '*' & take top?
|
|
|
|
const nodeKey = _.filter(Object.keys(this.moduleConfig.nodes), addr => {
|
2016-03-09 05:30:04 +00:00
|
|
|
return Address.fromString(addr).isPatternMatch(uplink);
|
2016-02-29 05:04:03 +00:00
|
|
|
})[0];
|
|
|
|
|
|
|
|
return nodeKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.exportMessagesByUuid = function(messageUuids, exportOpts, cb) {
|
|
|
|
//
|
|
|
|
// This method has a lot of madness going on:
|
|
|
|
// - Try to stuff messages into packets until we've hit the target size
|
|
|
|
// - We need to wait for write streams to finish before proceeding in many cases
|
|
|
|
// or data will be cut off when closing and creating a new stream
|
|
|
|
//
|
|
|
|
let exportedFiles = [];
|
|
|
|
let currPacketSize = self.moduleConfig.packetTargetByteSize;
|
|
|
|
let packet;
|
|
|
|
let ws;
|
|
|
|
let remainMessageBuf;
|
|
|
|
let remainMessageId;
|
|
|
|
|
|
|
|
async.each(messageUuids, (msgUuid, nextUuid) => {
|
|
|
|
let message = new Message();
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function finalizePrevious(callback) {
|
|
|
|
if(packet && currPacketSize >= self.moduleConfig.packetTargetByteSize) {
|
|
|
|
packet.writeTerminator(ws);
|
|
|
|
ws.end();
|
|
|
|
ws.once('finish', () => {
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function loadMessage(callback) {
|
|
|
|
message.load( { uuid : msgUuid }, err => {
|
|
|
|
if(!err) {
|
|
|
|
self.prepareMessage(message, exportOpts);
|
|
|
|
}
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function createNewPacket(callback) {
|
|
|
|
if(currPacketSize >= self.moduleConfig.packetTargetByteSize) {
|
|
|
|
packet = new ftnMailPacket.Packet();
|
|
|
|
|
|
|
|
const packetHeader = new ftnMailPacket.PacketHeader(
|
|
|
|
exportOpts.network.localAddress,
|
|
|
|
exportOpts.destAddress,
|
|
|
|
exportOpts.nodeConfig.packetType);
|
|
|
|
|
|
|
|
packetHeader.password = exportOpts.nodeConfig.packetPassword || '';
|
|
|
|
|
|
|
|
// use current message ID for filename seed
|
2016-03-04 05:54:32 +00:00
|
|
|
const pktFileName = self.getOutgoingPacketFileName(exportOpts.tempDir, message.messageId);
|
2016-02-29 05:04:03 +00:00
|
|
|
exportedFiles.push(pktFileName);
|
|
|
|
|
|
|
|
ws = fs.createWriteStream(pktFileName);
|
|
|
|
|
|
|
|
currPacketSize = packet.writeHeader(ws, packetHeader);
|
|
|
|
|
|
|
|
if(remainMessageBuf) {
|
|
|
|
currPacketSize += packet.writeMessageEntry(ws, remainMessageBuf);
|
|
|
|
remainMessageBuf = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function appendMessage(callback) {
|
|
|
|
const msgBuf = packet.getMessageEntryBuffer(message, exportOpts);
|
|
|
|
currPacketSize += msgBuf.length;
|
|
|
|
|
|
|
|
if(currPacketSize >= self.moduleConfig.packetTargetByteSize) {
|
|
|
|
remainMessageBuf = msgBuf; // save for next packet
|
|
|
|
remainMessageId = message.messageId;
|
|
|
|
} else {
|
|
|
|
ws.write(msgBuf);
|
|
|
|
}
|
|
|
|
callback(null);
|
2016-03-09 05:30:04 +00:00
|
|
|
},
|
|
|
|
function updateStoredMeta(callback) {
|
|
|
|
//
|
|
|
|
// We want to store some meta as if we had imported
|
|
|
|
// this message for later reference
|
|
|
|
//
|
|
|
|
if(message.meta.FtnKludge.MSGID) {
|
|
|
|
message.persistMetaValue('FtnKludge', 'MSGID', message.meta.FtnKludge.MSGID, err => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
2016-02-29 05:04:03 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
nextUuid(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}, err => {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function terminateLast(callback) {
|
|
|
|
if(packet) {
|
|
|
|
packet.writeTerminator(ws);
|
|
|
|
ws.end();
|
|
|
|
ws.once('finish', () => {
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function writeRemainPacket(callback) {
|
|
|
|
if(remainMessageBuf) {
|
|
|
|
// :TODO: DRY this with the code above -- they are basically identical
|
|
|
|
packet = new ftnMailPacket.Packet();
|
|
|
|
|
|
|
|
const packetHeader = new ftnMailPacket.PacketHeader(
|
|
|
|
exportOpts.network.localAddress,
|
|
|
|
exportOpts.destAddress,
|
|
|
|
exportOpts.nodeConfig.packetType);
|
|
|
|
|
|
|
|
packetHeader.password = exportOpts.nodeConfig.packetPassword || '';
|
|
|
|
|
|
|
|
// use current message ID for filename seed
|
2016-03-04 05:54:32 +00:00
|
|
|
const pktFileName = self.getOutgoingPacketFileName(exportOpts.tempDir, remainMessageId);
|
2016-02-29 05:04:03 +00:00
|
|
|
exportedFiles.push(pktFileName);
|
|
|
|
|
|
|
|
ws = fs.createWriteStream(pktFileName);
|
|
|
|
|
|
|
|
packet.writeHeader(ws, packetHeader);
|
|
|
|
ws.write(remainMessageBuf);
|
|
|
|
packet.writeTerminator(ws);
|
|
|
|
ws.end();
|
|
|
|
ws.once('finish', () => {
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
cb(err, exportedFiles);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.exportMessagesToUplinks = function(messageUuids, areaConfig, cb) {
|
|
|
|
async.each(areaConfig.uplinks, (uplink, nextUplink) => {
|
|
|
|
const nodeConfigKey = self.getNodeConfigKeyForUplink(uplink);
|
|
|
|
if(!nodeConfigKey) {
|
|
|
|
return nextUplink();
|
|
|
|
}
|
|
|
|
|
|
|
|
const exportOpts = {
|
|
|
|
nodeConfig : self.moduleConfig.nodes[nodeConfigKey],
|
|
|
|
network : Config.messageNetworks.ftn.networks[areaConfig.network],
|
|
|
|
destAddress : Address.fromString(uplink),
|
|
|
|
networkName : areaConfig.network,
|
|
|
|
};
|
|
|
|
|
|
|
|
if(_.isString(exportOpts.network.localAddress)) {
|
|
|
|
exportOpts.network.localAddress = Address.fromString(exportOpts.network.localAddress);
|
|
|
|
}
|
|
|
|
|
|
|
|
const outgoingDir = self.getOutgoingPacketDir(exportOpts.networkName, exportOpts.destAddress);
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function createTempDir(callback) {
|
2016-03-04 05:54:32 +00:00
|
|
|
temp.mkdir('enigftnexport--', (err, tempDir) => {
|
|
|
|
exportOpts.tempDir = tempDir;
|
2016-02-29 05:04:03 +00:00
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function createOutgoingDir(callback) {
|
|
|
|
mkdirp(outgoingDir, err => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function exportToTempArea(callback) {
|
|
|
|
self.exportMessagesByUuid(messageUuids, exportOpts, callback);
|
|
|
|
},
|
|
|
|
function createArcMailBundle(exportedFileNames, callback) {
|
|
|
|
if(self.archUtil.haveArchiver(exportOpts.nodeConfig.archiveType)) {
|
|
|
|
// :TODO: support bundleTargetByteSize:
|
|
|
|
//
|
|
|
|
// Compress to a temp location then we'll move it in the next step
|
|
|
|
//
|
|
|
|
// Note that we must use the *final* output dir for getOutgoingBundleFileName()
|
|
|
|
// as it checks for collisions in bundle names!
|
|
|
|
//
|
|
|
|
self.getOutgoingBundleFileName(outgoingDir, exportOpts.network.localAddress, exportOpts.destAddress, (err, bundlePath) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
// adjust back to temp path
|
2016-03-04 05:54:32 +00:00
|
|
|
const tempBundlePath = paths.join(exportOpts.tempDir, paths.basename(bundlePath));
|
2016-02-29 05:04:03 +00:00
|
|
|
|
|
|
|
self.archUtil.compressTo(
|
|
|
|
exportOpts.nodeConfig.archiveType,
|
|
|
|
tempBundlePath,
|
|
|
|
exportedFileNames, err => {
|
2016-03-10 05:32:00 +00:00
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-02-29 05:04:03 +00:00
|
|
|
// :TODO: we need to delete the original input file(s)
|
|
|
|
fs.rename(tempBundlePath, bundlePath, err => {
|
|
|
|
callback(err, [ bundlePath ] );
|
|
|
|
});
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null, exportedFileNames);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function moveFilesToOutgoing(exportedFileNames, callback) {
|
|
|
|
async.each(exportedFileNames, (oldPath, nextFile) => {
|
|
|
|
const ext = paths.extname(oldPath);
|
|
|
|
if('.pk_' === ext) {
|
|
|
|
const newPath = paths.join(outgoingDir, paths.basename(oldPath, ext) + '.pkt');
|
|
|
|
fs.rename(oldPath, newPath, nextFile);
|
|
|
|
} else {
|
|
|
|
const newPath = paths.join(outgoingDir, paths.basename(oldPath));
|
|
|
|
fs.rename(oldPath, newPath, nextFile);
|
|
|
|
}
|
|
|
|
}, callback);
|
2016-03-04 05:54:32 +00:00
|
|
|
},
|
|
|
|
function cleanUpTempDir(callback) {
|
|
|
|
temp.cleanup((err, stats) => {
|
|
|
|
Log.trace(
|
|
|
|
Object.assign(stats, { tempDir : exportOpts.tempDir }),
|
|
|
|
'Temporary directory cleaned up');
|
2016-03-10 05:32:00 +00:00
|
|
|
|
|
|
|
callback(null);
|
2016-03-04 05:54:32 +00:00
|
|
|
});
|
2016-02-29 05:04:03 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
2016-03-10 05:32:00 +00:00
|
|
|
// :TODO: do something with |err| ?
|
2016-03-04 05:54:32 +00:00
|
|
|
nextUplink();
|
2016-02-29 05:04:03 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}, cb); // complete
|
|
|
|
};
|
2016-03-01 05:32:51 +00:00
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
this.setReplyToMsgIdFtnReplyKludge = function(message, cb) {
|
|
|
|
//
|
|
|
|
// Given a FTN REPLY kludge, set |message.replyToMsgId|, if possible,
|
|
|
|
// by looking up an associated MSGID kludge meta.
|
|
|
|
//
|
|
|
|
// See also: http://ftsc.org/docs/fts-0009.001
|
|
|
|
//
|
|
|
|
if(!_.isString(message.meta.FtnKludge.REPLY)) {
|
|
|
|
// nothing to do
|
|
|
|
return cb();
|
|
|
|
}
|
2016-03-04 05:54:32 +00:00
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
Message.getMessageIdsByMetaValue('FtnKludge', 'MSGID', message.meta.FtnKludge.REPLY, (err, msgIds) => {
|
|
|
|
if(!err) {
|
|
|
|
assert(1 === msgIds.length);
|
|
|
|
message.replyToMsgId = msgIds[0];
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.importNetMailToArea = function(localAreaTag, header, message, cb) {
|
|
|
|
async.series(
|
|
|
|
[
|
2016-03-10 05:32:00 +00:00
|
|
|
function validateDestinationAddress(callback) {
|
|
|
|
const localNetworkPattern = `${message.meta.FtnProperty.ftn_dest_network}/${message.meta.FtnProperty.ftn_dest_node}`;
|
2016-03-09 05:30:04 +00:00
|
|
|
const localNetworkName = self.getNetworkNameByAddressPattern(localNetworkPattern);
|
|
|
|
|
|
|
|
callback(_.isString(localNetworkName) ? null : new Error('Packet destination is not us'));
|
|
|
|
},
|
|
|
|
function basicSetup(callback) {
|
|
|
|
message.areaTag = localAreaTag;
|
|
|
|
|
|
|
|
//
|
|
|
|
// If duplicates are NOT allowed in the area (the default), we need to update
|
|
|
|
// the message UUID using data available to us. Duplicate UUIDs are internally
|
|
|
|
// not allowed in our local database.
|
|
|
|
//
|
|
|
|
if(!Config.messageNetworks.ftn.areas[localAreaTag].allowDupes) {
|
|
|
|
if(self.messageHasValidMSGID(message)) {
|
|
|
|
// Update UUID with our preferred generation method
|
|
|
|
message.uuid = ftnUtil.createMessageUuid(
|
|
|
|
message.meta.FtnKludge.MSGID,
|
|
|
|
message.meta.FtnProperty.ftn_area);
|
|
|
|
} else {
|
|
|
|
// Update UUID with alternate/backup generation method
|
|
|
|
message.uuid = ftnUtil.createMessageUuidAlternate(
|
|
|
|
message.meta.FtnProperty.ftn_area,
|
|
|
|
message.modTimestamp,
|
|
|
|
message.subject,
|
|
|
|
message.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
},
|
|
|
|
function setReplyToMessageId(callback) {
|
|
|
|
self.setReplyToMsgIdFtnReplyKludge(message, () => {
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function persistImport(callback) {
|
|
|
|
message.persist(err => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}
|
2016-03-10 05:32:00 +00:00
|
|
|
],
|
|
|
|
err => {
|
2016-03-09 05:30:04 +00:00
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Ref. implementations on import:
|
|
|
|
// * https://github.com/larsks/crashmail/blob/26e5374710c7868dab3d834be14bf4041041aae5/crashmail/pkt.c
|
|
|
|
// https://github.com/larsks/crashmail/blob/26e5374710c7868dab3d834be14bf4041041aae5/crashmail/handle.c
|
|
|
|
//
|
|
|
|
this.importMessagesFromPacketFile = function(packetPath, cb) {
|
|
|
|
let packetHeader;
|
|
|
|
|
|
|
|
new ftnMailPacket.Packet().read(packetPath, (entryType, entryData, next) => {
|
2016-03-04 05:54:32 +00:00
|
|
|
if('header' === entryType) {
|
2016-03-09 05:30:04 +00:00
|
|
|
packetHeader = entryData;
|
|
|
|
|
|
|
|
const localNetworkName = self.getNetworkNameByAddress(packetHeader.destAddress);
|
|
|
|
if(!_.isString(localNetworkName)) {
|
|
|
|
next(new Error('No configuration for this packet'));
|
|
|
|
} else {
|
|
|
|
next(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if('message' === entryType) {
|
|
|
|
const message = entryData;
|
2016-03-04 05:54:32 +00:00
|
|
|
const areaTag = message.meta.FtnProperty.ftn_area;
|
|
|
|
|
|
|
|
if(areaTag) {
|
|
|
|
//
|
2016-03-09 05:30:04 +00:00
|
|
|
// EchoMail
|
|
|
|
//
|
|
|
|
const localAreaTag = self.getLocalAreaTagByFtnAreaTag(areaTag);
|
|
|
|
if(localAreaTag) {
|
|
|
|
self.importNetMailToArea(localAreaTag, packetHeader, message, err => {
|
|
|
|
if(err) {
|
|
|
|
if('SQLITE_CONSTRAINT' === err.code) {
|
|
|
|
Log.info(
|
|
|
|
{ subject : message.subject, uuid : message.uuid },
|
|
|
|
'Not importing non-unique message');
|
|
|
|
|
|
|
|
return next(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// No local area configured for this import
|
|
|
|
//
|
|
|
|
// :TODO: Handle the "catch all" case, if configured
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// NetMail
|
2016-03-04 05:54:32 +00:00
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, err => {
|
|
|
|
cb(err);
|
2016-03-09 05:30:04 +00:00
|
|
|
});
|
|
|
|
};
|
2016-03-02 05:42:29 +00:00
|
|
|
|
2016-03-04 05:54:32 +00:00
|
|
|
this.importPacketFilesFromDirectory = function(importDir, cb) {
|
2016-03-01 05:32:51 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
2016-03-04 05:54:32 +00:00
|
|
|
function getPacketFiles(callback) {
|
2016-03-01 05:32:51 +00:00
|
|
|
fs.readdir(importDir, (err, files) => {
|
2016-03-02 05:42:29 +00:00
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2016-03-04 05:54:32 +00:00
|
|
|
callback(null, files.filter(f => '.pkt' === paths.extname(f)));
|
2016-03-01 05:32:51 +00:00
|
|
|
});
|
|
|
|
},
|
2016-03-04 05:54:32 +00:00
|
|
|
function importPacketFiles(packetFiles, callback) {
|
|
|
|
let rejects = [];
|
2016-03-02 05:42:29 +00:00
|
|
|
async.each(packetFiles, (packetFile, nextFile) => {
|
2016-03-04 05:54:32 +00:00
|
|
|
self.importMessagesFromPacketFile(paths.join(importDir, packetFile), err => {
|
|
|
|
// :TODO: check err -- log / track rejects, etc.
|
|
|
|
if(err) {
|
|
|
|
rejects.push(packetFile);
|
|
|
|
}
|
2016-03-02 05:42:29 +00:00
|
|
|
nextFile();
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
// :TODO: Handle err! we should try to keep going though...
|
2016-03-04 05:54:32 +00:00
|
|
|
callback(err, packetFiles, rejects);
|
|
|
|
});
|
2016-03-02 05:42:29 +00:00
|
|
|
},
|
2016-03-04 05:54:32 +00:00
|
|
|
function handleProcessedFiles(packetFiles, rejects, callback) {
|
|
|
|
async.each(packetFiles, (packetFile, nextFile) => {
|
|
|
|
const fullPath = paths.join(importDir, packetFile);
|
|
|
|
if(rejects.indexOf(packetFile) > -1) {
|
|
|
|
// :TODO: rename to .bad, perhaps move to a rejects dir + log
|
|
|
|
nextFile();
|
|
|
|
} else {
|
2016-03-09 05:30:04 +00:00
|
|
|
fs.unlink(fullPath, err => {
|
2016-03-04 05:54:32 +00:00
|
|
|
nextFile();
|
2016-03-09 05:30:04 +00:00
|
|
|
});
|
2016-03-04 05:54:32 +00:00
|
|
|
}
|
|
|
|
}, err => {
|
|
|
|
callback(err);
|
|
|
|
});
|
2016-03-01 05:32:51 +00:00
|
|
|
}
|
2016-03-04 05:54:32 +00:00
|
|
|
],
|
2016-03-01 05:32:51 +00:00
|
|
|
err => {
|
|
|
|
cb(err);
|
2016-03-04 05:54:32 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
this.importMessagesFromDirectory = function(inboundType, importDir, cb) {
|
|
|
|
let tempDirectory;
|
|
|
|
|
2016-03-04 05:54:32 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
// start with .pkt files
|
|
|
|
function importPacketFiles(callback) {
|
|
|
|
self.importPacketFilesFromDirectory(importDir, err => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function discoverBundles(callback) {
|
|
|
|
fs.readdir(importDir, (err, files) => {
|
|
|
|
files = files.filter(f => '.pkt' !== paths.extname(f));
|
|
|
|
|
|
|
|
async.map(files, (file, transform) => {
|
|
|
|
const fullPath = paths.join(importDir, file);
|
|
|
|
self.archUtil.detectType(fullPath, (err, archName) => {
|
|
|
|
transform(null, { path : fullPath, archName : archName } );
|
|
|
|
});
|
|
|
|
}, (err, bundleFiles) => {
|
|
|
|
callback(err, bundleFiles);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function createTempDir(bundleFiles, callback) {
|
|
|
|
temp.mkdir('enigftnimport-', (err, tempDir) => {
|
2016-03-09 05:30:04 +00:00
|
|
|
tempDirectory = tempDir;
|
|
|
|
callback(err, bundleFiles);
|
2016-03-04 05:54:32 +00:00
|
|
|
});
|
|
|
|
},
|
2016-03-09 05:30:04 +00:00
|
|
|
function importBundles(bundleFiles, callback) {
|
|
|
|
let rejects = [];
|
|
|
|
|
2016-03-04 05:54:32 +00:00
|
|
|
async.each(bundleFiles, (bundleFile, nextFile) => {
|
|
|
|
if(_.isUndefined(bundleFile.archName)) {
|
2016-03-09 05:30:04 +00:00
|
|
|
Log.info(
|
|
|
|
{ fileName : bundleFile.path },
|
|
|
|
'Unknown bundle archive type');
|
|
|
|
|
|
|
|
rejects.push(bundleFile.path);
|
|
|
|
|
2016-03-04 05:54:32 +00:00
|
|
|
return nextFile(); // unknown archive type
|
|
|
|
}
|
|
|
|
|
|
|
|
self.archUtil.extractTo(
|
|
|
|
bundleFile.path,
|
2016-03-09 05:30:04 +00:00
|
|
|
tempDirectory,
|
2016-03-04 05:54:32 +00:00
|
|
|
bundleFile.archName,
|
|
|
|
err => {
|
2016-03-09 05:30:04 +00:00
|
|
|
if(err) {
|
|
|
|
Log.info(
|
|
|
|
{ fileName : bundleFile.path, error : err.toString() },
|
|
|
|
'Failed to extract bundle');
|
|
|
|
|
|
|
|
rejects.push(bundleFile.path);
|
|
|
|
}
|
|
|
|
|
2016-03-04 05:54:32 +00:00
|
|
|
nextFile();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}, err => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// All extracted - import .pkt's
|
|
|
|
//
|
2016-03-09 05:30:04 +00:00
|
|
|
self.importPacketFilesFromDirectory(tempDirectory, err => {
|
|
|
|
callback(null, bundleFiles, rejects);
|
2016-03-04 05:54:32 +00:00
|
|
|
});
|
|
|
|
});
|
2016-03-09 05:30:04 +00:00
|
|
|
},
|
|
|
|
function handleProcessedBundleFiles(bundleFiles, rejects, callback) {
|
|
|
|
async.each(bundleFiles, (bundleFile, nextFile) => {
|
|
|
|
if(rejects.indexOf(bundleFile.path) > -1) {
|
|
|
|
// :TODO: rename to .bad, perhaps move to a rejects dir + log
|
|
|
|
nextFile();
|
|
|
|
} else {
|
|
|
|
fs.unlink(bundleFile.path, err => {
|
|
|
|
nextFile();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, err => {
|
|
|
|
callback(err);
|
|
|
|
});
|
2016-03-04 05:54:32 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
2016-03-09 05:30:04 +00:00
|
|
|
if(tempDirectory) {
|
|
|
|
temp.cleanup( (errIgnored, stats) => {
|
|
|
|
Log.trace(
|
|
|
|
Object.assign(stats, { tempDir : tempDirectory } ),
|
|
|
|
'Temporary directory cleaned up'
|
|
|
|
);
|
|
|
|
|
|
|
|
cb(err); // orig err
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
cb(err);
|
|
|
|
}
|
2016-03-04 05:54:32 +00:00
|
|
|
}
|
2016-03-01 05:32:51 +00:00
|
|
|
);
|
|
|
|
};
|
2016-02-10 05:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
require('util').inherits(FTNMessageScanTossModule, MessageScanTossModule);
|
|
|
|
|
|
|
|
FTNMessageScanTossModule.prototype.startup = function(cb) {
|
2016-02-17 05:11:55 +00:00
|
|
|
Log.info('FidoNet Scanner/Tosser starting up');
|
2016-02-21 00:57:38 +00:00
|
|
|
|
2016-02-29 05:04:03 +00:00
|
|
|
if(_.isObject(this.moduleConfig.schedule)) {
|
|
|
|
const exportSchedule = this.parseScheduleString(this.moduleConfig.schedule.export);
|
|
|
|
if(exportSchedule) {
|
|
|
|
if(exportSchedule.sched) {
|
|
|
|
let exporting = false;
|
|
|
|
this.exportTimer = later.setInterval( () => {
|
|
|
|
if(!exporting) {
|
|
|
|
exporting = true;
|
|
|
|
|
2016-03-01 05:32:51 +00:00
|
|
|
Log.info( { module : exports.moduleInfo.name }, 'Performing scheduled message scan/export...');
|
2016-02-29 05:04:03 +00:00
|
|
|
|
|
|
|
this.performExport(err => {
|
|
|
|
exporting = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, exportSchedule.sched);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(exportSchedule.watchFile) {
|
|
|
|
// :TODO: monitor file for changes/existance with gaze
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 05:32:51 +00:00
|
|
|
|
|
|
|
const importSchedule = this.parseScheduleString(this.moduleConfig.schedule.import);
|
|
|
|
if(importSchedule) {
|
|
|
|
if(importSchedule.sched) {
|
|
|
|
let importing = false;
|
|
|
|
this.importTimer = later.setInterval( () => {
|
|
|
|
if(!importing) {
|
|
|
|
importing = true;
|
|
|
|
|
|
|
|
Log.info( { module : exports.moduleInfo.name }, 'Performing scheduled message import/toss...');
|
|
|
|
|
|
|
|
this.performImport(err => {
|
|
|
|
importing = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, importSchedule.sched);
|
|
|
|
}
|
|
|
|
}
|
2016-02-29 05:04:03 +00:00
|
|
|
}
|
|
|
|
|
2016-02-21 00:57:38 +00:00
|
|
|
FTNMessageScanTossModule.super_.prototype.startup.call(this, cb);
|
2016-02-10 05:30:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
FTNMessageScanTossModule.prototype.shutdown = function(cb) {
|
2016-02-17 05:11:55 +00:00
|
|
|
Log.info('FidoNet Scanner/Tosser shutting down');
|
2016-02-29 05:04:03 +00:00
|
|
|
|
|
|
|
if(this.exportTimer) {
|
|
|
|
this.exportTimer.clear();
|
|
|
|
}
|
2016-02-17 05:11:55 +00:00
|
|
|
|
2016-02-21 00:57:38 +00:00
|
|
|
FTNMessageScanTossModule.super_.prototype.shutdown.call(this, cb);
|
2016-02-10 05:30:59 +00:00
|
|
|
};
|
|
|
|
|
2016-03-01 05:32:51 +00:00
|
|
|
FTNMessageScanTossModule.prototype.performImport = function(cb) {
|
|
|
|
if(!this.hasValidConfiguration()) {
|
|
|
|
return cb(new Error('Missing or invalid configuration'));
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
async.each( [ 'inbound', 'secInbound' ], (inboundType, nextDir) => {
|
|
|
|
self.importMessagesFromDirectory(inboundType, self.moduleConfig.paths[inboundType], err => {
|
2016-03-01 05:32:51 +00:00
|
|
|
|
|
|
|
nextDir();
|
|
|
|
});
|
|
|
|
}, cb);
|
|
|
|
};
|
|
|
|
|
2016-02-29 05:04:03 +00:00
|
|
|
FTNMessageScanTossModule.prototype.performExport = function(cb) {
|
|
|
|
//
|
|
|
|
// We're only concerned with areas related to FTN. For each area, loop though
|
|
|
|
// and let's find out what messages need exported.
|
|
|
|
//
|
|
|
|
if(!this.hasValidConfiguration()) {
|
2016-03-01 05:32:51 +00:00
|
|
|
return cb(new Error('Missing or invalid configuration'));
|
2016-02-29 05:04:03 +00:00
|
|
|
}
|
|
|
|
|
2016-03-10 05:32:00 +00:00
|
|
|
//
|
|
|
|
// Select all messages that have a message_id > our last scan ID.
|
|
|
|
// Additionally exclude messages that have a ftn_attr_flags FtnProperty meta
|
|
|
|
// as those came via import!
|
|
|
|
//
|
2016-02-29 05:04:03 +00:00
|
|
|
const getNewUuidsSql =
|
2016-02-29 05:35:43 +00:00
|
|
|
`SELECT message_id, message_uuid
|
2016-03-10 05:32:00 +00:00
|
|
|
FROM message m
|
|
|
|
WHERE area_tag = ? AND message_id > ? AND
|
|
|
|
(SELECT COUNT(message_id)
|
|
|
|
FROM message_meta
|
|
|
|
WHERE message_id = m.message_id AND meta_category = 'FtnProperty' AND meta_name = 'ftn_attr_flags') = 0
|
2016-02-29 05:04:03 +00:00
|
|
|
ORDER BY message_id;`;
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
async.each(Object.keys(Config.messageNetworks.ftn.areas), (areaTag, nextArea) => {
|
|
|
|
const areaConfig = Config.messageNetworks.ftn.areas[areaTag];
|
|
|
|
if(!this.isAreaConfigValid(areaConfig)) {
|
|
|
|
return nextArea();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// For each message that is newer than that of the last scan
|
|
|
|
// we need to export to each configured associated uplink(s)
|
|
|
|
//
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getLastScanId(callback) {
|
|
|
|
self.getAreaLastScanId(areaTag, callback);
|
|
|
|
},
|
|
|
|
function getNewUuids(lastScanId, callback) {
|
|
|
|
msgDb.all(getNewUuidsSql, [ areaTag, lastScanId ], (err, rows) => {
|
|
|
|
if(err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
2016-02-29 05:35:43 +00:00
|
|
|
if(0 === rows.length) {
|
|
|
|
let nothingToDoErr = new Error('Nothing to do!');
|
|
|
|
nothingToDoErr.noRows = true;
|
|
|
|
callback(nothingToDoErr);
|
|
|
|
} else {
|
|
|
|
callback(null, rows);
|
|
|
|
}
|
2016-02-29 05:04:03 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2016-02-29 05:35:43 +00:00
|
|
|
function exportToConfiguredUplinks(msgRows, callback) {
|
2016-03-01 05:32:51 +00:00
|
|
|
const uuidsOnly = msgRows.map(r => r.message_uuid); // convert to array of UUIDs only
|
2016-02-29 05:35:43 +00:00
|
|
|
self.exportMessagesToUplinks(uuidsOnly, areaConfig, err => {
|
2016-03-01 05:32:51 +00:00
|
|
|
const newLastScanId = msgRows[msgRows.length - 1].message_id;
|
|
|
|
|
|
|
|
Log.info(
|
2016-03-09 05:30:04 +00:00
|
|
|
{ areaTag : areaTag, messagesExported : msgRows.length, newLastScanId : newLastScanId },
|
2016-03-01 05:32:51 +00:00
|
|
|
'Export complete');
|
|
|
|
|
|
|
|
callback(err, newLastScanId);
|
2016-02-29 05:04:03 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function updateLastScanId(newLastScanId, callback) {
|
2016-02-29 05:35:43 +00:00
|
|
|
self.setAreaLastScanId(areaTag, newLastScanId, callback);
|
2016-02-29 05:04:03 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
nextArea();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}, err => {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-02-21 00:57:38 +00:00
|
|
|
FTNMessageScanTossModule.prototype.record = function(message) {
|
2016-02-17 05:11:55 +00:00
|
|
|
//
|
2016-02-29 05:35:43 +00:00
|
|
|
// :TODO: If @immediate, we should do something here!
|
2016-02-10 05:30:59 +00:00
|
|
|
};
|