Skip to content

Commit

Permalink
unfinished work
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Good committed May 19, 2015
1 parent 060b916 commit 9fb2056
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 10 deletions.
59 changes: 51 additions & 8 deletions pymap/interfaces.py
Expand Up @@ -288,16 +288,59 @@ def __init__(self, seq, uid):
self.uid = uid

@asyncio.coroutine
def fetch(self, attributes):
"""Get the requested attributes associated with the message.
def fetch_size(self):
"""Returns the total size of the RFC-2822 representation of the
message.
.. seealso: `RFC 3501 7.4.2
<https://tools.ietf.org/html/rfc3501#section-7.4.2>`_
:rtype: int
:param attributes: List of
:class:`~pymap.parsing.specials.FetchAttribute`
objects.
:returns: Dictionary keyed on the requested attributes.
"""
raise NotImplementedError

@asyncio.coroutine
def fetch_header(self, header, section=None):
"""Returns the list of values associated with the given header, like
you would get from the :meth:`~email.message.Message.get_all` method.
:param str header: The name of the header to return values for,
case-insensitive.
:param section: Optional nested index of the MIME part. For example,
``[2, 3]`` would index the 2nd MIME part and then the
3rd MIME sub-part within it. The default ``None``
refers to the top-level MIME part.
:returns: List of header values, as *str* objects.
"""
raise NotImplementedError

@asyncio.coroutine
def fetch_part(self, headers_only=False, section=None):
"""Returns the bytestring of the given MIME part.
:param bool headers_only: Only include the headers in the result.
:param section: Optional nested index of the MIME part. For example,
``[2, 3]`` would index the 2nd MIME part and then the
3rd MIME sub-part within it. The default ``None``
refers to the top-level MIME part.
:rtype: bytes
"""
raise NotImplementedError

@asyncio.coroutine
def fetch_internal_date(self):
"""Returns the internal time associated with the message.
:rtype: datetime.datetime
"""
raise NotImplementedError

@asyncio.coroutine
def fetch_flags(self):
"""Returns the flags associated with the message.
:returns: List of bytestrings.
"""
raise NotImplementedError
22 changes: 20 additions & 2 deletions pymap/state.py
Expand Up @@ -25,7 +25,7 @@
from pymap.exceptions import * # NOQA

from pymap.core import PymapError
from pymap.parsing.primitives import List, Number
from pymap.parsing.primitives import * # NOQA
from pymap.parsing.specials import FetchAttribute
from pymap.parsing.command import CommandAuth, CommandNonAuth, CommandSelect
from pymap.parsing.response import * # NOQA
Expand Down Expand Up @@ -287,8 +287,26 @@ def do_copy(self, cmd):
def do_fetch(self, cmd):
messages = yield from self._get_messages(cmd.sequence_set, cmd.uid)
resp = ResponseOk(cmd.tag, b'FETCH completed.')
mark_seen = False
for msg in messages:
fetch_data = yield from msg.fetch(cmd.attributes)
fetch_data = {}
for attr in cmd.attributes:
if attr.attribute == b'UID':
fetch_data[attr] = Number(msg.uid)
elif attr.attribute == b'FLAGS':
msg_flags = yield from msg.fetch_flags()
fetch_data[attr] = List(msg_flags)
elif attr.attribute == b'RFC822.SIZE':
msg_size = yield from msg.fetch_size()
fetch_data[attr] = Number(msg_size)
elif attr.attribute == b'ENVELOPE':
fetch_data[attr] = yield from self._fetch_msg_envelope(msg)
elif attr.attribute == b'BODY':
mark_seen = True
fetch_data[attr] = yield from self._fetch_msg_body(
msg, attr)
elif attr.attribute == b'BODY.PEEK':
fetch_data[attr] = yield from self._fetch_msg_body
resp.add_data(FetchResponse(msg.seq, fetch_data))
return resp

Expand Down
97 changes: 97 additions & 0 deletions pymap/utils/message.py
@@ -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])

0 comments on commit 9fb2056

Please sign in to comment.