2017-04-25 03:59:38 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-07-16 19:37:33 +08:00
|
|
|
const config = require('wild-config');
|
2017-04-25 03:59:38 +08:00
|
|
|
const db = require('./db');
|
|
|
|
const SeqIndex = require('seq-index');
|
|
|
|
const DkimStream = require('./dkim-stream');
|
|
|
|
const MessageSplitter = require('./message-splitter');
|
|
|
|
const seqIndex = new SeqIndex();
|
2017-05-15 21:09:08 +08:00
|
|
|
const GridFSBucket = require('mongodb').GridFSBucket;
|
2017-05-07 20:09:14 +08:00
|
|
|
const uuid = require('uuid');
|
|
|
|
const os = require('os');
|
2017-07-12 02:38:23 +08:00
|
|
|
const hostname = os.hostname().toLowerCase();
|
2017-05-07 20:09:14 +08:00
|
|
|
const addressparser = require('addressparser');
|
|
|
|
const punycode = require('punycode');
|
2017-10-26 19:57:19 +08:00
|
|
|
const plugins = require('./plugins');
|
2017-04-25 03:59:38 +08:00
|
|
|
|
|
|
|
let gridstore;
|
|
|
|
|
2017-05-07 20:09:14 +08:00
|
|
|
function convertAddresses(addresses, withNames, addressList) {
|
|
|
|
addressList = addressList || new Map();
|
|
|
|
|
|
|
|
flatten(addresses || []).forEach(address => {
|
|
|
|
if (address.address) {
|
|
|
|
let normalized = normalizeAddress(address, withNames);
|
|
|
|
let key = typeof normalized === 'string' ? normalized : normalized.address;
|
|
|
|
addressList.set(key, normalized);
|
|
|
|
} else if (address.group) {
|
|
|
|
convertAddresses(address.group, withNames, addressList);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return addressList;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseAddressList(headers, key, withNames) {
|
|
|
|
return parseAddressses(headers.getDecoded(key).map(header => header.value), withNames);
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseAddressses(headerList, withNames) {
|
2017-06-03 14:51:58 +08:00
|
|
|
let map = convertAddresses(
|
|
|
|
headerList.map(address => {
|
|
|
|
if (typeof address === 'string') {
|
|
|
|
address = addressparser(address);
|
|
|
|
}
|
|
|
|
return address;
|
|
|
|
}),
|
|
|
|
withNames
|
|
|
|
);
|
2017-05-07 20:09:14 +08:00
|
|
|
return Array.from(map).map(entry => entry[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeDomain(domain) {
|
2017-06-07 17:58:10 +08:00
|
|
|
domain = domain.toLowerCase().trim();
|
|
|
|
try {
|
|
|
|
domain = punycode.toASCII(domain);
|
|
|
|
} catch (E) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
return domain;
|
2017-05-07 20:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to flatten arrays
|
|
|
|
function flatten(arr) {
|
|
|
|
let flat = [].concat(...arr);
|
|
|
|
return flat.some(Array.isArray) ? flatten(flat) : flat;
|
|
|
|
}
|
|
|
|
|
|
|
|
function normalizeAddress(address, withNames) {
|
|
|
|
if (typeof address === 'string') {
|
|
|
|
address = {
|
|
|
|
address
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (!address || !address.address) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
let user = address.address.substr(0, address.address.lastIndexOf('@'));
|
|
|
|
let domain = address.address.substr(address.address.lastIndexOf('@') + 1);
|
|
|
|
let addr = user.trim() + '@' + normalizeDomain(domain);
|
|
|
|
|
|
|
|
if (withNames) {
|
|
|
|
return {
|
|
|
|
name: address.name || '',
|
|
|
|
address: addr
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateHeaders(envelope) {
|
|
|
|
// Fetch sender and receiver addresses
|
|
|
|
envelope.parsedEnvelope = {
|
|
|
|
from: parseAddressList(envelope.headers, 'from').shift() || false,
|
|
|
|
to: parseAddressList(envelope.headers, 'to'),
|
|
|
|
cc: parseAddressList(envelope.headers, 'cc'),
|
|
|
|
bcc: parseAddressList(envelope.headers, 'bcc'),
|
|
|
|
replyTo: parseAddressList(envelope.headers, 'reply-to').shift() || false,
|
|
|
|
sender: parseAddressList(envelope.headers, 'sender').shift() || false
|
|
|
|
};
|
|
|
|
|
|
|
|
// Check Message-ID: value. Add if missing
|
|
|
|
let mId = envelope.headers.getFirst('message-id');
|
|
|
|
if (!mId) {
|
|
|
|
mId = '<' + uuid.v4() + '@' + (envelope.from.substr(envelope.from.lastIndexOf('@') + 1) || hostname) + '>';
|
|
|
|
|
|
|
|
envelope.headers.remove('message-id'); // in case there's an empty value
|
|
|
|
envelope.headers.add('Message-ID', mId);
|
|
|
|
}
|
|
|
|
envelope.messageId = mId;
|
|
|
|
|
|
|
|
// Check Date: value. Add if missing or invalid or future date
|
|
|
|
let date = envelope.headers.getFirst('date');
|
|
|
|
let dateVal = new Date(date);
|
|
|
|
if (!date || dateVal.toString() === 'Invalid Date' || dateVal < new Date(1000)) {
|
|
|
|
date = new Date().toUTCString().replace(/GMT/, '+0000');
|
|
|
|
envelope.headers.remove('date'); // remove old empty or invalid values
|
|
|
|
envelope.headers.add('Date', date);
|
|
|
|
}
|
|
|
|
|
|
|
|
envelope.date = date;
|
|
|
|
|
|
|
|
// Remove BCC if present
|
|
|
|
envelope.headers.remove('bcc');
|
|
|
|
}
|
|
|
|
|
2017-04-25 03:59:38 +08:00
|
|
|
module.exports = (options, callback) => {
|
|
|
|
if (!config.sender.enabled) {
|
2017-12-15 06:22:52 +08:00
|
|
|
setImmediate(() => callback(null, false));
|
|
|
|
return false;
|
2017-04-25 03:59:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let id = options.id || seqIndex.get();
|
|
|
|
let seq = 0;
|
|
|
|
let documents = [];
|
|
|
|
|
|
|
|
let envelope = {
|
|
|
|
id,
|
|
|
|
|
2017-05-07 20:09:14 +08:00
|
|
|
from: options.from || '',
|
2017-04-25 03:59:38 +08:00
|
|
|
to: Array.isArray(options.to) ? options.to : [].concat(options.to || []),
|
|
|
|
|
|
|
|
interface: options.interface || 'maildrop',
|
|
|
|
transtype: 'API',
|
|
|
|
time: Date.now(),
|
|
|
|
|
|
|
|
dkim: {
|
|
|
|
hashAlgo: 'sha256'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-20 18:43:44 +08:00
|
|
|
if (options.parentId) {
|
|
|
|
envelope.parentId = options.parentId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.reason) {
|
|
|
|
envelope.reason = options.reason;
|
|
|
|
}
|
|
|
|
|
2017-05-07 20:09:14 +08:00
|
|
|
let deliveries = [];
|
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
if (options.targets) {
|
|
|
|
options.targets.forEach(target => {
|
|
|
|
switch (target.type) {
|
|
|
|
case 'mail':
|
|
|
|
deliveries.push({
|
|
|
|
to: target.value
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'relay':
|
2017-11-13 21:53:38 +08:00
|
|
|
(Array.isArray(options.to) ? options.to : [].concat(options.to || [])).forEach(to => {
|
|
|
|
deliveries.push({
|
|
|
|
to,
|
|
|
|
mx: target.value.mx,
|
|
|
|
mxPort: target.value.mxPort,
|
|
|
|
mxAuth: target.value.mxAuth,
|
|
|
|
mxSecure: target.value.mxSecure
|
|
|
|
});
|
2017-10-26 19:57:19 +08:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'http':
|
2017-11-13 21:53:38 +08:00
|
|
|
(Array.isArray(options.to) ? options.to : [].concat(options.to || [])).forEach(to => {
|
|
|
|
deliveries.push({
|
|
|
|
to,
|
|
|
|
http: true,
|
|
|
|
targetUrl: target.value
|
|
|
|
});
|
2017-10-26 19:57:19 +08:00
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
2017-05-07 20:09:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!deliveries.length) {
|
|
|
|
deliveries = envelope.to.map(to => ({
|
|
|
|
to
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!deliveries.length) {
|
2017-12-15 06:22:52 +08:00
|
|
|
let err = new Error('No valid recipients');
|
|
|
|
err.code = 'ENORECIPIENTS';
|
|
|
|
setImmediate(() => callback(err));
|
|
|
|
return false;
|
2017-04-25 03:59:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let messageSplitter = new MessageSplitter();
|
|
|
|
let dkimStream = new DkimStream();
|
|
|
|
|
|
|
|
messageSplitter.once('headers', headers => {
|
2017-05-07 20:09:14 +08:00
|
|
|
envelope.headers = headers;
|
|
|
|
updateHeaders(envelope);
|
2017-04-25 03:59:38 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
dkimStream.on('hash', bodyHash => {
|
|
|
|
// store relaxed body hash for signing
|
|
|
|
envelope.dkim.bodyHash = bodyHash;
|
|
|
|
envelope.bodySize = dkimStream.byteLength;
|
|
|
|
});
|
|
|
|
|
|
|
|
messageSplitter.once('error', err => dkimStream.emit('error', err));
|
|
|
|
|
|
|
|
store(id, dkimStream, err => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
plugins.runHooks('maildrop', options, envelope, deliveries, () => {
|
|
|
|
envelope.headers = envelope.headers.getList();
|
|
|
|
setMeta(id, envelope, err => {
|
|
|
|
if (err) {
|
|
|
|
return removeMessage(id, () => callback(err));
|
|
|
|
}
|
2017-04-25 03:59:38 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
let date = new Date();
|
2017-04-25 03:59:38 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
for (let i = 0, len = deliveries.length; i < len; i++) {
|
|
|
|
let recipient = deliveries[i];
|
2017-11-13 21:49:08 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
let deliveryZone = options.zone || config.sender.zone || 'default';
|
|
|
|
let recipientDomain = recipient.to.substr(recipient.to.lastIndexOf('@') + 1).replace(/[[\]]/g, '');
|
2017-04-25 03:59:38 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
seq++;
|
|
|
|
let deliverySeq = (seq < 0x100 ? '0' : '') + (seq < 0x10 ? '0' : '') + seq.toString(16);
|
|
|
|
let delivery = {
|
|
|
|
id,
|
|
|
|
seq: deliverySeq,
|
2017-04-25 03:59:38 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
// Actual delivery data
|
|
|
|
domain: recipientDomain,
|
|
|
|
sendingZone: deliveryZone,
|
2017-04-25 03:59:38 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
assigned: 'no',
|
2017-08-29 15:36:45 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
// actual recipient address
|
|
|
|
recipient: recipient.to,
|
2017-04-25 03:59:38 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
locked: false,
|
|
|
|
lockTime: 0,
|
2017-04-25 03:59:38 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
// earliest time to attempt delivery, defaults to now
|
2017-11-29 19:59:58 +08:00
|
|
|
queued: options.sendTime || date,
|
2017-04-25 03:59:38 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
// queued date might change but created should not
|
|
|
|
created: date
|
|
|
|
};
|
2017-04-25 03:59:38 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
if (recipient.http) {
|
|
|
|
delivery.http = recipient.http;
|
|
|
|
delivery.targetUrl = recipient.targetUrl;
|
|
|
|
}
|
2017-04-25 03:59:38 +08:00
|
|
|
|
2017-10-26 19:57:19 +08:00
|
|
|
['mx', 'mxPort', 'mxAuth', 'mxSecure'].forEach(key => {
|
|
|
|
if (recipient[key]) {
|
|
|
|
delivery[key] = recipient[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
documents.push(delivery);
|
2017-04-25 03:59:38 +08:00
|
|
|
}
|
|
|
|
|
2017-12-15 17:02:47 +08:00
|
|
|
db.senderDb.collection(config.sender.collection).insertMany(
|
|
|
|
documents,
|
|
|
|
{
|
|
|
|
w: 1,
|
|
|
|
ordered: false
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-12-15 06:22:52 +08:00
|
|
|
|
2017-12-15 17:02:47 +08:00
|
|
|
callback(null, envelope);
|
|
|
|
}
|
|
|
|
);
|
2017-04-25 03:59:38 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
messageSplitter.pipe(dkimStream);
|
|
|
|
return messageSplitter;
|
|
|
|
};
|
|
|
|
|
|
|
|
function store(id, stream, callback) {
|
2017-06-03 14:51:58 +08:00
|
|
|
gridstore =
|
|
|
|
gridstore ||
|
|
|
|
new GridFSBucket(db.senderDb, {
|
|
|
|
bucketName: config.sender.gfs
|
|
|
|
});
|
2017-04-25 03:59:38 +08:00
|
|
|
|
|
|
|
let returned = false;
|
2017-05-15 21:09:08 +08:00
|
|
|
let store = gridstore.openUploadStream('message ' + id, {
|
2017-04-25 03:59:38 +08:00
|
|
|
fsync: true,
|
2017-05-15 21:09:08 +08:00
|
|
|
contentType: 'message/rfc822',
|
2017-04-25 03:59:38 +08:00
|
|
|
metadata: {
|
|
|
|
created: new Date()
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.once('error', err => {
|
|
|
|
if (returned) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
returned = true;
|
|
|
|
|
2017-05-15 21:09:08 +08:00
|
|
|
store.once('finish', () => {
|
2017-04-25 03:59:38 +08:00
|
|
|
removeMessage(id, () => callback(err));
|
|
|
|
});
|
|
|
|
|
|
|
|
store.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
store.once('error', err => {
|
|
|
|
if (returned) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
returned = true;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
|
2017-05-15 21:09:08 +08:00
|
|
|
store.once('finish', () => {
|
2017-04-25 03:59:38 +08:00
|
|
|
if (returned) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
returned = true;
|
|
|
|
|
|
|
|
return callback(null, id);
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.pipe(store);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeMessage(id, callback) {
|
|
|
|
gridstore.unlink('message ' + id, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setMeta(id, data, callback) {
|
2017-12-15 17:02:47 +08:00
|
|
|
db.senderDb.collection(config.sender.gfs + '.files').findOneAndUpdate(
|
|
|
|
{
|
|
|
|
filename: 'message ' + id
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
'metadata.data': data
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{},
|
|
|
|
err => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback();
|
2017-12-15 06:22:52 +08:00
|
|
|
}
|
2017-12-15 17:02:47 +08:00
|
|
|
);
|
2017-04-25 03:59:38 +08:00
|
|
|
}
|