userbot.py 6.9 KB

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