summaryrefslogtreecommitdiffstats
path: root/src/api.c
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2022-02-16 19:56:47 +0100
committerAnton Luka Šijanec <anton@sijanec.eu>2022-02-16 19:56:47 +0100
commit316a09bcf460fe762c41ea0e082d216120f087d0 (patch)
tree7d32655f3e4df44584afcf7ff91b152fa4d203cf /src/api.c
parentnekaj sem delal v šoli (diff)
downloaddiscord.c-316a09bcf460fe762c41ea0e082d216120f087d0.tar
discord.c-316a09bcf460fe762c41ea0e082d216120f087d0.tar.gz
discord.c-316a09bcf460fe762c41ea0e082d216120f087d0.tar.bz2
discord.c-316a09bcf460fe762c41ea0e082d216120f087d0.tar.lz
discord.c-316a09bcf460fe762c41ea0e082d216120f087d0.tar.xz
discord.c-316a09bcf460fe762c41ea0e082d216120f087d0.tar.zst
discord.c-316a09bcf460fe762c41ea0e082d216120f087d0.zip
Diffstat (limited to 'src/api.c')
-rw-r--r--src/api.c84
1 files changed, 53 insertions, 31 deletions
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: