list2form : gestion des erreurs et des petits fichiers (merci cgi.py…)
[progfou.git] / thunderbird / autoconfig / autoconfig-mail
CommitLineData
aa308eb0 1#!/usr/bin/python3
3deec619
P
2# -*- coding: utf-8 -*-
3# https://developer.mozilla.org/fr/docs/Mozilla/Thunderbird/Autoconfiguration
4from sys import stdout
5import psycopg2
6import psycopg2.extras
7import cgi
8import cgitb
9cgitb.enable(display=0, logdir="/tmp")
10
aa308eb0 11ANNUAIRE_DSN = 'host=db-srv.auf user=XXX password=XXX dbname=AUF'
3deec619 12
aa308eb0
P
13def get_info_for_email(email):
14 cnx = psycopg2.connect(ANNUAIRE_DSN)
15 cur = cnx.cursor(cursor_factory=psycopg2.extras.DictCursor)
16 cur.execute("""
17 SELECT login_local,serveur_bal,pays,redirection_courriel
18 FROM "Authentification".annuaire WHERE courriel=%s
19 """, (email,))
20 res = cur.fetchone()
21 if not res:
22 return {}
3deec619 23
aa308eb0
P
24 info = dict(res)
25 # cas général
26 if not info['login_local']:
27 info['login_local'] = email
28 info.setdefault('serveur_envoi', info['serveur_bal'])
3deec619 29 info['domaine'] = "auf.org"
aa308eb0
P
30 info['name'] = info['shortname'] = 'AUF'
31 #info['domaine'] = info['pays'] + ".auf.org"
c5cbe432 32
aa308eb0
P
33 # exception pour les SCP
34 if info['serveur_bal'] == 'mail.fr.auf.org':
35 if not info['login_local']:
36 info['login_local'] = email.partition('@')[0]
37 # exception pour les SCM
38 if info['serveur_bal'] == 'mail.ca.auf.org':
39 info['serveur_bal'] = 'imap.ca.auf.org'
40 if info['serveur_bal'] == 'imap.ca.auf.org':
41 info['serveur_envoi'] = 'smtp-sortant.ca.auf.org'
42 # exception pour Office 365
43 if info['redirection_courriel'].endswith('@auforg.onmicrosoft.com'):
44 info['serveur_bal'] = 'outlook.office365.com'
45 info['serveur_envoi'] = 'smtp.office365.com'
46 info['name'] = info['shortname'] = 'AUF365'
c5cbe432 47
aa308eb0 48 return info
fb42639a 49
c5cbe432 50
aa308eb0
P
51# paramètres par défaut
52info = {
53 'login_local': '%EMAILADDRESS%',
54 'serveur_bal': 'imap.refer.org',
55 'serveur_envoi': 'imap.refer.org',
56 'domaine': 'refer.org',
57 'shortname': 'REFER',
58 'name': 'REFER',
59}
60# traitement des paramètres CGI
61form = cgi.FieldStorage()
62if form:
63 email = form.getvalue('emailaddress')
64 info.update(get_info_for_email(email))
c18df57f 65
fb42639a 66# préparation de la réponse XML
3deec619 67data = """<?xml version="1.0" encoding="utf-8"?>
c5cbe432 68<clientConfig version="1.1">
3deec619 69 <emailProvider id="auf.org">
aa308eb0
P
70 <domain>{domaine}</domain>
71 <displayName>{name}</displayName>
72 <displayShortName>{shortname}</displayShortName>
3deec619 73 <incomingServer type="imap">
aa308eb0 74 <hostname>{serveur_bal}</hostname>
3deec619
P
75 <port>993</port>
76 <socketType>SSL</socketType>
77 <authentication>password-cleartext</authentication>
aa308eb0 78 <username>{login_local}</username>
3deec619
P
79 </incomingServer>
80 <outgoingServer type="smtp">
aa308eb0 81 <hostname>{serveur_envoi}</hostname>
3deec619
P
82 <port>587</port>
83 <socketType>STARTTLS</socketType>
84 <authentication>password-cleartext</authentication>
aa308eb0 85 <username>{login_local}</username>
3deec619
P
86 </outgoingServer>
87 </emailProvider>
aa308eb0 88</clientConfig>""".format(**info)
3deec619 89
fb42639a 90# préparation des en-têtes de la réponse
3deec619
P
91headers = dict()
92headers['Content-Type'] = 'text/xml; charset=utf-8'
3deec619 93headers['Content-Length'] = len(data)
fb42639a
P
94headers['Pragma'] = 'no-cache'
95#headers['Vary'] = 'Content-Encoding'
3deec619
P
96
97# envoi de la réponse
aa308eb0 98headers = ''.join(map(lambda x: "{}: {}\r\n".format(x, headers[x]), headers))
3deec619
P
99stdout.write(headers + "\r\n")
100if data:
101 stdout.write(data)