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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
#include "handlers.hpp"
#include <algorithm>
#include <exception>
#include <memory>
#include <optional>
#include <random>
#include <regex>
#include <string>
#include <vector>
#include "bundle.hpp"
#include "commands/command.hpp"
#include "commands/request.hpp"
#include "commands/response.hpp"
#include "commands/response_error.hpp"
#include "constants.hpp"
#include "cpr/api.h"
#include "cpr/multipart.h"
#include "cpr/response.h"
#include "database.hpp"
#include "irc/message.hpp"
#include "localization/line_id.hpp"
#include "logger.hpp"
#include "nlohmann/json.hpp"
#include "schemas/channel.hpp"
#include "utils/string.hpp"
namespace bot::handlers {
std::optional<command::Response> run_command(
const InstanceBundle &bundle, command::CommandLoader &command_loader,
const irc::Message<irc::MessageType::Privmsg> &message,
const command::Requester &requester,
std::unique_ptr<db::BaseDatabase> &conn) {
std::optional<command::Request> request =
command::generate_request(command_loader, message, requester, conn);
if (request.has_value()) {
try {
auto response = command_loader.run(bundle, request.value());
return response;
} catch (const std::exception &e) {
bundle.irc_client.say(message.source.login, e.what());
log::error("PrivMsg/" + request->command_id, e.what());
}
}
return std::nullopt;
}
std::optional<std::string> handle_custom_commands(
const InstanceBundle &bundle, command::CommandLoader &command_loader,
std::unique_ptr<db::BaseDatabase> &conn,
const command::Requester &requester,
const irc::Message<irc::MessageType::Privmsg> &message) {
std::vector<std::string> parts =
utils::string::split_text(message.message, ' ');
if (parts.empty()) {
return std::nullopt;
}
std::string cid = parts[0];
std::string cid_without_prefix =
"{prefix}" +
cid.substr(requester.channel_preferences.get_prefix().size(),
cid.size());
db::DatabaseRows cmds = conn->exec(
"SELECT name, message FROM custom_commands WHERE (name = $1 OR name "
"LIKE $2) "
"AND (channel_id "
"= $3 OR is_global = TRUE)",
{cid, cid_without_prefix, std::to_string(requester.channel.get_id())});
if (cmds.empty()) {
return std::nullopt;
}
db::DatabaseRow cmd = cmds[0];
std::string cmd_name = cmd.at("name");
if (cmd_name.length() > 8 && cmd_name.substr(0, 8) == "{prefix}" &&
cid.substr(0, requester.channel_preferences.get_prefix().size()) !=
requester.channel_preferences.get_prefix()) {
return std::nullopt;
}
parts.erase(parts.begin());
std::string initial_message = utils::string::join_vector(parts, ' ');
std::string msg = cmd.at("message");
// parsing values
std::regex pattern(R"(\{([^}]*)\})");
std::string output;
std::sregex_iterator iter(msg.begin(), msg.end(), pattern);
std::sregex_iterator end;
int last_pos = 0;
for (; iter != end; ++iter) {
auto m = *iter;
output += msg.substr(last_pos, m.position() - last_pos);
std::string inside = m[1].str();
int placeholder_pos = inside.find("$1");
if (placeholder_pos != std::string::npos) {
inside.replace(placeholder_pos, 3, initial_message);
}
irc::Message<irc::MessageType::Privmsg> msg2 = message;
msg2.message = requester.channel_preferences.get_prefix() + inside;
std::optional<command::Response> response =
run_command(bundle, command_loader, msg2, requester, conn);
std::string answer = inside;
if (response.has_value()) {
if (response->is_single()) {
answer = response->get_single();
} else if (response->is_multiple()) {
answer = "[multiple strings]";
}
}
output += answer;
last_pos = m.position() + m.length();
}
output += msg.substr(last_pos);
return output;
}
void handle_private_message(
const InstanceBundle &bundle, command::CommandLoader &command_loader,
const irc::Message<irc::MessageType::Privmsg> &message) {
std::unique_ptr<db::BaseDatabase> conn =
db::create_connection(bundle.configuration);
std::optional<command::Requester> requester =
command::get_requester(message, conn, bundle.configuration);
if (!requester.has_value()) {
return;
}
std::optional<command::Response> response =
run_command(bundle, command_loader, message, *requester, conn);
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);
}
}
return;
}
std::optional<std::string> custom_command_response = handle_custom_commands(
bundle, command_loader, conn, *requester, message);
if (custom_command_response.has_value()) {
bundle.irc_client.say(message.source.login, *custom_command_response);
return;
}
conn->close();
}
void make_markov_response(
const InstanceBundle &bundle,
const irc::Message<irc::MessageType::Privmsg> &message,
const schemas::Channel &channel,
const schemas::ChannelPreferences &preference) {
bool are_markov_responses_enabled =
std::any_of(preference.get_features().begin(),
preference.get_features().end(), [](const int &x) {
return (schemas::ChannelFeature)x ==
schemas::ChannelFeature::MARKOV_RESPONSES;
});
if (!are_markov_responses_enabled) return;
bool are_random_markov_responses_enabled =
std::any_of(preference.get_features().begin(),
preference.get_features().end(), [](const int &x) {
return (schemas::ChannelFeature)x ==
schemas::ChannelFeature::RANDOM_MARKOV_RESPONSES;
});
std::string prefix = "@" + bundle.irc_client.get_bot_username() + ",";
bool intended_call = message.message.substr(0, prefix.length()) == prefix;
int random = -1;
std::string question;
if (are_random_markov_responses_enabled && !intended_call) {
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist(0, 100);
random = dist(rng);
if (random > MARKOV_RESPONSE_CHANCE) return;
question = message.message;
} else {
question =
message.message.substr(prefix.length(), message.message.length());
}
if (random == -1 && !intended_call) return;
cpr::Response response =
cpr::Post(cpr::Url{"https://markov.ilotterytea.kz/api/v1/generate"},
cpr::Multipart{{"question", question}, {"max_length", 200}});
if (response.status_code != 200) return;
nlohmann::json j = nlohmann::json::parse(response.text);
std::string answer;
auto answer_field = j["data"]["answer"];
if (answer_field.is_null())
answer = "...";
else
answer = answer_field;
bundle.irc_client.say(message.source.login,
message.sender.login + ": " + answer);
}
}
|