Remove the mailman.interface magic. Use the more specific interface imports.
[mailman.git] / mailman / chains / headers.py
blobd76e48c50d6a1a196ba3ae4245ed871c2a202b1b
1 # Copyright (C) 2007-2009 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 header-matching chain."""
20 __metaclass__ = type
21 __all__ = [
22 'HeaderMatchChain',
26 import re
27 import logging
28 import itertools
30 from zope.interface import implements
32 from mailman.chains.base import Chain, Link
33 from mailman.config import config
34 from mailman.i18n import _
35 from mailman.interfaces.chain import IChainIterator, LinkAction
36 from mailman.interfaces.rules import IRule
39 log = logging.getLogger('mailman.vette')
43 def make_link(entry):
44 """Create a Link object.
46 :param entry: a 2- or 3-tuple describing a link. If a 2-tuple, it is a
47 header and a pattern, and a default chain of 'hold' will be used. If
48 a 3-tuple, the third item is the chain name to use.
49 :return: an ILink.
50 """
51 if len(entry) == 2:
52 header, pattern = entry
53 chain_name = 'hold'
54 elif len(entry) == 3:
55 header, pattern, chain_name = entry
56 # We don't assert that the chain exists here because the jump
57 # chain may not yet have been created.
58 else:
59 raise AssertionError('Bad link description: %s' % entry)
60 rule = HeaderMatchRule(header, pattern)
61 chain = config.chains[chain_name]
62 return Link(rule, LinkAction.jump, chain)
66 class HeaderMatchRule:
67 """Header matching rule used by header-match chain."""
68 implements(IRule)
70 # Sequential rule counter.
71 _count = 1
73 def __init__(self, header, pattern):
74 self._header = header
75 self._pattern = pattern
76 self.name = 'header-match-%002d' % HeaderMatchRule._count
77 HeaderMatchRule._count += 1
78 self.description = u'%s: %s' % (header, pattern)
79 # XXX I think we should do better here, somehow recording that a
80 # particular header matched a particular pattern, but that gets ugly
81 # with RFC 2822 headers. It also doesn't match well with the rule
82 # name concept. For now, we just record the rather useless numeric
83 # rule name. I suppose we could do the better hit recording in the
84 # check() method, and set self.record = False.
85 self.record = True
87 def check(self, mlist, msg, msgdata):
88 """See `IRule`."""
89 for value in msg.get_all(self._header, []):
90 if re.search(self._pattern, value, re.IGNORECASE):
91 return True
92 return False
96 class HeaderMatchChain(Chain):
97 """Default header matching chain.
99 This could be extended by header match rules in the database.
102 def __init__(self):
103 super(HeaderMatchChain, self).__init__(
104 'header-match', _('The built-in header matching chain'))
105 # The header match rules are not global, so don't register them.
106 # These are the only rules that the header match chain can execute.
107 self._links = []
108 # Initialize header check rules with those from the global
109 # HEADER_MATCHES variable.
110 for entry in config.header_matches:
111 self._links.append(make_link(entry))
112 # Keep track of how many global header matching rules we've seen.
113 # This is so the flush() method will only delete those that were added
114 # via extend() or append_link().
115 self._permanent_link_count = len(self._links)
117 def extend(self, header, pattern, chain_name='hold'):
118 """Extend the existing header matches.
120 :param header: The case-insensitive header field name.
121 :param pattern: The pattern to match the header's value again. The
122 match is not anchored and is done case-insensitively.
123 :param chain: Option chain to jump to if the pattern matches any of
124 the named header values. If not given, the 'hold' chain is used.
126 self._links.append(make_link((header, pattern, chain_name)))
128 def flush(self):
129 """See `IMutableChain`."""
130 del self._links[self._permanent_link_count:]
132 def get_links(self, mlist, msg, msgdata):
133 """See `IChain`."""
134 list_iterator = HeaderMatchIterator(mlist)
135 return itertools.chain(iter(self._links), iter(list_iterator))
137 def __iter__(self):
138 for link in self._links:
139 yield link
143 class HeaderMatchIterator:
144 """An iterator of both the global and list-specific chain links."""
146 implements(IChainIterator)
148 def __init__(self, mlist):
149 self._mlist = mlist
151 def __iter__(self):
152 """See `IChainIterator`."""
153 for entry in self._mlist.header_matches:
154 yield make_link(entry)