| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- """
- CREDITS:
- MOST OF THE CODE IN THIS FILE IS 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 html import escape
- from io import StringIO
- from pyrogram import filters
- from pyrogram.enums import ChatType
- from pyrogram.errors import MessageNotModified
- from pyrogram.types import Message, ReplyKeyboardMarkup
- from wbb import app2 # don't remove
- from wbb import SUDOERS, USERBOT_PREFIX, eor
- from wbb.core.tasks import add_task, rm_task
- # Eval and Sh module from nana-remix
- m = None
- p = print
- r = None
- arrow = lambda x: (x.text if isinstance(x, Message) else "") + "\n`→`"
- 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)
- async def iter_edit(message: Message, text: str):
- async for m in app2.get_chat_history(message.chat.id):
- # If no replies found, reply
- if m.id == message.id:
- return 0
- if not m.from_user or not m.text or not m.reply_to_message:
- continue
- if (
- (m.reply_to_message.id == message.id)
- and (m.from_user.id == message.from_user.id)
- and ("→" in m.text)
- ):
- try:
- return await m.edit(text)
- except MessageNotModified:
- return
- @app2.on_message(
- SUDOERS
- & ~filters.forwarded
- & ~filters.via_bot
- & filters.command("eval", prefixes=USERBOT_PREFIX)
- )
- async def executor(client, message: Message):
- global m, p, r
- if not message.from_user or message.from_user.id not in SUDOERS:
- return
- try:
- cmd = message.text.split(" ", maxsplit=1)[1]
- except IndexError:
- return await message.delete()
- if message.chat.type == ChatType.CHANNEL:
- return
- m = message
- p = print
- # To prevent keyboard input attacks
- if m.reply_to_message:
- r = m.reply_to_message
- if r.reply_markup and isinstance(r.reply_markup, ReplyKeyboardMarkup):
- return await eor(m, text="INSECURE!")
- status = None
- 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:
- task, task_id = await add_task(
- aexec,
- "Eval",
- cmd,
- client,
- m,
- )
- text = f"{arrow('')} Pending Task `{task_id}`"
- if not message.edit_date:
- status = await m.reply(text, quote=True)
- await task
- except Exception as e:
- e = traceback.format_exc()
- print(e)
- exc = e.splitlines()[-1]
- await rm_task()
- 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"**→**\n`{escape(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()))
- await message.reply_document(
- document=filename,
- caption="`→`\n **Attached Document**",
- quote=False,
- )
- os.remove(filename)
- if status:
- await status.delete()
- return
- # Edit the output if input is edited
- if message.edit_date:
- status_ = await iter_edit(message, final_output)
- if status_ == 0:
- return await message.reply(final_output, quote=True)
- return
- if not status.from_user:
- status = await app2.get_messages(status.chat.id, status.id)
- await eor(status, text=final_output, quote=True)
- @app2.on_message(
- SUDOERS
- & ~filters.forwarded
- & ~filters.via_bot
- & filters.command("sh", prefixes=USERBOT_PREFIX),
- )
- async def shellrunner(_, message: Message):
- if not message.from_user or message.from_user.id not in SUDOERS:
- return
- if len(message.command) < 2:
- return await eor(message, text="**Usage:**\n/sh git pull")
- if message.reply_to_message:
- r = message.reply_to_message
- if r.reply_markup and isinstance(
- r.reply_markup,
- ReplyKeyboardMarkup,
- ):
- return await eor(message, text="INSECURE!")
- 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"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{escape(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)
- errors = traceback.format_exc()
- return await eor(
- message,
- text=f"**INPUT:**\n```{escape(text)}```\n\n**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 app2.send_document(
- message.chat.id,
- "output.txt",
- reply_to_message_id=message.id,
- caption=escape(text),
- )
- return os.remove("output.txt")
- await eor(
- message,
- text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT:**\n```{escape(output)}```",
- )
- else:
- await eor(
- message,
- text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT: **\n`No output`",
- )
- @app2.on_message(
- filters.command("reserve", prefixes=USERBOT_PREFIX)
- & ~filters.forwarded
- & ~filters.via_bot
- & SUDOERS
- )
- async def reserve_channel_handler(_, message: Message):
- if len(message.text.split()) != 2:
- return await eor(message, text="Pass a username as argument!!")
- username = message.text.split(None, 1)[1].strip().replace("@", "")
- m = await eor(message, text="Reserving...")
- chat = await app2.create_channel(username, "Created by .reserve command")
- try:
- await app2.set_chat_username(chat.id, username)
- except Exception as e:
- await m.edit(f"Couldn't Reserve, Error: `{str(e)}`")
- return await app2.delete_channel(chat.id)
- await m.edit(f"Reserved @{username} Successfully")
|