-
Notifications
You must be signed in to change notification settings - Fork 8
/
message.py
97 lines (84 loc) · 3.88 KB
/
message.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Copyright (c) 2014 Ian C. Good
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import email
from email.message import Message
from ..interfaces import MessageInterface
from .primitives import LiteralString, QuotedString, List, Nil
__all__ = ['MessageHelper']
class MessageHelper(object):
charset = email.charset.Charset('utf-8')
def __init__(self, msg, header_msg=None):
super().__init__()
self.msg = msg
self.header_msg = header_msg or msg
def get_header(self):
ret = Message(policy=email.policy.SMTP)
for name, value in self.header_msg.items():
ret[name] = value
return LiteralString(bytes(ret))
def get_header_subset(self, headers, inverse=False):
ret = Message(policy=email.policy.SMTP)
headers_upper = [header.upper() for header in headers]
for name, value in self.header_msg.items():
name_bytes = bytes(name, 'utf-8').upper()
if inverse != (name_bytes in headers_upper):
ret[name] = value
return LiteralString(bytes(ret))
def get_body_structure(self):
pass
def _get_header_str_or_nil(self, name):
value = self.header_msg.get(name)
if value is None:
return Nil()
value_enc = self.charset.header_encode(value)
return QuotedString(bytes(value_enc, 'ascii'))
def _get_header_addresses(self, name, default_name=None):
values = self.header_msg.get_all(name)
if not values:
if default_name:
return self._get_header_addresses(default_name)
else:
return Nil()
address_info = []
for realname, address in email.utils.getaddresses(values):
realname_bytes = bytes(realname, 'utf-8')
address_bytes = bytes(address, 'utf-8')
localpart_bytes, _, domain_bytes = address_bytes.rpartition('@')
address_info.append(List([QuotedString(realname_bytes),
Nil(),
QuotedString(localpart_bytes),
QuotedString(domain_bytes)]))
return List(address_info)
def get_envelope(self):
date = self._get_header_str_or_nil('Date')
subject = self._get_header_str_or_nil('Subject')
from_ = self._get_header_addresses('From')
sender = self._get_header_addresses('Sender', 'From')
reply_to = self._get_header_addresses('Reply-To', 'From')
to = self._get_header_addresses('To')
cc = self._get_header_addresses('Cc')
bcc = self._get_header_addresses('Bcc')
in_reply_to = self._get_header_str_or_nil('In-Reply-To')
message_id = self.get_header_str_or_nil('Message-Id')
return List([date, subject,
from_, sender, reply_to,
to, cc, bcc,
in_reply_to, message_id])