userbot.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 html import escape
  13. from inspect import getfullargspec
  14. from io import StringIO
  15. from time import time
  16. import aiofiles
  17. from pyrogram import filters
  18. from pyrogram.types import Message
  19. from wbb import SUDOERS, USERBOT_PREFIX, app, app2, arq
  20. from wbb.core.decorators.misc import exec_time
  21. __MODULE__ = "Userbot"
  22. __HELP__ = """
  23. .alive - Send Alive Message.
  24. .l - Execute Python Code.
  25. .sh - Execute Shell Code.
  26. .approve | .disapprove - Approve Or Disapprove A User To PM You.
  27. .block | .unblock - Block Or Unblock A User.
  28. """
  29. # Eval and Sh module from nana-remix
  30. async def aexec(code, client, message):
  31. exec(
  32. "async def __aexec(client, message): "
  33. + "".join(f"\n {a}" for a in code.split("\n"))
  34. )
  35. return await locals()["__aexec"](client, message)
  36. async def edit_or_reply(msg: Message, **kwargs):
  37. func = msg.edit_text if msg.from_user.is_self else msg.reply
  38. spec = getfullargspec(func.__wrapped__).args
  39. await func(**{k: v for k, v in kwargs.items() if k in spec})
  40. @app2.on_message(
  41. filters.user(SUDOERS)
  42. & ~filters.forwarded
  43. & ~filters.via_bot
  44. & filters.command("l", prefixes=USERBOT_PREFIX)
  45. )
  46. async def executor(client, message: Message):
  47. try:
  48. cmd = message.text.split(" ", maxsplit=1)[1]
  49. except IndexError:
  50. return await message.delete()
  51. m = message
  52. p = print
  53. if message.reply_to_message:
  54. r = message.reply_to_message
  55. old_stderr = sys.stderr
  56. old_stdout = sys.stdout
  57. redirected_output = sys.stdout = StringIO()
  58. redirected_error = sys.stderr = StringIO()
  59. stdout, stderr, exc = None, None, None
  60. try:
  61. await aexec(cmd, client, message)
  62. except Exception:
  63. exc = traceback.format_exc()
  64. stdout = redirected_output.getvalue()
  65. stderr = redirected_error.getvalue()
  66. sys.stdout = old_stdout
  67. sys.stderr = old_stderr
  68. evaluation = ""
  69. if exc:
  70. evaluation = exc
  71. elif stderr:
  72. evaluation = stderr
  73. elif stdout:
  74. evaluation = stdout
  75. else:
  76. evaluation = "Success"
  77. final_output = f"**INPUT:**\n```{escape(cmd)}```\n\n**OUTPUT**:\n```{escape(evaluation.strip())}```"
  78. if len(final_output) > 4096:
  79. filename = "output.txt"
  80. with open(filename, "w+", encoding="utf8") as out_file:
  81. out_file.write(str(evaluation.strip()))
  82. await message.reply_document(
  83. document=filename,
  84. caption=f"**INPUT:**\n`{escape(cmd[0:980])}`\n\n**OUTPUT:**\n`Attached Document`",
  85. quote=False,
  86. )
  87. await message.delete()
  88. os.remove(filename)
  89. else:
  90. await edit_or_reply(message, text=final_output)
  91. @app2.on_message(
  92. filters.user(SUDOERS)
  93. & ~filters.forwarded
  94. & ~filters.via_bot
  95. & filters.command("sh", prefixes=USERBOT_PREFIX),
  96. )
  97. async def shellrunner(client, message: Message):
  98. if len(message.command) < 2:
  99. return await edit_or_reply(message, text="**Usage:**\n/sh git pull")
  100. text = message.text.split(None, 1)[1]
  101. if "\n" in text:
  102. code = text.split("\n")
  103. output = ""
  104. for x in code:
  105. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x)
  106. try:
  107. process = subprocess.Popen(
  108. shell,
  109. stdout=subprocess.PIPE,
  110. stderr=subprocess.PIPE,
  111. )
  112. except Exception as err:
  113. print(err)
  114. await edit_or_reply(
  115. message,
  116. text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{escape(err)}```",
  117. )
  118. output += f"**{code}**\n"
  119. output += process.stdout.read()[:-1].decode("utf-8")
  120. output += "\n"
  121. else:
  122. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  123. for a in range(len(shell)):
  124. shell[a] = shell[a].replace('"', "")
  125. try:
  126. process = subprocess.Popen(
  127. shell,
  128. stdout=subprocess.PIPE,
  129. stderr=subprocess.PIPE,
  130. )
  131. except Exception as err:
  132. print(err)
  133. exc_type, exc_obj, exc_tb = sys.exc_info()
  134. errors = traceback.format_exception(
  135. etype=exc_type,
  136. value=exc_obj,
  137. tb=exc_tb,
  138. )
  139. return await edit_or_reply(
  140. message,
  141. text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{''.join(errors)}```",
  142. )
  143. output = process.stdout.read()[:-1].decode("utf-8")
  144. if str(output) == "\n":
  145. output = None
  146. if output:
  147. if len(output) > 4096:
  148. with open("output.txt", "w+") as file:
  149. file.write(output)
  150. await app.send_document(
  151. message.chat.id,
  152. "output.txt",
  153. reply_to_message_id=message.message_id,
  154. caption="`Output`",
  155. )
  156. return os.remove("output.txt")
  157. await edit_or_reply(
  158. message,
  159. text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT:**\n```{escape(output)}```",
  160. )
  161. else:
  162. await edit_or_reply(
  163. message,
  164. text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT: **\n`No output`",
  165. )
  166. """ C and CPP Eval """
  167. async def sendFile(message: Message, text: str):
  168. file = "output.txt"
  169. async with aiofiles.open(file, mode="w+") as f:
  170. await f.write(text)
  171. await message.reply_document(file)
  172. os.remove(file)
  173. @app2.on_message(
  174. filters.command(["c", "cpp"], prefixes=USERBOT_PREFIX)
  175. & filters.user(SUDOERS)
  176. )
  177. async def c_cpp_eval(_, message: Message):
  178. code = message.text.split(None, 1)[1]
  179. file = "exec.c"
  180. compiler = "g++"
  181. out = "exec"
  182. cmdCompile = [compiler, "-g", file, "-o", out]
  183. cmdRun = [f"./{out}"]
  184. async with aiofiles.open(file, mode="w+") as f:
  185. await f.write(code)
  186. pCompile = subprocess.run(cmdCompile, capture_output=True)
  187. os.remove(file)
  188. err = pCompile.stderr.decode()
  189. if err:
  190. text = f"**INPUT:**\n```{escape(code)}```\n\n**COMPILE-TIME ERROR:**```{escape(err)}```"
  191. if len(text) > 4090:
  192. return await sendFile(message, text)
  193. return await edit_or_reply(message, text=text)
  194. pRun = subprocess.run(cmdRun, capture_output=True)
  195. os.remove(out)
  196. err = pRun.stderr.decode()
  197. out = pRun.stdout.decode()
  198. err = f"**RUNTIME ERROR:**\n```{escape(err)}```" if err else None
  199. out = f"**OUTPUT:**\n```{escape(out)}```" if out else None
  200. text = f"**INPUT:**\n```{escape(code)}```\n\n{err if err else out}"
  201. if len(text) > 4090:
  202. return await sendFile(message, text)
  203. await edit_or_reply(message, text=text)