54 lines
2.9 KiB
Python
54 lines
2.9 KiB
Python
from configparser import ConfigParser
|
|
import requests
|
|
import os
|
|
import subprocess
|
|
|
|
path = os.path.dirname(os.path.abspath(__file__))
|
|
filename = path + "/generated/changed"
|
|
|
|
config = ConfigParser()
|
|
config.read(path + '/config.ini')
|
|
|
|
mailman_url = config.get('Mailman', 'url')
|
|
mailman_username = config.get('Mailman', 'username')
|
|
mailman_password = config.get('Mailman', 'password')
|
|
domain = config.get('Mailman', 'domain')
|
|
roster_url = config.get('Mailman', 'roster_url', fallback='http://{mailman_url}/3.1/lists/{list_name}@{domain}/roster/member')
|
|
changed = int(open(filename).read())
|
|
|
|
if changed:
|
|
for section in config.sections():
|
|
if section not in ["Re2o", "Mailman"] and config.getboolean(section, 'activate'):
|
|
list_name = config.get(section, "list_name", fallback=section)
|
|
response1 = requests.get(roster_url.format(mailman_url=mailman_url, list_namelist_name, domain=domain), auth=(mailman_username, mailman_password))
|
|
if "entries" in response1.json():
|
|
entries = response1.json()['entries']
|
|
old_emails = [entry['email'] for entry in entries]
|
|
new_emails = open(path + "/generated/ml.{}.list".format(section)).read().split("\n")
|
|
emails_to_delete = [email for email in old_emails if email not in new_emails]
|
|
if emails_to_delete:
|
|
print("[..] Deleting non members from list {}".format(list_name))
|
|
response = requests.delete(roster_url.format(mailman_url=mailman_url, list_name=list_name, domain=domain), auth=(mailman_username,mailman_password), params={'emails': emails_to_delete})
|
|
print("[OK] Non members where deleted from list {}".format(list_name))
|
|
else:
|
|
print("[INFO] No member to delete for list {}".format(list_name))
|
|
emails_to_add = [email for email in new_emails if email not in old_emails]
|
|
if emails_to_add:
|
|
print("[..] Adding members to list {}".format(list_name))
|
|
with open(path + "/tmp", "w+") as f:
|
|
for email in emails_to_add:
|
|
f.write("{}\n".format(email))
|
|
subprocess.call(["mailman", "members", "{}@{}".format(list_name, domain), "-a", path + "/tmp"])
|
|
os.remove(path + "/tmp")
|
|
print("[OK] Members added to list {}".format(list_name))
|
|
else:
|
|
print("[INFO] No member to add to list {}".format(list_name))
|
|
else:
|
|
print("[..] Subscribing members to list {}".format(list_name))
|
|
subprocess.call(["mailman", "members", "{}@{}".format(list_name, domain), "-a", path + "/generated/ml.{}.list".format(section)])
|
|
print("[OK] List {} was regenerated".format(list_name))
|
|
|
|
with open(filename, "w+") as f:
|
|
f.write("0")
|
|
else:
|
|
print("Files have not changed since last execution. Skipping")
|