"""Find all list members with 'bad' email addresses and remove them. Save as bin/remove_bad_address.py Run via bin/withlist -r remove_bad_address [options] or bin/withlist -a -r remove_bad_address [-- options] to do all lists. Options: -n --no-change Don't actually remove any members. Just report. """ import sys import getopt from Mailman import Utils from Mailman import Errors try: True, False except NameError: True = 1 False = 0 def usage(code, msg=''): if code: fd = sys.stderr else: fd = sys.stdout print >> fd, __doc__ if msg: print >> fd, msg sys.exit(code) def remove_bad_address(mlist, *args): try: opts, args = getopt.getopt(args, 'n', ['no-change']) except getopt.error, msg: usage(1, msg) change = True for opt, arg in opts: if opt in ('-n', '--no-change'): change = False if not mlist.Locked(): mlist.Lock() for member in mlist.getMembers(): try: Utils.ValidateEmail(member) except Errors.EmailAddressError: print 'List: %s, Invalid address: %s' % (mlist.real_name, member) if change: try: mlist.removeMember(member) print ' removed.' except Errors.NotAMemberError: print ' unable to remove.' mlist.Save() mlist.Unlock()