Compare commits

...

24 Commits

Author SHA1 Message Date
udf 692cc6b9ac
remove plugins that i dont want 2019-09-09 09:41:27 +02:00
udf 080f18302c
Merge branch 'master' into kate 2019-09-09 09:39:50 +02:00
udf 27d7b5ed82
Merge branch 'master' of https://git.togrand.xyz/uniborg/uniborg 2019-09-09 09:35:39 +02:00
Dan Elkouby 9fe2e80742 Count messages and sort by top posters 2019-09-07 08:08:07 +00:00
Dan Elkouby 40c398833d Add member list command 2019-09-07 07:31:43 +00:00
Lonami 332ecdcaea Escape display name in who 2019-09-03 21:50:39 +02:00
udf 21f80a953d
fuck you lonami you fucking retard
"but dan made the commit"
fuck you, you dont know shit, lonami came up with the idea to use ++
2019-08-14 23:54:48 +02:00
Lonami 69ccfd1207 Add license verbatim 2019-06-08 10:18:28 +02:00
Lonami 6d67ebad06 Dab *has* to be a standard plugin 2019-06-08 10:15:51 +02:00
Lonami 1dd6e63d6a Use HTML in who 2019-05-20 09:53:16 +02:00
kate fe6b878064 Fix info formatting in some cases (#8) 2019-05-19 23:10:01 +02:00
Lonami 168ee165b9 await the now async remove_plugin 2019-04-30 20:44:08 +02:00
Lonami 93cbb44459 Make unload_plugin async for reasons 2019-04-30 20:42:32 +02:00
Lonami febe3c4bac Be more careful when unloading plugin 2019-04-30 20:34:05 +02:00
Lonami c8b5179cb5 Allow plugins to define an unload function 2019-04-30 20:32:35 +02:00
Dan Elkouby c2197e5280 Disable markdown in sed 2019-04-27 10:22:36 +00:00
Dan Elkouby ca5619b9cc Add fpost plugin 2019-03-31 21:58:32 +00:00
Lonami b4be66162e Fix fixreply on different chats 2019-03-04 08:05:40 +01:00
Lonami Exo 59a0ebe9dc Add fixreply plugin 2019-02-27 13:20:24 +01:00
Dan Elkouby 88a8b4e0d3 Add a timeout for sed 2019-02-26 22:27:01 +00:00
Dan Elkouby a53b5fb147 Revert "s/regex/re/g"
This reverts commit dc2eefd581.
2019-02-26 21:10:21 +00:00
Lonami dc2eefd581 s/regex/re/g 2019-02-26 18:52:57 +01:00
Lonami 84e3c4bef8 Support enclosing circle in markdown 2019-02-21 13:22:49 +01:00
Dan Elkouby d274f79d87 Allow sending a snippet with ! 2019-02-18 17:44:42 +00:00
6 changed files with 70 additions and 11 deletions

View File

@ -36,6 +36,10 @@ def parse_strikethrough(m):
return ("\u0336".join(m[1]) + "\u0336"), None
def parse_enclosing_circle(m):
return ("\u20e0".join(m[1]) + "\u20e0"), None
def parse_subreddit(m):
text = '/' + m.group(3)
entity = MessageEntityTextUrl(
@ -61,8 +65,9 @@ def parse_snip(m):
# where the parse function takes the match and returns (text, entity)
MATCHERS = [
(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_enclosing_circle),
(re.compile(r'([^/\w]|^)(/?(r/\w+))'), parse_subreddit),
(re.compile(r'(!\w+)'), parse_snip)
]

View File

@ -13,7 +13,7 @@ TYPE_DOCUMENT = 2
snips = storage.snips or {}
@borg.on(events.NewMessage(pattern=r'\.snip (\S+)', outgoing=True))
@borg.on(events.NewMessage(pattern=r'(?:\.snip +|!)(\w+)$', outgoing=True))
async def on_snip(event):
await event.delete()
name = event.pattern_match.group(1)

View File

@ -1,12 +1,21 @@
# 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 html
from telethon import events
from telethon import utils
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))
async def _(event):
if not event.message.is_reply:
@ -14,14 +23,30 @@ async def _(event):
else:
msg = await event.message.get_reply_message()
if msg.forward:
# FIXME forward privacy memes
who = await borg.get_entity(
msg.forward.from_id or msg.forward.channel_id)
else:
who = await msg.get_sender()
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(get_who_string(who), parse_mode='html')
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')

View File

@ -17,7 +17,7 @@ async def load_reload(event):
try:
if shortname in borg._plugins:
borg.remove_plugin(shortname)
await borg.remove_plugin(shortname)
borg.load_plugin(shortname)
msg = await event.respond(
@ -39,7 +39,7 @@ async def remove(event):
if shortname == "_core":
msg = await event.respond(f"Not removing {shortname}")
elif shortname in borg._plugins:
borg.remove_plugin(shortname)
await borg.remove_plugin(shortname)
msg = await event.respond(f"Removed plugin {shortname}")
else:
msg = await event.respond(f"Plugin {shortname} is not loaded")

View File

@ -3,6 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import asyncio
import importlib.util
import inspect
import logging
from pathlib import Path
@ -71,7 +72,7 @@ class Uniborg(TelegramClient):
self._plugins[shortname] = mod
self._logger.info(f"Successfully loaded plugin {shortname}")
def remove_plugin(self, shortname):
async def remove_plugin(self, shortname):
name = self._plugins[shortname].__name__
for i in reversed(range(len(self._event_builders))):
@ -79,7 +80,16 @@ class Uniborg(TelegramClient):
if cb.__module__ == name:
del self._event_builders[i]
del self._plugins[shortname]
plugin = self._plugins.pop(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}")
def await_event(self, event_matcher, filter=None):

View File

@ -2,7 +2,9 @@
# 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 functools
import re
import signal
from telethon import events
from telethon.tl.functions.messages import GetPeerDialogsRequest
@ -35,3 +37,20 @@ async def get_recent_self_message(borg, event):
await event.get_input_chat(), limit=20):
if message.out:
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