1# Copyright (C) 2016-2020 by the Free Software Foundation, Inc.
2#
3# This file is part of GNU Mailman.
4#
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.
9#
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.
14#
15# You should have received a copy of the GNU General Public License along with
16# GNU Mailman.  If not, see <https://www.gnu.org/licenses/>.
17
18"""Banned addresses rule."""
19
20from mailman.core.i18n import _
21from mailman.interfaces.bans import IBanManager
22from mailman.interfaces.rules import IRule
23from public import public
24from zope.interface import implementer
25
26
27@public
28@implementer(IRule)
29class BannedAddress:
30    """The banned address rule."""
31
32    name = 'banned-address'
33    description = _('Match messages sent by banned addresses.')
34    record = True
35
36    def check(self, mlist, msg, msgdata):
37        """See `IRule`."""
38        ban_manager = IBanManager(mlist)
39        for sender in msg.senders:
40            if ban_manager.is_banned(sender):
41                msgdata['moderation_sender'] = sender
42                with _.defer_translation():
43                    # This will be translated at the point of use.
44                    msgdata.setdefault('moderation_reasons', []).append(
45                        (_('Message sender {} is banned from this list'),
46                         sender))
47                return True
48        return False
49