xbot/myirc/bot.cpp

84 lines
2.0 KiB
C++
Raw Permalink Normal View History

2025-02-01 11:04:33 -08:00
#include "myirc/bot.hpp"
2025-01-25 21:24:33 -08:00
#include <boost/log/trivial.hpp>
2025-02-01 11:04:33 -08:00
namespace myirc {
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-27 17:46:07 -08:00
2025-02-05 09:24:47 -08:00
thread->self_->sig_chat.connect([thread](auto &chat, bool) {
2025-01-27 17:46:07 -08:00
thread->on_chat(chat);
2025-01-25 21:24:33 -08:00
});
return thread;
}
2025-01-27 17:46:07 -08:00
auto Bot::process_command(std::string_view message, const Chat &chat) -> void
2025-01-25 21:24:33 -08:00
{
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;
2025-01-27 17:46:07 -08:00
for (auto [key, value] : chat.tags)
2025-01-26 19:35:56 -08:00
{
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({
2025-01-27 17:46:07 -08:00
.source = chat.source,
.target = chat.target,
2025-01-26 19:35:56 -08:00
.oper = oper,
.account = account,
.command = command,
.arguments = arguments
});
2025-01-25 21:24:33 -08:00
}
2025-01-27 17:46:07 -08:00
auto Bot::on_chat(const Chat &chat) -> void
2025-01-25 21:24:33 -08:00
{
2025-01-27 17:46:07 -08:00
if (not chat.is_notice)
2025-01-25 21:24:33 -08:00
{
2025-01-27 17:46:07 -08:00
if (self_->is_my_nick(chat.target))
{
process_command(chat.message, chat);
}
else if (self_->is_channel(chat.target))
2025-01-25 21:24:33 -08:00
{
2025-01-27 17:46:07 -08:00
const auto colon = chat.message.find(':');
if (colon == chat.message.npos) return;
if (not self_->is_my_nick(chat.message.substr(0, colon))) return;
process_command(chat.message.substr(colon + 1), chat);
2025-01-25 21:24:33 -08:00
}
}
2025-01-26 19:35:56 -08:00
}
auto Bot::shutdown() -> void
{
sig_command.disconnect_all_slots();
}
2025-02-01 11:04:33 -08:00
} // namespace myirc