diff --git a/self_thread.cpp b/self_thread.cpp index 62af94e..2a23a7d 100644 --- a/self_thread.cpp +++ b/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 { auto thread = std::make_shared(connection); @@ -16,118 +92,36 @@ auto SelfThread::start(Connection& connection) -> std::shared_ptr { // 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; } }); diff --git a/self_thread.hpp b/self_thread.hpp index 88d7158..e27394c 100644 --- a/self_thread.hpp +++ b/self_thread.hpp @@ -5,6 +5,7 @@ #include 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 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;