diff --git a/src/mail.js b/src/mail.js index 43f002531..69b631e8b 100644 --- a/src/mail.js +++ b/src/mail.js @@ -6,6 +6,9 @@ exports = module.exports = { get: get, getAll: getAll, + add: add, + del: del, + setMailFromValidation: setMailFromValidation, setCatchAllAddress: setCatchAllAddress, setMailRelay: setMailRelay, @@ -553,6 +556,31 @@ function getAll(callback) { }); } +function add(domain, callback) { + assert.strictEqual(typeof domain, 'string'); + assert.strictEqual(typeof callback, 'function'); + + maildb.add(domain, function (error) { + if (error && error.reason === DatabaseError.ALREADY_EXISTS) return callback(new MailError(MailError.ALREADY_EXISTS, error.message)); + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new MailError(MailError.NOT_FOUND, error.message)); + if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); + + callback(); + }); +} + +function del(domain, callback) { + assert.strictEqual(typeof domain, 'string'); + assert.strictEqual(typeof callback, 'function'); + + maildb.del(domain, function (error) { + if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new MailError(MailError.NOT_FOUND, error.message)); + if (error) return callback(new MailError(MailError.INTERNAL_ERROR, error)); + + callback(); + }); +} + function setMailFromValidation(domain, enabled, callback) { assert.strictEqual(typeof domain, 'string'); assert.strictEqual(typeof enabled, 'boolean'); diff --git a/src/routes/mail.js b/src/routes/mail.js index ca4163668..ed876b637 100644 --- a/src/routes/mail.js +++ b/src/routes/mail.js @@ -3,6 +3,9 @@ exports = module.exports = { get: get, + add: add, + del: del, + getStatus: getStatus, setMailFromValidation: setMailFromValidation, @@ -35,6 +38,29 @@ function get(req, res, next) { }); } +function add(req, res, next) { + assert.strictEqual(typeof req.params.domain, 'string'); + + mail.add(req.params.domain, function (error, result) { + if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message)); + if (error && error.reason === MailError.ALREADY_EXISTS) return next(new HttpError(400, error.message)); + if (error) return next(new HttpError(500, error)); + + next(new HttpSuccess(200, result)); + }); +} + +function del(req, res, next) { + assert.strictEqual(typeof req.params.domain, 'string'); + + mail.del(req.params.domain, 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, { })); + }); +} + function getStatus(req, res, next) { assert.strictEqual(typeof req.params.domain, 'string'); diff --git a/src/server.js b/src/server.js index f080a380c..9df063c82 100644 --- a/src/server.js +++ b/src/server.js @@ -213,6 +213,8 @@ function initializeExpressSync() { // email routes router.get ('/api/v1/mail/:domain', settingsScope, routes.user.requireAdmin, routes.mail.get); + router.post('/api/v1/mail/:domain', settingsScope, routes.user.requireAdmin, routes.mail.add); + router.del('/api/v1/mail/:domain', settingsScope, routes.user.requireAdmin, routes.mail.del); router.get ('/api/v1/mail/:domain/status', settingsScope, routes.user.requireAdmin, routes.mail.getStatus); router.post('/api/v1/mail/:domain/mail_from_validation', settingsScope, routes.user.requireAdmin, routes.mail.setMailFromValidation); router.post('/api/v1/mail/:domain/catch_all', settingsScope, routes.user.requireAdmin, routes.mail.setCatchAllAddress);