From a6337b079d5397f6d74b4b8b4f5f569be7dd132f Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Mon, 7 Apr 2025 23:00:43 +0400 Subject: feat: a command for executing remote lua scripts --- bot/src/commands/command.cpp | 1 + bot/src/modules/lua.hpp | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) (limited to 'bot') 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()); this->add_command(std::make_unique()); + this->add_command(std::make_unique()); this->luaState = std::make_shared(); 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 +#include #include +#include #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( + request, bundle.localization, command::CommandArgument::VALUE); + } + + std::string url = request.message.value(); + + std::vector 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( + 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( + 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 -- cgit v1.2.3