From 316a09bcf460fe762c41ea0e082d216120f087d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20Luka=20=C5=A0ijanec?= Date: Wed, 16 Feb 2022 19:56:47 +0100 Subject: i suppose parsing json is easyer with cjson than lejp (: --- .gitignore | 1 + Makefile | 2 +- src/api.c | 84 +++++++++++++++++++++++++++++++++++-------------------- src/h.c | 8 ++++-- src/identify.json | 2 +- src/json.c | 13 +++++---- src/main.c | 10 ++++++- 7 files changed, 78 insertions(+), 42 deletions(-) diff --git a/.gitignore b/.gitignore index 634b716..9b05aed 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ a.out valgrind-out.txt .gdb_history out.txt +core # files I like to keep in my WD src.old/ src/ui.glade~ diff --git a/Makefile b/Makefile index 290da8e..19d2c60 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ O=0 CFLAGS += -Wextra -Wall -pedantic -g -O$O -Itmp -Isrc -I. -odiscord.c -Wno-unused-parameter -rdynamic -finput-charset=UTF-8 -fextended-identifiers LIBS += -lm CFLAGS += $(shell pkg-config --cflags libwebsockets) $(shell pkg-config --cflags gtk+-3.0) $(shell pkg-config --cflags gmodule-export-2.0) -LIBS += $(shell pkg-config --libs libwebsockets) $(shell pkg-config --libs gtk+-3.0) $(shell pkg-config --libs gmodule-export-2.0) +LIBS += $(shell pkg-config --libs libwebsockets) $(shell pkg-config --libs gtk+-3.0) $(shell pkg-config --libs gmodule-export-2.0) -lcjson CC=cc VGARGS += --leak-check=full --track-origins=yes --verbose --log-file=valgrind-out.txt --suppressions=/usr/share/glib-2.0/valgrind/glib.supp --suppressions=tmp/gtk.supp --suppressions=/usr/lib/i386-linux-gnu/valgrind/default.supp --suppressions=tmp/gnome.supp --show-leak-kinds=definite --suppressions=misc/misc.supp --suppressions=/usr/share/gtk-3.0/valgrind/gtk.supp --leak-resolution=low # notparallel ker morajo biti ukazi izvedeni po vrsti, mkdir se mora zgoditi pred xxd recimo. diff --git a/src/api.c b/src/api.c index 4520888..fbd9495 100644 --- a/src/api.c +++ b/src/api.c @@ -59,7 +59,7 @@ struct dc_user * dc_parse_user (struct dc_user * dst, const cJSON * src) { if (!dst) dst = dc_user_init(); if ((cp = cJSON_GetStringValue(cJSON_GetObjectItem(src, "username")))) - dst->user = strdup(cp); + dst->username = strdup(cp); if ((cp = cJSON_GetStringValue(cJSON_GetObjectItem(src, "discriminator")))) dst->discriminator = atoi(cp); else @@ -207,42 +207,63 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us, fprintf(stdout, "%.*s", len, (const unsigned char *) in); char * serialized = dc_json(pass->json, in, len); while (serialized) { - serialized = dc_json(handler, NULL, 0); cJSON * json = cJSON_Parse(serialized); + cJSON * obj; + char * st; + if (getenv("DC_J")) { + st = cJSON_Print(json); + if (st) { + printf("%s\n", st); + cJSON_free(st); + } else + fprintf(stderr, "_RECEIVE: _Print NULL: %s\n", serialized); + } pass->api_io.client->last_packet = cJSON_GetNumberValue(cJSON_GetObjectItem(json, "s")); - switch (cJSON_GetNumberValue(cJSON_GetObjectItem(json, "op"))) { - case DC_PING: - client->last_ping = 0; - dc_handle_ping(pass->api_io, NULL); - default: - break; + if (cJSON_GetNumberValue(cJSON_GetObjectItem(json, "op")) + == DC_PING) { + pass->api_io.client->last_ping = 0; + dc_handle_ping(pass->api_io, NULL); } - double db; - if ((db = cJSON_GetNumberValue(cJSON_GetObjectItem2(json, - "d", "heartbeat_interval")) != NAN)) { - pass->api_io.client->ping_interval = pi/1000-1; - pass->api_io.client->last_ping = time(NULL); + if (cJSON_IsNumber((obj + = cJSON_GetObjectItem2(json, "d", "heartbeat_interval")))) { + pass->api_io.client->ping_interval + = cJSON_GetNumberValue(obj)/1000-1; + pass->api_io.client->last_ping = 1; } - cJSON * obj; - if ((obj = cJSON_GetObjectItem(json, "d", "user"))) { /* parsing */ - struct dc_user * user; /* single */ - user = dc_parse_user(NULL, obj); /* user */ - user = dc_addr_user(program, NULL, user, - DC_MAY_FREE | DC_REPLACE); - if (!pass->api_io.client->user) /* 1. user is us in d.user */ - pass->api_io.client->user = user; +#define DC_PARSEOBJ(w, object, also) if ((obj = object)) { \ + struct dc_##w * w; \ + w = dc_parse_##w(NULL, obj); \ + w = dc_addr_##w(pass->api_io.program, \ + DC_ISAE(pass->api_io.program->w##s), w, \ + DC_MAY_FREE | DC_REPLACE);\ + also \ } - cJSON_ArrayForEach(user, cJSON_GetObjectItem(json, "users")) { - struct dc_user * user = dc_parse_user(NULL, obj); - if (user) /* parsing an array of users */ - dc_addr_user(program, NULL, user, - DC_MAY_FREE | DC_REPLACE); + DC_PARSEOBJ(user, cJSON_GetObjectItem2(json, "d", "user"), + if (!pass->api_io.client->user) pass->api_io.client->user = user;) + DC_PARSEOBJ(user, cJSON_GetObjectItem3(json, "d", "member", "user"),) + DC_PARSEOBJ(user, cJSON_GetObjectItem2(json, "d", "author"), ) + DC_PARSEOBJ(user, cJSON_GetObjectItem3(json, "d", + "referenced_message", "author"), ) +#define DC_PARSEARR(what, arr) cJSON_ArrayForEach(obj, arr) { \ + struct dc_##what * what = dc_parse_##what(NULL, obj); \ + if (!what) \ + continue; \ + dc_addr_##what(pass->api_io.program, \ + DC_ISAE(pass->api_io.program->what##s), \ + what, DC_MAY_FREE | DC_REPLACE); \ + } + DC_PARSEARR(user, cJSON_GetObjectItem2(json, "d", "users")); + DC_PARSEARR(user, cJSON_GetObjectItem2(json, "d", "mentions")); + DC_PARSEARR(user, cJSON_GetObjectItem3(json, "d", + "referenced_message", "mentions")); +#define DC_PTYP(t) ((st = cJSON_GetStringValue(cJSON_GetObjectItem(json, "t"))) && !strcmp(st, t)) + if (DC_PTYP("MESSAGE_CREATE")) { + } -#define DC_PTYP(t) (!strcmp(cJSON_GetStringValue(cJSON_GetObjectItem(json, "t")), t)) - if (DC_PTYP("MESSAGE_CREATE")); cJSON_Delete(json); json = NULL; + serialized = dc_json(pass->json, NULL, 0); } break; case LWS_CALLBACK_CLIENT_WRITEABLE: /* invoke w/ lws_callback_on_writeable(wsi)4ws */ @@ -333,7 +354,8 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun DC_STACK_RETURN(DC_VERIFICATION_NEEDED); char * cp, * c2; #define DC_LTS "\"token\": \"" - if (!(cp = strstr(i.pass->body, DC_LTS)+strlen(DC_LTS)) || !(c2 = strchr(cp+strlen(DC_LTS), '"'))) + if ((size_t) (cp = strstr(i.pass->body, DC_LTS)+strlen(DC_LTS)) + == strlen(DC_LTS) || !(c2 = strchr(cp+strlen(DC_LTS), '"'))) DC_STACK_RETURN(DC_ERROR); c2[0] = '\0'; /* body is on heap, we can edit it */ free(i.client->authorization); /* in case we set it previously */ @@ -395,12 +417,12 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun } dc_api_stack(i); /* cl.stat is either NET_ERROR 4 closed or error or OK 4 esta */ break; + case DC_API_USER: + break; case DC_API_REGISTER: break; case DC_API_STATUS: break; - case DC_API_USER: - break; case DC_API_ROLE: break; case DC_API_NONE: diff --git a/src/h.c b/src/h.c index 83fafd8..04fb7e9 100644 --- a/src/h.c +++ b/src/h.c @@ -668,6 +668,8 @@ enum dc_status dc_handle_ping (struct dc_api_io io, void * userdata) { /* tries continue; if (!pass->wsi) continue; + fprintf(stderr, "dc_handle_ping: interval %lu last %lu\n", + client->ping_interval, client->last_ping); if (!userdata) { /* so packet parser iow server may request an immediate ping */ if (client->ping_interval && client->last_ping + client->ping_interval < time(NULL)) client->last_ping = time(NULL); @@ -753,7 +755,8 @@ void dc_api_stack (struct dc_api_io); struct dc_##x * us; \ assert(u); \ if ((us = dc_find_##x(*p, *l, u->id))) { \ - /* fprintf(stderr, "debug: %s found already existing member\n", __func__); */ \ + if (getenv("DC_I")) \ + fprintf(stderr, "debug: %s found already existing member\n", __func__); \ if (us == u) \ return us; \ if (s & DC_REPLACE) { \ @@ -772,7 +775,8 @@ void dc_api_stack (struct dc_api_io); *so = ceil(*so*DC_REALLOC_K); \ *p = realloc(*p, sizeof(**p) * *so); \ } \ - /* fprintf(stderr, "debug: %s inserted into ISA\n", __func__); */ /* too much */ \ + if (getenv("DC_I")) \ + fprintf(stderr, "debug: %s inserted into ISA\n", __func__); \ (*p)[(*l)++] = u; \ return u; \ } diff --git a/src/identify.json b/src/identify.json index bf20503..332fd49 100644 --- a/src/identify.json +++ b/src/identify.json @@ -1 +1 @@ -{"op":2,"d":{"token":"%s","capabilities":125,"intents":32767,"properties":{"os":"Linux","browser":"Chrome","device":"","system_locale":"en-US","browser_user_agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36","browser_version":"90.0.4430.212","os_version":"","referrer":"http://of.sijanec.eu:7327/","referring_domain":"of.sijanec.eu:7327","referrer_current":"","referring_domain_current":"","release_channel":"stable","client_build_number":97662,"client_event_source":null},"presence":{"status":"online","since":0,"activities":[{"name":"Custom Status","type":4,"state":"mrt5hg","emoji":null}],"afk":false},"compress":false,"client_state":{"guild_hashes":{},"highest_last_message_id":"0","read_state_version":0,"user_guild_settings_version":-1}}} +{"op":2,"d":{"token":"%s","capabilities":125,"intents_break_content":32767,"properties":{"os":"Linux","browser":"Chrome","device":"","system_locale":"en-US","browser_user_agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36","browser_version":"90.0.4430.212","os_version":"","referrer":"http://of.sijanec.eu:7327/","referring_domain":"of.sijanec.eu:7327","referrer_current":"","referring_domain_current":"","release_channel":"stable","client_build_number":97662,"client_event_source":null},"presence":{"status":"online","since":0,"activities":[{"name":"Custom Status","type":4,"state":"mrt5hg","emoji":null}],"afk":false},"compress":false,"client_state":{"guild_hashes":{},"highest_last_message_id":"0","read_state_version":0,"user_guild_settings_version":-1}}} diff --git a/src/json.c b/src/json.c index 4fcfcf5..aae6f5d 100644 --- a/src/json.c +++ b/src/json.c @@ -24,8 +24,9 @@ char * dc_json (struct dc_json * j, const char * in, int ln) { /* detects start/ ln = strlen(in); i = bufstrlen; if (bufstrlen + ln > j->bufcap) - j->buf = realloc(j->buf, (j->bufcap=(bufstrlen+ln)*DC_REALLOC_K)*sizeof(char)); - strcpy(j->buf+bufstrlen, in); + j->buf = realloc(j->buf, (j->bufcap=(bufstrlen+ln+2)*DC_REALLOC_K)*sizeof(char)); + strncpy(j->buf+bufstrlen, in, ln); + j->buf[bufstrlen+ln] = '\0'; bufstrlen += ln; while (i < bufstrlen) { if (j->instr) { @@ -40,9 +41,9 @@ char * dc_json (struct dc_json * j, const char * in, int ln) { /* detects start/ } if (!escaped) { j->instr = 0; - fprintf(stderr, "dc_json: j->instr = 0\n"); - } else - fprintf(stderr, "dc_json: escaped\n"); + /* fprintf(stderr, "dc_json: j->instr = 0\n"); */ + } /* else + fprintf(stderr, "dc_json: escaped\n"); */ } goto next; } @@ -54,7 +55,7 @@ char * dc_json (struct dc_json * j, const char * in, int ln) { /* detects start/ break; case '"': j->instr++; - fprintf(stderr, "dc_json: j->instr++\n"); + /* fprintf(stderr, "dc_json: j->instr++\n"); */ break; case '}': case ']': diff --git a/src/main.c b/src/main.c index 02b27da..7c3f3b9 100644 --- a/src/main.c +++ b/src/main.c @@ -41,8 +41,16 @@ int main (int argc, char * argv[]) { i.attached_function->type = DC_API_MESSAGE; i.attached_function->function = dc_print_message; dc_api_i(i); - while (!dc_interrupted) + while (!dc_interrupted) { i = dc_api_o(i); + switch (i.type) { + case DC_API_USER: + fprintf(stderr, "DC_API_USER: %s#%04d %llu\n", i.user->username, i.user->discriminator, i.user->id); + break; + default: + break; + } + } /* dc_ui(argc, argv); */ dc_program_free(p, DC_UNSET); return 0; -- cgit v1.2.3