userbot.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import sys
  2. import traceback
  3. from io import StringIO
  4. import os
  5. import re
  6. import subprocess
  7. from inspect import getfullargspec
  8. from wbb import app, app2, SUDOERS
  9. from pyrogram import filters
  10. from pyrogram.types import Message
  11. __MODULE__ = "Userbot"
  12. __HELP__ = ".l - Execute Python Code\n.sh - Execute Shell Code"
  13. # Eval and Sh module from nana-remix
  14. async def aexec(code, client, message):
  15. exec(
  16. "async def __aexec(client, message): "
  17. + "".join(f"\n {a}" for a in code.split("\n"))
  18. )
  19. return await locals()["__aexec"](client, message)
  20. async def edit_or_reply(msg: Message, **kwargs):
  21. func = msg.edit_text if msg.from_user.is_self else msg.reply
  22. spec = getfullargspec(func.__wrapped__).args
  23. await func(**{k: v for k, v in kwargs.items() if k in spec})
  24. @app2.on_message(filters.user(SUDOERS) & ~filters.forwarded &
  25. ~filters.via_bot & filters.command("l", "."))
  26. async def executor(client, message):
  27. try:
  28. cmd = message.text.split(" ", maxsplit=1)[1]
  29. except IndexError:
  30. await message.delete()
  31. return
  32. reply_to_id = message.message_id
  33. if message.reply_to_message:
  34. reply_to_id = message.reply_to_message.message_id
  35. old_stderr = sys.stderr
  36. old_stdout = sys.stdout
  37. redirected_output = sys.stdout = StringIO()
  38. redirected_error = sys.stderr = StringIO()
  39. stdout, stderr, exc = None, None, None
  40. try:
  41. await aexec(cmd, client, message)
  42. except Exception:
  43. exc = traceback.format_exc()
  44. stdout = redirected_output.getvalue()
  45. stderr = redirected_error.getvalue()
  46. sys.stdout = old_stdout
  47. sys.stderr = old_stderr
  48. evaluation = ""
  49. if exc:
  50. evaluation = exc
  51. elif stderr:
  52. evaluation = stderr
  53. elif stdout:
  54. evaluation = stdout
  55. else:
  56. evaluation = "Success"
  57. final_output = (
  58. f"**INPUT:**\n```{cmd}```\n\n**OUTPUT**:\n```{evaluation.strip()}```")
  59. if len(final_output) > 4096:
  60. filename = "output.txt"
  61. with open(filename, "w+", encoding="utf8") as out_file:
  62. out_file.write(str(evaluation.strip()))
  63. await message.reply_document(
  64. document=filename,
  65. caption=cmd,
  66. disable_notification=True,
  67. reply_to_message_id=reply_to_id,
  68. )
  69. os.remove(filename)
  70. await message.delete()
  71. else:
  72. await edit_or_reply(message, text=final_output)
  73. @app2.on_message(
  74. filters.user(SUDOERS)
  75. & ~filters.forwarded
  76. & ~filters.via_bot
  77. & filters.command('sh', "."),
  78. )
  79. async def shellrunner(client, message):
  80. if len(message.command) < 2:
  81. await edit_or_reply(message, text='**Usage:**\n/sh git pull')
  82. return
  83. text = message.text.split(None, 1)[1]
  84. if '\n' in text:
  85. code = text.split('\n')
  86. output = ''
  87. for x in code:
  88. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x)
  89. try:
  90. process = subprocess.Popen(
  91. shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  92. )
  93. except Exception as err:
  94. print(err)
  95. await edit_or_reply(
  96. message,
  97. text=f"**INPUT:**\n```{text}```\n\n**ERROR:**\n```{err}```")
  98. output += f'**{code}**\n'
  99. output += process.stdout.read()[:-1].decode('utf-8')
  100. output += '\n'
  101. else:
  102. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  103. for a in range(len(shell)):
  104. shell[a] = shell[a].replace('"', '')
  105. try:
  106. process = subprocess.Popen(
  107. shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  108. )
  109. except Exception as err:
  110. print(err)
  111. exc_type, exc_obj, exc_tb = sys.exc_info()
  112. errors = traceback.format_exception(
  113. etype=exc_type, value=exc_obj, tb=exc_tb,
  114. )
  115. await edit_or_reply(
  116. message,
  117. text=f"**INPUT:**\n```{text}```\n\n**ERROR:**\n```{''.join(errors)}```")
  118. return
  119. output = process.stdout.read()[:-1].decode('utf-8')
  120. if str(output) == '\n':
  121. output = None
  122. if output:
  123. if len(output) > 4096:
  124. with open('output.txt', 'w+') as file:
  125. file.write(output)
  126. await app.send_document(
  127. message.chat.id,
  128. 'output.txt',
  129. reply_to_message_id=message.message_id,
  130. caption='`Output`',
  131. )
  132. os.remove('output.txt')
  133. return
  134. await edit_or_reply(
  135. message,
  136. text=f"**INPUT:**\n```{text}```\n\n**OUTPUT:**\n```{output}```"
  137. )
  138. else:
  139. await edit_or_reply(
  140. message,
  141. text='**INPUT:**\n```{text}```\n\n**OUTPUT: **\n`No output`')