Files
cloudron-box/src/wellknown.js
T
2021-06-29 15:59:02 -07:00

47 lines
1.8 KiB
JavaScript

'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'),
util = require('util');
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
const getDomainFunc = util.callbackify(mail.getDomain);
getDomainFunc(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'));
}
}