| 1 | /** |
| 2 | * D++ eval command example. |
| 3 | * This is dangerous and for educational use only, here be dragons! |
| 4 | */ |
| 5 | |
| 6 | #include <dpp/dpp.h> |
| 7 | #include <fmt/format.h> |
| 8 | #include <fstream> |
| 9 | #include <iostream> |
| 10 | /* We have to define this to make certain functions visible */ |
| 11 | #ifndef _GNU_SOURCE |
| 12 | #define _GNU_SOURCE |
| 13 | #endif |
| 14 | #include <link.h> |
| 15 | #include <dlfcn.h> |
| 16 | #include "eval.h" |
| 17 | |
| 18 | /* This is an example function you can expose to your eval command */ |
| 19 | int test_function() { |
| 20 | return 42; |
| 21 | } |
| 22 | |
| 23 | /* Important: This code is for UNIX-like systems only, e.g. |
| 24 | * Linux, BSD, OSX. It will NOT work on Windows! |
| 25 | * Note for OSX you'll probably have to change all references |
| 26 | * from .so to .dylib. |
| 27 | */ |
| 28 | int main() { |
| 29 | dpp::cluster bot("token", dpp::i_default_intents | dpp::i_message_content); |
| 30 | |
| 31 | bot.on_log(dpp::utility::cout_logger()); |
| 32 | |
| 33 | /* This won't work in a slash command very well yet, as there is not yet |
| 34 | * a multi-line slash command input type. |
| 35 | */ |
| 36 | bot.on_message_create([&bot](const auto & event) { |
| 37 | if (dpp::utility::utf8substr(event.msg.content, 0, 5) == "!eval") { |
| 38 | |
| 39 | /** |
| 40 | * THIS IS CRITICALLY IMPORTANT! |
| 41 | * Never EVER make an eval command that isn't restricted to a specific developer by user id. |
| 42 | * With access to this command the person who invokes it has at best full control over |
| 43 | * your bot's user account and at worst, full control over your entire network!!! |
| 44 | * Eval commands are Evil (pun intended) and could even be considered a security |
| 45 | * vulnerability. YOU HAVE BEEN WARNED! |
| 46 | */ |
| 47 | if (event.msg.author.id != dpp::snowflake(MY_DEVELOPER)) { |
| 48 | bot.message_create(dpp::message(event.msg.channel_id, "On the day i do this for you, Satan will be ice skating to work.")); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | /* We start by creating a string that contains a cpp program for a simple library. |
| 53 | * The library will contain one exported function called so_exec() that is called |
| 54 | * containing the raw C++ code to eval. |
| 55 | */ |
| 56 | std::string code = "#include <iostream>\n\ |
| 57 | #include <string>\n\ |
| 58 | #include <map>\n\ |
| 59 | #include <unordered_map>\n\ |
| 60 | #include <stdint.h>\n\ |
| 61 | #include <dpp/dpp.h>\n\ |
| 62 | #include <dpp/nlohmann/json.hpp>\n\ |
| 63 | #include <fmt/format.h>\n\ |
| 64 | #include \"eval.h\"\n\ |
| 65 | extern \"C\" void so_exec(dpp::cluster& bot, dpp::message_create_t event) {\n\ |
| 66 | " + dpp::utility::utf8substr( |
| 67 | event.msg.content, |
| 68 | 6, |
| 69 | dpp::utility::utf8len(event.msg.content) |
| 70 | ) + ";\n\ |
| 71 | return;\n\ |
| 72 | }"; |
| 73 | |
| 74 | /* Next we output this string full of C++ to a cpp file on disk. |
| 75 | * This code assumes the current directory is writeable. The file will have a |
| 76 | * unique name made from the user's id and the message id. |
| 77 | */ |
| 78 | std::string source_filename = std::to_string(event.msg.author.id) + "_" + std::to_string(event.msg.id) + ".cpp"; |
| 79 | std::fstream code_file(source_filename, std::fstream::binary | std::fstream::out); |
| 80 | if (!code_file.is_open()) { |
| 81 | bot.message_create(dpp::message(event.msg.channel_id, "Unable to create source file for `eval`")); |
| 82 | return; |
| 83 | } |
| 84 | code_file << code; |
| 85 | code_file.close(); |
| 86 | |
| 87 | /* Now to actually compile the file. We use dpp::utility::exec to |
| 88 | * invoke a compiler. This assumes you are using g++, and it is in your path. |
| 89 | */ |
| 90 | double compile_start = dpp::utility::time_f(); |
| 91 | dpp::utility::exec("g++", { |
| 92 | "-std=c++20", |
| 93 | "-shared", /* Build the output as a .so file */ |
| 94 | "-fPIC", |
| 95 | std::string("-o") + std::to_string(event.msg.author.id) + "_" + std::to_string(event.msg.id) + ".so", |
| 96 | std::to_string(event.msg.author.id) + "_" + std::to_string(event.msg.id) + ".cpp", |
| 97 | "-ldpp", |
| 98 | "-ldl" |
| 99 | }, [event, &bot, compile_start](const std::string &output) { |
| 100 | |
| 101 | /* After g++ is ran we end up inside this lambda with the output as a string */ |
| 102 | double compile_time = dpp::utility::time_f() - compile_start; |
| 103 | |
| 104 | /* Delete our cpp file, we don't need it any more */ |
| 105 | std::string del_file = std::string(getenv("PWD")) + std::to_string(event.msg.author.id) + "_" + std::to_string(event.msg.id) + ".cpp"; |
| 106 | unlink(del_file.c_str()); |
| 107 | |
| 108 | /* On successful compilation g++ outputs nothing, so any output here is error output */ |
| 109 | if (output.length()) { |
| 110 | bot.message_create(dpp::message(event.msg.channel_id, "Compile error: ```\n" + output + "\n```")); |
| 111 | } else { |
| 112 | |
| 113 | /* Now for the meat of the function. To actually load |
| 114 | * our shared object we use dlopen() to load it into the |
| 115 | * memory space of our bot. If dlopen() returns a nullptr, |
| 116 | * the shared object could not be loaded. The user probably |
| 117 | * did something odd with the symbols inside their eval. |
| 118 | */ |
| 119 | std::string dl = std::string(getenv("PWD")) + std::to_string(event.msg.author.id) + "_" + std::to_string(event.msg.id) + ".so"; |
| 120 | auto shared_object_handle = dlopen(dl.c_str(), RTLD_NOW); |
| 121 | if (!shared_object_handle) { |
| 122 | const char *dlsym_error = dlerror(); |
| 123 | bot.message_create(dpp::message(event.msg.channel_id, "Shared object load error: ```\n" + |
| 124 | std::string(dlsym_error ? dlsym_error : "Unknown error") +"\n```")); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | /* This type represents the "void so_exec()" function inside |
| 129 | * the shared object library file. |
| 130 | */ |
| 131 | using function_pointer = void(*)(dpp::cluster&, dpp::message_create_t); |
| 132 | |
| 133 | /* Attempt to find the function called so_exec() inside the |
| 134 | * library we just loaded. If we can't find it, then the user |
| 135 | * did something really strange in their eval. Also note it's |
| 136 | * important we call dlerror() here to reset it before trying |
| 137 | * to use it a second time. It's weird-ass C code and is just |
| 138 | * like that. |
| 139 | */ |
| 140 | dlerror(); |
| 141 | function_pointer exec_run = (function_pointer)dlsym(shared_object_handle, "so_exec"); |
| 142 | const char *dlsym_error = dlerror(); |
| 143 | if (dlsym_error) { |
| 144 | bot.message_create(dpp::message(event.msg.channel_id, "Shared object load error: ```\n" + std::string(dlsym_error) +"\n```")); |
| 145 | dlclose(shared_object_handle); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | /* Now we have a function pointer to our actual exec code in |
| 150 | * 'exec_run', so lets call it, and pass it a reference to |
| 151 | * the cluster, and also a copy of the message_create_t. |
| 152 | */ |
| 153 | double run_start = dpp::utility::time_f(); |
| 154 | exec_run(bot, event); |
| 155 | double run_time = dpp::utility::time_f() - run_start; |
| 156 | |
| 157 | /* When we're done with a .so file we must always dlclose() it */ |
| 158 | dlclose(shared_object_handle); |
| 159 | |
| 160 | /* We are now done with the compiled code too */ |
| 161 | unlink(dl.c_str()); |
| 162 | |
| 163 | /* Output some statistics */ |
| 164 | bot.message_create(dpp::message(event.msg.channel_id, |
| 165 | "Execution completed. Compile time: " + std::to_string(compile_time) + |
| 166 | "s, execution time " + std::to_string(run_time) + "s")); |
| 167 | } |
| 168 | }); |
| 169 | } |
| 170 | }); |
| 171 | |
| 172 | bot.start(dpp::st_wait); |
| 173 | |
| 174 | return 0; |
| 175 | } |