From fd4b2844e60b3d3d53e1de903a8eb9a3d7c840fe Mon Sep 17 00:00:00 2001 From: Gustavo Sousa Date: Mon, 19 Feb 2024 14:37:39 -0300 Subject: [PATCH] editnew: Use raw body text instead of rendered version Rendering mail content via mailcap makes sense when viewing email, but not when reusing the email for composing a new message, as it is desirable to rather edit the "original" content and, if mailcap rendering produces ANSI CSI escapes, the user would be left with a bunch of garbarge characters. Make sure that the editnew command do not render the body when creating the envelope. --- alot/commands/thread.py | 2 +- alot/db/message.py | 4 ++-- alot/db/utils.py | 22 ++++++++++++++++------ 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/alot/commands/thread.py b/alot/commands/thread.py index ad3839991..e11a60794 100644 --- a/alot/commands/thread.py +++ b/alot/commands/thread.py @@ -466,7 +466,7 @@ async def apply(self, ui): 'signed', 'encrypted', 'unread', 'attachment'}) tags = list(tags) # set body text - mailcontent = self.message.get_body_text() + mailcontent = self.message.get_body_text(render=False) envelope = Envelope(bodytext=mailcontent, tags=tags) # copy selected headers diff --git a/alot/db/message.py b/alot/db/message.py index 6b1a3d9b8..4fdb62f99 100644 --- a/alot/db/message.py +++ b/alot/db/message.py @@ -282,9 +282,9 @@ def get_mime_part(self): def set_mime_part(self, mime_part): self._mime_part = mime_part - def get_body_text(self): + def get_body_text(self, render=True): """ returns bodystring extracted from this mail """ - return extract_body_part(self.get_mime_part()) + return extract_body_part(self.get_mime_part(), render=render) def matches(self, querystring): """tests if this messages is in the resultset for `querystring`""" diff --git a/alot/db/utils.py b/alot/db/utils.py index 892bc2872..8ce5368ff 100644 --- a/alot/db/utils.py +++ b/alot/db/utils.py @@ -491,13 +491,23 @@ def get_body_part(mail, mimetype=None): return body_part -def extract_body_part(body_part): - """Returns a string view of a Message.""" +def extract_body_part(body_part, render=True): + """ + Returns a string view of a Message. + + :param render: If true (the default), try to render the content with + `render_part`; otherwise skip rendering. + :type render: bool + """ displaystring = "" - rendered_payload = render_part( - body_part, - **{'field_key': 'view'} if body_part.get_content_type() == 'text/plain' - else {}) + if render: + rendered_payload = render_part( + body_part, + **{'field_key': 'view'} if body_part.get_content_type() == 'text/plain' + else {}) + else: + rendered_payload = None + if rendered_payload: # handler had output displaystring = string_sanitize(rendered_payload) elif body_part.get_content_type() == 'text/plain':