summaryrefslogtreecommitdiffstats
path: root/src/api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/api.c')
-rw-r--r--src/api.c139
1 files changed, 63 insertions, 76 deletions
diff --git a/src/api.c b/src/api.c
index c5fb0b3..2344bf4 100644
--- a/src/api.c
+++ b/src/api.c
@@ -48,9 +48,9 @@ void dc_api_stack (struct dc_api_io i) { /* stack output struct to be delivered
}
unsigned long long int dc_calculate_permissions (struct dc_user * u, struct dc_channel * c) {
unsigned long long int p = 0; /* note: this is NOT according to server's implementation of */
- struct dc_role ** role = &c->guild->role; /* perm parsing, but should suffice 4 most cases */
- if (!*role) /* see struct dc_guild: if NULL then assume all permissions - a DM guild */
+ if (!c->guild) /* see struct dc_channel: if NULL then assume all permissions - a DM guild */
return DC_ALL_PERMISSIONS;
+ struct dc_role ** role = &c->guild->role; /* perm parsing, but should suffice 4 most cases */
while (*role) {
if (DC_ROLE_EVERYONE(*role)
|| dc_find_user((*role)->users, (*role)->users_length, u->id))
@@ -72,25 +72,53 @@ unsigned long long int dc_calculate_permissions (struct dc_user * u, struct dc_c
}
return p;
}
-struct dc_user * dc_parse_user (struct dc_user * dst, const cJSON * src) {
- char * cp;
- if (!dst)
- dst = dc_user_init();
- if ((cp = cJSON_GSV(cJSON_GOI(src, "username"))))
- dst->username = strdup(cp);
- if ((cp = cJSON_GSV(cJSON_GOI(src, "discriminator"))))
- dst->discriminator = atoi(cp);
- if ((cp = cJSON_GSV(cJSON_GOI(src, "id"))))
- dst->id = strtoull(cp, NULL, 10);
- if (!dst->username || dst->discriminator == -1) { /* it's quite useless to store only ids */
- dc_user_free(dst, DC_UNSET);
+struct dc_user * dc_parse_user (struct dc_program * p, const cJSON * s) {
+ if (!s) /* in case cJSON_GOIx returns NULL, it will most of the times, this isn't a bug */
+ return NULL;
+ char * c = cJSON_GSV(cJSON_GOI(s, "id"));
+ if (!c)
+ return NULL;
+ unsigned long long int i = strtoull(c, NULL, 10);
+ struct dc_user * u = dc_find_user(p->users, p->users_length, i);
+ if (!u) {
+ u = dc_user_init();
+ u->id = i;
+ dc_addr_user(p, DC_ISAE(p->users), u, 0);
+ }
+ if ((c = cJSON_GSV(cJSON_GOI(s, "username"))) && strlen(c)) {
+ free(u->username);
+ u->username = strdup(c);
+ }
+ if ((c = cJSON_GSV(cJSON_GOI(s, "discriminator"))))
+ u->discriminator = atoi(c);
+ return u;
+}
+struct dc_channel * dc_parse_channel (struct dc_program * p, const cJSON * s) {
+ cJSON * o;
+ if (!s) /* in case cJSON_GOIx returns NULL */
+ return NULL;
+ char * c = cJSON_GSV(cJSON_GOI(s, "id"));
+ if (!c)
return NULL;
+ if (!(DC_CHANNEL_SUPPORTED(cJSON_GNV(cJSON_GOI(s, "type"))))) return NULL;
+ unsigned long long int i = strtoull(c, NULL, 10);
+ struct dc_channel * u = dc_find_channel(p->channels, p->channels_length, i);
+ if (!u) {
+ u = dc_channel_init();
+ u->id = i;
+ dc_addr_channel(p, DC_ISAE(p->channels), u, 0);
}
- return dst;
+ if ((c = cJSON_GSV(cJSON_GOI(s, "name")))) {
+ free(u->name);
+ u->name = strdup(c);
+ }
+ if ((o = cJSON_GOI(s, "type")))
+ u->type = cJSON_GNV(o);
+ return u;
}
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! */
+ unsigned char buf[LWS_PRE+DC_LWS_BUF+1];
switch (rs) {
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR: /* TODO: handle and report somehow */
if (pass) {
@@ -250,78 +278,37 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us,
pass->api_io.client->ping_interval = cJSON_GNV(obj)/1000-1;
pass->api_io.client->last_ping = 1;
}
-#define DC_PARSEOBJ(w, object, also) if ((obj = object)) { \
- struct dc_##w * w; \
- if ((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 | DC_INCOMPLETE);\
- also \
- }
- DC_PARSEOBJ(user, cJSON_GOI2(json, "d", "user"),
- if (!pass->api_io.client->user)
- pass->api_io.client->user = user;
- )
- DC_PARSEOBJ(user, cJSON_GOI3(json, "d", "member", "user"),)
- DC_PARSEOBJ(user, cJSON_GOI2(json, "d", "author"),)
- DC_PARSEOBJ(user, cJSON_GOI3(json,
- "d", "referenced_message", "author"),)
-#define DC_PARSEARR(what, arr, also) cJSON_AFE(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_INCOMPLETE);\
- also \
- }
- DC_PARSEARR(user, cJSON_GOI2(json, "d", "users"),)
- DC_PARSEARR(user, cJSON_GOI2(json, "d", "mentions"),)
- DC_PARSEARR(user, cJSON_GOI3(json,
- "d", "referenced_message", "mentions"),)
- cJSON_AFE(obj, cJSON_GOI2(json, "d", "private_channels")) {
- if (/* !cJSON_GAS(cJSON_GOI(obj, "recipient_ids")) || */
- /* commented. DMs with 0 members're shown /\ */ !cJSON_GSV(cJSON_GOI(obj, "id")) ||
- !DC_CHANNEL_SUPPORTED(
- /* cJSON is called many times here but I don't care */ cJSON_GNV(cJSON_GOI(obj, "type"))))
- continue;
- struct dc_channel * ch = dc_channel_init();
- if ((st = cJSON_GSV(cJSON_GOI(obj, "name"))))
- ch->name = strdup(st);
- ch->type = cJSON_GNV(cJSON_GOI(obj, "type"));
- ch->id = strtoull(cJSON_GSV(cJSON_GOI(obj, "id")), NULL, 10);
- cJSON_AFE(obje, cJSON_GOI(obj, "recipient_ids")) {
- if (!(st = cJSON_GSV(obje)))
- continue;
- struct dc_user * part = dc_user_init();
- part->id = strtoull(st, NULL, 10);
- part = dc_add_user(
- DC_ISAE(pass->api_io.program->users),
- /* no replace here. stored user can be better. */ part, DC_MAY_FREE);
- DC_MR(ch->users); /* needn't dc_add here coz start */
- ch->users[ch->users_length++] = part; /* empty ch. */
- }
- ch = dc_addr_channel(pass->api_io.program,
- DC_ISAE(pass->api_io.program->channels), ch,
- /* replace here. ours is better - fresher. */ DC_MAY_FREE | DC_REPLACE | DC_INCOMPLETE);
- ch->guild = pass->api_io.client->guilds[0];
- }
+ struct dc_user * user = dc_parse_user(DC_PAIP, cJSON_GOI2(json, "d", "user"));
+ if (!pass->api_io.client->user)
+ pass->api_io.client->user = user;
+ dc_parse_user(DC_PAIP, cJSON_GOI3(json, "d", "member", "user"));
+ dc_parse_user(DC_PAIP, cJSON_GOI2(json, "d", "author"));
+ dc_parse_user(DC_PAIP, cJSON_GOI3(json, "d", "referenced_message", "author"));
+ cJSON_AFE(obj, cJSON_GOI2(json, "d", "users"))
+ dc_parse_user(DC_PAIP, obj);
+ cJSON_AFE(obj, cJSON_GOI2(json, "d", "mentions"))
+ dc_parse_user(DC_PAIP, obj);
+ cJSON_AFE(obj, cJSON_GOI3(json, "d", "referenced_message",
+ "mentions"))
+ dc_parse_user(DC_PAIP, obj);
+ cJSON_AFE(obj, cJSON_GOI2(json, "d", "private_channels"))
+ dc_parse_channel(DC_PAIP, obj);
cJSON_AFE(ob, cJSON_GOI2(json, "d", "merged_members")) {
obj = cJSON_GetArrayItem(ob, 0);
if (!cJSON_GAS(cJSON_GOI(obj, "roles")))
continue;
struct dc_user * user = dc_user_init();
user->id = strtoull(cJSON_GSV(cJSON_GOI(obj, "user_id")),
- NULL, 10);
- user = dc_addr_user(
+ NULL, 10);
+ user = dc_addr_user(DC_PAIP,
DC_ISAE(pass->api_io.program->users), user,
- /* again, no replace here, only ID */ DC_MAY_FREE);
+ DC_MAY_FREE); /* no replace - we only have ID */
cJSON_AFE(obje, cJSON_GOI(obj, "roles")) {
if (!(st = cJSON_GSV(obje)))
continue;
struct dc_role * role = dc_role_init();
role->id = strtoull(st, NULL, 10);
- role = dc_addr_role(
+ role = dc_addr_role(DC_PAIP,
DC_ISAE(pass->api_io.program->roles),
role, DC_MAY_FREE);
/* role may have users. NO FREE! */ dc_add_user(DC_ISAE(role->users), user, DC_UNSET);