ansible-role-mail-autodiscover/files/app/__init__.py
2025-01-16 02:46:28 +01:00

52 lines
1.6 KiB
Python

import json
from flask import Flask, request, Response, abort, render_template
from defusedxml import ElementTree as ET
app = Flask(__name__)
with open('/etc/mail-autodiscover.json') as f:
settings = json.load(f)
@app.route('/autodiscover/autodiscover.xml', methods=['GET', 'POST'])
@app.route('/Autodiscover/Autodiscover.xml', methods=['GET', 'POST'])
def outlook_autodiscover():
data = request.get_data()
try:
root = ET.fromstring(data)
schema = root.find('./{*}Request/{*}AcceptableResponseSchema').text
email = root.find('./{*}Request/{*}EMailAddress').text
email_username = email.rsplit('@', 1)[0]
email_domain = email.rsplit('@', 1)[-1]
except (SyntaxError, ValueError):
schema = 'http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006'
email = ''
email_username = ''
email_domain = ''
ctx = settings.copy()
ctx['email'] = email
ctx['email_username'] = email_username
ctx['email_domain'] = email_domain
return Response(
render_template('autodiscover.xml', **ctx),
content_type='application/xml',
)
@app.route('/mail/config-v1.1.xml')
def thunderbird_autoconfig():
ctx = settings.copy()
ctx['email'] = request.args.get('email', '')
return Response(
render_template('autoconfig.xml', **ctx),
content_type='application/xml',
)
@app.route('/email.mobileconfig')
def apple_mobileconfig():
ctx = settings.copy()
ctx['email'] = request.args['email']
return Response(
render_template('mobileconfig.xml', **ctx),
content_type='application/x-apple-aspen-config; charset=utf-8',
headers={'Content-Disposition': 'attachment; filename="email.mobileconfig"'},
)