Mailinglist JSON API
Closes #29 (closed) Closes #475 (closed)
With a new syncmailinglists.py:
#!/usr/bin/env python3
import subprocess
import sys
import tempfile
import urllib.request
import json
API_URL = 'https://thalia.nu/api/v1/mailinglists/?secret={{ mailman_apikey }}'
def get_json():
response = urllib.request.urlopen(API_URL)
obj = json.load(response)
return obj
def populate_list(list_name, prefix, archived, moderated, list_addresses):
archived = 1 if archived else 0
moderated = 1 if moderated else 0
f = tempfile.NamedTemporaryFile(mode='w')
f.write('real_name = "{}"\n'.format(list_name))
f.write('subject_prefix = "{}"\n'.format(prefix))
f.write('admin_immed_notify = {}\n'.format(moderated))
f.write('default_member_moderation = {}\n'.format(moderated))
f.write('generic_nonmember_action = {}\n'.format(moderated))
f.write('archive = {}\n'.format(archived))
f.flush()
subprocess.call(['/usr/sbin/newlist',
'-qe',
'thalia.nu',
list_name,
'postmaster@thalia.nu',
'{{ mailman_listadmin_password }}'
],
stderr=subprocess.DEVNULL)
subprocess.check_call(['/usr/sbin/config_list',
'-i',
'/etc/mailman/default_listconfigs.py',
list_name])
# First apply the default configurations
subprocess.check_call(['/usr/sbin/config_list',
'-i',
f.name,
list_name])
with subprocess.Popen(['/usr/sbin/sync_members',
'-f',
'-',
'--welcome-msg=no',
'--goodbye-msg=no',
'--digest=no',
list_name],
stdin=subprocess.PIPE) as proc:
for a in list_addresses:
proc.stdin.write((a + '\n').encode('utf-8'))
proc.stdin.flush()
old_lists = subprocess.check_output(['/usr/sbin/list_lists', '-b']).decode(
'utf-8').splitlines()
server_lists = get_json()
for list in server_lists:
for name in list['names']:
populate_list(name, list['prefix'], list['archived'],
list['moderated'], list['addresses'])
if name in old_lists:
old_lists.remove(name)
old_lists.remove('mailman')
if len(old_lists) > 1:
print('We want to remove more than one mailinglist!', file=sys.stderr)
sys.exit(1)
for old_list in old_lists:
subprocess.call(['/usr/sbin/rmlist', old_list])
Edited by Sébastiaan Versteeg