userbot.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. __MODULE__ = "Userbot"
  21. __HELP__ = """
  22. .alive - Send Alive Message.
  23. .l - Execute Python Code.
  24. .sh - Execute Shell Code.
  25. .approve | .disapprove - Approve Or Disapprove A User To PM You.
  26. .block | .unblock - Block Or Unblock A User.
  27. """
  28. arq = arq
  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. await message.delete()
  51. return
  52. old_stderr = sys.stderr
  53. old_stdout = sys.stdout
  54. redirected_output = sys.stdout = StringIO()
  55. redirected_error = sys.stderr = StringIO()
  56. stdout, stderr, exc = None, None, None
  57. try:
  58. await aexec(cmd, client, message)
  59. except Exception:
  60. exc = traceback.format_exc()
  61. stdout = redirected_output.getvalue()
  62. stderr = redirected_error.getvalue()
  63. sys.stdout = old_stdout
  64. sys.stderr = old_stderr
  65. evaluation = ""
  66. if exc:
  67. evaluation = exc
  68. elif stderr:
  69. evaluation = stderr
  70. elif stdout:
  71. evaluation = stdout
  72. else:
  73. evaluation = "Success"
  74. final_output = f"**INPUT:**\n```{escape(cmd)}```\n\n**OUTPUT**:\n```{escape(evaluation.strip())}```"
  75. if len(final_output) > 4096:
  76. filename = "output.txt"
  77. with open(filename, "w+", encoding="utf8") as out_file:
  78. out_file.write(str(evaluation.strip()))
  79. await message.reply_document(
  80. document=filename,
  81. caption=f"**INPUT:**\n`{escape(cmd[0:980])}`\n\n**OUTPUT:**\n`Attached Document`",
  82. quote=False,
  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: 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,
  114. text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{escape(err)}```",
  115. )
  116. output += f"**{code}**\n"
  117. output += process.stdout.read()[:-1].decode("utf-8")
  118. output += "\n"
  119. else:
  120. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  121. for a in range(len(shell)):
  122. shell[a] = shell[a].replace('"', "")
  123. try:
  124. process = subprocess.Popen(
  125. shell,
  126. stdout=subprocess.PIPE,
  127. stderr=subprocess.PIPE,
  128. )
  129. except Exception as err:
  130. print(err)
  131. exc_type, exc_obj, exc_tb = sys.exc_info()
  132. errors = traceback.format_exception(
  133. etype=exc_type,
  134. value=exc_obj,
  135. tb=exc_tb,
  136. )
  137. await edit_or_reply(
  138. message,
  139. text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{''.join(errors)}```",
  140. )
  141. return
  142. output = process.stdout.read()[:-1].decode("utf-8")
  143. if str(output) == "\n":
  144. output = None
  145. if output:
  146. if len(output) > 4096:
  147. with open("output.txt", "w+") as file:
  148. file.write(output)
  149. await app.send_document(
  150. message.chat.id,
  151. "output.txt",
  152. reply_to_message_id=message.message_id,
  153. caption="`Output`",
  154. )
  155. os.remove("output.txt")
  156. return
  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 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", prefixes=USERBOT_PREFIX) & filters.user(SUDOERS)
  175. )
  176. async def cEval(_, message: Message):
  177. code = message.text.strip()[3:]
  178. file = "exec.c"
  179. cmdCompile = ["gcc", "-g", "exec.c", "-o", "exec"]
  180. cmdRun = ["./exec"]
  181. async with aiofiles.open(file, mode="w+") as f:
  182. await f.write(code)
  183. t1 = time()
  184. pCompile = subprocess.run(cmdCompile, capture_output=True)
  185. os.remove(file)
  186. err = pCompile.stderr.decode()
  187. if err:
  188. text = f"""
  189. **INPUT:**
  190. ```{escape(code)}```
  191. **COMPILE-TIME ERROR:**
  192. ```{escape(err)}```
  193. """
  194. if len(text) > 4090:
  195. await sendFile(message, text)
  196. return
  197. await edit_or_reply(message, text=text)
  198. return
  199. pRun = subprocess.run(cmdRun, capture_output=True)
  200. t2 = time()
  201. os.remove("exec")
  202. err = pRun.stderr.decode()
  203. out = pRun.stdout.decode()
  204. err = f"**RUNTIME ERROR:**\n```{escape(err)}```" if err else None
  205. out = f"**OUTPUT:**\n```{escape(out)}```" if out else None
  206. text = f"""
  207. **INPUT:**
  208. ```{escape(code)}```
  209. {err if err else out}
  210. `Compiled and executed in {round(t2-t1, 5)} seconds`
  211. """
  212. if len(text) > 4090:
  213. await sendFile(message, text)
  214. return
  215. await edit_or_reply(message, text=text)