diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-18 14:48:12 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-18 14:48:12 +0500 |
| commit | d1793df1eda463b10107d41785ad1d7f055ed476 (patch) | |
| tree | fd3e41c3b4a05924748ae4b762e1ae55a0bc815c /bot/src/handlers.cpp | |
| parent | d7a2de17e9b7931f68b5b4079b1c36866a19d343 (diff) | |
upd: moved the bot part to a relative subfolder
Diffstat (limited to 'bot/src/handlers.cpp')
| -rw-r--r-- | bot/src/handlers.cpp | 85 |
1 files changed, 85 insertions, 0 deletions
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 <exception> +#include <optional> +#include <pqxx/pqxx> +#include <string> +#include <vector> + +#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<irc::MessageType::Privmsg> &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<command::Request> 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<std::string>(*response); + bundle.irc_client.say(message.source.login, str); + } catch (const std::exception &e) { + } + + try { + auto strs = std::get<std::vector<std::string>>(*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<int>(); + 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<std::string>(); + + bundle.irc_client.say(message.source.login, msg); + } + } + + work.commit(); + } +} |
