From 80d0c19c5a8d548a8c6019033bf574ff2be4c0ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Tue, 14 Jun 2016 20:14:36 +0200 Subject: Refactor, test and improve the way we cut too-long messages for IRC --- src/irc/irc_client.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'src/irc/irc_client.cpp') diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index e320db9..2cf0840 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -455,15 +456,13 @@ bool IrcClient::send_channel_message(const std::string& chan_name, const std::st log_warning("Cannot send message to channel ", chan_name, ", it is not joined"); return false; } - // Cut the message body into 400-bytes parts (because the whole command - // must fit into 512 bytes, that's an easy way to make sure the chan name - // + body fits. I’m lazy.) - std::string::size_type pos = 0; - while (pos < body.size()) - { - this->send_message(IrcMessage("PRIVMSG", {chan_name, body.substr(pos, 400)})); - pos += 400; - } + // Cut the message body into 512-bytes parts, because the whole command + // must fit into 512 bytes. + // Count the ':' at the start of the text, and two spaces + const auto line_size = 512 - ::strlen("PRIVMSG") - chan_name.length() - 3; + const auto lines = cut(body, line_size); + for (const auto& line: lines) + this->send_message(IrcMessage("PRIVMSG", {chan_name, line})); return true; } -- cgit v1.2.3