userbot.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. """
  2. CREDITS:
  3. EVAL AND SH FUNCTION IN THIS MODULE IS WRITTEN BY @Pokurt.
  4. SOURCE:
  5. https://github.com/pokurt/Nana-Remix/blob/master/nana/plugins/devs.py
  6. """
  7. import os
  8. import re
  9. import subprocess
  10. import sys
  11. import traceback
  12. from inspect import getfullargspec
  13. from io import StringIO
  14. from pyrogram import filters
  15. from pyrogram.types import Message
  16. from wbb import SUDOERS, USERBOT_PREFIX, app, app2
  17. __MODULE__ = "Userbot"
  18. __HELP__ = """
  19. .alive - Send Alive Message.
  20. .l - Execute Python Code.
  21. .sh - Execute Shell Code.
  22. .approve | .disapprove - Approve Or Disapprove A User To PM You.
  23. .block | .unblock - Block Or Unblock A User.
  24. """
  25. # Eval and Sh module from nana-remix
  26. async def aexec(code, client, message):
  27. exec(
  28. "async def __aexec(client, message): "
  29. + "".join(f"\n {a}" for a in code.split("\n"))
  30. )
  31. return await locals()["__aexec"](client, message)
  32. async def edit_or_reply(msg: Message, **kwargs):
  33. func = msg.edit_text if msg.from_user.is_self else msg.reply
  34. spec = getfullargspec(func.__wrapped__).args
  35. await func(**{k: v for k, v in kwargs.items() if k in spec})
  36. @app2.on_message(
  37. filters.user(SUDOERS)
  38. & ~filters.forwarded
  39. & ~filters.via_bot
  40. & filters.command("l", prefixes=USERBOT_PREFIX)
  41. )
  42. async def executor(client, message):
  43. try:
  44. cmd = message.text.split(" ", maxsplit=1)[1]
  45. except IndexError:
  46. await message.delete()
  47. return
  48. old_stderr = sys.stderr
  49. old_stdout = sys.stdout
  50. redirected_output = sys.stdout = StringIO()
  51. redirected_error = sys.stderr = StringIO()
  52. stdout, stderr, exc = None, None, None
  53. try:
  54. await aexec(cmd, client, message)
  55. except Exception:
  56. exc = traceback.format_exc()
  57. stdout = redirected_output.getvalue()
  58. stderr = redirected_error.getvalue()
  59. sys.stdout = old_stdout
  60. sys.stderr = old_stderr
  61. evaluation = ""
  62. if exc:
  63. evaluation = exc
  64. elif stderr:
  65. evaluation = stderr
  66. elif stdout:
  67. evaluation = stdout
  68. else:
  69. evaluation = "Success"
  70. final_output = (
  71. f"**INPUT:**\n```{cmd}```\n\n**OUTPUT**:\n```{evaluation.strip()}```"
  72. )
  73. if len(final_output) > 4096:
  74. filename = "output.txt"
  75. with open(filename, "w+", encoding="utf8") as out_file:
  76. out_file.write(str(evaluation.strip()))
  77. await message.reply_document(
  78. document=filename,
  79. caption=f"**INPUT:**\n`{cmd[0:980]}`\n\n**OUTPUT:**\n`Attached Document`",
  80. quote=False,
  81. )
  82. await message.delete()
  83. os.remove(filename)
  84. else:
  85. await edit_or_reply(message, text=final_output)
  86. @app2.on_message(
  87. filters.user(SUDOERS)
  88. & ~filters.forwarded
  89. & ~filters.via_bot
  90. & filters.command("sh", prefixes=USERBOT_PREFIX),
  91. )
  92. async def shellrunner(client, message):
  93. if len(message.command) < 2:
  94. await edit_or_reply(message, text="**Usage:**\n/sh git pull")
  95. return
  96. text = message.text.split(None, 1)[1]
  97. if "\n" in text:
  98. code = text.split("\n")
  99. output = ""
  100. for x in code:
  101. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x)
  102. try:
  103. process = subprocess.Popen(
  104. shell,
  105. stdout=subprocess.PIPE,
  106. stderr=subprocess.PIPE,
  107. )
  108. except Exception as err:
  109. print(err)
  110. await edit_or_reply(
  111. message,
  112. text=f"**INPUT:**\n```{text}```\n\n**ERROR:**\n```{err}```",
  113. )
  114. output += f"**{code}**\n"
  115. output += process.stdout.read()[:-1].decode("utf-8")
  116. output += "\n"
  117. else:
  118. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  119. for a in range(len(shell)):
  120. shell[a] = shell[a].replace('"', "")
  121. try:
  122. process = subprocess.Popen(
  123. shell,
  124. stdout=subprocess.PIPE,
  125. stderr=subprocess.PIPE,
  126. )
  127. except Exception as err:
  128. print(err)
  129. exc_type, exc_obj, exc_tb = sys.exc_info()
  130. errors = traceback.format_exception(
  131. etype=exc_type,
  132. value=exc_obj,
  133. tb=exc_tb,
  134. )
  135. await edit_or_reply(
  136. message,
  137. text=f"**INPUT:**\n```{text}```\n\n**ERROR:**\n```{''.join(errors)}```",
  138. )
  139. return
  140. output = process.stdout.read()[:-1].decode("utf-8")
  141. if str(output) == "\n":
  142. output = None
  143. if output:
  144. if len(output) > 4096:
  145. with open("output.txt", "w+") as file:
  146. file.write(output)
  147. await app.send_document(
  148. message.chat.id,
  149. "output.txt",
  150. reply_to_message_id=message.message_id,
  151. caption="`Output`",
  152. )
  153. os.remove("output.txt")
  154. return
  155. await edit_or_reply(
  156. message,
  157. text=f"**INPUT:**\n```{text}```\n\n**OUTPUT:**\n```{output}```",
  158. )
  159. else:
  160. await edit_or_reply(
  161. message,
  162. text=f"**INPUT:**\n```{text}```\n\n**OUTPUT: **\n`No output`",
  163. )