Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Doc/library/email.headerregistry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,22 @@ variant, :attr:`~.BaseHeader.max_count` is set to 1.
``quoted-printable``. See :rfc:`2045` for more information.


.. class:: ReferencesHeader

Handles the :mailheader:`In-Reply-To` and :mailheader:`References` headers.

.. attribute:: message_ids

A tuple of the message identifiers in the header, in order. Each one is
in its ``<id-left@id-right>`` form, without the comments and folding
white space that may surround it in the header, so it can be compared
with the value of another message's :mailheader:`Message-ID` header or
with the result of :func:`email.utils.make_msgid`. Identifiers that
cannot be parsed are left out and reported in :attr:`~BaseHeader.defects`.

.. versionadded:: next



.. class:: HeaderRegistry(base_class=BaseHeader, \
default_class=UnstructuredHeader, \
Expand Down Expand Up @@ -343,6 +359,8 @@ variant, :attr:`~.BaseHeader.max_count` is set to 1.
:content-disposition: ContentDispositionHeader
:content-transfer-encoding: ContentTransferEncodingHeader
:message-id: MessageIDHeader
:in-reply-to: ReferencesHeader
:references: ReferencesHeader

``HeaderRegistry`` has the following methods:

Expand Down
10 changes: 10 additions & 0 deletions Doc/whatsnew/3.16.rst
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,16 @@ difflib
(Contributed by Tomasz Kazimierczak in :gh:`118150`.)


email
-----

* :class:`email.headerregistry.ReferencesHeader`, used for the
:mailheader:`In-Reply-To` and :mailheader:`References` headers, has a new
:attr:`~email.headerregistry.ReferencesHeader.message_ids` attribute: a tuple
of the message identifiers the header holds.
(Contributed by Sudeep Gowda in :gh:`148797`.)


encodings
---------

Expand Down
11 changes: 11 additions & 0 deletions Lib/email/headerregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,17 @@ def parse(cls, value, kwds):
kwds['decoded'] = str(parse_tree)
kwds['defects'].extend(parse_tree.all_defects)

def init(self, *args, **kw):
super().init(*args, **kw)
# Comments and folding whitespace around a msg-id are not part of it.
self._message_ids = tuple(
''.join(str(token) for token in msg_id if token.token_type != 'cfws')
for msg_id in self._parse_tree.message_ids)

@property
def message_ids(self):
return self._message_ids


# The header factory #

Expand Down
55 changes: 55 additions & 0 deletions Lib/test/test_email/test_headerregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import unittest
from email import errors
from email import policy
from email import utils
from email import message_from_string
from email.message import Message
from test.test_email import TestEmailBase, parameterize
from email import headerregistry
Expand Down Expand Up @@ -1688,6 +1690,59 @@ def test_group_comparison(self):
self.assertTrue(g == ALWAYS_EQ)


class TestReferencesHeader(TestHeaderBase):

def test_references_message_ids(self):
h = self.make_header('References',
'<a@example.com> (the first) <b@example.com>')
self.assertEqual(h.message_ids, ('<a@example.com>', '<b@example.com>'))
self.assertDefectsEqual(h.defects, [])

def test_in_reply_to_message_ids(self):
h = self.make_header('In-Reply-To', ' <a@example.com> ')
self.assertEqual(h.message_ids, ('<a@example.com>',))

def test_message_ids_match_make_msgid(self):
msgid = utils.make_msgid()
h = self.make_header('In-Reply-To', msgid)
self.assertEqual(h.message_ids, (msgid,))

def test_message_ids_id_right_forms(self):
h = self.make_header('References',
'<a@[127.0.0.1]> <b@xn--fa-hia.example>')
self.assertEqual(h.message_ids,
('<a@[127.0.0.1]>', '<b@xn--fa-hia.example>'))

def test_message_ids_comma_separated(self):
h = self.make_header('References', '<a@example.com>,<b@example.com>')
self.assertEqual(h.message_ids, ('<a@example.com>', '<b@example.com>'))
self.assertDefectsEqual(h.defects, [errors.InvalidHeaderDefect])

def test_message_ids_skip_invalid(self):
h = self.make_header('References', '<a@example.com> not-a-msg-id')
self.assertEqual(h.message_ids, ('<a@example.com>',))
self.assertDefectsEqual(h.defects, [errors.InvalidHeaderDefect])

def test_message_ids_from_parsed_message(self):
msg = message_from_string(textwrap.dedent('''\
In-Reply-To: <b@example.com>
References: <a@example.com>
<b@example.com>

body
'''), policy=policy.default)
[in_reply_to] = msg['In-Reply-To'].message_ids
*_, last_reference = msg['References'].message_ids
self.assertEqual(in_reply_to, last_reference)
self.assertEqual(msg['References'].message_ids,
('<a@example.com>', '<b@example.com>'))

def test_message_ids_read_only(self):
h = self.make_header('References', '<a@example.com>')
with self.assertRaises(AttributeError):
h.message_ids = ()


class TestFolding(TestHeaderBase):

def test_address_display_names(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add :attr:`email.headerregistry.ReferencesHeader.message_ids`, a tuple of the
message identifiers in an :mailheader:`In-Reply-To` or :mailheader:`References`
header.
Loading