From 9c713a09801c4b97b2f33614255b819a1d4e5a53 Mon Sep 17 00:00:00 2001 From: osmarks Date: Thu, 28 Oct 2021 12:55:40 +0100 Subject: [PATCH] Improve bridging capabilities --- src/eventbus.py | 1 + src/irc_link.py | 33 +++++++++++++++++++++++++++++---- src/telephone.py | 6 +++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/eventbus.py b/src/eventbus.py index 3bf3962..b4f7a2e 100644 --- a/src/eventbus.py +++ b/src/eventbus.py @@ -27,6 +27,7 @@ class Message: source: (str, any) id: int attachments: list[discord.Attachment] + reply: (AuthorInfo, str) = None 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 bf3bd51..b4536ea 100644 --- a/src/irc_link.py +++ b/src/irc_link.py @@ -54,14 +54,39 @@ async def initialize(): 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)) + def bytewise_truncate(x, max): + x = x[:max] + while True: + try: + return x, x.decode("utf-8") + except UnicodeDecodeError: + x = x[:-1] + + def render_line(author, content): + # colorize for aesthetics + # add ZWS to prevent pinging + return f"<{random_color(author.id)}{author.name[0]}\u200B{author.name[1:]}{color_code('')}> {content}" + async def on_bridge_message(channel_name, msg): if channel_name in util.config["irc"]["channels"]: if channel_name not in joined: conn.join(channel_name) - # 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) + if msg.reply: + reply_line = render_line(msg.reply[0], render_formatting(msg.reply[1])).encode("utf-8") + reply_line_new, reply_line_u = bytewise_truncate(reply_line, 300) + if reply_line_new != reply_line: + reply_line_u += " ..." + conn.privmsg(channel_name, f"[Replying to {reply_line_u}]") + lines = [] + content = render_formatting(msg.message).encode("utf-8") + # somewhat accursedly break string into valid UTF-8 substrings with <=400 bytes + while content: + next_line, next_line_u = bytewise_truncate(content, 400) + lines.append(next_line_u) + content = content[len(next_line):] + for line in lines: + conn.privmsg(channel_name, render_line(msg.author, line)) for at in msg.attachments: - conn.privmsg(channel_name, f"-> {at.filename}: {at.proxy_url}") + conn.privmsg(channel_name, render_line(msg.author, f"-> {at.filename}: {at.proxy_url}")) else: logging.warning("IRC channel %s not allowed", channel_name) diff --git a/src/telephone.py b/src/telephone.py index f9c2dbc..64d3de7 100644 --- a/src/telephone.py +++ b/src/telephone.py @@ -113,8 +113,12 @@ class Telephone(commands.Cog): 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 + reply = None + if msg.reference and msg.reference.cached_message: + replying_to = msg.reference.cached_message + reply = (eventbus.AuthorInfo(replying_to.author.name, replying_to.author.id, str(replying_to.author.avatar_url), replying_to.author.bot), parse_formatting(self.bot, replying_to.content)) 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() ]) + parse_formatting(self.bot, msg.content), ("discord", channel_id), msg.id, [ at for at in msg.attachments if not at.is_spoiler() ], reply=reply) await eventbus.push(msg) def cog_unload(self):