aboutsummaryrefslogblamecommitdiffstats
path: root/bin/remove_members
blob: 33aa6a2b8636aac2095ffdbde4516316e85a3c7d (plain) (tree)
1
2
3

           
                                                               












                                                                   


                                                                           














                                                                    
                                           

             



                                                                             


               

                                                                         


                

                                                                          







                                                    








                            
                           
 





                 






                        
                            








                        
                      

                           
                     














                                                                           
                   




                                 






                                     
                      
                                
                           
                                          
                           
                                           
                               
 


                                                     


















                                                                              
                                                                      





                                              
                                                                   








                                           
                                                           
                           

                                                                     
                           
                                                                               







                          
#! @PYTHON@
#
# Copyright (C) 1998-2005 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.

"""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.  If not specified, the list
        default value is used.

    --noadminack
    -N
        Don't send the admin acknowledgements.  If not specified, the list
        default value is used.

    --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 C_

try:
    True, False
except NameError:
    True = 1
    False = 0



def usage(code, msg=''):
    if code:
        fd = sys.stderr
    else:
        fd = sys.stdout
    print >> fd, C_(__doc__)
    if msg:
        print >> fd, msg
    sys.exit(code)


def ReadFile(filename):
    lines = []
    if filename == "-":
        fp = sys.stdin
        closep = False
    else:
        fp = open(filename)
        closep = True
    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)

    filename = None
    all = False
    alllists = False
    # None means use list default
    userack = None
    admin_notif = None

    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 = True
        elif opt == '--fromall':
            alllists = True
        elif opt in ('-n', '--nouserack'):
            userack = False
        elif opt in ('-N', '--noadminack'):
            admin_notif = False

    if len(args) < 1 and not (filename and alllists):
        usage(1)

    # 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 C_('Could not open file for reading: %(filename)s.')

    for listname in listnames:
       try:
           # open locked
           mlist = MailList.MailList(listname)
       except Errors.MMListError:
           print C_('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 C_('No such member: %(addr)s')
                   continue
               mlist.ApprovedDeleteMember(addr, 'bin/remove_members',
                                          admin_notif, userack)
               if alllists:
                   print C_("User `%(addr)s' removed from list: %(listname)s.")
           mlist.Save()
       finally:
           mlist.Unlock()



if __name__ == '__main__':
    main()