From 43a28e6c42305b52e46d8903cc04a8ccc7a51800 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sat, 12 Sep 2020 14:29:06 -0600 Subject: [PATCH] Error on qwk export with oputil #316 --- core/ftn_util.js | 59 +++++++++++++++++++++++++++++++---------- core/qwk_mail_packet.js | 2 +- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/core/ftn_util.js b/core/ftn_util.js index 9d53c9a3..9a171a52 100644 --- a/core/ftn_util.js +++ b/core/ftn_util.js @@ -375,14 +375,8 @@ function getCharacterSetIdentifierByEncoding(encodingName) { return value ? `${value[0]} ${value[1]}` : encodingName.toUpperCase(); } -// http://ftsc.org/docs/fts-5003.001 -// http://www.unicode.org/L2/L1999/99325-N.htm -function getEncodingFromCharacterSetIdentifier(chrs) { - const ident = chrs.split(' ')[0].toUpperCase(); - - // :TODO: fill in the rest!!! - return { - // level 1 +const CHRSToEncodingTable = { + Level1 : { 'ASCII' : 'ascii', // ISO-646-1 'DUTCH' : 'ascii', // ISO-646 'FINNISH' : 'ascii', // ISO-646-10 @@ -397,8 +391,8 @@ function getEncodingFromCharacterSetIdentifier(chrs) { 'SWISS' : 'ascii', // ISO-646 'UK' : 'ascii', // ISO-646 'ISO-10' : 'ascii', // ISO-646-10 - - // level 2 + }, + Level2 : { 'CP437' : 'cp437', 'CP850' : 'cp850', 'CP852' : 'cp852', @@ -412,15 +406,52 @@ function getEncodingFromCharacterSetIdentifier(chrs) { 'LATIN-2' : 'iso-8859-2', 'LATIN-5' : 'iso-8859-9', 'LATIN-9' : 'iso-8859-15', + }, - // level 4 + Level4 : { 'UTF-8' : 'utf8', + }, - // deprecated stuff + DeprecatedMisc : { 'IBMPC' : 'cp1250', // :TODO: validate '+7_FIDO' : 'cp866', '+7' : 'cp866', 'MAC' : 'macroman', // :TODO: validate + } +}; - }[ident]; -} \ No newline at end of file +// 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; +} diff --git a/core/qwk_mail_packet.js b/core/qwk_mail_packet.js index 6bdfe8ad..eab57eb7 100644 --- a/core/qwk_mail_packet.js +++ b/core/qwk_mail_packet.js @@ -1089,7 +1089,7 @@ class QWKPacketWriter extends EventEmitter { // indicator such as FTN-style CHRS, try to use that. encoding = _.get(message.meta, 'FtnKludge.CHRS'); if (encoding) { - // convert from CHRS to something standard + // Convert from CHRS to something standard encoding = getEncodingFromCharacterSetIdentifier(encoding); if (encoding) { return encoding;