aboutsummaryrefslogtreecommitdiff
path: root/modules/mmanager/mmanager.cpp
blob: f9ca70434da35d47377eccce61a14c033844e576 (plain) (blame)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
 * =====================================================================================
 *
 *       Filename:  mmanager.cpp
 *
 *    Description:
 *
 *        Version:  1.0
 *        Created:  03/30/2023 11:30:55 AM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Cara Salter (muirrum), cara@devcara.com
 *   Organization:  Worcester Polytechnic Institute
 *
 * =====================================================================================
 */
#include <dpp/message.h>
#include <stdlib.h> 
#include <string>
#include <142bot/modules.hpp>
#include <142bot/util.hpp>
#include <pcre.h>
#include <dpp/dpp.h>
#include <fmt/format.h>
using namespace fmt;

class MManagerModule : public Module
{
    double microseconds_ping;

public:
    MManagerModule(Bot *creator, ModuleLoader *ml) : Module(creator, ml)
    {
        ml->attach({I_OnMessage, I_OnReady}, this);
        creator->core->log(dpp::ll_info, "ModuleManager online!");
    }

    virtual std::string version()
    {
        return "0.1.0";
    }

    virtual std::string description()
    {
        return "module manager";
    }

    virtual bool OnReady(const dpp::ready_t &ready)
    {
        bot->core->log(dpp::ll_info, "Got ready event");
        return true;
    }

