From 9c114a52f1355a139faf71b037f1976cad708364 Mon Sep 17 00:00:00 2001 From: LyricLy Date: Tue, 21 Dec 2021 05:32:08 +1300 Subject: [PATCH] Add support for IRC actions --- src/eventbus.py | 1 + src/irc_link.py | 27 ++++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/eventbus.py b/src/eventbus.py index b4f7a2e..1d2cc62 100644 --- a/src/eventbus.py +++ b/src/eventbus.py @@ -27,6 +27,7 @@ class Message: source: (str, any) id: int attachments: list[discord.Attachment] + action: bool = False reply: (AuthorInfo, str) = None evbus_messages = prometheus_client.Counter("abr_evbus_messages", "Messages processed by event bus", ["source_type"]) diff --git a/src/irc_link.py b/src/irc_link.py index b4536ea..017ff1c 100644 --- a/src/irc_link.py +++ b/src/irc_link.py @@ -50,10 +50,16 @@ async def initialize(): def inuse(conn, event): 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(), []) + def on_msg(conn, event, action): + msg = eventbus.Message(eventbus.AuthorInfo(event.source.nick, str(event.source), None), [" ".join(event.arguments)], (util.config["irc"]["name"], event.target), util.random_id(), [], action=action) asyncio.create_task(eventbus.push(msg)) + def pubmsg(conn, event): + on_msg(conn, event, False) + + def action(conn, event): + on_msg(conn, event, True) + def bytewise_truncate(x, max): x = x[:max] while True: @@ -62,16 +68,22 @@ async def initialize(): except UnicodeDecodeError: x = x[:-1] - def render_line(author, content): + def render_name(author): + return f"{random_color(author.id)}{author.name[0]}\u200B{author.name[1:]}{color_code('')}" + + def render_line(author, content, action): # colorize for aesthetics # add ZWS to prevent pinging - return f"<{random_color(author.id)}{author.name[0]}\u200B{author.name[1:]}{color_code('')}> {content}" + if not action: + return f"<{render_name(author)}> {content}" + else: + return f"* {render_name(author)} {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) if msg.reply: - reply_line = render_line(msg.reply[0], render_formatting(msg.reply[1])).encode("utf-8") + reply_line = render_line(msg.reply[0], render_formatting(msg.reply[1]), False).encode("utf-8") reply_line_new, reply_line_u = bytewise_truncate(reply_line, 300) if reply_line_new != reply_line: reply_line_u += " ..." @@ -84,9 +96,9 @@ async def initialize(): lines.append(next_line_u) content = content[len(next_line):] for line in lines: - conn.privmsg(channel_name, render_line(msg.author, line)) + conn.privmsg(channel_name, render_line(msg.author, line, msg.action)) for at in msg.attachments: - conn.privmsg(channel_name, render_line(msg.author, f"-> {at.filename}: {at.proxy_url}")) + conn.privmsg(channel_name, render_line(msg.author, f"-> {at.filename}: {at.proxy_url}", False)) else: logging.warning("IRC channel %s not allowed", channel_name) @@ -106,6 +118,7 @@ async def initialize(): conn.add_global_handler("disconnect", disconnect) conn.add_global_handler("nicknameinuse", inuse) conn.add_global_handler("pubmsg", pubmsg) + conn.add_global_handler("action", action) global unlisten unlisten = eventbus.add_listener(util.config["irc"]["name"], on_bridge_message)