2015-07-14 06:13:29 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const ftn = require('./ftn_util.js');
|
|
|
|
const Message = require('./message.js');
|
|
|
|
const sauce = require('./sauce.js');
|
|
|
|
const Address = require('./ftn_address.js');
|
|
|
|
const strUtil = require('./string_util.js');
|
|
|
|
const Log = require('./logger.js').log;
|
|
|
|
const ansiPrep = require('./ansi_prep.js');
|
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
|
|
|
const { Parser } = require('binary-parser');
|
|
|
|
const fs = require('graceful-fs');
|
|
|
|
const async = require('async');
|
|
|
|
const iconv = require('iconv-lite');
|
|
|
|
const moment = require('moment');
|
|
|
|
|
|
|
|
exports.Packet = Packet;
|
|
|
|
|
|
|
|
const FTN_PACKET_HEADER_SIZE = 58; // fixed header size
|
|
|
|
const FTN_PACKET_HEADER_TYPE = 2;
|
|
|
|
const FTN_PACKET_MESSAGE_TYPE = 2;
|
|
|
|
const FTN_PACKET_BAUD_TYPE_2_2 = 2;
|
|
|
|
|
|
|
|
// SAUCE magic header + version ("00")
|
2018-04-28 12:59:07 +00:00
|
|
|
const FTN_MESSAGE_SAUCE_HEADER = Buffer.from('SAUCE00');
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const FTN_MESSAGE_KLUDGE_PREFIX = '\x01';
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
class PacketHeader {
|
2018-06-22 05:15:04 +00:00
|
|
|
constructor(origAddr, destAddr, version, createdMoment) {
|
|
|
|
const EMPTY_ADDRESS = {
|
2018-06-23 03:26:46 +00:00
|
|
|
node : 0,
|
|
|
|
net : 0,
|
|
|
|
zone : 0,
|
|
|
|
point : 0,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
this.version = version || '2+';
|
|
|
|
this.origAddress = origAddr || EMPTY_ADDRESS;
|
|
|
|
this.destAddress = destAddr || EMPTY_ADDRESS;
|
|
|
|
this.created = createdMoment || moment();
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// uncommon to set the following explicitly
|
|
|
|
this.prodCodeLo = 0xfe; // http://ftsc.org/docs/fta-1005.003
|
|
|
|
this.prodRevLo = 0;
|
|
|
|
this.baud = 0;
|
|
|
|
this.packetType = FTN_PACKET_HEADER_TYPE;
|
|
|
|
this.password = '';
|
|
|
|
this.prodData = 0x47694e45; // "ENiG"
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
this.capWord = 0x0001;
|
|
|
|
this.capWordValidate = ((this.capWord & 0xff) << 8) | ((this.capWord >> 8) & 0xff); // swap
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
this.prodCodeHi = 0xfe; // see above
|
|
|
|
this.prodRevHi = 0;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get origAddress() {
|
|
|
|
let addr = new Address({
|
2018-06-23 03:26:46 +00:00
|
|
|
node : this.origNode,
|
|
|
|
zone : this.origZone,
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if(this.origPoint) {
|
2018-06-23 03:26:46 +00:00
|
|
|
addr.point = this.origPoint;
|
|
|
|
addr.net = this.auxNet;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
addr.net = this.origNet;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
set origAddress(address) {
|
|
|
|
if(_.isString(address)) {
|
|
|
|
address = Address.fromString(address);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.origNode = address.node;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// See FSC-48
|
|
|
|
// :TODO: disabled for now until we have separate packet writers for 2, 2+, 2+48, and 2.2
|
2018-06-22 05:15:04 +00:00
|
|
|
/*if(address.point) {
|
2018-06-23 03:26:46 +00:00
|
|
|
this.auxNet = address.origNet;
|
|
|
|
this.origNet = -1;
|
|
|
|
} else {
|
|
|
|
this.origNet = address.net;
|
|
|
|
this.auxNet = 0;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
this.origNet = address.net;
|
|
|
|
this.auxNet = 0;
|
|
|
|
|
|
|
|
this.origZone = address.zone;
|
|
|
|
this.origZone2 = address.zone;
|
|
|
|
this.origPoint = address.point || 0;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get destAddress() {
|
|
|
|
let addr = new Address({
|
2018-06-23 03:26:46 +00:00
|
|
|
node : this.destNode,
|
|
|
|
net : this.destNet,
|
|
|
|
zone : this.destZone,
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if(this.destPoint) {
|
|
|
|
addr.point = this.destPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
set destAddress(address) {
|
|
|
|
if(_.isString(address)) {
|
|
|
|
address = Address.fromString(address);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
this.destNode = address.node;
|
|
|
|
this.destNet = address.net;
|
|
|
|
this.destZone = address.zone;
|
|
|
|
this.destZone2 = address.zone;
|
|
|
|
this.destPoint = address.point || 0;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get created() {
|
|
|
|
return moment({
|
2018-06-23 03:26:46 +00:00
|
|
|
year : this.year,
|
|
|
|
month : this.month - 1, // moment uses 0 indexed months
|
|
|
|
date : this.day,
|
|
|
|
hour : this.hour,
|
|
|
|
minute : this.minute,
|
|
|
|
second : this.second
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
set created(momentCreated) {
|
|
|
|
if(!moment.isMoment(momentCreated)) {
|
|
|
|
momentCreated = moment(momentCreated);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
this.year = momentCreated.year();
|
|
|
|
this.month = momentCreated.month() + 1; // moment uses 0 indexed months
|
|
|
|
this.day = momentCreated.date(); // day of month
|
|
|
|
this.hour = momentCreated.hour();
|
|
|
|
this.minute = momentCreated.minute();
|
|
|
|
this.second = momentCreated.second();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2016-02-17 05:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.PacketHeader = PacketHeader;
|
|
|
|
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Read/Write FTN packets with support for the following formats:
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// * Type 2 FTS-0001 @ http://ftsc.org/docs/fts-0001.016 (Obsolete)
|
|
|
|
// * Type 2.2 FSC-0045 @ http://ftsc.org/docs/fsc-0045.001
|
|
|
|
// * Type 2+ FSC-0039 and FSC-0048 @ http://ftsc.org/docs/fsc-0039.004
|
|
|
|
// and http://ftsc.org/docs/fsc-0048.002
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Additional resources:
|
|
|
|
// * Writeup on differences between type 2, 2.2, and 2+:
|
|
|
|
// http://walon.org/pub/fidonet/FTSC-nodelists-etc./pkt-types.txt
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2019-12-06 03:48:13 +00:00
|
|
|
const PacketHeaderParser = new Parser()
|
|
|
|
.uint16le('origNode')
|
|
|
|
.uint16le('destNode')
|
|
|
|
.uint16le('year')
|
|
|
|
.uint16le('month')
|
|
|
|
.uint16le('day')
|
|
|
|
.uint16le('hour')
|
|
|
|
.uint16le('minute')
|
|
|
|
.uint16le('second')
|
|
|
|
.uint16le('baud')
|
|
|
|
.uint16le('packetType')
|
|
|
|
.uint16le('origNet')
|
|
|
|
.uint16le('destNet')
|
|
|
|
.int8('prodCodeLo')
|
|
|
|
.int8('prodRevLo') // aka serialNo
|
|
|
|
.buffer('password', { length : 8 }) // can't use string; need CP437 - see https://github.com/keichi/binary-parser/issues/33
|
|
|
|
.uint16le('origZone')
|
|
|
|
.uint16le('destZone')
|
|
|
|
//
|
|
|
|
// The following is "filler" in FTS-0001, specifics in
|
|
|
|
// FSC-0045 and FSC-0048
|
|
|
|
//
|
|
|
|
.uint16le('auxNet')
|
|
|
|
.uint16le('capWordValidate')
|
|
|
|
.int8('prodCodeHi')
|
|
|
|
.int8('prodRevHi')
|
|
|
|
.uint16le('capWord')
|
|
|
|
.uint16le('origZone2')
|
|
|
|
.uint16le('destZone2')
|
|
|
|
.uint16le('origPoint')
|
|
|
|
.uint16le('destPoint')
|
|
|
|
.uint32le('prodData');
|
|
|
|
|
|
|
|
const MessageHeaderParser = new Parser()
|
|
|
|
.uint16le('messageType')
|
|
|
|
.uint16le('ftn_msg_orig_node')
|
|
|
|
.uint16le('ftn_msg_dest_node')
|
|
|
|
.uint16le('ftn_msg_orig_net')
|
|
|
|
.uint16le('ftn_msg_dest_net')
|
|
|
|
.uint16le('ftn_attr_flags')
|
|
|
|
.uint16le('ftn_cost')
|
|
|
|
//
|
|
|
|
// It would be nice to just string() these, but we want CP437 which requires
|
|
|
|
// iconv. Another option would be to use a formatter, but until issue 33
|
|
|
|
// (https://github.com/keichi/binary-parser/issues/33) is fixed, this is cumbersome.
|
|
|
|
//
|
|
|
|
.array('modDateTime', {
|
|
|
|
type : 'uint8',
|
|
|
|
length : 20, // FTS-0001.016: 20 bytes
|
|
|
|
})
|
|
|
|
.array('toUserName', {
|
|
|
|
type : 'uint8',
|
|
|
|
// :TODO: array needs some soft of 'limit' field
|
|
|
|
readUntil : b => 0x00 === b,
|
|
|
|
})
|
|
|
|
.array('fromUserName', {
|
|
|
|
type : 'uint8',
|
|
|
|
readUntil : b => 0x00 === b,
|
|
|
|
})
|
|
|
|
.array('subject', {
|
|
|
|
type : 'uint8',
|
|
|
|
readUntil : b => 0x00 === b,
|
|
|
|
})
|
|
|
|
.array('message', {
|
|
|
|
type : 'uint8',
|
|
|
|
readUntil : b => 0x00 === b,
|
|
|
|
});
|
|
|
|
|
2016-03-23 04:24:00 +00:00
|
|
|
function Packet(options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.options = options || {};
|
|
|
|
|
|
|
|
this.parsePacketHeader = function(packetBuffer, cb) {
|
|
|
|
assert(Buffer.isBuffer(packetBuffer));
|
|
|
|
|
|
|
|
let packetHeader;
|
|
|
|
try {
|
2019-12-06 03:48:13 +00:00
|
|
|
packetHeader = PacketHeaderParser.parse(packetBuffer);
|
2018-06-22 05:15:04 +00:00
|
|
|
} catch(e) {
|
|
|
|
return Errors.Invalid(`Unable to parse FTN packet header: ${e.message}`);
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// Convert password from NULL padded array to string
|
2018-06-22 05:15:04 +00:00
|
|
|
packetHeader.password = strUtil.stringFromNullTermBuffer(packetHeader.password, 'CP437');
|
|
|
|
|
|
|
|
if(FTN_PACKET_HEADER_TYPE !== packetHeader.packetType) {
|
|
|
|
return cb(Errors.Invalid(`Unsupported FTN packet header type: ${packetHeader.packetType}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// What kind of packet do we really have here?
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: adjust values based on version discovered
|
2018-06-22 05:15:04 +00:00
|
|
|
if(FTN_PACKET_BAUD_TYPE_2_2 === packetHeader.baud) {
|
|
|
|
packetHeader.version = '2.2';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// See FSC-0045
|
|
|
|
packetHeader.origPoint = packetHeader.year;
|
|
|
|
packetHeader.destPoint = packetHeader.month;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
packetHeader.destDomain = packetHeader.origZone2;
|
2018-06-23 03:26:46 +00:00
|
|
|
packetHeader.origDomain = packetHeader.auxNet;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// See heuristics described in FSC-0048, "Receiving Type-2+ bundles"
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const capWordValidateSwapped =
|
2018-06-23 03:26:46 +00:00
|
|
|
((packetHeader.capWordValidate & 0xff) << 8) |
|
|
|
|
((packetHeader.capWordValidate >> 8) & 0xff);
|
2018-01-21 18:58:19 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(capWordValidateSwapped === packetHeader.capWord &&
|
2018-06-23 03:26:46 +00:00
|
|
|
0 != packetHeader.capWord &&
|
|
|
|
packetHeader.capWord & 0x0001)
|
2018-06-22 05:15:04 +00:00
|
|
|
{
|
|
|
|
packetHeader.version = '2+';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// See FSC-0048
|
2018-06-22 05:15:04 +00:00
|
|
|
if(-1 === packetHeader.origNet) {
|
|
|
|
packetHeader.origNet = packetHeader.auxNet;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
packetHeader.version = '2';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: should fill bytes be 0?
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
packetHeader.created = moment({
|
2018-06-23 03:26:46 +00:00
|
|
|
year : packetHeader.year,
|
|
|
|
month : packetHeader.month - 1, // moment uses 0 indexed months
|
|
|
|
date : packetHeader.day,
|
|
|
|
hour : packetHeader.hour,
|
|
|
|
minute : packetHeader.minute,
|
|
|
|
second : packetHeader.second
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const ph = new PacketHeader();
|
|
|
|
_.assign(ph, packetHeader);
|
|
|
|
|
|
|
|
return cb(null, ph);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getPacketHeaderBuffer = function(packetHeader) {
|
|
|
|
let buffer = Buffer.alloc(FTN_PACKET_HEADER_SIZE);
|
|
|
|
|
|
|
|
buffer.writeUInt16LE(packetHeader.origNode, 0);
|
|
|
|
buffer.writeUInt16LE(packetHeader.destNode, 2);
|
|
|
|
buffer.writeUInt16LE(packetHeader.year, 4);
|
|
|
|
buffer.writeUInt16LE(packetHeader.month, 6);
|
|
|
|
buffer.writeUInt16LE(packetHeader.day, 8);
|
|
|
|
buffer.writeUInt16LE(packetHeader.hour, 10);
|
|
|
|
buffer.writeUInt16LE(packetHeader.minute, 12);
|
|
|
|
buffer.writeUInt16LE(packetHeader.second, 14);
|
|
|
|
|
|
|
|
buffer.writeUInt16LE(packetHeader.baud, 16);
|
|
|
|
buffer.writeUInt16LE(FTN_PACKET_HEADER_TYPE, 18);
|
|
|
|
buffer.writeUInt16LE(-1 === packetHeader.origNet ? 0xffff : packetHeader.origNet, 20);
|
|
|
|
buffer.writeUInt16LE(packetHeader.destNet, 22);
|
|
|
|
buffer.writeUInt8(packetHeader.prodCodeLo, 24);
|
|
|
|
buffer.writeUInt8(packetHeader.prodRevHi, 25);
|
|
|
|
|
|
|
|
const pass = ftn.stringToNullPaddedBuffer(packetHeader.password, 8);
|
|
|
|
pass.copy(buffer, 26);
|
|
|
|
|
|
|
|
buffer.writeUInt16LE(packetHeader.origZone, 34);
|
|
|
|
buffer.writeUInt16LE(packetHeader.destZone, 36);
|
|
|
|
buffer.writeUInt16LE(packetHeader.auxNet, 38);
|
|
|
|
buffer.writeUInt16LE(packetHeader.capWordValidate, 40);
|
|
|
|
buffer.writeUInt8(packetHeader.prodCodeHi, 42);
|
|
|
|
buffer.writeUInt8(packetHeader.prodRevLo, 43);
|
|
|
|
buffer.writeUInt16LE(packetHeader.capWord, 44);
|
|
|
|
buffer.writeUInt16LE(packetHeader.origZone2, 46);
|
|
|
|
buffer.writeUInt16LE(packetHeader.destZone2, 48);
|
|
|
|
buffer.writeUInt16LE(packetHeader.origPoint, 50);
|
|
|
|
buffer.writeUInt16LE(packetHeader.destPoint, 52);
|
|
|
|
buffer.writeUInt32LE(packetHeader.prodData, 54);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.writePacketHeader = function(packetHeader, ws) {
|
|
|
|
let buffer = Buffer.alloc(FTN_PACKET_HEADER_SIZE);
|
|
|
|
|
|
|
|
buffer.writeUInt16LE(packetHeader.origNode, 0);
|
|
|
|
buffer.writeUInt16LE(packetHeader.destNode, 2);
|
|
|
|
buffer.writeUInt16LE(packetHeader.year, 4);
|
|
|
|
buffer.writeUInt16LE(packetHeader.month, 6);
|
|
|
|
buffer.writeUInt16LE(packetHeader.day, 8);
|
|
|
|
buffer.writeUInt16LE(packetHeader.hour, 10);
|
|
|
|
buffer.writeUInt16LE(packetHeader.minute, 12);
|
|
|
|
buffer.writeUInt16LE(packetHeader.second, 14);
|
|
|
|
|
|
|
|
buffer.writeUInt16LE(packetHeader.baud, 16);
|
|
|
|
buffer.writeUInt16LE(FTN_PACKET_HEADER_TYPE, 18);
|
|
|
|
buffer.writeUInt16LE(-1 === packetHeader.origNet ? 0xffff : packetHeader.origNet, 20);
|
|
|
|
buffer.writeUInt16LE(packetHeader.destNet, 22);
|
|
|
|
buffer.writeUInt8(packetHeader.prodCodeLo, 24);
|
|
|
|
buffer.writeUInt8(packetHeader.prodRevHi, 25);
|
|
|
|
|
|
|
|
const pass = ftn.stringToNullPaddedBuffer(packetHeader.password, 8);
|
|
|
|
pass.copy(buffer, 26);
|
|
|
|
|
|
|
|
buffer.writeUInt16LE(packetHeader.origZone, 34);
|
|
|
|
buffer.writeUInt16LE(packetHeader.destZone, 36);
|
|
|
|
buffer.writeUInt16LE(packetHeader.auxNet, 38);
|
|
|
|
buffer.writeUInt16LE(packetHeader.capWordValidate, 40);
|
|
|
|
buffer.writeUInt8(packetHeader.prodCodeHi, 42);
|
|
|
|
buffer.writeUInt8(packetHeader.prodRevLo, 43);
|
|
|
|
buffer.writeUInt16LE(packetHeader.capWord, 44);
|
|
|
|
buffer.writeUInt16LE(packetHeader.origZone2, 46);
|
|
|
|
buffer.writeUInt16LE(packetHeader.destZone2, 48);
|
|
|
|
buffer.writeUInt16LE(packetHeader.origPoint, 50);
|
|
|
|
buffer.writeUInt16LE(packetHeader.destPoint, 52);
|
|
|
|
buffer.writeUInt32LE(packetHeader.prodData, 54);
|
|
|
|
|
|
|
|
ws.write(buffer);
|
|
|
|
|
|
|
|
return buffer.length;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.processMessageBody = function(messageBodyBuffer, cb) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// From FTS-0001.16:
|
|
|
|
// "Message text is unbounded and null terminated (note exception below).
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// A 'hard' carriage return, 0DH, marks the end of a paragraph, and must
|
|
|
|
// be preserved.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// So called 'soft' carriage returns, 8DH, may mark a previous
|
|
|
|
// processor's automatic line wrap, and should be ignored. Beware that
|
|
|
|
// they may be followed by linefeeds, or may not.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// All linefeeds, 0AH, should be ignored. Systems which display message
|
|
|
|
// text should wrap long lines to suit their application."
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// This can be a bit tricky:
|
|
|
|
// * Decoding as CP437 converts 0x8d -> 0xec, so we'll need to correct for that
|
|
|
|
// * Many kludge lines specify an encoding. If we find one of such lines, we'll
|
|
|
|
// likely need to re-decode as the specified encoding
|
|
|
|
// * SAUCE is binary-ish data, so we need to inspect for it before any
|
|
|
|
// decoding occurs
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
let messageBodyData = {
|
2018-06-23 03:26:46 +00:00
|
|
|
message : [],
|
|
|
|
kludgeLines : {}, // KLUDGE:[value1, value2, ...] map
|
|
|
|
seenBy : [],
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function addKludgeLine(line) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// We have to special case INTL/TOPT/FMPT as they don't contain
|
|
|
|
// a ':' name/value separator like the rest of the kludge lines... because stupdity.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
let key = line.substr(0, 4).trim();
|
|
|
|
let value;
|
|
|
|
if( ['INTL', 'TOPT', 'FMPT', 'Via' ].includes(key)) {
|
|
|
|
value = line.substr(key.length).trim();
|
|
|
|
} else {
|
|
|
|
const sepIndex = line.indexOf(':');
|
2018-06-23 03:26:46 +00:00
|
|
|
key = line.substr(0, sepIndex).toUpperCase();
|
|
|
|
value = line.substr(sepIndex + 1).trim();
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Allow mapped value to be either a key:value if there is only
|
|
|
|
// one entry, or key:[value1, value2,...] if there are more
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(messageBodyData.kludgeLines[key]) {
|
|
|
|
if(!_.isArray(messageBodyData.kludgeLines[key])) {
|
|
|
|
messageBodyData.kludgeLines[key] = [ messageBodyData.kludgeLines[key] ];
|
|
|
|
}
|
|
|
|
messageBodyData.kludgeLines[key].push(value);
|
|
|
|
} else {
|
|
|
|
messageBodyData.kludgeLines[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let encoding = 'cp437';
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function extractSauce(callback) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: This is wrong: SAUCE may not have EOF marker for one, also if it's
|
|
|
|
// present, we need to extract it but keep the rest of hte message intact as it likely
|
|
|
|
// has SEEN-BY, PATH, and other kludge information *appended*
|
2018-06-22 05:15:04 +00:00
|
|
|
const sauceHeaderPosition = messageBodyBuffer.indexOf(FTN_MESSAGE_SAUCE_HEADER);
|
|
|
|
if(sauceHeaderPosition > -1) {
|
|
|
|
sauce.readSAUCE(messageBodyBuffer.slice(sauceHeaderPosition, sauceHeaderPosition + sauce.SAUCE_SIZE), (err, theSauce) => {
|
|
|
|
if(!err) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// we read some SAUCE - don't re-process that portion into the body
|
|
|
|
messageBodyBuffer = messageBodyBuffer.slice(0, sauceHeaderPosition) + messageBodyBuffer.slice(sauceHeaderPosition + sauce.SAUCE_SIZE);
|
|
|
|
// messageBodyBuffer = messageBodyBuffer.slice(0, sauceHeaderPosition);
|
|
|
|
messageBodyData.sauce = theSauce;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
|
|
|
Log.warn( { error : err.message }, 'Found what looks like to be a SAUCE record, but failed to read');
|
|
|
|
}
|
2018-06-23 03:26:46 +00:00
|
|
|
return callback(null); // failure to read SAUCE is OK
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function extractChrsAndDetermineEncoding(callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// From FTS-5003.001:
|
|
|
|
// "The CHRS control line is formatted as follows:
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// ^ACHRS: <identifier> <level>
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Where <identifier> is a character string of no more than eight (8)
|
|
|
|
// ASCII characters identifying the character set or character encoding
|
|
|
|
// scheme used, and level is a positive integer value describing what
|
|
|
|
// level of CHRS the message is written in."
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Also according to the spec, the deprecated "CHARSET" value may be used
|
|
|
|
// :TODO: Look into CHARSET more - should we bother supporting it?
|
|
|
|
// :TODO: See encodingFromHeader() for CHRS/CHARSET support @ https://github.com/Mithgol/node-fidonet-jam
|
|
|
|
const FTN_CHRS_PREFIX = Buffer.from( [ 0x01, 0x43, 0x48, 0x52, 0x53, 0x3a, 0x20 ] ); // "\x01CHRS:"
|
|
|
|
const FTN_CHRS_SUFFIX = Buffer.from( [ 0x0d ] );
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
let chrsPrefixIndex = messageBodyBuffer.indexOf(FTN_CHRS_PREFIX);
|
|
|
|
if(chrsPrefixIndex < 0) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
chrsPrefixIndex += FTN_CHRS_PREFIX.length;
|
|
|
|
|
|
|
|
const chrsEndIndex = messageBodyBuffer.indexOf(FTN_CHRS_SUFFIX, chrsPrefixIndex);
|
|
|
|
if(chrsEndIndex < 0) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
let chrsContent = messageBodyBuffer.slice(chrsPrefixIndex, chrsEndIndex);
|
|
|
|
if(0 === chrsContent.length) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
chrsContent = iconv.decode(chrsContent, 'CP437');
|
|
|
|
const chrsEncoding = ftn.getEncodingFromCharacterSetIdentifier(chrsContent);
|
|
|
|
if(chrsEncoding) {
|
|
|
|
encoding = chrsEncoding;
|
|
|
|
}
|
|
|
|
return callback(null);
|
|
|
|
},
|
|
|
|
function extractMessageData(callback) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Decode |messageBodyBuffer| using |encoding| defaulted or detected above
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Look into \xec thing more - document
|
2018-06-22 05:15:04 +00:00
|
|
|
let decoded;
|
|
|
|
try {
|
|
|
|
decoded = iconv.decode(messageBodyBuffer, encoding);
|
|
|
|
} catch(e) {
|
|
|
|
Log.debug( { encoding : encoding, error : e.toString() }, 'Error decoding. Falling back to ASCII');
|
|
|
|
decoded = iconv.decode(messageBodyBuffer, 'ascii');
|
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const messageLines = strUtil.splitTextAtTerms(decoded.replace(/\xec/g, ''));
|
|
|
|
let endOfMessage = false;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
messageLines.forEach(line => {
|
|
|
|
if(0 === line.length) {
|
|
|
|
messageBodyData.message.push('');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(line.startsWith('AREA:')) {
|
|
|
|
messageBodyData.area = line.substring(line.indexOf(':') + 1).trim();
|
|
|
|
} else if(line.startsWith('--- ')) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// Tear Lines are tracked allowing for specialized display/etc.
|
2018-06-22 05:15:04 +00:00
|
|
|
messageBodyData.tearLine = line;
|
2018-06-23 03:26:46 +00:00
|
|
|
} else if(/^[ ]{1,2}\* Origin: /.test(line)) { // To spec is " * Origin: ..."
|
2018-06-22 05:15:04 +00:00
|
|
|
messageBodyData.originLine = line;
|
2018-06-23 03:26:46 +00:00
|
|
|
endOfMessage = true; // Anything past origin is not part of the message body
|
2018-06-22 05:15:04 +00:00
|
|
|
} else if(line.startsWith('SEEN-BY:')) {
|
2018-06-23 03:26:46 +00:00
|
|
|
endOfMessage = true; // Anything past the first SEEN-BY is not part of the message body
|
2018-06-22 05:15:04 +00:00
|
|
|
messageBodyData.seenBy.push(line.substring(line.indexOf(':') + 1).trim());
|
|
|
|
} else if(FTN_MESSAGE_KLUDGE_PREFIX === line.charAt(0)) {
|
|
|
|
if('PATH:' === line.slice(1, 6)) {
|
2018-06-23 03:26:46 +00:00
|
|
|
endOfMessage = true; // Anything pats the first PATH is not part of the message body
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
addKludgeLine(line.slice(1));
|
|
|
|
} else if(!endOfMessage) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// regular ol' message line
|
2018-06-22 05:15:04 +00:00
|
|
|
messageBodyData.message.push(line);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
() => {
|
|
|
|
messageBodyData.message = messageBodyData.message.join('\n');
|
|
|
|
return cb(messageBodyData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.parsePacketMessages = function(header, packetBuffer, iterator, cb) {
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Check for end-of-messages marker up front before parse so we can easily
|
|
|
|
// tell the difference between end and bad header
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(packetBuffer.length < 3) {
|
|
|
|
const peek = packetBuffer.slice(0, 2);
|
|
|
|
if(peek.equals(Buffer.from([ 0x00 ])) || peek.equals(Buffer.from( [ 0x00, 0x00 ]))) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// end marker - no more messages
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(null);
|
|
|
|
}
|
2018-06-23 03:26:46 +00:00
|
|
|
// else fall through & hit exception below to log error
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let msgData;
|
|
|
|
try {
|
2019-12-06 03:48:13 +00:00
|
|
|
msgData = MessageHeaderParser.parse(packetBuffer);
|
2018-06-22 05:15:04 +00:00
|
|
|
} catch(e) {
|
|
|
|
return cb(Errors.Invalid(`Failed to parse FTN message header: ${e.message}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(FTN_PACKET_MESSAGE_TYPE != msgData.messageType) {
|
|
|
|
return cb(Errors.Invalid(`Unsupported FTN message type: ${msgData.messageType}`));
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Convert null terminated arrays to strings
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2019-11-23 04:44:24 +00:00
|
|
|
// From FTS-0001.016:
|
|
|
|
// * modDateTime: 20 bytes exactly (see above)
|
|
|
|
// * toUserName and fromUserName: *max* 36 bytes, aka "up to"; null terminated
|
|
|
|
// * subject: *max* 72 bytes, aka "up to"; null terminated
|
|
|
|
// * message: Unbounded & null terminated
|
|
|
|
//
|
|
|
|
// For everything above but message, we can get away with assuming CP437
|
|
|
|
// and probably even just "ascii" for most cases. The message field is
|
|
|
|
// much more complex so we'll look for encoding kludges, detection, etc.
|
|
|
|
// later on.
|
|
|
|
//
|
|
|
|
if(msgData.modDateTime.length != 20) {
|
|
|
|
return cb(Errors.Invalid(`FTN packet DateTime field must be 20 bytes (got ${msgData.modDateTime.length})`));
|
|
|
|
}
|
|
|
|
if(msgData.toUserName.length > 36) {
|
|
|
|
return cb(Errors.Invalid(`FTN packet toUserName field must be 36 bytes max (got ${msgData.toUserName.length})`));
|
|
|
|
}
|
|
|
|
if(msgData.fromUserName.length > 36) {
|
|
|
|
return cb(Errors.Invalid(`FTN packet fromUserName field must be 36 bytes max (got ${msgData.fromUserName.length})`));
|
|
|
|
}
|
|
|
|
if(msgData.subject.length > 72) {
|
|
|
|
return cb(Errors.Invalid(`FTN packet subject field must be 72 bytes max (got ${msgData.subject.length})`));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Arrays of CP437 bytes -> String
|
2018-06-22 05:15:04 +00:00
|
|
|
[ 'modDateTime', 'toUserName', 'fromUserName', 'subject' ].forEach(k => {
|
|
|
|
msgData[k] = strUtil.stringFromNullTermBuffer(msgData[k], 'CP437');
|
|
|
|
});
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// The message body itself is a special beast as it may
|
|
|
|
// contain an origin line, kludges, SAUCE in the case
|
|
|
|
// of ANSI files, etc.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
const msg = new Message( {
|
2018-06-23 03:26:46 +00:00
|
|
|
toUserName : msgData.toUserName,
|
|
|
|
fromUserName : msgData.fromUserName,
|
|
|
|
subject : msgData.subject,
|
|
|
|
modTimestamp : ftn.getDateFromFtnDateTime(msgData.modDateTime),
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: When non-private (e.g. EchoMail), attempt to extract SRC from MSGID vs headers, when avail (or Orgin line? research further)
|
2018-06-22 05:15:04 +00:00
|
|
|
msg.meta.FtnProperty = {
|
2018-06-23 03:26:46 +00:00
|
|
|
ftn_orig_node : header.origNode,
|
|
|
|
ftn_dest_node : header.destNode,
|
|
|
|
ftn_orig_network : header.origNet,
|
|
|
|
ftn_dest_network : header.destNet,
|
|
|
|
|
|
|
|
ftn_attr_flags : msgData.ftn_attr_flags,
|
|
|
|
ftn_cost : msgData.ftn_cost,
|
|
|
|
|
|
|
|
ftn_msg_orig_node : msgData.ftn_msg_orig_node,
|
|
|
|
ftn_msg_dest_node : msgData.ftn_msg_dest_node,
|
|
|
|
ftn_msg_orig_net : msgData.ftn_msg_orig_net,
|
|
|
|
ftn_msg_dest_net : msgData.ftn_msg_dest_net,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.processMessageBody(msgData.message, messageBodyData => {
|
2018-06-23 03:26:46 +00:00
|
|
|
msg.message = messageBodyData.message;
|
|
|
|
msg.meta.FtnKludge = messageBodyData.kludgeLines;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
if(messageBodyData.tearLine) {
|
|
|
|
msg.meta.FtnProperty.ftn_tear_line = messageBodyData.tearLine;
|
|
|
|
|
|
|
|
if(self.options.keepTearAndOrigin) {
|
|
|
|
msg.message += `\r\n${messageBodyData.tearLine}\r\n`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(messageBodyData.seenBy.length > 0) {
|
|
|
|
msg.meta.FtnProperty.ftn_seen_by = messageBodyData.seenBy;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(messageBodyData.area) {
|
|
|
|
msg.meta.FtnProperty.ftn_area = messageBodyData.area;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(messageBodyData.originLine) {
|
|
|
|
msg.meta.FtnProperty.ftn_origin = messageBodyData.originLine;
|
|
|
|
|
|
|
|
if(self.options.keepTearAndOrigin) {
|
|
|
|
msg.message += `${messageBodyData.originLine}\r\n`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2020-11-18 02:06:54 +00:00
|
|
|
// Attempt to handle FTN time zone kludges of 'TZUTC' and
|
|
|
|
// 'TZUTCINFO'.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2020-11-18 02:06:54 +00:00
|
|
|
// See http://ftsc.org/docs/frl-1004.002
|
|
|
|
//
|
|
|
|
const tzKludge = msg.meta.FtnKludge.TZUTC || msg.meta.FtnKludge.TZUTCINFO;
|
|
|
|
const tzMatch = /([+-]?)([0-9]{2})([0-9]{2})/.exec(tzKludge);
|
|
|
|
if (tzMatch) {
|
|
|
|
//
|
|
|
|
// - Both kludges should provide a offset in hhmm format
|
|
|
|
// - Negative offsets must proceed with '-'
|
|
|
|
// - Positive offsets must not (to spec) proceed with '+', but
|
|
|
|
// we'll allow it.
|
|
|
|
//
|
|
|
|
const [, sign, hours, minutes ] = tzMatch;
|
|
|
|
|
|
|
|
// convert to a [+|-]hh:mm format.
|
|
|
|
// example: 1300 -> +13:00
|
|
|
|
const utcOffset = `${sign||'+'}${hours}:${minutes}`;
|
|
|
|
|
|
|
|
// finally, update our modTimestamp
|
|
|
|
msg.modTimestamp = msg.modTimestamp.utcOffset(utcOffset);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Parser should give is this info:
|
2018-06-22 05:15:04 +00:00
|
|
|
const bytesRead =
|
2018-06-23 03:26:46 +00:00
|
|
|
14 + // fixed header size
|
|
|
|
msgData.modDateTime.length + 1 + // +1 = NULL
|
|
|
|
msgData.toUserName.length + 1 + // +1 = NULL
|
|
|
|
msgData.fromUserName.length + 1 + // +1 = NULL
|
|
|
|
msgData.subject.length + 1 + // +1 = NULL
|
|
|
|
msgData.message.length; // includes NULL
|
2018-01-21 18:58:19 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const nextBuf = packetBuffer.slice(bytesRead);
|
|
|
|
if(nextBuf.length > 0) {
|
|
|
|
const next = function(e) {
|
|
|
|
if(e) {
|
|
|
|
cb(e);
|
|
|
|
} else {
|
|
|
|
self.parsePacketMessages(header, nextBuf, iterator, cb);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
iterator('message', msg, next);
|
|
|
|
} else {
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sanatizeFtnProperties = function(message) {
|
|
|
|
[
|
|
|
|
Message.FtnPropertyNames.FtnOrigNode,
|
|
|
|
Message.FtnPropertyNames.FtnDestNode,
|
|
|
|
Message.FtnPropertyNames.FtnOrigNetwork,
|
|
|
|
Message.FtnPropertyNames.FtnDestNetwork,
|
|
|
|
Message.FtnPropertyNames.FtnAttrFlags,
|
|
|
|
Message.FtnPropertyNames.FtnCost,
|
|
|
|
Message.FtnPropertyNames.FtnOrigZone,
|
|
|
|
Message.FtnPropertyNames.FtnDestZone,
|
|
|
|
Message.FtnPropertyNames.FtnOrigPoint,
|
|
|
|
Message.FtnPropertyNames.FtnDestPoint,
|
|
|
|
Message.FtnPropertyNames.FtnAttribute,
|
|
|
|
Message.FtnPropertyNames.FtnMsgOrigNode,
|
|
|
|
Message.FtnPropertyNames.FtnMsgDestNode,
|
|
|
|
Message.FtnPropertyNames.FtnMsgOrigNet,
|
|
|
|
Message.FtnPropertyNames.FtnMsgDestNet,
|
|
|
|
].forEach( propName => {
|
|
|
|
if(message.meta.FtnProperty[propName]) {
|
|
|
|
message.meta.FtnProperty[propName] = parseInt(message.meta.FtnProperty[propName]) || 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
this.writeMessageHeader = function(message, buf) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// ensure address FtnProperties are numbers
|
2018-06-22 05:15:04 +00:00
|
|
|
self.sanatizeFtnProperties(message);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const destNode = message.meta.FtnProperty.ftn_msg_dest_node || message.meta.FtnProperty.ftn_dest_node;
|
|
|
|
const destNet = message.meta.FtnProperty.ftn_msg_dest_net || message.meta.FtnProperty.ftn_dest_network;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
buf.writeUInt16LE(FTN_PACKET_MESSAGE_TYPE, 0);
|
|
|
|
buf.writeUInt16LE(message.meta.FtnProperty.ftn_orig_node, 2);
|
|
|
|
buf.writeUInt16LE(destNode, 4);
|
|
|
|
buf.writeUInt16LE(message.meta.FtnProperty.ftn_orig_network, 6);
|
|
|
|
buf.writeUInt16LE(destNet, 8);
|
|
|
|
buf.writeUInt16LE(message.meta.FtnProperty.ftn_attr_flags, 10);
|
|
|
|
buf.writeUInt16LE(message.meta.FtnProperty.ftn_cost, 12);
|
|
|
|
|
|
|
|
const dateTimeBuffer = Buffer.from(ftn.getDateTimeString(message.modTimestamp) + '\0');
|
|
|
|
dateTimeBuffer.copy(buf, 14);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getMessageEntryBuffer = function(message, options, cb) {
|
|
|
|
|
|
|
|
function getAppendMeta(k, m, sepChar=':') {
|
|
|
|
let append = '';
|
|
|
|
if(m) {
|
|
|
|
let a = m;
|
|
|
|
if(!_.isArray(a)) {
|
|
|
|
a = [ a ];
|
|
|
|
}
|
|
|
|
a.forEach(v => {
|
|
|
|
append += `${k}${sepChar} ${v}\r`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return append;
|
|
|
|
}
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function prepareHeaderAndKludges(callback) {
|
|
|
|
const basicHeader = Buffer.alloc(34);
|
|
|
|
self.writeMessageHeader(message, basicHeader);
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// To, from, and subject must be NULL term'd and have max lengths as per spec.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
const toUserNameBuf = strUtil.stringToNullTermBuffer(message.toUserName, { encoding : 'cp437', maxBufLen : 36 } );
|
|
|
|
const fromUserNameBuf = strUtil.stringToNullTermBuffer(message.fromUserName, { encoding : 'cp437', maxBufLen : 36 } );
|
|
|
|
const subjectBuf = strUtil.stringToNullTermBuffer(message.subject, { encoding : 'cp437', maxBufLen : 72 } );
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// message: unbound length, NULL term'd
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// We need to build in various special lines - kludges, area,
|
|
|
|
// seen-by, etc.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
let msgBody = '';
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// FTN-0004.001 @ http://ftsc.org/docs/fts-0004.001
|
|
|
|
// AREA:CONFERENCE
|
|
|
|
// Should be first line in a message
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(message.meta.FtnProperty.ftn_area) {
|
2018-06-23 03:26:46 +00:00
|
|
|
msgBody += `AREA:${message.meta.FtnProperty.ftn_area}\r`; // note: no ^A (0x01)
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: DRY with similar function in this file!
|
2018-06-22 05:15:04 +00:00
|
|
|
Object.keys(message.meta.FtnKludge).forEach(k => {
|
|
|
|
switch(k) {
|
|
|
|
case 'PATH' :
|
2018-06-23 03:26:46 +00:00
|
|
|
break; // skip & save for last
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
case 'Via' :
|
|
|
|
case 'FMPT' :
|
|
|
|
case 'TOPT' :
|
|
|
|
case 'INTL' :
|
2018-06-23 03:26:46 +00:00
|
|
|
msgBody += getAppendMeta(`\x01${k}`, message.meta.FtnKludge[k], ''); // no sepChar
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
default :
|
2018-06-22 05:15:04 +00:00
|
|
|
msgBody += getAppendMeta(`\x01${k}`, message.meta.FtnKludge[k]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return callback(null, basicHeader, toUserNameBuf, fromUserNameBuf, subjectBuf, msgBody);
|
|
|
|
},
|
|
|
|
function prepareAnsiMessageBody(basicHeader, toUserNameBuf, fromUserNameBuf, subjectBuf, msgBody, callback) {
|
|
|
|
if(!strUtil.isAnsi(message.message)) {
|
|
|
|
return callback(null, basicHeader, toUserNameBuf, fromUserNameBuf, subjectBuf, msgBody, message.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
ansiPrep(
|
|
|
|
message.message,
|
|
|
|
{
|
2018-06-23 03:26:46 +00:00
|
|
|
cols : 80,
|
|
|
|
rows : 'auto',
|
|
|
|
forceLineTerm : true,
|
|
|
|
exportMode : true,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
(err, preppedMsg) => {
|
|
|
|
return callback(null, basicHeader, toUserNameBuf, fromUserNameBuf, subjectBuf, msgBody, preppedMsg || message.message);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function addMessageBody(basicHeader, toUserNameBuf, fromUserNameBuf, subjectBuf, msgBody, preppedMsg, callback) {
|
|
|
|
msgBody += preppedMsg + '\r';
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// FTN-0004.001 @ http://ftsc.org/docs/fts-0004.001
|
|
|
|
// Tear line should be near the bottom of a message
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(message.meta.FtnProperty.ftn_tear_line) {
|
|
|
|
msgBody += `${message.meta.FtnProperty.ftn_tear_line}\r`;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Origin line should be near the bottom of a message
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(message.meta.FtnProperty.ftn_origin) {
|
|
|
|
msgBody += `${message.meta.FtnProperty.ftn_origin}\r`;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// FTN-0004.001 @ http://ftsc.org/docs/fts-0004.001
|
|
|
|
// SEEN-BY and PATH should be the last lines of a message
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
msgBody += getAppendMeta('SEEN-BY', message.meta.FtnProperty.ftn_seen_by); // note: no ^A (0x01)
|
2018-06-22 05:15:04 +00:00
|
|
|
msgBody += getAppendMeta('\x01PATH', message.meta.FtnKludge['PATH']);
|
|
|
|
|
|
|
|
let msgBodyEncoded;
|
|
|
|
try {
|
|
|
|
msgBodyEncoded = iconv.encode(msgBody + '\0', options.encoding);
|
|
|
|
} catch(e) {
|
|
|
|
msgBodyEncoded = iconv.encode(msgBody + '\0', 'ascii');
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
Buffer.concat( [
|
|
|
|
basicHeader,
|
|
|
|
toUserNameBuf,
|
|
|
|
fromUserNameBuf,
|
|
|
|
subjectBuf,
|
|
|
|
msgBodyEncoded
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
(err, msgEntryBuffer) => {
|
|
|
|
return cb(err, msgEntryBuffer);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.writeMessage = function(message, ws, options) {
|
|
|
|
const basicHeader = Buffer.alloc(34);
|
|
|
|
self.writeMessageHeader(message, basicHeader);
|
|
|
|
|
|
|
|
ws.write(basicHeader);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// toUserName & fromUserName: up to 36 bytes in length, NULL term'd
|
|
|
|
// :TODO: DRY...
|
2018-06-22 05:15:04 +00:00
|
|
|
let encBuf = iconv.encode(message.toUserName + '\0', 'CP437').slice(0, 36);
|
2018-06-23 03:26:46 +00:00
|
|
|
encBuf[encBuf.length - 1] = '\0'; // ensure it's null term'd
|
2018-06-22 05:15:04 +00:00
|
|
|
ws.write(encBuf);
|
|
|
|
|
|
|
|
encBuf = iconv.encode(message.fromUserName + '\0', 'CP437').slice(0, 36);
|
2018-06-23 03:26:46 +00:00
|
|
|
encBuf[encBuf.length - 1] = '\0'; // ensure it's null term'd
|
2018-06-22 05:15:04 +00:00
|
|
|
ws.write(encBuf);
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// subject: up to 72 bytes in length, NULL term'd
|
2018-06-22 05:15:04 +00:00
|
|
|
encBuf = iconv.encode(message.subject + '\0', 'CP437').slice(0, 72);
|
2018-06-23 03:26:46 +00:00
|
|
|
encBuf[encBuf.length - 1] = '\0'; // ensure it's null term'd
|
2018-06-22 05:15:04 +00:00
|
|
|
ws.write(encBuf);
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// message: unbound length, NULL term'd
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// We need to build in various special lines - kludges, area,
|
|
|
|
// seen-by, etc.
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Put this in it's own method
|
2018-06-22 05:15:04 +00:00
|
|
|
let msgBody = '';
|
|
|
|
|
|
|
|
function appendMeta(k, m, sepChar=':') {
|
|
|
|
if(m) {
|
|
|
|
let a = m;
|
|
|
|
if(!_.isArray(a)) {
|
|
|
|
a = [ a ];
|
|
|
|
}
|
|
|
|
a.forEach(v => {
|
|
|
|
msgBody += `${k}${sepChar} ${v}\r`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// FTN-0004.001 @ http://ftsc.org/docs/fts-0004.001
|
|
|
|
// AREA:CONFERENCE
|
|
|
|
// Should be first line in a message
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(message.meta.FtnProperty.ftn_area) {
|
2018-06-23 03:26:46 +00:00
|
|
|
msgBody += `AREA:${message.meta.FtnProperty.ftn_area}\r`; // note: no ^A (0x01)
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(message.meta.FtnKludge).forEach(k => {
|
|
|
|
switch(k) {
|
2018-06-23 03:26:46 +00:00
|
|
|
case 'PATH' : break; // skip & save for last
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
case 'Via' :
|
|
|
|
case 'FMPT' :
|
|
|
|
case 'TOPT' :
|
2018-06-23 03:26:46 +00:00
|
|
|
case 'INTL' : appendMeta(`\x01${k}`, message.meta.FtnKludge[k], ''); break; // no sepChar
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
default : appendMeta(`\x01${k}`, message.meta.FtnKludge[k]); break;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
msgBody += message.message + '\r';
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// FTN-0004.001 @ http://ftsc.org/docs/fts-0004.001
|
|
|
|
// Tear line should be near the bottom of a message
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(message.meta.FtnProperty.ftn_tear_line) {
|
|
|
|
msgBody += `${message.meta.FtnProperty.ftn_tear_line}\r`;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Origin line should be near the bottom of a message
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(message.meta.FtnProperty.ftn_origin) {
|
|
|
|
msgBody += `${message.meta.FtnProperty.ftn_origin}\r`;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// FTN-0004.001 @ http://ftsc.org/docs/fts-0004.001
|
|
|
|
// SEEN-BY and PATH should be the last lines of a message
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
appendMeta('SEEN-BY', message.meta.FtnProperty.ftn_seen_by); // note: no ^A (0x01)
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
appendMeta('\x01PATH', message.meta.FtnKludge['PATH']);
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: We should encode based on config and add the proper kludge here!
|
2018-06-22 05:15:04 +00:00
|
|
|
ws.write(iconv.encode(msgBody + '\0', options.encoding));
|
|
|
|
};
|
|
|
|
|
|
|
|
this.parsePacketBuffer = function(packetBuffer, iterator, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function processHeader(callback) {
|
|
|
|
self.parsePacketHeader(packetBuffer, (err, header) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const next = function(e) {
|
|
|
|
return callback(e, header);
|
|
|
|
};
|
|
|
|
|
|
|
|
iterator('header', header, next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function processMessages(header, callback) {
|
|
|
|
self.parsePacketMessages(
|
|
|
|
header,
|
|
|
|
packetBuffer.slice(FTN_PACKET_HEADER_SIZE),
|
|
|
|
iterator,
|
|
|
|
callback);
|
|
|
|
}
|
|
|
|
],
|
2018-06-23 03:26:46 +00:00
|
|
|
cb // complete
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
|
|
|
};
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 00:56:05 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Message attributes defined in FTS-0001.016
|
|
|
|
// http://ftsc.org/docs/fts-0001.016
|
2016-02-16 00:56:05 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// See also:
|
|
|
|
// * http://www.skepticfiles.org/aj/basics03.htm
|
2016-03-10 05:32:00 +00:00
|
|
|
//
|
2016-02-16 00:56:05 +00:00
|
|
|
Packet.Attribute = {
|
2018-06-23 03:26:46 +00:00
|
|
|
Private : 0x0001, // Private message / NetMail
|
|
|
|
Crash : 0x0002,
|
|
|
|
Received : 0x0004,
|
|
|
|
Sent : 0x0008,
|
|
|
|
FileAttached : 0x0010,
|
|
|
|
InTransit : 0x0020,
|
|
|
|
Orphan : 0x0040,
|
|
|
|
KillSent : 0x0080,
|
|
|
|
Local : 0x0100, // Message is from *this* system
|
|
|
|
Hold : 0x0200,
|
|
|
|
Reserved0 : 0x0400,
|
|
|
|
FileRequest : 0x0800,
|
|
|
|
ReturnReceiptRequest : 0x1000,
|
|
|
|
ReturnReceipt : 0x2000,
|
|
|
|
AuditRequest : 0x4000,
|
|
|
|
FileUpdateRequest : 0x8000,
|
2016-02-16 00:56:05 +00:00
|
|
|
};
|
|
|
|
Object.freeze(Packet.Attribute);
|
|
|
|
|
|
|
|
Packet.prototype.read = function(pathOrBuffer, iterator, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function getBufferIfPath(callback) {
|
|
|
|
if(_.isString(pathOrBuffer)) {
|
|
|
|
fs.readFile(pathOrBuffer, (err, data) => {
|
|
|
|
pathOrBuffer = data;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function parseBuffer(callback) {
|
|
|
|
self.parsePacketBuffer(pathOrBuffer, iterator, err => {
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
2016-02-03 04:35:59 +00:00
|
|
|
};
|
|
|
|
|
2016-02-29 05:04:03 +00:00
|
|
|
Packet.prototype.writeHeader = function(ws, packetHeader) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return this.writePacketHeader(packetHeader, ws);
|
2016-02-29 05:04:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Packet.prototype.writeMessageEntry = function(ws, msgEntry) {
|
2018-06-22 05:15:04 +00:00
|
|
|
ws.write(msgEntry);
|
|
|
|
return msgEntry.length;
|
2016-02-29 05:04:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Packet.prototype.writeTerminator = function(ws) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// From FTS-0001.016:
|
|
|
|
// "A pseudo-message beginning with the word 0000H signifies the end of the packet."
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
ws.write(Buffer.from( [ 0x00, 0x00 ] )); // final extra null term
|
2018-06-22 05:15:04 +00:00
|
|
|
return 2;
|
2016-02-29 05:04:03 +00:00
|
|
|
};
|
|
|
|
|
2016-02-16 00:56:05 +00:00
|
|
|
Packet.prototype.writeStream = function(ws, messages, options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!_.isBoolean(options.terminatePacket)) {
|
|
|
|
options.terminatePacket = true;
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(_.isObject(options.packetHeader)) {
|
|
|
|
this.writePacketHeader(options.packetHeader, ws);
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
options.encoding = options.encoding || 'utf8';
|
2016-02-03 04:35:59 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
messages.forEach(msg => {
|
|
|
|
this.writeMessage(msg, ws, options);
|
|
|
|
});
|
2016-02-16 00:56:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
if(true === options.terminatePacket) {
|
2018-06-23 03:26:46 +00:00
|
|
|
ws.write(Buffer.from( [ 0 ] )); // final extra null term
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2016-03-10 05:32:00 +00:00
|
|
|
};
|
2016-02-16 00:56:05 +00:00
|
|
|
|
2016-02-21 00:57:38 +00:00
|
|
|
Packet.prototype.write = function(path, packetHeader, messages, options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
if(!_.isArray(messages)) {
|
|
|
|
messages = [ messages ];
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
options = options || { encoding : 'utf8' }; // utf-8 = 'CHRS UTF-8 4'
|
2016-02-16 00:56:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
this.writeStream(
|
2018-06-23 03:26:46 +00:00
|
|
|
fs.createWriteStream(path), // :TODO: specify mode/etc.
|
2018-06-22 05:15:04 +00:00
|
|
|
messages,
|
|
|
|
Object.assign( { packetHeader : packetHeader, terminatePacket : true }, options)
|
|
|
|
);
|
2016-02-16 00:56:05 +00:00
|
|
|
};
|