devs.py 5.3 KB

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