diff --git a/core/ftn_mail_packet.js b/core/ftn_mail_packet.js index 0ef88a9e..c3fa1233 100644 --- a/core/ftn_mail_packet.js +++ b/core/ftn_mail_packet.js @@ -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 ]); }; diff --git a/core/message_area.js b/core/message_area.js index 87d4db3d..45cd7301 100644 --- a/core/message_area.js +++ b/core/message_area.js @@ -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;'; diff --git a/core/scanner_tossers/ftn_bso.js b/core/scanner_tossers/ftn_bso.js index e9de2147..f2075d3f 100644 --- a/core/scanner_tossers/ftn_bso.js +++ b/core/scanner_tossers/ftn_bso.js @@ -22,6 +22,7 @@ const temp = require('temp').track(); // track() cleans up temp dir/files fo const assert = require('assert'); const gaze = require('gaze'); const fse = require('fs-extra'); +const iconv = require('iconv-lite'); exports.moduleInfo = { name : 'FTN BSO', @@ -359,6 +360,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); @@ -367,6 +369,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? @@ -987,7 +997,7 @@ function FTNMessageScanTossModule() { //nextFile(); } else { self.archivePacketFile('import', fullPath, 'imported', () => { - fs.unlink(fullPath, err => { + fs.unlink(fullPath, () => { nextFile(); }); });