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
|
#! @PYTHON@
#
# Copyright (C) 1998-2018 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 lists that a member's address is on.
Usage:
find_member [options] regex [regex [...]]
Where:
--listname=listname
-l listname
Include only the named list in the search.
--exclude=listname
-x listname
Exclude the named list from the search.
--owners
-w
Search list owners as well as members.
--help
-h
Print this help message and exit.
regex
A Python regular expression to match against.
The interaction between -l and -x is as follows. If any -l option is given
then only the named list will be included in the search. If any -x option is
given but no -l option is given, then all lists will be search except those
specifically excluded.
Regular expression syntax is Perl5-like, using the Python re module. Complete
specifications are at:
http://docs.python.org/library/re.html
Address matches are case-insensitive, but case-preserved addresses are
displayed.
"""
import sys
import re
import getopt
import paths
from Mailman import Utils
from Mailman import MailList
from Mailman import Errors
from Mailman.i18n import C_
AS_MEMBER = 0x01
AS_OWNER = 0x02
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 scanlists(options):
cres = []
for r in options.regexps:
cres.append(re.compile(r, re.IGNORECASE))
#
# dictionary of {address, (listname, ownerp)}
matches = {}
for listname in options.listnames:
try:
mlist = MailList.MailList(listname, lock=0)
except Errors.MMListError:
print C_('No such list: %(listname)s')
continue
if options.owners:
owners = mlist.owner
else:
owners = []
for cre in cres:
for member in mlist.getMembers():
if cre.search(member):
addr = mlist.getMemberCPAddress(member)
entries = matches.get(addr, {})
aswhat = entries.get(listname, 0)
aswhat |= AS_MEMBER
entries[listname] = aswhat
matches[addr] = entries
for owner in owners:
if cre.search(owner):
entries = matches.get(owner, {})
aswhat = entries.get(listname, 0)
aswhat |= AS_OWNER
entries[listname] = aswhat
matches[owner] = entries
return matches
class Options:
listnames = Utils.list_names()
owners = None
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'l:x:wh',
['listname=', 'exclude=', 'owners',
'help'])
except getopt.error, msg:
usage(1, msg)
options = Options()
loptseen = 0
excludes = []
for opt, arg in opts:
if opt in ('-h', '--help'):
usage(0)
elif opt in ('-l', '--listname'):
if not loptseen:
options.listnames = []
loptseen = 1
options.listnames.append(arg.lower())
elif opt in ('-x', '--exclude'):
excludes.append(arg.lower())
elif opt in ('-w', '--owners'):
options.owners = 1
for ex in excludes:
try:
options.listnames.remove(ex)
except ValueError:
pass
if not args:
usage(1, C_('Search regular expression required'))
options.regexps = args
if not options.listnames:
print C_('No lists to search')
return
matches = scanlists(options)
addrs = matches.keys()
addrs.sort()
for k in addrs:
hits = matches[k]
lists = hits.keys()
print k, C_('found in:')
for name in lists:
aswhat = hits[name]
if aswhat & AS_MEMBER:
print ' ', name
if aswhat & AS_OWNER:
print ' ', name, C_('(as owner)')
if __name__ == '__main__':
main()
|