xbot/self_thread.cpp
2023-11-27 19:09:45 -08:00

78 lines
2.4 KiB
C++

#include "self_thread.hpp"
#include "connection.hpp"
#include "ircmsg.hpp"
#include "irc_parse_thread.hpp"
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->nickname_ = event.irc.args[0];
break;
// Track changes to our nickname
case IrcCommand::NICK:
{
auto const bang = event.irc.source.find('!');
if (bang != std::string::npos
&& thread->nickname_ == event.irc.source.substr(0, bang)
)
{
thread->nickname_ = event.irc.args[0];
}
break;
}
// Re-establish user modes
case IrcCommand::RPL_UMODEIS:
thread->mode_ = event.irc.args[1];
break;
// Interpret self mode changes
case IrcCommand::MODE:
if (2 == event.irc.args.size()
&& thread->nickname_ == 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;
}
});
return thread;
}