2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
initialize: initialize,
|
|
|
|
|
uninitialize: uninitialize,
|
|
|
|
|
|
|
|
|
|
userAdded: userAdded,
|
|
|
|
|
userRemoved: userRemoved,
|
|
|
|
|
adminChanged: adminChanged,
|
|
|
|
|
passwordReset: passwordReset,
|
|
|
|
|
boxUpdateAvailable: boxUpdateAvailable,
|
|
|
|
|
appUpdateAvailable: appUpdateAvailable,
|
|
|
|
|
|
2016-01-18 16:11:00 +01:00
|
|
|
sendInvite: sendInvite,
|
2016-04-19 18:39:44 -07:00
|
|
|
unexpectedExit: unexpectedExit,
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-08-04 14:31:40 +02:00
|
|
|
appDied: appDied,
|
|
|
|
|
|
2016-01-22 17:37:41 -08:00
|
|
|
outOfDiskSpace: outOfDiskSpace,
|
2016-10-14 14:46:34 -07:00
|
|
|
backupFailed: backupFailed,
|
2016-01-22 17:37:41 -08:00
|
|
|
|
2016-07-26 16:20:31 -07:00
|
|
|
certificateRenewalError: certificateRenewalError,
|
2016-03-19 20:40:03 -07:00
|
|
|
|
2015-08-04 14:31:40 +02:00
|
|
|
FEEDBACK_TYPE_FEEDBACK: 'feedback',
|
|
|
|
|
FEEDBACK_TYPE_TICKET: 'ticket',
|
2016-02-25 21:38:37 +01:00
|
|
|
FEEDBACK_TYPE_APP_MISSING: 'app_missing',
|
|
|
|
|
FEEDBACK_TYPE_APP_ERROR: 'app_error',
|
2016-04-18 17:11:36 +02:00
|
|
|
FEEDBACK_TYPE_UPGRADE_REQUEST: 'upgrade_request',
|
2016-01-18 14:00:35 +01:00
|
|
|
sendFeedback: sendFeedback,
|
|
|
|
|
|
|
|
|
|
_getMailQueue: _getMailQueue,
|
|
|
|
|
_clearMailQueue: _clearMailQueue
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
|
|
|
|
async = require('async'),
|
2015-10-29 10:12:44 -07:00
|
|
|
cloudron = require('./cloudron.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
config = require('./config.js'),
|
|
|
|
|
debug = require('debug')('box:mailer'),
|
2015-10-30 15:54:13 -07:00
|
|
|
dns = require('native-dns'),
|
2015-10-19 11:24:21 -07:00
|
|
|
docker = require('./docker.js').connection,
|
2015-07-20 00:09:47 -07:00
|
|
|
ejs = require('ejs'),
|
|
|
|
|
nodemailer = require('nodemailer'),
|
|
|
|
|
path = require('path'),
|
|
|
|
|
safe = require('safetydance'),
|
2016-10-13 11:14:17 +02:00
|
|
|
settings = require('./settings.js'),
|
2016-11-07 12:50:09 +01:00
|
|
|
showdown = require('showdown'),
|
2015-07-20 00:09:47 -07:00
|
|
|
smtpTransport = require('nodemailer-smtp-transport'),
|
2016-01-15 16:15:21 +01:00
|
|
|
users = require('./user.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
util = require('util'),
|
|
|
|
|
_ = require('underscore');
|
|
|
|
|
|
|
|
|
|
var MAIL_TEMPLATES_DIR = path.join(__dirname, 'mail_templates');
|
|
|
|
|
|
|
|
|
|
var gMailQueue = [ ],
|
|
|
|
|
gDnsReady = false,
|
|
|
|
|
gCheckDnsTimerId = null;
|
|
|
|
|
|
|
|
|
|
function initialize(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2017-01-05 19:06:10 -08:00
|
|
|
if (cloudron.getConfigStateSync().configured) {
|
2015-10-29 10:12:44 -07:00
|
|
|
checkDns();
|
|
|
|
|
} else {
|
2015-11-03 15:50:02 -08:00
|
|
|
cloudron.events.on(cloudron.EVENT_CONFIGURED, checkDns);
|
2015-10-29 10:12:44 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
callback(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function uninitialize(callback) {
|
|
|
|
|
assert.strictEqual(typeof callback, 'function');
|
|
|
|
|
|
2015-11-03 15:50:02 -08:00
|
|
|
cloudron.events.removeListener(cloudron.EVENT_CONFIGURED, checkDns);
|
2015-10-30 12:52:33 -07:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
// TODO: interrupt processQueue as well
|
|
|
|
|
clearTimeout(gCheckDnsTimerId);
|
|
|
|
|
gCheckDnsTimerId = null;
|
|
|
|
|
|
|
|
|
|
debug(gMailQueue.length + ' mail items dropped');
|
|
|
|
|
gMailQueue = [ ];
|
|
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-03 12:02:42 -07:00
|
|
|
function mailConfig() {
|
|
|
|
|
return {
|
2016-09-27 10:27:44 -07:00
|
|
|
from: '"Cloudron" <no-reply@' + config.fqdn() + '>'
|
2016-09-03 12:02:42 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 15:54:13 -07:00
|
|
|
function getTxtRecords(callback) {
|
|
|
|
|
dns.resolveNs(config.zoneName(), function (error, nameservers) {
|
|
|
|
|
if (error || !nameservers) return callback(error || new Error('Unable to get nameservers'));
|
|
|
|
|
|
|
|
|
|
var nameserver = nameservers[0];
|
|
|
|
|
|
|
|
|
|
dns.resolve4(nameserver, function (error, nsIps) {
|
|
|
|
|
if (error || !nsIps || nsIps.length === 0) return callback(error);
|
|
|
|
|
|
|
|
|
|
var req = dns.Request({
|
|
|
|
|
question: dns.Question({ name: config.fqdn(), type: 'TXT' }),
|
|
|
|
|
server: { address: nsIps[0] },
|
|
|
|
|
timeout: 5000
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
req.on('timeout', function () { return callback(new Error('ETIMEOUT')); });
|
|
|
|
|
|
|
|
|
|
req.on('message', function (error, message) {
|
|
|
|
|
if (error || !message.answer || message.answer.length === 0) return callback(null, null);
|
|
|
|
|
|
|
|
|
|
var records = message.answer.map(function (a) { return a.data[0]; });
|
|
|
|
|
callback(null, records);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
req.send();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-31 08:44:31 -07:00
|
|
|
// keep this in sync with the cloudron.js dns changes
|
2015-07-20 00:09:47 -07:00
|
|
|
function checkDns() {
|
2015-10-30 15:54:13 -07:00
|
|
|
getTxtRecords(function (error, records) {
|
2015-10-30 16:24:56 -07:00
|
|
|
if (error || !records) {
|
2016-06-07 20:09:53 -07:00
|
|
|
debug('checkDns: DNS error or no records looking up TXT records for %s %s', config.fqdn(), error, records);
|
2015-10-28 14:44:37 -07:00
|
|
|
gCheckDnsTimerId = setTimeout(checkDns, 60000);
|
2015-07-20 00:09:47 -07:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-28 14:44:37 -07:00
|
|
|
var allowedToSendMail = false;
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < records.length; i++) {
|
2015-10-30 16:10:01 -07:00
|
|
|
if (records[i].indexOf('v=spf1 ') !== 0) continue; // not SPF
|
2015-10-28 14:44:37 -07:00
|
|
|
|
2016-03-31 08:44:31 -07:00
|
|
|
allowedToSendMail = records[i].indexOf('a:' + config.adminFqdn()) !== -1;
|
2015-10-28 14:44:37 -07:00
|
|
|
break; // only one SPF record can exist (https://support.google.com/a/answer/4568483?hl=en)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!allowedToSendMail) {
|
|
|
|
|
debug('checkDns: SPF records disallow sending email from cloudron. %j', records);
|
|
|
|
|
gCheckDnsTimerId = setTimeout(checkDns, 60000);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-28 15:21:47 -07:00
|
|
|
debug('checkDns: SPF check passed. commencing mail processing');
|
2015-11-05 10:28:41 -08:00
|
|
|
gDnsReady = true;
|
2015-07-20 00:09:47 -07:00
|
|
|
processQueue();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function processQueue() {
|
2015-11-05 10:28:41 -08:00
|
|
|
assert(gDnsReady);
|
|
|
|
|
|
2015-11-10 00:24:40 -08:00
|
|
|
sendMails(gMailQueue);
|
|
|
|
|
gMailQueue = [ ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// note : this function should NOT access the database. it is called by the crashnotifier
|
|
|
|
|
// which does not initialize mailer or the databse
|
|
|
|
|
function sendMails(queue) {
|
|
|
|
|
assert(util.isArray(queue));
|
|
|
|
|
|
2016-05-16 08:18:33 -07:00
|
|
|
docker.getContainer('mail').inspect(function (error, data) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return console.error(error);
|
|
|
|
|
|
2016-06-20 18:26:22 -05:00
|
|
|
var mailServerIp = safe.query(data, 'NetworkSettings.Networks.cloudron.IPAddress');
|
2015-07-20 00:09:47 -07:00
|
|
|
if (!mailServerIp) return debug('Error querying mail server IP');
|
|
|
|
|
|
|
|
|
|
var transport = nodemailer.createTransport(smtpTransport({
|
|
|
|
|
host: mailServerIp,
|
2016-09-27 10:27:44 -07:00
|
|
|
port: config.get('smtpPort')
|
2015-07-20 00:09:47 -07:00
|
|
|
}));
|
|
|
|
|
|
2016-05-16 12:52:36 -07:00
|
|
|
debug('Processing mail queue of size %d (through %s:2525)', queue.length, mailServerIp);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2015-11-10 00:24:40 -08:00
|
|
|
async.mapSeries(queue, function iterator(mailOptions, callback) {
|
2015-07-20 00:09:47 -07:00
|
|
|
transport.sendMail(mailOptions, function (error) {
|
|
|
|
|
if (error) return console.error(error); // TODO: requeue?
|
|
|
|
|
debug('Email sent to ' + mailOptions.to);
|
|
|
|
|
});
|
|
|
|
|
callback(null);
|
|
|
|
|
}, function done() {
|
|
|
|
|
debug('Done processing mail queue');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enqueue(mailOptions) {
|
|
|
|
|
assert.strictEqual(typeof mailOptions, 'object');
|
|
|
|
|
|
2016-01-29 12:40:34 +01:00
|
|
|
if (!mailOptions.from) console.error('sender address is missing');
|
|
|
|
|
if (!mailOptions.to) console.error('recipient address is missing');
|
2016-01-18 16:11:00 +01:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
debug('Queued mail for ' + mailOptions.from + ' to ' + mailOptions.to);
|
|
|
|
|
gMailQueue.push(mailOptions);
|
|
|
|
|
|
|
|
|
|
if (gDnsReady) processQueue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function render(templateFile, params) {
|
|
|
|
|
assert.strictEqual(typeof templateFile, 'string');
|
|
|
|
|
assert.strictEqual(typeof params, 'object');
|
|
|
|
|
|
|
|
|
|
return ejs.render(safe.fs.readFileSync(path.join(MAIL_TEMPLATES_DIR, templateFile), 'utf8'), params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getAdminEmails(callback) {
|
2016-01-15 16:15:21 +01:00
|
|
|
users.getAllAdmins(function (error, admins) {
|
2015-07-20 00:09:47 -07:00
|
|
|
if (error) return callback(error);
|
|
|
|
|
|
2016-04-05 12:23:27 -07:00
|
|
|
if (admins.length === 0) return callback(new Error('No admins on this cloudron')); // box not activated yet
|
|
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
var adminEmails = [ ];
|
|
|
|
|
admins.forEach(function (admin) { adminEmails.push(admin.email); });
|
|
|
|
|
|
|
|
|
|
callback(null, adminEmails);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mailUserEventToAdmins(user, event) {
|
|
|
|
|
assert.strictEqual(typeof user, 'object');
|
|
|
|
|
assert.strictEqual(typeof event, 'string');
|
|
|
|
|
|
|
|
|
|
getAdminEmails(function (error, adminEmails) {
|
|
|
|
|
if (error) return console.log('Error getting admins', error);
|
|
|
|
|
|
|
|
|
|
adminEmails = _.difference(adminEmails, [ user.email ]);
|
|
|
|
|
|
|
|
|
|
var mailOptions = {
|
2016-09-03 12:02:42 -07:00
|
|
|
from: mailConfig().from,
|
2015-07-20 00:09:47 -07:00
|
|
|
to: adminEmails.join(', '),
|
2016-09-28 13:25:41 -07:00
|
|
|
subject: util.format('%s %s in Cloudron %s', user.username || user.alternateEmail || user.email, event, config.fqdn()),
|
2016-04-03 01:41:47 +02:00
|
|
|
text: render('user_event.ejs', { fqdn: config.fqdn(), user: user, event: event, format: 'text' }),
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-18 16:11:00 +01:00
|
|
|
function sendInvite(user, invitor) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof user, 'object');
|
|
|
|
|
assert(typeof invitor === 'object');
|
|
|
|
|
|
2016-01-18 16:11:00 +01:00
|
|
|
debug('Sending invite mail');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-10-13 11:14:17 +02:00
|
|
|
settings.getCloudronName(function (error, cloudronName) {
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
cloudronName = 'Cloudron';
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-10-13 11:14:17 +02:00
|
|
|
var templateData = {
|
|
|
|
|
user: user,
|
|
|
|
|
webadminUrl: config.adminOrigin(),
|
|
|
|
|
setupLink: config.adminOrigin() + '/api/v1/session/account/setup.html?reset_token=' + user.resetToken,
|
|
|
|
|
fqdn: config.fqdn(),
|
|
|
|
|
invitor: invitor,
|
|
|
|
|
cloudronName: cloudronName,
|
|
|
|
|
cloudronAvatarUrl: config.adminOrigin() + '/api/v1/cloudron/avatar'
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:38:52 +02:00
|
|
|
var templateDataText = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataText.format = 'text';
|
|
|
|
|
|
|
|
|
|
var templateDataHTML = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataHTML.format = 'html';
|
|
|
|
|
|
2016-10-13 11:14:17 +02:00
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig().from,
|
|
|
|
|
to: user.alternateEmail || user.email,
|
|
|
|
|
subject: util.format('Welcome to %s', cloudronName),
|
2016-10-13 11:38:52 +02:00
|
|
|
text: render('welcome_user.ejs', templateDataText),
|
|
|
|
|
html: render('welcome_user.ejs', templateDataHTML)
|
2016-10-13 11:14:17 +02:00
|
|
|
};
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-10-13 11:14:17 +02:00
|
|
|
enqueue(mailOptions);
|
|
|
|
|
});
|
2016-01-18 16:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
2016-01-20 12:40:54 +01:00
|
|
|
function userAdded(user, inviteSent) {
|
2016-01-18 16:11:00 +01:00
|
|
|
assert.strictEqual(typeof user, 'object');
|
2016-01-20 12:40:54 +01:00
|
|
|
assert.strictEqual(typeof inviteSent, 'boolean');
|
2016-01-18 16:11:00 +01:00
|
|
|
|
2016-01-20 12:40:54 +01:00
|
|
|
debug('Sending mail for userAdded %s including invite link', inviteSent ? 'not' : '');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-01-20 12:40:54 +01:00
|
|
|
getAdminEmails(function (error, adminEmails) {
|
|
|
|
|
if (error) return console.log('Error getting admins', error);
|
|
|
|
|
|
|
|
|
|
adminEmails = _.difference(adminEmails, [ user.email ]);
|
|
|
|
|
|
2016-10-13 12:37:25 +02:00
|
|
|
settings.getCloudronName(function (error, cloudronName) {
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
cloudronName = 'Cloudron';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var templateData = {
|
|
|
|
|
fqdn: config.fqdn(),
|
|
|
|
|
user: user,
|
|
|
|
|
inviteLink: inviteSent ? null : config.adminOrigin() + '/api/v1/session/account/setup.html?reset_token=' + user.resetToken,
|
|
|
|
|
format: 'text',
|
|
|
|
|
cloudronName: cloudronName
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig().from,
|
|
|
|
|
to: adminEmails.join(', '),
|
|
|
|
|
subject: util.format('%s: user %s added', cloudronName, user.alternateEmail || user.email),
|
|
|
|
|
text: render('user_added.ejs', templateData)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
|
|
|
|
});
|
2016-01-20 12:40:54 +01:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-03 01:41:47 +02:00
|
|
|
function userRemoved(user) {
|
|
|
|
|
assert.strictEqual(typeof user, 'object');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-04-03 01:41:47 +02:00
|
|
|
debug('Sending mail for userRemoved.', user.id, user.email);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-04-03 01:41:47 +02:00
|
|
|
mailUserEventToAdmins(user, 'was removed');
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
2016-02-08 16:53:20 -08:00
|
|
|
function adminChanged(user, admin) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof user, 'object');
|
2016-02-08 16:53:20 -08:00
|
|
|
assert.strictEqual(typeof admin, 'boolean');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
debug('Sending mail for adminChanged');
|
|
|
|
|
|
2016-02-08 16:53:20 -08:00
|
|
|
mailUserEventToAdmins(user, admin ? 'is now an admin' : 'is no more an admin');
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function passwordReset(user) {
|
|
|
|
|
assert.strictEqual(typeof user, 'object');
|
|
|
|
|
|
2016-04-03 01:41:47 +02:00
|
|
|
debug('Sending mail for password reset for user %s.', user.email, user.id);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-10-13 16:44:09 +02:00
|
|
|
settings.getCloudronName(function (error, cloudronName) {
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
cloudronName = 'Cloudron';
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-10-13 16:44:09 +02:00
|
|
|
var templateData = {
|
|
|
|
|
fqdn: config.fqdn(),
|
|
|
|
|
user: user,
|
|
|
|
|
resetLink: config.adminOrigin() + '/api/v1/session/password/reset.html?reset_token=' + user.resetToken,
|
|
|
|
|
cloudronName: cloudronName,
|
|
|
|
|
cloudronAvatarUrl: config.adminOrigin() + '/api/v1/cloudron/avatar'
|
|
|
|
|
};
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-10-13 16:44:09 +02:00
|
|
|
var templateDataText = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataText.format = 'text';
|
|
|
|
|
|
|
|
|
|
var templateDataHTML = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataHTML.format = 'html';
|
|
|
|
|
|
|
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig().from,
|
|
|
|
|
to: user.alternateEmail || user.email,
|
2016-10-13 16:56:55 +02:00
|
|
|
subject: cloudronName + ': Password Reset',
|
2016-10-13 16:44:09 +02:00
|
|
|
text: render('password_reset.ejs', templateDataText),
|
|
|
|
|
html: render('password_reset.ejs', templateDataHTML)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function appDied(app) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
|
|
|
|
|
debug('Sending mail for app %s @ %s died', app.id, app.location);
|
|
|
|
|
|
|
|
|
|
getAdminEmails(function (error, adminEmails) {
|
|
|
|
|
if (error) return console.log('Error getting admins', error);
|
|
|
|
|
|
|
|
|
|
var mailOptions = {
|
2016-09-03 12:02:42 -07:00
|
|
|
from: mailConfig().from,
|
2016-08-02 14:40:47 -07:00
|
|
|
to: config.provider() === 'caas' ? 'support@cloudron.io' : adminEmails.concat('support@cloudron.io').join(', '),
|
2015-07-20 00:09:47 -07:00
|
|
|
subject: util.format('App %s is down', app.location),
|
|
|
|
|
text: render('app_down.ejs', { fqdn: config.fqdn(), title: app.manifest.title, appFqdn: config.appFqdn(app.location), format: 'text' })
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function boxUpdateAvailable(newBoxVersion, changelog) {
|
|
|
|
|
assert.strictEqual(typeof newBoxVersion, 'string');
|
|
|
|
|
assert(util.isArray(changelog));
|
|
|
|
|
|
|
|
|
|
getAdminEmails(function (error, adminEmails) {
|
|
|
|
|
if (error) return console.log('Error getting admins', error);
|
|
|
|
|
|
2016-11-07 12:46:03 +01:00
|
|
|
settings.getCloudronName(function (error, cloudronName) {
|
|
|
|
|
if (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
cloudronName = 'Cloudron';
|
|
|
|
|
}
|
2016-11-07 12:32:57 +01:00
|
|
|
|
2016-11-07 12:50:09 +01:00
|
|
|
var converter = new showdown.Converter();
|
|
|
|
|
|
2016-11-07 12:46:03 +01:00
|
|
|
var templateData = {
|
|
|
|
|
fqdn: config.fqdn(),
|
|
|
|
|
webadminUrl: config.adminOrigin(),
|
|
|
|
|
newBoxVersion: newBoxVersion,
|
|
|
|
|
changelog: changelog,
|
2016-11-07 12:50:09 +01:00
|
|
|
changelogHTML: changelog.map(function (e) { return converter.makeHtml(e); }),
|
2016-11-07 12:46:03 +01:00
|
|
|
cloudronName: cloudronName,
|
|
|
|
|
cloudronAvatarUrl: config.adminOrigin() + '/api/v1/cloudron/avatar'
|
|
|
|
|
};
|
2016-11-07 12:32:57 +01:00
|
|
|
|
2016-11-07 12:46:03 +01:00
|
|
|
var templateDataText = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataText.format = 'text';
|
2016-11-07 12:32:57 +01:00
|
|
|
|
2016-11-07 12:46:03 +01:00
|
|
|
var templateDataHTML = JSON.parse(JSON.stringify(templateData));
|
|
|
|
|
templateDataHTML.format = 'html';
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-11-07 12:46:03 +01:00
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig().from,
|
|
|
|
|
to: adminEmails.join(', '),
|
|
|
|
|
subject: util.format('%s has a new update available', config.fqdn()),
|
|
|
|
|
text: render('box_update_available.ejs', templateDataText),
|
|
|
|
|
html: render('box_update_available.ejs', templateDataHTML)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
|
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function appUpdateAvailable(app, updateInfo) {
|
|
|
|
|
assert.strictEqual(typeof app, 'object');
|
|
|
|
|
assert.strictEqual(typeof updateInfo, 'object');
|
|
|
|
|
|
|
|
|
|
getAdminEmails(function (error, adminEmails) {
|
|
|
|
|
if (error) return console.log('Error getting admins', error);
|
|
|
|
|
|
|
|
|
|
var mailOptions = {
|
2016-09-03 12:02:42 -07:00
|
|
|
from: mailConfig().from,
|
2015-07-20 00:09:47 -07:00
|
|
|
to: adminEmails.join(', '),
|
|
|
|
|
subject: util.format('%s has a new update available', app.fqdn),
|
2016-02-04 14:45:43 +01:00
|
|
|
text: render('app_update_available.ejs', { fqdn: config.fqdn(), webadminUrl: config.adminOrigin(), app: app, updateInfo: updateInfo, format: 'text' })
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-22 17:37:41 -08:00
|
|
|
function outOfDiskSpace(message) {
|
|
|
|
|
assert.strictEqual(typeof message, 'string');
|
|
|
|
|
|
2016-07-26 16:43:10 -07:00
|
|
|
getAdminEmails(function (error, adminEmails) {
|
|
|
|
|
if (error) return console.log('Error getting admins', error);
|
2016-01-22 17:37:41 -08:00
|
|
|
|
2016-07-26 16:43:10 -07:00
|
|
|
var mailOptions = {
|
2016-09-03 12:02:42 -07:00
|
|
|
from: mailConfig().from,
|
2016-07-27 11:48:03 -07:00
|
|
|
to: config.provider() === 'caas' ? 'support@cloudron.io' : adminEmails.join(', '),
|
2016-07-26 16:43:10 -07:00
|
|
|
subject: util.format('[%s] Out of disk space alert', config.fqdn()),
|
|
|
|
|
text: render('out_of_disk_space.ejs', { fqdn: config.fqdn(), message: message, format: 'text' })
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sendMails([ mailOptions ]);
|
|
|
|
|
});
|
2016-01-22 17:37:41 -08:00
|
|
|
}
|
|
|
|
|
|
2016-10-14 14:46:34 -07:00
|
|
|
function backupFailed(message) {
|
|
|
|
|
getAdminEmails(function (error, adminEmails) {
|
|
|
|
|
if (error) return console.log('Error getting admins', error);
|
|
|
|
|
|
|
|
|
|
var mailOptions = {
|
|
|
|
|
from: mailConfig().from,
|
|
|
|
|
to: config.provider() === 'caas' ? 'support@cloudron.io' : adminEmails.concat('support@cloudron.io').join(', '),
|
|
|
|
|
subject: util.format('[%s] Failed to backup', config.fqdn()),
|
|
|
|
|
text: render('backup_failed.ejs', { fqdn: config.fqdn(), message: message, format: 'text' })
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-26 16:20:31 -07:00
|
|
|
function certificateRenewalError(domain, message) {
|
2016-03-19 20:40:03 -07:00
|
|
|
assert.strictEqual(typeof domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof message, 'string');
|
|
|
|
|
|
2016-07-26 16:47:58 -07:00
|
|
|
getAdminEmails(function (error, adminEmails) {
|
|
|
|
|
if (error) return console.log('Error getting admins', error);
|
2016-03-19 20:40:03 -07:00
|
|
|
|
2016-07-26 16:47:58 -07:00
|
|
|
var mailOptions = {
|
2016-09-03 12:02:42 -07:00
|
|
|
from: mailConfig().from,
|
2017-01-07 12:29:40 -08:00
|
|
|
to: config.provider() === 'caas' ? 'support@cloudron.io' : adminEmails.concat('support@cloudron.io').join(', '),
|
2016-07-26 16:47:58 -07:00
|
|
|
subject: util.format('[%s] Certificate renewal error', domain),
|
|
|
|
|
text: render('certificate_renewal_error.ejs', { domain: domain, message: message, format: 'text' })
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sendMails([ mailOptions ]);
|
|
|
|
|
});
|
2016-03-19 20:40:03 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-10 00:24:40 -08:00
|
|
|
// this function bypasses the queue intentionally. it is also expected to work without the mailer module initialized
|
2016-09-03 11:35:56 -07:00
|
|
|
// NOTE: crashnotifier should be able to send mail when there is no db
|
2016-04-19 18:39:44 -07:00
|
|
|
function unexpectedExit(program, context) {
|
2015-07-20 00:09:47 -07:00
|
|
|
assert.strictEqual(typeof program, 'string');
|
|
|
|
|
assert.strictEqual(typeof context, 'string');
|
|
|
|
|
|
2016-09-03 11:35:56 -07:00
|
|
|
var mailOptions = {
|
2016-09-03 12:02:42 -07:00
|
|
|
from: mailConfig().from,
|
2016-09-03 11:35:56 -07:00
|
|
|
to: 'support@cloudron.io',
|
|
|
|
|
subject: util.format('[%s] %s exited unexpectedly', config.fqdn(), program),
|
|
|
|
|
text: render('unexpected_exit.ejs', { fqdn: config.fqdn(), program: program, context: context, format: 'text' })
|
|
|
|
|
};
|
2016-08-02 14:40:47 -07:00
|
|
|
|
2016-09-03 11:35:56 -07:00
|
|
|
sendMails([ mailOptions ]);
|
2015-07-20 00:09:47 -07:00
|
|
|
}
|
2015-08-04 14:31:40 +02:00
|
|
|
|
2015-08-04 15:39:14 +02:00
|
|
|
function sendFeedback(user, type, subject, description) {
|
2015-08-04 14:31:40 +02:00
|
|
|
assert.strictEqual(typeof user, 'object');
|
|
|
|
|
assert.strictEqual(typeof type, 'string');
|
|
|
|
|
assert.strictEqual(typeof subject, 'string');
|
|
|
|
|
assert.strictEqual(typeof description, 'string');
|
|
|
|
|
|
2016-02-25 21:38:37 +01:00
|
|
|
assert(type === exports.FEEDBACK_TYPE_TICKET ||
|
|
|
|
|
type === exports.FEEDBACK_TYPE_FEEDBACK ||
|
|
|
|
|
type === exports.FEEDBACK_TYPE_APP_MISSING ||
|
2016-04-18 17:11:36 +02:00
|
|
|
type === exports.FEEDBACK_TYPE_UPGRADE_REQUEST ||
|
2016-02-25 21:38:37 +01:00
|
|
|
type === exports.FEEDBACK_TYPE_APP_ERROR);
|
2015-08-04 14:31:40 +02:00
|
|
|
|
|
|
|
|
var mailOptions = {
|
2016-09-03 12:02:42 -07:00
|
|
|
from: mailConfig().from,
|
2015-08-04 16:05:20 +02:00
|
|
|
to: 'support@cloudron.io',
|
2015-08-04 14:31:40 +02:00
|
|
|
subject: util.format('[%s] %s - %s', type, config.fqdn(), subject),
|
2015-08-04 14:56:43 +02:00
|
|
|
text: render('feedback.ejs', { fqdn: config.fqdn(), type: type, user: user, subject: subject, description: description, format: 'text'})
|
2015-08-04 14:31:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enqueue(mailOptions);
|
|
|
|
|
}
|
2016-01-18 14:00:35 +01:00
|
|
|
|
|
|
|
|
function _getMailQueue() {
|
|
|
|
|
return gMailQueue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 16:53:20 -08:00
|
|
|
function _clearMailQueue(callback) {
|
2016-01-18 14:00:35 +01:00
|
|
|
gMailQueue = [];
|
2016-02-08 16:53:20 -08:00
|
|
|
|
|
|
|
|
if (callback) callback();
|
2016-01-18 14:00:35 +01:00
|
|
|
}
|