#include "self_thread.hpp" #include #include "connection.hpp" #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); connection.add_listener([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; } }); return thread; } auto SelfThread::get_my_nickname() const -> std::string const& { return nickname_; } auto SelfThread::get_my_mode() const -> std::string const& { return mode_; } auto SelfThread::get_my_channels() const -> std::unordered_set const& { return channels_; } auto SelfThread::is_my_nick(std::string_view nick) const -> bool { return nick == nickname_; } auto SelfThread::is_my_mask(std::string_view mask) const -> bool { auto const bang = mask.find('!'); return bang != std::string_view::npos && nickname_ == mask.substr(0, bang); }