forked from cee-studio/orca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord-misc.c
475 lines (425 loc) · 13 KB
/
discord-misc.c
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#define _GNU_SOURCE /* asprintf() */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "discord.h"
#include "discord-internal.h"
#include "orka-utils.h"
struct msg {
u64_snowflake_t id;
bool matched;
};
ORCAcode
discord_delete_messages_by_author_id(
struct discord *client,
u64_snowflake_t channel_id,
u64_snowflake_t author_id)
{
if (!channel_id) {
log_error("Missing 'channel_id");
return ORCA_MISSING_PARAMETER;
}
if (!author_id) {
log_error("Missing 'author_id");
return ORCA_MISSING_PARAMETER;
}
ORCAcode code;
struct discord_get_channel_messages_params params = { .limit = 100 };
NTL_T(struct discord_message) messages=NULL;
code = discord_get_channel_messages(client, channel_id, ¶ms, &messages);
if (ORCA_OK != code) {
log_error("Couldn't fetch channel messages");
return code;
}
u64_unix_ms_t now = orka_timestamp_ms();
NTL_T(u64_snowflake_t) list = NULL;
int count=0;
for (int i=0; messages[i]; ++i) {
if (now > messages[i]->timestamp && now - messages[i]->timestamp > 1209600000)
{
break;
}
if (!author_id || author_id == messages[i]->author->id)
++count;
}
if (0 == count) {
log_trace("Couldn't fetch messages from author");
return ORCA_OK;
}
list = (NTL_T(u64_snowflake_t))ntl_calloc(count, sizeof(u64_snowflake_t));
for (int i=0, j=0; messages[i] && j < count; ++i) {
if (!author_id || author_id == messages[i]->author->id) {
*list[j] = messages[i]->id;
++j;
}
}
ntl_free((ntl_t)messages, discord_message_cleanup_v);
if (count == 1)
code = discord_delete_message(client, channel_id, *list[0]);
else
code = discord_bulk_delete_messages(client, channel_id, list);
return code;
}
void
discord_message_from_json(char *json, size_t len, struct discord_message *p)
{
p->referenced_message = discord_message_alloc();
static size_t ret=0; // used for debugging
size_t r=0;
r=json_extract(json, len,
"(id):F,"
"(channel_id):F,"
"(guild_id):F,"
"(author):F,"
"(member):F,"
"(content):?s,"
"(timestamp):F,"
"(edited_timestamp):F,"
"(tts):b,"
"(mention_everyone):b,"
"(mentions):F,"
"(mention_roles):F,"
"(mention_channels):F,"
"(attachments):F,"
"(embeds):F,"
"(reactions):F,"
"(nonce):?s,"
"(pinned):b,"
"(webhook_id):F,"
"(type):d,"
"(activity):F,"
"(application):F,"
"(message_reference):F,"
"(flags):d,"
"(stickers):F,"
"(referenced_message):F,"
"@arg_switches:b"
"@record_defined"
"@record_null",
orka_strtoull, &p->id,
orka_strtoull, &p->channel_id,
orka_strtoull, &p->guild_id,
discord_user_from_json, p->author,
discord_guild_member_from_json, p->member,
&p->content,
orka_iso8601_to_unix_ms, &p->timestamp,
orka_iso8601_to_unix_ms, &p->edited_timestamp,
&p->tts,
&p->mention_everyone,
discord_user_list_from_json, &p->mentions,
ja_u64_list_from_json, &p->mention_roles,
discord_channel_mention_list_from_json, &p->mention_channels,
discord_channel_attachment_list_from_json, &p->attachments,
discord_embed_list_from_json, &p->embeds,
discord_channel_reaction_list_from_json, &p->reactions,
&p->nonce,
&p->pinned,
orka_strtoull, &p->webhook_id,
&p->type,
discord_message_activity_from_json, p->activity,
discord_message_application_list_from_json, &p->application,
discord_message_reference_from_json, p->message_reference,
&p->flags,
discord_message_sticker_list_from_json, &p->stickers,
discord_message_from_json, p->referenced_message,
p->__M.arg_switches, sizeof(p->__M.arg_switches), p->__M.enable_arg_switches,
p->__M.record_defined, sizeof(p->__M.record_defined),
p->__M.record_null, sizeof(p->__M.record_null));
if(!p->referenced_message->id) {
discord_message_free(p->referenced_message);
p->referenced_message = NULL;
}
ret = r;
}
void discord_channel_overwrite_from_json(char *json, size_t len, struct discord_channel_overwrite *p)
{
static size_t ret=0; //used for debugging
size_t r=0;
r=json_extract(json, len,
"(id):F,"
//"(type):s," @todo
//"(allow_new):s," @todo
"(allow):lld,"
//"(deny_new):s," @todo
"(deny):lld,"
"@arg_switches:b"
"@record_defined"
"@record_null",
orka_strtoull, &p->id,
//&p->type,
&p->allow,
&p->deny,
p->__M.arg_switches, sizeof(p->__M.arg_switches), p->__M.enable_arg_switches,
p->__M.record_defined, sizeof(p->__M.record_defined),
p->__M.record_null, sizeof(p->__M.record_null));
ret = r;
(void)ret;
}
size_t
discord_channel_overwrite_to_json(char *json, size_t len, struct discord_channel_overwrite *p)
{
size_t r;
r=json_inject(json, len,
"(id):|F|,"
"(type):d,"
"(allow):s_as_u64,"
"(deny):s_as_u64,"
"@arg_switches:b",
orka_ulltostr, &p->id,
&p->type,
&p->allow,
&p->deny,
p->__M.arg_switches, sizeof(p->__M.arg_switches), p->__M.enable_arg_switches);
return r;
}
void
discord_embed_set_footer(
struct discord_embed *embed,
char text[],
char icon_url[],
char proxy_icon_url[])
{
if (IS_EMPTY_STRING(text)) {
log_error("Missing 'text'");
return;
}
if (embed->footer) {
free(embed->footer);
}
struct discord_embed_footer *new_footer = discord_embed_footer_alloc();
strncpy(new_footer->text, text, EMBED_FOOTER_TEXT_LEN);
if (icon_url)
asprintf(&new_footer->icon_url, "%s", icon_url);
if (proxy_icon_url)
asprintf(&new_footer->proxy_icon_url, "%s", proxy_icon_url);
embed->footer = new_footer;
}
void
discord_embed_set_thumbnail(
struct discord_embed *embed,
char url[],
char proxy_url[],
int height,
int width)
{
if (embed->thumbnail) {
free(embed->thumbnail);
}
struct discord_embed_thumbnail *new_thumbnail = discord_embed_thumbnail_alloc();
if (url)
asprintf(&new_thumbnail->url, "%s", url);
if (proxy_url)
asprintf(&new_thumbnail->proxy_url, "%s", proxy_url);
if (height)
new_thumbnail->height = height;
if (width)
new_thumbnail->width = width;
embed->thumbnail = new_thumbnail;
}
void
discord_embed_set_image(
struct discord_embed *embed,
char url[],
char proxy_url[],
int height,
int width)
{
if (embed->image) {
free(embed->image);
}
struct discord_embed_image *new_image = discord_embed_image_alloc();
if (url)
asprintf(&new_image->url, "%s", url);
if (proxy_url)
asprintf(&new_image->proxy_url, "%s", proxy_url);
if (height)
new_image->height = height;
if (width)
new_image->width = width;
embed->image = new_image;
}
void
discord_embed_set_video(
struct discord_embed *embed,
char url[],
char proxy_url[],
int height,
int width)
{
if (embed->video) {
free(embed->video);
}
struct discord_embed_video *new_video = discord_embed_video_alloc();
if (url)
asprintf(&new_video->url, "%s", url);
if (proxy_url)
asprintf(&new_video->proxy_url, "%s", proxy_url);
if (height)
new_video->height = height;
if (width)
new_video->width = width;
embed->video = new_video;
}
void
discord_embed_set_provider(struct discord_embed *embed, char name[], char url[])
{
if (embed->provider) {
free(embed->provider);
}
struct discord_embed_provider *new_provider = discord_embed_provider_alloc();
if (url)
asprintf(&new_provider->url, "%s", url);
if (!IS_EMPTY_STRING(name))
strncpy(new_provider->name, name, EMBED_AUTHOR_NAME_LEN);
embed->provider = new_provider;
}
void
discord_embed_set_author(
struct discord_embed *embed,
char name[],
char url[],
char icon_url[],
char proxy_icon_url[])
{
if (embed->author) {
free(embed->author);
}
struct discord_embed_author *new_author = discord_embed_author_alloc();
if (!IS_EMPTY_STRING(name))
strncpy(new_author->name, name, EMBED_AUTHOR_NAME_LEN);
if (url)
asprintf(&new_author->url, "%s", url);
if (icon_url)
asprintf(&new_author->icon_url, "%s", icon_url);
if (proxy_icon_url)
asprintf(&new_author->proxy_icon_url, "%s", proxy_icon_url);
embed->author = new_author;
}
void
discord_embed_add_field(struct discord_embed *embed, char name[], char value[], bool Inline)
{
if (ntl_length((ntl_t)embed->fields) >= EMBED_MAX_FIELDS) {
log_error("Reach embed fields threshold (max %d)", EMBED_MAX_FIELDS);
return;
}
if (IS_EMPTY_STRING(name)) {
log_error("Missing 'name'");
return;
}
if (IS_EMPTY_STRING(value)) {
log_error("Missing 'value'");
return;
}
struct discord_embed_field *field = discord_embed_field_alloc();
field->Inline = Inline;
size_t ret;
if (!(ret = orka_str_bounds_check(name, sizeof(field->name)))) {
log_warn("'name' exceeds %d characters, truncation will occur", sizeof(field->name));
snprintf(field->name, sizeof(field->name), "%.*s(...)", \
(int)(sizeof(field->name)-6), name);
}
else {
snprintf(field->name, sizeof(field->name), "%s", name);
}
if (!(ret = orka_str_bounds_check(value, sizeof(field->value)))) {
log_warn("'value' exceeds %d characters, truncation will occur", sizeof(field->value));
snprintf(field->value, sizeof(field->value), "%.*s(...)", \
(int)(sizeof(field->value)-6), value);
}
else {
snprintf(field->value, sizeof(field->value), "%s", value);
}
ntl_append2((ntl_t*)&embed->fields, sizeof(struct discord_embed_field), field);
discord_embed_field_free(field);
}
void
discord_overwrite_append(
NTL_T(struct discord_channel_overwrite) *permission_overwrites,
u64_snowflake_t id,
int type,
enum discord_permissions_bitwise_flags allow,
enum discord_permissions_bitwise_flags deny)
{
if (!id) {
log_error("Missing 'id'");
return;
}
if ( !(0 == type || 1 == type) ) {
log_error("'type' should be 0 (role) or 1 (member)");
return;
}
struct discord_channel_overwrite new_overwrite;
discord_channel_overwrite_init(&new_overwrite);
new_overwrite.id = id;
new_overwrite.type = type;
new_overwrite.allow = allow;
new_overwrite.deny = deny;
ntl_append2((ntl_t*)permission_overwrites, sizeof(struct discord_channel_overwrite), &new_overwrite);
}
//@todo create some manner of copying a struct, including its pointer fields
ORCAcode
discord_get_channel_at_pos(
struct discord *client,
const u64_snowflake_t guild_id,
const enum discord_channel_types type,
const size_t position,
struct discord_channel *p_channel)
{
if (!guild_id) {
log_error("Missing 'guild_id'");
return ORCA_MISSING_PARAMETER;
}
if (!p_channel) {
log_error("Missing 'p_channel'");
return ORCA_MISSING_PARAMETER;
}
NTL_T(struct discord_channel) channels = NULL;
ORCAcode code;
code = discord_get_guild_channels(client, guild_id, &channels);
if (ORCA_OK != code) {
log_error("Couldn't fetch channels from guild");
return code;
}
size_t j=0; // calculate position
for (size_t i=0; channels[i]; ++i) {
if (type == channels[i]->type && j++ == position) {
memcpy(p_channel, channels[i], sizeof(struct discord_channel));
// avoid double freeing
p_channel->permission_overwrites = NULL;
p_channel->recipients = NULL;
p_channel->messages = NULL;
break; /* EARLY BREAK */
}
}
discord_channel_list_free(channels);
return code; // ORCA_OK
}
ORCAcode
discord_disconnect_guild_member(
struct discord *client,
const u64_snowflake_t guild_id,
const u64_snowflake_t user_id,
struct discord_guild_member *p_member)
{
if (!guild_id) {
log_error("Missing 'guild_id'");
return ORCA_MISSING_PARAMETER;
}
if (!user_id) {
log_error("Missing 'user_id'");
return ORCA_MISSING_PARAMETER;
}
struct ua_resp_handle resp_handle = {
.ok_cb = p_member ? &discord_guild_member_from_json_v : NULL,
.ok_obj = p_member,
};
char payload[MAX_PAYLOAD_LEN];
size_t ret = json_inject(payload, sizeof(payload), "(channel_id):null");
struct sized_buffer req_body = { payload, ret };
return discord_adapter_run(
&client->adapter,
&resp_handle,
&req_body,
HTTP_PATCH,
"/guilds/%"PRIu64"/members/%"PRIu64, guild_id, user_id);
}