redundant checks

This commit is contained in:
Eric Mertens 2024-03-03 12:53:59 -08:00
parent 0aab173d94
commit efb49b8708
2 changed files with 98 additions and 95 deletions

View File

@ -6,102 +6,54 @@
#include "ircmsg.hpp" #include "ircmsg.hpp"
#include "irc_parse_thread.hpp" #include "irc_parse_thread.hpp"
auto SelfThread::start(Connection& connection) -> std::shared_ptr<SelfThread> auto SelfThread::on_welcome(IrcMsg const& irc) -> void
{ {
auto thread = std::make_shared<SelfThread>(connection); nickname_ = irc.args[0];
}
connection.add_listener<IrcMsgEvent>([thread](IrcMsgEvent& event) auto SelfThread::on_nick(IrcMsg const& irc) -> void
{
if (is_my_mask(irc.source))
{ {
switch (event.command) nickname_ = irc.args[0];
{
// 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]; }
break;
// Track changes to our nickname auto SelfThread::on_umodeis(IrcMsg const& irc) -> void
case IrcCommand::NICK: {
{ mode_ = irc.args[1];
if (event.irc.args.size() < 1) { }
BOOST_LOG_TRIVIAL(debug) << "NICK has too few arguments";
break;
}
if (thread->is_my_mask(event.irc.source)) auto SelfThread::on_join(IrcMsg const& irc) -> void
{
if (is_my_mask(irc.source))
{ {
thread->nickname_ = event.irc.args[0]; channels_.insert(std::string{irc.args[0]});
}
break;
} }
}
// Re-establish user modes auto SelfThread::on_kick(IrcMsg const& irc) -> void
case IrcCommand::RPL_UMODEIS: {
if (event.irc.args.size() < 1) if (is_my_nick(irc.args[1]))
{ {
BOOST_LOG_TRIVIAL(debug) << "RPL_UMODEIS has too few arguments"; channels_.erase(std::string{irc.args[0]});
break;
} }
}
thread->mode_ = event.irc.args[1]; auto SelfThread::on_part(IrcMsg const& irc) -> void
break; {
if (is_my_mask(irc.source))
{
channels_.erase(std::string{irc.args[0]});
}
}
case IrcCommand::JOIN: { auto SelfThread::on_mode(IrcMsg const& irc) -> void
if (event.irc.args.size() < 1) {
{ if (is_my_nick(irc.args[0]))
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]});
}
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]});
}
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]});
}
break;
}
// Interpret self mode changes
case IrcCommand::MODE:
if (event.irc.args.size() < 2)
{
BOOST_LOG_TRIVIAL(debug) << "MODE has too few arguments";
break;
}
if (thread->is_my_nick(event.irc.args[0]))
{ {
auto polarity = true; auto polarity = true;
for (char const c : event.irc.args[1]) for (char const c : irc.args[1])
{ {
switch (c) switch (c)
{ {
@ -114,20 +66,62 @@ auto SelfThread::start(Connection& connection) -> std::shared_ptr<SelfThread>
default: default:
if (polarity) if (polarity)
{ {
thread->mode_ += c; mode_ += c;
} }
else else
{ {
auto const ix = thread->mode_.find(c); auto const ix = mode_.find(c);
if (ix != std::string::npos) if (ix != std::string::npos)
{ {
thread->mode_.erase(ix, 1); mode_.erase(ix, 1);
} }
} }
break; break;
} }
} }
} }
}
auto SelfThread::start(Connection& connection) -> std::shared_ptr<SelfThread>
{
auto thread = std::make_shared<SelfThread>(connection);
connection.add_listener<IrcMsgEvent>([thread](IrcMsgEvent& event)
{
switch (event.command)
{
// Learn nickname from 001
case IrcCommand::RPL_WELCOME:
thread->on_welcome(event.irc);
break;
// Track changes to our nickname
case IrcCommand::NICK:
thread->on_nick(event.irc);
break;
// Re-establish user modes
case IrcCommand::RPL_UMODEIS:
thread->on_umodeis(event.irc);
break;
case IrcCommand::JOIN:
thread->on_join(event.irc);
break;
case IrcCommand::KICK:
thread->on_kick(event.irc);
break;
case IrcCommand::PART:
thread->on_part(event.irc);
break;
// Interpret self mode changes
case IrcCommand::MODE:
thread->on_mode(event.irc);
break;
default: break; default: break;
} }
}); });

View File

@ -5,6 +5,7 @@
#include <unordered_set> #include <unordered_set>
struct Connection; struct Connection;
struct IrcMsg;
/** /**
* @brief Thread to track this connection's identity, and IRC state. * @brief Thread to track this connection's identity, and IRC state.
@ -17,6 +18,14 @@ class SelfThread
std::string mode_; std::string mode_;
std::unordered_set<std::string> channels_; 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: public:
SelfThread(Connection& connection) : connection_{connection} {} SelfThread(Connection& connection) : connection_{connection} {}
static auto start(Connection&) -> std::shared_ptr<SelfThread>; static auto start(Connection&) -> std::shared_ptr<SelfThread>;