userbot.py 5.2 KB

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