Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ian Good
committed
May 19, 2015
1 parent
060b916
commit 9fb2056
Showing
3 changed files
with
168 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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]) |