diff --git a/domains/akshay.json b/domains/akshay.json index 4523d9f33..33bc64562 100644 --- a/domains/akshay.json +++ b/domains/akshay.json @@ -6,6 +6,10 @@ "email": "phenax5@gmail.com" }, "record": { - "CNAME": "phenax.github.io" + "CNAME": "phenax.github.io", + "MX": [ + "mx1.improvmx.com", + "mx2.improvmx.com" + ] } -} \ No newline at end of file +} diff --git a/utils/domain-service.js b/utils/domain-service.js index 14ebf3730..14112d484 100644 --- a/utils/domain-service.js +++ b/utils/domain-service.js @@ -24,11 +24,12 @@ const recordToZone = ({ name, type, address, id, priority }) => ({ const cleanName = name => name === DOMAIN_DOMAIN ? '@' : `${name}`.replace(new RegExp(`\\.${DOMAIN_DOMAIN}\\.?$`), '').toLowerCase(); -const zoneToRecord = ({ name, type, cname, address, record, line: id }) => ({ +const zoneToRecord = ({ name, type, cname, address, priority, preference, exchange, record, line: id }) => ({ id, name: cleanName(name), type: `${type}`, - address: `${cname || address || record}`.replace(/\.$/g, '').toLowerCase(), + address: `${exchange || cname || address || record}`.replace(/\.$/g, '').toLowerCase(), + priority: priority || preference, }); const redirectionToRecord = ({ domain, destination }) => ({ id: domain, @@ -37,6 +38,12 @@ const redirectionToRecord = ({ domain, destination }) => ({ address: `${destination}`.replace(/\/$/g, ''), }); +const recordToEmailMx = ({ name, address, priority }) => ({ + domain: `${name}.is-a.dev`, + exchanger: address, + priority, +}) + const getHostKey = host => `${host.name}##${host.type}##${host.address}`; const diffRecords = (oldRecords, newRecords) => { @@ -70,13 +77,18 @@ const getDomainService = ({ cpanel }) => { const fetchRedirections = R.compose(then(R.map(redirectionToRecord)), cpanel.redirection.fetch); const addZoneRecord = lazyTask(R.compose( - cpanel.zone.add, + R.ifElse(R.propEq('type', 'MX'), + R.compose(cpanel.email.add, recordToEmailMx), + cpanel.zone.add + ), recordToZone, print(({ name }) => `Adding zone for ${name}...`), )); const removeZoneRecord = lazyTask(R.compose( - cpanel.zone.remove, - R.pick(['line']), + R.ifElse(R.propEq('type', 'MX'), + R.compose(cpanel.email.remove, recordToEmailMx), + R.compose(cpanel.zone.remove, R.pick(['line'])) + ), recordToZone, print(({ name }) => `Deleting zone for ${name}...`), )); diff --git a/utils/lib/cpanel.js b/utils/lib/cpanel.js index 542d6c98f..29743295a 100644 --- a/utils/lib/cpanel.js +++ b/utils/lib/cpanel.js @@ -35,7 +35,7 @@ const CpanelClient = (options) => { return { zone: { // { customonly, domain } - // -> { cpanelresult: { data[{ class, ttl, name, line, Line, cname, type, record }] } } + // -> [{ class, ttl, name, line, Line, cname, type, record }] fetch: R.compose( p => p.then(R.pathOr([], ['cpanelresult', 'data'])), api2('ZoneEdit', 'fetchzone_records', { customonly: 1, domain: options.domain }) @@ -44,13 +44,14 @@ const CpanelClient = (options) => { // { name, type(A|CNAME), cname, address, ttl } // -> {} add: api2('ZoneEdit', 'add_zone_record', { domain: options.domain }), + // { line } // -> {} remove: api2('ZoneEdit', 'remove_zone_record', { domain: options.domain }), }, redirection: { // {} - // -> { domain, destination } + // -> [{ domain, destination }] fetch: R.compose( p => p.then(R.pathOr([], ['data'])), uapi('Mime', 'list_redirects'), @@ -59,6 +60,7 @@ const CpanelClient = (options) => { // { domain, redirect, type(permanent|tmp), redirect_wildcard(0|1), redirect(0|1|2) } // -> {} add: uapi('Mime', 'add_redirect'), + // { domain } // -> {} remove: uapi('Mime', 'delete_redirect'), @@ -66,6 +68,15 @@ const CpanelClient = (options) => { file: { write: uapi('Fileman', 'save_file_content', { from_charset: 'UTF-8', to_charset: 'UTF-8', fallback: 1 }), }, + email: { + // { domain, exchanger, priority } + // -> {} + add: uapi('Email', 'add_mx', { alwaysaccept: 'auto' }), + + // { domain, exchanger, priority } + // -> {} + remove: uapi('Email', 'delete_mx', { alwaysaccept: 'auto' }), + }, }; };