Fix some encode/decoding errors with unknown/unsupported encodings

This commit is contained in:
Bryan Ashby 2016-04-14 22:11:01 -06:00
parent f0919b5227
commit 1113a5f366
3 changed files with 52 additions and 34 deletions

View File

@ -1,19 +1,20 @@
/* jslint node: true */
'use strict';
let ftn = require('./ftn_util.js');
let Message = require('./message.js');
let sauce = require('./sauce.js');
let Address = require('./ftn_address.js');
let strUtil = require('./string_util.js');
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;
let _ = require('lodash');
let assert = require('assert');
let binary = require('binary');
let fs = require('fs');
let async = require('async');
let iconv = require('iconv-lite');
let moment = require('moment');
const _ = require('lodash');
const assert = require('assert');
const binary = require('binary');
const fs = require('fs');
const async = require('async');
const iconv = require('iconv-lite');
const moment = require('moment');
exports.Packet = Packet;
@ -473,7 +474,7 @@ function Packet(options) {
try {
decoded = iconv.decode(messageBodyBuffer, encoding);
} catch(e) {
// :TODO: add log warning here including failure reason
Log.debug( { encoding : encoding, error : e.toString() }, 'Error decoding. Falling back to ASCII');
decoded = iconv.decode(messageBodyBuffer, 'ascii');
}
//const messageLines = iconv.decode(messageBodyBuffer, encoding).replace(/\xec/g, '').split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g);
@ -720,12 +721,19 @@ function Packet(options) {
appendMeta('\x01PATH', message.meta.FtnKludge['PATH']);
let msgBodyEncoded;
try {
msgBodyEncoded = iconv.encode(msgBody + '\0', options.encoding);
} catch(e) {
msgBodyEncoded = iconv.encode(msgBody + '\0', 'ascii');
}
return Buffer.concat( [
basicHeader,
toUserNameBuf,
fromUserNameBuf,
subjectBuf,
iconv.encode(msgBody + '\0', options.encoding)
msgBodyEncoded
]);
};

View File

@ -331,7 +331,7 @@ function getNewMessagesInAreaForUser(userId, areaTag, cb) {
sql +=
` AND message_id in (
SELECT message_id from message_meta where meta_category ="System"
AND meta_name ="${Message.SystemMetaNames.LocalToUserID}" AND meta_value =${userId})`;
AND meta_name = "${Message.SystemMetaNames.LocalToUserID}" AND meta_value = ${userId})`;
}
sql += ' ORDER BY message_id;';

View File

@ -2,26 +2,27 @@
'use strict';
// ENiGMA½
let MessageScanTossModule = require('../msg_scan_toss_module.js').MessageScanTossModule;
let Config = require('../config.js').config;
let ftnMailPacket = require('../ftn_mail_packet.js');
let ftnUtil = require('../ftn_util.js');
let Address = require('../ftn_address.js');
let Log = require('../logger.js').log;
let ArchiveUtil = require('../archive_util.js');
let msgDb = require('../database.js').dbs.message;
let Message = require('../message.js');
const MessageScanTossModule = require('../msg_scan_toss_module.js').MessageScanTossModule;
const Config = require('../config.js').config;
const ftnMailPacket = require('../ftn_mail_packet.js');
const ftnUtil = require('../ftn_util.js');
const Address = require('../ftn_address.js');
const Log = require('../logger.js').log;
const ArchiveUtil = require('../archive_util.js');
const msgDb = require('../database.js').dbs.message;
const Message = require('../message.js');
let moment = require('moment');
let _ = require('lodash');
let paths = require('path');
let async = require('async');
let fs = require('fs');
let later = require('later');
let temp = require('temp').track(); // track() cleans up temp dir/files for us
let assert = require('assert');
let gaze = require('gaze');
let fse = require('fs-extra');
const moment = require('moment');
const _ = require('lodash');
const paths = require('path');
const async = require('async');
const fs = require('fs');
const later = require('later');
const temp = require('temp').track(); // track() cleans up temp dir/files for us
const assert = require('assert');
const gaze = require('gaze');
const fse = require('fs-extra');
const iconv = require('iconv-lite');
exports.moduleInfo = {
name : 'FTN BSO',
@ -361,6 +362,7 @@ function FTNMessageScanTossModule() {
//
// Determine CHRS and actual internal encoding name
// Try to preserve anything already here
//
let encoding = options.nodeConfig.encoding || 'utf8';
if(message.meta.FtnKludge.CHRS) {
const encFromChars = ftnUtil.getEncodingFromCharacterSetIdentifier(message.meta.FtnKludge.CHRS);
@ -369,6 +371,14 @@ function FTNMessageScanTossModule() {
}
}
//
// Ensure we ended up with something useable. If not, back to utf8!
//
if(!iconv.encodingExists(encoding)) {
Log.debug( { encoding : encoding }, 'Unknown encoding. Falling back to utf8');
encoding = 'utf8';
}
options.encoding = encoding; // save for later
message.meta.FtnKludge.CHRS = ftnUtil.getCharacterSetIdentifierByEncoding(encoding);
// :TODO: FLAGS kludge?