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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# Copyright (C) 2009-2015 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman 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 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman 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
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
"""The 'members' subcommand."""
__all__ = [
'Members',
]
import sys
from contextlib import ExitStack
from email.utils import formataddr, parseaddr
from mailman.app.membership import add_member
from mailman.core.i18n import _
from mailman.database.transaction import transactional
from mailman.interfaces.command import ICLISubCommand
from mailman.interfaces.listmanager import IListManager
from mailman.interfaces.member import (
AlreadySubscribedError, DeliveryMode, DeliveryStatus, MemberRole)
from mailman.interfaces.subscriptions import RequestRecord
from operator import attrgetter
from zope.component import getUtility
from zope.interface import implementer
@implementer(ICLISubCommand)
class Members:
"""Manage list memberships. With no arguments, list all members."""
name = 'members'
def add(self, parser, command_parser):
"""See `ICLISubCommand`."""
self.parser = parser
command_parser.add_argument(
'-a', '--add',
dest='input_filename', metavar='FILENAME',
help=_("""\
Add all member addresses in FILENAME. FILENAME can be '-' to
indicate standard input. Blank lines and lines That start with a
'#' are ignored. Without this option, this command displays
mailing list members."""))
command_parser.add_argument(
'-o', '--output',
dest='output_filename', metavar='FILENAME',
help=_("""Display output to FILENAME instead of stdout. FILENAME
can be '-' to indicate standard output."""))
command_parser.add_argument(
'-R', '--role',
default=None, metavar='ROLE',
choices=('any', 'owner', 'moderator', 'nonmember', 'member',
'administrator'),
help=_("""Display only members with a given ROLE. The role may be
'any', 'member', 'nonmember', 'owner', 'moderator', or
'administrator' (i.e. owners and moderators). If not
given, then delivery members are used. """))
command_parser.add_argument(
'-r', '--regular',
default=None, action='store_true',
help=_('Display only regular delivery members.'))
command_parser.add_argument(
'-d', '--digest',
default=None, metavar='KIND',
# BAW 2010-01-23 summary digests are not really supported yet.
choices=('any', 'plaintext', 'mime'),
help=_("""Display only digest members of KIND. 'any' means any
digest type, 'plaintext' means only plain text (RFC 1153) type
digests, 'mime' means MIME type digests."""))
command_parser.add_argument(
'-n', '--nomail',
default=None, metavar='WHY',
choices=('enabled', 'any', 'unknown'
'byadmin', 'byuser', 'bybounces'),
help=_("""Display only members with a given delivery
status. 'enabled' means all members whose delivery is enabled,
'any' means members whose delivery is disabled for any reason,
'byuser' means that the member disabled their own delivery,
'bybounces' means that delivery was disabled by the automated
bounce processor, 'byadmin' means delivery was disabled by the
list administrator or moderator, and 'unknown' means that delivery
was disabled for unknown (legacy) reasons."""))
# Required positional argument.
command_parser.add_argument(
'list', metavar='LIST', nargs=1,
help=_("""\
The list to operate on. This can be the fully qualified list
name', i.e. the posting address of the mailing list or the
List-ID."""))
command_parser.epilog = _(
"""Display a mailing list's members, with filtering along various
criteria.""")
def process(self, args):
"""See `ICLISubCommand`."""
assert len(args.list) == 1, 'Missing mailing list name'
list_spec = args.list[0]
list_manager = getUtility(IListManager)
if '@' in list_spec:
mlist = list_manager.get(list_spec)
else:
mlist = list_manager.get_by_list_id(list_spec)
if mlist is None:
self.parser.error(_('No such list: $list_spec'))
if args.input_filename is None:
self.display_members(mlist, args)
else:
self.add_members(mlist, args)
def display_members(self, mlist, args):
"""Display the members of a mailing list.
:param mlist: The mailing list to operate on.
:type mlist: `IMailingList`
:param args: The command line arguments.
:type args: `argparse.Namespace`
"""
if args.digest == 'any':
digest_types = [DeliveryMode.plaintext_digests,
DeliveryMode.mime_digests,
DeliveryMode.summary_digests]
elif args.digest is not None:
digest_types = [DeliveryMode[args.digest + '_digests']]
else:
# Don't filter on digest type.
pass
if args.nomail is None:
# Don't filter on delivery status.
pass
elif args.nomail == 'byadmin':
status_types = [DeliveryStatus.by_moderator]
elif args.nomail.startswith('by'):
status_types = [DeliveryStatus['by_' + args.nomail[2:]]]
elif args.nomail == 'enabled':
status_types = [DeliveryStatus.enabled]
elif args.nomail == 'unknown':
status_types = [DeliveryStatus.unknown]
elif args.nomail == 'any':
status_types = [DeliveryStatus.by_user,
DeliveryStatus.by_bounces,
DeliveryStatus.by_moderator,
DeliveryStatus.unknown]
else:
status = args.nomail
self.parser.error(_('Unknown delivery status: $status'))
if args.role is None:
# By default, filter on members.
roster = mlist.members
elif args.role == 'administrator':
roster = mlist.administrators
elif args.role == 'any':
roster = mlist.subscribers
else:
try:
roster = mlist.get_roster(MemberRole[args.role])
except KeyError:
role = args.role
self.parser.error(_('Unknown member role: $role'))
with ExitStack() as resources:
if args.output_filename == '-' or args.output_filename is None:
fp = sys.stdout
else:
fp = resources.enter_context(
open(args.output_filename, 'w', encoding='utf-8'))
addresses = list(roster.addresses)
if len(addresses) == 0:
print(_('$mlist.list_id has no members'), file=fp)
return
for address in sorted(addresses, key=attrgetter('email')):
if args.regular:
member = roster.get_member(address.email)
if member.delivery_mode != DeliveryMode.regular:
continue
if args.digest is not None:
member = roster.get_member(address.email)
if member.delivery_mode not in digest_types:
continue
if args.nomail is not None:
member = roster.get_member(address.email)
if member.delivery_status not in status_types:
continue
print(
formataddr((address.display_name, address.original_email)),
file=fp)
@transactional
def add_members(self, mlist, args):
"""Add the members in a file to a mailing list.
:param mlist: The mailing list to operate on.
:type mlist: `IMailingList`
:param args: The command line arguments.
:type args: `argparse.Namespace`
"""
with ExitStack() as resources:
if args.input_filename == '-':
fp = sys.stdin
else:
fp = resources.enter_context(
open(args.input_filename, 'r', encoding='utf-8'))
for line in fp:
# Ignore blank lines and lines that start with a '#'.
if line.startswith('#') or len(line.strip()) == 0:
continue
# Parse the line and ensure that the values are unicodes.
display_name, email = parseaddr(line)
try:
add_member(mlist,
RequestRecord(email, display_name,
DeliveryMode.regular,
mlist.preferred_language.code))
except AlreadySubscribedError:
# It's okay if the address is already subscribed, just
# print a warning and continue.
if not display_name:
print(_('Already subscribed (skipping): $email'))
else:
print(_('Already subscribed (skipping): '
'$display_name <$email>'))
|