2015-07-14 06:13:29 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2022-06-05 20:04:25 +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;
|
2018-06-23 03:26:46 +00:00
|
|
|
|
|
|
|
// 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 = {
|
2022-06-05 20:04:25 +00:00
|
|
|
node: 0,
|
|
|
|
net: 0,
|
|
|
|
zone: 0,
|
|
|
|
point: 0,
|
2018-06-22 05:15:04 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +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
|
2022-06-05 20:04:25 +00:00
|
|
|
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"
|
|
|
|
|
|
|
|
this.capWord = 0x0001;
|
|
|
|
this.capWordValidate =
|
|
|
|
((this.capWord & 0xff) << 8) | ((this.capWord >> 8) & 0xff); // swap
|
|
|
|
|
|
|
|
this.prodCodeHi = 0xfe; // see above
|
|
|
|
this.prodRevHi = 0;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get origAddress() {
|
|
|
|
let addr = new Address({
|
2022-06-05 20:04:25 +00:00
|
|
|
node: this.origNode,
|
|
|
|
zone: this.origZone,
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.origPoint) {
|
|
|
|
addr.point = this.origPoint;
|
|
|
|
addr.net = this.auxNet;
|
2018-06-22 05:15:04 +00:00
|
|
|
} else {
|
2022-06-05 20:04:25 +00:00
|
|
|
addr.net = this.origNet;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
set origAddress(address) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(address)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
*/
|
2022-06-05 20:04:25 +00:00
|
|
|
this.origNet = address.net;
|
|
|
|
this.auxNet = 0;
|
2018-06-23 03:26:46 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
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({
|
2022-06-05 20:04:25 +00:00
|
|
|
node: this.destNode,
|
|
|
|
net: this.destNet,
|
|
|
|
zone: this.destZone,
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (this.destPoint) {
|
2018-06-22 05:15:04 +00:00
|
|
|
addr.point = this.destPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
set destAddress(address) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(address)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
address = Address.fromString(address);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +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({
|
2022-06-05 20:04:25 +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) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!moment.isMoment(momentCreated)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
momentCreated = moment(momentCreated);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +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();
|
2018-06-23 03:26:46 +00:00
|
|
|
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')
|
2022-06-05 20:04:25 +00:00
|
|
|
.int8('prodRevLo') // aka serialNo
|
|
|
|
.buffer('password', { length: 8 }) // can't use string; need CP437 - see https://github.com/keichi/binary-parser/issues/33
|
2019-12-06 03:48:13 +00:00
|
|
|
.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', {
|
2022-06-05 20:04:25 +00:00
|
|
|
type: 'uint8',
|
|
|
|
length: 20, // FTS-0001.016: 20 bytes
|
2019-12-06 03:48:13 +00:00
|
|
|
})
|
|
|
|
.array('toUserName', {
|
2022-06-05 20:04:25 +00:00
|
|
|
type: 'uint8',
|
2019-12-06 03:48:13 +00:00
|
|
|
// :TODO: array needs some soft of 'limit' field
|
2022-06-05 20:04:25 +00:00
|
|
|
readUntil: b => 0x00 === b,
|
2019-12-06 03:48:13 +00:00
|
|
|
})
|
|
|
|
.array('fromUserName', {
|
2022-06-05 20:04:25 +00:00
|
|
|
type: 'uint8',
|
|
|
|
readUntil: b => 0x00 === b,
|
2019-12-06 03:48:13 +00:00
|
|
|
})
|
|
|
|
.array('subject', {
|
2022-06-05 20:04:25 +00:00
|
|
|
type: 'uint8',
|
|
|
|
readUntil: b => 0x00 === b,
|
2019-12-06 03:48:13 +00:00
|
|
|
})
|
|
|
|
.array('message', {
|
2022-06-05 20:04:25 +00:00
|
|
|
type: 'uint8',
|
|
|
|
readUntil: b => 0x00 === b,
|
2019-12-06 03:48:13 +00:00
|
|
|
});
|
|
|
|
|
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 || {};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.parsePacketHeader = function (packetBuffer, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
assert(Buffer.isBuffer(packetBuffer));
|
|
|
|
|
|
|
|
let packetHeader;
|
|
|
|
try {
|
2019-12-06 03:48:13 +00:00
|
|
|
packetHeader = PacketHeaderParser.parse(packetBuffer);
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
2022-06-05 20:04:25 +00:00
|
|
|
packetHeader.password = strUtil.stringFromNullTermBuffer(
|
|
|
|
packetHeader.password,
|
|
|
|
'CP437'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (FTN_PACKET_HEADER_TYPE !== packetHeader.packetType) {
|
|
|
|
return cb(
|
|
|
|
Errors.Invalid(
|
|
|
|
`Unsupported FTN packet header type: ${packetHeader.packetType}`
|
|
|
|
)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
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
|
2022-06-05 20:04:25 +00:00
|
|
|
if (FTN_PACKET_BAUD_TYPE_2_2 === packetHeader.baud) {
|
2018-06-22 05:15:04 +00:00
|
|
|
packetHeader.version = '2.2';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// See FSC-0045
|
2022-06-05 20:04:25 +00:00
|
|
|
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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (
|
|
|
|
capWordValidateSwapped === packetHeader.capWord &&
|
2018-06-23 03:26:46 +00:00
|
|
|
0 != packetHeader.capWord &&
|
2022-06-05 20:04:25 +00:00
|
|
|
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
|
2022-06-05 20:04:25 +00:00
|
|
|
if (-1 === packetHeader.origNet) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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({
|
2022-06-05 20:04:25 +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);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getPacketHeaderBuffer = function (packetHeader) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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);
|
2022-06-05 20:04:25 +00:00
|
|
|
buffer.writeUInt16LE(
|
|
|
|
-1 === packetHeader.origNet ? 0xffff : packetHeader.origNet,
|
|
|
|
20
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.writePacketHeader = function (packetHeader, ws) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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);
|
2022-06-05 20:04:25 +00:00
|
|
|
buffer.writeUInt16LE(
|
|
|
|
-1 === packetHeader.origNet ? 0xffff : packetHeader.origNet,
|
|
|
|
20
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.processMessageBody = function (messageBodyBuffer, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
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 = {
|
2022-06-05 20:04:25 +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;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (['INTL', 'TOPT', 'FMPT', 'Via'].includes(key)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
value = line.substr(key.length).trim();
|
|
|
|
} else {
|
|
|
|
const sepIndex = line.indexOf(':');
|
2022-06-05 20:04:25 +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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (messageBodyData.kludgeLines[key]) {
|
|
|
|
if (!_.isArray(messageBodyData.kludgeLines[key])) {
|
|
|
|
messageBodyData.kludgeLines[key] = [messageBodyData.kludgeLines[key]];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
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*
|
2022-06-05 20:04:25 +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) {
|
|
|
|
// 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;
|
|
|
|
} else {
|
|
|
|
Log.warn(
|
|
|
|
{ error: err.message },
|
|
|
|
'Found what looks like to be a SAUCE record, but failed to read'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return callback(null); // failure to read SAUCE is OK
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
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
|
2022-06-05 20:04:25 +00:00
|
|
|
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);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (chrsPrefixIndex < 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
chrsPrefixIndex += FTN_CHRS_PREFIX.length;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const chrsEndIndex = messageBodyBuffer.indexOf(
|
|
|
|
FTN_CHRS_SUFFIX,
|
|
|
|
chrsPrefixIndex
|
|
|
|
);
|
|
|
|
if (chrsEndIndex < 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
let chrsContent = messageBodyBuffer.slice(
|
|
|
|
chrsPrefixIndex,
|
|
|
|
chrsEndIndex
|
|
|
|
);
|
|
|
|
if (0 === chrsContent.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
chrsContent = iconv.decode(chrsContent, 'CP437');
|
2022-06-05 20:04:25 +00:00
|
|
|
const chrsEncoding =
|
|
|
|
ftn.getEncodingFromCharacterSetIdentifier(chrsContent);
|
|
|
|
if (chrsEncoding) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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);
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
|
|
|
Log.debug(
|
|
|
|
{ encoding: encoding, error: e.toString() },
|
|
|
|
'Error decoding. Falling back to ASCII'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
decoded = iconv.decode(messageBodyBuffer, 'ascii');
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const messageLines = strUtil.splitTextAtTerms(
|
|
|
|
decoded.replace(/\xec/g, '')
|
|
|
|
);
|
|
|
|
let endOfMessage = false;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
|
|
|
messageLines.forEach(line => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === line.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
messageBodyData.message.push('');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
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;
|
2022-06-05 20:04:25 +00:00
|
|
|
} else if (/^[ ]{1,2}\* Origin: /.test(line)) {
|
|
|
|
// To spec is " * Origin: ..."
|
2018-06-22 05:15:04 +00:00
|
|
|
messageBodyData.originLine = line;
|
2022-06-05 20:04:25 +00:00
|
|
|
endOfMessage = true; // Anything past origin is not part of the message body
|
|
|
|
} else if (line.startsWith('SEEN-BY:')) {
|
|
|
|
endOfMessage = true; // Anything past the first SEEN-BY is not part of the message body
|
|
|
|
messageBodyData.seenBy.push(
|
|
|
|
line.substring(line.indexOf(':') + 1).trim()
|
|
|
|
);
|
|
|
|
} else if (FTN_MESSAGE_KLUDGE_PREFIX === line.charAt(0)) {
|
|
|
|
if ('PATH:' === line.slice(1, 6)) {
|
|
|
|
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));
|
2022-06-05 20:04:25 +00:00
|
|
|
} 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);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
() => {
|
|
|
|
messageBodyData.message = messageBodyData.message.join('\n');
|
|
|
|
return cb(messageBodyData);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.parsePacketMessages = function (header, packetBuffer, iterator, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (packetBuffer.length < 3) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const peek = packetBuffer.slice(0, 2);
|
2022-06-05 20:04:25 +00:00
|
|
|
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);
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return cb(Errors.Invalid(`Failed to parse FTN message header: ${e.message}`));
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (FTN_PACKET_MESSAGE_TYPE != msgData.messageType) {
|
|
|
|
return cb(
|
|
|
|
Errors.Invalid(`Unsupported FTN message type: ${msgData.messageType}`)
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
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.
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (msgData.modDateTime.length != 20) {
|
|
|
|
return cb(
|
|
|
|
Errors.Invalid(
|
|
|
|
`FTN packet DateTime field must be 20 bytes (got ${msgData.modDateTime.length})`
|
|
|
|
)
|
|
|
|
);
|
2019-11-23 04:44:24 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (msgData.toUserName.length > 36) {
|
|
|
|
return cb(
|
|
|
|
Errors.Invalid(
|
|
|
|
`FTN packet toUserName field must be 36 bytes max (got ${msgData.toUserName.length})`
|
|
|
|
)
|
|
|
|
);
|
2019-11-23 04:44:24 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (msgData.fromUserName.length > 36) {
|
|
|
|
return cb(
|
|
|
|
Errors.Invalid(
|
|
|
|
`FTN packet fromUserName field must be 36 bytes max (got ${msgData.fromUserName.length})`
|
|
|
|
)
|
|
|
|
);
|
2019-11-23 04:44:24 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
if (msgData.subject.length > 72) {
|
|
|
|
return cb(
|
|
|
|
Errors.Invalid(
|
|
|
|
`FTN packet subject field must be 72 bytes max (got ${msgData.subject.length})`
|
|
|
|
)
|
|
|
|
);
|
2019-11-23 04:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Arrays of CP437 bytes -> String
|
2022-06-05 20:04:25 +00:00
|
|
|
['modDateTime', 'toUserName', 'fromUserName', 'subject'].forEach(k => {
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
const msg = new Message({
|
|
|
|
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 = {
|
2022-06-05 20:04:25 +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 => {
|
2022-06-05 20:04:25 +00:00
|
|
|
msg.message = messageBodyData.message;
|
|
|
|
msg.meta.FtnKludge = messageBodyData.kludgeLines;
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (messageBodyData.tearLine) {
|
2018-06-22 05:15:04 +00:00
|
|
|
msg.meta.FtnProperty.ftn_tear_line = messageBodyData.tearLine;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.options.keepTearAndOrigin) {
|
2018-06-22 05:15:04 +00:00
|
|
|
msg.message += `\r\n${messageBodyData.tearLine}\r\n`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (messageBodyData.seenBy.length > 0) {
|
2018-06-22 05:15:04 +00:00
|
|
|
msg.meta.FtnProperty.ftn_seen_by = messageBodyData.seenBy;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (messageBodyData.area) {
|
2018-06-22 05:15:04 +00:00
|
|
|
msg.meta.FtnProperty.ftn_area = messageBodyData.area;
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (messageBodyData.originLine) {
|
2018-06-22 05:15:04 +00:00
|
|
|
msg.meta.FtnProperty.ftn_origin = messageBodyData.originLine;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (self.options.keepTearAndOrigin) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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.
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
const [, sign, hours, minutes] = tzMatch;
|
2020-11-18 02:06:54 +00:00
|
|
|
|
|
|
|
// convert to a [+|-]hh:mm format.
|
|
|
|
// example: 1300 -> +13:00
|
2022-06-05 20:04:25 +00:00
|
|
|
const utcOffset = `${sign || '+'}${hours}:${minutes}`;
|
2020-11-18 02:06:54 +00:00
|
|
|
|
|
|
|
// 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 =
|
2022-06-05 20:04:25 +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);
|
2022-06-05 20:04:25 +00:00
|
|
|
if (nextBuf.length > 0) {
|
|
|
|
const next = function (e) {
|
|
|
|
if (e) {
|
2018-06-22 05:15:04 +00:00
|
|
|
cb(e);
|
|
|
|
} else {
|
|
|
|
self.parsePacketMessages(header, nextBuf, iterator, cb);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
iterator('message', msg, next);
|
|
|
|
} else {
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.sanatizeFtnProperties = function (message) {
|
2018-06-22 05:15:04 +00:00
|
|
|
[
|
|
|
|
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,
|
2022-06-05 20:04:25 +00:00
|
|
|
].forEach(propName => {
|
|
|
|
if (message.meta.FtnProperty[propName]) {
|
|
|
|
message.meta.FtnProperty[propName] =
|
|
|
|
parseInt(message.meta.FtnProperty[propName]) || 0;
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
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);
|
|
|
|
|
2022-06-05 20:04:25 +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);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const dateTimeBuffer = Buffer.from(
|
|
|
|
ftn.getDateTimeString(message.modTimestamp) + '\0'
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
dateTimeBuffer.copy(buf, 14);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.getMessageEntryBuffer = function (message, options, cb) {
|
|
|
|
function getAppendMeta(k, m, sepChar = ':') {
|
2018-06-22 05:15:04 +00:00
|
|
|
let append = '';
|
2022-06-05 20:04:25 +00:00
|
|
|
if (m) {
|
2018-06-22 05:15:04 +00:00
|
|
|
let a = m;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isArray(a)) {
|
|
|
|
a = [a];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (message.meta.FtnProperty.ftn_area) {
|
|
|
|
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 => {
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (k) {
|
|
|
|
case 'PATH':
|
|
|
|
break; // skip & save for last
|
|
|
|
|
|
|
|
case 'Via':
|
|
|
|
case 'FMPT':
|
|
|
|
case 'TOPT':
|
|
|
|
case 'INTL':
|
|
|
|
msgBody += getAppendMeta(
|
|
|
|
`\x01${k}`,
|
|
|
|
message.meta.FtnKludge[k],
|
|
|
|
''
|
|
|
|
); // no sepChar
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
default:
|
|
|
|
msgBody += getAppendMeta(
|
|
|
|
`\x01${k}`,
|
|
|
|
message.meta.FtnKludge[k]
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
basicHeader,
|
|
|
|
toUserNameBuf,
|
|
|
|
fromUserNameBuf,
|
|
|
|
subjectBuf,
|
|
|
|
msgBody
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
function prepareAnsiMessageBody(
|
|
|
|
basicHeader,
|
|
|
|
toUserNameBuf,
|
|
|
|
fromUserNameBuf,
|
|
|
|
subjectBuf,
|
|
|
|
msgBody,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
if (!strUtil.isAnsi(message.message)) {
|
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
basicHeader,
|
|
|
|
toUserNameBuf,
|
|
|
|
fromUserNameBuf,
|
|
|
|
subjectBuf,
|
|
|
|
msgBody,
|
|
|
|
message.message
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ansiPrep(
|
|
|
|
message.message,
|
|
|
|
{
|
2022-06-05 20:04:25 +00:00
|
|
|
cols: 80,
|
|
|
|
rows: 'auto',
|
|
|
|
forceLineTerm: true,
|
|
|
|
exportMode: true,
|
2018-06-22 05:15:04 +00:00
|
|
|
},
|
|
|
|
(err, preppedMsg) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
basicHeader,
|
|
|
|
toUserNameBuf,
|
|
|
|
fromUserNameBuf,
|
|
|
|
subjectBuf,
|
|
|
|
msgBody,
|
|
|
|
preppedMsg || message.message
|
|
|
|
);
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
function addMessageBody(
|
|
|
|
basicHeader,
|
|
|
|
toUserNameBuf,
|
|
|
|
fromUserNameBuf,
|
|
|
|
subjectBuf,
|
|
|
|
msgBody,
|
|
|
|
preppedMsg,
|
|
|
|
callback
|
|
|
|
) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (message.meta.FtnProperty.ftn_tear_line) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (message.meta.FtnProperty.ftn_origin) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +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);
|
2022-06-05 20:04:25 +00:00
|
|
|
} catch (e) {
|
2018-06-22 05:15:04 +00:00
|
|
|
msgBodyEncoded = iconv.encode(msgBody + '\0', 'ascii');
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(
|
|
|
|
null,
|
2022-06-05 20:04:25 +00:00
|
|
|
Buffer.concat([
|
2018-06-22 05:15:04 +00:00
|
|
|
basicHeader,
|
|
|
|
toUserNameBuf,
|
|
|
|
fromUserNameBuf,
|
|
|
|
subjectBuf,
|
2022-06-05 20:04:25 +00:00
|
|
|
msgBodyEncoded,
|
2018-06-22 05:15:04 +00:00
|
|
|
])
|
|
|
|
);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
(err, msgEntryBuffer) => {
|
|
|
|
return cb(err, msgEntryBuffer);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.writeMessage = function (message, ws, options) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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);
|
2022-06-05 20:04:25 +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);
|
2022-06-05 20:04:25 +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);
|
2022-06-05 20:04:25 +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 = '';
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
function appendMeta(k, m, sepChar = ':') {
|
|
|
|
if (m) {
|
2018-06-22 05:15:04 +00:00
|
|
|
let a = m;
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!_.isArray(a)) {
|
|
|
|
a = [a];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (message.meta.FtnProperty.ftn_area) {
|
|
|
|
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 => {
|
2022-06-05 20:04:25 +00:00
|
|
|
switch (k) {
|
|
|
|
case 'PATH':
|
|
|
|
break; // skip & save for last
|
|
|
|
|
|
|
|
case 'Via':
|
|
|
|
case 'FMPT':
|
|
|
|
case 'TOPT':
|
|
|
|
case 'INTL':
|
|
|
|
appendMeta(`\x01${k}`, message.meta.FtnKludge[k], '');
|
|
|
|
break; // no sepChar
|
|
|
|
|
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (message.meta.FtnProperty.ftn_tear_line) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (message.meta.FtnProperty.ftn_origin) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
|
|
//
|
2022-06-05 20:04:25 +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));
|
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
this.parsePacketBuffer = function (packetBuffer, iterator, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function processHeader(callback) {
|
|
|
|
self.parsePacketHeader(packetBuffer, (err, header) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
const next = function (e) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(e, header);
|
|
|
|
};
|
|
|
|
|
|
|
|
iterator('header', header, next);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function processMessages(header, callback) {
|
|
|
|
self.parsePacketMessages(
|
|
|
|
header,
|
|
|
|
packetBuffer.slice(FTN_PACKET_HEADER_SIZE),
|
|
|
|
iterator,
|
2022-06-05 20:04:25 +00:00
|
|
|
callback
|
|
|
|
);
|
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
2022-06-05 20:04:25 +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 = {
|
2022-06-05 20:04:25 +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);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Packet.prototype.read = function (pathOrBuffer, iterator, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function getBufferIfPath(callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isString(pathOrBuffer)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
fs.readFile(pathOrBuffer, (err, data) => {
|
|
|
|
pathOrBuffer = data;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function parseBuffer(callback) {
|
|
|
|
self.parsePacketBuffer(pathOrBuffer, iterator, err => {
|
|
|
|
callback(err);
|
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
2016-02-03 04:35:59 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +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
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +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
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +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
|
|
|
//
|
2022-06-05 20:04:25 +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
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Packet.prototype.writeStream = function (ws, messages, options) {
|
|
|
|
if (!_.isBoolean(options.terminatePacket)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
options.terminatePacket = true;
|
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (_.isObject(options.packetHeader)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (true === options.terminatePacket) {
|
|
|
|
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
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
Packet.prototype.write = function (path, packetHeader, messages, options) {
|
|
|
|
if (!_.isArray(messages)) {
|
|
|
|
messages = [messages];
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2022-06-05 20:04:25 +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,
|
2022-06-05 20:04:25 +00:00
|
|
|
Object.assign({ packetHeader: packetHeader, terminatePacket: true }, options)
|
2018-06-22 05:15:04 +00:00
|
|
|
);
|
2016-02-16 00:56:05 +00:00
|
|
|
};
|