--- /dev/null
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+"""
+auf-esmtp-sizes — Vérification des tailles de messages ESMTP acceptées
+
+Copyright : Agence universitaire de la Francophonie — www.auf.org
+Licence : GNU General Public Licence, version 3
+Auteur : Jean Christophe André
+Date de création : 1 mars 2013
+
+Dépendance Debian : python-dnspython
+"""
+import sys
+import time
+import smtplib
+import aufrefer
+import dns.resolver
+
+SMTP_TIMEOUT = 60 # 5 suffit pour la moitié, 30 ne suffit pas pour tout le monde
+
+moinmoin = (len(sys.argv) > 1) and (sys.argv[1] == '--moinmoin')
+
+# on cherche le meilleur MX, ou sinon le domaine lui-même
+def get_mail_exchanger(domain):
+ mx_hostname = domain
+ mx_preference = 1000
+ for rdata in dns.resolver.query(domain + '.', 'MX'):
+ if rdata.preference < mx_preference:
+ mx_hostname = rdata.exchange.to_text()
+ mx_preference = rdata.preference
+ return mx_hostname
+
+# on contacte le MX en ESMTP pour obtenir sa taille limite
+def get_mx_size(mx):
+ try:
+ smtp = smtplib.SMTP(mx, timeout=SMTP_TIMEOUT)
+ smtp.ehlo_or_helo_if_needed()
+ except:
+ raise Exception("non joignable")
+ if not smtp.does_esmtp:
+ raise Exception("non compatible ESMTP ?!?")
+ try:
+ size = int(smtp.esmtp_features['size'])
+ except:
+ raise Exception("sans taille valide")
+ smtp.quit()
+ return size
+
+# liste des domaines internes de communication à l'AuF
+domains = [u['redir'].split('@')[1] for u in aufrefer.get('annuaire.json')]
+domains = [d for d in set(domains)]
+domains.sort()
+
+if moinmoin:
+ today = time.strftime('%d/%m/%Y')
+ print u"== Tailles acceptées pour les courriels au %s ==" % today
+ print u"||'''Domaine'''||'''Taille acceptée'''||"
+
+# pour chaque domaine, vérifier la taille sur le MX principal
+for domain in domains:
+ mx = get_mail_exchanger(domain)
+ #print "Test de '%s'..." % mx
+ try:
+ size = get_mx_size(mx)
+ except Exception, e:
+ if moinmoin:
+ print "||%s||''%s''||" % (domain, e.message)
+ else:
+ print "Domaine '%s' %s." % (domain, e.message)
+ continue
+ if moinmoin:
+ if size <= 0:
+ color = '#ff0000'
+ elif size <= 3*1024*1024:
+ color = '#00ff00'
+ elif size <= 10*1024*1024:
+ color = '#ffff00'
+ else:
+ color = '#ff0000'
+ print "||%s||<)%s>%s||" % (domain, color, size)
+ else:
+ print "Taille pour le domaine '%s' : %s" % (domain, size)
+