userbot.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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, OWNER_ID
  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(OWNER_ID) & ~filters.forwarded &
  25. ~filters.via_bot & filters.command("eval"))
  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 = (f"**OUTPUT**:\n```{evaluation.strip()}```")
  58. if len(final_output) > 4096:
  59. filename = "output.txt"
  60. with open(filename, "w+", encoding="utf8") as out_file:
  61. out_file.write(str(evaluation.strip()))
  62. await message.reply_document(
  63. document=filename,
  64. caption=cmd,
  65. disable_notification=True,
  66. reply_to_message_id=reply_to_id,
  67. )
  68. os.remove(filename)
  69. await message.delete()
  70. else:
  71. await edit_or_reply(message, text=final_output)
  72. @app2.on_message(
  73. filters.user(OWNER_ID)
  74. & ~filters.forwarded
  75. & ~filters.via_bot
  76. & filters.command('sh'),
  77. )
  78. async def shellrunner(client, message):
  79. if len(message.command) < 2:
  80. await edit_or_reply(message, text='**Usage:**\n/sh git pull')
  81. return
  82. text = message.text.split(None, 1)[1]
  83. if '\n' in text:
  84. code = text.split('\n')
  85. output = ''
  86. for x in code:
  87. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", x)
  88. try:
  89. process = subprocess.Popen(
  90. shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  91. )
  92. except Exception as err:
  93. print(err)
  94. await edit_or_reply(
  95. message,
  96. text=f"**Error:**\n```{err}```")
  97. output += f'**{code}**\n'
  98. output += process.stdout.read()[:-1].decode('utf-8')
  99. output += '\n'
  100. else:
  101. shell = re.split(""" (?=(?:[^'"]|'[^']*'|"[^"]*")*$)""", text)
  102. for a in range(len(shell)):
  103. shell[a] = shell[a].replace('"', '')
  104. try:
  105. process = subprocess.Popen(
  106. shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
  107. )
  108. except Exception as err:
  109. print(err)
  110. exc_type, exc_obj, exc_tb = sys.exc_info()
  111. errors = traceback.format_exception(
  112. etype=exc_type, value=exc_obj, tb=exc_tb,
  113. )
  114. await edit_or_reply(
  115. message,
  116. text=f"**Error:**\n```{''.join(errors)}```")
  117. return
  118. output = process.stdout.read()[:-1].decode('utf-8')
  119. if str(output) == '\n':
  120. output = None
  121. if output:
  122. if len(output) > 4096:
  123. with open('output.txt', 'w+') as file:
  124. file.write(output)
  125. await app.send_document(
  126. message.chat.id,
  127. 'output.txt',
  128. reply_to_message_id=message.message_id,
  129. caption='`Output`',
  130. )
  131. os.remove('output.txt')
  132. return
  133. await edit_or_reply(
  134. message,
  135. text=f"**Output:**\n```{output}```"
  136. )
  137. else:
  138. await edit_or_reply(
  139. message,
  140. text='**Output: **\n`No output`')