2015-07-14 06:13:29 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const Config = require('./config.js').get;
|
|
|
|
const Address = require('./ftn_address.js');
|
|
|
|
const FNV1a = require('./fnv1a.js');
|
|
|
|
const getCleanEnigmaVersion = require('./misc_util.js').getCleanEnigmaVersion;
|
2017-07-23 19:03:47 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const _ = require('lodash');
|
|
|
|
const iconv = require('iconv-lite');
|
|
|
|
const moment = require('moment');
|
|
|
|
const os = require('os');
|
2016-02-21 00:57:38 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
const packageJson = require('../package.json');
|
2015-07-14 06:13:29 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Remove "Ftn" from most of these -- it's implied in the module
|
|
|
|
exports.stringToNullPaddedBuffer = stringToNullPaddedBuffer;
|
|
|
|
exports.getMessageSerialNumber = getMessageSerialNumber;
|
|
|
|
exports.getDateFromFtnDateTime = getDateFromFtnDateTime;
|
|
|
|
exports.getDateTimeString = getDateTimeString;
|
2015-07-14 06:13:29 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.getMessageIdentifier = getMessageIdentifier;
|
|
|
|
exports.getProductIdentifier = getProductIdentifier;
|
|
|
|
exports.getUTCTimeZoneOffset = getUTCTimeZoneOffset;
|
|
|
|
exports.getOrigin = getOrigin;
|
|
|
|
exports.getTearLine = getTearLine;
|
|
|
|
exports.getVia = getVia;
|
|
|
|
exports.getIntl = getIntl;
|
|
|
|
exports.getAbbreviatedNetNodeList = getAbbreviatedNetNodeList;
|
|
|
|
exports.parseAbbreviatedNetNodeList = parseAbbreviatedNetNodeList;
|
|
|
|
exports.getUpdatedSeenByEntries = getUpdatedSeenByEntries;
|
|
|
|
exports.getUpdatedPathEntries = getUpdatedPathEntries;
|
2016-02-10 05:30:59 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.getCharacterSetIdentifierByEncoding = getCharacterSetIdentifierByEncoding;
|
|
|
|
exports.getEncodingFromCharacterSetIdentifier = getEncodingFromCharacterSetIdentifier;
|
2016-02-21 00:57:38 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
exports.getQuotePrefix = getQuotePrefix;
|
2015-09-21 01:10:09 +00:00
|
|
|
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Namespace for RFC-4122 name based UUIDs generated from
|
|
|
|
// FTN kludges MSGID + AREA
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
//const ENIGMA_FTN_MSGID_NAMESPACE = uuid.parse('a5c7ae11-420c-4469-a116-0e9a6d8d2654');
|
2016-02-10 05:30:59 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// See list here: https://github.com/Mithgol/node-fidonet-jam
|
2015-07-14 23:08:52 +00:00
|
|
|
|
2018-01-15 19:22:11 +00:00
|
|
|
function stringToNullPaddedBuffer(s, bufLen) {
|
2018-06-23 03:26:46 +00:00
|
|
|
let buffer = Buffer.alloc(bufLen);
|
|
|
|
let enc = iconv.encode(s, 'CP437').slice(0, bufLen);
|
2018-06-22 05:15:04 +00:00
|
|
|
for(let i = 0; i < enc.length; ++i) {
|
|
|
|
buffer[i] = enc[i];
|
|
|
|
}
|
|
|
|
return buffer;
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
2015-07-16 05:51:00 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Convert a FTN style DateTime string to a Date object
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2020-10-16 02:54:35 +00:00
|
|
|
// :TODO: Name the next couple methods better - for FTN *packets* e.g. parsePacketDateTime()
|
2015-07-16 05:51:00 +00:00
|
|
|
function getDateFromFtnDateTime(dateTime) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Examples seen in the wild (Working):
|
|
|
|
// "12 Sep 88 18:17:59"
|
|
|
|
// "Tue 01 Jan 80 00:00"
|
|
|
|
// "27 Feb 15 00:00:03"
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: Use moment.js here
|
|
|
|
return moment(Date.parse(dateTime)); // Date.parse() allows funky formats
|
2015-07-16 05:51:00 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
function getDateTimeString(m) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// From http://ftsc.org/docs/fts-0001.016:
|
|
|
|
// DateTime = (* a character string 20 characters long *)
|
|
|
|
// (* 01 Jan 86 02:34:56 *)
|
|
|
|
// DayOfMonth " " Month " " Year " "
|
|
|
|
// " " HH ":" MM ":" SS
|
|
|
|
// Null
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// DayOfMonth = "01" | "02" | "03" | ... | "31" (* Fido 0 fills *)
|
|
|
|
// Month = "Jan" | "Feb" | "Mar" | "Apr" | "May" | "Jun" |
|
|
|
|
// "Jul" | "Aug" | "Sep" | "Oct" | "Nov" | "Dec"
|
|
|
|
// Year = "01" | "02" | .. | "85" | "86" | ... | "99" | "00"
|
|
|
|
// HH = "00" | .. | "23"
|
|
|
|
// MM = "00" | .. | "59"
|
|
|
|
// SS = "00" | .. | "59"
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
if(!moment.isMoment(m)) {
|
|
|
|
m = moment(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.format('DD MMM YY HH:mm:ss');
|
2016-02-03 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
2016-02-29 05:04:03 +00:00
|
|
|
function getMessageSerialNumber(messageId) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const msSinceEnigmaEpoc = (Date.now() - Date.UTC(2016, 1, 1));
|
2018-06-23 03:26:46 +00:00
|
|
|
const hash = Math.abs(new FNV1a(msSinceEnigmaEpoc + messageId).value).toString(16);
|
2018-06-22 05:15:04 +00:00
|
|
|
return `00000000${hash}`.substr(-8);
|
2015-07-14 06:13:29 +00:00
|
|
|
}
|
|
|
|
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Return a FTS-0009.001 compliant MSGID value given a message
|
|
|
|
// See http://ftsc.org/docs/fts-0009.001
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// "A MSGID line consists of the string "^AMSGID:" (where ^A is a
|
|
|
|
// control-A (hex 01) and the double-quotes are not part of the
|
|
|
|
// string), followed by a space, the address of the originating
|
|
|
|
// system, and a serial number unique to that message on the
|
|
|
|
// originating system, i.e.:
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// ^AMSGID: origaddr serialno
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// The originating address should be specified in a form that
|
|
|
|
// constitutes a valid return address for the originating network.
|
|
|
|
// If the originating address is enclosed in double-quotes, the
|
|
|
|
// entire string between the beginning and ending double-quotes is
|
|
|
|
// considered to be the orginating address. A double-quote character
|
|
|
|
// within a quoted address is represented by by two consecutive
|
|
|
|
// double-quote characters. The serial number may be any eight
|
|
|
|
// character hexadecimal number, as long as it is unique - no two
|
|
|
|
// messages from a given system may have the same serial number
|
|
|
|
// within a three years. The manner in which this serial number is
|
|
|
|
// generated is left to the implementor."
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Examples & Implementations
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Synchronet: <msgNum>.<conf+area>@<ftnAddr> <serial>
|
|
|
|
// 2606.agora-agn_tst@46:1/142 19609217
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Mystic: <ftnAddress> <serial>
|
|
|
|
// 46:3/102 46686263
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// ENiGMA½: <messageId>.<areaTag>@<5dFtnAddress> <serial>
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// 0.0.8-alpha:
|
|
|
|
// Made compliant with FTN spec *when exporting NetMail* due to
|
|
|
|
// Mystic rejecting messages with the true-unique version.
|
|
|
|
// Strangely, Synchronet uses the unique format and Mystic does
|
|
|
|
// OK with it. Will need to research further. Note also that
|
|
|
|
// g00r00 was kind enough to fix Mystic to allow for the Sync/Enig
|
|
|
|
// format, but that will only help when using newer Mystic versions.
|
2018-01-10 02:38:36 +00:00
|
|
|
//
|
|
|
|
function getMessageIdentifier(message, address, isNetMail = false) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const addrStr = new Address(address).toString('5D');
|
|
|
|
return isNetMail ?
|
|
|
|
`${addrStr} ${getMessageSerialNumber(message.messageId)}` :
|
|
|
|
`${message.messageId}.${message.areaTag.toLowerCase()}@${addrStr} ${getMessageSerialNumber(message.messageId)}`
|
|
|
|
;
|
2016-02-10 05:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Return a FSC-0046.005 Product Identifier or "PID"
|
|
|
|
// http://ftsc.org/docs/fsc-0046.005
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Note that we use a variant on the spec for <serial>
|
|
|
|
// in which (<os>; <arch>; <nodeVer>) is used instead
|
2016-02-17 05:11:55 +00:00
|
|
|
//
|
2016-02-10 05:30:59 +00:00
|
|
|
function getProductIdentifier() {
|
2018-06-22 05:15:04 +00:00
|
|
|
const version = getCleanEnigmaVersion();
|
2018-06-23 03:26:46 +00:00
|
|
|
const nodeVer = process.version.substr(1); // remove 'v' prefix
|
2016-02-10 05:30:59 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return `ENiGMA1/2 ${version} (${os.platform()}; ${os.arch()}; ${nodeVer})`;
|
2016-02-10 05:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Return a FRL-1004 style time zone offset for a
|
|
|
|
// 'TZUTC' kludge line
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// http://ftsc.org/docs/frl-1004.002
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
|
|
|
function getUTCTimeZoneOffset() {
|
2018-06-22 05:15:04 +00:00
|
|
|
return moment().format('ZZ').replace(/\+/, '');
|
2015-07-14 06:13:29 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 17:17:29 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Get a FSC-0032 style quote prefix
|
|
|
|
// http://ftsc.org/docs/fsc-0032.001
|
2018-01-15 19:22:11 +00:00
|
|
|
//
|
2015-09-21 01:10:09 +00:00
|
|
|
function getQuotePrefix(name) {
|
2018-06-22 05:15:04 +00:00
|
|
|
let initials;
|
|
|
|
|
|
|
|
const parts = name.split(' ');
|
|
|
|
if(parts.length > 1) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// First & Last initials - (Bryan Ashby -> BA)
|
2018-06-22 05:15:04 +00:00
|
|
|
initials = `${parts[0].slice(0, 1)}${parts[parts.length - 1].slice(0, 1)}`.toUpperCase();
|
|
|
|
} else {
|
2018-06-23 03:26:46 +00:00
|
|
|
// Just use the first two - (NuSkooler -> Nu)
|
2018-06-22 05:15:04 +00:00
|
|
|
initials = _.capitalize(name.slice(0, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
return ` ${initials}> `;
|
2015-09-21 01:10:09 +00:00
|
|
|
}
|
|
|
|
|
2015-07-14 23:08:52 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Return a FTS-0004 Origin line
|
|
|
|
// http://ftsc.org/docs/fts-0004.001
|
2016-02-10 05:30:59 +00:00
|
|
|
//
|
|
|
|
function getOrigin(address) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const config = Config();
|
|
|
|
const origin = _.has(config, 'messageNetworks.originLine') ?
|
|
|
|
config.messageNetworks.originLine :
|
|
|
|
config.general.boardName;
|
2015-07-14 23:08:52 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const addrStr = new Address(address).toString('5D');
|
|
|
|
return ` * Origin: ${origin} (${addrStr})`;
|
2016-02-16 00:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getTearLine() {
|
2018-06-23 03:26:46 +00:00
|
|
|
const nodeVer = process.version.substr(1); // remove 'v' prefix
|
2018-06-22 05:15:04 +00:00
|
|
|
return `--- ENiGMA 1/2 v${packageJson.version} (${os.platform()}; ${os.arch()}; ${nodeVer})`;
|
2016-02-16 00:56:05 +00:00
|
|
|
}
|
|
|
|
|
2016-02-17 05:11:55 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Return a FRL-1005.001 "Via" line
|
|
|
|
// http://ftsc.org/docs/frl-1005.001
|
2016-02-17 05:11:55 +00:00
|
|
|
//
|
|
|
|
function getVia(address) {
|
2018-06-22 05:15:04 +00:00
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
FRL-1005.001 states teh following format:
|
2016-02-17 05:11:55 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
^AVia: <FTN Address> @YYYYMMDD.HHMMSS[.Precise][.Time Zone]
|
|
|
|
<Program Name> <Version> [Serial Number]<CR>
|
|
|
|
*/
|
|
|
|
const addrStr = new Address(address).toString('5D');
|
|
|
|
const dateTime = moment().utc().format('YYYYMMDD.HHmmSS.SSSS.UTC');
|
|
|
|
const version = getCleanEnigmaVersion();
|
2016-02-17 05:11:55 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return `${addrStr} @${dateTime} ENiGMA1/2 ${version}`;
|
2016-02-17 05:11:55 +00:00
|
|
|
}
|
|
|
|
|
2018-01-01 00:54:11 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Creates a INTL kludge value as per FTS-4001
|
|
|
|
// http://retro.fidoweb.ru/docs/index=ftsc&doc=FTS-4001&enc=mac
|
2018-01-01 00:54:11 +00:00
|
|
|
//
|
|
|
|
function getIntl(toAddress, fromAddress) {
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// INTL differs from 'standard' kludges in that there is no ':' after "INTL"
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// "<SOH>"INTL "<destination address>" "<origin address><CR>"
|
|
|
|
// "...These addresses shall be given on the form <zone>:<net>/<node>"
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
|
|
|
return `${toAddress.toString('3D')} ${fromAddress.toString('3D')}`;
|
2018-01-01 00:54:11 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 00:56:05 +00:00
|
|
|
function getAbbreviatedNetNodeList(netNodes) {
|
2018-06-22 05:15:04 +00:00
|
|
|
let abbrList = '';
|
|
|
|
let currNet;
|
|
|
|
netNodes.forEach(netNode => {
|
|
|
|
if(_.isString(netNode)) {
|
|
|
|
netNode = Address.fromString(netNode);
|
|
|
|
}
|
|
|
|
if(currNet !== netNode.net) {
|
|
|
|
abbrList += `${netNode.net}/`;
|
|
|
|
currNet = netNode.net;
|
|
|
|
}
|
|
|
|
abbrList += `${netNode.node} `;
|
|
|
|
});
|
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
return abbrList.trim(); // remove trailing space
|
2016-02-16 00:56:05 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Parse an abbreviated net/node list commonly used for SEEN-BY and PATH
|
2016-03-09 05:30:04 +00:00
|
|
|
//
|
2016-02-16 00:56:05 +00:00
|
|
|
function parseAbbreviatedNetNodeList(netNodes) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const re = /([0-9]+)\/([0-9]+)\s?|([0-9]+)\s?/g;
|
|
|
|
let net;
|
|
|
|
let m;
|
|
|
|
let results = [];
|
|
|
|
while(null !== (m = re.exec(netNodes))) {
|
|
|
|
if(m[1] && m[2]) {
|
|
|
|
net = parseInt(m[1]);
|
|
|
|
results.push(new Address( { net : net, node : parseInt(m[2]) } ));
|
|
|
|
} else if(net) {
|
|
|
|
results.push(new Address( { net : net, node : parseInt(m[3]) } ));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
2016-02-16 00:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Return a FTS-0004.001 SEEN-BY entry(s) that include
|
|
|
|
// all pre-existing SEEN-BY entries with the addition
|
|
|
|
// of |additions|.
|
2016-02-16 00:56:05 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// See http://ftsc.org/docs/fts-0004.001
|
|
|
|
// and notes at http://ftsc.org/docs/fsc-0043.002.
|
2016-02-16 00:56:05 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// For a great write up, see http://www.skepticfiles.org/aj/basics03.htm
|
2016-02-16 00:56:05 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// This method returns an sorted array of values, but
|
|
|
|
// not the "SEEN-BY" prefix itself
|
2016-02-16 00:56:05 +00:00
|
|
|
//
|
|
|
|
function getUpdatedSeenByEntries(existingEntries, additions) {
|
2018-06-22 05:15:04 +00:00
|
|
|
/*
|
2018-06-23 03:26:46 +00:00
|
|
|
From FTS-0004:
|
|
|
|
|
|
|
|
"There can be many seen-by lines at the end of Conference
|
|
|
|
Mail messages, and they are the real "meat" of the control
|
|
|
|
information. They are used to determine the systems to
|
|
|
|
receive the exported messages. The format of the line is:
|
|
|
|
|
|
|
|
SEEN-BY: 132/101 113 136/601 1014/1
|
|
|
|
|
|
|
|
The net/node numbers correspond to the net/node numbers of
|
|
|
|
the systems having already received the message. In this way
|
|
|
|
a message is never sent to a system twice. In a conference
|
|
|
|
with many participants the number of seen-by lines can be
|
|
|
|
very large. This line is added if it is not already a part
|
|
|
|
of the message, or added to if it already exists, each time
|
|
|
|
a message is exported to other systems. This is a REQUIRED
|
|
|
|
field, and Conference Mail will not function correctly if
|
|
|
|
this field is not put in place by other Echomail compatible
|
|
|
|
programs."
|
2016-02-16 00:56:05 +00:00
|
|
|
*/
|
2018-06-22 05:15:04 +00:00
|
|
|
existingEntries = existingEntries || [];
|
|
|
|
if(!_.isArray(existingEntries)) {
|
|
|
|
existingEntries = [ existingEntries ];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_.isString(additions)) {
|
|
|
|
additions = parseAbbreviatedNetNodeList(getAbbreviatedNetNodeList(additions));
|
|
|
|
}
|
|
|
|
|
|
|
|
additions = additions.sort(Address.getComparator());
|
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// For now, we'll just append a new SEEN-BY entry
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: we should at least try and update what is already there in a smart way
|
2018-06-22 05:15:04 +00:00
|
|
|
existingEntries.push(getAbbreviatedNetNodeList(additions));
|
|
|
|
return existingEntries;
|
2016-02-16 00:56:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getUpdatedPathEntries(existingEntries, localAddress) {
|
2018-06-23 03:26:46 +00:00
|
|
|
// :TODO: append to PATH in a smart way! We shoudl try to fit at least the last existing line
|
2016-02-16 00:56:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
existingEntries = existingEntries || [];
|
|
|
|
if(!_.isArray(existingEntries)) {
|
|
|
|
existingEntries = [ existingEntries ];
|
|
|
|
}
|
2016-02-16 00:56:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
existingEntries.push(getAbbreviatedNetNodeList(
|
|
|
|
parseAbbreviatedNetNodeList(localAddress)));
|
2016-02-16 00:56:05 +00:00
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
return existingEntries;
|
2015-07-14 06:13:29 +00:00
|
|
|
}
|
2016-02-21 00:57:38 +00:00
|
|
|
|
|
|
|
//
|
2018-06-23 03:26:46 +00:00
|
|
|
// Return FTS-5000.001 "CHRS" value
|
|
|
|
// http://ftsc.org/docs/fts-5003.001
|
2016-02-21 00:57:38 +00:00
|
|
|
//
|
|
|
|
const ENCODING_TO_FTS_5003_001_CHARS = {
|
2018-06-23 03:26:46 +00:00
|
|
|
// level 1 - generally should not be used
|
|
|
|
ascii : [ 'ASCII', 1 ],
|
|
|
|
'us-ascii' : [ 'ASCII', 1 ],
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// level 2 - 8 bit, ASCII based
|
|
|
|
cp437 : [ 'CP437', 2 ],
|
|
|
|
cp850 : [ 'CP850', 2 ],
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// level 3 - reserved
|
2018-01-15 19:22:11 +00:00
|
|
|
|
2018-06-23 03:26:46 +00:00
|
|
|
// level 4
|
|
|
|
utf8 : [ 'UTF-8', 4 ],
|
|
|
|
'utf-8' : [ 'UTF-8', 4 ],
|
2016-02-21 00:57:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function getCharacterSetIdentifierByEncoding(encodingName) {
|
2018-06-22 05:15:04 +00:00
|
|
|
const value = ENCODING_TO_FTS_5003_001_CHARS[encodingName.toLowerCase()];
|
|
|
|
return value ? `${value[0]} ${value[1]}` : encodingName.toUpperCase();
|
2016-02-21 00:57:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 20:29:06 +00:00
|
|
|
const CHRSToEncodingTable = {
|
|
|
|
Level1 : {
|
2020-05-03 17:10:40 +00:00
|
|
|
'ASCII' : 'ascii', // ISO-646-1
|
|
|
|
'DUTCH' : 'ascii', // ISO-646
|
|
|
|
'FINNISH' : 'ascii', // ISO-646-10
|
|
|
|
'FRENCH' : 'ascii', // ISO-646
|
|
|
|
'CANADIAN' : 'ascii', // ISO-646
|
|
|
|
'GERMAN' : 'ascii', // ISO-646
|
|
|
|
'ITALIAN' : 'ascii', // ISO-646
|
|
|
|
'NORWEIG' : 'ascii', // ISO-646
|
|
|
|
'PORTU' : 'ascii', // ISO-646
|
2018-06-23 03:26:46 +00:00
|
|
|
'SPANISH' : 'iso-656',
|
2020-05-03 17:10:40 +00:00
|
|
|
'SWEDISH' : 'ascii', // ISO-646-10
|
|
|
|
'SWISS' : 'ascii', // ISO-646
|
|
|
|
'UK' : 'ascii', // ISO-646
|
|
|
|
'ISO-10' : 'ascii', // ISO-646-10
|
2020-09-12 20:29:06 +00:00
|
|
|
},
|
|
|
|
Level2 : {
|
2018-06-23 03:26:46 +00:00
|
|
|
'CP437' : 'cp437',
|
|
|
|
'CP850' : 'cp850',
|
|
|
|
'CP852' : 'cp852',
|
|
|
|
'CP866' : 'cp866',
|
|
|
|
'CP848' : 'cp848',
|
|
|
|
'CP1250' : 'cp1250',
|
|
|
|
'CP1251' : 'cp1251',
|
|
|
|
'CP1252' : 'cp1252',
|
|
|
|
'CP10000' : 'macroman',
|
|
|
|
'LATIN-1' : 'iso-8859-1',
|
|
|
|
'LATIN-2' : 'iso-8859-2',
|
|
|
|
'LATIN-5' : 'iso-8859-9',
|
|
|
|
'LATIN-9' : 'iso-8859-15',
|
2020-09-12 20:29:06 +00:00
|
|
|
},
|
2018-06-23 03:26:46 +00:00
|
|
|
|
2020-09-12 20:29:06 +00:00
|
|
|
Level4 : {
|
2018-06-23 03:26:46 +00:00
|
|
|
'UTF-8' : 'utf8',
|
2020-09-12 20:29:06 +00:00
|
|
|
},
|
2018-06-23 03:26:46 +00:00
|
|
|
|
2020-09-12 20:29:06 +00:00
|
|
|
DeprecatedMisc : {
|
2018-06-23 03:26:46 +00:00
|
|
|
'IBMPC' : 'cp1250', // :TODO: validate
|
|
|
|
'+7_FIDO' : 'cp866',
|
|
|
|
'+7' : 'cp866',
|
|
|
|
'MAC' : 'macroman', // :TODO: validate
|
2020-09-12 20:29:06 +00:00
|
|
|
}
|
|
|
|
};
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2020-09-12 20:29:06 +00:00
|
|
|
// Given 1:N CHRS kludge IDs, try to pick the best encoding we can
|
|
|
|
// http://ftsc.org/docs/fts-5003.001
|
|
|
|
// http://www.unicode.org/L2/L1999/99325-N.htm
|
|
|
|
function getEncodingFromCharacterSetIdentifier(chrs) {
|
|
|
|
if (!Array.isArray(chrs)) {
|
|
|
|
chrs = [ chrs ];
|
|
|
|
}
|
|
|
|
|
|
|
|
const encLevel = (ident, table, level) => {
|
|
|
|
const enc = table[ident];
|
|
|
|
if (enc) {
|
|
|
|
return { enc, level };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const mapping = [];
|
|
|
|
chrs.forEach(c => {
|
|
|
|
const ident = c.split(' ')[0].toUpperCase();
|
|
|
|
const mapped =
|
|
|
|
encLevel(ident, CHRSToEncodingTable.Level1, 2) ||
|
|
|
|
encLevel(ident, CHRSToEncodingTable.Level2, 1) ||
|
|
|
|
encLevel(ident, CHRSToEncodingTable.Level4, 0) ||
|
|
|
|
encLevel(ident, CHRSToEncodingTable.DeprecatedMisc, 3);
|
|
|
|
|
|
|
|
if (mapped) {
|
|
|
|
mapping.push(mapped);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mapping.sort( (l, r) => {
|
|
|
|
return l.level - r.level;
|
|
|
|
});
|
|
|
|
|
|
|
|
return mapping[0] && mapping[0].enc;
|
|
|
|
}
|