2012-11-26 15:14:47 +01:00
|
|
|
#!/usr/bin/python2
|
|
|
|
import subprocess
|
|
|
|
import sys
|
2018-11-21 16:13:19 +01:00
|
|
|
MAILMAN_HOME = "/usr/lib/mailman/bin"
|
|
|
|
MAILING_PREFIX = "noel"
|
|
|
|
ADMIN_MAIL = "mathieu@mathux.org"
|
|
|
|
ADMIN_PWD = ""
|
|
|
|
|
2012-11-26 15:14:47 +01:00
|
|
|
|
|
|
|
def getListUser(listName):
|
|
|
|
cmd = MAILMAN_HOME+"/list_members "+listName
|
|
|
|
proc = subprocess.Popen(cmd,
|
|
|
|
shell=True,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
(stdout, stderr) = proc.communicate()
|
|
|
|
ret = proc.returncode
|
|
|
|
if ret != 0:
|
2018-11-21 16:13:19 +01:00
|
|
|
print "error when checking list %s : %s" % (listName, stderr)
|
2012-11-26 15:14:47 +01:00
|
|
|
return []
|
|
|
|
return stdout.split('\n')
|
|
|
|
|
|
|
|
|
2018-11-21 16:13:19 +01:00
|
|
|
def getMailingLists(prefix):
|
2012-11-26 15:14:47 +01:00
|
|
|
cmd = MAILMAN_HOME+"/list_lists -b"
|
|
|
|
proc = subprocess.Popen(cmd,
|
|
|
|
shell=True,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
(stdout, stderr) = proc.communicate()
|
|
|
|
ret = proc.returncode
|
|
|
|
if ret != 0:
|
2018-11-21 16:13:19 +01:00
|
|
|
print "error when getting mailing lists : %s" % stderr
|
2012-11-26 15:14:47 +01:00
|
|
|
sys.exit(1)
|
2018-11-21 16:13:19 +01:00
|
|
|
lists = []
|
2012-11-26 15:14:47 +01:00
|
|
|
for line in stdout.split('\n'):
|
|
|
|
if line.startswith(prefix):
|
|
|
|
lists.append(line)
|
|
|
|
return lists
|
|
|
|
|
2018-11-21 16:13:19 +01:00
|
|
|
|
2012-11-26 15:14:47 +01:00
|
|
|
def createList(name):
|
2018-11-21 16:13:19 +01:00
|
|
|
cmd = MAILMAN_HOME + \
|
|
|
|
"/newlist -a -l fr %s %s %s" % (name, ADMIN_MAIL, ADMIN_PWD)
|
2012-11-26 15:14:47 +01:00
|
|
|
proc = subprocess.Popen(cmd,
|
|
|
|
shell=True,
|
|
|
|
stdin=sys.stdin,
|
|
|
|
stdout=sys.stdout,
|
|
|
|
stderr=sys.stderr)
|
|
|
|
|
|
|
|
(stdout, stderr) = proc.communicate()
|
|
|
|
ret = proc.returncode
|
|
|
|
if ret != 0:
|
2018-11-21 16:13:19 +01:00
|
|
|
print "error when creating mailing lists : %s" % stderr
|
2012-11-26 15:14:47 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
2018-11-21 16:13:19 +01:00
|
|
|
|
2012-11-26 15:14:47 +01:00
|
|
|
def listContainsUser(user, listName):
|
|
|
|
return user in getListUser(listName)
|
|
|
|
|
|
|
|
|
|
|
|
def addUserToList(userEmail, listName):
|
|
|
|
if listContainsUser(userEmail, listName):
|
|
|
|
return
|
2018-11-21 16:13:19 +01:00
|
|
|
print "Add %s to list %s" % (userEmail, listName)
|
2012-11-26 15:14:47 +01:00
|
|
|
cmd = MAILMAN_HOME+"/add_members -w n -r - "+listName
|
|
|
|
proc = subprocess.Popen(cmd,
|
|
|
|
shell=True,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
(stdout, stderr) = proc.communicate(input=""+userEmail)
|
|
|
|
ret = proc.returncode
|
|
|
|
if ret != 0:
|
2018-11-21 16:13:19 +01:00
|
|
|
print "error when adding user %s to list %s : %s" % (userEmail, listName, stderr)
|
2012-11-26 15:14:47 +01:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
def delUserFromList(userEmail, listName):
|
|
|
|
if not listContainsUser(userEmail, listName):
|
|
|
|
return
|
2018-11-21 16:13:19 +01:00
|
|
|
print "Remove %s from list %s" % (userEmail, listName)
|
2012-11-26 15:14:47 +01:00
|
|
|
cmd = MAILMAN_HOME+"/remove_members "+listName+" "+userEmail
|
|
|
|
proc = subprocess.Popen(cmd,
|
|
|
|
shell=True,
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.PIPE)
|
|
|
|
|
|
|
|
(stdout, stderr) = proc.communicate()
|
|
|
|
ret = proc.returncode
|
|
|
|
|
2016-10-24 23:58:14 +02:00
|
|
|
|
2018-11-21 16:13:19 +01:00
|
|
|
# --------------------MAIN--------------------
|
2012-11-26 15:14:47 +01:00
|
|
|
if len(sys.argv) < 3:
|
2018-11-21 16:13:19 +01:00
|
|
|
print "Usage %s nom email [ancien_email]" % sys.argv[0]
|
|
|
|
print "nom : nom de la mailing list de l'user sans prefix (ex: celine et pas %s-celine). Sera creee si elle n'exite pas" % MAILING_PREFIX
|
|
|
|
print "email : email a ajouter dans toutes les mailing lists sauf dans %s-nom" % MAILING_PREFIX
|
2012-11-26 15:40:22 +01:00
|
|
|
print "ancien_email : email a supprimer de toutes les mailing lists"
|
|
|
|
sys.exit(1)
|
2012-11-26 15:14:47 +01:00
|
|
|
|
2018-11-21 16:13:19 +01:00
|
|
|
mailingName = MAILING_PREFIX+"-"+sys.argv[1]
|
2012-11-26 15:14:47 +01:00
|
|
|
existingLists = getMailingLists(MAILING_PREFIX)
|
|
|
|
|
2018-11-21 16:13:19 +01:00
|
|
|
print "existing lists : %s" % existingLists
|
2012-11-26 15:14:47 +01:00
|
|
|
if mailingName not in existingLists:
|
2018-11-21 16:13:19 +01:00
|
|
|
print "Create list %s " % mailingName
|
2012-11-26 15:14:47 +01:00
|
|
|
createList(mailingName)
|
2018-11-21 16:13:19 +01:00
|
|
|
# populate new mailing list with existing users
|
2012-11-26 15:14:47 +01:00
|
|
|
users = getListUser(MAILING_PREFIX)
|
|
|
|
for user in users:
|
|
|
|
addUserToList(user, mailingName)
|
|
|
|
|
|
|
|
allButUserList = list(existingLists)
|
|
|
|
|
|
|
|
if mailingName in existingLists:
|
|
|
|
allButUserList.remove(mailingName)
|
|
|
|
|
|
|
|
for listName in allButUserList:
|
|
|
|
addUserToList(sys.argv[2], listName)
|
|
|
|
|
|
|
|
if len(sys.argv) == 4:
|
|
|
|
for listName in existingLists:
|
|
|
|
delUserFromList(sys.argv[3], listName)
|