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.177]) by gnuweeb.org (Postfix) with ESMTPSA id F116580D4E; Sat, 1 Oct 2022 13:04:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1664629460; bh=MKARajSFPJIUtnkbgBTUjqltxCMNTOMmnoOs71q5/iE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XrpEgmkZJW43q92VOjtEJC4GTwnpeVhhOnXaFE0HZ4N4lepdL2q7nFVy0b7te1hoR I+n4aMw6AYz0Cs+/wMcwxXXyXFWCG/pW3b5Ydqj4cH53yNSqOc/NkcxWb82gQE8xcg WuUEoYJn7inlau9r3Qwb+k2x3Z9Iu2MC1CZON7gMsEoIF/rKD+63NvljK7uEavaDGy ODy8VennLgoNcwQNBmfXMFjg4Ur8hvgMh4+H5oH8yAvkpY6tzjR6X+9qyDXRQsXVFz +mP633SFYW9QMuMuTe++R4CPJ4sUptPwaIaaUuhAz9uaVyYzPPRuhdCoHDDFT0CjF1 jdY7qVBKW2AgA== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH v1 02/26] Fix the storage management after the refactor was happened Date: Sat, 1 Oct 2022 20:03:30 +0700 Message-Id: <20221001130355.784-3-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20221001130355.784-1-kiizuha@gnuweeb.org> References: <20221001130355.784-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit List-Id: I didn't test the Telegram bot when the refactor happened. I forgot to update the utility function, like creating a file or patch directory into the `storage` directory, which is so important. Add the `platform` parameter to the necessary functions and add quote_reply() for the Discord bot to create a vertical line reply display. The `platform` parameter is to tell it for `telegram` or `discord`. This will place the patch into its own `storage` directory. Signed-off-by: Muhammad Rizki --- daemon/atom/utils.py | 75 ++++++++++++++++++++++-------- daemon/telegram/mailer/listener.py | 2 +- daemon/telegram/packages/client.py | 4 +- 3 files changed, 59 insertions(+), 22 deletions(-) diff --git a/daemon/atom/utils.py b/daemon/atom/utils.py index c428a33..f813f39 100644 --- a/daemon/atom/utils.py +++ b/daemon/atom/utils.py @@ -113,25 +113,39 @@ def consruct_to_n_cc(to: list, cc: list): return ret -def gen_temp(name: str): +def gen_temp(name: str, platform: str): + platform = platform.lower() + plt_ls = ["telegram", "discord"] + + if platform not in plt_ls: + t = f"Platform {platform} is not found, " + t += f"only {', '.join(plt_ls)} is available" + raise ValueError(f"Platform {platform} is not found") + md5 = hashlib.md5(name.encode()).hexdigest() - ret = os.getenv("STORAGE_DIR", "storage") + "/" + md5 + store_dir = os.getenv("STORAGE_DIR", "storage") + platform = platform.replace("discord", "dscord") + path = f"{platform}/{store_dir}/{md5}" try: - os.mkdir(ret) + os.mkdir(path) except FileExistsError: pass - return ret + return path -def extract_body(thread: Message): +def extract_body(thread: Message, platform: str): if not thread.is_multipart(): - p = thread.get_payload(decode=True) - return f"{p.decode(errors='replace')}\n".lstrip(), [] + p = thread.get_payload(decode=True).decode(errors='replace') + + if platform == "discord": + p = quote_reply(p) + + return f"{p}\n".lstrip(), [] ret = "" files = [] - temp = gen_temp(str(uuid.uuid4())) + temp = gen_temp(str(uuid.uuid4()), platform) for p in thread.get_payload(): fname = p.get_filename() payload = p.get_payload(decode=True) @@ -164,35 +178,42 @@ def __is_patch(subject, content): return True -def create_template(thread: Message, to=None, cc=None): +def create_template(thread: Message, platform: str, to=None, cc=None): if not to: to = extract_list("to", thread) if not cc: cc = extract_list("cc", thread) + if platform == "telegram": + substr = 4000 + border = f"\n{'-'*72}" + else: + substr = 1900 + border = f"\n{'-'*80}" subject = thread.get('subject') ret = f"From: {thread.get('from')}\n" ret += consruct_to_n_cc(to, cc) ret += f"Date: {thread.get('date')}\n" ret += f"Subject: {subject}\n\n" - content, files = extract_body(thread) + content, files = extract_body(thread, platform) is_patch = __is_patch(subject, content) if is_patch: ret += content else: ret += content.strip().replace("\t", " ") - if len(ret) >= 4000: - ret = ret[:4000] + "..." - ret = fix_utf8_char(ret) - ret += f"\n{'-'*72}" + if len(ret) >= substr: + ret = ret[:substr] + "..." + + ret = fix_utf8_char(ret, platform == "telegram") + ret += border return ret, files, is_patch -def prepare_send_patch(mail, text, url): - tmp = gen_temp(url) +def prepare_send_patch(mail, text, url, platform: str): + tmp = gen_temp(url, platform) fnm = str(mail.get("subject")) sch = re.search(PATCH_PATTERN, fnm, re.IGNORECASE) @@ -210,7 +231,10 @@ def prepare_send_patch(mail, text, url): with open(file, "wb") as f: f.write(bytes(text, encoding="utf8")) - caption = "#patch #ml\n" + fix_utf8_char(cap) + caption = "#patch #ml" + if platform == "telegram": + caption += fix_utf8_char("\n" + cap, True) + return tmp, file, caption, url @@ -218,9 +242,11 @@ def clean_up_after_send_patch(tmp): shutil.rmtree(tmp) -def fix_utf8_char(text: str): - text = text.rstrip().replace("�"," ") - return html.escape(html.escape(text)) +def fix_utf8_char(text: str, html_escape: bool = True): + t = text.rstrip().replace("�"," ") + if html_escape: + t = html.escape(html.escape(text)) + return t EMAIL_MSG_ID_PATTERN = r"<([^\<\>]+)>" @@ -240,6 +266,15 @@ async def is_atom_url(text: str): return mime == "application/atom+xml" except: return False +def quote_reply(text: str): + a = "" + for b in text.split("\n"): + b = b.replace(">\n", "> ") + if b.startswith(">"): + a += "> " + a += f"{b}\n" + return a + def remove_command(text: str): txt = text.split(" ") txt = text.replace(txt[0] + " ","") diff --git a/daemon/telegram/mailer/listener.py b/daemon/telegram/mailer/listener.py index decf85f..208aed0 100644 --- a/daemon/telegram/mailer/listener.py +++ b/daemon/telegram/mailer/listener.py @@ -99,7 +99,7 @@ class Bot(): # return False - text, files, is_patch = utils.create_template(mail) + text, files, is_patch = utils.create_template(mail, "telegram") reply_to = self.get_reply(mail, tg_chat_id) url = str(re.sub(r"/raw$", "", url)) diff --git a/daemon/telegram/packages/client.py b/daemon/telegram/packages/client.py index 4f9c596..17061ec 100644 --- a/daemon/telegram/packages/client.py +++ b/daemon/telegram/packages/client.py @@ -56,7 +56,9 @@ class DaemonClient(Client): parse_mode: ParseMode = ParseMode.HTML ) -> Message: print("[send_patch_email]") - tmp, doc, caption, url = utils.prepare_send_patch(mail, text, url) + tmp, doc, caption, url = utils.prepare_send_patch( + mail, text, url, "telegram" + ) m = await self.send_document( chat_id=chat_id, document=doc, -- Muhammad Rizki