-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathforward.js
59 lines (50 loc) · 1.59 KB
/
forward.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
module.exports = (options, callback) => {
let mail = {
parentId: options.parentId,
reason: 'forward',
user: options.userData && options.userData._id,
origin: options.origin,
from: options.sender,
to: options.recipient,
targets: options.targets,
interface: 'forwarder',
mtaRelay: options.userData?.mtaRelay || false
};
let message = options.maildrop.push(mail, (err, ...args) => {
if (err || !args[0]) {
if (err && !err.code && err.name === 'SMTPReject') {
err.code = 'MessageRejected';
}
if (err) {
err.code = err.code || 'ERRCOMPOSE';
}
return callback(err, ...args);
}
return callback(err, args && args[0] && args[0].id);
});
if (message) {
if (options.stream) {
options.stream.pipe(message);
options.stream.once('error', err => {
message.emit('error', err);
});
return;
}
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);
});
}
};