userbot.py 8.3 KB

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