Skip to content
This repository has been archived by the owner on Jan 2, 2020. It is now read-only.

Added show raw mail function #739

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion service/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Pixelated User Agent Service
ixelated User Agent Service
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "P" here has been removed, I believe, accidentally.

============================

This is the service for the Pixelated User Agent. The primary purpose of this is to provide an interface for the user agent to communicate with the Pixelated Provider.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class MailboxIndexerListener(object):
@classmethod
@defer.inlineCallbacks
def listen(cls, account, mailbox_name, mail_store, search_engine):
listener = MailboxIndexerListener(mailbox_name, mail_store, search_engine)
listener = MailboxIndexerListener(
mailbox_name, mail_store, search_engine)
mail_collection = yield account.get_collection_by_mailbox(mailbox_name)
mail_collection.addListener(listener)

Expand All @@ -39,14 +40,15 @@ def __init__(self, mailbox_name, mail_store, search_engine):
@defer.inlineCallbacks
def notify_new(self):
try:
indexed_idents = set(self.search_engine.search('tag:' + self.mailbox_name.lower(), all_mails=True))
indexed_idents = set(self.search_engine.search(
'tag:' + self.mailbox_name.lower(), all_mails=True))
soledad_idents = yield self.mail_store.get_mailbox_mail_ids(self.mailbox_name)
soledad_idents = set(soledad_idents)

missing_idents = soledad_idents.difference(indexed_idents)

self.search_engine.index_mails((yield self.mail_store.get_mails(missing_idents, include_body=True)))
except Exception, e: # this is a event handler, don't let exceptions escape
except Exception as e: # this is a event handler, don't let exceptions escape
logger.error(e)

def __eq__(self, other):
Expand Down
21 changes: 16 additions & 5 deletions service/pixelated/adapter/mailstore/body_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,25 @@
logger = Logger()


def _parse_charset_header(content_type_and_charset_header, default_charset='us-ascii'):
def _parse_charset_header(
content_type_and_charset_header,
default_charset='us-ascii'):
try:
return re.compile('.*charset="?([a-zA-Z0-9-]+)"?', re.MULTILINE | re.DOTALL).match(content_type_and_charset_header).group(1)
return re.compile(
'.*charset="?([a-zA-Z0-9-]+)"?',
re.MULTILINE | re.DOTALL).match(content_type_and_charset_header).group(1)
except:
return default_charset


class BodyParser(object):

def __init__(self, content, content_type='text/plain; charset="us-ascii"', content_transfer_encoding=None, charset=None):
def __init__(
self,
content,
content_type='text/plain; charset="us-ascii"',
content_transfer_encoding=None,
charset=None):
self._content = content
self._content_type = content_type
self._content_transfer_encoding = content_transfer_encoding
Expand Down Expand Up @@ -62,8 +71,10 @@ def _serialize_for_parser(self, charset):
if isinstance(self._content, unicode):
try:
return encoded_text + self._content.encode(charset)
except UnicodeError, e:
logger.warn('Failed to encode content for charset %s. Ignoring invalid chars: %s' % (charset, e))
except UnicodeError as e:
logger.warn(
'Failed to encode content for charset %s. Ignoring invalid chars: %s' %
(charset, e))
return encoded_text + self._content.encode(charset, 'ignore')
else:
return encoded_text + self._content
23 changes: 17 additions & 6 deletions service/pixelated/adapter/mailstore/leap_attachment_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ def get_mail_attachment(self, attachment_id):
results = yield self.soledad.get_from_index('by-type-and-payloadhash', 'cnt', attachment_id) if attachment_id else []
if results:
content = ContentDocWrapper(**results[0].content)
defer.returnValue({'content-type': content.content_type, 'content': self._try_decode(
content.raw, content.content_transfer_encoding)})
defer.returnValue({'content-type': content.content_type,
'content': self._try_decode(content.raw,
content.content_transfer_encoding)})
else:
raise ValueError('No attachment with id %s found!' % attachment_id)

Expand All @@ -45,21 +46,31 @@ def _try_decode(self, raw, encoding):

return bytearray(data)

def _attachment_to_cdoc(self, content, content_type, encoder=encoders.encode_base64):
def _attachment_to_cdoc(
self,
content,
content_type,
encoder=encoders.encode_base64):
major, sub = content_type.split('/')
attachment = MIMENonMultipart(major, sub)
attachment.set_payload(content)
encoder(attachment)
attachment.add_header('Content-Disposition', 'attachment', filename='does_not_matter.txt')
attachment.add_header('Content-Disposition',
'attachment', filename='does_not_matter.txt')

pseudo_mail = MIMEMultipart()
pseudo_mail.attach(attachment)

tmp_mail = SoledadMailAdaptor().get_msg_from_string(MessageClass=Message, raw_msg=pseudo_mail.as_string())
tmp_mail = SoledadMailAdaptor().get_msg_from_string(
MessageClass=Message, raw_msg=pseudo_mail.as_string())

cdoc = tmp_mail.get_wrapper().cdocs[1]
return cdoc

def _calc_attachment_id_(self, content, content_type, encoder=encoders.encode_base64):
def _calc_attachment_id_(
self,
content,
content_type,
encoder=encoders.encode_base64):
cdoc = self._attachment_to_cdoc(content, content_type, encoder)
return cdoc.phash
Loading