userbot.py 6.9 KB

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