userbot.py 7.1 KB

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