From d1793df1eda463b10107d41785ad1d7f055ed476 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sat, 18 May 2024 14:48:12 +0500 Subject: upd: moved the bot part to a relative subfolder --- bot/src/handlers.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 bot/src/handlers.cpp (limited to 'bot/src/handlers.cpp') diff --git a/bot/src/handlers.cpp b/bot/src/handlers.cpp new file mode 100644 index 0000000..c7820b4 --- /dev/null +++ b/bot/src/handlers.cpp @@ -0,0 +1,85 @@ +#include "handlers.hpp" + +#include +#include +#include +#include +#include + +#include "bundle.hpp" +#include "commands/command.hpp" +#include "commands/request.hpp" +#include "commands/request_util.hpp" +#include "irc/message.hpp" +#include "localization/line_id.hpp" +#include "logger.hpp" +#include "utils/string.hpp" + +namespace bot::handlers { + void handle_private_message( + const InstanceBundle &bundle, + const command::CommandLoader &command_loader, + const irc::Message &message, + pqxx::connection &conn) { + if (utils::string::string_contains_sql_injection(message.message)) { + log::warn("PrivateMessageHandler", + "Received the message in #" + message.source.login + + " with SQL injection: " + message.message); + return; + } + + std::optional request = + command::generate_request(command_loader, message, conn); + + if (request.has_value()) { + try { + auto response = command_loader.run(bundle, request.value()); + + if (response.has_value()) { + try { + auto str = std::get(*response); + bundle.irc_client.say(message.source.login, str); + } catch (const std::exception &e) { + } + + try { + auto strs = std::get>(*response); + for (const std::string &str : strs) { + bundle.irc_client.say(message.source.login, str); + } + } catch (const std::exception &e) { + } + } + } catch (const std::exception &e) { + std::string line = + bundle.localization + .get_formatted_line(request.value(), loc::LineId::ErrorTemplate, + {e.what()}) + .value(); + + bundle.irc_client.say(message.source.login, line); + } + } + + pqxx::work work(conn); + pqxx::result channels = + work.exec("SELECT id FROM channels WHERE alias_id = " + + std::to_string(message.source.id)); + + if (!channels.empty()) { + int channel_id = channels[0][0].as(); + pqxx::result cmds = + work.exec("SELECT message FROM custom_commands WHERE name = '" + + message.message + "' AND channel_id = '" + + std::to_string(channel_id) + "'"); + + if (!cmds.empty()) { + std::string msg = cmds[0][0].as(); + + bundle.irc_client.say(message.source.login, msg); + } + } + + work.commit(); + } +} -- cgit v1.2.3