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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# Copyright (C) 2007-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/>.
"""Application level bounce handling."""
import re
import uuid
import logging
from email.mime.message import MIMEMessage
from email.mime.text import MIMEText
from email.utils import parseaddr
from mailman import public
from mailman.config import config
from mailman.core.i18n import _
from mailman.email.message import OwnerNotification, UserNotification
from mailman.interfaces.bounce import UnrecognizedBounceDisposition
from mailman.interfaces.listmanager import IListManager
from mailman.interfaces.pending import IPendable, IPendings
from mailman.interfaces.subscriptions import ISubscriptionService
from mailman.interfaces.template import ITemplateLoader
from mailman.utilities.email import split_email
from mailman.utilities.string import expand, oneline, wrap
from string import Template
from zope.component import getUtility
from zope.interface import implementer
log = logging.getLogger('mailman.config')
elog = logging.getLogger('mailman.error')
blog = logging.getLogger('mailman.bounce')
DOT = '.'
@public
def bounce_message(mlist, msg, error=None):
"""Bounce the message back to the original author.
:param mlist: The mailing list that the message was posted to.
:type mlist: `IMailingList`
:param msg: The original message.
:type msg: `email.message.Message`
:param error: Optional exception causing the bounce. The exception
instance must have a `.message` attribute.
:type error: Exception
"""
# Bounce a message back to the sender, with an error message if provided
# in the exception argument. .sender might be None or the empty string.
if not msg.sender:
# We can't bounce the message if we don't know who it's supposed to go
# to.
return
subject = msg.get('subject', _('(no subject)'))
subject = oneline(subject, mlist.preferred_language.charset)
if error is None:
notice = _('[No bounce details are available]')
else:
notice = _(error.message)
# Currently we always craft bounces as MIME messages.
bmsg = UserNotification(msg.sender, mlist.owner_address, subject,
lang=mlist.preferred_language)
# BAW: Be sure you set the type before trying to attach, or you'll get
# a MultipartConversionError.
bmsg.set_type('multipart/mixed')
txt = MIMEText(notice, _charset=mlist.preferred_language.charset)
bmsg.attach(txt)
bmsg.attach(MIMEMessage(msg))
bmsg.send(mlist)
class _BaseVERPParser:
"""Base class for parsing VERP messages.
Sadly not every MTA bounces VERP messages correctly, or consistently.
First, the To: header is checked, then Delivered-To: (Postfix),
Envelope-To: (Exim) and Apparently-To:. Note that there can be multiple
headers so we need to search them all
"""
def __init__(self, pattern):
self._pattern = pattern
self._cre = re.compile(pattern, re.IGNORECASE)
def get_verp(self, mlist, msg):
"""Extract a set of VERP bounce addresses.
:param mlist: The mailing list being checked.
:type mlist: `IMailingList`
:param msg: The message being parsed.
:type msg: `email.message.Message`
:return: The set of addresses extracted from the VERP headers.
:rtype: set of strings
"""
blocal, bdomain = split_email(mlist.bounces_address)
values = set()
verp_matches = set()
for header in ('to', 'delivered-to', 'envelope-to', 'apparently-to'):
values.update(msg.get_all(header, []))
for field in values:
address = parseaddr(field)[1]
if not address:
# This header was empty.
continue
mo = self._cre.search(address)
if not mo:
# This did not match the VERP regexp.
continue
try:
if blocal != mo.group('bounces'):
# This was not a bounce to our mailing list.
continue
original_address = self._get_address(mo)
except IndexError:
elog.error('Bad VERP pattern: {0}'.format(self._pattern))
return set()
else:
if original_address is not None:
verp_matches.add(original_address)
return verp_matches
@public
class StandardVERP(_BaseVERPParser):
def __init__(self):
super().__init__(config.mta.verp_regexp)
def _get_address(self, match_object):
return '{0}@{1}'.format(*match_object.group('local', 'domain'))
@public
class ProbeVERP(_BaseVERPParser):
def __init__(self):
super().__init__(config.mta.verp_probe_regexp)
def _get_address(self, match_object):
# Extract the token and get the matching address.
token = match_object.group('token')
pendable = getUtility(IPendings).confirm(token)
if pendable is None:
# The token must have already been confirmed, or it may have been
# evicted from the database already.
return None
# We had to pend the uuid as a unicode.
member_id = uuid.UUID(hex=pendable['member_id'])
member = getUtility(ISubscriptionService).get_member(member_id)
if member is None:
return None
return member.address.email
@implementer(IPendable)
class _ProbePendable(dict):
"""The pendable dictionary for probe messages."""
PEND_TYPE = 'probe'
@public
def send_probe(member, msg):
"""Send a VERP probe to the member.
:param member: The member to send the probe to. From this object, both
the user and the mailing list can be determined.
:type member: IMember
:param msg: The bouncing message that caused the probe to be sent.
:type msg:
:return: The token representing this probe in the pendings database.
:rtype: string
"""
mlist = getUtility(IListManager).get_by_list_id(
member.mailing_list.list_id)
template = getUtility(ITemplateLoader).get(
'list:user:notice:probe', mlist,
language=member.preferred_language.code,
# For backward compatibility.
code=member.preferred_language.code,
)
text = wrap(expand(template, mlist, dict(
sender_email=member.subscriber.email,
# For backward compatibility.
address=member.address.email,
email=member.address.email,
owneraddr=mlist.owner_address,
)))
message_id = msg['message-id']
if isinstance(message_id, bytes):
message_id = message_id.decode('ascii')
pendable = _ProbePendable(
# We can only pend unicodes.
member_id=member.member_id.hex,
message_id=message_id,
)
token = getUtility(IPendings).add(pendable)
mailbox, domain_parts = split_email(mlist.bounces_address)
probe_sender = Template(config.mta.verp_probe_format).safe_substitute(
bounces=mailbox,
token=token,
domain=DOT.join(domain_parts),
)
# Calculate the Subject header, in the member's preferred language.
with _.using(member.preferred_language.code):
subject = _('$mlist.display_name mailing list probe message')
# Craft the probe message. This will be a multipart where the first part
# is the probe text and the second part is the message that caused this
# probe to be sent.
probe = UserNotification(member.address.email, probe_sender,
subject, lang=member.preferred_language)
probe.set_type('multipart/mixed')
notice = MIMEText(text, _charset=mlist.preferred_language.charset)
probe.attach(notice)
probe.attach(MIMEMessage(msg))
# Probes should not have the Precedence: bulk header.
probe.send(mlist, envsender=probe_sender, verp=False, probe_token=token,
add_precedence=False)
return token
@public
def maybe_forward(mlist, msg):
"""Possibly forward bounce messages with no recognizable addresses.
:param mlist: The mailing list.
:type mlist: `IMailingList`
:param msg: The bounce message to scan.
:type msg: `Message`
"""
message_id = msg['message-id']
if (mlist.forward_unrecognized_bounces_to
is UnrecognizedBounceDisposition.discard):
blog.error('Discarding unrecognized bounce: {0}'.format(message_id))
return
# The notification is either going to go to the list's administrators
# (owners and moderators), or to the site administrators. Most of the
# notification is exactly the same in either case.
subject = _('Uncaught bounce notification')
template = getUtility(ITemplateLoader).get(
'list:admin:notice:unrecognized', mlist)
text = expand(template, mlist)
text_part = MIMEText(text, _charset=mlist.preferred_language.charset)
attachment = MIMEMessage(msg)
if (mlist.forward_unrecognized_bounces_to
is UnrecognizedBounceDisposition.administrators):
keywords = dict(roster=mlist.administrators)
elif (mlist.forward_unrecognized_bounces_to
is UnrecognizedBounceDisposition.site_owner):
keywords = {}
else:
raise AssertionError('Invalid forwarding disposition: {0}'.format(
mlist.forward_unrecognized_bounces_to))
# Create the notification and send it.
notice = OwnerNotification(mlist, subject, **keywords)
notice.set_type('multipart/mixed')
notice.attach(text_part)
notice.attach(attachment)
notice.send(mlist)
|