    virtual bool OnMessage(const dpp::message_create_t &message, const std::string &clean_message, bool mentioned, const std::vector<std::string> &stringmentions)
    {
       dpp::message msg = message.msg;
        sentry_set_tag("module", "mmanager");
        std::vector<std::string> param;
        const char *pcre_error;
        int pcre_error_ofs;
        auto comp = pcre_compile(std::string("^sudo(\\s+(.+?))$").c_str(), PCRE_CASELESS | PCRE_MULTILINE, &pcre_error, &pcre_error_ofs, NULL);
        if (!comp)
        {
            bot->core->log(dpp::ll_error, pcre_error);
        }

        int matcharr[90];
        int matchcount = pcre_exec(comp, NULL, clean_message.c_str(), clean_message.length(), 0, 0, matcharr, 90);
        for (int i = 0; i < matchcount; ++i)
        {
            param.push_back(std::string(clean_message.c_str() + matcharr[2 * i], (size_t)(matcharr[2 * i + 1] - matcharr[2 * i])));
        }
        if (mentioned && matchcount > 0)
        {
            if (message.msg.author.id == bot->get_owner_id())
            {
                // Tokenize
                for (int i = 0; i < param.size(); i++)
                {
                    bot->core->log(dpp::ll_debug, fmt::format("{}", param[i]));
                }
                std::stringstream tokens(trim(param[2]));
                std::string subcommand;
                tokens >> subcommand;

                bot->core->log(dpp::ll_warning, fmt::format("SUDO: <{}> {}", message.msg.author.username, clean_message));
        sentry_value_t crumb = sentry_value_new_breadcrumb("user", "got sudo command");
        sentry_value_set_by_key(crumb, "level", sentry_value_new_string("warning"));
        sentry_value_set_by_key(crumb, "category", sentry_value_new_string("sudo"));
        sentry_value_set_by_key(crumb, "data", sentry_value_new_string(subcommand.c_str()));
        sentry_add_breadcrumb(crumb);

                if (lowercase(subcommand) == "modules")
                {
                    std::stringstream s;

                    // NOTE: GetModuleList's reference is safe from within a module event
                    const ModuleMap &modlist = bot->loader->get_loaded_modules();

                    s << "```diff" << std::endl;
                    s << fmt::format("╭─────────────────────────┬───────────┬────────────────────────────────────────────────╮") << std::endl;
                    s << fmt::format("│ Filename                | Version   | Description                                    |") << std::endl;
                    s << fmt::format("├─────────────────────────┼───────────┼────────────────────────────────────────────────┤") << std::endl;

                    for (auto mod = modlist.begin(); mod != modlist.end(); ++mod)
                    {
                        s << fmt::format("│ {:23} | {:9} | {:46} |", mod->first, mod->second->version(), mod->second->description()) << std::endl;
                    }
                    s << fmt::format("╰─────────────────────────┴───────────┴────────────────────────────────────────────────╯") << std::endl;
                    s << "```";

                    dpp::channel *c = dpp::find_channel(message.msg.channel_id);
                    if (c)
                    {
                        bot->core->message_create(dpp::message(c->id, s.str()));
                    }
                }
                else if (lowercase(subcommand) == "load")
                {
                    std::string modfile;
                    tokens >> modfile;
                    if (bot->loader->load(modfile))
                    {
                        EmbedSimple("Loaded module: " + modfile, message.msg.channel_id);
                    }
                    else
                    {
                        EmbedSimple(std::string("Can't do that, check server logs"), message.msg.channel_id);
                    }
                }
                else if (lowercase(subcommand) == "unload")
                {
                    std::string modfile;
                    tokens >> modfile;
                    if (modfile == "module_mmanager.so")
                    {
                        EmbedSimple("That's the module manager, are you sure about that chief?", message.msg.channel_id);
                    }

                    if (bot->loader->unload(modfile))
                    {
                        EmbedSimple("Unloaded module: " + modfile, message.msg.channel_id);
                    }
                    else
                    {
                        EmbedSimple("Can't do that, check server logs.", message.msg.channel_id);
                    }
                }
                else if (lowercase(subcommand) == "reload")
                {
                    std::string modfile;
                    tokens >> modfile;
                    if (modfile == "module_mmanager.so")
                    {
                        EmbedSimple("That's the module manager, are you sure about that chief?", message.msg.channel_id);
                        ::sleep(500);
                    }

                    if (bot->loader->reload(modfile))
                    {
                        EmbedSimple("Reloaded module: " + modfile, message.msg.channel_id);
                    }
                    else
                    {
                        EmbedSimple("Can't do that, check server logs", message.msg.channel_id);
                    }
                }
                else if (lowercase(subcommand) == "ping")
                {
                    dpp::channel *c = dpp::find_channel(message.msg.channel_id);
                    if (c)
                    {
                        std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
                        dpp::snowflake cid = message.msg.channel_id;
                        bot->core->message_create(dpp::message(message.msg.channel_id, "Pinging..."), [cid, this, start_time](const dpp::confirmation_callback_t &state)
                                                  {
								double microseconds_ping = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - start_time).count();
								dpp::snowflake mid = (std::get<dpp::message>(state.value)).id;
								this->bot->core->message_delete(mid, cid);
								this->EmbedSimple(fmt::format("**Pong!** REST Response time: {:.3f} ms", microseconds_ping / 1000, 4), cid); });
                    }
                }
                else if (lowercase(subcommand) == "restart")
                {
                    EmbedSimple("Restarting...", message.msg.channel_id);
                    ::sleep(5);
                    exit(0);
                }
                else if (lowercase(subcommand) == "sql")
                {
                    std::string sql;
                    std::getline(tokens, sql);
                    sql = trim(sql);
                    // Create transaction
                    pqxx::work tx(bot->conn); 

                    try {
                        auto res = tx.exec(sql);
                    
                    std::stringstream w;
                 
                    for (size_t i = 0; i < res.size(); i++) {
                        bot->core->log(dpp::ll_debug, res[i][0].c_str());
                    }
                    if (res.size() == 0)
                    {
                        {
                            EmbedSimple("Successfully executed, no rows returned.", msg.channel_id);
                        }
                    }
                    else
                    {
                        w << "- " << sql << std::endl; 
                        auto check = res.begin();
                        w << "+ Rows Returned: " << res.size() << std::endl;
                        for (auto row = res.begin(); row != res.end(); ++row)
                        {
                            if (row == res.begin())
                            {
                                w << "  ╭";
                            }
                            w << "────────────────────";
                            check = row;
                            w << (++check != res.end() ? "┬" : "╮\n");
                        }
                        w << "  ";
                        for (auto row = res.begin(); row != res.end(); ++row)
                        {
                            w << fmt::format("│{:20}", row[0].c_str());
                        }
                        w << "│" << std::endl;
                        for (auto row = res.begin(); row != res.end(); ++row)
                        {
                            if (row == res.begin())
                            {
                                w << "  ├";
                            }
                            w << "────────────────────";
                            check = row;
                            w << (++check != res.end() ? "┼" : "┤\n");
                        }
                        for (auto row : res)
                        {
                            if (w.str().length() < 1900)
                            {
                                w << "  ";
                                for (auto field : row)
                                {
                                    w << fmt::format("│{:20}", field.as<std::string>().substr(0, 20));
                                }
                                w << "│" << std::endl;
                            }
                        }
                        for (auto row = res.begin(); row != res.end(); ++row)
                        {
                            if (row == res.begin())
                            {
                                w << "  ╰";
                            }
                            w << "────────────────────";
                            check = row;
                            w << (++check != res.end() ? "┴" : "╯\n");
                        }
                        dpp::channel *c = dpp::find_channel(msg.channel_id);
                        if (c)
                        {
                                bot->core->message_create(dpp::message(msg.channel_id, "```diff\n" + w.str() + "```"));
                        }
                    }
                    tx.commit();
                } catch (std::exception &e) {
                        std::string error_msg = fmt::format("Error in SQL statement: {}", e.what());
                        bot->core->log(dpp::ll_error, error_msg);
                        EmbedSimple("Error in SQL statement, check logs", msg.channel_id);
                        sentry_value_t event = sentry_value_new_event();
                        sentry_value_t exc = sentry_value_new_exception("Exception", "Custom SQL error");
                        sentry_value_set_stacktrace(exc, NULL, 5);
                        sentry_event_add_exception(event, exc);
                        sentry_capture_event(event);
                        tx.abort();
                        return false;
                    
                } 
                }
                else
                {
                    EmbedSimple("Command not found.", message.msg.channel_id);
                }
            }
            else
            {
                bot->core->log(dpp::ll_error, fmt::format("Called ModuleManager as a mortal ({})", bot->get_owner_id()));
                message.reply(dpp::message("nope"));
            }
        }

        return true;
    }
};

ENTRYPOINT(MManagerModule)