diff options
Diffstat (limited to 'service')
-rw-r--r-- | service/pixelated/adapter/mail.py | 21 | ||||
-rw-r--r-- | service/pixelated/adapter/soledad_querier.py | 3 | ||||
-rw-r--r-- | service/test/unit/adapter/mail_test.py | 12 |
3 files changed, 33 insertions, 3 deletions
diff --git a/service/pixelated/adapter/mail.py b/service/pixelated/adapter/mail.py index fdfeda89..f152c73f 100644 --- a/service/pixelated/adapter/mail.py +++ b/service/pixelated/adapter/mail.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with Pixelated. If not, see <http://www.gnu.org/licenses/>. import json +from uuid import uuid4 from leap.mail.imap.fields import fields import leap.mail.walk as walk @@ -181,8 +182,10 @@ class InputMail(Mail): class PixelatedMail(Mail): @staticmethod - def from_soledad(fdoc, hdoc, bdoc, soledad_querier=None): + def from_soledad(fdoc, hdoc, bdoc, soledad_querier=None, parts=None): mail = PixelatedMail() + mail.parts = parts + mail.boundary = str(uuid4()).replace('-', '') mail.bdoc = bdoc mail.fdoc = fdoc mail.hdoc = hdoc @@ -191,7 +194,19 @@ class PixelatedMail(Mail): @property def body(self): - return self.bdoc.content['raw'] + if self.parts and len(self.parts['alternatives']) > 1: + body = '' + for alternative in self.parts['alternatives']: + body += "--%(boundary)s\n" + for header, value in alternative['headers'].items(): + body += "%s: %s\n" % (header, value) + body += '\n' + body += alternative['content'] + body += '\n' + body += '--%(boundary)s--' + return body % {'boundary': self.boundary} + else: + return self.bdoc.content['raw'] @property def headers(self): @@ -206,6 +221,8 @@ class PixelatedMail(Mail): for header in ['From', 'Subject']: _headers[header] = self.hdoc.content['headers'].get(header) _headers['Date'] = self._get_date() + if self.parts and len(self.parts['alternatives']) > 1: + _headers['content_type'] = 'multipart/alternative; boundary="%s"' % self.boundary return _headers def _get_date(self): diff --git a/service/pixelated/adapter/soledad_querier.py b/service/pixelated/adapter/soledad_querier.py index e4fc04c2..2ccbcd72 100644 --- a/service/pixelated/adapter/soledad_querier.py +++ b/service/pixelated/adapter/soledad_querier.py @@ -105,9 +105,10 @@ class SoledadQuerier: def mail(self, ident): fdoc = self.soledad.get_from_index('by-type-and-contenthash', 'flags', ident)[0] hdoc = self.soledad.get_from_index('by-type-and-contenthash', 'head', ident)[0] + parts = self._extract_parts(hdoc.content) bdoc = self.soledad.get_from_index('by-type-and-payloadhash', 'cnt', hdoc.content['body'])[0] - return PixelatedMail.from_soledad(fdoc, hdoc, bdoc, soledad_querier=self) + return PixelatedMail.from_soledad(fdoc, hdoc, bdoc, soledad_querier=self, parts=parts) def mails(self, idents): fdocs_chash = [(self.soledad.get_from_index('by-type-and-contenthash', 'flags', ident), ident) for ident in idents] diff --git a/service/test/unit/adapter/mail_test.py b/service/test/unit/adapter/mail_test.py index 3bec6fc7..35fe411e 100644 --- a/service/test/unit/adapter/mail_test.py +++ b/service/test/unit/adapter/mail_test.py @@ -90,6 +90,18 @@ class TestPixelatedMail(unittest.TestCase): 'tags': [] }) + def test_alternatives_body(self): + parts = {'alternatives': [], 'attachments': []} + parts['alternatives'].append({'content': 'blablabla', 'headers': {'Content-Type': 'text/plain'}}) + parts['alternatives'].append({'content': '<p>blablabla</p>', 'headers': {'Content-Type': 'text/html'}}) + + mail = PixelatedMail.from_soledad(None, None, None, None, parts=parts) + + self.assertRegexpMatches(mail.body, '^--' + mail.boundary + '\n.*') + self.assertRegexpMatches(mail.body, '\nContent-Type: text/html\n\n<p>blablabla</p>\n') + self.assertRegexpMatches(mail.body, '\nContent-Type: text/plain\n\nblablabla\n') + self.assertRegexpMatches(mail.body, '.*--' + mail.boundary + '--$') + class InputMailTest(unittest.TestCase): mail_dict = lambda x: { |