-#!/usr/bin/python
+#!/usr/bin/python3
# -*- coding: utf-8 -*-
# https://developer.mozilla.org/fr/docs/Mozilla/Thunderbird/Autoconfiguration
from sys import stdout
import cgitb
cgitb.enable(display=0, logdir="/tmp")
-DSN = 'host=db-srv.auf user=XXX password=XXX dbname=AUF'
+ANNUAIRE_DSN = 'host=db-srv.auf user=XXX password=XXX dbname=AUF'
-info = {
- 'login_local': '%EMAILADDRESS%',
- 'serveur_bal': "imap.refer.org",
- 'domaine': "refer.org",
- 'shortname': 'REFER',
- 'name': 'REFER',
-}
+def get_info_for_email(email):
+ cnx = psycopg2.connect(ANNUAIRE_DSN)
+ cur = cnx.cursor(cursor_factory=psycopg2.extras.DictCursor)
+ cur.execute("""
+ SELECT login_local,serveur_bal,pays,redirection_courriel
+ FROM "Authentification".annuaire WHERE courriel=%s
+ """, (email,))
+ res = cur.fetchone()
+ if not res:
+ return {}
-form = cgi.FieldStorage()
-if form:
- email = form.getvalue('emailaddress')
- cnx = psycopg2.connect(DSN)
- cur = cnx.cursor(cursor_factory=psycopg2.extras.DictCursor)
- cur.execute("""SELECT login_local,serveur_bal,pays,redirection_courriel """
- """FROM "Authentification".annuaire WHERE courriel=%s""", (email,))
- res = cur.fetchall()
- if res:
- row = res[0]
- if row['login_local']:
- info['login_local'] = row['login_local']
- else:
- info['login_local'] = '%EMAILADDRESS%'
- info['serveur_bal'] = row['serveur_bal']
- info['redir'] = row['redirection_courriel']
- #info['domaine'] = row['pays'] + ".auf.org"
+ info = dict(res)
+ # cas général
+ if not info['login_local']:
+ info['login_local'] = email
+ info.setdefault('serveur_envoi', info['serveur_bal'])
info['domaine'] = "auf.org"
- info['shortname'] = 'AUF'
- info['name'] = 'Agence universitaire de la Francophonie'
-
-info['serveur_envoi'] = info['serveur_bal']
+ info['name'] = info['shortname'] = 'AUF'
+ #info['domaine'] = info['pays'] + ".auf.org"
-# Gestion des exceptions
+ # exception pour les SCP
+ if info['serveur_bal'] == 'mail.fr.auf.org':
+ if not info['login_local']:
+ info['login_local'] = email.partition('@')[0]
+ # exception pour les SCM
+ if info['serveur_bal'] == 'mail.ca.auf.org':
+ info['serveur_bal'] = 'imap.ca.auf.org'
+ if info['serveur_bal'] == 'imap.ca.auf.org':
+ info['serveur_envoi'] = 'smtp-sortant.ca.auf.org'
+ # exception pour Office 365
+ if info['redirection_courriel'].endswith('@auforg.onmicrosoft.com'):
+ info['serveur_bal'] = 'outlook.office365.com'
+ info['serveur_envoi'] = 'smtp.office365.com'
+ info['name'] = info['shortname'] = 'AUF365'
-# France
-if info['serveur_bal'] == 'mail.fr.auf.org':
- if row['login_local']:
- info['login_local'] = row['login_local']
- else:
- info['login_local'] = email.partition('@')[0]
+ return info
-# Canada
-if info['serveur_bal'] == 'mail.ca.auf.org':
- info['serveur_bal'] = 'imap.ca.auf.org'
-if info['serveur_bal'] == 'imap.ca.auf.org':
- info['serveur_envoi'] = 'smtp-sortant.ca.auf.org'
-# Office 365
-if info['redir'].endswith('@auforg.onmicrosoft.com'):
- info['serveur_bal'] = 'outlook.office365.com'
- info['serveur_envoi'] = 'smtp.office365.com'
- info['name'] = info['shortname'] = 'AUF365'
+# paramètres par défaut
+info = {
+ 'login_local': '%EMAILADDRESS%',
+ 'serveur_bal': 'imap.refer.org',
+ 'serveur_envoi': 'imap.refer.org',
+ 'domaine': 'refer.org',
+ 'shortname': 'REFER',
+ 'name': 'REFER',
+}
+# traitement des paramètres CGI
+form = cgi.FieldStorage()
+if form:
+ email = form.getvalue('emailaddress')
+ info.update(get_info_for_email(email))
# préparation de la réponse XML
data = """<?xml version="1.0" encoding="utf-8"?>
<clientConfig version="1.1">
<emailProvider id="auf.org">
- <domain>%(domaine)s</domain>
- <displayName>%(name)s</displayName>
- <displayShortName>%(shortname)s</displayShortName>
+ <domain>{domaine}</domain>
+ <displayName>{name}</displayName>
+ <displayShortName>{shortname}</displayShortName>
<incomingServer type="imap">
- <hostname>%(serveur_bal)s</hostname>
+ <hostname>{serveur_bal}</hostname>
<port>993</port>
<socketType>SSL</socketType>
<authentication>password-cleartext</authentication>
- <username>%(login_local)s</username>
+ <username>{login_local}</username>
</incomingServer>
<outgoingServer type="smtp">
- <hostname>%(serveur_envoi)s</hostname>
+ <hostname>{serveur_envoi}</hostname>
<port>587</port>
<socketType>STARTTLS</socketType>
<authentication>password-cleartext</authentication>
- <username>%(login_local)s</username>
+ <username>{login_local}</username>
</outgoingServer>
</emailProvider>
-</clientConfig>""" % info
+</clientConfig>""".format(**info)
# préparation des en-têtes de la réponse
headers = dict()
#headers['Vary'] = 'Content-Encoding'
# envoi de la réponse
-headers = ''.join(map(lambda x: "%s: %s\r\n" % (x, headers[x]), headers))
+headers = ''.join(map(lambda x: "{}: {}\r\n".format(x, headers[x]), headers))
stdout.write(headers + "\r\n")
if data:
stdout.write(data)
-