# Copyright (C) 2007 by the Free Software Foundation, Inc. # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, # USA. """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()