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
|
#include "command.hpp"
#include <algorithm>
#include <chrono>
#include <ctime>
#include <fstream>
#include <memory>
#include <optional>
#include <pqxx/pqxx>
#include <sol/state.hpp>
#include <sol/types.hpp>
#include <stdexcept>
#include <string>
#include "../bundle.hpp"
#include "../utils/chrono.hpp"
#include "commands/lua.hpp"
#include "modules/lua.hpp"
#include "request.hpp"
#include "response.hpp"
namespace bot {
namespace command {
CommandLoader::CommandLoader() {
this->add_command(std::make_unique<mod::LuaExecution>());
this->add_command(std::make_unique<mod::LuaRemoteExecution>());
this->luaState = std::make_shared<sol::state>();
this->luaState->open_libraries(sol::lib::base, sol::lib::string,
sol::lib::table, sol::lib::math);
lua::library::add_base_libraries(this->luaState);
}
void CommandLoader::load_lua_directory(const std::string &folder_path) {
for (const auto &entry :
std::filesystem::directory_iterator(folder_path)) {
load_lua_file(entry.path());
}
}
void CommandLoader::load_lua_file(const std::string &file_path) {
std::ifstream ifs(file_path);
if (!ifs.is_open()) {
throw new std::runtime_error("Failed to open the Lua file at " +
file_path);
}
std::string content, line;
while (std::getline(ifs, line)) {
content += line + '\n';
}
ifs.close();
this->add_command(
std::make_unique<lua::LuaCommand>(this->luaState, content));
}
void CommandLoader::add_command(std::unique_ptr<Command> command) {
auto it = std::find_if(this->commands.begin(), this->commands.end(),
[&command](const auto &x) {
return command->get_name() == x->get_name();
});
if (it != this->commands.end()) {
this->commands.erase(it);
}
this->commands.push_back(std::move(command));
}
std::optional<Response> CommandLoader::run(const InstanceBundle &bundle,
const Request &request) {
lua::library::add_chat_libraries(this->luaState, request, bundle);
auto command = std::find_if(
this->commands.begin(), this->commands.end(),
[&](const auto &x) { return x->get_name() == request.command_id; });
if (command == this->commands.end()) {
return std::nullopt;
}
if ((*command)->get_permission_level() >
request.user_rights.get_level()) {
return std::nullopt;
}
pqxx::work work(request.conn);
pqxx::result action_query = work.exec(
"SELECT sent_at FROM actions WHERE user_id = " +
std::to_string(request.user.get_id()) +
" AND channel_id = " + std::to_string(request.channel.get_id()) +
" AND command = '" + request.command_id + "' ORDER BY sent_at DESC");
if (!action_query.empty()) {
auto last_sent_at = utils::chrono::string_to_time_point(
action_query[0][0].as<std::string>());
auto now = std::chrono::system_clock::now();
auto now_time_it = std::chrono::system_clock::to_time_t(now);
auto now_tm = std::gmtime(&now_time_it);
now = std::chrono::system_clock::from_time_t(std::mktime(now_tm));
auto difference = std::chrono::duration_cast<std::chrono::seconds>(
now - last_sent_at);
if (difference.count() < command->get()->get_delay_seconds()) {
return std::nullopt;
}
}
std::string arguments;
if (request.subcommand_id.has_value()) {
arguments += request.subcommand_id.value() + " ";
}
if (request.message.has_value()) {
arguments += request.message.value();
}
work.exec(
"INSERT INTO actions(user_id, channel_id, command, arguments, "
"full_message) VALUES (" +
std::to_string(request.user.get_id()) + ", " +
std::to_string(request.channel.get_id()) + ", '" +
request.command_id + "', '" + arguments + "', '" +
request.irc_message.message + "')");
work.commit();
return (*command)->run(bundle, request);
}
}
}
|