aboutsummaryrefslogtreecommitdiffstats
path: root/bin/remove_members
blob: b8f92f5e9ef24f3c89fe1bdf039e1a904217e113 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#! @PYTHON@
#
# Copyright (C) 1998-2003 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.  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 _

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

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

    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

    # 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()