[mail-all.py] utilisation de postconf
darcs-hash:20091104101102-bd074-14a9eafb6c5765a5a9c6447282a0200363ea39b8.gz
This commit is contained in:
parent
d128700174
commit
21a036c1e0
1 changed files with 22 additions and 89 deletions
|
@ -1,80 +1,21 @@
|
|||
#! /usr/bin/env python
|
||||
# -*- encoding: utf-8 -*-
|
||||
# -*- coding: utf-8 -*-
|
||||
# Envoi d'un mail donné à certains adherents
|
||||
# Premier parametre : critere de recherche
|
||||
# Second parametre, fichier à envoyer
|
||||
|
||||
import smtplib
|
||||
import sys,os
|
||||
import ldap_crans
|
||||
import time
|
||||
import socket
|
||||
import smtplib, sys, os, ldap_crans, time, socket, commands
|
||||
from email_tools import format_sender
|
||||
from affich_tools import cprint
|
||||
|
||||
POSTFIX_CONF = "/etc/postfix/main.cf"
|
||||
LIMIT_CONF = "smtpd_client_message_rate_limit = 10\n"
|
||||
|
||||
def reload_postfix():
|
||||
def postconf(i):
|
||||
"Fixe la fréquence d'envoi maximale par client (en msg/min)"
|
||||
os.system("/usr/sbin/postconf -e smtpd_client_message_rate_limit=%s" % i)
|
||||
os.system("/etc/init.d/postfix reload")
|
||||
|
||||
def reconf_postfix():
|
||||
# Est-ce qu'on est sur rouge ?!
|
||||
if socket.gethostname() != "rouge":
|
||||
print u"Il y a trop de destinataires, il faut se logger sur redisdead pour lever\n(temporairement) la limite."
|
||||
sys.exit(1)
|
||||
|
||||
conf_fd = open (POSTFIX_CONF, 'r')
|
||||
lines = conf_fd.readlines()
|
||||
nlines = []
|
||||
conf_fd.close()
|
||||
|
||||
# On regarde s'il y a la limite dans le fichier de conf de postfix
|
||||
limit="no"
|
||||
for line in lines:
|
||||
if line == LIMIT_CONF:
|
||||
nlines+=u"## mail_all.py a commenté la ligne suivante"
|
||||
nlines+="# smtpd_client_message_rate_limit = 10\n"
|
||||
limit="yes"
|
||||
else:
|
||||
nlines+=line
|
||||
|
||||
if limit == "no":
|
||||
print u"[31mFatal! Il n'y a pas l'air d'y avoir de limite dans le fichier de conf de\npostfix[0m"
|
||||
sys.exit(1)
|
||||
|
||||
if limit == "yes":
|
||||
# Si oui, on demande à l'utilisateur si on y touche
|
||||
|
||||
negatif = ["N", "n", ""]
|
||||
positif = ["O", "o", "Y", "y"]
|
||||
|
||||
poursuivre = "x"
|
||||
while not (poursuivre in negatif + positif):
|
||||
poursuivre = raw_input("Il y a trop de destinataires, il faut réécrire la conf de postfix [o/N] ")
|
||||
|
||||
if poursuivre in negatif:
|
||||
print "Ok, on ne touche pas au fichier..."
|
||||
sys.exit(0)
|
||||
|
||||
assert (poursuivre in positif)
|
||||
|
||||
# On réécrit la conf
|
||||
try:
|
||||
nconf_fd = open (POSTFIX_CONF, "w")
|
||||
print u"Réécriture de la configuration de postfix"
|
||||
nconf_fd.writelines(nlines)
|
||||
nconf_fd.close ()
|
||||
except IOError:
|
||||
print u"Je n'arrive pas à réécrire la conf de postfix, es-tu root ?"
|
||||
sys.exit(1)
|
||||
|
||||
reload_postfix()
|
||||
|
||||
return(limit,lines)
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 3:
|
||||
print """Usage:
|
||||
cprint(u"""Usage:
|
||||
|
||||
Ce script permet d'envoyer un mail à toute une catégorie d'adhérents.
|
||||
|
||||
|
@ -98,28 +39,29 @@ par le script.
|
|||
donc contourner les limitations qui ont été mises en place au
|
||||
Cr@ns. Si plus de 10 mails doivent être envoyés, il faut le faire
|
||||
depuis rouge, et ce en root.
|
||||
"""
|
||||
|
||||
""")
|
||||
sys.exit(0)
|
||||
|
||||
# On en est là
|
||||
# On ouvre la base et on cherche
|
||||
adherents = ldap_crans.crans_ldap().search(sys.argv[1])['adherent']
|
||||
card= len(adherents)
|
||||
print "%d adhérent(s) a/ont été trouvé(s)..." % card
|
||||
cprint(u"%d adhérent(s) a/ont été trouvé(s)..." % len(adherents))
|
||||
time.sleep(3) # On dort un peu, ctrl-c welcome
|
||||
|
||||
limit="no"
|
||||
# if card >= 10:
|
||||
# (limit, backup_conf) = reconf_postfix()
|
||||
opt = commands.getoutput("postconf smtpd_client_message_rate_limit")
|
||||
limit = int(opt.split()[-1])
|
||||
if limit > 0 and len(adherents) >= 10:
|
||||
limit_risen = True
|
||||
postconf(0)
|
||||
else: limit_risen = False
|
||||
|
||||
# Il faudra quoiqu'il arrive rétablir la conf de postfix
|
||||
# try capture-t-il les SIGTERM ?!
|
||||
try:
|
||||
try:
|
||||
texte = "".join(file(sys.argv[2], "r").readlines())
|
||||
fichier = open(sys.argv[2])
|
||||
texte = fichier.read()
|
||||
fichier.close()
|
||||
except IOError:
|
||||
print "Impossible d'ouvrir le fichier à envoyer, merci, au revoir."
|
||||
cprint(u"Impossible d'ouvrir le fichier à envoyer, merci, au revoir.")
|
||||
sys.exit(1)
|
||||
|
||||
echecs = []
|
||||
|
@ -129,14 +71,14 @@ par le script.
|
|||
mail = adherent.mail().encode("iso-8859-15", "ignore")
|
||||
if "@" not in mail:
|
||||
mail = mail + "@crans.org"
|
||||
print "Envoi du mail à %s <%s>..." % (adherent.Nom().encode("iso-8859-15","ignore"), mail)
|
||||
cprint(u"Envoi du mail à %s <%s>..." % (adherent.Nom(), mail))
|
||||
try:
|
||||
recipient = format_sender(u'"%s" <%s>\n' % (adherent.Nom(), mail))
|
||||
s.sendmail('bulk+%s@crans.org' % mail.replace("@",'-at-'),
|
||||
(mail,),
|
||||
"To: %s\n%s" % (recipient, texte))
|
||||
except:
|
||||
print "Erreur lors de l'envoi à %s <%s>..." % (adherent.Nom().encode("iso-8859-15","ignore"), mail)
|
||||
cprint(u"Erreur lors de l'envoi à %s <%s>..." % (adherent.Nom(), mail), "rouge")
|
||||
echecs.append(mail)
|
||||
else:
|
||||
# Tout va bien
|
||||
|
@ -150,15 +92,6 @@ par le script.
|
|||
|
||||
# On rétablit la conf de postfix
|
||||
finally:
|
||||
if limit == "yes":
|
||||
try:
|
||||
conf_fd= open (POSTFIX_CONF, "w")
|
||||
print u"Restauration de la configuration de postfix"
|
||||
conf_fd.writelines (backup_conf)
|
||||
conf_fd.close()
|
||||
reload_postfix()
|
||||
except IOError:
|
||||
print "Could not rewrite postfix conf..."
|
||||
sys.exit(1)
|
||||
if limit_risen: postconf(limit)
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue