| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- """
- CREDITS:
- THIS MODULE IS COMPLETELY WRITTEN BY @Pokurt.
- SOURCE:
- https://github.com/pokurt/Nana-Remix/blob/master/nana/plugins/devs.py
- """
- import os
- import re
- import subprocess
- import sys
- import traceback
- from io import StringIO
- from time import time
- from pyrogram import filters
- from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, Message
- from wbb import SUDOERS, app, eor
- # Module help for this is in sudoers.py
- async def aexec(code, client, message):
- exec(
- "async def __aexec(client, message): "
- + "".join(f"\n {a}" for a in code.split("\n"))
- )
- return await locals()["__aexec"](client, message)
- @app.on_message(
- filters.user(SUDOERS)
- & ~filters.forwarded
- & ~filters.via_bot
- & ~filters.edited
- & filters.command("eval")
- )
- async def executor(client, message: Message):
- try:
- cmd = message.text.split(" ", maxsplit=1)[1]
- except IndexError:
- return await message.delete()
- t1 = time()
- old_stderr = sys.stderr
- old_stdout = sys.stdout
- redirected_output = sys.stdout = StringIO()
- redirected_error = sys.stderr = StringIO()
- stdout, stderr, exc = None, None, None
- try:
- await aexec(cmd, client, 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 = f"**OUTPUT**:\n```{evaluation.strip()}```"
- if len(final_output) > 4096:
- filename = "output.txt"
- with open(filename, "w+", encoding="utf8") as out_file:
- out_file.write(str(evaluation.strip()))
- t2 = time()
- keyboard = InlineKeyboardMarkup(
- [
- [
- InlineKeyboardButton(
- text="⏳",
- callback_data=f"runtime {t2-t1} Seconds",
- )
- ]
- ]
- )
- await message.reply_document(
- document=filename,
- caption=f"**INPUT:**\n`{cmd[0:980]}`\n\n**OUTPUT:**\n`Attached Document`",
- quote=False,
- reply_markup=keyboard,
- )
- await message.delete()
- os.remove(filename)
- else:
- t2 = time()
- keyboard = InlineKeyboardMarkup(
- [
- [
- InlineKeyboardButton(
- text="⏳",
- callback_data=f"runtime {round(t2-t1, 3)} Seconds",
- )
- ]
- ]
- )
- await eor(message, text=final_output, reply_markup=keyboard)
- @app.on_callback_query(filters.regex(r"runtime"))
- async def runtime_func_cq(_, cq):
- runtime = cq.data.split(None, 1)[1]
- await cq.answer(runtime, show_alert=True)
- @app.on_message(
- filters.user(SUDOERS)
- & ~filters.forwarded
- & ~filters.via_bot
- & ~filters.edited
- & filters.command("sh"),
- )
- async def shellrunner(_, message: Message):
- if len(message.command) < 2:
- return await eor(message, text="**Usage:**\n/sh git pull")
- text = message.text.split(None, 1)[1]
- if "\n" in text:
- code = text.split("\n")
- output = ""
- for x in code:
- shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x)
- try:
- process = subprocess.Popen(
- shell,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- )
- except Exception as err:
- print(err)
- await eor(message, text=f"**ERROR:**\n```{err}```")
- output += f"**{code}**\n"
- output += process.stdout.read()[:-1].decode("utf-8")
- output += "\n"
- else:
- shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
- for a, _ in enumerate(shell):
- shell[a] = shell[a].replace('"', "")
- try:
- process = subprocess.Popen(
- shell,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- )
- except Exception as err:
- print(err)
- exc_type, exc_obj, exc_tb = sys.exc_info()
- errors = traceback.format_exception(
- etype=exc_type,
- value=exc_obj,
- tb=exc_tb,
- )
- return await eor(
- message, text=f"**ERROR:**\n```{''.join(errors)}```"
- )
- output = process.stdout.read()[:-1].decode("utf-8")
- if str(output) == "\n":
- output = None
- if output:
- if len(output) > 4096:
- with open("output.txt", "w+") as file:
- file.write(output)
- await app.send_document(
- message.chat.id,
- "output.txt",
- reply_to_message_id=message.message_id,
- caption="`Output`",
- )
- return os.remove("output.txt")
- await eor(message, text=f"**OUTPUT:**\n```{output}```")
- else:
- await eor(message, text="**OUTPUT: **\n`No output`")
|