userbot.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. reply_to_id = message.message_id
  49. if message.reply_to_message:
  50. reply_to_id = message.reply_to_message.message_id
  51. old_stderr = sys.stderr
  52. old_stdout = sys.stdout
  53. redirected_output = sys.stdout = StringIO()
  54. redirected_error = sys.stderr = StringIO()
  55. stdout, stderr, exc = None, None, None
  56. try:
  57. await aexec(cmd, client, message)
  58. except Exception:
  59. exc = traceback.format_exc()
  60. stdout = redirected_output.getvalue()
  61. stderr = redirected_error.getvalue()
  62. sys.stdout = old_stdout
  63. sys.stderr = old_stderr
  64. evaluation = ""
  65. if exc:
  66. evaluation = exc
  67. elif stderr:
  68. evaluation = stderr
  69. elif stdout:
  70. evaluation = stdout
  71. else:
  72. evaluation = "Success"
  73. final_output = f"**INPUT:**\n```{cmd}```\n\n**OUTPUT**:\n```{evaluation.strip()}```"
  74. if len(final_output) > 4096:
  75. filename = "output.txt"
  76. with open(filename, "w+", encoding="utf8") as out_file:
  77. out_file.write(str(evaluation.strip()))
  78. await message.reply_document(
  79. document=filename,
  80. caption=f"`{cmd[0:1020]}`",
  81. quote=False,
  82. reply_to_message_id=reply_to_id,
  83. )
  84. await message.delete()
  85. os.remove(filename)
  86. else:
  87. await edit_or_reply(message, text=final_output)
  88. @app2.on_message(
  89. filters.user(SUDOERS)
  90. & ~filters.forwarded
  91. & ~filters.via_bot
  92. & filters.command("sh", prefixes=USERBOT_PREFIX),
  93. )
  94. async def shellrunner(client, message):
  95. if len(message.command) < 2:
  96. await edit_or_reply(message, text="**Usage:**\n/sh git pull")
  97. return
  98. text = message.text.split(None, 1)[1]
  99. if "\n" in text:
  100. code = text.split("\n")
  101. output = ""
  102. for x in code:
  103. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x)
  104. try:
  105. process = subprocess.Popen(
  106. shell,
  107. stdout=subprocess.PIPE,
  108. stderr=subprocess.PIPE,
  109. )
  110. except Exception as err:
  111. print(err)
  112. await edit_or_reply(
  113. message, text=f"**INPUT:**\n```{text}```\n\n**ERROR:**\n```{err}```"
  114. )
  115. output += f"**{code}**\n"
  116. output += process.stdout.read()[:-1].decode("utf-8")
  117. output += "\n"
  118. else:
  119. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  120. for a in range(len(shell)):
  121. shell[a] = shell[a].replace('"', "")
  122. try:
  123. process = subprocess.Popen(
  124. shell,
  125. stdout=subprocess.PIPE,
  126. stderr=subprocess.PIPE,
  127. )
  128. except Exception as err:
  129. print(err)
  130. exc_type, exc_obj, exc_tb = sys.exc_info()
  131. errors = traceback.format_exception(
  132. etype=exc_type,
  133. value=exc_obj,
  134. tb=exc_tb,
  135. )
  136. await edit_or_reply(
  137. message,
  138. text=f"**INPUT:**\n```{text}```\n\n**ERROR:**\n```{''.join(errors)}```",
  139. )
  140. return
  141. output = process.stdout.read()[:-1].decode("utf-8")
  142. if str(output) == "\n":
  143. output = None
  144. if output:
  145. if len(output) > 4096:
  146. with open("output.txt", "w+") as file:
  147. file.write(output)
  148. await app.send_document(
  149. message.chat.id,
  150. "output.txt",
  151. reply_to_message_id=message.message_id,
  152. caption="`Output`",
  153. )
  154. os.remove("output.txt")
  155. return
  156. await edit_or_reply(
  157. message, text=f"**INPUT:**\n```{text}```\n\n**OUTPUT:**\n```{output}```"
  158. )
  159. else:
  160. await edit_or_reply(
  161. message, text=f"**INPUT:**\n```{text}```\n\n**OUTPUT: **\n`No output`"
  162. )