2016-06-28 07:05:31 +08:00
|
|
|
const mimelib = require('mimelib');
|
2016-06-23 01:59:22 +08:00
|
|
|
const SNIPPET_SIZE = 100
|
|
|
|
|
2016-06-29 06:30:51 +08:00
|
|
|
function Contact({name, address} = {}) {
|
2016-06-23 01:59:22 +08:00
|
|
|
return {
|
|
|
|
name,
|
|
|
|
email: address,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 07:05:31 +08:00
|
|
|
const extractContacts = (values) =>
|
|
|
|
(values || []).map(v => Contact(mimelib.parseAddresses(v).pop()))
|
2016-06-23 01:59:22 +08:00
|
|
|
|
2016-07-09 08:13:30 +08:00
|
|
|
function processMessage({message, logger}) {
|
2016-06-28 07:05:31 +08:00
|
|
|
if (message.snippet) {
|
|
|
|
// trim and clean snippet which is alreay present (from message plaintext)
|
2016-06-29 09:01:43 +08:00
|
|
|
message.snippet = message.snippet.replace(/[\n\r]/g, ' ').replace(/\s\s+/g, ' ')
|
2016-06-28 07:05:31 +08:00
|
|
|
const loc = message.snippet.indexOf(' ', SNIPPET_SIZE);
|
|
|
|
if (loc !== -1) {
|
|
|
|
message.snippet = message.snippet.substr(0, loc);
|
|
|
|
}
|
|
|
|
} else if (message.body) {
|
|
|
|
// create snippet from body, which is most likely html
|
|
|
|
// TODO: Fanciness
|
|
|
|
message.snippet = message.body.substr(0, Math.min(message.body.length, SNIPPET_SIZE));
|
|
|
|
} else {
|
2016-07-09 08:13:30 +08:00
|
|
|
logger.info("MessageProcessor: Parsing - Received message has no body or snippet.")
|
2016-06-28 07:05:31 +08:00
|
|
|
}
|
|
|
|
|
2016-06-30 07:10:39 +08:00
|
|
|
message.to = extractContacts(message.headers.to);
|
|
|
|
message.cc = extractContacts(message.headers.cc);
|
|
|
|
message.bcc = extractContacts(message.headers.bcc);
|
|
|
|
message.from = extractContacts(message.headers.from);
|
|
|
|
message.replyTo = extractContacts(message.headers['reply-to']);
|
2016-06-23 01:59:22 +08:00
|
|
|
|
2016-06-28 07:05:31 +08:00
|
|
|
return Promise.resolve(message);
|
2016-06-23 01:59:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
order: 0,
|
|
|
|
processMessage,
|
|
|
|
}
|