From 329dfe422b1d5152f65a09e86bb0dcfa48929fdf Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Sat, 18 Jul 2026 15:24:34 +0200 Subject: [PATCH] fix: irc messages not getting split by line --- src/EchoHub.Server.Irc/IrcMessageFormatter.cs | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/EchoHub.Server.Irc/IrcMessageFormatter.cs b/src/EchoHub.Server.Irc/IrcMessageFormatter.cs index 005b157..1fba9d2 100644 --- a/src/EchoHub.Server.Irc/IrcMessageFormatter.cs +++ b/src/EchoHub.Server.Irc/IrcMessageFormatter.cs @@ -138,34 +138,41 @@ public static class IrcMessageFormatter } /// - /// Split a message into chunks of approximately maxBytes (UTF-8), at word boundaries. + /// Split a message into chunks of approximately maxBytes (UTF-8), at line and word boundaries. /// public static List SplitMessage(string content, int maxBytes) { - if (Encoding.UTF8.GetByteCount(content) <= maxBytes) - return [content]; - var chunks = new List(); - var current = new StringBuilder(); - var currentBytes = 0; - foreach (var word in content.Split(' ')) + foreach (var line in content.Split('\n')) { - var wordBytes = Encoding.UTF8.GetByteCount(word) + 1; // +1 for space - - if (currentBytes + wordBytes > maxBytes && current.Length > 0) + if (Encoding.UTF8.GetByteCount(line) <= maxBytes) { - chunks.Add(current.ToString().TrimEnd()); - current.Clear(); - currentBytes = 0; + chunks.Add(line); + continue; } - current.Append(word).Append(' '); - currentBytes += wordBytes; - } + var current = new StringBuilder(); + var currentBytes = 0; - if (current.Length > 0) - chunks.Add(current.ToString().TrimEnd()); + foreach (var word in line.Split(' ')) + { + var wordBytes = Encoding.UTF8.GetByteCount(word) + 1; // +1 for space + + if (currentBytes + wordBytes > maxBytes && current.Length > 0) + { + chunks.Add(current.ToString().TrimEnd()); + current.Clear(); + currentBytes = 0; + } + + current.Append(word).Append(' '); + currentBytes += wordBytes; + } + + if (current.Length > 0) + chunks.Add(current.ToString().TrimEnd()); + } return chunks; }