# Copyright (C) 2009 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. """Change a members address. Save as bin/change_member_address.py Run via bin/withlist -r change_member_address [options] or bin/withlist -a -r change_member_address -- [options] to do all lists (global change). Options: -d --domain The and arguments are domain names. All members addresses in the domain (e.g. user@) will be changed to the domain (e.g. user@). -v --verbose Print what was done (If new_address is banned or already a member, that will always be printed). """ import re import sys import getopt from Mailman import Errors 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 doit(mlist, old_addr, new_addr): global verbose, changed try: mlist.ApprovedChangeMemberAddress(old_addr, new_addr, globally=False) except Errors.MembershipIsBanned: print 'List: %s - %s is banned from membership.' % (mlist.real_name, new_addr) except Errors.MMAlreadyAMember: print 'List: %s - %s is already a member.' % (mlist.real_name, new_addr) except Errors.NotAMemberError: if verbose: print 'List: %s - %s is not a member' % (mlist.real_name, old_addr) else: if verbose: print 'List: %s - %s changed to %s' % (mlist.real_name, old_addr, new_addr) changed = True def change_member_address(mlist, *args): global verbose, changed try: opts, args = getopt.getopt(args, 'dv', ['domain', 'verbose']) except getopt.error, msg: usage(1, msg) verbose = False domain = False for opt, arg in opts: if opt in ('-v', '--verbose'): verbose = True if opt in ('-d', '--domain'): domain = True if len(args) <> 2: usage(1, 'Incorrect number of arguments') old_addr = args[0] new_addr = args[1] if not mlist.Locked(): mlist.Lock() changed = False if domain: for member in mlist.getMembers(): if member.endswith('@' + old_addr): new_member = re.sub('@.*$', '@' + new_addr, member) doit(mlist, member, new_member) else: doit(mlist, old_addr, new_addr) if changed: mlist.Save() mlist.Unlock()