summaryrefslogtreecommitdiff
path: root/bot/src/handlers.cpp
blob: e7646523a07bb46289247330ed48d0c1932eb400 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#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()) {
          if (response->is_single()) {
            bundle.irc_client.say(message.source.login, response->get_single());
          } else if (response->is_multiple()) {
            for (const std::string &msg : response->get_multiple()) {
              bundle.irc_client.say(message.source.login, msg);
            }
          }
        }
      } 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();
  }
}