redundant checks
This commit is contained in:
parent
0aab173d94
commit
efb49b8708
144
self_thread.cpp
144
self_thread.cpp
|
@ -6,102 +6,54 @@
|
|||
#include "ircmsg.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);
|
||||
|
||||
connection.add_listener<IrcMsgEvent>([thread](IrcMsgEvent& event)
|
||||
{
|
||||
switch (event.command)
|
||||
{
|
||||
// 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
|
||||
case IrcCommand::NICK:
|
||||
{
|
||||
if (event.irc.args.size() < 1) {
|
||||
BOOST_LOG_TRIVIAL(debug) << "NICK has too few arguments";
|
||||
break;
|
||||
nickname_ = irc.args[0];
|
||||
}
|
||||
|
||||
if (thread->is_my_mask(event.irc.source))
|
||||
auto SelfThread::on_nick(IrcMsg const& irc) -> void
|
||||
{
|
||||
thread->nickname_ = event.irc.args[0];
|
||||
if (is_my_mask(irc.source))
|
||||
{
|
||||
nickname_ = irc.args[0];
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// Re-establish user modes
|
||||
case IrcCommand::RPL_UMODEIS:
|
||||
if (event.irc.args.size() < 1)
|
||||
auto SelfThread::on_umodeis(IrcMsg const& irc) -> void
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(debug) << "RPL_UMODEIS has too few arguments";
|
||||
break;
|
||||
mode_ = irc.args[1];
|
||||
}
|
||||
|
||||
thread->mode_ = event.irc.args[1];
|
||||
break;
|
||||
|
||||
case IrcCommand::JOIN: {
|
||||
if (event.irc.args.size() < 1)
|
||||
auto SelfThread::on_join(IrcMsg const& irc) -> void
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(debug) << "JOIN has too few arguments";
|
||||
break;
|
||||
if (is_my_mask(irc.source))
|
||||
{
|
||||
channels_.insert(std::string{irc.args[0]});
|
||||
}
|
||||
}
|
||||
|
||||
if (thread->is_my_mask(event.irc.source))
|
||||
auto SelfThread::on_kick(IrcMsg const& irc) -> void
|
||||
{
|
||||
thread->channels_.insert(std::string{event.irc.args[0]});
|
||||
}
|
||||
break;
|
||||
}
|
||||
case IrcCommand::KICK: {
|
||||
if (event.irc.args.size() < 2)
|
||||
if (is_my_nick(irc.args[1]))
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(debug) << "PART has too few arguments";
|
||||
break;
|
||||
channels_.erase(std::string{irc.args[0]});
|
||||
}
|
||||
}
|
||||
|
||||
if (thread->is_my_nick(event.irc.args[1]))
|
||||
auto SelfThread::on_part(IrcMsg const& irc) -> void
|
||||
{
|
||||
thread->channels_.erase(std::string{event.irc.args[0]});
|
||||
}
|
||||
break;
|
||||
}
|
||||
case IrcCommand::PART: {
|
||||
if (event.irc.args.size() < 1)
|
||||
if (is_my_mask(irc.source))
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(debug) << "PART has too few arguments";
|
||||
break;
|
||||
channels_.erase(std::string{irc.args[0]});
|
||||
}
|
||||
}
|
||||
|
||||
if (thread->is_my_mask(event.irc.source))
|
||||
auto SelfThread::on_mode(IrcMsg const& irc) -> void
|
||||
{
|
||||
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]))
|
||||
if (is_my_nick(irc.args[0]))
|
||||
{
|
||||
auto polarity = true;
|
||||
for (char const c : event.irc.args[1])
|
||||
for (char const c : irc.args[1])
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
|
@ -114,20 +66,62 @@ auto SelfThread::start(Connection& connection) -> std::shared_ptr<SelfThread>
|
|||
default:
|
||||
if (polarity)
|
||||
{
|
||||
thread->mode_ += c;
|
||||
mode_ += c;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto const ix = thread->mode_.find(c);
|
||||
auto const ix = mode_.find(c);
|
||||
if (ix != std::string::npos)
|
||||
{
|
||||
thread->mode_.erase(ix, 1);
|
||||
mode_.erase(ix, 1);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -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