userbot.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. """
  2. CREDITS:
  3. THIS MODULE IS WRITTEN BY @Pokurt.
  4. SOURCE:
  5. https://github.com/pokurt/Nana-Remix/blob/master/nana/plugins/devs.py
  6. """
  7. import sys
  8. import traceback
  9. from io import StringIO
  10. import os
  11. import re
  12. import subprocess
  13. from inspect import getfullargspec
  14. from wbb import app, app2, SUDOERS
  15. from pyrogram import filters
  16. from pyrogram.types import Message
  17. __MODULE__ = "Userbot"
  18. __HELP__ = ".l - Execute Python Code\n.sh - Execute Shell Code"
  19. # Eval and Sh module from nana-remix
  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. @app2.on_message(filters.user(SUDOERS) & ~filters.forwarded &
  31. ~filters.via_bot & filters.command("l", "."))
  32. async def executor(client, message):
  33. try:
  34. cmd = message.text.split(" ", maxsplit=1)[1]
  35. except IndexError:
  36. await message.delete()
  37. return
  38. reply_to_id = message.message_id
  39. if message.reply_to_message:
  40. reply_to_id = message.reply_to_message.message_id
  41. old_stderr = sys.stderr
  42. old_stdout = sys.stdout
  43. redirected_output = sys.stdout = StringIO()
  44. redirected_error = sys.stderr = StringIO()
  45. stdout, stderr, exc = None, None, None
  46. try:
  47. await aexec(cmd, client, message)
  48. except Exception:
  49. exc = traceback.format_exc()
  50. stdout = redirected_output.getvalue()
  51. stderr = redirected_error.getvalue()
  52. sys.stdout = old_stdout
  53. sys.stderr = old_stderr
  54. evaluation = ""
  55. if exc:
  56. evaluation = exc
  57. elif stderr:
  58. evaluation = stderr
  59. elif stdout:
  60. evaluation = stdout
  61. else:
  62. evaluation = "Success"
  63. final_output = (
  64. f"**INPUT:**\n```{cmd}```\n\n**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. await message.reply_document(
  70. document=filename,
  71. caption=cmd,
  72. disable_notification=True,
  73. reply_to_message_id=reply_to_id,
  74. )
  75. os.remove(filename)
  76. await message.delete()
  77. else:
  78. await edit_or_reply(message, text=final_output)
  79. @app2.on_message(
  80. filters.user(SUDOERS)
  81. & ~filters.forwarded
  82. & ~filters.via_bot
  83. & filters.command('sh', "."),
  84. )
  85. async def shellrunner(client, message):
  86. if len(message.command) < 2:
  87. await edit_or_reply(message, text='**Usage:**\n/sh git pull')
  88. return
  89. text = message.text.split(None, 1)[1]
  90. if '\n' in text:
  91. code = text.split('\n')
  92. output = ''
  93. for x in code:
  94. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x)
  95. try:
  96. process = subprocess.Popen(
  97. shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  98. )
  99. except Exception as err:
  100. print(err)
  101. await edit_or_reply(
  102. message,
  103. text=f"**INPUT:**\n```{text}```\n\n**ERROR:**\n```{err}```")
  104. output += f'**{code}**\n'
  105. output += process.stdout.read()[:-1].decode('utf-8')
  106. output += '\n'
  107. else:
  108. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  109. for a in range(len(shell)):
  110. shell[a] = shell[a].replace('"', '')
  111. try:
  112. process = subprocess.Popen(
  113. shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  114. )
  115. except Exception as err:
  116. print(err)
  117. exc_type, exc_obj, exc_tb = sys.exc_info()
  118. errors = traceback.format_exception(
  119. etype=exc_type, value=exc_obj, tb=exc_tb,
  120. )
  121. await edit_or_reply(
  122. message,
  123. text=f"**INPUT:**\n```{text}```\n\n**ERROR:**\n```{''.join(errors)}```")
  124. return
  125. output = process.stdout.read()[:-1].decode('utf-8')
  126. if str(output) == '\n':
  127. output = None
  128. if output:
  129. if len(output) > 4096:
  130. with open('output.txt', 'w+') as file:
  131. file.write(output)
  132. await app.send_document(
  133. message.chat.id,
  134. 'output.txt',
  135. reply_to_message_id=message.message_id,
  136. caption='`Output`',
  137. )
  138. os.remove('output.txt')
  139. return
  140. await edit_or_reply(
  141. message,
  142. text=f"**INPUT:**\n```{text}```\n\n**OUTPUT:**\n```{output}```"
  143. )
  144. else:
  145. await edit_or_reply(
  146. message,
  147. text=f'**INPUT:**\n```{text}```\n\n**OUTPUT: **\n`No output`')
  148. """ The code below is for inline eval"""
  149. async def aexec2(code, client):
  150. exec(
  151. "async def __aexecc(client): "
  152. + "".join(f"\n {a}" for a in code.split("\n"))
  153. )
  154. return await locals()["__aexecc"](client)
  155. async def eval_executor_func(code):
  156. old_stderr = sys.stderr
  157. old_stdout = sys.stdout
  158. redirected_output = sys.stdout = StringIO()
  159. redirected_error = sys.stderr = StringIO()
  160. stdout, stderr, exc = None, None, None
  161. try:
  162. await aexec2(code, app)
  163. except Exception:
  164. exc = traceback.format_exc()
  165. stdout = redirected_output.getvalue()
  166. stderr = redirected_error.getvalue()
  167. sys.stdout = old_stdout
  168. sys.stderr = old_stderr
  169. evaluation = ""
  170. if exc:
  171. evaluation = exc
  172. elif stderr:
  173. evaluation = stderr
  174. elif stdout:
  175. evaluation = stdout
  176. else:
  177. evaluation = "Success"
  178. output = evaluation.strip()
  179. final_output = (
  180. f"**INPUT:**\n```{code}```\n\n**OUTPUT**:\n```{output}```")
  181. return [final_output, output]