wildduck/lib/forward.js

58 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-04-24 21:51:50 +08:00
'use strict';
module.exports = (options, callback) => {
2017-10-26 19:57:19 +08:00
let mail = {
parentId: options.parentId,
reason: 'forward',
2017-10-20 18:43:44 +08:00
user: options.userData && options.userData._id,
origin: options.origin,
2017-10-26 19:57:19 +08:00
from: options.sender,
to: options.recipient,
2017-05-07 20:09:14 +08:00
2017-10-26 19:57:19 +08:00
targets: options.targets,
2017-05-07 20:09:14 +08:00
interface: 'forwarder'
2017-10-26 19:57:19 +08:00
};
2017-12-21 20:26:18 +08:00
let message = options.maildrop.push(mail, (err, ...args) => {
2017-10-26 19:57:19 +08:00
if (err || !args[0]) {
if (err && !err.code && err.name === 'SMTPReject') {
err.code = 'MessageRejected';
}
2017-12-15 06:22:52 +08:00
if (err) {
err.code = err.code || 'ERRCOMPOSE';
}
2017-10-26 19:57:19 +08:00
return callback(err, ...args);
2017-10-20 18:43:44 +08:00
}
2018-10-19 17:19:43 +08:00
return callback(err, args && args[0] && args[0].id);
2017-10-26 19:57:19 +08:00
});
2017-12-15 06:22:52 +08:00
if (message) {
if (options.stream) {
options.stream.pipe(message);
options.stream.once('error', err => {
message.emit('error', err);
});
return;
}
2017-04-24 21:51:50 +08:00
2017-12-15 06:22:52 +08:00
setImmediate(() => {
let pos = 0;
let writeNextChunk = () => {
if (pos >= options.chunks.length) {
return message.end();
}
let chunk = options.chunks[pos++];
if (!message.write(chunk)) {
return message.once('drain', writeNextChunk);
} else {
setImmediate(writeNextChunk);
}
};
setImmediate(writeNextChunk);
2017-10-27 20:45:51 +08:00
});
}
2017-04-25 02:20:06 +08:00
};