userbot.py 8.2 KB

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