userbot.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 aiofiles
  8. import os
  9. import re
  10. import subprocess
  11. import sys
  12. import traceback
  13. from html import escape
  14. from inspect import getfullargspec
  15. from io import StringIO
  16. from time import time
  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. # Eval and Sh module from nana-remix
  29. async def aexec(code, client, message):
  30. exec(
  31. "async def __aexec(client, message): "
  32. + "".join(f"\n {a}" for a in code.split("\n"))
  33. )
  34. return await locals()["__aexec"](client, message)
  35. async def edit_or_reply(msg: Message, **kwargs):
  36. func = msg.edit_text if msg.from_user.is_self else msg.reply
  37. spec = getfullargspec(func.__wrapped__).args
  38. await func(**{k: v for k, v in kwargs.items() if k in spec})
  39. @app2.on_message(
  40. filters.user(SUDOERS)
  41. & ~filters.forwarded
  42. & ~filters.via_bot
  43. & filters.command("l", prefixes=USERBOT_PREFIX)
  44. )
  45. async def executor(client, message: Message):
  46. try:
  47. cmd = message.text.split(" ", maxsplit=1)[1]
  48. except IndexError:
  49. await message.delete()
  50. return
  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 = (
  74. f"**INPUT:**\n```{escape(cmd)}```\n\n**OUTPUT**:\n```{escape(evaluation.strip())}```"
  75. )
  76. if len(final_output) > 4096:
  77. filename = "output.txt"
  78. with open(filename, "w+", encoding="utf8") as out_file:
  79. out_file.write(str(evaluation.strip()))
  80. await message.reply_document(
  81. document=filename,
  82. caption=f"**INPUT:**\n`{escape(cmd[0:980])}`\n\n**OUTPUT:**\n`Attached Document`",
  83. quote=False,
  84. )
  85. await message.delete()
  86. os.remove(filename)
  87. else:
  88. await edit_or_reply(message, text=final_output)
  89. @app2.on_message(
  90. filters.user(SUDOERS)
  91. & ~filters.forwarded
  92. & ~filters.via_bot
  93. & filters.command("sh", prefixes=USERBOT_PREFIX),
  94. )
  95. async def shellrunner(client, message: Message):
  96. if len(message.command) < 2:
  97. await edit_or_reply(message, text="**Usage:**\n/sh git pull")
  98. return
  99. text = message.text.split(None, 1)[1]
  100. if "\n" in text:
  101. code = text.split("\n")
  102. output = ""
  103. for x in code:
  104. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x)
  105. try:
  106. process = subprocess.Popen(
  107. shell,
  108. stdout=subprocess.PIPE,
  109. stderr=subprocess.PIPE,
  110. )
  111. except Exception as err:
  112. print(err)
  113. await edit_or_reply(
  114. message,
  115. text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{escape(err)}```",
  116. )
  117. output += f"**{code}**\n"
  118. output += process.stdout.read()[:-1].decode("utf-8")
  119. output += "\n"
  120. else:
  121. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  122. for a in range(len(shell)):
  123. shell[a] = shell[a].replace('"', "")
  124. try:
  125. process = subprocess.Popen(
  126. shell,
  127. stdout=subprocess.PIPE,
  128. stderr=subprocess.PIPE,
  129. )
  130. except Exception as err:
  131. print(err)
  132. exc_type, exc_obj, exc_tb = sys.exc_info()
  133. errors = traceback.format_exception(
  134. etype=exc_type,
  135. value=exc_obj,
  136. tb=exc_tb,
  137. )
  138. await edit_or_reply(
  139. message,
  140. text=f"**INPUT:**\n```{escape(text)}```\n\n**ERROR:**\n```{''.join(errors)}```",
  141. )
  142. return
  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. os.remove("output.txt")
  157. return
  158. await edit_or_reply(
  159. message,
  160. text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT:**\n```{escape(output)}```",
  161. )
  162. else:
  163. await edit_or_reply(
  164. message,
  165. text=f"**INPUT:**\n```{escape(text)}```\n\n**OUTPUT: **\n`No output`",
  166. )
  167. """ C Eval """
  168. @app2.on_message(filters.command("c", prefixes=USERBOT_PREFIX) & filters.user(SUDOERS))
  169. async def cEval(_, message: Message):
  170. code = message.text.strip()[3:]
  171. file = "exec.c"
  172. cmdCompile = ['gcc', '-g', 'exec.c', '-o', 'exec']
  173. cmdRun = ['./exec']
  174. async with aiofiles.open(file, mode="w+") as f:
  175. await f.write(code)
  176. t1 = time()
  177. pCompile = subprocess.run(cmdCompile, capture_output=True)
  178. os.remove(file)
  179. err = pCompile.stderr.decode()
  180. if err:
  181. text = f"""
  182. **INPUT:**
  183. ```{escape(code)}```
  184. **COMPILE-TIME ERROR:**
  185. ```{escape(err)}```
  186. """
  187. if len(text) > 4090:
  188. file = "output.txt"
  189. async with aiofiles.open(file, mode="w+") as f2:
  190. await f2.write(text)
  191. await message.reply_document(file)
  192. return
  193. await edit_or_reply(message, text=text)
  194. return
  195. pRun = subprocess.run(cmdRun, capture_output=True)
  196. t2 = time()
  197. os.remove("exec")
  198. err = pRun.stderr.decode()
  199. out = pRun.stdout.decode()
  200. err = f"**ERROR:**\n```{escape(err)}```" if err else None
  201. out = f"**OUTPUT:**\n```{escape(out)}```" if out else None
  202. text = f"""
  203. **INPUT:**
  204. ```{escape(code)}```
  205. {err if err else out}
  206. `Compiled and executed in {round(t2-t1, 5)} seconds`
  207. """
  208. if len(text) > 4090:
  209. file = "output.txt"
  210. async with aiofiles.open(file, mode="w+") as f2:
  211. await f2.write(text)
  212. await message.reply_document(file)
  213. return
  214. await edit_or_reply(message, text=text)