Archived
1
0
Fork 0

Added eval command

This commit is contained in:
niansa 2021-03-08 00:19:31 +01:00
parent aca3591c39
commit 80e6564a3d
3 changed files with 80 additions and 1 deletions

View file

@ -22,6 +22,7 @@ add_executable(tuxiflux
bot/modules/help.cpp
bot/modules/money.cpp
bot/modules/fun.cpp
bot/modules/eval.cpp
)
target_include_directories(tuxiflux PRIVATE
@ -34,7 +35,7 @@ find_package(PkgConfig REQUIRED)
pkg_check_modules(cdlpp REQUIRED IMPORTED_TARGET cdlpp)
target_link_libraries(${PROJECT_NAME} PUBLIC
PkgConfig::cdlpp -lcdlpp) #TODO: Why is -lcdlpp required?? I don't get it...
PkgConfig::cdlpp -lcdlpp -ldl) #TODO: Why is -lcdlpp required?? I don't get it...
cdlppdb_libs()

77
bot/modules/eval.cpp Normal file
View file

@ -0,0 +1,77 @@
#include <string>
#include <cdlpp/cdltypes.hpp>
#include <boost/process.hpp>
#include <dlhandle.hpp>
#include "permassert.hpp"
using namespace std;
using namespace CDL;
class Eval {
static void eval(CMessage msg, CChannel channel, cmdargs& args) {
// Only bot owner may use this
if (msg->author->id != env.settings["owner"]) {
return;
}
// Check args
if (args.empty()) {
return;
}
// Generate code
remove("./codef.cpp");
ofstream codef("./codef.cpp");
codef << "#include <cdlpp/cdltypes.hpp>\n"
"#include <dlhandle.hpp>\n"
"extern \"C\"\n"
"void eval_main(CDL::CMessage msg, CDL::CChannel channel) {\n"
"using namespace CDL;"
<< args[0] <<
"}";
codef.close();
// Compile code
channel->start_typing([=] (const bool) {
string libnme = "./codef_"+to_string(msg->id)+".so";
boost::process::async_system(*env.aioc, [=] (boost::system::error_code, int code) {
// Check compiler result
if (code != 0) {
channel->send(":warning: Compilation failed");
return;
}
// Run code
try {
auto evalo = new Dlhandle(libnme);
auto fnc = evalo->get<void(CMessage, CChannel)>("eval_main");
if (not fnc) {
channel->send(":warning: Failed to find main function");
delete evalo; // Delete evalo directly
} else {
fnc(msg, channel);
// Delete evalo after a minute
auto t = new boost::asio::deadline_timer(*env.aioc, boost::posix_time::minutes(1));
t->async_wait([=] (const boost::system::error_code&) {
delete t;
delete evalo;
remove(libnme.c_str());
});
}
} catch (Dlhandle::Exception&) {
channel->send(":warning: Loading failed");
}
}, "gcc -std=c++17 ./codef.cpp -shared -fPIC -l cdlpp -o "+libnme);
});
}
public:
Eval() {
using namespace CDL;
// Commands
register_command("eval", eval, 1);
}
};
static Eval eval;

View file

@ -6,6 +6,7 @@
"team": [
<team member IDs as integers>
],
"owner": <owner user ID>,
"start_money": <how much initial money the users receive; ommit to set to 0>,
"daily_money": <how much daily money the users can receive; ommit to disable daily money>
}