From ba2ce97290ce8a8cabd2060ae6dea88821051cae Mon Sep 17 00:00:00 2001 From: osmarks Date: Wed, 28 Jul 2021 20:30:37 +0100 Subject: [PATCH] bridge attachments, other fixes --- src/eventbus.py | 2 ++ src/irc_link.py | 4 +++- src/reminders.py | 2 +- src/telephone.py | 31 ++++++++++++++++++------------- src/util.py | 3 +++ 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/eventbus.py b/src/eventbus.py index 4f35bdf..3bf3962 100644 --- a/src/eventbus.py +++ b/src/eventbus.py @@ -4,6 +4,7 @@ import dataclasses import typing import collections import logging +import discord import util @@ -25,6 +26,7 @@ class Message: message: list[typing.Union[str, dict]] source: (str, any) id: int + attachments: list[discord.Attachment] evbus_messages = prometheus_client.Counter("abr_evbus_messages", "Messages processed by event bus", ["source_type"]) evbus_messages_dropped = prometheus_client.Counter("abr_evbus_messages_dropped", "Messages received by event bus but dropped by rate limits", ["source_type"]) diff --git a/src/irc_link.py b/src/irc_link.py index 8976826..14e6698 100644 --- a/src/irc_link.py +++ b/src/irc_link.py @@ -51,7 +51,7 @@ async def initialize(): conn.nick(scramble(conn.get_nickname())) def pubmsg(conn, event): - msg = eventbus.Message(eventbus.AuthorInfo(event.source.nick, str(event.source), None), [" ".join(event.arguments)], (util.config["irc"]["name"], event.target), util.random_id()) + msg = eventbus.Message(eventbus.AuthorInfo(event.source.nick, str(event.source), None), [" ".join(event.arguments)], (util.config["irc"]["name"], event.target), util.random_id(), []) asyncio.create_task(eventbus.push(msg)) async def on_bridge_message(channel_name, msg): @@ -60,6 +60,8 @@ async def initialize(): # ping fix - zero width space embedded in messages line = f"<{random_color(msg.author.id)}{msg.author.name[0]}\u200B{msg.author.name[1:]}{color_code('')}> " + render_formatting(msg.message)[:400] conn.privmsg(channel_name, line) + for at in msg.attachments: + conn.privmsg(channel_name, f"-> {at.filename}: {at.proxy_url}") else: logging.warning("IRC channel %s not allowed", channel_name) diff --git a/src/reminders.py b/src/reminders.py index 7ee3e5d..91c01b7 100644 --- a/src/reminders.py +++ b/src/reminders.py @@ -86,7 +86,7 @@ def setup(bot): created_timestamp = datetime.utcfromtimestamp(created_timestamp).replace(tzinfo=timezone.utc) extra = json.loads(extra) uid = extra["author_id"] - tz = await util.get_user_timezone(util.AltCtx(util.IDWrapper(uid), util.IDWrapper(None), bot)) + tz = await util.get_user_timezone(util.AltCtx(util.IDWrapper(uid), util.IDWrapper(extra.get("guild_id")), bot)) print(created_timestamp, tz, created_timestamp.astimezone(tz)) created_time = util.format_time(created_timestamp.astimezone(tz)) text = f"<@{uid}> Reminder queued at {created_time}: {reminder_text}" diff --git a/src/telephone.py b/src/telephone.py index e2df6ca..67b8830 100644 --- a/src/telephone.py +++ b/src/telephone.py @@ -87,29 +87,34 @@ class Telephone(commands.Cog): self.webhooks[row["channel_id"]] = row["webhook"] logging.info("Loaded %d webhooks", len(rows)) - async def on_bridge_message(self, channel_id, msg): + async def on_bridge_message(self, channel_id, msg: eventbus.Message): channel = self.bot.get_channel(channel_id) if channel: webhook = self.webhooks.get(channel_id) - if webhook: - try: - self.webhook_queue.put_nowait((webhook, render_formatting(channel, msg.message)[:2000], msg.author.name, msg.author.avatar_url)) - except asyncio.QueueFull: - text = f"<{msg.author.name}> {render_formatting(channel, msg.message)}" + attachments_text = "\n".join(f"{at.filename}: {at.proxy_url}" for at in msg.attachments) + async def send_raw(text): + if webhook: + try: + self.webhook_queue.put_nowait((webhook, text, msg.author.name, msg.author.avatar_url)) + except asyncio.QueueFull: + text = f"<{msg.author.name}> {text}" + await channel.send(text[:2000], allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False)) + else: + text = f"<{msg.author.name}> {text}" await channel.send(text[:2000], allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False)) - else: - text = f"<{msg.author.name}> {render_formatting(channel, msg.message)}" - await channel.send(text[:2000], allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False)) + await send_raw(render_formatting(channel, msg.message)[:2000]) + if attachments_text: await send_raw(attachments_text) else: logging.warning("Channel %d not found", channel_id) @commands.Cog.listener("on_message") async def send_to_bridge(self, msg): # discard webhooks and bridge messages (hackily, admittedly, not sure how else to do this) - if msg.content == "": return + if msg.content == "" and len(msg.attachments) == 0: return if (msg.author == self.bot.user and msg.content[0] == "<") or msg.author.discriminator == "0000": return channel_id = msg.channel.id - msg = eventbus.Message(eventbus.AuthorInfo(msg.author.name, msg.author.id, str(msg.author.avatar_url), msg.author.bot), parse_formatting(self.bot, msg.content), ("discord", channel_id), msg.id) + msg = eventbus.Message(eventbus.AuthorInfo(msg.author.name, msg.author.id, str(msg.author.avatar_url), msg.author.bot), + parse_formatting(self.bot, msg.content), ("discord", channel_id), msg.id, [ at for at in msg.attachments if not at.is_spoiler() ]) await eventbus.push(msg) def cog_unload(self): @@ -136,7 +141,7 @@ When you want to end a call, use hangup. @telephone.command(brief="Link to other channels", help="""Connect to another channel on Discord or any supported bridges. Virtual channels also exist. """) - @commands.check(util.admin_check) + @commands.check(util.extpriv_check) async def link(self, ctx, target_type, target_id, bidirectional: bool = True): target_id = util.extract_codeblock(target_id) try: @@ -147,7 +152,7 @@ When you want to end a call, use hangup. pass @telephone.command(brief="Undo link commands.") - @commands.check(util.admin_check) + @commands.check(util.extpriv_check) async def unlink(self, ctx, target_type, target_id, bidirectional: bool = True): target_id = util.extract_codeblock(target_id) try: diff --git a/src/util.py b/src/util.py index 0f5b861..0e53e9f 100644 --- a/src/util.py +++ b/src/util.py @@ -255,6 +255,9 @@ async def server_mod_check(ctx): async def admin_check(ctx): return await ctx.bot.is_owner(ctx.author) +async def extpriv_check(ctx): + return await ctx.bot.is_owner(ctx.author) or ctx.author.id in config["extpriv_users"] + async def get_asset(bot: commands.Bot, identifier): safe_ident = re.sub("[^A-Za-z0-9_.-]", "_", identifier) x = await bot.database.execute_fetchone("SELECT * FROM assets WHERE identifier = ?", (safe_ident,))