diff --git a/CHANGES b/CHANGES index 03408b4fa..9ea8572c7 100644 --- a/CHANGES +++ b/CHANGES @@ -2188,4 +2188,5 @@ [6.1.2] * App disk usage was not shown in graphs +* Email autoconfig diff --git a/src/autoconfig.xml.ejs b/src/autoconfig.xml.ejs new file mode 100644 index 000000000..ebfc7a5d6 --- /dev/null +++ b/src/autoconfig.xml.ejs @@ -0,0 +1,29 @@ + + + + <%= domain %> + Cloudron Mail + Cloudron + + <%= mailFqdn %> + 993 + SSL + password-cleartext + %EMAILADDRESS% + + + <%= mailFqdn %> + 587 + STARTTLS + password-cleartext + %EMAILADDRESS% + true + + + + Cloudron Email + + + + + diff --git a/src/routes/wellknown.js b/src/routes/wellknown.js index 40cba64c2..84c1286ea 100644 --- a/src/routes/wellknown.js +++ b/src/routes/wellknown.js @@ -4,18 +4,16 @@ exports = module.exports = { get }; -const domains = require('../domains.js'), - HttpError = require('connect-lastmile').HttpError; +const HttpError = require('connect-lastmile').HttpError, + wellknown = require('../wellknown.js'); function get(req, res, next) { const host = req.headers['host']; + const location = req.params[0]; - domains.get(host, function (error, domain) { + wellknown.get(host, location, function (error, result) { if (error) return next(new HttpError(404, error.message)); - const location = req.params[0]; - if (!domain.wellKnown || !(location in domain.wellKnown)) return next(new HttpError(404, 'No custom well-known config')); - - res.status(200).send(domain.wellKnown[location]); + res.status(200).set('content-type', result.type).send(result.body); }); } diff --git a/src/wellknown.js b/src/wellknown.js new file mode 100644 index 000000000..8c9345efa --- /dev/null +++ b/src/wellknown.js @@ -0,0 +1,43 @@ +'use strict'; + +exports = module.exports = { + get +}; + +const assert = require('assert'), + BoxError = require('./boxerror.js'), + domains = require('./domains.js'), + ejs = require('ejs'), + fs = require('fs'), + mail = require('./mail.js'), + settings = require('./settings.js'); + +const MAIL_AUTOCONFIG_EJS = fs.readFileSync(__dirname + '/autoconfig.xml.ejs', { encoding: 'utf8' }); + +function get(domain, location, callback) { + assert.strictEqual(typeof domain, 'string'); + assert.strictEqual(typeof location, 'string'); + assert.strictEqual(typeof callback, 'function'); + + if (location === 'autoconfig/mail/config-v1.1.xml') { // this also gets a ?emailaddress + mail.getDomain(domain, function (error, mailDomain) { + if (error) return callback(new BoxError(BoxError.NOT_FOUND, error.message)); + if (!mailDomain.enabled) return callback(new BoxError(BoxError.NOT_FOUND, 'Email not enabled')); + + const autoconfig = ejs.render(MAIL_AUTOCONFIG_EJS, { domain, mailFqdn: settings.mailFqdn() }); + + callback(null, { type: 'text/xml', body: autoconfig }); + }); + } else if (location === 'host-meta' || location === 'matrix/server') { + const type = location === 'host-meta' ? 'text/xml' : 'application/json'; + + domains.get(domain, function (error, domainObject) { + if (error) return callback(error); + if (!domainObject.wellKnown || !(location in domainObject.wellKnown)) return callback(new BoxError(BoxError.NOT_FOUND, 'No custom well-known config')); + + callback(null, { type, body: domainObject.wellKnown[location] }); + }); + } else { + callback(new BoxError(BoxError.NOT_FOUND, 'No custom well-known config')); + } +}