Files
cloudron-box/src/routes/mail.js

346 lines
13 KiB
JavaScript
Raw Normal View History

2018-01-20 18:30:14 -08:00
'use strict';
exports = module.exports = {
2020-07-15 15:33:53 -07:00
getDomain,
2020-07-15 15:33:53 -07:00
getStatus,
2020-07-15 15:33:53 -07:00
setMailFromValidation,
setCatchAllAddress,
setMailRelay,
setMailEnabled,
setBanner,
2020-07-15 15:33:53 -07:00
sendTestMail,
2020-07-15 15:33:53 -07:00
listMailboxes,
getMailbox,
addMailbox,
updateMailbox,
removeMailbox,
2020-07-15 15:33:53 -07:00
getAliases,
setAliases,
2018-01-26 10:22:50 +01:00
2020-07-15 15:33:53 -07:00
getLists,
getList,
addList,
updateList,
removeList,
getMailboxCount
2018-01-20 18:30:14 -08:00
};
var assert = require('assert'),
2019-03-25 15:07:06 -07:00
auditSource = require('../auditsource.js'),
2019-10-24 13:34:14 -07:00
BoxError = require('../boxerror.js'),
mail = require('../mail.js'),
2018-01-20 18:30:14 -08:00
HttpError = require('connect-lastmile').HttpError,
2020-02-11 22:10:57 -08:00
HttpSuccess = require('connect-lastmile').HttpSuccess;
2018-01-20 18:30:14 -08:00
2018-04-03 14:37:52 -07:00
function getDomain(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
2018-04-03 14:37:52 -07:00
mail.getDomain(req.params.domain, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
2018-01-20 18:30:14 -08:00
2019-02-15 10:55:15 -08:00
next(new HttpSuccess(200, mail.removePrivateFields(result)));
2018-01-20 18:30:14 -08:00
});
}
function getStatus(req, res, next) {
2018-01-20 23:17:39 -08:00
assert.strictEqual(typeof req.params.domain, 'string');
2018-03-08 09:32:06 -08:00
// can take a while to query all the DNS entries
req.clearTimeout();
2018-01-20 23:17:39 -08:00
mail.getStatus(req.params.domain, function (error, records) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, records));
});
}
function setMailFromValidation(req, res, next) {
2018-01-20 23:17:39 -08:00
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled is required'));
2018-01-20 23:17:39 -08:00
mail.setMailFromValidation(req.params.domain, req.body.enabled, function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
}
function setCatchAllAddress(req, res, next) {
2018-01-20 23:17:39 -08:00
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (!req.body.addresses) return next(new HttpError(400, 'addresses is required'));
if (!Array.isArray(req.body.addresses)) return next(new HttpError(400, 'addresses must be an array of strings'));
for (var i = 0; i < req.body.addresses.length; i++) {
if (typeof req.body.addresses[i] !== 'string') return next(new HttpError(400, 'addresses must be an array of strings'));
}
mail.setCatchAllAddress(req.params.domain, req.body.addresses, function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
}
function setMailRelay(req, res, next) {
2018-01-20 23:17:39 -08:00
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
if ('host' in req.body && typeof req.body.host !== 'string') return next(new HttpError(400, 'host must be a string'));
if ('port' in req.body && typeof req.body.port !== 'number') return next(new HttpError(400, 'port must be a string'));
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username must be a string'));
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));
if ('acceptSelfSignedCerts' in req.body && typeof req.body.acceptSelfSignedCerts !== 'boolean') return next(new HttpError(400, 'acceptSelfSignedCerts must be a boolean'));
2018-01-20 23:17:39 -08:00
mail.setMailRelay(req.params.domain, req.body, function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
}
2018-01-20 23:17:39 -08:00
function setMailEnabled(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled is required'));
2019-03-25 15:07:06 -07:00
mail.setMailEnabled(req.params.domain, !!req.body.enabled, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
}
function sendTestMail(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (!req.body.to || typeof req.body.to !== 'string') return next(new HttpError(400, 'to must be a non-empty string'));
mail.sendTestMail(req.params.domain, req.body.to, function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
}
2018-12-06 10:23:10 -08:00
function listMailboxes(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
2019-10-22 10:11:35 -07:00
var page = typeof req.query.page !== 'undefined' ? parseInt(req.query.page) : 1;
2020-02-07 14:11:52 -08:00
if (!page || page < 0) return next(new HttpError(400, 'page query param has to be a positive number'));
2019-10-22 10:11:35 -07:00
var perPage = typeof req.query.per_page !== 'undefined'? parseInt(req.query.per_page) : 25;
2020-02-07 14:11:52 -08:00
if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a positive number'));
2019-10-22 10:11:35 -07:00
if (req.query.search && typeof req.query.search !== 'string') return next(new HttpError(400, 'search must be a string'));
mail.listMailboxes(req.params.domain, req.query.search || null, page, perPage, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { mailboxes: result }));
});
}
2020-07-15 15:33:53 -07:00
function getMailboxCount(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
mail.getMailboxCount(req.params.domain, function (error, count) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { count }));
});
}
function getMailbox(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
mail.getMailbox(req.params.name, req.params.domain, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { mailbox: result }));
});
}
function addMailbox(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be a string'));
2020-11-12 23:25:33 -08:00
if (typeof req.body.ownerId !== 'string') return next(new HttpError(400, 'ownerId must be a string'));
if (typeof req.body.ownerType !== 'string') return next(new HttpError(400, 'ownerType must be a string'));
if (typeof req.body.active !== 'boolean') return next(new HttpError(400, 'active must be a boolean'));
mail.addMailbox(req.body.name, req.params.domain, req.body, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, {}));
});
}
function updateMailbox(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
2020-11-12 23:25:33 -08:00
if (typeof req.body.ownerId !== 'string') return next(new HttpError(400, 'ownerId must be a string'));
if (typeof req.body.ownerType !== 'string') return next(new HttpError(400, 'ownerType must be a string'));
if (typeof req.body.active !== 'boolean') return next(new HttpError(400, 'active must be a boolean'));
mail.updateMailbox(req.params.name, req.params.domain, req.body, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
}
function removeMailbox(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
if (typeof req.body.deleteMails !== 'boolean') return next(new HttpError(400, 'deleteMails must be a boolean'));
mail.removeMailbox(req.params.name, req.params.domain, req.body, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, {}));
});
}
function getAliases(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
mail.getAliases(req.params.name, req.params.domain, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { aliases: result }));
});
}
function setAliases(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
assert.strictEqual(typeof req.body, 'object');
if (!Array.isArray(req.body.aliases)) return next(new HttpError(400, 'aliases must be an array'));
for (let alias of req.body.aliases) {
if (!alias || typeof alias !== 'object') return next(new HttpError(400, 'each alias must have a name and domain'));
if (typeof alias.name !== 'string') return next(new HttpError(400, 'name must be a string'));
if (typeof alias.domain !== 'string') return next(new HttpError(400, 'domain must be a string'));
}
mail.setAliases(req.params.name, req.params.domain, req.body.aliases, function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
}
2018-01-26 10:22:50 +01:00
function setBanner(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.text !== 'string') return res.status(400).send({ message: 'text must be a string' });
if ('html' in req.body && typeof req.body.html !== 'string') return res.status(400).send({ message: 'html must be a string' });
mail.setBanner(req.params.domain, { text: req.body.text, html: req.body.html || null }, function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
}
2018-01-26 10:22:50 +01:00
function getLists(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
const page = typeof req.query.page !== 'undefined' ? parseInt(req.query.page) : 1;
if (!page || page < 0) return next(new HttpError(400, 'page query param has to be a positive number'));
const perPage = typeof req.query.per_page !== 'undefined'? parseInt(req.query.per_page) : 25;
if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a positive number'));
if (req.query.search && typeof req.query.search !== 'string') return next(new HttpError(400, 'search must be a string'));
mail.getLists(req.params.domain, req.query.search || null, page, perPage, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
2018-01-26 10:22:50 +01:00
next(new HttpSuccess(200, { lists: result }));
});
}
function getList(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
2018-01-26 10:22:50 +01:00
2020-01-24 16:54:14 -08:00
mail.getList(req.params.name, req.params.domain, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
2018-01-26 10:22:50 +01:00
next(new HttpSuccess(200, { list: result }));
});
}
function addList(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be a string'));
if (!Array.isArray(req.body.members)) return next(new HttpError(400, 'members must be a string'));
2019-09-11 14:09:36 -07:00
if (req.body.members.length === 0) return next(new HttpError(400, 'list must have atleast one member'));
2018-01-26 10:22:50 +01:00
for (var i = 0; i < req.body.members.length; i++) {
if (typeof req.body.members[i] !== 'string') return next(new HttpError(400, 'member must be a string'));
}
2020-04-17 16:55:23 -07:00
if (typeof req.body.membersOnly !== 'boolean') return next(new HttpError(400, 'membersOnly must be a boolean'));
if (typeof req.body.active !== 'boolean') return next(new HttpError(400, 'active must be a boolean'));
mail.addList(req.body.name, req.params.domain, req.body, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
2018-01-26 10:22:50 +01:00
next(new HttpSuccess(201, {}));
});
}
function updateList(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
if (!Array.isArray(req.body.members)) return next(new HttpError(400, 'members must be a string'));
2019-09-11 14:09:36 -07:00
if (req.body.members.length === 0) return next(new HttpError(400, 'list must have atleast one member'));
for (var i = 0; i < req.body.members.length; i++) {
if (typeof req.body.members[i] !== 'string') return next(new HttpError(400, 'member must be a string'));
}
2020-04-17 16:55:23 -07:00
if (typeof req.body.membersOnly !== 'boolean') return next(new HttpError(400, 'membersOnly must be a boolean'));
if (typeof req.body.active !== 'boolean') return next(new HttpError(400, 'active must be a boolean'));
mail.updateList(req.params.name, req.params.domain, req.body, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
}
2018-01-26 10:22:50 +01:00
function removeList(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
2018-01-26 10:22:50 +01:00
2019-03-25 15:07:06 -07:00
mail.removeList(req.params.name, req.params.domain, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
2018-01-26 10:22:50 +01:00
2018-01-30 12:22:55 +01:00
next(new HttpSuccess(204));
2018-01-26 10:22:50 +01:00
});
}