2017-05-08 00:00:04 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const MailComposer = require('nodemailer/lib/mail-composer');
|
|
|
|
const MessageSplitter = require('./message-splitter');
|
2017-10-03 18:09:16 +08:00
|
|
|
const errors = require('./errors');
|
2021-09-06 19:19:47 +08:00
|
|
|
const { SettingsHandler } = require('./settings-handler');
|
2017-05-08 00:00:04 +08:00
|
|
|
|
2021-09-06 19:19:47 +08:00
|
|
|
async function autoreply(options, autoreplyData) {
|
2017-07-30 23:07:35 +08:00
|
|
|
if (!options.sender || /mailer-daemon|no-?reply/gi.test(options.sender)) {
|
2021-09-06 19:19:47 +08:00
|
|
|
return false;
|
2017-05-08 00:00:04 +08:00
|
|
|
}
|
|
|
|
|
2018-01-04 18:03:25 +08:00
|
|
|
// step 1. check if recipient is valid (non special address)
|
|
|
|
// step 2. check if recipient not in cache list
|
|
|
|
// step 3. parse headers, check if not automatic message
|
|
|
|
// step 4. prepare message with special headers (in-reply-to, references, Auto-Submitted)
|
2017-05-08 00:00:04 +08:00
|
|
|
|
2018-01-04 18:03:25 +08:00
|
|
|
let messageHeaders = false;
|
|
|
|
let messageSplitter = new MessageSplitter();
|
2017-07-30 23:07:35 +08:00
|
|
|
|
2021-09-06 19:19:47 +08:00
|
|
|
let settingsHandler = new SettingsHandler({ db: options.db.database });
|
|
|
|
let maxAutoreplyInterval = await settingsHandler.get('const:autoreply:interval', {});
|
2018-01-04 18:03:25 +08:00
|
|
|
|
2021-09-06 19:19:47 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
messageSplitter.once('headers', headers => {
|
|
|
|
messageHeaders = headers;
|
|
|
|
|
|
|
|
let autoSubmitted = headers.getFirst('Auto-Submitted');
|
|
|
|
if (autoSubmitted && autoSubmitted.toLowerCase() !== 'no') {
|
|
|
|
// skip automatic messages
|
|
|
|
return resolve(false);
|
|
|
|
}
|
|
|
|
let precedence = headers.getFirst('Precedence');
|
|
|
|
if (precedence && ['list', 'junk', 'bulk'].includes(precedence.toLowerCase())) {
|
|
|
|
return resolve(false);
|
|
|
|
}
|
|
|
|
let listUnsubscribe = headers.getFirst('List-Unsubscribe');
|
|
|
|
if (listUnsubscribe) {
|
|
|
|
return resolve(false);
|
|
|
|
}
|
|
|
|
let suppressAutoresponse = headers.getFirst('X-Auto-Response-Suppress');
|
|
|
|
if (suppressAutoresponse && /OOF|AutoReply/i.test(suppressAutoresponse)) {
|
|
|
|
return resolve(false);
|
|
|
|
}
|
2017-05-08 00:00:04 +08:00
|
|
|
|
2021-09-06 19:19:47 +08:00
|
|
|
options.db.redis
|
|
|
|
.multi()
|
|
|
|
// delete all old entries
|
|
|
|
.zremrangebyscore('war:' + autoreplyData._id, '-inf', Date.now() - maxAutoreplyInterval)
|
|
|
|
// add new entry if not present
|
|
|
|
.zadd('war:' + autoreplyData._id, 'NX', Date.now(), options.sender)
|
|
|
|
// if no-one touches this key from now, then delete after max interval has passed
|
|
|
|
.expire('war:' + autoreplyData._id, maxAutoreplyInterval)
|
|
|
|
.exec((err, result) => {
|
|
|
|
if (err) {
|
|
|
|
errors.notify(err, { userId: autoreplyData._id });
|
|
|
|
return resolve(false);
|
2018-01-04 18:03:25 +08:00
|
|
|
}
|
2017-07-30 23:07:35 +08:00
|
|
|
|
2021-09-06 19:19:47 +08:00
|
|
|
if (!result || !result[1] || !result[1][1]) {
|
|
|
|
// already responded
|
|
|
|
return resolve(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check limiting counters
|
2021-09-06 19:54:37 +08:00
|
|
|
options.messageHandler.counters.ttlcounter('wda:' + autoreplyData._id, 1, maxAutoreplyInterval, false, (err, result) => {
|
2021-09-06 19:19:47 +08:00
|
|
|
if (err || !result.success) {
|
|
|
|
return resolve(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
let inReplyTo = Buffer.from(headers.getFirst('Message-ID'), 'binary').toString();
|
|
|
|
|
|
|
|
let data = {
|
|
|
|
envelope: {
|
|
|
|
from: '',
|
|
|
|
to: options.sender
|
|
|
|
},
|
|
|
|
from: {
|
|
|
|
name: autoreplyData.name || (options.userData && options.userData.name),
|
|
|
|
address: options.recipient
|
|
|
|
},
|
2018-01-04 18:03:25 +08:00
|
|
|
to: options.sender,
|
2021-09-06 19:19:47 +08:00
|
|
|
subject: (autoreplyData.subject && 'Auto: ' + autoreplyData.subject) || {
|
|
|
|
prepared: true,
|
|
|
|
value: 'Auto: Re: ' + Buffer.from(headers.getFirst('Subject'), 'binary').toString()
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
'Auto-Submitted': 'auto-replied',
|
|
|
|
'X-WD-Autoreply-For': (options.parentId || options.queueId).toString()
|
|
|
|
},
|
|
|
|
inReplyTo,
|
|
|
|
references: (inReplyTo + ' ' + Buffer.from(headers.getFirst('References'), 'binary').toString()).trim(),
|
|
|
|
text: autoreplyData.text,
|
|
|
|
html: autoreplyData.html
|
|
|
|
};
|
|
|
|
|
|
|
|
let compiler = new MailComposer(data);
|
|
|
|
let message = options.maildrop.push(
|
|
|
|
{
|
|
|
|
parentId: options.parentId,
|
|
|
|
reason: 'autoreply',
|
2018-01-09 19:50:29 +08:00
|
|
|
from: '',
|
|
|
|
to: options.sender,
|
2021-09-06 19:19:47 +08:00
|
|
|
interface: 'autoreplies'
|
|
|
|
},
|
|
|
|
(err, ...args) => {
|
|
|
|
if (err || !args[0]) {
|
|
|
|
if (err) {
|
|
|
|
err.responseCode = 500;
|
|
|
|
err.code = err.code || 'ERRCOMPOSE';
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
return resolve(false);
|
|
|
|
}
|
2018-01-09 19:50:29 +08:00
|
|
|
|
2021-09-06 19:19:47 +08:00
|
|
|
let logentry = {
|
|
|
|
id: args[0].id,
|
|
|
|
messageId: args[0].messageId,
|
|
|
|
action: 'AUTOREPLY',
|
|
|
|
from: '',
|
|
|
|
to: options.sender,
|
|
|
|
sender: options.recipient,
|
|
|
|
created: new Date()
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.parentId) {
|
|
|
|
logentry.parentId = options.parentId;
|
|
|
|
}
|
|
|
|
if (options.queueId) {
|
|
|
|
logentry.queueId = options.queueId;
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolve(args && args[0] && args[0].id);
|
2018-01-09 19:50:29 +08:00
|
|
|
}
|
2021-09-06 19:19:47 +08:00
|
|
|
);
|
2018-01-09 19:50:29 +08:00
|
|
|
|
2021-09-06 19:19:47 +08:00
|
|
|
if (message) {
|
|
|
|
compiler.compile().createReadStream().pipe(message);
|
2018-01-04 18:03:25 +08:00
|
|
|
}
|
2021-09-06 19:19:47 +08:00
|
|
|
});
|
2018-01-04 18:03:25 +08:00
|
|
|
});
|
2021-09-06 19:19:47 +08:00
|
|
|
});
|
2017-12-15 17:02:47 +08:00
|
|
|
|
2021-09-06 19:19:47 +08:00
|
|
|
messageSplitter.on('error', err => reject(err));
|
|
|
|
messageSplitter.on('data', () => false);
|
|
|
|
messageSplitter.on('end', () => false);
|
2017-12-15 17:02:47 +08:00
|
|
|
|
2021-09-06 19:19:47 +08:00
|
|
|
setImmediate(() => {
|
|
|
|
let pos = 0;
|
|
|
|
let writeNextChunk = () => {
|
|
|
|
if (messageHeaders || pos >= options.chunks.length) {
|
|
|
|
return messageSplitter.end();
|
|
|
|
}
|
|
|
|
let chunk = options.chunks[pos++];
|
|
|
|
if (!messageSplitter.write(chunk)) {
|
|
|
|
return messageSplitter.once('drain', writeNextChunk);
|
|
|
|
} else {
|
|
|
|
setImmediate(writeNextChunk);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
setImmediate(writeNextChunk);
|
|
|
|
});
|
2018-01-04 18:03:25 +08:00
|
|
|
});
|
2021-09-06 19:19:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = autoreply;
|