288 lines
6.8 KiB
C++
288 lines
6.8 KiB
C++
#include "client.hpp"
|
|
|
|
#include "connection.hpp"
|
|
|
|
#include <mybase64.hpp>
|
|
|
|
#include <boost/container/flat_map.hpp>
|
|
#include <boost/log/trivial.hpp>
|
|
|
|
using namespace std::literals;
|
|
|
|
auto Client::on_welcome(const IrcMsg &irc) -> void
|
|
{
|
|
nickname_ = irc.args[0];
|
|
}
|
|
|
|
auto Client::on_registered() -> void
|
|
{
|
|
sig_registered();
|
|
sig_registered.disconnect_all_slots();
|
|
}
|
|
|
|
auto Client::on_nick(const IrcMsg &irc) -> void
|
|
{
|
|
if (is_my_mask(irc.source))
|
|
{
|
|
nickname_ = irc.args[0];
|
|
}
|
|
}
|
|
|
|
auto Client::on_umodeis(const IrcMsg &irc) -> void
|
|
{
|
|
mode_ = irc.args[1];
|
|
}
|
|
|
|
auto Client::on_join(const IrcMsg &irc) -> void
|
|
{
|
|
if (is_my_mask(irc.source))
|
|
{
|
|
channels_.insert(std::string{irc.args[0]});
|
|
}
|
|
}
|
|
|
|
auto Client::on_kick(const IrcMsg &irc) -> void
|
|
{
|
|
if (is_my_nick(irc.args[1]))
|
|
{
|
|
channels_.erase(std::string{irc.args[0]});
|
|
}
|
|
}
|
|
|
|
auto Client::on_part(const IrcMsg &irc) -> void
|
|
{
|
|
if (is_my_mask(irc.source))
|
|
{
|
|
channels_.erase(std::string{irc.args[0]});
|
|
}
|
|
}
|
|
|
|
auto Client::on_mode(const IrcMsg &irc) -> void
|
|
{
|
|
if (is_my_nick(irc.args[0]))
|
|
{
|
|
auto polarity = true;
|
|
for (const char c : irc.args[1])
|
|
{
|
|
switch (c)
|
|
{
|
|
case '+':
|
|
polarity = true;
|
|
break;
|
|
case '-':
|
|
polarity = false;
|
|
break;
|
|
default:
|
|
if (polarity)
|
|
{
|
|
mode_ += c;
|
|
}
|
|
else
|
|
{
|
|
const auto ix = mode_.find(c);
|
|
if (ix != std::string::npos)
|
|
{
|
|
mode_.erase(ix, 1);
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
auto Client::on_isupport(const IrcMsg &msg) -> void
|
|
{
|
|
const auto hi = msg.args.size() - 1;
|
|
for (int i = 1; i < hi; ++i)
|
|
{
|
|
auto &entry = msg.args[i];
|
|
|
|
// Leading minus means to stop support
|
|
if (entry.starts_with("-"))
|
|
{
|
|
const auto key = std::string{entry.substr(1)};
|
|
if (auto cursor = isupport_.find(key); cursor != isupport_.end())
|
|
{
|
|
isupport_.erase(cursor);
|
|
}
|
|
}
|
|
else if (const auto cursor = entry.find('='); cursor != entry.npos)
|
|
{
|
|
isupport_.emplace(entry.substr(0, cursor), entry.substr(cursor + 1));
|
|
}
|
|
else
|
|
{
|
|
isupport_.emplace(entry, std::string{});
|
|
}
|
|
}
|
|
}
|
|
|
|
auto Client::start(Connection &connection) -> std::shared_ptr<Client>
|
|
{
|
|
auto thread = std::make_shared<Client>(connection);
|
|
|
|
connection.sig_ircmsg.connect([thread](auto cmd, auto &msg) {
|
|
switch (cmd)
|
|
{
|
|
case IrcCommand::JOIN:
|
|
thread->on_join(msg);
|
|
break;
|
|
case IrcCommand::KICK:
|
|
thread->on_kick(msg);
|
|
break;
|
|
case IrcCommand::MODE:
|
|
thread->on_mode(msg);
|
|
break;
|
|
case IrcCommand::NICK:
|
|
thread->on_nick(msg);
|
|
break;
|
|
case IrcCommand::PART:
|
|
thread->on_part(msg);
|
|
break;
|
|
case IrcCommand::RPL_ISUPPORT:
|
|
thread->on_isupport(msg);
|
|
break;
|
|
case IrcCommand::RPL_UMODEIS:
|
|
thread->on_umodeis(msg);
|
|
break;
|
|
case IrcCommand::RPL_WELCOME:
|
|
thread->on_welcome(msg);
|
|
break;
|
|
case IrcCommand::RPL_ENDOFMOTD:
|
|
thread->on_registered();
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
});
|
|
|
|
connection.sig_authenticate.connect([thread](auto msg) {
|
|
thread->on_authenticate(msg);
|
|
});
|
|
|
|
return thread;
|
|
}
|
|
|
|
auto Client::get_my_nickname() const -> const std::string &
|
|
{
|
|
return nickname_;
|
|
}
|
|
|
|
auto Client::get_my_mode() const -> const std::string &
|
|
{
|
|
return mode_;
|
|
}
|
|
|
|
auto Client::get_my_channels() const -> const std::unordered_set<std::string> &
|
|
{
|
|
return channels_;
|
|
}
|
|
|
|
auto Client::is_my_nick(std::string_view nick) const -> bool
|
|
{
|
|
return casemap_compare(nick, nickname_) == 0;
|
|
}
|
|
|
|
auto Client::is_my_mask(std::string_view mask) const -> bool
|
|
{
|
|
const auto bang = mask.find('!');
|
|
return bang != std::string_view::npos && nickname_ == mask.substr(0, bang);
|
|
}
|
|
|
|
auto Client::is_channel(std::string_view name) const -> bool
|
|
{
|
|
return not name.empty() && channel_prefix_.find(name[0]) != channel_prefix_.npos;
|
|
}
|
|
|
|
auto Client::on_authenticate(const std::string_view body) -> void
|
|
{
|
|
if (not sasl_mechanism_)
|
|
{
|
|
BOOST_LOG_TRIVIAL(warning) << "Unexpected AUTHENTICATE from server"sv;
|
|
connection_.send_authenticate_abort();
|
|
return;
|
|
}
|
|
|
|
if (auto reply = sasl_mechanism_->step(body))
|
|
{
|
|
|
|
connection_.send_authenticate_encoded(*reply);
|
|
|
|
// Clean up completed SASL transactions
|
|
if (sasl_mechanism_->is_complete())
|
|
{
|
|
sasl_mechanism_.reset();
|
|
}
|
|
}
|
|
}
|
|
|
|
auto Client::start_sasl(std::unique_ptr<SaslMechanism> mechanism) -> void
|
|
{
|
|
if (sasl_mechanism_)
|
|
{
|
|
connection_.send_authenticate("*"sv); // abort SASL
|
|
}
|
|
|
|
sasl_mechanism_ = std::move(mechanism);
|
|
connection_.send_authenticate(sasl_mechanism_->mechanism_name());
|
|
}
|
|
|
|
static auto tolower_rfc1459(int c) -> int
|
|
{
|
|
return 97 <= c && c <= 126 ? c - 32 : c;
|
|
}
|
|
|
|
static auto tolower_rfc1459_strict(int c) -> int
|
|
{
|
|
return 97 <= c && c <= 125 ? c - 32 : c;
|
|
}
|
|
|
|
template <int lower(int)>
|
|
static auto casemap_impl(std::string_view str) -> std::string
|
|
{
|
|
std::string result;
|
|
result.reserve(str.size());
|
|
for (auto c : str) {
|
|
result.push_back(lower(c));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
auto Client::casemap(std::string_view str) const -> std::string
|
|
{
|
|
switch (casemap_) {
|
|
case Casemap::Ascii: return casemap_impl<tolower>(str);
|
|
case Casemap::Rfc1459: return casemap_impl<tolower_rfc1459>(str);
|
|
case Casemap::Rfc1459_Strict: return casemap_impl<tolower_rfc1459_strict>(str);
|
|
}
|
|
}
|
|
|
|
template <int lower(int)>
|
|
static auto casemap_compare_impl(std::string_view lhs, std::string_view rhs) -> int
|
|
{
|
|
size_t n1 = lhs.size();
|
|
size_t n2 = rhs.size();
|
|
size_t n = std::min(n1, n2);
|
|
for (size_t i = 0; i < n; ++i) {
|
|
if (lower(lhs[i]) < lower(rhs[i])) return -1;
|
|
if (lower(lhs[i]) > lower(rhs[i])) return 1;
|
|
}
|
|
if (n1 < n2) return -1;
|
|
if (n1 > n2) return 1;
|
|
return 0;
|
|
}
|
|
|
|
auto Client::casemap_compare(std::string_view lhs, std::string_view rhs) const -> int
|
|
{
|
|
switch (casemap_) {
|
|
case Casemap::Ascii: return casemap_compare_impl<tolower>(lhs, rhs);
|
|
case Casemap::Rfc1459: return casemap_compare_impl<tolower_rfc1459>(lhs, rhs);
|
|
case Casemap::Rfc1459_Strict: return casemap_compare_impl<tolower_rfc1459_strict>(lhs, rhs);
|
|
}
|
|
}
|
|
|
|
auto Client::shutdown() -> void
|
|
{
|
|
sig_registered.disconnect_all_slots();
|
|
} |