From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-0.8 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NO_DNS_FOR_FROM,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.6 Received: from localhost.localdomain (unknown [101.128.125.87]) by gnuweeb.org (Postfix) with ESMTPSA id 5DC1C81116; Tue, 18 Oct 2022 08:17:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1666081066; bh=JP2HSIkX3sqhAK0QcAUqgG6LFCCRe/1lqxMdIIacJoM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LjyUdlzzzKFEqkCJqI4Z/7tTwV5C/rRt1VOTjHNN7rwN/ltnmvv2rhKT2TWIoElYR 6IveIwF4oUzGEcaEPVMQAqPp/HwF0EL5+LrtaMm90rFQukJr/+8YdLjGUenoguNqfz LynVLSGOUSeBb02JMABkQ3Fi2vBLslGYpg1PSeQ8tNFFZ9kfRPJfwzT8ENvi/XuuHQ g99gRxxnqqy1LCWgnksRQ3n09XO53LrBqQSM1oK4Mvzq04xYWWaRtpsJyZu1CLW674 HTBgDiWI5wJP7IAyJLAqZboM2eZvD3jE+hmNoAJ89BV8b7il0ZEQW3m5Zew0/n/ixj KRETlcYy8+XvQ== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v1 6/7] atom: add manage_payload() Date: Tue, 18 Oct 2022 15:16:34 +0700 Message-Id: <20221018081635.1617-7-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20221018081635.1617-1-kiizuha@gnuweeb.org> References: <20221018081635.1617-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Add manage_payload() to handle the email decoding to utf-8. This include a non-UTF8 character and base64 decoding. Signed-off-by: Muhammad Rizki --- daemon/atom/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/daemon/atom/utils.py b/daemon/atom/utils.py index c129925..e24c5df 100644 --- a/daemon/atom/utils.py +++ b/daemon/atom/utils.py @@ -8,6 +8,7 @@ from pyrogram.types import Chat, InlineKeyboardMarkup, InlineKeyboardButton from email.message import Message from typing import Dict, Union from slugify import slugify +from base64 import b64decode import hashlib import uuid import os @@ -136,7 +137,7 @@ def gen_temp(name: str, platform: str): def extract_body(thread: Message, platform: str): if not thread.is_multipart(): - p = thread.get_payload(decode=True).decode(errors='replace') + p = manage_payload(thread) if platform == "discord": p = quote_reply(p) @@ -255,6 +256,14 @@ def fix_utf8_char(text: str, unescape: bool = True): return t +def manage_payload(payload: Message): + p = str(payload.get_payload()) + tf_encode = payload.get("Content-Transfer-Encoding") + if tf_encode != "base64": + return p.encode().decode("utf-8", errors="replace") + return b64decode(p).decode("utf-8") + + EMAIL_MSG_ID_PATTERN = r"<([^\<\>]+)>" def extract_email_msg_id(msg_id): ret = re.search(EMAIL_MSG_ID_PATTERN, msg_id) -- Muhammad Rizki