diff options
Diffstat (limited to 'src/api.c')
-rw-r--r-- | src/api.c | 45 |
1 files changed, 29 insertions, 16 deletions
@@ -1,7 +1,9 @@ -#define DC_LOGIN_FORMAT "{\"login\":\"%s\",\"password\":\"%s\",\"undelete\":false,\"captcha_key\":null,\"login_source\":null,\"gift_code_sku_id\":null}" +#define DC_LOGIN_FORMAT "{\"login\":\"%s\",\"password\":\"%s\",\"undelete\":false," \ + "\"captcha_key\":null,\"login_source\":null,\"gift_code_sku_id\":null}" #define DC_COMPILE_INFO __VERSION__ " " __BASE_FILE__ " " __FILE__ " " __TIMESTAMP__ #define DC_STR(x) x -#define DC_USER_AGENT "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36" /* this is so we can fool the login system */ +#define DC_USER_AGENT "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) " \ + "Chrome/90.0.4430.212 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 @@ -14,6 +16,14 @@ #define DC_API_PREFIX "/api/v9/" #define DC_LWS_ABLE_TO_PARSE_HEADERS 1 #define DC_RECONNECT_DELAY 10 +#define cJSON_GetObjectItem2(root, name1, name2) (cJSON_GetObjectItem(root, name1) ? /* mazohi= */ \ + cJSON_GetObjectItem(cJSON_GetObjectItem(root, name1), name2) : NULL) /* stično! */ +#define cJSON_GetObjectItem3(root, name1, name2, name3) (cJSON_GetObjectItem2(root, name1, name2) ? \ + cJSON_GetObjectItem(cJSON_GetObjectItem2(root, name1, name2), name3) : NULL) +#define cJSON_GetObjectItem4(ro, na1, na2, na3, na4) (cJSON_GetObjectItem3(ro, na1, na2, na3) ? \ + cJSON_GetObjectItem(cJSON_GetObjectItem3(ro, na1, na2, na3), na4) : NULL) +#define cJSON_GetObjectItem5(r, n1, n2, n3, n4, n5) (cJSON_GetObjectItem4(r, n1, n2, n3, n4) ? \ + cJSON_GetObjectItem(cJSON_GetObjectItem4(r, n1, n2, n3, n4), n5) : NULL) unsigned char dc_api_identify_u[] = { #include <identify.xxd> }; @@ -129,27 +139,26 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us, case LWS_CALLBACK_CLOSED_CLIENT_HTTP: lwsl_user("LWS_CALLBACK_CLOSED_CLIENT_HTTP\n"); break; - case LWS_CALLBACK_WSI_DESTROY: /* if I understand the docs correctly, this is allways */ + case LWS_CALLBACK_WSI_DESTROY: /* this is allways received */ if (pass->api_io.pass != pass) - fprintf(stderr, "[!!!] REPORT THIS BUG: pass->api_io.pass != pass\n"); + fprintf(stderr, "[!!!] REPORT BUG: pass->api_io.pass != pass\n"); if (pass->api_io.status & DC_DESTROY_CB) { pass->api_io.status |= DC_FROM_LWS; dc_api_i(pass->api_io); pass->api_io.status &= ~DC_FROM_LWS; } DC_API_IO_GC(pass->api_io); /* frees all unfinished parsing objects */ - pass->packet = DC_NONE; pass->api_io.client->pass = NULL; - dc_lws_pass_free(pass, DC_UNSET); /* called the last time us ptr & wsi r still */ - break; /* accessible - we can free the struct that was passed in as a heap ptr */ + dc_lws_pass_free(pass, DC_UNSET); /* called last time us ptr & wsi r still */ + break; case LWS_CALLBACK_WSI_CREATE: /* first - outermost - call with wsi present */ if (pass->api_io.client) { if (pass->api_io.status & DC_SET_PASS) { fprintf(stderr, "pass->api_io.client->pass = pass\n"); pass->api_io.client->pass = pass; } - if (pass->api_io.status & DC_SET_WS_ACTIVE) /* how to get if wsi is ws */ - pass->api_io.client->status |= DC_WS_ACTIVE; /* or http? IDFK. */ + if (pass->api_io.status & DC_SET_WS_ACTIVE) /* how to see if ws */ + pass->api_io.client->status |= DC_WS_ACTIVE; /* or http? */ } break; case LWS_CALLBACK_CLIENT_ESTABLISHED: /* websocket established */ @@ -163,17 +172,16 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us, case LWS_CALLBACK_WS_PEER_INITIATED_CLOSE: pass->reason = 0; if (len < 2) - fprintf(stderr, "SERVER CLOSED CONNECTION WITHOUT PROIVIDING REASON!\n"); + fprintf(stderr, "SERVER CLOSED CONNECTION W/O PROIVIDING REASON!\n"); else - pass->reason = ntohs(*(uint16_t *) in /* i sure hope this is legal */); + pass->reason = ntohs(*(uint16_t *) in /* i hope this is legal */); fprintf(stderr, "SERVER CLOSED CONNECTION WITH REASON %d\n", pass->reason); if (len > 2) - fprintf(stderr, "with additional message: %.*s\n", len-2, (char *) in+2); + fprintf(stderr, "with additional msg: %.*s\n", len-2, (char *) in+2); break; case LWS_CALLBACK_CLIENT_CLOSED: /* websocket closed */ pass->api_io.client->disconnect_time = time(NULL); 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; @@ -183,8 +191,13 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us, case LWS_CALLBACK_CLIENT_RECEIVE: /* websocket receive, pass to json parser ? */ if (getenv("DC_N")) /* output received network to stdout */ fprintf(stdout, "%.*s", len, (const unsigned char *) in); + char * serialized = dc_json(pass->json, in, len); + while (serialized) { + + serialized = dc_json(handler, NULL, 0); + } break; - case LWS_CALLBACK_CLIENT_WRITEABLE: /* invoke with lws_callback_on_writeable(wsi) 4 ws */ + case LWS_CALLBACK_CLIENT_WRITEABLE: /* invoke w/ lws_callback_on_writeable(wsi)4ws */ if (!pass->api_io.client) /* we empty all payloads from 0 to finish */ break; for (size_t i = 0; i < pass->api_io.client->payloads_length; i++) { @@ -234,7 +247,7 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun info.host = info.address; /* info.origin = DC_SERVER_ORIGIN; */ /* just don't send it */ info.method = "POST"; - pass = calloc(1, sizeof(struct dc_lws_pass)); /* cb frees */ + pass = dc_lws_pass_init(); /* cb frees */ fprintf(stderr, "allocated pass at %p\n", (void *) pass); i.status |= DC_DESTROY_CB; /* so that lws_cb will call api on destroy - fin rq */ pass->body_length = asprintf(&pass->body, DC_LOGIN_FORMAT, i.client->email, i.client->password); @@ -300,7 +313,7 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun #endif info.protocol = dc_lws_protocols[0].name; info.local_protocol_name = info.protocol; - pass = calloc(1, sizeof(struct dc_lws_pass)); + pass = dc_lws_pass_init(); i.type = DC_API_WS_CB; i.status |= DC_SET_PASS | DC_SET_WS_ACTIVE; i.status &= ~DC_DESTROY_CB; /* this is only for http requests */ |