flake8-respect-noqa is no longer required with flake8>3.0
[mailman.git] / src / mailman / chains / reject.py
blob6711538b798b0c86c94718583cc4f1a5012f3e98
1 # Copyright (C) 2007-2016 by the Free Software Foundation, Inc.
3 # This file is part of GNU Mailman.
5 # GNU Mailman is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option)
8 # any later version.
10 # GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
15 # You should have received a copy of the GNU General Public License along with
16 # GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
18 """The terminal 'reject' chain."""
20 import logging
22 from mailman import public
23 from mailman.app.bounces import bounce_message
24 from mailman.chains.base import TerminalChainBase
25 from mailman.core.i18n import _
26 from mailman.interfaces.chain import RejectEvent
27 from mailman.interfaces.pipeline import RejectMessage
28 from zope.event import notify
31 log = logging.getLogger('mailman.vette')
33 NEWLINE = '\n'
34 SEMISPACE = '; '
37 @public
38 class RejectChain(TerminalChainBase):
39 """Reject/bounce a message."""
41 name = 'reject'
42 description = _('Reject/bounce a message and stop processing.')
44 def _process(self, mlist, msg, msgdata):
45 """See `TerminalChainBase`."""
46 # Start by decorating the message with a header that contains a list
47 # of all the rules that matched. These metadata could be None or an
48 # empty list.
49 rule_hits = msgdata.get('rule_hits')
50 if rule_hits:
51 msg['X-Mailman-Rule-Hits'] = SEMISPACE.join(rule_hits)
52 rule_misses = msgdata.get('rule_misses')
53 if rule_misses:
54 msg['X-Mailman-Rule-Misses'] = SEMISPACE.join(rule_misses)
55 reasons = msgdata.get('moderation_reasons')
56 if reasons is None:
57 error = None
58 else:
59 error = RejectMessage(_("""
60 Your message to the {list_name} mailing-list was rejected for the following
61 reasons:
63 {reasons}
65 The original message as received by Mailman is attached.
66 """).format(
67 list_name=mlist.display_name, # noqa
68 reasons=NEWLINE.join(reasons) # noqa
69 )) # noqa
70 bounce_message(mlist, msg, error)
71 log.info('REJECT: %s', msg.get('message-id', 'n/a'))
72 notify(RejectEvent(mlist, msg, msgdata, self))