Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/eventbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down
27 changes: 20 additions & 7 deletions src/irc_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -62,16 +68,22 @@ def bytewise_truncate(x, max):
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 += " ..."
Expand All @@ -84,9 +96,9 @@ async def on_bridge_message(channel_name, msg):
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)

Expand All @@ -106,6 +118,7 @@ def disconnect(conn, event):
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)
Expand Down