summaryrefslogtreecommitdiff
path: root/bot
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-04-07 23:00:43 +0400
committerilotterytea <iltsu@alright.party>2025-04-07 23:00:43 +0400
commita6337b079d5397f6d74b4b8b4f5f569be7dd132f (patch)
tree54377bc424ebb297ca88f263244e76a3370f079a /bot
parentcb2a0267da367bd1ce0559d4a1230a6e426d9e3c (diff)
feat: a command for executing remote lua scripts
Diffstat (limited to 'bot')
-rw-r--r--bot/src/commands/command.cpp1
-rw-r--r--bot/src/modules/lua.hpp45
2 files changed, 46 insertions, 0 deletions
diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp
index 4942e96..48bca09 100644
--- a/bot/src/commands/command.cpp
+++ b/bot/src/commands/command.cpp
@@ -49,6 +49,7 @@ namespace bot {
this->add_command(std::make_unique<mod::MinecraftServerCheck>());
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,
diff --git a/bot/src/modules/lua.hpp b/bot/src/modules/lua.hpp
index b60850a..1f4b665 100644
--- a/bot/src/modules/lua.hpp
+++ b/bot/src/modules/lua.hpp
@@ -1,11 +1,16 @@
#pragma once
+#include <algorithm>
+#include <optional>
#include <string>
+#include <vector>
#include "bundle.hpp"
#include "commands/command.hpp"
#include "commands/lua.hpp"
#include "commands/response_error.hpp"
+#include "cpr/api.h"
+#include "utils/string.hpp"
namespace bot::mod {
class LuaExecution : public command::Command {
@@ -25,4 +30,44 @@ namespace bot::mod {
return command::lua::run_safe_lua_script(request, bundle, script);
}
};
+
+ class LuaRemoteExecution : public command::Command {
+ std::string get_name() const override { return "luaimport"; }
+
+ int get_delay_seconds() const override { return 2; }
+
+ command::Response run(const InstanceBundle &bundle,
+ const command::Request &request) const override {
+ if (!request.message.has_value()) {
+ throw ResponseException<ResponseError::NOT_ENOUGH_ARGUMENTS>(
+ request, bundle.localization, command::CommandArgument::VALUE);
+ }
+
+ std::string url = request.message.value();
+
+ std::vector<std::string> mimeTypes = {"text/plain", "text/x-lua"};
+
+ cpr::Response response = cpr::Get(
+ cpr::Url{url},
+ cpr::Header{{"Accept", utils::string::join_vector(mimeTypes, ',')},
+ {"User-Agent", "https://github.com/ilotterytea/bot"}});
+
+ if (response.status_code != 200) {
+ throw ResponseException<ResponseError::EXTERNAL_API_ERROR>(
+ request, bundle.localization, response.status_code);
+ }
+
+ std::string contentType = response.header["Content-Type"];
+ if (!std::any_of(
+ mimeTypes.begin(), mimeTypes.end(),
+ [&contentType](const auto &x) { return x == contentType; })) {
+ throw ResponseException<ResponseError::INCORRECT_ARGUMENT>(
+ request, bundle.localization, url);
+ }
+
+ std::string script = response.text;
+
+ return command::lua::run_safe_lua_script(request, bundle, script);
+ }
+ };
} \ No newline at end of file