forked from uniborg/uniborg
Compare commits
No commits in common. "692cc6b9acde773ecc216446f59248dfd0d8dfec" and "3178eb5fbafd214dbd06d60213ae41e7d0d1b8c0" have entirely different histories.
692cc6b9ac
...
3178eb5fba
|
@ -36,10 +36,6 @@ def parse_strikethrough(m):
|
||||||
return ("\u0336".join(m[1]) + "\u0336"), None
|
return ("\u0336".join(m[1]) + "\u0336"), None
|
||||||
|
|
||||||
|
|
||||||
def parse_enclosing_circle(m):
|
|
||||||
return ("\u20e0".join(m[1]) + "\u20e0"), None
|
|
||||||
|
|
||||||
|
|
||||||
def parse_subreddit(m):
|
def parse_subreddit(m):
|
||||||
text = '/' + m.group(3)
|
text = '/' + m.group(3)
|
||||||
entity = MessageEntityTextUrl(
|
entity = MessageEntityTextUrl(
|
||||||
|
@ -65,9 +61,8 @@ def parse_snip(m):
|
||||||
# where the parse function takes the match and returns (text, entity)
|
# where the parse function takes the match and returns (text, entity)
|
||||||
MATCHERS = [
|
MATCHERS = [
|
||||||
(DEFAULT_URL_RE, parse_url_match),
|
(DEFAULT_URL_RE, parse_url_match),
|
||||||
(re.compile(r'!\+(.+?)\+!'), parse_aesthetics),
|
(re.compile(r'\+\+(.+?)\+\+'), parse_aesthetics),
|
||||||
(re.compile(r'~~(.+?)~~'), parse_strikethrough),
|
(re.compile(r'~~(.+?)~~'), parse_strikethrough),
|
||||||
(re.compile(r'@@(.+?)@@'), parse_enclosing_circle),
|
|
||||||
(re.compile(r'([^/\w]|^)(/?(r/\w+))'), parse_subreddit),
|
(re.compile(r'([^/\w]|^)(/?(r/\w+))'), parse_subreddit),
|
||||||
(re.compile(r'(!\w+)'), parse_snip)
|
(re.compile(r'(!\w+)'), parse_snip)
|
||||||
]
|
]
|
||||||
|
|
|
@ -13,7 +13,7 @@ TYPE_DOCUMENT = 2
|
||||||
snips = storage.snips or {}
|
snips = storage.snips or {}
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r'(?:\.snip +|!)(\w+)$', outgoing=True))
|
@borg.on(events.NewMessage(pattern=r'\.snip (\S+)', outgoing=True))
|
||||||
async def on_snip(event):
|
async def on_snip(event):
|
||||||
await event.delete()
|
await event.delete()
|
||||||
name = event.pattern_match.group(1)
|
name = event.pattern_match.group(1)
|
||||||
|
|
|
@ -1,21 +1,12 @@
|
||||||
# This Source Code Form is subject to the terms of the Mozilla Public
|
# 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
|
# 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/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
import html
|
|
||||||
|
|
||||||
from telethon import events
|
from telethon import events
|
||||||
from telethon import utils
|
from telethon import utils
|
||||||
from telethon.tl import types
|
from telethon.tl import types
|
||||||
|
|
||||||
|
|
||||||
def get_who_string(who):
|
|
||||||
who_string = html.escape(utils.get_display_name(who))
|
|
||||||
if isinstance(who, (types.User, types.Channel)) and who.username:
|
|
||||||
who_string += f" <i>(@{who.username})</i>"
|
|
||||||
who_string += f", <a href='tg://user?id={who.id}'>#{who.id}</a>"
|
|
||||||
return who_string
|
|
||||||
|
|
||||||
|
|
||||||
@borg.on(events.NewMessage(pattern=r"\.who", outgoing=True))
|
@borg.on(events.NewMessage(pattern=r"\.who", outgoing=True))
|
||||||
async def _(event):
|
async def _(event):
|
||||||
if not event.message.is_reply:
|
if not event.message.is_reply:
|
||||||
|
@ -23,30 +14,14 @@ async def _(event):
|
||||||
else:
|
else:
|
||||||
msg = await event.message.get_reply_message()
|
msg = await event.message.get_reply_message()
|
||||||
if msg.forward:
|
if msg.forward:
|
||||||
# FIXME forward privacy memes
|
|
||||||
who = await borg.get_entity(
|
who = await borg.get_entity(
|
||||||
msg.forward.from_id or msg.forward.channel_id)
|
msg.forward.from_id or msg.forward.channel_id)
|
||||||
else:
|
else:
|
||||||
who = await msg.get_sender()
|
who = await msg.get_sender()
|
||||||
|
|
||||||
await event.edit(get_who_string(who), parse_mode='html')
|
who_string = utils.get_display_name(who)
|
||||||
|
if isinstance(who, (types.User, types.Channel)) and who.username:
|
||||||
|
who_string += f" (@{who.username})"
|
||||||
|
who_string += f", [#{who.id}](tg://user?id={who.id})"
|
||||||
|
|
||||||
|
await event.edit(who_string)
|
||||||
@borg.on(events.NewMessage(pattern=r"\.members", outgoing=True))
|
|
||||||
async def _(event):
|
|
||||||
members = []
|
|
||||||
async for member in borg.iter_participants(event.chat_id):
|
|
||||||
messages = await borg.get_messages(
|
|
||||||
event.chat_id,
|
|
||||||
from_user=member,
|
|
||||||
limit=0
|
|
||||||
)
|
|
||||||
members.append((
|
|
||||||
messages.total,
|
|
||||||
f"{messages.total} - {get_who_string(member)}"
|
|
||||||
))
|
|
||||||
members = (
|
|
||||||
m[1] for m in sorted(members, key=lambda m: m[0], reverse=True)
|
|
||||||
)
|
|
||||||
|
|
||||||
await event.edit("\n".join(members), parse_mode='html')
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ async def load_reload(event):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if shortname in borg._plugins:
|
if shortname in borg._plugins:
|
||||||
await borg.remove_plugin(shortname)
|
borg.remove_plugin(shortname)
|
||||||
borg.load_plugin(shortname)
|
borg.load_plugin(shortname)
|
||||||
|
|
||||||
msg = await event.respond(
|
msg = await event.respond(
|
||||||
|
@ -39,7 +39,7 @@ async def remove(event):
|
||||||
if shortname == "_core":
|
if shortname == "_core":
|
||||||
msg = await event.respond(f"Not removing {shortname}")
|
msg = await event.respond(f"Not removing {shortname}")
|
||||||
elif shortname in borg._plugins:
|
elif shortname in borg._plugins:
|
||||||
await borg.remove_plugin(shortname)
|
borg.remove_plugin(shortname)
|
||||||
msg = await event.respond(f"Removed plugin {shortname}")
|
msg = await event.respond(f"Removed plugin {shortname}")
|
||||||
else:
|
else:
|
||||||
msg = await event.respond(f"Plugin {shortname} is not loaded")
|
msg = await event.respond(f"Plugin {shortname} is not loaded")
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
import asyncio
|
import asyncio
|
||||||
import importlib.util
|
import importlib.util
|
||||||
import inspect
|
|
||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
@ -72,7 +71,7 @@ class Uniborg(TelegramClient):
|
||||||
self._plugins[shortname] = mod
|
self._plugins[shortname] = mod
|
||||||
self._logger.info(f"Successfully loaded plugin {shortname}")
|
self._logger.info(f"Successfully loaded plugin {shortname}")
|
||||||
|
|
||||||
async def remove_plugin(self, shortname):
|
def remove_plugin(self, shortname):
|
||||||
name = self._plugins[shortname].__name__
|
name = self._plugins[shortname].__name__
|
||||||
|
|
||||||
for i in reversed(range(len(self._event_builders))):
|
for i in reversed(range(len(self._event_builders))):
|
||||||
|
@ -80,16 +79,7 @@ class Uniborg(TelegramClient):
|
||||||
if cb.__module__ == name:
|
if cb.__module__ == name:
|
||||||
del self._event_builders[i]
|
del self._event_builders[i]
|
||||||
|
|
||||||
plugin = self._plugins.pop(shortname)
|
del self._plugins[shortname]
|
||||||
if callable(getattr(plugin, 'unload', None)):
|
|
||||||
try:
|
|
||||||
unload = plugin.unload()
|
|
||||||
if inspect.isawaitable(unload):
|
|
||||||
await unload
|
|
||||||
except Exception:
|
|
||||||
self._logger.exception(f'Unhandled exception unloading {shortname}')
|
|
||||||
|
|
||||||
del plugin
|
|
||||||
self._logger.info(f"Removed plugin {shortname}")
|
self._logger.info(f"Removed plugin {shortname}")
|
||||||
|
|
||||||
def await_event(self, event_matcher, filter=None):
|
def await_event(self, event_matcher, filter=None):
|
||||||
|
|
|
@ -2,9 +2,7 @@
|
||||||
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
# 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/.
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||||
|
|
||||||
import functools
|
|
||||||
import re
|
import re
|
||||||
import signal
|
|
||||||
|
|
||||||
from telethon import events
|
from telethon import events
|
||||||
from telethon.tl.functions.messages import GetPeerDialogsRequest
|
from telethon.tl.functions.messages import GetPeerDialogsRequest
|
||||||
|
@ -37,20 +35,3 @@ async def get_recent_self_message(borg, event):
|
||||||
await event.get_input_chat(), limit=20):
|
await event.get_input_chat(), limit=20):
|
||||||
if message.out:
|
if message.out:
|
||||||
return message
|
return message
|
||||||
|
|
||||||
def _handle_timeout(signum, frame):
|
|
||||||
raise TimeoutError("Execution took too long")
|
|
||||||
|
|
||||||
def sync_timeout(seconds):
|
|
||||||
def decorator(func):
|
|
||||||
@functools.wraps(func)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
signal.signal(signal.SIGALRM, _handle_timeout)
|
|
||||||
signal.setitimer(signal.ITIMER_REAL, seconds)
|
|
||||||
try:
|
|
||||||
r = func(*args, **kwargs)
|
|
||||||
finally:
|
|
||||||
signal.alarm(0)
|
|
||||||
return r
|
|
||||||
return wrapper
|
|
||||||
return decorator
|
|
||||||
|
|
Loading…
Reference in New Issue