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
|
# Copyright (C) 2002-2016 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 email commands 'join' and 'subscribe'."""
__all__ = [
'Join',
'Subscribe',
'Leave',
'Unsubscribe',
]
from email.utils import formataddr, parseaddr
from mailman.core.i18n import _
from mailman.interfaces.command import ContinueProcessing, IEmailCommand
from mailman.interfaces.member import DeliveryMode, MemberRole
from mailman.interfaces.registrar import IRegistrar
from mailman.interfaces.subscriptions import ISubscriptionService
from mailman.interfaces.usermanager import IUserManager
from zope.component import getUtility
from zope.interface import implementer
def match_subscriber(email, display_name):
# Return something matching the email which should be used as the
# subscriber by the IRegistrar interface.
manager = getUtility(IUserManager)
# Is there a user with a preferred address matching the email?
user = manager.get_user(email)
if user is not None:
preferred = user.preferred_address
if preferred is not None and preferred.email == email.lower():
return user
# Is there an address matching the email?
address = manager.get_address(email)
if address is not None:
return address
# Make a new user and subscribe their first (and only) address. We can't
# make the first address their preferred address because it hasn't been
# verified yet.
user = manager.make_user(email, display_name)
return list(user.addresses)[0]
@implementer(IEmailCommand)
class Join:
"""The email 'join' command."""
name = 'join'
# XXX 2012-02-29 BAW: DeliveryMode.summary is not yet supported.
argument_description = '[digest=<no|mime|plain>]'
description = _("""\
You will be asked to confirm your subscription request and you may be issued a
provisional password.
By using the 'digest' option, you can specify whether you want digest delivery
or not. If not specified, the mailing list's default delivery mode will be
used.
""")
short_description = _('Join this mailing list.')
def process(self, mlist, msg, msgdata, arguments, results):
"""See `IEmailCommand`."""
# Parse the arguments.
delivery_mode = self._parse_arguments(arguments, results)
if delivery_mode is ContinueProcessing.no:
return ContinueProcessing.no
display_name, email = parseaddr(msg['from'])
# Address could be None or the empty string.
if not email:
email = msg.sender
if not email:
print(_('$self.name: No valid address found to subscribe'),
file=results)
return ContinueProcessing.no
if isinstance(email, bytes):
email = email.decode('ascii')
# Have we already seen one join request from this user during the
# processing of this email?
joins = getattr(results, 'joins', set())
if email in joins:
# Do not register this join.
return ContinueProcessing.yes
joins.add(email)
results.joins = joins
person = formataddr((display_name, email))
# Is this person already a member of the list? Search for all
# matching memberships.
members = getUtility(ISubscriptionService).find_members(
email, mlist.list_id, MemberRole.member)
if len(members) > 0:
print(_('$person is already a member'), file=results)
return ContinueProcessing.yes
subscriber = match_subscriber(email, display_name)
IRegistrar(mlist).register(subscriber)
print(_('Confirmation email sent to $person'), file=results)
return ContinueProcessing.yes
def _parse_arguments(self, arguments, results):
"""Parse command arguments.
:param arguments: The sequences of arguments as given to the
`process()` method.
:param results: The results object.
:return: The delivery mode, None, or ContinueProcessing.no on error.
"""
mode = DeliveryMode.regular
for argument in arguments:
parts = argument.split('=', 1)
if len(parts) != 2 or parts[0] != 'digest':
print(self.name, _('bad argument: $argument'),
file=results)
return ContinueProcessing.no
mode = {
'no': DeliveryMode.regular,
'plain': DeliveryMode.plaintext_digests,
'mime': DeliveryMode.mime_digests,
}.get(parts[1])
if mode is None:
print(self.name, _('bad argument: $argument'),
file=results)
return ContinueProcessing.no
return mode
class Subscribe(Join):
"""The email 'subscribe' command (an alias for 'join')."""
name = 'subscribe'
description = _("An alias for 'join'.")
short_description = description
@implementer(IEmailCommand)
class Leave:
"""The email 'leave' command."""
name = 'leave'
argument_description = ''
description = _("""Leave this mailing list.
You may be asked to confirm your request.""")
short_description = _('Leave this mailing list.')
def process(self, mlist, msg, msgdata, arguments, results):
"""See `IEmailCommand`."""
email = msg.sender
if not email:
print(_('$self.name: No valid email address found to unsubscribe'),
file=results)
return ContinueProcessing.no
user_manager = getUtility(IUserManager)
user = user_manager.get_user(email)
if user is None:
print(_('No registered user for email address: $email'),
file=results)
return ContinueProcessing.no
# The address that the -leave command was sent from, must be verified.
# Otherwise you could link a bogus address to anyone's account, and
# then send a leave command from that address.
if user_manager.get_address(email).verified_on is None:
print(_('Invalid or unverified email address: $email'),
file=results)
return ContinueProcessing.no
for user_address in user.addresses:
# Only recognize verified addresses.
if user_address.verified_on is None:
continue
member = mlist.members.get_member(user_address.email)
if member is not None:
break
else:
# None of the user's addresses are subscribed to this mailing list.
print(_(
'$self.name: $email is not a member of $mlist.fqdn_listname'),
file=results)
return ContinueProcessing.no
member.unsubscribe()
person = formataddr((user.display_name, email))
print(_('$person left $mlist.fqdn_listname'), file=results)
return ContinueProcessing.yes
class Unsubscribe(Leave):
"""The email 'unsubscribe' command (an alias for 'leave')."""
name = 'unsubscribe'
description = _("An alias for 'leave'.")
short_description = description
|