From df2fe0bc4bf6240e47abf26ff88ffdfc9373f481 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sun, 16 Aug 2020 15:51:47 +0200 Subject: Fix the parsing of IRC messages, especially with trailing spaces --- src/irc/irc_message.cpp | 39 +++++++++----------- src/irc/irc_message.hpp | 4 +- .../end_to_end/scenarios/multiple_channels_join.py | 4 ++ tests/irc.cpp | 43 ++++++++++++++++++++++ 4 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 tests/irc.cpp diff --git a/src/irc/irc_message.cpp b/src/irc/irc_message.cpp index 14fdb0e..62fe9a7 100644 --- a/src/irc/irc_message.cpp +++ b/src/irc/irc_message.cpp @@ -1,33 +1,30 @@ #include #include -IrcMessage::IrcMessage(std::string&& line) +IrcMessage::IrcMessage(std::stringstream ss) { - std::string::size_type pos; - - // optional prefix - if (line[0] == ':') + if (ss.peek() == ':') { - pos = line.find(' '); - this->prefix = line.substr(1, pos - 1); - line = line.substr(pos + 1, std::string::npos); + ss.ignore(); + ss >> this->prefix; } - // command - pos = line.find(' '); - this->command = line.substr(0, pos); - line = line.substr(pos + 1, std::string::npos); - // arguments - do + ss >> this->command; + while (ss >> std::ws) { - if (line[0] == ':') + std::string arg; + if (ss.peek() == ':') + { + ss.ignore(); + std::getline(ss, arg); + } + else { - this->arguments.emplace_back(line.substr(1, std::string::npos)); - break ; + ss >> arg; + if (arg.empty()) + break; } - pos = line.find(' '); - this->arguments.emplace_back(line.substr(0, pos)); - line = line.substr(pos + 1, std::string::npos); - } while (pos != std::string::npos); + this->arguments.push_back(std::move(arg)); + } } IrcMessage::IrcMessage(std::string&& prefix, diff --git a/src/irc/irc_message.hpp b/src/irc/irc_message.hpp index 269a12a..5475fd2 100644 --- a/src/irc/irc_message.hpp +++ b/src/irc/irc_message.hpp @@ -4,11 +4,13 @@ #include #include #include +#include class IrcMessage { public: - IrcMessage(std::string&& line); + IrcMessage(std::stringstream ss); + IrcMessage(std::string str): IrcMessage{std::stringstream{str}} {} IrcMessage(std::string&& prefix, std::string&& command, std::vector&& args); IrcMessage(std::string&& command, std::vector&& args); ~IrcMessage() = default; diff --git a/tests/end_to_end/scenarios/multiple_channels_join.py b/tests/end_to_end/scenarios/multiple_channels_join.py index 839909f..281a902 100644 --- a/tests/end_to_end/scenarios/multiple_channels_join.py +++ b/tests/end_to_end/scenarios/multiple_channels_join.py @@ -14,5 +14,9 @@ scenario = ( expect_self_join_presence(jid='{jid_one}/{resource_one}', chan="#foo", nick="{nick_one}"), expect_self_join_presence(jid='{jid_one}/{resource_one}', chan="#bar", nick="{nick_one}"), expect_self_join_presence(jid='{jid_one}/{resource_one}', chan="#baz", nick="{nick_one}"), + + send_stanza("Le topic"), + expect_stanza("/message"), + ) diff --git a/tests/irc.cpp b/tests/irc.cpp new file mode 100644 index 0000000..0f30f15 --- /dev/null +++ b/tests/irc.cpp @@ -0,0 +1,43 @@ +#include "catch.hpp" + +#include + +TEST_CASE("Basic IRC message parsing") +{ + IrcMessage m(":prefix COMMAND un deux trois"); + CHECK(m.prefix == "prefix"); + CHECK(m.command == "COMMAND"); + CHECK(m.arguments.size() == 3); + CHECK(m.arguments[0] == "un"); + CHECK(m.arguments[1] == "deux"); + CHECK(m.arguments[2] == "trois"); +} + +TEST_CASE("Trailing space") +{ + IrcMessage m(":prefix COMMAND un deux trois "); + CHECK(m.prefix == "prefix"); + CHECK(m.arguments.size() == 3); + CHECK(m.arguments[0] == "un"); + CHECK(m.arguments[1] == "deux"); + CHECK(m.arguments[2] == "trois"); +} + +TEST_CASE("Message with :") +{ + IrcMessage m(":prefix COMMAND un :coucou les amis "); + CHECK(m.prefix == "prefix"); + CHECK(m.arguments.size() == 2); + CHECK(m.arguments[0] == "un"); + CHECK(m.arguments[1] == "coucou les amis "); +} + +TEST_CASE("Message with empty :") +{ + IrcMessage m("COMMAND un deux :"); + CHECK(m.prefix == ""); + CHECK(m.arguments.size() == 3); + CHECK(m.arguments[0] == "un"); + CHECK(m.arguments[1] == "deux"); + CHECK(m.arguments[2] == ""); +} -- cgit v1.2.3 From b98434b5d04d1ada9b24475e17ee8947d96ad1e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?louiz=E2=80=99?= Date: Sun, 16 Aug 2020 16:05:15 +0200 Subject: In CAP messages, handle the last arg as a list of capabilities Instead of just one. This fixes the issue of the "trailing whitespace" since we now split it on ' ' Fix #3442 --- src/irc/irc_client.cpp | 25 ++++++++++++++----------- tests/utils.cpp | 3 +++ 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/irc/irc_client.cpp b/src/irc/irc_client.cpp index 3ae5ac6..5f0d9b9 100644 --- a/src/irc/irc_client.cpp +++ b/src/irc/irc_client.cpp @@ -1340,19 +1340,22 @@ long int IrcClient::get_throttle_limit() const void IrcClient::on_cap(const IrcMessage &message) { const auto& sub_command = message.arguments[1]; - const auto& cap = message.arguments[2]; - auto it = this->capabilities.find(cap); - if (it == this->capabilities.end()) + const auto& caps = utils::split(message.arguments[2], ' ', false); + for (const auto& cap: caps) { - log_warning("Received a CAP message for something we didn’t ask, or that we already handled."); - return; + auto it = this->capabilities.find(cap); + if (it == this->capabilities.end()) + { + log_warning("Received a CAP message for something we didn’t ask, or that we already handled: [", cap, "]"); + return; + } + Capability& capability = it->second; + if (sub_command == "ACK") + capability.on_ack(); + else if (sub_command == "NACK") + capability.on_nack(); + this->capabilities.erase(it); } - Capability& capability = it->second; - if (sub_command == "ACK") - capability.on_ack(); - else if (sub_command == "NACK") - capability.on_nack(); - this->capabilities.erase(it); if (this->capabilities.empty()) this->cap_end(); } diff --git a/tests/utils.cpp b/tests/utils.cpp index 6de19f0..6151733 100644 --- a/tests/utils.cpp +++ b/tests/utils.cpp @@ -28,6 +28,9 @@ TEST_CASE("String split") CHECK(splitted.size() == 2); CHECK(splitted[0] == ""); CHECK(splitted[1] == "a"); + splitted = utils::split("multi-prefix ", ' '); + CHECK(splitted[0] == "multi-prefix"); + CHECK(splitted.size() == 1); } TEST_CASE("tolower") -- cgit v1.2.3