diff --git a/gen_config.py b/gen_config.py index 293a1306e43edf4e1ed72ad9aa83500ed79d902e..58669987f69277ef3190d2d06d6f1dd4be3751a5 100755 --- a/gen_config.py +++ b/gen_config.py @@ -8,6 +8,13 @@ import xml.etree.ElementTree as ET import json import mpildap +def err(msg): + print(msg, file=sys.stderr) +def warn(msg): + print(msg, file=sys.stderr) +def info(msg): + print(msg) + def normalize_email(email): lp, domain = email.split('@') ldap_mail = mpildap.ldaps(f'&(istEmailName={lp})(istMailDomainReceive={domain})', 'mail', unique=True) @@ -15,6 +22,19 @@ def normalize_email(email): return ldap_mail[0] return email +def unregexp(e, listtype): + mapping = { + } + if e in mapping: + info(f"mapping {listtype} entry '{e}' to '{mapping[e]}'") + return mapping[e] + for char in '*^$': + if char in e: + warn(f"{listtype} entry '{e}' contains regexp char '{char}', this won't work!") + break + return e + + parser = argparse.ArgumentParser(description='import mailman list to sympa') parser.add_argument('input') parser.add_argument('membersin') @@ -181,19 +201,17 @@ tree = ET.ElementTree(xml) try: tree.write(args.output, encoding='utf-8', xml_declaration=True) except Exception as e: - print(f'Failed to write XML for sympa: {e}', file=sys.stderr) + err(f'Failed to write XML for sympa: {e}') exit(1) with open(args.info, mode='w') as fd: fd.write(old_vars['info']) blocklist = [] -for black in sorted(set(old_vars['ban_list'] + old_vars['hold_these_nonmembers'] + old_vars['reject_these_nonmembers'] + old_vars['discard_these_nonmembers'])): - for char in '*^$': - if char in black: - print(f"blocklist entry '{black}' contains regexp char '{char}', this won't work!", file=sys.stderr) - break - blocklist.append(black) +for block in sorted(set(old_vars['ban_list'] + old_vars['hold_these_nonmembers'] + old_vars['reject_these_nonmembers'] + old_vars['discard_these_nonmembers'])): + entry = unregexp(block, 'blocklist') + if entry: + blocklist.append(entry) if blocklist: # ensure non-empty file ends with newline blocklist.append('') @@ -202,14 +220,13 @@ with open(args.blocklist, mode='w') as fd: whitelist = [] for white in sorted(set(old_vars['accept_these_nonmembers'])): - for char in '*^$': - if char in white: - print(f"whitelist entry '{white}' contains regexp char '{char}', this won't work!", file=sys.stderr) - whitelist.append(white) + entry = unregexp(white, 'whitelist') + if entry: + whitelist.append(entry) if moderated: for m, d in members.items(): if d['_mod'] == 'off': - print(f'Whitelisting member "{m}" for list with moderated policy', file=sys.stderr) + info(f'Whitelisting member "{m}" for list with moderated policy') whitelist.append(m) if whitelist: # ensure non-empty file ends with newline @@ -221,7 +238,7 @@ modlist = [] if not moderated: for m, d in members.items(): if d['_mod'] == 'on': - print(f'Mod-listing member "{m}" for list with open policy', file=sys.stderr) + info(f'Mod-listing member "{m}" for list with open policy') modlist.append(m) if modlist: # ensure non-empty file ends with newline