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
|
#include "handlers.hpp"
#include <algorithm>
#include <exception>
#include <optional>
#include <pqxx/pqxx>
#include <random>
#include <string>
#include <vector>
#include "bundle.hpp"
#include "commands/command.hpp"
#include "commands/request.hpp"
#include "commands/request_util.hpp"
#include "commands/response_error.hpp"
#include "constants.hpp"
#include "cpr/api.h"
#include "cpr/multipart.h"
#include "cpr/response.h"
#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 {
void handle_private_message(
const InstanceBundle &bundle, 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);
}
}
return;
}
} catch (const std::exception &e) {
bundle.irc_client.say(message.source.login, e.what());
log::error("PrivMsg/" + request->command_id, e.what());
}
}
pqxx::work work(conn);
pqxx::result channels =
work.exec("SELECT * FROM channels WHERE alias_id = " +
std::to_string(message.source.id));
if (!channels.empty()) {
schemas::Channel channel(channels[0]);
pqxx::result channel_preferences = work.exec(
"SELECT * FROM channel_preferences WHERE "
"channel_id = " +
std::to_string(channel.get_id()));
schemas::ChannelPreferences preference(channel_preferences[0]);
pqxx::result cmds =
work.exec("SELECT message FROM custom_commands WHERE name = '" +
message.message + "' AND channel_id = '" +
std::to_string(channel.get_id()) + "'");
if (!cmds.empty()) {
std::string msg = cmds[0][0].as<std::string>();
bundle.irc_client.say(message.source.login, msg);
} else {
make_markov_response(bundle, message, channel, preference);
}
}
work.commit();
}
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);
}
}
|