devs.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 io import StringIO
  13. from time import time
  14. from pyrogram import filters
  15. from pyrogram.types import (InlineKeyboardButton,
  16. InlineKeyboardMarkup, Message)
  17. from wbb import SUDOERS, app, eor
  18. # Module help for this is in sudoers.py
  19. async def aexec(code, client, message):
  20. exec(
  21. "async def __aexec(client, message): "
  22. + "".join(f"\n {a}" for a in code.split("\n"))
  23. )
  24. return await locals()["__aexec"](client, message)
  25. @app.on_message(
  26. filters.user(SUDOERS)
  27. & ~filters.forwarded
  28. & ~filters.via_bot
  29. & ~filters.edited
  30. & filters.command("eval")
  31. )
  32. async def executor(client, message: Message):
  33. try:
  34. cmd = message.text.split(" ", maxsplit=1)[1]
  35. except IndexError:
  36. return await message.delete()
  37. t1 = time()
  38. old_stderr = sys.stderr
  39. old_stdout = sys.stdout
  40. redirected_output = sys.stdout = StringIO()
  41. redirected_error = sys.stderr = StringIO()
  42. stdout, stderr, exc = None, None, None
  43. try:
  44. await aexec(cmd, client, message)
  45. except Exception:
  46. exc = traceback.format_exc()
  47. stdout = redirected_output.getvalue()
  48. stderr = redirected_error.getvalue()
  49. sys.stdout = old_stdout
  50. sys.stderr = old_stderr
  51. evaluation = ""
  52. if exc:
  53. evaluation = exc
  54. elif stderr:
  55. evaluation = stderr
  56. elif stdout:
  57. evaluation = stdout
  58. else:
  59. evaluation = "Success"
  60. final_output = f"**OUTPUT**:\n```{evaluation.strip()}```"
  61. if len(final_output) > 4096:
  62. filename = "output.txt"
  63. with open(filename, "w+", encoding="utf8") as out_file:
  64. out_file.write(str(evaluation.strip()))
  65. t2 = time()
  66. keyboard = InlineKeyboardMarkup(
  67. [
  68. [
  69. InlineKeyboardButton(
  70. text="⏳",
  71. callback_data=f"runtime {t2-t1} Seconds",
  72. )
  73. ]
  74. ]
  75. )
  76. await message.reply_document(
  77. document=filename,
  78. caption=f"**INPUT:**\n`{cmd[0:980]}`\n\n**OUTPUT:**\n`Attached Document`",
  79. quote=False,
  80. reply_markup=keyboard,
  81. )
  82. await message.delete()
  83. os.remove(filename)
  84. else:
  85. t2 = time()
  86. keyboard = InlineKeyboardMarkup(
  87. [
  88. [
  89. InlineKeyboardButton(
  90. text="⏳",
  91. callback_data=f"runtime {round(t2-t1, 3)} Seconds",
  92. )
  93. ]
  94. ]
  95. )
  96. await eor(message, text=final_output, reply_markup=keyboard)
  97. @app.on_callback_query(filters.regex(r"runtime"))
  98. async def runtime_func_cq(_, cq):
  99. runtime = cq.data.split(None, 1)[1]
  100. await cq.answer(runtime, show_alert=True)
  101. @app.on_message(
  102. filters.user(SUDOERS)
  103. & ~filters.forwarded
  104. & ~filters.via_bot
  105. & ~filters.edited
  106. & filters.command("sh"),
  107. )
  108. async def shellrunner(_, message: Message):
  109. if len(message.command) < 2:
  110. return await eor(message, text="**Usage:**\n/sh git pull")
  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 eor(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 enumerate(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 eor(
  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 eor(message, text=f"**OUTPUT:**\n```{output}```")
  167. else:
  168. await eor(message, text="**OUTPUT: **\n`No output`")