From cd36ccf552484797a0904cebd6a482b1bf49cdd3 Mon Sep 17 00:00:00 2001 From: Dan Elkouby Date: Sun, 8 Apr 2018 17:44:09 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stdplugins/ninja.py | 8 ++++++-- uniborg/_core.py | 5 ++++- uniborg/hacks.py | 1 + uniborg/telethon.py | 14 +++++++++----- uniborg/util.py | 11 +++++++---- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/stdplugins/ninja.py b/stdplugins/ninja.py index 68db5a7..159a9f3 100644 --- a/stdplugins/ninja.py +++ b/stdplugins/ninja.py @@ -9,6 +9,7 @@ import telethon.utils from uniborg import util + async def get_target_message(event): if event.is_reply and (await event.reply_message).from_id == borg.uid: return event.reply_message @@ -16,11 +17,13 @@ async def get_target_message(event): if message.out: return message + async def await_read(chat, message): chat = telethon.utils.get_peer_id(chat) + async def read_filter(read_event): - return telethon.utils.get_peer_id(await read_event.input_chat) == chat \ - and read_event.is_read(message) + return (telethon.utils.get_peer_id(await read_event.input_chat) == chat + and read_event.is_read(message)) fut = borg.await_event(events.MessageRead(inbox=False), read_filter) if await util.is_read(borg, chat, message): @@ -29,6 +32,7 @@ async def await_read(chat, message): await fut + @borg.on(util.admin_cmd(r"^\.del(ete)?$")) async def delete(event): await event.delete() diff --git a/uniborg/_core.py b/uniborg/_core.py index 4c726ab..378819f 100644 --- a/uniborg/_core.py +++ b/uniborg/_core.py @@ -9,6 +9,7 @@ from uniborg import util DELETE_TIMEOUT = 2 + @borg.on(util.admin_cmd(r"^\.load (?P\w+)$")) async def load_reload(event): await event.delete() @@ -19,7 +20,8 @@ async def load_reload(event): borg.remove_plugin(shortname) borg.load_plugin(shortname) - msg = await event.respond(f"Successfully (re)loaded plugin {shortname}") + msg = await event.respond( + f"Successfully (re)loaded plugin {shortname}") await asyncio.sleep(DELETE_TIMEOUT) await borg.delete_messages(msg.to_id, msg) @@ -28,6 +30,7 @@ async def load_reload(event): logger.warn(f"Failed to (re)load plugin {shortname}: {tb}") await event.respond(f"Failed to (re)load plugin {shortname}: {e}") + @borg.on(util.admin_cmd(r"^\.remove (?P\w+)$")) async def remove(event): await event.delete() diff --git a/uniborg/hacks.py b/uniborg/hacks.py index 19fb210..9ce40e6 100644 --- a/uniborg/hacks.py +++ b/uniborg/hacks.py @@ -2,6 +2,7 @@ # 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/. + class ReverseList(list): def __iter__(self): return reversed(self) diff --git a/uniborg/telethon.py b/uniborg/telethon.py index 6a6cf7a..35270e6 100644 --- a/uniborg/telethon.py +++ b/uniborg/telethon.py @@ -13,16 +13,20 @@ import telethon.events from . import hacks + class Uniborg(TelegramClient): - def __init__(self, session, *, plugin_path="plugins", bot_token=None, **kwargs): + def __init__( + self, session, *, plugin_path="plugins", + bot_token=None, **kwargs): # TODO: handle non-string session self._name = session self._logger = logging.getLogger(session) self._plugins = {} self._plugin_path = plugin_path - super().__init__(session, - 17349, "344583e45741c457fe1862106095a5eb", # yarr + super().__init__( + session, + 17349, "344583e45741c457fe1862106095a5eb", # yarr **kwargs) # This is a hack, please avert your eyes @@ -87,7 +91,7 @@ class Uniborg(TelegramClient): fut.set_result(event) raise telethon.events.StopPropagation - fut.add_done_callback(lambda _: - self.remove_event_handler(cb, event_matcher)) + fut.add_done_callback( + lambda _: self.remove_event_handler(cb, event_matcher)) return fut diff --git a/uniborg/util.py b/uniborg/util.py index 7c06fb8..e9be581 100644 --- a/uniborg/util.py +++ b/uniborg/util.py @@ -7,20 +7,23 @@ import re from telethon import events from telethon.tl.functions.messages import GetPeerDialogsRequest + def admin_cmd(pattern): return events.NewMessage(outgoing=True, pattern=re.compile(pattern)) + async def is_read(borg, entity, message, is_out=None): """ Returns True if the given message (or id) has been read if a id is given, is_out needs to be a bool """ - is_out = getattr(message, 'out', is_out) + is_out = getattr(message, "out", is_out) if not isinstance(is_out, bool): - raise ValueError('Message was id but is_out not provided or not a bool') - message_id = getattr(message, 'id', message) + raise ValueError( + "Message was id but is_out not provided or not a bool") + message_id = getattr(message, "id", message) if not isinstance(message_id, int): - raise ValueError('Failed to extract id from message') + raise ValueError("Failed to extract id from message") dialog = (await borg(GetPeerDialogsRequest([entity]))).dialogs[0] max_id = dialog.read_outbox_max_id if is_out else dialog.read_inbox_max_id