2015-07-11 22:39:42 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-10-07 03:03:04 +00:00
|
|
|
const msgDb = require('./database.js').dbs.message;
|
|
|
|
const wordWrapText = require('./word_wrap.js').wordWrapText;
|
|
|
|
const ftnUtil = require('./ftn_util.js');
|
|
|
|
const createNamedUUID = require('./uuid_util.js').createNamedUUID;
|
|
|
|
const getISOTimestampString = require('./database.js').getISOTimestampString;
|
2017-08-17 03:36:14 +00:00
|
|
|
const Errors = require('./enig_error.js').Errors;
|
|
|
|
const ANSI = require('./ansi_term.js');
|
|
|
|
|
|
|
|
const {
|
2017-08-29 05:17:35 +00:00
|
|
|
isAnsi, isFormattedLine,
|
2017-08-27 17:17:29 +00:00
|
|
|
splitTextAtTerms,
|
|
|
|
renderSubstr
|
2017-08-17 03:36:14 +00:00
|
|
|
} = require('./string_util.js');
|
2016-10-07 03:03:04 +00:00
|
|
|
|
2017-08-29 05:17:35 +00:00
|
|
|
const ansiPrep = require('./ansi_prep.js');
|
|
|
|
|
2016-10-07 03:03:04 +00:00
|
|
|
// deps
|
2017-01-31 07:17:19 +00:00
|
|
|
const uuidParse = require('uuid-parse');
|
2016-10-07 03:03:04 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const assert = require('assert');
|
|
|
|
const moment = require('moment');
|
|
|
|
const iconvEncode = require('iconv-lite').encode;
|
2015-07-13 04:56:33 +00:00
|
|
|
|
2015-07-16 05:51:00 +00:00
|
|
|
module.exports = Message;
|
2015-07-12 06:32:31 +00:00
|
|
|
|
2017-01-31 07:17:19 +00:00
|
|
|
const ENIGMA_MESSAGE_UUID_NAMESPACE = uuidParse.parse('154506df-1df8-46b9-98f8-ebb5815baaf8');
|
2016-07-06 04:18:43 +00:00
|
|
|
|
2015-07-11 22:39:42 +00:00
|
|
|
function Message(options) {
|
2015-09-01 05:18:46 +00:00
|
|
|
options = options || {};
|
2015-07-16 05:51:00 +00:00
|
|
|
|
2015-07-13 04:56:33 +00:00
|
|
|
this.messageId = options.messageId || 0; // always generated @ persist
|
2016-02-03 04:35:59 +00:00
|
|
|
this.areaTag = options.areaTag || Message.WellKnownAreaTags.Invalid;
|
2016-07-06 04:18:43 +00:00
|
|
|
|
|
|
|
if(options.uuid) {
|
|
|
|
// note: new messages have UUID generated @ time of persist. See also Message.createMessageUUID()
|
|
|
|
this.uuid = options.uuid;
|
|
|
|
}
|
|
|
|
|
2015-07-13 04:56:33 +00:00
|
|
|
this.replyToMsgId = options.replyToMsgId || 0;
|
|
|
|
this.toUserName = options.toUserName || '';
|
|
|
|
this.fromUserName = options.fromUserName || '';
|
|
|
|
this.subject = options.subject || '';
|
|
|
|
this.message = options.message || '';
|
2017-08-21 02:41:53 +00:00
|
|
|
|
2016-03-15 04:29:41 +00:00
|
|
|
if(_.isDate(options.modTimestamp) || moment.isMoment(options.modTimestamp)) {
|
|
|
|
this.modTimestamp = moment(options.modTimestamp);
|
2015-07-16 23:13:48 +00:00
|
|
|
} else if(_.isString(options.modTimestamp)) {
|
2016-03-15 04:29:41 +00:00
|
|
|
this.modTimestamp = moment(options.modTimestamp);
|
2015-07-16 23:13:48 +00:00
|
|
|
}
|
|
|
|
|
2015-07-13 04:56:33 +00:00
|
|
|
this.viewCount = options.viewCount || 0;
|
2015-07-17 04:57:08 +00:00
|
|
|
|
|
|
|
this.meta = {
|
2016-01-03 01:34:50 +00:00
|
|
|
System : {}, // we'll always have this one
|
2015-07-17 04:57:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if(_.isObject(options.meta)) {
|
|
|
|
_.defaultsDeep(this.meta, options.meta);
|
|
|
|
}
|
|
|
|
|
2015-08-20 04:10:18 +00:00
|
|
|
if(options.meta) {
|
|
|
|
this.meta = options.meta;
|
|
|
|
}
|
|
|
|
|
2015-07-13 04:56:33 +00:00
|
|
|
this.hashTags = options.hashTags || [];
|
2015-07-12 06:32:31 +00:00
|
|
|
|
|
|
|
this.isValid = function() {
|
|
|
|
// :TODO: validate as much as possible
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2015-10-22 21:44:44 +00:00
|
|
|
this.isPrivate = function() {
|
2016-07-25 06:58:49 +00:00
|
|
|
return Message.isPrivateAreaTag(this.areaTag);
|
2015-10-22 21:44:44 +00:00
|
|
|
};
|
2018-01-12 04:39:14 +00:00
|
|
|
|
|
|
|
this.isFromRemoteUser = function() {
|
|
|
|
return null !== _.get(this, 'meta.System.remote_from_user', null);
|
|
|
|
};
|
2015-07-12 06:32:31 +00:00
|
|
|
}
|
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
Message.WellKnownAreaTags = {
|
2015-12-31 05:28:25 +00:00
|
|
|
Invalid : '',
|
|
|
|
Private : 'private_mail',
|
|
|
|
Bulletin : 'local_bulletin',
|
2015-07-13 04:56:33 +00:00
|
|
|
};
|
|
|
|
|
2016-07-25 06:58:49 +00:00
|
|
|
Message.isPrivateAreaTag = function(areaTag) {
|
|
|
|
return areaTag.toLowerCase() === Message.WellKnownAreaTags.Private;
|
|
|
|
};
|
|
|
|
|
2015-07-17 04:57:08 +00:00
|
|
|
Message.SystemMetaNames = {
|
2015-07-13 04:56:33 +00:00
|
|
|
LocalToUserID : 'local_to_user_id',
|
|
|
|
LocalFromUserID : 'local_from_user_id',
|
2016-03-15 04:29:41 +00:00
|
|
|
StateFlags0 : 'state_flags0', // See Message.StateFlags0
|
2017-08-21 02:41:53 +00:00
|
|
|
ExplicitEncoding : 'explicit_encoding', // Explicitly set encoding when exporting/etc.
|
2018-01-13 15:57:13 +00:00
|
|
|
ExternalFlavor : 'external_flavor', // "Flavor" of message - imported from or to be exported to. See Message.AddressFlavor
|
|
|
|
RemoteToUser : 'remote_to_user', // Opaque value depends on external system, e.g. FTN address
|
|
|
|
RemoteFromUser : 'remote_from_user', // Opaque value depends on external system, e.g. FTN address
|
2018-01-12 04:39:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Types for Message.SystemMetaNames.ExternalFlavor meta
|
2018-01-13 15:57:13 +00:00
|
|
|
Message.AddressFlavor = {
|
|
|
|
Local : 'local', // local / non-remote addressing
|
2018-01-12 04:39:14 +00:00
|
|
|
FTN : 'ftn', // FTN style
|
2018-01-13 15:57:13 +00:00
|
|
|
Email : 'email',
|
2016-03-15 04:29:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Message.StateFlags0 = {
|
|
|
|
None : 0x00000000,
|
|
|
|
Imported : 0x00000001, // imported from foreign system
|
|
|
|
Exported : 0x00000002, // exported to foreign system
|
2015-07-17 04:57:08 +00:00
|
|
|
};
|
2015-07-13 04:56:33 +00:00
|
|
|
|
2016-02-03 04:35:59 +00:00
|
|
|
Message.FtnPropertyNames = {
|
2015-07-17 04:57:08 +00:00
|
|
|
FtnOrigNode : 'ftn_orig_node',
|
|
|
|
FtnDestNode : 'ftn_dest_node',
|
|
|
|
FtnOrigNetwork : 'ftn_orig_network',
|
|
|
|
FtnDestNetwork : 'ftn_dest_network',
|
2016-02-16 00:56:05 +00:00
|
|
|
FtnAttrFlags : 'ftn_attr_flags',
|
2016-02-03 04:35:59 +00:00
|
|
|
FtnCost : 'ftn_cost',
|
2015-07-17 04:57:08 +00:00
|
|
|
FtnOrigZone : 'ftn_orig_zone',
|
|
|
|
FtnDestZone : 'ftn_dest_zone',
|
|
|
|
FtnOrigPoint : 'ftn_orig_point',
|
|
|
|
FtnDestPoint : 'ftn_dest_point',
|
2018-01-06 05:03:33 +00:00
|
|
|
|
2015-07-17 04:57:08 +00:00
|
|
|
FtnAttribute : 'ftn_attribute',
|
|
|
|
|
|
|
|
FtnTearLine : 'ftn_tear_line', // http://ftsc.org/docs/fts-0004.001
|
|
|
|
FtnOrigin : 'ftn_origin', // http://ftsc.org/docs/fts-0004.001
|
|
|
|
FtnArea : 'ftn_area', // http://ftsc.org/docs/fts-0004.001
|
|
|
|
FtnSeenBy : 'ftn_seen_by', // http://ftsc.org/docs/fts-0004.001
|
2015-07-16 23:13:48 +00:00
|
|
|
};
|
|
|
|
|
2015-07-17 14:07:43 +00:00
|
|
|
// Note: kludges are stored with their names as-is
|
2015-07-17 04:57:08 +00:00
|
|
|
|
2015-07-13 04:56:33 +00:00
|
|
|
Message.prototype.setLocalToUserId = function(userId) {
|
2018-01-01 00:54:11 +00:00
|
|
|
this.meta.System = this.meta.System || {};
|
|
|
|
this.meta.System[Message.SystemMetaNames.LocalToUserID] = userId;
|
2015-07-13 04:56:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Message.prototype.setLocalFromUserId = function(userId) {
|
2018-01-01 00:54:11 +00:00
|
|
|
this.meta.System = this.meta.System || {};
|
|
|
|
this.meta.System[Message.SystemMetaNames.LocalFromUserID] = userId;
|
2015-07-13 04:56:33 +00:00
|
|
|
};
|
|
|
|
|
2018-01-12 04:39:14 +00:00
|
|
|
Message.prototype.setRemoteToUser = function(remoteTo) {
|
|
|
|
this.meta.System = this.meta.System || {};
|
|
|
|
this.meta.System[Message.SystemMetaNames.RemoteToUser] = remoteTo;
|
|
|
|
};
|
|
|
|
|
|
|
|
Message.prototype.setExternalFlavor = function(flavor) {
|
|
|
|
this.meta.System = this.meta.System || {};
|
|
|
|
this.meta.System[Message.SystemMetaNames.ExternalFlavor] = flavor;
|
|
|
|
};
|
|
|
|
|
2016-07-06 04:18:43 +00:00
|
|
|
Message.createMessageUUID = function(areaTag, modTimestamp, subject, body) {
|
|
|
|
assert(_.isString(areaTag));
|
|
|
|
assert(_.isDate(modTimestamp) || moment.isMoment(modTimestamp));
|
|
|
|
assert(_.isString(subject));
|
|
|
|
assert(_.isString(body));
|
|
|
|
|
|
|
|
if(!moment.isMoment(modTimestamp)) {
|
|
|
|
modTimestamp = moment(modTimestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
areaTag = iconvEncode(areaTag.toUpperCase(), 'CP437');
|
|
|
|
modTimestamp = iconvEncode(modTimestamp.format('DD MMM YY HH:mm:ss'), 'CP437');
|
|
|
|
subject = iconvEncode(subject.toUpperCase().trim(), 'CP437');
|
|
|
|
body = iconvEncode(body.replace(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g, '').trim(), 'CP437');
|
|
|
|
|
2017-01-31 07:17:19 +00:00
|
|
|
return uuidParse.unparse(createNamedUUID(ENIGMA_MESSAGE_UUID_NAMESPACE, Buffer.concat( [ areaTag, modTimestamp, subject, body ] )));
|
2016-09-20 03:30:26 +00:00
|
|
|
};
|
2016-07-06 04:18:43 +00:00
|
|
|
|
2016-03-09 05:30:04 +00:00
|
|
|
Message.getMessageIdByUuid = function(uuid, cb) {
|
|
|
|
msgDb.get(
|
|
|
|
`SELECT message_id
|
|
|
|
FROM message
|
|
|
|
WHERE message_uuid = ?
|
|
|
|
LIMIT 1;`,
|
|
|
|
[ uuid ],
|
|
|
|
(err, row) => {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
|
|
|
const success = (row && row.message_id);
|
|
|
|
cb(success ? null : new Error('No match'), success ? row.message_id : null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Message.getMessageIdsByMetaValue = function(category, name, value, cb) {
|
|
|
|
msgDb.all(
|
|
|
|
`SELECT message_id
|
|
|
|
FROM message_meta
|
|
|
|
WHERE meta_category = ? AND meta_name = ? AND meta_value = ?;`,
|
|
|
|
[ category, name, value ],
|
|
|
|
(err, rows) => {
|
|
|
|
if(err) {
|
|
|
|
cb(err);
|
|
|
|
} else {
|
|
|
|
cb(null, rows.map(r => parseInt(r.message_id))); // return array of ID(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-03-15 04:29:41 +00:00
|
|
|
Message.getMetaValuesByMessageId = function(messageId, category, name, cb) {
|
|
|
|
const sql =
|
|
|
|
`SELECT meta_value
|
|
|
|
FROM message_meta
|
|
|
|
WHERE message_id = ? AND meta_category = ? AND meta_name = ?;`;
|
|
|
|
|
|
|
|
msgDb.all(sql, [ messageId, category, name ], (err, rows) => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(0 === rows.length) {
|
|
|
|
return cb(new Error('No value for category/name'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// single values are returned without an array
|
|
|
|
if(1 === rows.length) {
|
|
|
|
return cb(null, rows[0].meta_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, rows.map(r => r.meta_value)); // map to array of values only
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Message.getMetaValuesByMessageUuid = function(uuid, category, name, cb) {
|
2016-03-09 05:30:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function getMessageId(callback) {
|
|
|
|
Message.getMessageIdByUuid(uuid, (err, messageId) => {
|
|
|
|
callback(err, messageId);
|
|
|
|
});
|
|
|
|
},
|
2016-03-15 04:29:41 +00:00
|
|
|
function getMetaValues(messageId, callback) {
|
|
|
|
Message.getMetaValuesByMessageId(messageId, category, name, (err, values) => {
|
|
|
|
callback(err, values);
|
2016-03-09 05:30:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
2016-03-15 04:29:41 +00:00
|
|
|
(err, values) => {
|
|
|
|
cb(err, values);
|
2016-03-09 05:30:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Message.prototype.loadMeta = function(cb) {
|
|
|
|
/*
|
|
|
|
Example of loaded this.meta:
|
|
|
|
|
|
|
|
meta: {
|
|
|
|
System: {
|
|
|
|
local_to_user_id: 1234,
|
|
|
|
},
|
|
|
|
FtnProperty: {
|
|
|
|
ftn_seen_by: [ "1/102 103", "2/42 52 65" ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
const sql =
|
|
|
|
`SELECT meta_category, meta_name, meta_value
|
|
|
|
FROM message_meta
|
|
|
|
WHERE message_id = ?;`;
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
msgDb.each(sql, [ this.messageId ], (err, row) => {
|
|
|
|
if(!(row.meta_category in self.meta)) {
|
|
|
|
self.meta[row.meta_category] = { };
|
|
|
|
self.meta[row.meta_category][row.meta_name] = row.meta_value;
|
|
|
|
} else {
|
|
|
|
if(!(row.meta_name in self.meta[row.meta_category])) {
|
|
|
|
self.meta[row.meta_category][row.meta_name] = row.meta_value;
|
|
|
|
} else {
|
|
|
|
if(_.isString(self.meta[row.meta_category][row.meta_name])) {
|
|
|
|
self.meta[row.meta_category][row.meta_name] = [ self.meta[row.meta_category][row.meta_name] ];
|
|
|
|
}
|
|
|
|
|
|
|
|
self.meta[row.meta_category][row.meta_name].push(row.meta_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, err => {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-09-01 05:18:46 +00:00
|
|
|
Message.prototype.load = function(options, cb) {
|
|
|
|
assert(_.isString(options.uuid));
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadMessage(callback) {
|
|
|
|
msgDb.get(
|
2016-02-03 04:35:59 +00:00
|
|
|
'SELECT message_id, area_tag, message_uuid, reply_to_message_id, to_user_name, from_user_name, subject, ' +
|
2015-09-01 05:18:46 +00:00
|
|
|
'message, modified_timestamp, view_count ' +
|
|
|
|
'FROM message ' +
|
|
|
|
'WHERE message_uuid=? ' +
|
|
|
|
'LIMIT 1;',
|
|
|
|
[ options.uuid ],
|
2016-06-20 20:41:43 +00:00
|
|
|
(err, msgRow) => {
|
|
|
|
if(err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
if(!msgRow) {
|
|
|
|
return callback(new Error('Message (no longer) available'));
|
|
|
|
}
|
|
|
|
|
2015-09-01 05:18:46 +00:00
|
|
|
self.messageId = msgRow.message_id;
|
2016-02-03 04:35:59 +00:00
|
|
|
self.areaTag = msgRow.area_tag;
|
2015-09-01 05:18:46 +00:00
|
|
|
self.messageUuid = msgRow.message_uuid;
|
|
|
|
self.replyToMsgId = msgRow.reply_to_message_id;
|
|
|
|
self.toUserName = msgRow.to_user_name;
|
|
|
|
self.fromUserName = msgRow.from_user_name;
|
|
|
|
self.subject = msgRow.subject;
|
|
|
|
self.message = msgRow.message;
|
2016-03-15 04:29:41 +00:00
|
|
|
self.modTimestamp = moment(msgRow.modified_timestamp);
|
2015-09-01 05:18:46 +00:00
|
|
|
self.viewCount = msgRow.view_count;
|
|
|
|
|
|
|
|
callback(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
function loadMessageMeta(callback) {
|
2016-03-09 05:30:04 +00:00
|
|
|
self.loadMeta(err => {
|
|
|
|
callback(err);
|
|
|
|
});
|
2015-09-01 05:18:46 +00:00
|
|
|
},
|
|
|
|
function loadHashTags(callback) {
|
|
|
|
// :TODO:
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
Message.prototype.persistMetaValue = function(category, name, value, transOrDb, cb) {
|
|
|
|
if(!_.isFunction(cb) && _.isFunction(transOrDb)) {
|
|
|
|
cb = transOrDb;
|
|
|
|
transOrDb = msgDb;
|
|
|
|
}
|
|
|
|
|
|
|
|
const metaStmt = transOrDb.prepare(
|
2016-03-09 05:30:04 +00:00
|
|
|
`INSERT INTO message_meta (message_id, meta_category, meta_name, meta_value)
|
|
|
|
VALUES (?, ?, ?, ?);`);
|
|
|
|
|
|
|
|
if(!_.isArray(value)) {
|
|
|
|
value = [ value ];
|
|
|
|
}
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
async.each(value, (v, next) => {
|
|
|
|
metaStmt.run(self.messageId, category, name, v, err => {
|
|
|
|
next(err);
|
|
|
|
});
|
|
|
|
}, err => {
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-07-12 06:32:31 +00:00
|
|
|
Message.prototype.persist = function(cb) {
|
|
|
|
|
|
|
|
if(!this.isValid()) {
|
2016-03-09 05:30:04 +00:00
|
|
|
return cb(new Error('Cannot persist invalid message!'));
|
2015-07-12 06:32:31 +00:00
|
|
|
}
|
|
|
|
|
2016-09-15 03:08:59 +00:00
|
|
|
const self = this;
|
2016-03-09 05:30:04 +00:00
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
async.waterfall(
|
2015-07-12 06:32:31 +00:00
|
|
|
[
|
|
|
|
function beginTransaction(callback) {
|
2017-09-16 23:13:11 +00:00
|
|
|
return msgDb.beginTransaction(callback);
|
2015-07-12 06:32:31 +00:00
|
|
|
},
|
2017-09-16 23:13:11 +00:00
|
|
|
function storeMessage(trans, callback) {
|
2016-07-06 04:18:43 +00:00
|
|
|
// generate a UUID for this message if required (general case)
|
|
|
|
const msgTimestamp = moment();
|
|
|
|
if(!self.uuid) {
|
|
|
|
self.uuid = Message.createMessageUUID(
|
|
|
|
self.areaTag,
|
|
|
|
msgTimestamp,
|
|
|
|
self.subject,
|
|
|
|
self.message);
|
|
|
|
}
|
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
trans.run(
|
2016-03-09 05:30:04 +00:00
|
|
|
`INSERT INTO message (area_tag, message_uuid, reply_to_message_id, to_user_name, from_user_name, subject, message, modified_timestamp)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?);`,
|
2016-10-07 03:03:04 +00:00
|
|
|
[ self.areaTag, self.uuid, self.replyToMsgId, self.toUserName, self.fromUserName, self.subject, self.message, getISOTimestampString(msgTimestamp) ],
|
2016-09-15 03:08:59 +00:00
|
|
|
function inserted(err) { // use non-arrow function for 'this' scope
|
2015-07-12 06:32:31 +00:00
|
|
|
if(!err) {
|
|
|
|
self.messageId = this.lastID;
|
|
|
|
}
|
|
|
|
|
2017-09-16 23:13:11 +00:00
|
|
|
return callback(err, trans);
|
2015-07-12 06:32:31 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
2017-09-16 23:13:11 +00:00
|
|
|
function storeMeta(trans, callback) {
|
2015-07-12 06:32:31 +00:00
|
|
|
if(!self.meta) {
|
2017-09-16 23:13:11 +00:00
|
|
|
return callback(null, trans);
|
2016-09-15 03:08:59 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
Example of self.meta:
|
|
|
|
|
|
|
|
meta: {
|
|
|
|
System: {
|
|
|
|
local_to_user_id: 1234,
|
|
|
|
},
|
|
|
|
FtnProperty: {
|
|
|
|
ftn_seen_by: [ "1/102 103", "2/42 52 65" ]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
async.each(Object.keys(self.meta), (category, nextCat) => {
|
|
|
|
async.each(Object.keys(self.meta[category]), (name, nextName) => {
|
2017-09-16 23:13:11 +00:00
|
|
|
self.persistMetaValue(category, name, self.meta[category][name], trans, err => {
|
2016-09-15 03:08:59 +00:00
|
|
|
nextName(err);
|
2015-07-17 04:57:08 +00:00
|
|
|
});
|
2016-03-09 05:30:04 +00:00
|
|
|
}, err => {
|
2016-09-15 03:08:59 +00:00
|
|
|
nextCat(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
}, err => {
|
2017-09-16 23:13:11 +00:00
|
|
|
callback(err, trans);
|
2016-09-15 03:08:59 +00:00
|
|
|
});
|
2015-07-13 04:56:33 +00:00
|
|
|
},
|
2017-09-16 23:13:11 +00:00
|
|
|
function storeHashTags(trans, callback) {
|
2015-07-13 04:56:33 +00:00
|
|
|
// :TODO: hash tag support
|
2017-09-16 23:13:11 +00:00
|
|
|
return callback(null, trans);
|
2015-07-12 06:32:31 +00:00
|
|
|
}
|
2015-07-13 04:56:33 +00:00
|
|
|
],
|
2017-09-16 23:13:11 +00:00
|
|
|
(err, trans) => {
|
|
|
|
if(trans) {
|
|
|
|
trans[err ? 'rollback' : 'commit'](transErr => {
|
|
|
|
return cb(err ? err : transErr, self.messageId);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return cb(err);
|
|
|
|
}
|
2015-07-13 04:56:33 +00:00
|
|
|
}
|
2015-07-12 06:32:31 +00:00
|
|
|
);
|
2015-09-16 03:55:10 +00:00
|
|
|
};
|
|
|
|
|
2015-09-17 04:31:09 +00:00
|
|
|
Message.prototype.getFTNQuotePrefix = function(source) {
|
|
|
|
source = source || 'fromUserName';
|
|
|
|
|
2015-09-21 01:10:09 +00:00
|
|
|
return ftnUtil.getQuotePrefix(this[source]);
|
2015-09-17 04:31:09 +00:00
|
|
|
};
|
|
|
|
|
2017-08-25 04:21:37 +00:00
|
|
|
Message.prototype.getTearLinePosition = function(input) {
|
|
|
|
const m = input.match(/^--- .+$(?![\s\S]*^--- .+$)/m);
|
|
|
|
return m ? m.index : -1;
|
|
|
|
};
|
|
|
|
|
2017-08-17 03:36:14 +00:00
|
|
|
Message.prototype.getQuoteLines = function(options, cb) {
|
|
|
|
if(!options.termWidth || !options.termHeight || !options.cols) {
|
|
|
|
return cb(Errors.MissingParam());
|
|
|
|
}
|
|
|
|
|
|
|
|
options.startCol = options.startCol || 1;
|
|
|
|
options.includePrefix = _.get(options, 'includePrefix', true);
|
|
|
|
options.ansiResetSgr = options.ansiResetSgr || ANSI.getSGRFromGraphicRendition( { fg : 39, bg : 49 }, true);
|
|
|
|
options.ansiFocusPrefixSgr = options.ansiFocusPrefixSgr || ANSI.getSGRFromGraphicRendition( { intensity : 'bold', fg : 39, bg : 49 } );
|
2017-08-21 02:41:53 +00:00
|
|
|
options.isAnsi = options.isAnsi || isAnsi(this.message); // :TODO: If this.isAnsi, use that setting
|
2017-08-17 03:36:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Some long text that needs to be wrapped and quoted should look right after
|
|
|
|
doing so, don't ya think? yeah I think so
|
2015-09-17 04:31:09 +00:00
|
|
|
|
2017-08-17 03:36:14 +00:00
|
|
|
Nu> Some long text that needs to be wrapped and quoted should look right
|
|
|
|
Nu> after doing so, don't ya think? yeah I think so
|
|
|
|
|
|
|
|
Ot> Nu> Some long text that needs to be wrapped and quoted should look
|
|
|
|
Ot> Nu> right after doing so, don't ya think? yeah I think so
|
|
|
|
|
|
|
|
*/
|
|
|
|
const quotePrefix = options.includePrefix ? this.getFTNQuotePrefix(options.prefixSource || 'fromUserName') : '';
|
|
|
|
|
|
|
|
function getWrapped(text, extraPrefix) {
|
|
|
|
extraPrefix = extraPrefix ? ` ${extraPrefix}` : '';
|
|
|
|
|
|
|
|
const wrapOpts = {
|
|
|
|
width : options.cols - (quotePrefix.length + extraPrefix.length),
|
|
|
|
tabHandling : 'expand',
|
|
|
|
tabWidth : 4,
|
|
|
|
};
|
|
|
|
|
|
|
|
return wordWrapText(text, wrapOpts).wrapped.map( (w, i) => {
|
|
|
|
return i === 0 ? `${quotePrefix}${w}` : `${quotePrefix}${extraPrefix}${w}`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-28 02:08:13 +00:00
|
|
|
function getFormattedLine(line) {
|
|
|
|
// for pre-formatted text, we just append a line truncated to fit
|
|
|
|
let newLen;
|
|
|
|
const total = line.length + quotePrefix.length;
|
|
|
|
|
|
|
|
if(total > options.cols) {
|
|
|
|
newLen = options.cols - total;
|
|
|
|
} else {
|
|
|
|
newLen = total;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${quotePrefix}${line.slice(0, newLen)}`;
|
|
|
|
}
|
|
|
|
|
2017-08-19 21:24:23 +00:00
|
|
|
if(options.isAnsi) {
|
2017-08-29 05:17:35 +00:00
|
|
|
ansiPrep(
|
2017-08-17 03:36:14 +00:00
|
|
|
this.message.replace(/\r?\n/g, '\r\n'), // normalized LF -> CRLF
|
|
|
|
{
|
|
|
|
termWidth : options.termWidth,
|
|
|
|
termHeight : options.termHeight,
|
2017-08-27 17:17:29 +00:00
|
|
|
cols : options.cols,
|
2017-08-18 03:31:57 +00:00
|
|
|
rows : 'auto',
|
2017-08-17 03:36:14 +00:00
|
|
|
startCol : options.startCol,
|
|
|
|
forceLineTerm : true,
|
|
|
|
},
|
|
|
|
(err, prepped) => {
|
|
|
|
prepped = prepped || this.message;
|
|
|
|
|
|
|
|
let lastSgr = '';
|
|
|
|
const split = splitTextAtTerms(prepped);
|
|
|
|
|
|
|
|
const quoteLines = [];
|
|
|
|
const focusQuoteLines = [];
|
|
|
|
|
|
|
|
//
|
2017-08-27 17:17:29 +00:00
|
|
|
// Do not include quote prefixes (e.g. XX> ) on ANSI replies (and therefor quote builder)
|
|
|
|
// as while this works in ENiGMA, other boards such as Mystic, WWIV, etc. will try to
|
|
|
|
// strip colors, colorize the lines, etc. If we exclude the prefixes, this seems to do
|
|
|
|
// the trick and allow them to leave them alone!
|
2017-08-17 03:36:14 +00:00
|
|
|
//
|
|
|
|
split.forEach(l => {
|
2017-08-27 17:17:29 +00:00
|
|
|
quoteLines.push(`${lastSgr}${l}`);
|
|
|
|
|
2017-09-01 23:41:49 +00:00
|
|
|
focusQuoteLines.push(`${options.ansiFocusPrefixSgr}>${lastSgr}${renderSubstr(l, 1, l.length - 1)}`);
|
2017-08-25 04:21:37 +00:00
|
|
|
lastSgr = (l.match(/(?:\x1b\x5b)[\?=;0-9]*m(?!.*(?:\x1b\x5b)[\?=;0-9]*m)/) || [])[0] || ''; // eslint-disable-line no-control-regex
|
2017-08-27 17:17:29 +00:00
|
|
|
});
|
2017-08-17 03:36:14 +00:00
|
|
|
|
2017-08-21 02:41:53 +00:00
|
|
|
quoteLines[quoteLines.length - 1] += options.ansiResetSgr;
|
2017-08-17 03:36:14 +00:00
|
|
|
|
2017-08-19 21:24:23 +00:00
|
|
|
return cb(null, quoteLines, focusQuoteLines, true);
|
2017-08-17 03:36:14 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const QUOTE_RE = /^ ((?:[A-Za-z0-9]{2}\> )+(?:[A-Za-z0-9]{2}\>)*) */;
|
|
|
|
const quoted = [];
|
2017-08-28 02:08:13 +00:00
|
|
|
const input = _.trimEnd(this.message).replace(/\b/g, '');
|
2015-09-16 04:44:31 +00:00
|
|
|
|
2017-08-17 03:36:14 +00:00
|
|
|
// find *last* tearline
|
2017-08-25 04:21:37 +00:00
|
|
|
let tearLinePos = this.getTearLinePosition(input);
|
|
|
|
tearLinePos = -1 === tearLinePos ? input.length : tearLinePos; // we just want the index or the entire string
|
2017-08-17 03:36:14 +00:00
|
|
|
|
|
|
|
input.slice(0, tearLinePos).split(/\r\n\r\n|\n\n/).forEach(paragraph => {
|
|
|
|
//
|
|
|
|
// For each paragraph, a state machine:
|
|
|
|
// - New line - line
|
|
|
|
// - New (pre)quoted line - quote_line
|
|
|
|
// - Continuation of new/quoted line
|
|
|
|
//
|
2017-08-28 02:08:13 +00:00
|
|
|
// Also:
|
|
|
|
// - Detect pre-formatted lines & try to keep them as-is
|
|
|
|
//
|
2017-08-17 03:36:14 +00:00
|
|
|
let state;
|
|
|
|
let buf = '';
|
|
|
|
let quoteMatch;
|
2017-08-28 02:08:13 +00:00
|
|
|
|
|
|
|
if(quoted.length > 0) {
|
|
|
|
//
|
|
|
|
// Preserve paragraph seperation.
|
|
|
|
//
|
|
|
|
// FSC-0032 states something about leaving blank lines fully blank
|
|
|
|
// (without a prefix) but it seems nicer (and more consistent with other systems)
|
|
|
|
// to put 'em in.
|
|
|
|
//
|
|
|
|
quoted.push(quotePrefix);
|
|
|
|
}
|
|
|
|
|
2017-08-17 03:36:14 +00:00
|
|
|
paragraph.split(/\r?\n/).forEach(line => {
|
2017-08-28 02:08:13 +00:00
|
|
|
if(0 === line.trim().length) {
|
|
|
|
// see blank line notes above
|
|
|
|
return quoted.push(quotePrefix);
|
|
|
|
}
|
|
|
|
|
2017-08-17 03:36:14 +00:00
|
|
|
quoteMatch = line.match(QUOTE_RE);
|
|
|
|
|
|
|
|
switch(state) {
|
|
|
|
case 'line' :
|
|
|
|
if(quoteMatch) {
|
2017-08-28 02:08:13 +00:00
|
|
|
if(isFormattedLine(line)) {
|
|
|
|
quoted.push(getFormattedLine(line.replace(/\s/, '')));
|
|
|
|
} else {
|
|
|
|
quoted.push(...getWrapped(buf, quoteMatch[1]));
|
|
|
|
state = 'quote_line';
|
|
|
|
buf = line;
|
|
|
|
}
|
2017-08-17 03:36:14 +00:00
|
|
|
} else {
|
|
|
|
buf += ` ${line}`;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'quote_line' :
|
|
|
|
if(quoteMatch) {
|
|
|
|
const rem = line.slice(quoteMatch[0].length);
|
|
|
|
if(!buf.startsWith(quoteMatch[0])) {
|
|
|
|
quoted.push(...getWrapped(buf, quoteMatch[1]));
|
|
|
|
buf = rem;
|
|
|
|
} else {
|
|
|
|
buf += ` ${rem}`;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
quoted.push(...getWrapped(buf));
|
|
|
|
buf = line;
|
|
|
|
state = 'line';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default :
|
2017-08-28 02:08:13 +00:00
|
|
|
if(isFormattedLine(line)) {
|
|
|
|
quoted.push(getFormattedLine(line));
|
|
|
|
} else {
|
|
|
|
state = quoteMatch ? 'quote_line' : 'line';
|
|
|
|
buf = 'line' === state ? line : line.replace(/\s/, ''); // trim *first* leading space, if any
|
|
|
|
}
|
2017-08-17 03:36:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
quoted.push(...getWrapped(buf, quoteMatch ? quoteMatch[1] : null));
|
|
|
|
});
|
|
|
|
|
|
|
|
input.slice(tearLinePos).split(/\r?\n/).forEach(l => {
|
|
|
|
quoted.push(...getWrapped(l));
|
|
|
|
});
|
|
|
|
|
2017-08-19 21:24:23 +00:00
|
|
|
return cb(null, quoted, null, false);
|
2015-09-17 04:31:09 +00:00
|
|
|
}
|
2017-08-17 03:36:14 +00:00
|
|
|
};
|