2018-01-20 18:30:14 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2018-01-20 22:56:45 -08:00
|
|
|
get: get,
|
|
|
|
|
|
2018-01-20 18:56:17 -08:00
|
|
|
getStatus: getStatus,
|
|
|
|
|
|
|
|
|
|
setMailFromValidation: setMailFromValidation,
|
|
|
|
|
setCatchAllAddress: setCatchAllAddress,
|
|
|
|
|
setMailRelay: setMailRelay,
|
2018-01-23 16:10:23 -08:00
|
|
|
setMailEnabled: setMailEnabled,
|
|
|
|
|
|
2018-01-24 13:11:35 +01:00
|
|
|
sendTestMail: sendTestMail,
|
|
|
|
|
|
|
|
|
|
getMailboxes: getMailboxes,
|
|
|
|
|
getUserMailbox: getUserMailbox,
|
|
|
|
|
enableUserMailbox: enableUserMailbox,
|
|
|
|
|
disableUserMailbox: disableUserMailbox
|
2018-01-20 18:30:14 -08:00
|
|
|
};
|
|
|
|
|
|
2018-01-20 18:56:17 -08:00
|
|
|
var assert = require('assert'),
|
|
|
|
|
mail = require('../mail.js'),
|
|
|
|
|
MailError = mail.MailError,
|
2018-01-20 18:30:14 -08:00
|
|
|
HttpError = require('connect-lastmile').HttpError,
|
|
|
|
|
HttpSuccess = require('connect-lastmile').HttpSuccess;
|
|
|
|
|
|
2018-01-20 22:56:45 -08:00
|
|
|
function get(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
|
|
|
|
|
mail.get(req.params.domain, function (error, result) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:30:14 -08:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-01-20 22:56:45 -08:00
|
|
|
next(new HttpSuccess(200, result));
|
2018-01-20 18:30:14 -08:00
|
|
|
});
|
|
|
|
|
}
|
2018-01-20 18:56:17 -08:00
|
|
|
|
2018-01-20 22:56:45 -08:00
|
|
|
function getStatus(req, res, next) {
|
2018-01-20 23:17:39 -08:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
|
|
|
|
|
mail.getStatus(req.params.domain, function (error, records) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:56:17 -08:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-01-20 22:56:45 -08:00
|
|
|
next(new HttpSuccess(200, records));
|
2018-01-20 18:56:17 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setMailFromValidation(req, res, next) {
|
2018-01-20 23:17:39 -08:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-01-20 18:56:17 -08:00
|
|
|
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 && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:56:17 -08:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, 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');
|
2018-01-20 18:56:17 -08:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (!req.body.address || !Array.isArray(req.body.address)) return next(new HttpError(400, 'address array is required'));
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < req.body.address.length; i++) {
|
|
|
|
|
if (typeof req.body.address[i] !== 'string') return next(new HttpError(400, 'address must be an array of string'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-20 23:17:39 -08:00
|
|
|
mail.setCatchAllAddress(req.params.domain, req.body.address, function (error) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:56:17 -08:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, 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');
|
2018-01-20 18:56:17 -08:00
|
|
|
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'));
|
|
|
|
|
|
2018-01-20 23:17:39 -08:00
|
|
|
mail.setMailRelay(req.params.domain, req.body, function (error) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:56:17 -08:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, 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');
|
2018-01-20 18:56:17 -08:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled is required'));
|
|
|
|
|
|
2018-01-21 00:40:30 -08:00
|
|
|
mail.setMailEnabled(req.params.domain, !!req.body.enabled, function (error) {
|
2018-01-20 23:17:39 -08:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:56:17 -08:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(202));
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-01-23 16:10:23 -08:00
|
|
|
|
|
|
|
|
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 && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(202));
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-01-24 13:11:35 +01:00
|
|
|
|
|
|
|
|
function getMailboxes(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
|
|
|
|
|
mail.getMailboxes(req.params.domain, function (error, result) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { mailboxes: result }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getUserMailbox(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof req.params.userId, 'string');
|
|
|
|
|
|
|
|
|
|
mail.getUserMailbox(req.params.domain, req.params.userId, function (error, result) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { mailbox: result }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function enableUserMailbox(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof req.params.userId, 'string');
|
|
|
|
|
|
|
|
|
|
mail.enableUserMailbox(req.params.domain, req.params.userId, function (error) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(201, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function disableUserMailbox(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof req.params.userId, 'string');
|
|
|
|
|
|
|
|
|
|
mail.disableUserMailbox(req.params.domain, req.params.userId, function (error) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(201, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|