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.100]) by gnuweeb.org (Postfix) with ESMTPSA id 3DD1980D04; Tue, 6 Sep 2022 11:19:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=default; t=1662463190; bh=4SLnYuTIi5c3M9zVHn3nPeu6/dVLBr9gDnxWC+cvoqo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pq2GmpNMDhpAoD/lhpeL8RGgvn3XKd8n09dmUa793PRbTX9NrMhQ3mugaoHjJ9rSp GoqxSrRpN0NjbOWKjnNHX+rVgb7euJVDYklfVQ4uKZFEJF6lZCpWOj9Gd4QfEzntbT XwdHtex5op0S/FNJsqpz6BAxrOx4zFz0zJvM7Ybs2h0nX/HEwXhNaD6PA+q8hRUjOi 10J3NVFVPBdwlZwlE0ypXlqWTCzgJa29XHsEMt7Kdr7+0mlmOBqi1XGkK7nnpujEtq QTGYmvJ4HsYGg5qEJZn9RLYsZ9vsgkQ67VfIPYOU4nbM2qGjQ7Dp2xX7Np4dHNGsRA e18O3aQqzHHVQ== From: Muhammad Rizki To: Ammar Faizi Cc: Muhammad Rizki , GNU/Weeb Mailing List , Alviro Iskandar Setiawan Subject: [RFC PATCH v1 1/5] [telegram] Move the Telegram bot source code Date: Tue, 6 Sep 2022 18:19:25 +0700 Message-Id: <20220906111929.1657-2-kiizuha@gnuweeb.org> X-Mailer: git-send-email 2.34.1.windows.1 In-Reply-To: <20220906111929.1657-1-kiizuha@gnuweeb.org> References: <20220906111929.1657-1-kiizuha@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Move them into their own directory because there is another platform bot in the future, so it's better to split them. Signed-off-by: Muhammad Rizki --- .gitignore | 3 +- daemon/{ => telegram}/.env.example | 0 daemon/{ => telegram}/config.py.example | 0 daemon/{ => telegram}/db.sql | 0 daemon/{ => telegram}/packages/__init__.py | 0 daemon/{ => telegram}/packages/client.py | 0 daemon/{ => telegram}/packages/decorator.py | 0 daemon/telegram/packages/plugins/admin.py | 42 +++++++++ .../packages/plugins/callbacks/del_atom.py | 0 .../packages/plugins/callbacks/del_chat.py | 0 .../packages/plugins/commands/debugger.py | 0 .../packages/plugins/commands/manage_atom.py | 0 .../plugins/commands/manage_broadcast.py | 0 .../packages/plugins/commands/scrape.py | 0 daemon/telegram/packages/plugins/scrape.py | 86 +++++++++++++++++++ daemon/{ => telegram}/requirements.txt | 0 daemon/{ => telegram}/run.py | 0 daemon/{ => telegram}/scraper/__init__.py | 0 daemon/{ => telegram}/scraper/bot.py | 0 daemon/{ => telegram}/scraper/db.py | 0 daemon/{ => telegram}/scraper/scraper.py | 0 daemon/{ => telegram}/scraper/utils.py | 0 daemon/{ => telegram}/storage/.gitignore | 0 23 files changed, 130 insertions(+), 1 deletion(-) rename daemon/{ => telegram}/.env.example (100%) rename daemon/{ => telegram}/config.py.example (100%) rename daemon/{ => telegram}/db.sql (100%) rename daemon/{ => telegram}/packages/__init__.py (100%) rename daemon/{ => telegram}/packages/client.py (100%) rename daemon/{ => telegram}/packages/decorator.py (100%) create mode 100644 daemon/telegram/packages/plugins/admin.py rename daemon/{ => telegram}/packages/plugins/callbacks/del_atom.py (100%) rename daemon/{ => telegram}/packages/plugins/callbacks/del_chat.py (100%) rename daemon/{ => telegram}/packages/plugins/commands/debugger.py (100%) rename daemon/{ => telegram}/packages/plugins/commands/manage_atom.py (100%) rename daemon/{ => telegram}/packages/plugins/commands/manage_broadcast.py (100%) rename daemon/{ => telegram}/packages/plugins/commands/scrape.py (100%) create mode 100644 daemon/telegram/packages/plugins/scrape.py rename daemon/{ => telegram}/requirements.txt (100%) rename daemon/{ => telegram}/run.py (100%) rename daemon/{ => telegram}/scraper/__init__.py (100%) rename daemon/{ => telegram}/scraper/bot.py (100%) rename daemon/{ => telegram}/scraper/db.py (100%) rename daemon/{ => telegram}/scraper/scraper.py (100%) rename daemon/{ => telegram}/scraper/utils.py (100%) rename daemon/{ => telegram}/storage/.gitignore (100%) diff --git a/.gitignore b/.gitignore index 4de282d..4201a17 100644 --- a/.gitignore +++ b/.gitignore @@ -140,4 +140,5 @@ data.json *.patch # configuration file -daemon/config.py +daemon/telegram/config.py +daemon/discord/config.py diff --git a/daemon/.env.example b/daemon/telegram/.env.example similarity index 100% rename from daemon/.env.example rename to daemon/telegram/.env.example diff --git a/daemon/config.py.example b/daemon/telegram/config.py.example similarity index 100% rename from daemon/config.py.example rename to daemon/telegram/config.py.example diff --git a/daemon/db.sql b/daemon/telegram/db.sql similarity index 100% rename from daemon/db.sql rename to daemon/telegram/db.sql diff --git a/daemon/packages/__init__.py b/daemon/telegram/packages/__init__.py similarity index 100% rename from daemon/packages/__init__.py rename to daemon/telegram/packages/__init__.py diff --git a/daemon/packages/client.py b/daemon/telegram/packages/client.py similarity index 100% rename from daemon/packages/client.py rename to daemon/telegram/packages/client.py diff --git a/daemon/packages/decorator.py b/daemon/telegram/packages/decorator.py similarity index 100% rename from daemon/packages/decorator.py rename to daemon/telegram/packages/decorator.py diff --git a/daemon/telegram/packages/plugins/admin.py b/daemon/telegram/packages/plugins/admin.py new file mode 100644 index 0000000..e0f145e --- /dev/null +++ b/daemon/telegram/packages/plugins/admin.py @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Copyright (C) 2022 Muhammad Rizki +# + +from pyrogram import Client, filters, enums +from pyrogram.types import Message +from textwrap import indent +import io, import_expression, contextlib, traceback + +@Client.on_message( + filters.command(['d','debug']) & + filters.user(["nekoha", "kiizuah"]) +) +async def execute_v2(c: Client, m: Message): + sep = m.text.split('\n') + body = m.text.replace(sep[0] + '\n','') + + env = {"bot": c} + env.update(globals()) + + stdout = io.StringIO() + to_compile = f'async def func(_, m):\n{indent(body, " ")}' + + try: + import_expression.exec(to_compile, env) + except Exception as e: + text = f"```{e.__class__.__name__}: {e}"[0:4096]+"```" + + func = env["func"] + + try: + with contextlib.redirect_stdout(stdout): + await func(c, m) + except Exception: + value = stdout.getvalue() + text = f"```{value}{traceback.format_exc()}"[0:4096]+"```" + else: + value = stdout.getvalue() + text = f"```{value}"[0:4096]+"```" + + await c.send_message(m.chat.id, text, parse_mode=enums.ParseMode.MARKDOWN) diff --git a/daemon/packages/plugins/callbacks/del_atom.py b/daemon/telegram/packages/plugins/callbacks/del_atom.py similarity index 100% rename from daemon/packages/plugins/callbacks/del_atom.py rename to daemon/telegram/packages/plugins/callbacks/del_atom.py diff --git a/daemon/packages/plugins/callbacks/del_chat.py b/daemon/telegram/packages/plugins/callbacks/del_chat.py similarity index 100% rename from daemon/packages/plugins/callbacks/del_chat.py rename to daemon/telegram/packages/plugins/callbacks/del_chat.py diff --git a/daemon/packages/plugins/commands/debugger.py b/daemon/telegram/packages/plugins/commands/debugger.py similarity index 100% rename from daemon/packages/plugins/commands/debugger.py rename to daemon/telegram/packages/plugins/commands/debugger.py diff --git a/daemon/packages/plugins/commands/manage_atom.py b/daemon/telegram/packages/plugins/commands/manage_atom.py similarity index 100% rename from daemon/packages/plugins/commands/manage_atom.py rename to daemon/telegram/packages/plugins/commands/manage_atom.py diff --git a/daemon/packages/plugins/commands/manage_broadcast.py b/daemon/telegram/packages/plugins/commands/manage_broadcast.py similarity index 100% rename from daemon/packages/plugins/commands/manage_broadcast.py rename to daemon/telegram/packages/plugins/commands/manage_broadcast.py diff --git a/daemon/packages/plugins/commands/scrape.py b/daemon/telegram/packages/plugins/commands/scrape.py similarity index 100% rename from daemon/packages/plugins/commands/scrape.py rename to daemon/telegram/packages/plugins/commands/scrape.py diff --git a/daemon/telegram/packages/plugins/scrape.py b/daemon/telegram/packages/plugins/scrape.py new file mode 100644 index 0000000..1698c6d --- /dev/null +++ b/daemon/telegram/packages/plugins/scrape.py @@ -0,0 +1,86 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Copyright (C) 2022 Muhammad Rizki +# Copyright (C) 2022 Ammar Faizi +# + +from pyrogram.types import InlineKeyboardMarkup +from pyrogram.types import InlineKeyboardButton +from pyrogram.types import Message +from pyrogram import filters +from pyrogram import Client +from scraper import Scraper +from pyrogram import enums +from scraper import utils +from scraper import Bot +import shutil +import re +import asyncio + + +# +# This allows user to invoke the following commands: +# /lore https://lore.kernel.org/path/message_id/raw +# !lore https://lore.kernel.org/path/message_id/raw +# .lore https://lore.kernel.org/path/message_id/raw +# +LORE_CMD_URL_PATTERN = r"^(?:\/|\.|\!)lore\s+(https?:\/\/lore\.kernel\.org\/\S+)" +@Client.on_message( + filters.regex(LORE_CMD_URL_PATTERN) & + filters.chat(["kiizuah", "nekoha", -1001673279485]) +) +async def scrap_email(_, m: Message): + p = re.search(LORE_CMD_URL_PATTERN, m.text) + if not p: + return + + url = p.group(1) + if not url: + return + + s = Scraper() + mail = await s.get_email_from_url(url) + text, files, is_patch = utils.create_template(mail) + + if is_patch: + m = await __send_patch_msg(m, mail, text, url) + else: + text = "#ml\n" + text + m = await __send_text_msg(m, text, url) + + for d, f in files: + await m.reply_document(f"{d}/{f}", file_name=f) + await asyncio.sleep(1) + + if files: + shutil.rmtree(str(files[0][0])) + + +async def __send_patch_msg(m, mail, text, url): + tmp, fnm, caption, url = Bot.prepare_send_patch(mail, text, url) + ret = await m.reply_document( + fnm, + caption=caption, + parse_mode=enums.ParseMode.HTML, + reply_markup=InlineKeyboardMarkup([ + [InlineKeyboardButton( + "See the full message", + url=url + )] + ]) + ) + Bot.clean_up_after_send_patch(tmp) + return ret + + +async def __send_text_msg(m, text, url): + return await m.reply( + text, + parse_mode=enums.ParseMode.HTML, + reply_markup=InlineKeyboardMarkup([ + [InlineKeyboardButton( + "See the full message", + url=url.replace("/raw","") + )] + ]) + ) diff --git a/daemon/requirements.txt b/daemon/telegram/requirements.txt similarity index 100% rename from daemon/requirements.txt rename to daemon/telegram/requirements.txt diff --git a/daemon/run.py b/daemon/telegram/run.py similarity index 100% rename from daemon/run.py rename to daemon/telegram/run.py diff --git a/daemon/scraper/__init__.py b/daemon/telegram/scraper/__init__.py similarity index 100% rename from daemon/scraper/__init__.py rename to daemon/telegram/scraper/__init__.py diff --git a/daemon/scraper/bot.py b/daemon/telegram/scraper/bot.py similarity index 100% rename from daemon/scraper/bot.py rename to daemon/telegram/scraper/bot.py diff --git a/daemon/scraper/db.py b/daemon/telegram/scraper/db.py similarity index 100% rename from daemon/scraper/db.py rename to daemon/telegram/scraper/db.py diff --git a/daemon/scraper/scraper.py b/daemon/telegram/scraper/scraper.py similarity index 100% rename from daemon/scraper/scraper.py rename to daemon/telegram/scraper/scraper.py diff --git a/daemon/scraper/utils.py b/daemon/telegram/scraper/utils.py similarity index 100% rename from daemon/scraper/utils.py rename to daemon/telegram/scraper/utils.py diff --git a/daemon/storage/.gitignore b/daemon/telegram/storage/.gitignore similarity index 100% rename from daemon/storage/.gitignore rename to daemon/telegram/storage/.gitignore -- Muhammad Rizki