From 215a022ed845d5aa91469a87e9fd427422ff0bc6 Mon Sep 17 00:00:00 2001 From: Matthieu Baerts Date: Mon, 23 Nov 2026 11:07:12 +0100 Subject: [PATCH] mailbot: decode base64 bodies Some emails are sent with 'Content-Transfer-Encoding: base64'. In this case, _body() returned the raw base64 content, and the 'pw-bot:' and 'doc-bot:' lines could not be found. Use get_content() to decode base64 parts, for both single-part and multi-part emails. The previous behaviour is kept for other encodings. While at it, errors while decoding the body of a single-part email are now also caught, and extract_actions() handles a missing body. Signed-off-by: Matthieu Baerts --- mailbot.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/mailbot.py b/mailbot.py index 0eaf97a4..d06fd442 100755 --- a/mailbot.py +++ b/mailbot.py @@ -281,20 +281,24 @@ def _body(self): Email is hard, get_body() doesn't decode base64, get_payload() does but it's too MIME-aware. Luckily ML traffic is not multi-part 99% of the time. And it's not base64 90% of the time. So hopefully we'll cover 99.9% here.. + When the part is base64-encoded, decode it with get_content(). """ if self.msg.is_multipart(): body = self.msg.get_body(preferencelist=('plain',)) if body is None: return None - - try: - body_str = body.as_string() - except LookupError as e: - print('', '', "ERROR: can't parse body", e) - return None - return body_str else: - return self.msg.get_payload() + body = self.msg + + try: + if body.get('Content-Transfer-Encoding', '').strip().lower() == 'base64': + return body.get_content() + if body is self.msg: + return body.get_payload() + return body.as_string() + except (LookupError, ValueError) as e: + print('', '', "ERROR: can't parse body", e) + return None def user_authorized(self, pw=None): if self._authorized is None: @@ -429,7 +433,8 @@ def extract_actions(self, pw): return if self.user_authorized(pw) or self.self_reply(pw): - lines = self._body().split('\n') + body_str = self._body() or '' + lines = body_str.split('\n') for line in lines: if line.startswith('pw-bot:'): self.actions.append(line)