2018-11-03 02:39:21 +00:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
from telethon import events
|
|
|
|
from telethon.utils import add_surrogate
|
2019-08-03 15:41:48 +00:00
|
|
|
from telethon.tl.types import MessageEntityPre, DocumentAttributeFilename
|
2018-11-03 02:39:21 +00:00
|
|
|
from telethon.tl.tlobject import TLObject
|
2019-08-03 15:41:48 +00:00
|
|
|
from telethon.errors import MessageTooLongError
|
2018-11-03 02:39:21 +00:00
|
|
|
import datetime
|
|
|
|
|
|
|
|
|
|
|
|
def parse_pre(text):
|
|
|
|
text = text.strip()
|
|
|
|
return (
|
|
|
|
text,
|
2018-11-21 20:13:42 +00:00
|
|
|
[MessageEntityPre(offset=0, length=len(add_surrogate(text)), language='')]
|
2018-11-03 02:39:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-08-03 15:43:37 +00:00
|
|
|
def yaml_format(obj, indent=0, max_str_len=256, max_byte_len=64):
|
2018-11-03 02:39:21 +00:00
|
|
|
"""
|
|
|
|
Pretty formats the given object as a YAML string which is returned.
|
|
|
|
(based on TLObject.pretty_format)
|
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
if isinstance(obj, TLObject):
|
|
|
|
obj = obj.to_dict()
|
|
|
|
|
|
|
|
if isinstance(obj, dict):
|
2018-11-21 20:49:17 +00:00
|
|
|
if not obj:
|
|
|
|
return 'dict:'
|
2018-11-03 02:39:21 +00:00
|
|
|
result.append(obj.get('_', 'dict') + ':')
|
2018-11-21 20:49:17 +00:00
|
|
|
items = obj.items()
|
|
|
|
has_multiple_items = len(items) > 2
|
|
|
|
if has_multiple_items:
|
|
|
|
result.append('\n')
|
2019-05-19 21:00:23 +00:00
|
|
|
indent += 2
|
2018-11-21 20:49:17 +00:00
|
|
|
for k, v in items:
|
|
|
|
if k == '_' or v is None:
|
|
|
|
continue
|
|
|
|
formatted = yaml_format(v, indent)
|
|
|
|
if not formatted.strip():
|
|
|
|
continue
|
|
|
|
result.append(' ' * (indent if has_multiple_items else 1))
|
2019-05-19 20:59:12 +00:00
|
|
|
result.append(f'{k}:')
|
|
|
|
if not formatted[0].isspace():
|
|
|
|
result.append(' ')
|
|
|
|
result.append(f'{formatted}')
|
2018-11-21 20:49:17 +00:00
|
|
|
result.append('\n')
|
|
|
|
result.pop()
|
2019-05-19 21:00:23 +00:00
|
|
|
if has_multiple_items:
|
|
|
|
indent -= 2
|
2018-11-03 02:39:21 +00:00
|
|
|
elif isinstance(obj, str):
|
|
|
|
# truncate long strings and display elipsis
|
2019-08-03 15:43:37 +00:00
|
|
|
result = repr(obj[:max_str_len])
|
|
|
|
if len(obj) > max_str_len:
|
2018-11-21 20:49:17 +00:00
|
|
|
result += '…'
|
|
|
|
return result
|
2018-11-03 02:39:21 +00:00
|
|
|
elif isinstance(obj, bytes):
|
|
|
|
# repr() bytes if it's printable, hex like "FF EE BB" otherwise
|
2018-12-17 21:21:16 +00:00
|
|
|
if all(0x20 <= c < 0x7f for c in obj):
|
2018-11-21 20:49:17 +00:00
|
|
|
return repr(obj)
|
2018-11-03 02:39:21 +00:00
|
|
|
else:
|
2019-08-03 15:43:37 +00:00
|
|
|
return ('<…>' if len(obj) > max_byte_len else
|
2018-11-21 20:49:17 +00:00
|
|
|
' '.join(f'{b:02X}' for b in obj))
|
2018-11-03 02:39:21 +00:00
|
|
|
elif isinstance(obj, datetime.datetime):
|
|
|
|
# ISO-8601 without timezone offset (telethon dates are always UTC)
|
2018-11-21 20:49:17 +00:00
|
|
|
return obj.strftime('%Y-%m-%d %H:%M:%S')
|
2018-11-03 02:39:21 +00:00
|
|
|
elif hasattr(obj, '__iter__'):
|
|
|
|
# display iterables one after another at the base indentation level
|
|
|
|
result.append('\n')
|
|
|
|
indent += 2
|
|
|
|
for x in obj:
|
2018-11-21 20:38:02 +00:00
|
|
|
result.append(f"{' ' * indent}- {yaml_format(x, indent + 2)}")
|
2018-11-03 02:39:21 +00:00
|
|
|
result.append('\n')
|
|
|
|
result.pop()
|
|
|
|
indent -= 2
|
|
|
|
else:
|
2018-11-21 20:49:17 +00:00
|
|
|
return repr(obj)
|
2018-11-03 02:39:21 +00:00
|
|
|
|
|
|
|
return ''.join(result)
|
|
|
|
|
|
|
|
|
|
|
|
@borg.on(events.NewMessage(pattern=r"\.info", outgoing=True))
|
|
|
|
async def _(event):
|
|
|
|
if not event.message.is_reply:
|
|
|
|
return
|
|
|
|
msg = await event.message.get_reply_message()
|
|
|
|
yaml_text = yaml_format(msg)
|
2019-08-03 15:41:48 +00:00
|
|
|
try:
|
|
|
|
await event.edit(yaml_text, parse_mode=parse_pre)
|
|
|
|
except MessageTooLongError:
|
|
|
|
await event.delete()
|
|
|
|
await borg.send_file(
|
|
|
|
await event.get_input_chat(),
|
|
|
|
f'<pre>{yaml_text}</pre>'.encode('utf-8'),
|
|
|
|
reply_to=msg,
|
|
|
|
attributes=[
|
|
|
|
DocumentAttributeFilename('info.html')
|
|
|
|
],
|
|
|
|
allow_cache=False
|
|
|
|
)
|