-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathdiscord-misc.c
322 lines (270 loc) · 9 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "discord.h"
#include "discord-internal.h"
#include "cee-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)
{
struct discord_get_channel_messages_params params = { 0 };
struct discord_message **messages = NULL;
ORCAcode code;
ORCA_EXPECT(client, channel_id != 0, ORCA_BAD_PARAMETER, "");
ORCA_EXPECT(client, author_id != 0, ORCA_BAD_PARAMETER, "");
params.limit = 100;
code = discord_get_channel_messages(client, channel_id, ¶ms, &messages);
if (code != ORCA_OK) {
logconf_error(&client->conf, "Couldn't fetch channel messages");
}
else {
u64_unix_ms_t now = discord_timestamp(client);
u64_snowflake_t **list = NULL;
int count = 0;
int i, j;
for (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) {
logconf_trace(&client->conf, "Couldn't fetch messages from author");
return ORCA_OK;
}
list = (u64_snowflake_t **)ntl_calloc(count, sizeof(u64_snowflake_t));
for (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_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)
discord_embed_footer_cleanup(embed->footer);
else
embed->footer = malloc(sizeof *embed->footer);
discord_embed_footer_init(embed->footer);
if (text) cee_strndup(text, strlen(text), &embed->footer->text);
if (icon_url)
cee_strndup(icon_url, strlen(icon_url), &embed->footer->icon_url);
if (proxy_icon_url)
cee_strndup(proxy_icon_url, strlen(proxy_icon_url),
&embed->footer->proxy_icon_url);
}
void
discord_embed_set_title(struct discord_embed *embed, char format[], ...)
{
char buf[2048];
size_t len;
va_list args;
va_start(args, format);
len = vsnprintf(buf, sizeof(buf), format, args);
ASSERT_S(len < sizeof(buf), "Out of bounds write attempt");
if (embed->title) free(embed->title);
cee_strndup(buf, len, &embed->title);
va_end(args);
}
void
discord_embed_set_description(struct discord_embed *embed, char format[], ...)
{
char buf[2048];
size_t len;
va_list args;
va_start(args, format);
len = vsnprintf(buf, sizeof(buf), format, args);
ASSERT_S(len < sizeof(buf), "Out of bounds write attempt");
if (embed->description) free(embed->description);
cee_strndup(buf, len, &embed->description);
va_end(args);
}
void
discord_embed_set_url(struct discord_embed *embed, char format[], ...)
{
char buf[2048];
size_t len;
va_list args;
va_start(args, format);
len = vsnprintf(buf, sizeof(buf), format, args);
ASSERT_S(len < sizeof(buf), "Out of bounds write attempt");
if (embed->url) free(embed->url);
cee_strndup(buf, len, &embed->url);
va_end(args);
}
void
discord_embed_set_thumbnail(struct discord_embed *embed,
char url[],
char proxy_url[],
int height,
int width)
{
if (embed->thumbnail)
discord_embed_thumbnail_cleanup(embed->thumbnail);
else
embed->thumbnail = malloc(sizeof *embed->thumbnail);
discord_embed_thumbnail_init(embed->thumbnail);
if (url) cee_strndup(url, strlen(url), &embed->thumbnail->url);
if (proxy_url)
cee_strndup(proxy_url, strlen(proxy_url), &embed->thumbnail->proxy_url);
if (height) embed->thumbnail->height = height;
if (width) embed->thumbnail->width = width;
}
void
discord_embed_set_image(struct discord_embed *embed,
char url[],
char proxy_url[],
int height,
int width)
{
if (embed->image)
discord_embed_image_cleanup(embed->image);
else
embed->image = malloc(sizeof *embed->image);
discord_embed_image_init(embed->image);
if (url) cee_strndup(url, strlen(url), &embed->image->url);
if (proxy_url)
cee_strndup(proxy_url, strlen(proxy_url), &embed->image->proxy_url);
if (height) embed->image->height = height;
if (width) embed->image->width = width;
}
void
discord_embed_set_video(struct discord_embed *embed,
char url[],
char proxy_url[],
int height,
int width)
{
if (embed->video)
discord_embed_video_cleanup(embed->video);
else
embed->video = malloc(sizeof *embed->video);
discord_embed_video_init(embed->video);
if (url) cee_strndup(url, strlen(url), &embed->video->url);
if (proxy_url)
cee_strndup(proxy_url, strlen(proxy_url), &embed->video->proxy_url);
if (height) embed->video->height = height;
if (width) embed->video->width = width;
}
void
discord_embed_set_provider(struct discord_embed *embed,
char name[],
char url[])
{
if (embed->provider)
discord_embed_provider_cleanup(embed->provider);
else
embed->provider = malloc(sizeof *embed->provider);
discord_embed_provider_init(embed->provider);
if (name) cee_strndup(name, strlen(name), &embed->provider->name);
if (url) cee_strndup(url, strlen(url), &embed->provider->url);
}
void
discord_embed_set_author(struct discord_embed *embed,
char name[],
char url[],
char icon_url[],
char proxy_icon_url[])
{
if (embed->author)
discord_embed_author_cleanup(embed->author);
else
embed->author = malloc(sizeof *embed->author);
discord_embed_author_init(embed->author);
if (name) cee_strndup(name, strlen(name), &embed->author->name);
if (url) cee_strndup(url, strlen(url), &embed->author->url);
if (icon_url)
cee_strndup(icon_url, strlen(icon_url), &embed->author->icon_url);
if (proxy_icon_url)
cee_strndup(proxy_icon_url, strlen(proxy_icon_url),
&embed->author->proxy_icon_url);
}
void
discord_embed_add_field(struct discord_embed *embed,
char name[],
char value[],
bool Inline)
{
struct discord_embed_field field = { 0 };
field.Inline = Inline;
if (name) cee_strndup(name, strlen(name), &field.name);
if (value) cee_strndup(value, strlen(value), &field.value);
ntl_append2((ntl_t *)&embed->fields, sizeof(struct discord_embed_field),
&field);
}
void
discord_overwrite_append(struct discord_overwrite ***permission_overwrites,
u64_snowflake_t id,
int type,
u64_bitmask_t allow,
u64_bitmask_t deny)
{
struct discord_overwrite new_overwrite = { 0 };
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_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 *ret)
{
struct discord_channel **channels = NULL;
ORCAcode code;
ORCA_EXPECT(client, guild_id != 0, ORCA_BAD_PARAMETER, "");
ORCA_EXPECT(client, ret != NULL, ORCA_BAD_PARAMETER, "");
code = discord_get_guild_channels(client, guild_id, &channels);
if (ORCA_OK != code) {
log_error("Couldn't fetch channels from guild");
return code;
}
else {
size_t i, j; /* calculate position */
for (i = 0, j = 0; channels[i]; ++i) {
if (type == channels[i]->type && j++ == position) {
memcpy(ret, channels[i], sizeof(struct discord_channel));
/* avoid double freeing */
memset(channels[i], 0, sizeof(struct discord_channel));
break; /* EARLY BREAK */
}
}
discord_channel_list_free(channels);
}
return code;
}
void
discord_presence_add_activity(struct discord_presence_status *presence,
struct discord_activity *activity)
{
ntl_append2((ntl_t *)&presence->activities, sizeof(struct discord_activity),
activity);
}