summaryrefslogtreecommitdiffstats
path: root/src/api.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/api.c148
1 files changed, 130 insertions, 18 deletions
diff --git a/src/api.c b/src/api.c
index ce19d58..ead58df 100644
--- a/src/api.c
+++ b/src/api.c
@@ -1,5 +1,7 @@
#define DC_LOGIN_FORMAT "{\"login\":\"%s\",\"password\":\"%s\",\"undelete\":false,\"captcha_key\":null,\"login_source\":null,\"gift_code_sku_id\":null}"
-#define DC_USER_AGENT "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36"
+#define DC_COMPILE_INFO __VERSION__ " " __BASE_FILE__ " " __FILE__ " " __TIMESTAMP__
+#define DC_IDENTIFY_FORMAT "{\"op\":2,\"d\":{\"token\":\"%s\",\"intents\":%d" /* ",\"presence\":{\"since\":null,\"status\":\"online\",\"afk\":false,\"activities\":{\"name\":\":pager: Connected via discord.c\",\"type\":4}}" */ ",\"properties\":{\"$os\":\"posix\",\"$browser\":\"discord.c\",\"$device\":\"" DC_COMPILE_INFO "\"}}}" /* this exposes we are discord.c, if this turns out to get people banned, change to whatever an official client sends */
+#define DC_USER_AGENT "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" /* this is so we can fool the login system */
#define DC_SERVER_ADDRESS "discord.com"
#define DC_SERVER_PORT 443
#define DC_SERVER_SSL 1
@@ -8,7 +10,7 @@
#define DC_WS_PORT DC_SERVER_PORT
#define DC_WS_SSL DC_SERVER_SSL
#define DC_WS_ORIGIN DC_SERVER_ORIGIN
-#define DC_WS_PATH "/"
+#define DC_WS_PATH "/" /* add ?v=9&encoding=json in case of problems */
#define DC_API_PREFIX "/api/v9/"
#define DC_LWS_ABLE_TO_PARSE_HEADERS 1
/* libwebsockets information: libwebsockets works with event loops and discord.c primary targets debian for building (it must work on debian), but debian does not ship libwebsockets with glib or libevent event loop support, so loops are implemented in the classic poll() style. this reduces performance probably. if you use discord.c API with support for a platform specific event loop, you may rewrite LWS to do things differently. currently calls into API (dc_api_i and dc_api_o) will both do LWS stuff. */
@@ -18,11 +20,70 @@ void dc_api_stack (struct dc_api_io i) { /* stack output struct to be delivered
*(i.program->api_ios[i.program->api_ios_length++]) = i;
return;
}
+signed char dc_json_cb (struct lejp_ctx * ctx, char /* enum lejp_callbacks */ reason) {
+ enum dc_json_paths path = ctx->path_match-1; /* we assume that the order of incoming data is */
+ struct dc_lws_pass * pass = ctx->user; /* correct. op should come first, etc. */
+ pass->json_reason = reason;
+ if (reason == LEJPCB_FAILED) {
+ DC_API_IO_GC(pass->api_io);
+ pass->packet = DC_NONE;
+ pass->parsing_status = NULL;
+ return '\0';
+ }
+ fprintf(stderr, "JSON: %s reason: %d %.*s\n", ctx->path, reason, 40, pass->cp);
+ if (ctx->path_match && reason & LEJP_FLAG_CB_IS_VALUE) {
+ switch (path) {
+ case DC_JSON_S: /* packet sequence number */
+ pass->api_io.client->last_packet = strtoull(ctx->buf, NULL, 10);
+ break;
+ case DC_JSON_OP:
+ DC_API_IO_GC(pass->api_io); /* frees all unfinished parsing objects */
+ pass->packet = atoi(ctx->buf);
+ break;
+ case DC_JSON_PING:
+ pass->api_io.client->ping_interval = atoi(ctx->buf);
+ break;
+ default: /* to prevent warning: enumeration value DC_JSON_* not handled */
+ break;
+ }
+ return '\0';
+ }
+ if (pass->packet == DC_NONE) /* useless to do anything if we haven't received packet type */
+ return '\0';
+ if (reason == LEJPCB_COMPLETE) { /* gr8, fin, call back to api */
+ if (!pass->parsing_status) { /* some stuff is fully handled in switch (pass->pkt) */
+ pass->packet = DC_NONE;
+ return '\0';
+ }
+ *pass->parsing_status &= ~DC_IN_PROGRESS;
+ pass->api_io.status |= DC_FROM_LWS; /* we callback on FAILED too, so that we don't */
+ // dc_api_io(pass->api_io); /* end up with a memory leak if we allocated a struct */
+ pass->api_io.status &= ~DC_FROM_LWS;
+ pass->parsing_status = NULL;
+ pass->packet = DC_NONE;
+ return '\0';
+ }
+ switch (pass->packet) { /* we fill in structs, set DC_INCOMPLETE and pass->parsing_status */
+ case DC_PING:
+ dc_handle_ping(pass->api_io, pass /* this is just so it's not NULL */);
+ break;
+ default:
+ break;
+ }
+ return '\0'; /* by the way, what does the return value do here? write in the comments below (; */
+}
static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us, void * in, size_t len) {
struct dc_lws_pass * pass = (struct dc_lws_pass *) us;
+ unsigned char buf[LWS_PRE+DC_LWS_BUF+1]; /* whooh, boy, this is more than a meg of stack! */
switch (rs) {
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: /* TODO: handle and report somehow */
- pass->api_io.status = DC_NET_ERROR;
+ if (pass) {
+ pass->api_io.status = DC_NET_ERROR;
+ pass->wsi = NULL;
+ pass->api_io.status |= DC_FROM_LWS;
+ dc_api_i(pass->api_io);
+ pass->api_io.status &= ~DC_FROM_LWS;
+ }
lwsl_err("CLIENT_CONNECTION_ERROR: %s\n", in ? (char *) in : "(null)");
break;
case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: /* lws gives us len space after *in */
@@ -90,10 +151,9 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us,
pass->body[pass->body_length] = '\0'; /* on heap and freed on _DESTROY. */
return 0; /* don't pass to lws_callback_http_dummy */
case LWS_CALLBACK_RECEIVE_CLIENT_HTTP: /* uninterpreted http content */
- fprintf(stderr, "LCRCH\n");
- char buffer[DC_LWS_BUF + LWS_PRE];
- char * ptr = buffer + LWS_PRE; /* see, BEFORE the pointer - lower addr */
- int len2 /* DC_LWS_BUF again */ = sizeof(buffer) - LWS_PRE;
+ ;
+ char * ptr = (char *) buf + LWS_PRE; /* see, BEFORE the pointer - lower addr */
+ int len2 /* DC_LWS_BUF again */ = sizeof(buf) - LWS_PRE;
if (lws_http_client_read(wsi, &ptr, &len2) < 0) {
fprintf(stderr, "lws_http_client_read -1\n");
return -1;
@@ -114,21 +174,52 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us,
fprintf(stderr, "pass before freed is %p\n", (void *) pass);
if (pass->api_io.status & DC_MUST_FREE) /* we still do this check in case */
free(pass->body); /* _READ was not called and pointer was static */
+ pass->api_io.client->pass = NULL;
free(pass); /* called at the final moment when user pointer and wsi is still */
break; /* accessible - we can free the struct that was passed in as a heap ptr */
+ case LWS_CALLBACK_WSI_CREATE: /* first - outermost - call with wsi present */
+ if (pass->api_io.client && pass->api_io.status & DC_SET_PASS) {
+ fprintf(stderr, "pass->api_io.client->pass = pass\n");
+ pass->api_io.client->pass = pass;
+ }
+ break;
case LWS_CALLBACK_CLIENT_ESTABLISHED: /* websocket established */
+ fprintf(stderr, "LWS_CALLBACK_CLIENT_ESTABLISHED\n");
pass->api_io.status = DC_OK;
+ pass->wsi = wsi;
+ pass->api_io.status |= DC_FROM_LWS;
+ dc_api_i(pass->api_io);
+ pass->api_io.status &= ~DC_FROM_LWS;
break;
case LWS_CALLBACK_CLIENT_CLOSED: /* websocket closed */
+ DC_API_IO_GC(pass->api_io); /* frees all unfinished parsing objects */
+ pass->packet = DC_NONE;
pass->api_io.status = DC_NET_ERROR;
+ pass->wsi = NULL;
+ pass->api_io.status |= DC_FROM_LWS;
+ dc_api_i(pass->api_io);
+ pass->api_io.status &= ~DC_FROM_LWS;
break;
case LWS_CALLBACK_CLIENT_RECEIVE: /* websocket receive, pass to json parser ? */
- lwsl_hexdump_notice(in, len);
- if (pass->json_reason == LEJPCB_COMPLETE || !(pass->api_io.status & DC_LEJP_CONSTRUCTED)) { /* we regenerate a new context in case we didn't do that yet or in case the previous one finished parsing */
+ fprintf(stdout, "%.*s", len, (const unsigned char *) in);
+ fflush(stdout);
+ if (pass->json_reason == LEJPCB_COMPLETE || pass->json_reason == LEJPCB_FAILED || !(pass->api_io.status & DC_LEJP_CONSTRUCTED)) { /* we regenerate a new context in case we didn't do that yet or in case the previous one finished parsing */
pass->api_io.status |= DC_LEJP_CONSTRUCTED;
- lejp_construct(pass->json_context, dc_json_cb, us /* == (void *) pass */, dc_json_paths, LWS_ARRAY_SIZE(dc_json_paths));
+ pass->packet = DC_NONE;
+ lejp_construct(&pass->json_ctx, dc_json_cb, us /* == (void *) pass */, (const char * const *) dc_json_paths, LWS_ARRAY_SIZE(dc_json_paths));
+ } /* lejp_parse does return number of excess bytes after parsing json. but I */
+ pass->len = len; /* assume that server sends in chunks that always terminate */
+ pass->cp = in; /* when json object terminates. valve pls fix */
+ lejp_parse(&pass->json_ctx, (const unsigned char *) in, len);
+ break;
+ case LWS_CALLBACK_CLIENT_WRITEABLE: /* invoke with lws_callback_on_writeable(wsi) 4 ws */
+ memcpy(buf+LWS_PRE, pass->body, pass->body_length);
+ (buf+LWS_PRE)[pass->body_length] = '\0';
+ lwsl_hexdump_notice(buf+LWS_PRE, pass->body_length);
+ if (lws_write(wsi, (unsigned char *) buf+LWS_PRE, pass->body_length, LWS_WRITE_BINARY) != (int) pass->body_length) {
+ fprintf(stderr, "ws lws_write failed!\n");
+ return -1;
}
- lejp_parse(pass->json_context, (const unsigned char) in, len);
break;
default:
break;
@@ -149,10 +240,11 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun
break;
case DC_API_GUILD:
break;
- case DC_API_LOGIN: /* library currently only works for a single client */
+ case DC_API_LOGIN: /* TODO: library currently only works for a single client */
if (!i.program->clients_length) /* already attempted login */
DC_MR(i.program->clients);
i.program->clients[0] = i.client;
+ i.program->clients_length = 1;
memset(&info, 0, sizeof(info));
info.context = i.program->lws_context;
#if DC_SERVER_SSL
@@ -170,6 +262,7 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun
pass->body_length = smprintf(&pass->body, DC_LOGIN_FORMAT, i.client->email, i.client->password);
fprintf(stderr, "length: %u string: %s\n", pass->body_length, pass->body);
i.type = DC_API_LOGIN_CB;
+ i.status &= ~DC_SET_PASS; /* this is a HTTP request, pass is for websockets */
memcpy(&pass->api_io, &i, sizeof(i));
pass->api_io.pass = pass;
strcpy(pass->headers[DC_LWS_CONTENT_TYPE], "application/json");
@@ -186,7 +279,7 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun
case DC_API_LOGIN_CB:
#define DC_STACK_RETURN(x) do { i.client->status = x; dc_api_stack(i); return; } while (0)
i.type = DC_API_LOGIN;
- i.status &= ~(DC_OK);
+ i.status &= ~DC_OK;
if (!i.pass->body) {
i.client->status |= DC_REQUEST_FAILED;
dc_api_stack(i);
@@ -230,6 +323,7 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun
info.local_protocol_name = info.protocol;
pass = calloc(1, sizeof(struct dc_lws_pass));
i.type = DC_API_WS_CB;
+ i.status |= DC_SET_PASS;
memcpy(&pass->api_io, &i, sizeof(i));
pass->api_io.pass = pass;
info.userdata = pass;
@@ -243,7 +337,19 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun
}
break;
case DC_API_WS_CB:
- dc_api_stack(i); /* .status is either NET_ERROR 4 closed or error or OK 4 esta */
+ i.client->status &= ~(DC_NET_ERROR | DC_OK);
+ i.client->status |= i.status;
+ if (i.status & DC_NET_ERROR)
+ fprintf(stderr, "websocket connection was closed by the remote host\n");
+ else { /* commerce identify */
+ if (i.pass->api_io.status & DC_MUST_FREE)
+ free(i.pass->body);
+ i.pass->api_io.status |= DC_MUST_FREE;
+ i.pass->body_length = smprintf(&i.pass->body, DC_IDENTIFY_FORMAT, i.client->authorization, DC_INTENTS);
+ lws_callback_on_writable(i.pass->wsi);
+ }
+ dc_api_stack(i); /* cl.stat is either NET_ERROR 4 closed or error or OK 4 esta */
+ break;
case DC_API_REGISTER:
break;
case DC_API_STATUS:
@@ -255,9 +361,8 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun
case DC_API_NONE:
break;
case DC_API_ATTACH:
- if (i.attached_function->type == DC_API_NONE)
- break;
DC_MR(i.program->attached_functions); /* makes room for at least one */
+ i.program->attached_functions[i.program->attached_functions_length++] = i.attached_function; /* is on heap */
break;
}
}
@@ -273,9 +378,16 @@ struct dc_api_io dc_api_o (struct dc_api_io i /* for ->program */) {
memcpy(&o, (i.program->api_ios[--i.program->api_ios_length]), sizeof(o));
dc_api_io_free(i.program->api_ios[i.program->api_ios_length]);
}
+ int is_broken = 0;
for (size_t j = 0; j < i.program->attached_functions_length; j++) { /* call attached functions */
- if (i.program->attached_functions[j]->type == o.type) /* wow, it's possible to attach to NONE */
- i.program->attached_functions[j]->function(o, i.program->attached_functions[j]->user_data);
+ if (i.program->attached_functions[j]->type != DC_API_TIMEOUT) {
+ if (i.program->attached_functions[j]->type == o.type && !is_broken)
+ if (i.program->attached_functions[j]->function(o, i.program->attached_functions[j]->user_data) & DC_BREAK)
+ is_broken++;
+ } else
+ if (i.program->attached_functions[j]->last + i.program->attached_functions[j]->every < time(NULL))
+ if (!(i.program->attached_functions[j]->function(o, i.program->attached_functions[j]->user_data) & DC_RETRY))
+ i.program->attached_functions[j]->last = time(NULL);
}
return o;
}