xbot/bot.cpp

55 lines
1.6 KiB
C++

#include "bot.hpp"
#include <boost/log/trivial.hpp>
auto Bot::start(std::shared_ptr<SelfThread> self) -> std::shared_ptr<Bot>
{
const auto thread = std::make_shared<Bot>(std::move(self));
auto &connection = *thread->self_->get_connection();
connection.sig_ircmsg.connect([thread](auto cmd, auto &msg) {
thread->on_ircmsg(cmd, msg);
});
return thread;
}
auto Bot::process_command(std::string_view message, const IrcMsg &msg) -> void
{
const auto cmdstart = message.find_first_not_of(' ');
if (cmdstart == message.npos) return;
message = message.substr(cmdstart);
if (not message.starts_with(command_prefix_)) return;
auto cmdend = message.find_first_of(' ', 1);
std::string_view command =
cmdend == message.npos ? message : message.substr(0, cmdend);
std::string_view arguments =
cmdend == message.npos ? std::string_view{} : message.substr(cmdend + 1);
BOOST_LOG_TRIVIAL(debug) << "COMMAND: " << command << " -- [" << arguments << "]";
}
auto Bot::on_ircmsg(IrcCommand cmd, const IrcMsg &msg) -> void
{
if (cmd == IrcCommand::PRIVMSG)
{
const auto target = msg.args[0];
const auto message = msg.args[1];
if (self_->is_my_nick(target))
{
process_command(message, msg);
} else if (self_->is_channel(target)) {
const auto colon = message.find(':');
if (colon == message.npos) return;
if (not self_->is_my_nick(message.substr(0, colon))) return;
process_command(message.substr(colon+1), msg);
}
}
}