Commit 05582947 authored by Abhilash Raj's avatar Abhilash Raj

Merge branch 'header-match' into 'master'

Add support for tag and finding HeaderMatches for a list.

See merge request !105
parents 3e96a7b8 675f526a
Pipeline #79590713 passed with stage
in 6 minutes and 19 seconds
......@@ -10,6 +10,8 @@ NEWS for mailmanclient
* URL encode values in URL which are url unsafe. (Closes #44)
* Add support to mass unsubscribe memebrs from a Mailing List. (Closes #43)
* Add support to set a user's preferred address. (See !99)
* Add a new ``tag`` attribute to HeaderMatches and support to find a set
of matches based on tag.
3.2.2 (2019-02-09)
......@@ -1209,8 +1209,9 @@ Header matches can be added using the ``add()`` method. The arguments are:
- the regular expression to use for filtering (``str``)
- the action to take when the header matches the pattern. This can be
``'accept'``, ``'discard'``, ``'reject'``, or ``'hold'``.
- the tag (``str``) to group a set of header matches.
>>> print(header_matches.add('Subject', '^test: ', 'discard'))
>>> print(header_matches.add('Subject', '^test: ', 'discard', 'sometag'))
Header match on "subject"
>>> print(header_matches)
Header matches for ""
......@@ -1220,6 +1221,12 @@ Header matches can be added using the ``add()`` method. The arguments are:
... print(hm)
Header match on "subject"
Header matches can be filtered using ``.find()`` method to query a set
of HeaderMatches::
>>> header_matches.find(tag='sometag')
[<HeaderMatch on 'subject'>]
You can delete a header match by deleting it from the ``header_matches``
......@@ -13,6 +13,9 @@
# You should have received a copy of the GNU Lesser General Public License
# along with mailmanclient. If not, see <>.
from urllib.error import HTTPError
from mailmanclient.restbase.base import RESTList, RESTObject
__metaclass__ = type
......@@ -43,8 +46,9 @@ class HeaderMatches(RESTList):
def __str__(self):
return 'Header matches for "{}"'.format(self._mlist.list_id)
def add(self, header, pattern, action=None):
def add(self, header, pattern, action=None, tag=None):
"""Add a new HeaderMatch rule to the MailingList.
:param header: The header to consider.
:type header: str
:param pattern: The regular expression to use for filtering.
......@@ -56,15 +60,43 @@ class HeaderMatches(RESTList):
data = dict(header=header, pattern=pattern)
if action is not None:
data['action'] = action
if tag is not None:
data['tag'] = tag
response, content =, data)
return HeaderMatch(self._connection, response.headers.get('location'))
def find(self, header=None, tag=None, action=None):
"""Find a set of HeaderMatch rules.
:param header: The header to consider.
:type header: str
:param tag: The tag associated with header.
:type tag: str
:param action: The action to take when the header matches the pattern.
This can be 'accept', 'discard', 'reject', or 'hold'.
:type action: str
url = self._url + '/find'
data = dict(header=header, tag=tag, action=action)
data = {key: value for key, value in data.items() if value}
if not data:
return []
response, content =, data)
except HTTPError as e:
if e.code == 404:
return []
return [HeaderMatch(self._connection, entry['self_link'], entry)
for entry in content['entries']]
class HeaderMatch(RESTObject):
_properties = ('header', 'pattern', 'position', 'action', 'self_link')
_writable_properties = ('header', 'pattern', 'position', 'action')
_properties = ('header', 'pattern', 'position', 'action', 'tag',
_writable_properties = ('header', 'pattern', 'position', 'action', 'tag')
def __repr__(self):
return '<HeaderMatch on {0!r}>'.format(self.header)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment