Merge branch 'v9'

This commit is contained in:
louiz’
2020-08-16 16:09:13 +02:00
6 changed files with 85 additions and 33 deletions
+5 -2
View File
@@ -1340,11 +1340,13 @@ long int IrcClient::get_throttle_limit() const
void IrcClient::on_cap(const IrcMessage &message) void IrcClient::on_cap(const IrcMessage &message)
{ {
const auto& sub_command = message.arguments[1]; const auto& sub_command = message.arguments[1];
const auto& cap = message.arguments[2]; const auto& caps = utils::split(message.arguments[2], ' ', false);
for (const auto& cap: caps)
{
auto it = this->capabilities.find(cap); auto it = this->capabilities.find(cap);
if (it == this->capabilities.end()) if (it == this->capabilities.end())
{ {
log_warning("Received a CAP message for something we didn’t ask, or that we already handled."); log_warning("Received a CAP message for something we didn’t ask, or that we already handled: [", cap, "]");
return; return;
} }
Capability& capability = it->second; Capability& capability = it->second;
@@ -1353,6 +1355,7 @@ void IrcClient::on_cap(const IrcMessage &message)
else if (sub_command == "NACK") else if (sub_command == "NACK")
capability.on_nack(); capability.on_nack();
this->capabilities.erase(it); this->capabilities.erase(it);
}
if (this->capabilities.empty()) if (this->capabilities.empty())
this->cap_end(); this->cap_end();
} }
+18 -21
View File
@@ -1,33 +1,30 @@
#include <irc/irc_message.hpp> #include <irc/irc_message.hpp>
#include <iostream> #include <iostream>
IrcMessage::IrcMessage(std::string&& line) IrcMessage::IrcMessage(std::stringstream ss)
{ {
std::string::size_type pos; if (ss.peek() == ':')
// optional prefix
if (line[0] == ':')
{ {
pos = line.find(' '); ss.ignore();
this->prefix = line.substr(1, pos - 1); ss >> this->prefix;
line = line.substr(pos + 1, std::string::npos);
} }
// command ss >> this->command;
pos = line.find(' '); while (ss >> std::ws)
this->command = line.substr(0, pos);
line = line.substr(pos + 1, std::string::npos);
// arguments
do
{ {
if (line[0] == ':') std::string arg;
if (ss.peek() == ':')
{ {
this->arguments.emplace_back(line.substr(1, std::string::npos)); ss.ignore();
break ; std::getline(ss, arg);
}
else
{
ss >> arg;
if (arg.empty())
break;
}
this->arguments.push_back(std::move(arg));
} }
pos = line.find(' ');
this->arguments.emplace_back(line.substr(0, pos));
line = line.substr(pos + 1, std::string::npos);
} while (pos != std::string::npos);
} }
IrcMessage::IrcMessage(std::string&& prefix, IrcMessage::IrcMessage(std::string&& prefix,
+3 -1
View File
@@ -4,11 +4,13 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <ostream> #include <ostream>
#include <sstream>
class IrcMessage class IrcMessage
{ {
public: 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<std::string>&& args); IrcMessage(std::string&& prefix, std::string&& command, std::vector<std::string>&& args);
IrcMessage(std::string&& command, std::vector<std::string>&& args); IrcMessage(std::string&& command, std::vector<std::string>&& args);
~IrcMessage() = default; ~IrcMessage() = default;
@@ -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="#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="#bar", nick="{nick_one}"),
expect_self_join_presence(jid='{jid_one}/{resource_one}', chan="#baz", nick="{nick_one}"), expect_self_join_presence(jid='{jid_one}/{resource_one}', chan="#baz", nick="{nick_one}"),
send_stanza("<message from='{jid_one}/{resource_one}' to='#foo%{irc_server_one}' type='groupchat'><subject>Le topic</subject></message>"),
expect_stanza("/message"),
) )
+43
View File
@@ -0,0 +1,43 @@
#include "catch.hpp"
#include <irc/irc_message.hpp>
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] == "");
}
+3
View File
@@ -28,6 +28,9 @@ TEST_CASE("String split")
CHECK(splitted.size() == 2); CHECK(splitted.size() == 2);
CHECK(splitted[0] == ""); CHECK(splitted[0] == "");
CHECK(splitted[1] == "a"); CHECK(splitted[1] == "a");
splitted = utils::split("multi-prefix ", ' ');
CHECK(splitted[0] == "multi-prefix");
CHECK(splitted.size() == 1);
} }
TEST_CASE("tolower") TEST_CASE("tolower")