If output from .exec or .term is too long, send as a file instead

Signed-off-by: Justin Crawford <Justin@stacksmash.net>
This commit is contained in:
Justin Crawford 2020-11-29 17:34:07 -08:00 committed by blank X
parent af06deebc1
commit 55d41e65cc
2 changed files with 25 additions and 4 deletions

View File

@ -5,7 +5,7 @@ import sys
import html
import inspect
import asyncio
from io import StringIO
from io import StringIO, BytesIO
from pyrogram import Client, filters
from .. import config, help_dict, log_errors, slave, apps, session, public_log_errors
@ -57,15 +57,26 @@ async def pyexec(client, message):
wrapped_stdout.seek(0)
output = ''
wrapped_stderr_text = wrapped_stderr.read().strip()
wrapped_stdout_text = wrapped_stdout.read().strip()
if wrapped_stderr_text:
output += f'<code>{html.escape(wrapped_stderr_text)}</code>\n'
wrapped_stdout_text = wrapped_stdout.read().strip()
if wrapped_stdout_text:
output += f'<code>{html.escape(wrapped_stdout_text)}</code>\n'
for i in returned:
output += f'<code>{html.escape(str(i).strip())}</code>\n'
if not output.strip():
output = 'Executed'
await reply.edit_text(output)
# send as a file if it's longer than 4096 bytes
if len(output) > 4096:
out = wrapped_stderr_text + "\n" + wrapped_stdout_text + "\n"
for i in returned:
out += str(i).strip() + "\n"
f = BytesIO(out.strip().encode('utf-8'))
f.name = "output.txt"
await reply.delete()
await message.reply_document(f)
else:
await reply.edit_text(output)
help_dict['exec'] = ('Exec', '{prefix}exec <i>&lt;python code&gt;</i> - Executes python code')

View File

@ -1,6 +1,7 @@
import re
import html
import asyncio
from io import BytesIO
from pyrogram import Client, filters
from .. import config, help_dict, log_errors, public_log_errors
@ -21,7 +22,16 @@ async def shell(client, message):
text += f'<code>{html.escape(stderr)}</code>\n'
if stdout:
text += f'<code>{html.escape(stdout)}</code>'
await reply.edit_text(text)
# send as a file if it's longer than 4096 bytes
if len(text) > 4096:
out = stderr.strip() + "\n" + stdout.strip()
f = BytesIO(out.strip().encode('utf-8'))
f.name = "output.txt"
await reply.delete()
await message.reply_document(f)
else:
await reply.edit_text(text)
help_dict['shell'] = ('Shell',
'''{prefix}sh <i>&lt;command&gt;</i> \\n <i>[stdin]</i> - Executes <i>&lt;command&gt;</i> in shell