devs.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. """
  2. CREDITS:
  3. THIS MODULE IS COMPLETELY 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 inspect import getfullargspec
  13. from io import StringIO
  14. from time import time
  15. from pyrogram import filters
  16. from pyrogram.types import (InlineKeyboardButton,
  17. InlineKeyboardMarkup, Message)
  18. from wbb import SUDOERS, app
  19. __MODULE__ = "Devs"
  20. __HELP__ = "/eval - Execute Python Code\n/sh - Execute Shell Code"
  21. async def aexec(code, client, message):
  22. exec(
  23. "async def __aexec(client, message): "
  24. + "".join(f"\n {a}" for a in code.split("\n"))
  25. )
  26. return await locals()["__aexec"](client, message)
  27. async def edit_or_reply(msg: Message, **kwargs):
  28. func = msg.edit_text if msg.from_user.is_self else msg.reply
  29. spec = getfullargspec(func.__wrapped__).args
  30. await func(**{k: v for k, v in kwargs.items() if k in spec})
  31. @app.on_message(
  32. filters.user(SUDOERS)
  33. & ~filters.forwarded
  34. & ~filters.via_bot
  35. & ~filters.edited
  36. & filters.command("eval")
  37. )
  38. async def executor(client, message):
  39. try:
  40. cmd = message.text.split(" ", maxsplit=1)[1]
  41. except IndexError:
  42. return await message.delete()
  43. t1 = time()
  44. old_stderr = sys.stderr
  45. old_stdout = sys.stdout
  46. redirected_output = sys.stdout = StringIO()
  47. redirected_error = sys.stderr = StringIO()
  48. stdout, stderr, exc = None, None, None
  49. try:
  50. await aexec(cmd, client, message)
  51. except Exception:
  52. exc = traceback.format_exc()
  53. stdout = redirected_output.getvalue()
  54. stderr = redirected_error.getvalue()
  55. sys.stdout = old_stdout
  56. sys.stderr = old_stderr
  57. evaluation = ""
  58. if exc:
  59. evaluation = exc
  60. elif stderr:
  61. evaluation = stderr
  62. elif stdout:
  63. evaluation = stdout
  64. else:
  65. evaluation = "Success"
  66. final_output = f"**OUTPUT**:\n```{evaluation.strip()}```"
  67. if len(final_output) > 4096:
  68. filename = "output.txt"
  69. with open(filename, "w+", encoding="utf8") as out_file:
  70. out_file.write(str(evaluation.strip()))
  71. t2 = time()
  72. keyboard = InlineKeyboardMarkup(
  73. [
  74. [
  75. InlineKeyboardButton(
  76. text="⏳",
  77. callback_data=f"runtime {t2-t1} Seconds",
  78. )
  79. ]
  80. ]
  81. )
  82. await message.reply_document(
  83. document=filename,
  84. caption=f"**INPUT:**\n`{cmd[0:980]}`\n\n**OUTPUT:**\n`Attached Document`",
  85. quote=False,
  86. reply_markup=keyboard,
  87. )
  88. await message.delete()
  89. os.remove(filename)
  90. else:
  91. t2 = time()
  92. keyboard = InlineKeyboardMarkup(
  93. [
  94. [
  95. InlineKeyboardButton(
  96. text="⏳",
  97. callback_data=f"runtime {round(t2-t1, 3)} Seconds",
  98. )
  99. ]
  100. ]
  101. )
  102. await edit_or_reply(
  103. message, text=final_output, reply_markup=keyboard
  104. )
  105. @app.on_callback_query(filters.regex(r"runtime"))
  106. async def runtime_func_cq(_, cq):
  107. runtime = cq.data.split(None, 1)[1]
  108. await cq.answer(runtime, show_alert=True)
  109. @app.on_message(
  110. filters.user(SUDOERS)
  111. & ~filters.forwarded
  112. & ~filters.via_bot
  113. & ~filters.edited
  114. & filters.command("sh"),
  115. )
  116. async def shellrunner(client, message):
  117. if len(message.command) < 2:
  118. return await edit_or_reply(
  119. message, text="**Usage:**\n/sh git pull"
  120. )
  121. text = message.text.split(None, 1)[1]
  122. if "\n" in text:
  123. code = text.split("\n")
  124. output = ""
  125. for x in code:
  126. shell = re.split(
  127. """ (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x
  128. )
  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. await edit_or_reply(
  138. message, text=f"**ERROR:**\n```{err}```"
  139. )
  140. output += f"**{code}**\n"
  141. output += process.stdout.read()[:-1].decode("utf-8")
  142. output += "\n"
  143. else:
  144. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  145. for a in range(len(shell)):
  146. shell[a] = shell[a].replace('"', "")
  147. try:
  148. process = subprocess.Popen(
  149. shell,
  150. stdout=subprocess.PIPE,
  151. stderr=subprocess.PIPE,
  152. )
  153. except Exception as err:
  154. print(err)
  155. exc_type, exc_obj, exc_tb = sys.exc_info()
  156. errors = traceback.format_exception(
  157. etype=exc_type,
  158. value=exc_obj,
  159. tb=exc_tb,
  160. )
  161. return await edit_or_reply(
  162. message, text=f"**ERROR:**\n```{''.join(errors)}```"
  163. )
  164. output = process.stdout.read()[:-1].decode("utf-8")
  165. if str(output) == "\n":
  166. output = None
  167. if output:
  168. if len(output) > 4096:
  169. with open("output.txt", "w+") as file:
  170. file.write(output)
  171. await app.send_document(
  172. message.chat.id,
  173. "output.txt",
  174. reply_to_message_id=message.message_id,
  175. caption="`Output`",
  176. )
  177. return os.remove("output.txt")
  178. await edit_or_reply(
  179. message, text=f"**OUTPUT:**\n```{output}```"
  180. )
  181. else:
  182. await edit_or_reply(message, text="**OUTPUT: **\n`No output`")