2018-01-20 18:30:14 -08:00
'use strict' ;
exports = module . exports = {
2020-07-15 15:33:53 -07:00
getDomain ,
2018-01-24 21:15:58 -08:00
2020-07-15 15:33:53 -07:00
getStatus ,
2018-01-20 18:56:17 -08:00
2020-07-15 15:33:53 -07:00
setMailFromValidation ,
setCatchAllAddress ,
setMailRelay ,
setMailEnabled ,
2020-08-23 14:33:58 -07:00
setBanner ,
2018-01-23 16:10:23 -08:00
2020-07-15 15:33:53 -07:00
sendTestMail ,
2018-01-24 13:11:35 +01:00
2020-07-15 15:33:53 -07:00
listMailboxes ,
getMailbox ,
addMailbox ,
updateMailbox ,
removeMailbox ,
2018-01-25 18:03:02 +01:00
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
} ;
2018-01-20 18:56:17 -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' ) ,
2018-01-20 18:56:17 -08:00
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 ) {
2018-01-20 22:56:45 -08:00
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
2018-04-03 14:37:52 -07:00
mail . getDomain ( req . params . domain , function ( error , result ) {
2019-10-24 18:05:14 -07:00
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
} ) ;
}
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' ) ;
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 ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-20 18:56:17 -08:00
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 ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-20 18:56:17 -08:00
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' ) ;
2018-04-12 12:35:56 +02:00
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' ) ) ;
2018-01-20 18:56:17 -08:00
2018-04-12 12:35:56 +02:00
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' ) ) ;
2018-01-20 18:56:17 -08:00
}
2018-04-12 12:35:56 +02:00
mail . setCatchAllAddress ( req . params . domain , req . body . addresses , function ( error ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-20 18:56:17 -08:00
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' ) ) ;
2019-04-23 15:19:33 -07:00
if ( 'acceptSelfSignedCerts' in req . body && typeof req . body . acceptSelfSignedCerts !== 'boolean' ) return next ( new HttpError ( 400 , 'acceptSelfSignedCerts must be a boolean' ) ) ;
2018-01-20 18:56:17 -08:00
2018-01-20 23:17:39 -08:00
mail . setMailRelay ( req . params . domain , req . body , function ( error ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-20 18:56:17 -08:00
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' ) ) ;
2019-03-25 15:07:06 -07:00
mail . setMailEnabled ( req . params . domain , ! ! req . body . enabled , auditSource . fromRequest ( req ) , function ( error ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-20 18:56:17 -08:00
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 ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-23 16:10:23 -08:00
next ( new HttpSuccess ( 202 ) ) ;
} ) ;
}
2018-01-24 13:11:35 +01:00
2018-12-06 10:23:10 -08:00
function listMailboxes ( req , res , next ) {
2018-01-24 13:11:35 +01:00
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
2020-07-05 11:23:53 -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 ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-24 13:11:35 +01:00
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 } ) ) ;
} ) ;
}
2018-04-03 12:18:26 -07:00
function getMailbox ( req , res , next ) {
2018-01-24 13:11:35 +01:00
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
2018-04-03 12:18:26 -07:00
assert . strictEqual ( typeof req . params . name , 'string' ) ;
2018-01-24 13:11:35 +01:00
2018-04-03 12:18:26 -07:00
mail . getMailbox ( req . params . name , req . params . domain , function ( error , result ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-24 13:11:35 +01:00
next ( new HttpSuccess ( 200 , { mailbox : result } ) ) ;
} ) ;
}
2018-04-03 12:18:26 -07:00
function addMailbox ( req , res , next ) {
2018-01-24 13:11:35 +01:00
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
2018-04-03 12:18:26 -07:00
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' ) ) ;
2021-04-14 22:37:01 -07:00
if ( typeof req . body . active !== 'boolean' ) return next ( new HttpError ( 400 , 'active must be a boolean' ) ) ;
2018-04-03 12:18:26 -07:00
2021-04-14 22:37:01 -07:00
mail . addMailbox ( req . body . name , req . params . domain , req . body , auditSource . fromRequest ( req ) , function ( error ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-24 13:11:35 +01:00
next ( new HttpSuccess ( 201 , { } ) ) ;
} ) ;
}
2018-04-03 14:12:43 -07:00
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' ) ) ;
2021-04-14 22:37:01 -07:00
if ( typeof req . body . active !== 'boolean' ) return next ( new HttpError ( 400 , 'active must be a boolean' ) ) ;
2018-04-03 14:12:43 -07:00
2021-04-14 22:37:01 -07:00
mail . updateMailbox ( req . params . name , req . params . domain , req . body , auditSource . fromRequest ( req ) , function ( error ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-04-03 14:12:43 -07:00
next ( new HttpSuccess ( 204 ) ) ;
} ) ;
}
2018-04-03 12:18:26 -07:00
function removeMailbox ( req , res , next ) {
2018-01-24 13:11:35 +01:00
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
2018-04-03 12:18:26 -07:00
assert . strictEqual ( typeof req . params . name , 'string' ) ;
2018-01-24 13:11:35 +01:00
2020-07-27 22:26:10 -07:00
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 ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-24 13:11:35 +01:00
next ( new HttpSuccess ( 201 , { } ) ) ;
} ) ;
}
2018-01-25 18:03:02 +01:00
2018-04-03 12:18:26 -07:00
function getAliases ( req , res , next ) {
2018-04-01 18:23:54 +02:00
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
2018-04-03 12:18:26 -07:00
assert . strictEqual ( typeof req . params . name , 'string' ) ;
2018-01-25 18:03:02 +01:00
2018-04-03 12:18:26 -07:00
mail . getAliases ( req . params . name , req . params . domain , function ( error , result ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-25 18:03:02 +01:00
next ( new HttpSuccess ( 200 , { aliases : result } ) ) ;
} ) ;
}
2018-04-03 12:18:26 -07:00
function setAliases ( req , res , next ) {
2018-01-25 18:03:02 +01:00
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
2018-04-03 12:18:26 -07:00
assert . strictEqual ( typeof req . params . name , 'string' ) ;
2018-01-25 18:03:02 +01:00
assert . strictEqual ( typeof req . body , 'object' ) ;
if ( ! Array . isArray ( req . body . aliases ) ) return next ( new HttpError ( 400 , 'aliases must be an array' ) ) ;
2020-04-19 18:44:16 -07:00
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' ) ) ;
2018-01-25 18:03:02 +01:00
}
2018-04-03 12:18:26 -07:00
mail . setAliases ( req . params . name , req . params . domain , req . body . aliases , function ( error ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-25 18:03:02 +01:00
next ( new HttpSuccess ( 202 ) ) ;
} ) ;
}
2018-01-26 10:22:50 +01:00
2020-08-23 14:33:58 -07: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' ) ;
2020-07-05 10:36:17 -07:00
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' ) ) ;
2020-07-05 11:23:53 -07:00
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 ) {
2019-10-24 18:05:14 -07:00
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' ) ;
2018-04-03 09:34:33 -07:00
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 ) {
2019-10-24 18:05:14 -07:00
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' ) ;
2018-04-03 09:34:33 -07:00
if ( typeof req . body . name !== 'string' ) return next ( new HttpError ( 400 , 'name must be a string' ) ) ;
2018-04-05 15:46:13 -07:00
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
2018-04-05 15:46:13 -07: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' ) ) ;
2021-04-14 22:37:01 -07:00
if ( typeof req . body . active !== 'boolean' ) return next ( new HttpError ( 400 , 'active must be a boolean' ) ) ;
2018-04-05 15:46:13 -07:00
2021-04-14 22:37:01 -07:00
mail . addList ( req . body . name , req . params . domain , req . body , auditSource . fromRequest ( req ) , function ( error ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-01-26 10:22:50 +01:00
next ( new HttpSuccess ( 201 , { } ) ) ;
} ) ;
}
2018-04-03 14:12:43 -07:00
function updateList ( req , res , next ) {
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
assert . strictEqual ( typeof req . params . name , 'string' ) ;
2018-04-05 15:46:13 -07:00
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-04-05 15:46:13 -07: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' ) ) ;
2021-04-14 22:37:01 -07:00
if ( typeof req . body . active !== 'boolean' ) return next ( new HttpError ( 400 , 'active must be a boolean' ) ) ;
2018-04-03 14:12:43 -07:00
2021-04-14 22:37:01 -07:00
mail . updateList ( req . params . name , req . params . domain , req . body , auditSource . fromRequest ( req ) , function ( error ) {
2019-10-24 18:05:14 -07:00
if ( error ) return next ( BoxError . toHttpError ( error ) ) ;
2018-04-03 14:12:43 -07:00
next ( new HttpSuccess ( 204 ) ) ;
} ) ;
}
2018-01-26 10:22:50 +01:00
function removeList ( req , res , next ) {
assert . strictEqual ( typeof req . params . domain , 'string' ) ;
2018-04-03 09:34:33 -07:00
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 ) {
2019-10-24 18:05:14 -07:00
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
} ) ;
}