Skip to content

Commit 48c6d1d

Browse files
authored
fix(irc): tail of long messages weren't arriving on IRC (#169)
It was estimated that 470 characters would be the limit; it was a wrong estimate. Go to an even lower value of 400.
1 parent 858ac57 commit 48c6d1d

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

dibridge/discord.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
log = logging.getLogger(__name__)
1111

12+
# The maximum length of a message on IRC. This is different per network,
13+
# but 400 seems like a safe value for most modern IRC networks.
14+
IRC_MAX_LINE_LENGTH = 400
15+
1216

1317
class RelayDiscord(discord.Client):
1418
def __init__(self, channel_id):
@@ -123,8 +127,13 @@ def find_emojis(content):
123127
content = content.replace("\r\n", "\n").replace("\r", "\n").strip()
124128

125129
# On Discord text between _ and _ is what IRC calls an action.
126-
# IRC allows messages of ~470 characters, so if the action is longer, make it a multi-line message instead.
127-
if content.startswith("_") and content.endswith("_") and "\n" not in content and len(content) < 470:
130+
# IRC has a limit on message size; if reached, make the action multi-line too.
131+
if (
132+
content.startswith("_")
133+
and content.endswith("_")
134+
and "\n" not in content
135+
and len(content) < IRC_MAX_LINE_LENGTH
136+
):
128137
relay.IRC.send_action(message.author.id, message.author.name, content[1:-1])
129138
else:
130139
for full_line in content.split("\n"):
@@ -133,8 +142,8 @@ def find_emojis(content):
133142
if full_line == "```":
134143
continue
135144

136-
# Split the message in lines of at most 470 characters, breaking on words.
137-
for line in textwrap.wrap(full_line.strip(), 470):
145+
# Split the message in lines of at most IRC_MAX_LINE_LENGTH characters, breaking on words.
146+
for line in textwrap.wrap(full_line.strip(), IRC_MAX_LINE_LENGTH):
138147
relay.IRC.send_message(message.author.id, message.author.name, line)
139148

140149
async def on_presence_update(self, before, after):

0 commit comments

Comments
 (0)