2017-03-06 05:45:50 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// This module converts message structure into an ENVELOPE object
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a message header object to an ENVELOPE object
|
|
|
|
*
|
|
|
|
* @param {Object} message A parsed mime tree node
|
|
|
|
* @return {Object} ENVELOPE compatible object
|
|
|
|
*/
|
|
|
|
module.exports = function (header) {
|
|
|
|
return [
|
|
|
|
header.date || null,
|
2017-03-11 21:20:47 +08:00
|
|
|
toUtf8(header.subject || ''),
|
2017-03-06 05:45:50 +08:00
|
|
|
processAddress(header.from),
|
|
|
|
processAddress(header.sender, header.from),
|
|
|
|
processAddress(header['reply-to'], header.from),
|
|
|
|
processAddress(header.to),
|
|
|
|
processAddress(header.cc),
|
|
|
|
processAddress(header.bcc),
|
|
|
|
header['in-reply-to'] || null,
|
|
|
|
header['message-id'] || null
|
|
|
|
];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an address object to a list of arrays
|
|
|
|
* [{name: 'User Name', addres:'user@example.com'}] -> [['User Name', null, 'user', 'example.com']]
|
|
|
|
*
|
|
|
|
* @param {Array} arr An array of address objects
|
|
|
|
* @return {Array} A list of addresses
|
|
|
|
*/
|
|
|
|
function processAddress(arr, defaults) {
|
|
|
|
arr = [].concat(arr || []);
|
|
|
|
if (!arr.length) {
|
|
|
|
arr = [].concat(defaults || []);
|
|
|
|
}
|
|
|
|
if (!arr.length) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
let result = [];
|
|
|
|
arr.forEach(addr => {
|
|
|
|
if (!addr.group) {
|
|
|
|
result.push([
|
2017-03-11 21:20:47 +08:00
|
|
|
toUtf8(addr.name) || null, null, toUtf8(addr.address || '').split('@').shift() || null, toUtf8(addr.address || '').split('@').pop() || null
|
2017-03-06 05:45:50 +08:00
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
// Handle group syntax
|
|
|
|
result.push([null, null, addr.name || '', null]);
|
|
|
|
result = result.concat(processAddress(addr.group) || []);
|
|
|
|
result.push([null, null, null, null]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2017-03-11 21:20:47 +08:00
|
|
|
|
|
|
|
function toUtf8(value) {
|
|
|
|
if (value && typeof value === 'string') {
|
|
|
|
value = Buffer.from(value, 'binary').toString();
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|