aboutsummaryrefslogblamecommitdiffstats
path: root/bin/remove_members
blob: 888dfa51de1df153f49227d534e8f7e3463bc125 (plain) (tree)


















































































































































































                                                                              
#! @PYTHON@
#
# Copyright (C) 1998,1999,2000,2001,2002 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
"""Remove members from a list.

Usage:
    remove_members [options] [listname] [addr1 ...]

Options:

    --file=file
    -f file
        Remove member addresses found in the given file.  If file is
        `-', read stdin.

    --all
    -a
        Remove all members of the mailing list.
       (mutually exclusive with --fromall)

    --fromall
       Removes the given addresses from all the lists on this system
       regardless of virtual domains if you have any.  This option cannot be
       used -a/--all.  Also, you should not specify a listname when using this
       option.

    --nouserack
    -n
        Don't send the user acknowledgements.

    --noadminack
    -N
        Don't send the admin acknowledgements.

    --help
    -h
        Print this help message and exit.

    listname is the name of the mailing list to use.

    addr1 ... are additional addresses to remove.

"""

import sys
import getopt

import paths
from Mailman import MailList
from Mailman import Utils
from Mailman import Errors
from Mailman.i18n import _



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 ReadFile(filename):
    lines = []
    if filename == "-":
        fp = sys.stdin
        closep = 0
    else:
        fp = open(filename)
        closep = 1
    lines = filter(None, [line.strip() for line in fp.readlines()])
    if closep:
        fp.close()
    return lines



def main():
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], 'naf:hN',
            ['all', 'fromall', 'file=', 'help', 'nouserack', 'noadminack'])
    except getopt.error, msg:
        usage(1, msg)

    if len(args) < 1:
        usage(1)

    filename = None
    all = 0
    alllists = 0
    userack = 1
    admin_notif = 1

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage(0)
        elif opt in ('-f', '--file'):
            filename = arg
        elif opt in ('-a', '--all'):
            all = 1
        elif opt == '--fromall':
            alllists = 1
        elif opt in ('-n', '--nouserack'):
            userack = 0
        elif opt in ('-N', '--noadminack'):
            admin_notif = 0

    # You probably don't want to delete all the users of all the lists -- Marc
    if all and alllists:
       usage(1)

    if alllists:
       addresses = args
    else:
       listname = args[0].lower().strip()
       addresses = args[1:]

    if alllists:
        listnames = Utils.list_names()
    else:
        listnames = [listname]

    if filename:
        try:
            addresses = addresses + ReadFile(filename)
        except IOError:
            print _('Could not open file for reading: %(filename)s.')

    for listname in listnames:
       try:
           # open locked
           mlist = MailList.MailList(listname)
       except Errors.MMListError:
           print _('Error opening list %(listname)s... skipping.')
           continue

       if all:
           addresses = mlist.getMembers()

       try:
           for addr in addresses:
               if not mlist.isMember(addr):
                   if not alllists:
                       print _('No such member: %(addr)s')
                   continue
               mlist.ApprovedDeleteMember(addr,
                                          'bin/remove_members',
                                          admin_notif,
                                          userack)
               if alllists:
                   print _("User `%(addr)s' removed from list: %(listname)s.")
           mlist.Save()
       finally:
           mlist.Unlock()



if __name__ == '__main__':
    main()