userbot.py 7.0 KB

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