forked from cee-studio/orca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscord-adapter.c
More file actions
184 lines (158 loc) · 5.64 KB
/
Copy pathdiscord-adapter.c
File metadata and controls
184 lines (158 loc) · 5.64 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include "discord.h"
#include "discord-internal.h"
#include "cee-utils.h"
void
discord_adapter_init(struct discord_adapter *adapter, struct logconf *conf, struct sized_buffer *token)
{
adapter->ua = ua_init(conf);
ua_set_url(adapter->ua, DISCORD_API_BASE_URL);
adapter->ratelimit = calloc(1, sizeof *adapter->ratelimit);
if (pthread_mutex_init(&adapter->ratelimit->lock, NULL))
ERR("Couldn't initialize pthread mutex");
logconf_branch(&adapter->ratelimit->conf, conf, "DISCORD_RATELIMIT");
if (!token->size) { /* no token means a webhook-only client */
logconf_branch(&adapter->conf, conf, "DISCORD_WEBHOOK");
}
else {
logconf_branch(&adapter->conf, conf, "DISCORD_HTTP");
char auth[128];
int ret = snprintf(auth, sizeof(auth), "Bot %.*s", (int)token->size, token->start);
ASSERT_S(ret < sizeof(auth), "Out of bounds write attempt");
ua_reqheader_add(adapter->ua, "Authorization", auth);
}
}
void
discord_adapter_cleanup(struct discord_adapter *adapter)
{
ua_cleanup(adapter->ua);
discord_buckets_cleanup(adapter);
pthread_mutex_destroy(&adapter->ratelimit->lock);
free(adapter->ratelimit);
ua_info_cleanup(&adapter->err.info);
}
/**
* JSON ERROR CODES
* https://discord.com/developers/docs/topics/opcodes-and-status-codes#json-json-error-codes
*/
static void
json_error_cb(char *str, size_t len, void *p_adapter)
{
struct discord_adapter *adapter = p_adapter;
char message[256]="";
json_extract(str, len, "(message):.*s (code):d",
sizeof(message), message, &adapter->err.jsoncode);
logconf_error(&adapter->conf, ANSICOLOR("(JSON Error %d) %s", ANSI_BG_RED)
" - See Discord's JSON Error Codes\n\t\t%.*s",
adapter->err.jsoncode, message, (int)len, str);
snprintf(adapter->err.jsonstr, sizeof(adapter->err.jsonstr),
"%.*s", (int)len, str);
}
/* template function for performing requests */
ORCAcode
discord_adapter_run(
struct discord_adapter *adapter,
struct ua_resp_handle *resp_handle,
struct sized_buffer *req_body,
enum http_method http_method,
char endpoint_fmt[], ...)
{
va_list args;
char endpoint[2048];
va_start(args, endpoint_fmt);
int ret = vsnprintf(endpoint, sizeof(endpoint), endpoint_fmt, args);
ASSERT_S(ret < sizeof(endpoint), "Out of bounds write attempt");
/* IF UNSET, SET TO DEFAULT ERROR HANDLING CALLBACKS */
if (resp_handle && !resp_handle->err_cb) {
resp_handle->err_cb = &json_error_cb;
resp_handle->err_obj = adapter;
}
/* Check if endpoint_fmt contain a major param */
const char *route;
if (strstr(endpoint_fmt, "/channels/%"))
route = "@channel";
else if (strstr(endpoint_fmt, "/guilds/%"))
route = "@guild";
else if (strstr(endpoint_fmt, "/webhook/%"))
route = "@webhook";
else
route = endpoint_fmt;
struct discord_bucket *bucket = discord_bucket_try_get(adapter, route);
ORCAcode code;
bool keepalive=true;
long delay_ms;
if (bucket) pthread_mutex_lock(&bucket->lock);
do {
ua_info_cleanup(&adapter->err.info);
delay_ms = discord_bucket_get_cooldown(adapter, bucket);
if (delay_ms > 0) {
logconf_info(&adapter->ratelimit->conf,
"[%.4s] RATELIMITING (wait %ld sec)", bucket->hash, delay_ms);
uint64_t t = cee_timestamp_ms();
cee_sleep_ms(delay_ms);
log_info("took: %"PRIu64, cee_timestamp_ms() - t);
}
if (bucket) --bucket->remaining;
code = ua_run(
adapter->ua,
&adapter->err.info,
resp_handle,
req_body,
http_method, endpoint);
if (code != ORCA_HTTP_CODE)
{
keepalive = false;
}
else
{
switch (adapter->err.info.httpcode) {
case HTTP_FORBIDDEN:
case HTTP_NOT_FOUND:
case HTTP_BAD_REQUEST:
keepalive = false;
code = ORCA_DISCORD_JSON_CODE;
break;
case HTTP_UNAUTHORIZED:
keepalive = false;
logconf_fatal(&adapter->conf, "UNAUTHORIZED: Please provide a valid authentication token");
code = ORCA_DISCORD_BAD_AUTH;
break;
case HTTP_METHOD_NOT_ALLOWED:
keepalive = false;
logconf_fatal(&adapter->conf, "METHOD_NOT_ALLOWED: The server couldn't recognize the received HTTP method");
break;
case HTTP_TOO_MANY_REQUESTS: {
bool is_global = false;
char message[256] = "";
double retry_after = -1; /* seconds */
struct sized_buffer body = ua_info_get_body(&adapter->err.info);
json_extract(body.start, body.size,
"(global):b (message):s (retry_after):lf",
&is_global, message, &retry_after);
VASSERT_S(retry_after != -1, "(NO RETRY-AFTER INCLUDED) %s", message);
retry_after *= 1000;
if (is_global) {
logconf_warn(&adapter->conf, "429 GLOBAL RATELIMITING (wait: %.2lf ms) : %s", retry_after, message);
ua_block_ms(adapter->ua, (uint64_t)retry_after);
}
else {
logconf_warn(&adapter->conf, "429 RATELIMITING (wait: %.2lf ms) : %s", retry_after, message);
cee_sleep_ms((long)retry_after);
}
break; }
default:
if (adapter->err.info.httpcode >= 500) /* server related error, retry */
ua_block_ms(adapter->ua, 5000); /* wait for 5 seconds */
break;
}
}
discord_bucket_build(adapter, bucket, route, code, &adapter->err.info);
} while (keepalive);
if (bucket) pthread_mutex_unlock(&bucket->lock);
va_end(args);
return code;
}