summaryrefslogtreecommitdiff
path: root/src/commands/request_util.cpp
blob: 90750e5de090d864bc069cd56652847f449793c3 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "request_util.hpp"

#include <algorithm>
#include <optional>
#include <pqxx/pqxx>
#include <string>

#include "../constants.hpp"
#include "../irc/message.hpp"
#include "../schemas/channel.hpp"
#include "command.hpp"
#include "request.hpp"

namespace bot::command {
  std::optional<Request> generate_request(
      const command::CommandLoader &command_loader,
      const irc::Message<irc::MessageType::Privmsg> &irc_message,
      pqxx::connection &conn) {
    pqxx::work *work;

    work = new pqxx::work(conn);

    std::vector<std::string> parts =
        utils::string::split_text(irc_message.message, ' ');

    std::string command_id = parts[0];

    if (command_id.substr(0, DEFAULT_PREFIX.length()) != DEFAULT_PREFIX) {
      delete work;
      return std::nullopt;
    }

    command_id =
        command_id.substr(DEFAULT_PREFIX.length(), command_id.length());

    auto cmd = std::find_if(
        command_loader.get_commands().begin(),
        command_loader.get_commands().end(),
        [&](const auto &command) { return command->get_name() == command_id; });

    if (cmd == command_loader.get_commands().end()) {
      delete work;
      return std::nullopt;
    }

    parts.erase(parts.begin());

    pqxx::result query = work->exec("SELECT * FROM channels WHERE alias_id = " +
                                    std::to_string(irc_message.source.id));

    // Create new channel data in the database if it didn't exist b4
    if (query.empty()) {
      work->exec("INSERT INTO channels (alias_id, alias_name) VALUES (" +
                 std::to_string(irc_message.source.id) + ", '" +
                 irc_message.source.login + "')");

      work->commit();

      delete work;
      work = new pqxx::work(conn);

      query = work->exec("SELECT * FROM channels WHERE alias_id = " +
                         std::to_string(irc_message.source.id));
    }

    schemas::Channel channel(query[0]);

    if (channel.get_opted_out_at().has_value()) {
      delete work;
      return std::nullopt;
    }

    query = work->exec("SELECT * FROM channel_preferences WHERE channel_id = " +
                       std::to_string(channel.get_id()));

    // Create new channel preference data in the database if it didn't exist b4
    if (query.empty()) {
      work->exec(
          "INSERT INTO channel_preferences (channel_id, prefix, locale) VALUES "
          "(" +
          std::to_string(channel.get_id()) + ", '" + DEFAULT_PREFIX + "', '" +
          DEFAULT_LOCALE_ID + "')");

      work->commit();

      delete work;
      work = new pqxx::work(conn);

      query =
          work->exec("SELECT * FROM channel_preferences WHERE channel_id = " +
                     std::to_string(channel.get_id()));
    }

    schemas::ChannelPreferences channel_preferences(query[0]);

    query = work->exec("SELECT * FROM users WHERE alias_id = " +
                       std::to_string(irc_message.sender.id));

    // Create new user data in the database if it didn't exist before
    if (query.empty()) {
      work->exec("INSERT INTO users (alias_id, alias_name) VALUES (" +
                 std::to_string(irc_message.sender.id) + ", '" +
                 irc_message.sender.login + "')");

      work->commit();

      delete work;
      work = new pqxx::work(conn);

      query = work->exec("SELECT * FROM users WHERE alias_id = " +
                         std::to_string(irc_message.sender.id));
    }

    schemas::User user(query[0]);

    if (user.get_alias_name() != irc_message.sender.login) {
      work->exec("UPDATE users SET alias_name = '" + irc_message.sender.login +
                 "' WHERE id = " + std::to_string(user.get_id()));
      work->commit();

      delete work;
      work = new pqxx::work(conn);

      user.set_alias_name(irc_message.sender.login);
    }

    schemas::PermissionLevel level = schemas::PermissionLevel::USER;
    const auto &badges = irc_message.sender.badges;

    if (user.get_alias_id() == channel.get_alias_id()) {
      level = schemas::PermissionLevel::BROADCASTER;
    } else if (std::any_of(badges.begin(), badges.end(), [&](const auto &x) {
                 return x.first == "moderator";
               })) {
      level = schemas::PermissionLevel::MODERATOR;
    } else if (std::any_of(badges.begin(), badges.end(),
                           [&](const auto &x) { return x.first == "vip"; })) {
      level = schemas::PermissionLevel::VIP;
    }

    query = work->exec("SELECT * FROM user_rights WHERE user_id = " +
                       std::to_string(user.get_id()) +
                       " AND channel_id = " + std::to_string(channel.get_id()));

    if (query.empty()) {
      work->exec(
          "INSERT INTO user_rights (user_id, channel_id, level) VALUES (" +
          std::to_string(user.get_id()) + ", " +
          std::to_string(channel.get_id()) + ", " + std::to_string(level) +
          ")");

      work->commit();

      delete work;
      work = new pqxx::work(conn);

      query = work->exec("SELECT * FROM user_rights WHERE user_id = " +
                         std::to_string(user.get_id()) + " AND channel_id = " +
                         std::to_string(channel.get_id()));
    }

    schemas::UserRights user_rights(query[0]);

    if (user_rights.get_level() != level) {
      work->exec("UPDATE user_rights SET level = " + std::to_string(level) +
                 " WHERE id = " + std::to_string(query[0][0].as<int>()));

      work->commit();

      user_rights.set_level(level);
    }

    delete work;

    if (parts.empty()) {
      Request req{command_id,  std::nullopt, std::nullopt,
                  irc_message, channel,      channel_preferences,
                  user,        user_rights,  conn};

      return req;
    }

    std::optional<std::string> subcommand_id = parts[0];
    auto subcommand_ids = (*cmd)->get_subcommand_ids();

    if (std::any_of(
            subcommand_ids.begin(), subcommand_ids.end(),
            [&](const auto &x) { return x == subcommand_id.value(); })) {
      parts.erase(parts.begin());
    } else {
      subcommand_id = std::nullopt;
    }

    std::optional<std::string> message = utils::string::join_vector(parts, ' ');

    if (message->empty()) {
      message = std::nullopt;
    }

    Request req{command_id,  subcommand_id, message,
                irc_message, channel,       channel_preferences,
                user,        user_rights,   conn};
    return req;
  }
}