summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlouiz’ <louiz@louiz.org>2016-06-14 20:14:36 +0200
committerlouiz’ <louiz@louiz.org>2016-06-14 20:14:36 +0200
commit80d0c19c5a8d548a8c6019033bf574ff2be4c0ce (patch)
treec02303b06ee6a7603f175aab767f3d5742152c4b
parent19bca5c2f2b104d534a7c8be7f61dc48d928cf24 (diff)
downloadbiboumi-80d0c19c5a8d548a8c6019033bf574ff2be4c0ce.tar.gz
biboumi-80d0c19c5a8d548a8c6019033bf574ff2be4c0ce.tar.bz2
biboumi-80d0c19c5a8d548a8c6019033bf574ff2be4c0ce.tar.xz
biboumi-80d0c19c5a8d548a8c6019033bf574ff2be4c0ce.zip
Refactor, test and improve the way we cut too-long messages for IRC
-rw-r--r--louloulibs/utils/string.cpp12
-rw-r--r--louloulibs/utils/string.hpp2
-rw-r--r--src/irc/irc_client.cpp17
-rw-r--r--tests/utils.cpp6
4 files changed, 28 insertions, 9 deletions
diff --git a/louloulibs/utils/string.cpp b/louloulibs/utils/string.cpp
index 3977feb..7ed1aa3 100644
--- a/louloulibs/utils/string.cpp
+++ b/louloulibs/utils/string.cpp
@@ -4,3 +4,15 @@ bool to_bool(const std::string& val)
{
return (val == "1" || val == "true");
}
+
+std::vector<std::string> cut(const std::string& val, const std::size_t size)
+{
+ std::vector<std::string> res;
+ std::string::size_type pos = 0;
+ while (pos < val.size())
+ {
+ res.emplace_back(val.substr(pos, size));
+ pos += size;
+ }
+ return res;
+}
diff --git a/louloulibs/utils/string.hpp b/louloulibs/utils/string.hpp
index 3775c36..1c8f001 100644
--- a/louloulibs/utils/string.hpp
+++ b/louloulibs/utils/string.hpp
@@ -1,8 +1,10 @@
#ifndef STRING_UTILS_HPP_INCLUDED
#define STRING_UTILS_HPP_INCLUDED
+#include <vector>
#include <string>
bool to_bool(const std::string& val);
+std::vector<std::string> cut(const std::string& val, const std::size_t size);
#endif /* STRING_UTILS_HPP_INCLUDED */
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 <config/config.hpp>
#include <utils/tolower.hpp>
#include <utils/split.hpp>
+#include <utils/string.hpp>
#include <sstream>
#include <iostream>
@@ -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;
}
diff --git a/tests/utils.cpp b/tests/utils.cpp
index 8691910..54e743f 100644
--- a/tests/utils.cpp
+++ b/tests/utils.cpp
@@ -88,3 +88,9 @@ TEST_CASE("empty if fixed irc server")
}
}
+
+TEST_CASE("string cut")
+{
+ CHECK(cut("coucou", 2).size() == 3);
+ CHECK(cut("bonjour les copains", 6).size() == 4);
+} \ No newline at end of file