Merge branch 'master' of github.com:Mailtrain-org/mailtrain into access

This commit is contained in:
Tomas Bures 2017-06-03 07:50:09 +02:00
commit d13fc65ce2
32 changed files with 1190 additions and 295 deletions

View file

@ -4,6 +4,8 @@ let config = require('config');
let mysql = require('mysql');
let redis = require('redis');
let Lock = require('redfour');
let stringifyDate = require('json-stringify-date');
let tools = require('./tools');
module.exports = mysql.createPool(config.mysql);
if (config.redis && config.redis.enabled) {
@ -33,7 +35,7 @@ if (config.redis && config.redis.enabled) {
};
module.exports.clearCache = (key, callback) => {
module.exports.redis.del(key, err => callback(err));
module.exports.redis.del('mailtrain:cache:' + key, err => callback(err));
};
module.exports.addToCache = (key, value, callback) => {
@ -41,7 +43,7 @@ if (config.redis && config.redis.enabled) {
return setImmediate(() => callback());
}
module.exports.redis.multi().
lpush('mailtrain:cache:' + key, JSON.stringify(value)).
lpush('mailtrain:cache:' + key, stringifyDate.stringify(value)).
expire('mailtrain:cache:' + key, 24 * 3600).
exec(err => callback(err));
};
@ -52,7 +54,7 @@ if (config.redis && config.redis.enabled) {
return callback(err);
}
try {
value = JSON.parse(value);
value = stringifyDate.parse(value);
} catch (E) {
return callback(E);
}
@ -76,9 +78,21 @@ if (config.redis && config.redis.enabled) {
module.exports.clearCache = (key, callback) => {
caches.delete(key);
tools.workers.forEach(child => {
child.send({
cmd: 'db.clearCache',
key
});
});
setImmediate(() => callback());
};
process.on('message', m => {
if (m && m.cmd === 'db.clearCache' && m.key) {
caches.delete(m.key);
}
});
module.exports.addToCache = (key, value, callback) => {
if (!caches.has(key)) {
caches.set(key, []);

View file

@ -182,10 +182,10 @@ function createMailer(callback) {
module.exports.transport.checkThrottling = null;
}
let throttling = Number(configItems.smtpThrottling) || 0;
if (throttling) {
let sendingRate = Number(configItems.smtpThrottling) || 0;
if (sendingRate) {
// convert to messages/second
throttling = 1 / (throttling / (3600 * 1000));
sendingRate = sendingRate / 3600;
}
let transportOptions;
@ -236,7 +236,7 @@ function createMailer(callback) {
error: logfunc.bind(null, 'error')
},
maxConnections: Number(configItems.smtpMaxConnections),
sendingRate: throttling,
sendingRate,
tls: {
rejectUnauthorized: !configItems.smtpSelfSigned
}
@ -257,19 +257,30 @@ function createMailer(callback) {
oldListeners.forEach(listener => module.exports.transport.on('idle', listener));
}
let lastCheck = Date.now();
if (configItems.mailTransport === 'smtp' || !configItems.mailTransport) {
let throttling = Number(configItems.smtpThrottling) || 0;
if (throttling) {
throttling = 1 / (throttling / (3600 * 1000));
}
let lastCheck = Date.now();
module.exports.transport.checkThrottling = function (next) {
if (!throttling) {
return next();
}
let nextCheck = Date.now();
let checkDiff = (nextCheck - lastCheck);
lastCheck = nextCheck;
if (checkDiff < throttling) {
log.verbose('Mail', 'Throttling next message in %s sec.', (throttling - checkDiff) / 1000);
setTimeout(next, throttling - checkDiff);
setTimeout(() => {
lastCheck = Date.now();
next();
}, throttling - checkDiff);
} else {
lastCheck = nextCheck;
next();
}
};

View file

@ -458,7 +458,7 @@ module.exports.getRow = (fieldList, values, useDate, showAll, onlyExisting) => {
value: Number(valueList[field.column]) || 0,
visible: !!field.visible,
mergeTag: field.key,
mergeValue: Number(valueList[field.column]) || Number(field.defaultValue) || 0,
mergeValue: (Number(valueList[field.column]) || Number(field.defaultValue) || 0).toString(),
['type' + (field.type || '').toString().trim().replace(/(?:^|\-)([a-z])/g, (m, c) => c.toUpperCase())]: true
};
row.push(item);

View file

@ -2,6 +2,7 @@
let db = require('../db');
let shortid = require('shortid');
let striptags = require('striptags');
let tools = require('../tools');
let helpers = require('../helpers');
let fields = require('./fields');
@ -136,6 +137,8 @@ module.exports.insert = (listId, meta, subscriptionData, callback) => {
values.push(field.value);
});
values = values.map(v => typeof v === 'string' ? striptags(v) : v);
db.getConnection((err, connection) => {
if (err) {
return callback(err);
@ -355,7 +358,7 @@ module.exports.getWithMergeTags = (listId, cid, callback) => {
TIMEZONE: subscription.tz || ''
};
fields.getRow(fieldList, subscription, true, true).forEach(field => {
fields.getRow(fieldList, subscription, false, true).forEach(field => {
if (field.mergeTag) {
subscription.mergeTags[field.mergeTag] = field.mergeValue || '';
}
@ -420,6 +423,8 @@ module.exports.update = (listId, cid, updates, allowEmail, callback) => {
return callback(null, false);
}
values = values.map(v => typeof v === 'string' ? striptags(v) : v);
db.getConnection((err, connection) => {
if (err) {
return callback(err);

View file

@ -13,6 +13,7 @@ let he = require('he');
let _ = require('./translate')._;
let util = require('util');
let createDOMPurify = require('dompurify');
let htmlToText = require('html-to-text');
let blockedUsers = ['abuse', 'admin', 'billing', 'compliance', 'devnull', 'dns', 'ftp', 'hostmaster', 'inoc', 'ispfeedback', 'ispsupport', 'listrequest', 'list', 'maildaemon', 'noc', 'noreply', 'noreply', 'null', 'phish', 'phishing', 'postmaster', 'privacy', 'registrar', 'root', 'security', 'spam', 'support', 'sysadmin', 'tech', 'undisclosedrecipients', 'unsubscribe', 'usenet', 'uucp', 'webmaster', 'www'];
@ -200,17 +201,24 @@ function formatMessage(serviceUrl, campaign, list, subscription, message, filter
return links[key];
}
if (subscription.mergeTags.hasOwnProperty(key)) {
return isHTML ? he.encode((subscription.mergeTags[key] || ''), {
useNamedReferences: true
}) : subscription.mergeTags[key];
let value = (subscription.mergeTags[key] || '').toString();
let containsHTML = /<[a-z][\s\S]*>/.test(value);
return isHTML ? he.encode((containsHTML ? value : value.replace(/(?:\r\n|\r|\n)/g, '<br/>')), {
useNamedReferences: true,
allowUnsafeSymbols: true
}) : (containsHTML ? htmlToText.fromString(value) : value);
}
return false;
};
return message.replace(/\[([a-z0-9_]+)(?:\/([^\]]+))?\]/ig, (match, identifier, fallback) => {
identifier = identifier.toUpperCase();
let value = (getValue(identifier) || fallback || '').trim();
return value ? filter(value) : match;
let value = getValue(identifier);
if (value === false) {
return match;
}
value = (value || fallback || '').trim();
return filter(value);
});
}
@ -310,4 +318,3 @@ function mergeTemplateIntoLayout(template, layout, callback) {
return done(template, layout);
}
}