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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
#include <dlfcn.h>
#include <dpp/dpp.h>
#include <142bot/modules.hpp>
#include <fmt/format.h>
#include <mutex>
#include <limits.h>
#include <link.h>
const char* StringNames[I_END + 1] = {
"I_BEGIN",
"I_OnMessage",
"I_OnReady",
"I_OnChannelCreate",
"I_OnChannelDelete",
"I_OnGuildMemberAdd",
"I_OnGuildCreate",
"I_OnGuildDelete",
"I_OnPresenceUpdate",
"I_OnRestEnd",
"I_OnAllShardsReady",
"I_OnTypingStart",
"I_OnMessageUpdate",
"I_OnMessageDelete",
"I_OnMessageDeleteBulk",
"I_OnGuildUpdate",
"I_OnMessageReactionAdd",
"I_OnMessageReactionRemove",
"I_OnMessageReactionRemoveAll",
"I_OnUserUpdate",
"I_OnResumed",
"I_OnChannelUpdate",
"I_OnChannelPinsUpdate",
"I_OnGuildBanAdd",
"I_OnGuildBanRemove",
"I_OnGuildEmojisUpdate",
"I_OnGuildIntegrationsUpdate",
"I_OnGuildMemberRemove",
"I_OnGuildMemberUpdate",
"I_OnGuildMembersChunk",
"I_OnGuildRoleCreate",
"I_OnGuildRoleUpdate",
"I_OnGuildRoleDelete",
"I_OnPresenceUpdateWS",
"I_OnVoiceStateUpdate",
"I_OnVoiceServerUpdate",
"I_OnWebhooksUpdate",
"I_END"
};
ModuleLoader::ModuleLoader(Bot* creator): bot(creator) {
bot->core->log(dpp::ll_info, "Module loader init");
}
// Attach a module to an event dynamically
void ModuleLoader::attach(const std::vector<Events> &i, Module* m) {
for (auto n = i.begin(); n != i.end(); ++n) {
if (std::find(EventHandlers[*n].begin(), EventHandlers[*n].end(), m) == EventHandlers[*n].end()) {
EventHandlers[*n].push_back(m);
bot->core->log(dpp::ll_info, fmt::format("Module {} attached to event {}", m->description(), StringNames[*n]));
} else {
bot->core->log(dpp::ll_warning, fmt::format("Module {} already attached to event {}", m->description(), StringNames[*n]));
}
}
}
// Detach a module from an event
void ModuleLoader::detach(const std::vector<Events> &e, Module *m) {
for (auto n = e.begin(); n != e.end(); ++n) {
auto it = std::find(EventHandlers[*n].begin(), EventHandlers[*n].end(), m);
if (it != EventHandlers[*n].end()) {
EventHandlers[*n].erase(it);
bot->core->log(dpp::ll_info, fmt::format("Module {} detached from event {}", m->description(), StringNames[*n]));
}
}
}
// Get Loaded module list
const ModuleMap& ModuleLoader::get_loaded_modules() const {
return this->mod_map;
}
// Load a new module
bool ModuleLoader::load(const std::string &fname) {
ModuleLowLevel m;
m.dlopen_handle = nullptr;
m.init = nullptr;
m.mod = nullptr;
bot->core->log(dpp::ll_info, fmt::format("Attempting to load module {}", fname));
std::lock_guard l(this->mtx);
if (Modules.find(fname) == Modules.end()) {
char buffer[PATH_MAX + 1];
getcwd(buffer, PATH_MAX);
std::string full_path = std::string(buffer) + "/" + fname;
m.dlopen_handle = dlopen(full_path.c_str(), RTLD_NOW | RTLD_LOCAL);
if (!m.dlopen_handle) {
bot->core->log(dpp::ll_error, fmt::format("Can't load module: {}", dlerror()));
return false;
} else {
if (!get_symbol(m, "init_module")) {
bot->core->log(dpp::ll_error, fmt::format("Could not find init_module symbol"));
dlclose(m.dlopen_handle);
return false;
} else {
bot->core->log(dpp::ll_debug, "Symbol found, attempting to load module");
m.mod = m.init(bot, this);
if (!m.mod || (uint64_t)m.mod == 0xffffffffffffffff) {
bot->core->log(dpp::ll_error, "Invalid module pointer returned");
dlclose(m.dlopen_handle);
return false;
} else {
bot->core->log(dpp::ll_info, fmt::format("Loaded module {}", m.mod->description()));
Modules[fname] = m;
mod_map[fname] = m.mod;
return true;
}
}
}
} else {
bot->core->log(dpp::ll_error, "Module already loaded");
return false;
}
return true;
}
// Unloads a module
bool ModuleLoader::unload(const std::string &fname) {
std::lock_guard l(mtx);
auto m = Modules.find(fname);
if (m == Modules.end()) {
bot->core->log(dpp::ll_error, "Module not loaded");
return false;
}
ModuleLowLevel& mod = m->second;
// Remove attached events
for (int j = I_BEGIN; j != I_END; ++j) {
auto p = std::find(EventHandlers[j].begin(), EventHandlers[j].end(), mod.mod);
if (p != EventHandlers[j].end()) {
EventHandlers[j].erase(p);
bot->core->log(dpp::ll_debug, fmt::format("Removed event {} from {}", StringNames[j], fname));
}
}
Modules.erase(m);
auto v = mod_map.find(fname);
if (v != mod_map.end()) {
mod_map.erase(v);
bot->core->log(dpp::ll_debug, "Removed module from public list");
}
if (mod.mod) {
delete mod.mod;
}
if (mod.dlopen_handle) {
dlclose(mod.dlopen_handle);
bot->core->log(dpp::ll_debug, "Unloaded module");
}
return true;
}
/**
* Return a given symbol name from a shared object represented by the ModuleNative value.
*/
bool ModuleLoader::get_symbol(ModuleLowLevel &native, const char *sym_name)
{
/* Find exported symbol in shared object */
if (native.dlopen_handle) {
dlerror(); // clear value
native.init = (initfunctype*)dlsym(native.dlopen_handle, sym_name);
//printf("dlopen_handle=0x%016x, native.init=0x%016x native.err=\"%s\" dlsym=0x%016x sym_name=%s\n", native.dlopen_handle, native.init, native.err ? native.err : "<NULL>", dlsym(native.dlopen_handle, sym_name), sym_name);
} else {
bot->core->log(dpp::ll_error, "ModuleLoader::GetSymbol(): Invalid dlopen() handle");
return false;
}
return true;
}
Module::Module(Bot* instigator, ModuleLoader* ml) : bot(instigator)
{
}
Module::~Module()
{
}
std::string Module::version()
{
return "";
}
std::string Module::description()
{
return "";
}
bool Module::OnChannelCreate(const dpp::channel_create_t &channel)
{
return true;
}
bool Module::OnReady(const dpp::ready_t &ready)
{
return true;
}
bool Module::OnChannelDelete(const dpp::channel_delete_t &channel)
{
return true;
}
bool Module::OnGuildCreate(const dpp::guild_create_t &guild)
{
return true;
}
bool Module::OnGuildDelete(const dpp::guild_delete_t &guild)
{
return true;
}
bool Module::OnGuildMemberAdd(const dpp::guild_member_add_t &gma)
{
return true;
}
bool Module::OnMessage(const dpp::message_create_t &message, const std::string& clean_message, bool mentioned, const std::vector<std::string> &stringmentions)
{
return true;
}
bool Module::OnPresenceUpdate()
{
return true;
}
bool Module::OnTypingStart(const dpp::typing_start_t &obj)
{
return true;
}
bool Module::OnMessageUpdate(const dpp::message_update_t &obj)
{
return true;
}
bool Module::OnMessageDelete(const dpp::message_delete_t &obj)
{
return true;
}
bool Module::OnMessageDeleteBulk(const dpp::message_delete_bulk_t &obj)
{
return true;
}
bool Module::OnGuildUpdate(const dpp::guild_update_t &obj)
{
return true;
}
bool Module::OnMessageReactionAdd(const dpp::message_reaction_add_t &obj)
{
return true;
}
bool Module::OnMessageReactionRemove(const dpp::message_reaction_remove_t &obj)
{
return true;
}
bool Module::OnMessageReactionRemoveAll(const dpp::message_reaction_remove_all_t &obj)
{
return true;
}
bool Module::OnUserUpdate(const dpp::user_update_t &obj)
{
return true;
}
bool Module::OnResumed(const dpp::resumed_t &obj)
{
return true;
}
bool Module::OnChannelUpdate(const dpp::channel_update_t &obj)
{
return true;
}
bool Module::OnChannelPinsUpdate(const dpp::channel_pins_update_t &obj)
{
return true;
}
bool Module::OnGuildBanAdd(const dpp::guild_ban_add_t &obj)
{
return true;
}
bool Module::OnGuildBanRemove(const dpp::guild_ban_remove_t &obj)
{
return true;
}
bool Module::OnGuildEmojisUpdate(const dpp::guild_emojis_update_t &obj)
{
return true;
}
bool Module::OnGuildIntegrationsUpdate(const dpp::guild_integrations_update_t &obj)
{
return true;
}
bool Module::OnGuildMemberRemove(const dpp::guild_member_remove_t &obj)
{
return true;
}
bool Module::OnGuildMemberUpdate(const dpp::guild_member_update_t &obj)
{
return true;
}
bool Module::OnGuildMembersChunk(const dpp::guild_members_chunk_t &obj)
{
return true;
}
bool Module::OnGuildRoleCreate(const dpp::guild_role_create_t &obj)
{
return true;
}
bool Module::OnGuildRoleUpdate(const dpp::guild_role_update_t &obj)
{
return true;
}
bool Module::OnGuildRoleDelete(const dpp::guild_role_delete_t &obj)
{
return true;
}
bool Module::OnPresenceUpdateWS(const dpp::presence_update_t &obj)
{
return true;
}
bool Module::OnVoiceStateUpdate(const dpp::voice_state_update_t &obj)
{
return true;
}
bool Module::OnVoiceServerUpdate(const dpp::voice_server_update_t &obj)
{
return true;
}
bool Module::OnWebhooksUpdate(const dpp::webhooks_update_t &obj)
{
return true;
}
bool Module::OnAllShardsReady()
{
return true;
}
|