xbot/bot.cpp

79 lines
2.0 KiB
C++
Raw Normal View History

2025-01-25 21:24:33 -08:00
#include "bot.hpp"
#include <boost/log/trivial.hpp>
2025-01-26 19:35:56 -08:00
auto Bot::start(std::shared_ptr<Client> self) -> std::shared_ptr<Bot>
2025-01-25 21:24:33 -08:00
{
const auto thread = std::make_shared<Bot>(std::move(self));
2025-01-26 19:43:17 -08:00
thread->self_->get_connection()->sig_ircmsg.connect([thread](const auto cmd, auto &msg) {
2025-01-25 21:24:33 -08:00
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;
2025-01-26 19:35:56 -08:00
message = message.substr(1); // discard the prefix
2025-01-25 21:24:33 -08:00
2025-01-26 19:35:56 -08:00
auto cmdend = message.find(' ');
2025-01-26 19:43:17 -08:00
2025-01-25 21:24:33 -08:00
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);
2025-01-26 19:35:56 -08:00
std::string_view oper;
std::string_view account;
for (auto [key, value] : msg.tags)
{
if (key == "account")
{
account = value;
}
2025-01-26 19:43:17 -08:00
else if (key == "solanum.chat/oper")
2025-01-26 19:35:56 -08:00
{
oper = value;
}
}
sig_command({
.source = msg.args[0],
.target = msg.args[1],
.oper = oper,
.account = account,
.command = command,
.arguments = arguments
});
2025-01-25 21:24:33 -08:00
}
2025-01-26 19:43:17 -08:00
auto Bot::on_ircmsg(const IrcCommand cmd, const IrcMsg &msg) -> void
2025-01-25 21:24:33 -08:00
{
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);
}
}
2025-01-26 19:35:56 -08:00
}
auto Bot::shutdown() -> void
{
sig_command.disconnect_all_slots();
}