redundant checks
This commit is contained in:
parent
0aab173d94
commit
efb49b8708
184
self_thread.cpp
184
self_thread.cpp
@ -6,6 +6,82 @@
|
||||
#include "ircmsg.hpp"
|
||||
#include "irc_parse_thread.hpp"
|
||||
|
||||
auto SelfThread::on_welcome(IrcMsg const& irc) -> void
|
||||
{
|
||||
nickname_ = irc.args[0];
|
||||
}
|
||||
|
||||
auto SelfThread::on_nick(IrcMsg const& irc) -> void
|
||||
{
|
||||
if (is_my_mask(irc.source))
|
||||
{
|
||||
nickname_ = irc.args[0];
|
||||
}
|
||||
}
|
||||
|
||||
auto SelfThread::on_umodeis(IrcMsg const& irc) -> void
|
||||
{
|
||||
mode_ = irc.args[1];
|
||||
}
|
||||
|
||||
auto SelfThread::on_join(IrcMsg const& irc) -> void
|
||||
{
|
||||
if (is_my_mask(irc.source))
|
||||
{
|
||||
channels_.insert(std::string{irc.args[0]});
|
||||
}
|
||||
}
|
||||
|
||||
auto SelfThread::on_kick(IrcMsg const& irc) -> void
|
||||
{
|
||||
if (is_my_nick(irc.args[1]))
|
||||
{
|
||||
channels_.erase(std::string{irc.args[0]});
|
||||
}
|
||||
}
|
||||
|
||||
auto SelfThread::on_part(IrcMsg const& irc) -> void
|
||||
{
|
||||
if (is_my_mask(irc.source))
|
||||
{
|
||||
channels_.erase(std::string{irc.args[0]});
|
||||
}
|
||||
}
|
||||
|
||||
auto SelfThread::on_mode(IrcMsg const& irc) -> void
|
||||
{
|
||||
if (is_my_nick(irc.args[0]))
|
||||
{
|
||||
auto polarity = true;
|
||||
for (char const c : irc.args[1])
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '+':
|
||||
polarity = true;
|
||||
break;
|
||||
case '-':
|
||||
polarity = false;
|
||||
break;
|
||||
default:
|
||||
if (polarity)
|
||||
{
|
||||
mode_ += c;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto const ix = mode_.find(c);
|
||||
if (ix != std::string::npos)
|
||||
{
|
||||
mode_.erase(ix, 1);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto SelfThread::start(Connection& connection) -> std::shared_ptr<SelfThread>
|
||||
{
|
||||
auto thread = std::make_shared<SelfThread>(connection);
|
||||
@ -16,118 +92,36 @@ auto SelfThread::start(Connection& connection) -> std::shared_ptr<SelfThread>
|
||||
{
|
||||
// Learn nickname from 001
|
||||
case IrcCommand::RPL_WELCOME:
|
||||
if (event.irc.args.size() < 1)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(debug) << "RPL_WELCOME has too few arguments";
|
||||
break;
|
||||
}
|
||||
thread->nickname_ = event.irc.args[0];
|
||||
thread->on_welcome(event.irc);
|
||||
break;
|
||||
|
||||
|
||||
// Track changes to our nickname
|
||||
case IrcCommand::NICK:
|
||||
{
|
||||
if (event.irc.args.size() < 1) {
|
||||
BOOST_LOG_TRIVIAL(debug) << "NICK has too few arguments";
|
||||
break;
|
||||
}
|
||||
|
||||
if (thread->is_my_mask(event.irc.source))
|
||||
{
|
||||
thread->nickname_ = event.irc.args[0];
|
||||
}
|
||||
thread->on_nick(event.irc);
|
||||
break;
|
||||
}
|
||||
|
||||
// Re-establish user modes
|
||||
case IrcCommand::RPL_UMODEIS:
|
||||
if (event.irc.args.size() < 1)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(debug) << "RPL_UMODEIS has too few arguments";
|
||||
break;
|
||||
}
|
||||
|
||||
thread->mode_ = event.irc.args[1];
|
||||
thread->on_umodeis(event.irc);
|
||||
break;
|
||||
|
||||
case IrcCommand::JOIN: {
|
||||
if (event.irc.args.size() < 1)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(debug) << "JOIN has too few arguments";
|
||||
break;
|
||||
}
|
||||
|
||||
if (thread->is_my_mask(event.irc.source))
|
||||
{
|
||||
thread->channels_.insert(std::string{event.irc.args[0]});
|
||||
}
|
||||
case IrcCommand::JOIN:
|
||||
thread->on_join(event.irc);
|
||||
break;
|
||||
}
|
||||
case IrcCommand::KICK: {
|
||||
if (event.irc.args.size() < 2)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(debug) << "PART has too few arguments";
|
||||
break;
|
||||
}
|
||||
|
||||
if (thread->is_my_nick(event.irc.args[1]))
|
||||
{
|
||||
thread->channels_.erase(std::string{event.irc.args[0]});
|
||||
}
|
||||
case IrcCommand::KICK:
|
||||
thread->on_kick(event.irc);
|
||||
break;
|
||||
}
|
||||
case IrcCommand::PART: {
|
||||
if (event.irc.args.size() < 1)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(debug) << "PART has too few arguments";
|
||||
break;
|
||||
}
|
||||
|
||||
if (thread->is_my_mask(event.irc.source))
|
||||
{
|
||||
thread->channels_.erase(std::string{event.irc.args[0]});
|
||||
}
|
||||
case IrcCommand::PART:
|
||||
thread->on_part(event.irc);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// Interpret self mode changes
|
||||
case IrcCommand::MODE:
|
||||
if (event.irc.args.size() < 2)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(debug) << "MODE has too few arguments";
|
||||
break;
|
||||
}
|
||||
thread->on_mode(event.irc);
|
||||
break;
|
||||
|
||||
if (thread->is_my_nick(event.irc.args[0]))
|
||||
{
|
||||
auto polarity = true;
|
||||
for (char const c : event.irc.args[1])
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '+':
|
||||
polarity = true;
|
||||
break;
|
||||
case '-':
|
||||
polarity = false;
|
||||
break;
|
||||
default:
|
||||
if (polarity)
|
||||
{
|
||||
thread->mode_ += c;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto const ix = thread->mode_.find(c);
|
||||
if (ix != std::string::npos)
|
||||
{
|
||||
thread->mode_.erase(ix, 1);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
default: break;
|
||||
}
|
||||
});
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <unordered_set>
|
||||
|
||||
struct Connection;
|
||||
struct IrcMsg;
|
||||
|
||||
/**
|
||||
* @brief Thread to track this connection's identity, and IRC state.
|
||||
@ -17,6 +18,14 @@ class SelfThread
|
||||
std::string mode_;
|
||||
std::unordered_set<std::string> channels_;
|
||||
|
||||
auto on_welcome(IrcMsg const& irc) -> void;
|
||||
auto on_nick(IrcMsg const& irc) -> void;
|
||||
auto on_umodeis(IrcMsg const& irc) -> void;
|
||||
auto on_join(IrcMsg const& irc) -> void;
|
||||
auto on_kick(IrcMsg const& irc) -> void;
|
||||
auto on_part(IrcMsg const& irc) -> void;
|
||||
auto on_mode(IrcMsg const& irc) -> void;
|
||||
|
||||
public:
|
||||
SelfThread(Connection& connection) : connection_{connection} {}
|
||||
static auto start(Connection&) -> std::shared_ptr<SelfThread>;
|
||||
|
Loading…
Reference in New Issue
Block a user