Added VERP support

This commit is contained in:
Andris Reinman 2016-04-10 20:26:20 -07:00
parent 06d5e0d9bf
commit e5e71e0407
13 changed files with 374 additions and 148 deletions

View file

@ -1,15 +1,17 @@
'use strict';
let config = require('config');
let passport = require('../lib/passport');
let express = require('express');
let router = new express.Router();
let tools = require('../lib/tools');
let nodemailer = require('nodemailer');
let mailer = require('../lib/mailer');
let url = require('url');
let settings = require('../lib/models/settings');
let allowedKeys = ['service_url', 'smtp_hostname', 'smtp_port', 'smtp_encryption', 'smtp_user', 'smtp_pass', 'admin_email', 'smtp_log', 'smtp_max_connections', 'smtp_max_messages', 'default_from', 'default_address', 'default_subject', 'default_homepage', 'default_postaddress', 'default_sender'];
let allowedKeys = ['service_url', 'smtp_hostname', 'smtp_port', 'smtp_encryption', 'smtp_disable_auth', 'smtp_user', 'smtp_pass', 'admin_email', 'smtp_log', 'smtp_max_connections', 'smtp_max_messages', 'smtp_self_signed', 'default_from', 'default_address', 'default_subject', 'default_homepage', 'default_postaddress', 'default_sender', 'verp_hostname', 'verp_use'];
router.all('/*', (req, res, next) => {
if (!req.user) {
@ -42,6 +44,10 @@ router.get('/', passport.csrfProtection, (req, res, next) => {
value: 'Do not use encryption'
}];
let urlparts = url.parse(configItems.serviceUrl);
configItems.verpHostname = configItems.verpHostname || 'bounces.' + (urlparts.hostname || 'localhost');
configItems.verpEnabled = config.verp.enabled;
configItems.csrfToken = req.csrfToken();
res.render('settings', configItems);
});
@ -68,11 +74,14 @@ router.post('/update', passport.parseForm, passport.csrfProtection, (req, res) =
values.push(value);
}
});
// checkbox is not included in value listing if left unchecked
if (keys.indexOf('smtp_log') < 0) {
keys.push('smtp_log');
values.push('');
}
// checkboxs are not included in value listing if left unchecked
['smtp_log', 'smtp_self_signed', 'smtp_disable_auth', 'verp_use'].forEach(key => {
if (keys.indexOf(key) < 0) {
keys.push(key);
values.push('');
}
});
let i = 0;
let storeSettings = () => {
@ -110,9 +119,12 @@ router.post('/smtp-verify', passport.parseForm, passport.csrfProtection, (req, r
port: Number(configItems.smtpPort) || false,
secure: configItems.smtpEncryption === 'TLS',
ignoreTLS: configItems.smtpEncryption === 'NONE',
auth: {
auth: configItems.smtpDisableAuth ? false : {
user: configItems.smtpUser,
pass: configItems.smtpPass
},
tls: {
rejectUnauthorized: !configItems.smtpSelfSigned
}
});

View file

@ -4,8 +4,6 @@ let express = require('express');
let router = new express.Router();
let request = require('request');
let campaigns = require('../lib/models/campaigns');
let subscriptions = require('../lib/models/subscriptions');
let db = require('../lib/db');
let log = require('npmlog');
let multer = require('multer');
let uploads = multer();
@ -42,14 +40,14 @@ router.post('/aws', (req, res, next) => {
}
if (req.body.Message.mail && req.body.Message.mail.messageId) {
campaigns.findMail(req.body.Message.mail.messageId, (err, message) => {
campaigns.findMailByResponse(req.body.Message.mail.messageId, (err, message) => {
if (err || !message) {
return;
}
switch (req.body.Message.notificationType) {
case 'Bounce':
updateMessage(message, 'bounced', ['Undetermined', 'Permanent'].indexOf(req.body.Message.bounce.bounceType) >= 0, (err, updated) => {
campaigns.updateMessage(message, 'bounced', ['Undetermined', 'Permanent'].indexOf(req.body.Message.bounce.bounceType) >= 0, (err, updated) => {
if (err) {
log.error('AWS', 'Failed updating message: %s', err.stack);
} else if (updated) {
@ -59,7 +57,7 @@ router.post('/aws', (req, res, next) => {
break;
case 'Complaint':
if (req.body.Message.complaint) {
updateMessage(message, 'complained', true, (err, updated) => {
campaigns.updateMessage(message, 'complained', true, (err, updated) => {
if (err) {
log.error('AWS', 'Failed updating message: %s', err.stack);
} else if (updated) {
@ -105,7 +103,7 @@ router.post('/sparkpost', (req, res, next) => {
return processEvents();
}
getMessage(evt.campaign_id, (err, message) => {
campaigns.findMailByCampaign(evt.campaign_id, (err, message) => {
if (err) {
return next(err);
}
@ -117,7 +115,7 @@ router.post('/sparkpost', (req, res, next) => {
switch (evt.type) {
case 'bounce':
// https://support.sparkpost.com/customer/portal/articles/1929896
return updateMessage(message, 'bounced', [1, 10, 25, 30, 50].indexOf(Number(evt.bounce_class)) >= 0, (err, updated) => {
return campaigns.updateMessage(message, 'bounced', [1, 10, 25, 30, 50].indexOf(Number(evt.bounce_class)) >= 0, (err, updated) => {
if (err) {
log.error('Sparkpost', 'Failed updating message: %s', err.stack);
} else if (updated) {
@ -126,7 +124,7 @@ router.post('/sparkpost', (req, res, next) => {
return processEvents();
});
case 'spam_complaint':
return updateMessage(message, 'complained', true, (err, updated) => {
return campaigns.updateMessage(message, 'complained', true, (err, updated) => {
if (err) {
log.error('Sparkpost', 'Failed updating message: %s', err.stack);
} else if (updated) {
@ -135,7 +133,7 @@ router.post('/sparkpost', (req, res, next) => {
return processEvents();
});
case 'link_unsubscribe':
return updateMessage(message, 'unsubscribed', true, (err, updated) => {
return campaigns.updateMessage(message, 'unsubscribed', true, (err, updated) => {
if (err) {
log.error('Sparkpost', 'Failed updating message: %s', err.stack);
} else if (updated) {
@ -171,7 +169,7 @@ router.post('/sendgrid', (req, res, next) => {
return processEvents();
}
getMessage(evt.campaign_id, (err, message) => {
campaigns.findMailByCampaign(evt.campaign_id, (err, message) => {
if (err) {
return next(err);
}
@ -183,7 +181,7 @@ router.post('/sendgrid', (req, res, next) => {
switch (evt.event) {
case 'bounce':
// https://support.sparkpost.com/customer/portal/articles/1929896
return updateMessage(message, 'bounced', true, (err, updated) => {
return campaigns.updateMessage(message, 'bounced', true, (err, updated) => {
if (err) {
log.error('Sendgrid', 'Failed updating message: %s', err.stack);
} else if (updated) {
@ -192,7 +190,7 @@ router.post('/sendgrid', (req, res, next) => {
return processEvents();
});
case 'spamreport':
return updateMessage(message, 'complained', true, (err, updated) => {
return campaigns.updateMessage(message, 'complained', true, (err, updated) => {
if (err) {
log.error('Sendgrid', 'Failed updating message: %s', err.stack);
} else if (updated) {
@ -202,7 +200,7 @@ router.post('/sendgrid', (req, res, next) => {
});
case 'group_unsubscribe':
case 'unsubscribe':
return updateMessage(message, 'unsubscribed', true, (err, updated) => {
return campaigns.updateMessage(message, 'unsubscribed', true, (err, updated) => {
if (err) {
log.error('Sendgrid', 'Failed updating message: %s', err.stack);
} else if (updated) {
@ -222,14 +220,14 @@ router.post('/sendgrid', (req, res, next) => {
router.post('/mailgun', uploads.any(), (req, res) => {
let evt = req.body;
getMessage([].concat(evt && evt.campaign_id || []).shift(), (err, message) => {
campaigns.findMailByCampaign([].concat(evt && evt.campaign_id || []).shift(), (err, message) => {
if (err || !message) {
return;
}
switch (evt.event) {
case 'bounced':
return updateMessage(message, 'bounced', true, (err, updated) => {
return campaigns.updateMessage(message, 'bounced', true, (err, updated) => {
if (err) {
log.error('Mailgun', 'Failed updating message: %s', err.stack);
} else if (updated) {
@ -237,7 +235,7 @@ router.post('/mailgun', uploads.any(), (req, res) => {
}
});
case 'complained':
return updateMessage(message, 'complained', true, (err, updated) => {
return campaigns.updateMessage(message, 'complained', true, (err, updated) => {
if (err) {
log.error('Mailgun', 'Failed updating message: %s', err.stack);
} else if (updated) {
@ -245,7 +243,7 @@ router.post('/mailgun', uploads.any(), (req, res) => {
}
});
case 'unsubscribed':
return updateMessage(message, 'unsubscribed', true, (err, updated) => {
return campaigns.updateMessage(message, 'unsubscribed', true, (err, updated) => {
if (err) {
log.error('Mailgun', 'Failed updating message: %s', err.stack);
} else if (updated) {
@ -262,102 +260,3 @@ router.post('/mailgun', uploads.any(), (req, res) => {
});
module.exports = router;
function getMessage(messageHeader, callback) {
if (!messageHeader) {
return callback(null, false);
}
let parts = messageHeader.split('.');
let cCid = parts.shift();
let sCid = parts.pop();
db.getConnection((err, connection) => {
if (err) {
return callback(err);
}
let query = 'SELECT `id`, `list`, `segment` FROM `campaigns` WHERE `cid`=? LIMIT 1';
connection.query(query, [cCid], (err, rows) => {
if (err) {
connection.release();
return callback(err);
}
if (!rows || !rows.length) {
connection.release();
return callback(null, false);
}
let campaignId = rows[0].id;
let listId = rows[0].list;
let segmentId = rows[0].segment;
let query = 'SELECT id FROM `subscription__' + listId + '` WHERE cid=? LIMIT 1';
connection.query(query, [sCid], (err, rows) => {
if (err) {
connection.release();
return callback(err);
}
if (!rows || !rows.length) {
connection.release();
return callback(null, false);
}
let subscriptionId = rows[0].id;
let query = 'SELECT `id`, `list`, `segment`, `subscription` FROM `campaign__' + campaignId + '` WHERE `list`=? AND `segment`=? AND `subscription`=? LIMIT 1';
connection.query(query, [listId, segmentId, subscriptionId], (err, rows) => {
connection.release();
if (err) {
return callback(err);
}
if (!rows || !rows.length) {
return callback(null, false);
}
let message = rows[0];
message.campaign = campaignId;
return callback(null, message);
});
});
});
});
}
function updateMessage(message, status, updateSubscription, callback) {
db.getConnection((err, connection) => {
if (err) {
return callback(err);
}
let statusCode;
if (status === 'unsubscribed') {
statusCode = 2;
}
if (status === 'bounced') {
statusCode = 3;
}
if (status === 'complained') {
statusCode = 4;
}
let query = 'UPDATE `campaigns` SET `' + status + '`=`' + status + '`+1 WHERE id=? LIMIT 1';
connection.query(query, [message.campaign], () => {
let query = 'UPDATE `campaign__' + message.campaign + '` SET status=?, updated=NOW() WHERE id=? LIMIT 1';
connection.query(query, [statusCode, message.id], err => {
connection.release();
if (err) {
return callback(err);
}
if (updateSubscription) {
subscriptions.changeStatus(message.subscription, message.list, statusCode === 2 ? message.campaign : false, statusCode, callback);
} else {
return callback(null, true);
}
});
});
});
}