Compare commits

..

2 Commits

Author SHA1 Message Date
udf 8c0f04c0cb
Add license header 2018-11-21 22:08:27 +02:00
udf 3e5b6fddb3
Add superblock plugin 2018-11-21 22:08:27 +02:00
2 changed files with 58 additions and 91 deletions

View File

@ -1,91 +0,0 @@
# 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
from telethon.tl.types import MessageEntityPre
from telethon.tl.tlobject import TLObject
import datetime
STR_LEN_MAX = 256
BYTE_LEN_MAX = 64
def parse_pre(text):
text = text.strip()
return (
text,
[MessageEntityPre(offset=0, length=len(add_surrogate(text)), language='')]
)
def yaml_format(obj, indent=0):
"""
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):
if not obj:
return 'dict:'
result.append(obj.get('_', 'dict') + ':')
items = obj.items()
has_multiple_items = len(items) > 2
if has_multiple_items:
result.append('\n')
indent += 2
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))
result.append(f'{k}: {formatted}')
result.append('\n')
result.pop()
indent -= 2
result.append(' ' * indent)
elif isinstance(obj, str):
# truncate long strings and display elipsis
result = repr(obj[:STR_LEN_MAX])
if len(obj) > STR_LEN_MAX:
result += ''
return result
elif isinstance(obj, bytes):
# repr() bytes if it's printable, hex like "FF EE BB" otherwise
if all(0x20 <= c < 0x7f for c in obj):
return repr(obj)
else:
return ('<…>' if len(obj) > BYTE_LEN_MAX else
' '.join(f'{b:02X}' for b in obj))
elif isinstance(obj, datetime.datetime):
# ISO-8601 without timezone offset (telethon dates are always UTC)
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif hasattr(obj, '__iter__'):
# display iterables one after another at the base indentation level
result.append('\n')
indent += 2
for x in obj:
result.append(f"{' ' * indent}- {yaml_format(x, indent + 2)}")
result.append('\n')
result.pop()
indent -= 2
result.append(' ' * indent)
else:
return repr(obj)
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)
await event.edit(yaml_text, parse_mode=parse_pre)

58
stdplugins/superblock.py Normal file
View File

@ -0,0 +1,58 @@
# 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/.
import asyncio
from telethon import events
import telethon.tl.functions as tlf
from telethon.tl.types import InputPeerChannel, UpdateUserBlocked
from telethon.tl.functions.contacts import GetBlockedRequest
# How often to fetch the full list of blocked users
REFETCH_TIME = 60
blocked_user_ids = set()
@borg.on(events.NewMessage(incoming=True, func=lambda e: e.message.mentioned))
async def on_mentioned(event):
if not event.message.from_id: # Channel messages don't have a from_id
return
if event.from_id not in blocked_user_ids:
return
peer = await borg.get_input_entity(event.chat_id)
if isinstance(peer, InputPeerChannel):
o = tlf.channels.ReadMessageContentsRequest(peer, [event.message.id])
else:
o = tlf.messages.ReadMessageContentsRequest([event.message.id])
await borg(o)
@borg.on(events.Raw(types=UpdateUserBlocked))
async def on_blocked(event):
if event.blocked:
blocked_user_ids.add(event.user_id)
else:
blocked_user_ids.discard(event.user_id)
async def fetch_blocked_users():
global blocked_user_ids
while 1:
offset = 0
blocked_ids = set()
while 1:
blocked = await borg(GetBlockedRequest(offset=offset, limit=100))
offset += 100
for contact in blocked.blocked:
blocked_ids.add(contact.user_id)
if not blocked.blocked:
break
blocked_user_ids = blocked_ids
await asyncio.sleep(REFETCH_TIME)
asyncio.ensure_future(fetch_blocked_users())