devs.py 5.5 KB

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