GNU/Weeb Mailing List <[email protected]>
 help / color / mirror / Atom feed
From: Muhammad Rizki <[email protected]>
To: Ammar Faizi <[email protected]>
Cc: Muhammad Rizki <[email protected]>,
	Alviro Iskandar Setiawan <[email protected]>,
	GNU/Weeb Mailing List <[email protected]>
Subject: [PATCH v4 5/9] atom: add get_decoded_payload()
Date: Sat, 22 Oct 2022 13:51:45 +0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>

Add get_decoded_payload() to handle the email decoding to utf-8. This
include a non-UTF8 character, base64 decoding, and quoted-printable
decoding.

Signed-off-by: Muhammad Rizki <[email protected]>
---
 daemon/atom/utils.py | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/daemon/atom/utils.py b/daemon/atom/utils.py
index f554f6f..deff99d 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
@@ -15,6 +16,7 @@ import re
 import shutil
 import httpx
 import html
+import quopri
 
 
 def get_email_msg_id(mail):
@@ -136,7 +138,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 = get_decoded_payload(thread)
 
 		if platform == "discord":
 			p = quote_reply(p)
@@ -253,6 +255,20 @@ def fix_utf8_char(text: str, html_escape: bool = True):
 	return t
 
 
+def get_decoded_payload(payload: Message):
+	p = str(payload.get_payload())
+	tf_encode = payload.get("Content-Transfer-Encoding")
+	charset = payload.get_content_charset("utf-8")
+
+	if tf_encode == "base64":
+		return b64decode(p).decode(charset)
+	if tf_encode == "quoted-printable":
+		quobyte = quopri.decodestring(p.encode())
+		return quobyte.decode(charset)
+
+	return p.encode().decode(charset, errors="replace")
+
+
 EMAIL_MSG_ID_PATTERN = r"<([^\<\>]+)>"
 def extract_email_msg_id(msg_id):
 	ret = re.search(EMAIL_MSG_ID_PATTERN, msg_id)
-- 
Muhammad Rizki


  parent reply	other threads:[~2022-10-22  6:52 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-22  6:51 [PATCH v4 0/9] Fix some bugs and add some features Muhammad Rizki
2022-10-22  6:51 ` [PATCH v4 1/9] discord: Add send_text_mail_interaction() Muhammad Rizki
2022-10-22  6:51 ` [PATCH v4 2/9] discord: Add send_patch_mail_interaction() Muhammad Rizki
2022-10-22  6:51 ` [PATCH v4 3/9] discord: Add get lore mail slash command Muhammad Rizki
2022-10-22  6:51 ` [PATCH v4 4/9] atom: Improve remove_patch() Muhammad Rizki
2022-10-22  6:51 ` Muhammad Rizki [this message]
2022-10-22  6:51 ` [PATCH v4 6/9] telegram: Fix get lore command Muhammad Rizki
2022-10-22  6:51 ` [PATCH v4 7/9] atom: Improve extract_body() Muhammad Rizki
2022-10-22  6:51 ` [PATCH v4 8/9] enum: Add Platform enumeration Muhammad Rizki
2022-10-22  6:51 ` [PATCH v4 9/9] enum: Use the created Platform class enumeration Muhammad Rizki
2022-10-22  7:44 ` [PATCH v4 0/9] Fix some bugs and add some features Ammar Faizi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox