From 9db6e6c77cb301685fedf2007b8122de98ec45ed Mon Sep 17 00:00:00 2001 From: rking32 Date: Mon, 16 Mar 2020 23:17:03 +0530 Subject: [PATCH] stolen plugins from @spechides --- userge/__init__.py | 2 +- userge/client.py | 2 +- userge/plugins/all.py | 2 +- userge/plugins/eval.py | 78 +++++++++++++++++++++++++++++++++++++++++ userge/plugins/exec.py | 58 ++++++++++++++++++++++++++++++ userge/plugins/help.py | 2 +- userge/plugins/json.py | 39 +++++++++++++++++++++ userge/plugins/whois.py | 48 +++++++++++++++++++++++++ userge/utils/config.py | 2 ++ 9 files changed, 229 insertions(+), 4 deletions(-) create mode 100644 userge/plugins/eval.py create mode 100644 userge/plugins/exec.py create mode 100644 userge/plugins/json.py create mode 100644 userge/plugins/whois.py diff --git a/userge/__init__.py b/userge/__init__.py index 38bbab62b..9b125b0c9 100644 --- a/userge/__init__.py +++ b/userge/__init__.py @@ -1,4 +1,4 @@ -from .utils import logging +from .utils import Config, logging log = logging.getLogger(__name__) diff --git a/userge/client.py b/userge/client.py index 120808851..a4266a8e3 100644 --- a/userge/client.py +++ b/userge/client.py @@ -28,7 +28,7 @@ def on_cmd(self, command: str): self.log.info(f"setting new command => {command}") def decorator(func): - dec = self.on_message(Filters.command(command, ".") & Filters.me) + dec = self.on_message(Filters.regex(pattern=f"^.{command}") & Filters.me) return dec(func) return decorator diff --git a/userge/plugins/all.py b/userge/plugins/all.py index 80b878a00..42196e4d9 100644 --- a/userge/plugins/all.py +++ b/userge/plugins/all.py @@ -10,7 +10,7 @@ async def getplugins(_, message: Message): all_plugins = await get_all_plugins() out_str = "" for plugin in all_plugins: - out_str += f"{plugin}.py\n" + out_str += f"**{plugin}**\n" await message.edit(out_str) diff --git a/userge/plugins/eval.py b/userge/plugins/eval.py new file mode 100644 index 000000000..a7ebc0045 --- /dev/null +++ b/userge/plugins/eval.py @@ -0,0 +1,78 @@ +import io +import os +import sys +import traceback +from pyrogram import Message +from userge import userge, Config + +log = userge.getLogger(__name__) + + +@userge.on_cmd("eval") +async def eval_(_, message: Message): + await message.edit("Processing ...") + cmd = message.text.split(" ", maxsplit=1)[1] + reply_to_id = message.message_id + + if message.reply_to_message: + reply_to_id = message.reply_to_message.message_id + + old_stderr = sys.stderr + old_stdout = sys.stdout + redirected_output = sys.stdout = io.StringIO() + redirected_error = sys.stderr = io.StringIO() + stdout, stderr, exc = None, None, None + + try: + await aexec(cmd, userge, message) + except Exception: + exc = traceback.format_exc() + + stdout = redirected_output.getvalue() + stderr = redirected_error.getvalue() + sys.stdout = old_stdout + sys.stderr = old_stderr + + evaluation = "" + if exc: + evaluation = exc + elif stderr: + evaluation = stderr + elif stdout: + evaluation = stdout + else: + evaluation = "Success" + + final_output = "**EVAL**: ```{}```\n\n**OUTPUT**:\n```{}``` \n".format(cmd, evaluation.strip()) + + if len(final_output) > Config.MAX_MESSAGE_LENGTH: + with open("eval.text", "w+", encoding="utf8") as out_file: + out_file.write(str(final_output)) + + await userge.send_document( + chat_id=message.chat.id, + document="eval.text", + caption=cmd, + disable_notification=True, + reply_to_message_id=reply_to_id + ) + + os.remove("eval.text") + await message.delete() + + else: + await message.edit(final_output) + + +async def aexec(code, userge, message): + exec( + f'async def __aexec(userge, message): ' + + ''.join(f'\n {l}' for l in code.split('\n')) + ) + return await locals()['__aexec'](userge, message) + + +userge.add_help( + command="eval", + about="run eval" +) \ No newline at end of file diff --git a/userge/plugins/exec.py b/userge/plugins/exec.py new file mode 100644 index 000000000..0db3b121c --- /dev/null +++ b/userge/plugins/exec.py @@ -0,0 +1,58 @@ +import asyncio +import os +import time +from pyrogram import Message +from userge import userge, Config + +log = userge.getLogger(__name__) + + +@userge.on_cmd("exec") +async def exec_(_, message: Message): + cmd = message.text.split(" ", maxsplit=1)[1] + reply_to_id = message.message_id + + if message.reply_to_message: + reply_to_id = message.reply_to_message.message_id + + process = await asyncio.create_subprocess_shell( + cmd, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE + ) + stdout, stderr = await process.communicate() + + e = stderr.decode() or "No Error" + o = stdout.decode() + + if o: + _o = o.split("\n") + o = "`\n".join(_o) + else: + o = "**Tip**: \n`If you want to see the results of your code, I suggest printing them to stdout.`" + + OUTPUT = f"**QUERY:**\n__Command:__\n`{cmd}` \n__PID:__\n`{process.pid}`\n\n**stderr:** \n`{e}`\n**Output:**\n{o}" + + if len(OUTPUT) > Config.MAX_MESSAGE_LENGTH: + with open("exec.text", "w+", encoding="utf8") as out_file: + out_file.write(str(OUTPUT)) + + await userge.send_document( + chat_id=message.chat.id, + document="exec.text", + caption=cmd, + disable_notification=True, + reply_to_message_id=reply_to_id + ) + + os.remove("exec.text") + await message.delete() + + else: + await message.edit(OUTPUT) + + +userge.add_help( + command="exec", + about="run exec" +) \ No newline at end of file diff --git a/userge/plugins/help.py b/userge/plugins/help.py index 70ac8c992..6df176e94 100644 --- a/userge/plugins/help.py +++ b/userge/plugins/help.py @@ -8,7 +8,7 @@ async def helpme(_, message: Message): out_str = "" for cmd in userge.get_help(): - out_str += f".{cmd} : {userge.get_help(cmd)}\n" + out_str += f"**.{cmd}** : __{userge.get_help(cmd)}__\n" await message.edit(out_str) diff --git a/userge/plugins/json.py b/userge/plugins/json.py new file mode 100644 index 000000000..2dabb2820 --- /dev/null +++ b/userge/plugins/json.py @@ -0,0 +1,39 @@ +import os +from pyrogram import Message +from userge import userge + +log = userge.getLogger(__name__) + + +@userge.on_cmd("json") +async def jsonify(_, message: Message): + the_real_message = None + reply_to_id = None + + if message.reply_to_message: + reply_to_id = message.reply_to_message.message_id + the_real_message = message.reply_to_message + else: + the_real_message = message + reply_to_id = message.message_id + + try: + await message.edit(the_real_message) + except Exception as e: + with open("json.text", "w+", encoding="utf8") as out_file: + out_file.write(str(the_real_message)) + await userge.send_document( + chat_id=message.chat.id, + document="json.text", + caption=str(e), + disable_notification=True, + reply_to_message_id=reply_to_id + ) + os.remove("json.text") + await message.delete() + + +userge.add_help( + command="json", + about="replied msg to json" +) \ No newline at end of file diff --git a/userge/plugins/whois.py b/userge/plugins/whois.py new file mode 100644 index 000000000..d1fdf1095 --- /dev/null +++ b/userge/plugins/whois.py @@ -0,0 +1,48 @@ +import os +from pyrogram import Message +from userge import userge + +log = userge.getLogger(__name__) + + +@userge.on_cmd("whois") +async def who_is(_, message: Message): + from_user = None + if " " in message.text: + recvd_command, user_id = message.text.split(" ") + try: + user_id = int(user_id) + from_user = await userge.get_users(user_id) + except Exception as e: + await message.edit(str(e)) + return + elif message.reply_to_message: + from_user = message.reply_to_message.from_user + else: + await message.edit("no valid user_id / message specified") + return + if from_user is not None: + message_out_str = "" + message_out_str += f"ID: `{from_user.id}`\n" + message_out_str += f"First Name: {from_user.first_name}\n" + message_out_str += f"Last Name: {from_user.last_name}" + chat_photo = from_user.photo + local_user_photo = await userge.download_media( + message=chat_photo.big_file_id + ) + await message.reply_photo( + photo=local_user_photo, + quote=True, + caption=message_out_str, + parse_mode="html", + # ttl_seconds=, + disable_notification=True + ) + os.remove(local_user_photo) + await message.delete() + + +userge.add_help( + command="whois", + about="to get user details" +) \ No newline at end of file diff --git a/userge/utils/config.py b/userge/utils/config.py index e9319618b..ffad0c34f 100644 --- a/userge/utils/config.py +++ b/userge/utils/config.py @@ -23,3 +23,5 @@ class Config: HU_STRING_SESSION = os.environ.get("HU_STRING_SESSION", None) DB_URI = os.environ.get("DATABASE_URL", None) + + MAX_MESSAGE_LENGTH = 4096