diff options
Diffstat (limited to 'src/api.c')
-rw-r--r-- | src/api.c | 167 |
1 files changed, 141 insertions, 26 deletions
@@ -30,14 +30,10 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn struct dc_lws_pass * pass = ctx->user; /* correct. op and t should come first, etc. */ struct dc_client * client = pass->api_io.client; struct dc_program * program = pass->api_io.program; - struct dc_user * user; /* don't confuse this with client->user, this is just a var for you use */ pass->json_reason = reason; if (reason == LEJPCB_FAILED || reason == LEJPCB_COMPLETE || reason == LEJPCB_START) { - if (pass->parsing_status) - *pass->parsing_status &= ~DC_IN_PROGRESS; DC_API_IO_GC(pass->api_io); pass->packet = DC_NONE; - pass->parsing_status = NULL; return '\0'; } if (getenv("DC_J")) /* print json to standard error */ @@ -89,8 +85,6 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn if (client->user->status & DC_INCOMPLETE) { /* we can't just check if */ client->user->discriminator = atoi(ctx->buf); /* !discriminat, */ client->user->status &= ~DC_INCOMPLETE; /* because 0 is allowd */ - if (getenv("DC_C")) - dc_interrupted++; } break; default: @@ -98,19 +92,26 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn } return '\0'; /* because we use same checks for parsing other users GUILD_MEMBER_UPDATE */ } - if ((path == DC_JSON_ME || path == DC_JSON_USER || path == DC_JSON_MESSAGE_AUTHOR || path == DC_JSON_MESSAGE_REFOBJ_AUTHOR || path == DC_JSON_MESSAGE_MENTION_USER || path == DC_JSON_MESSAGE_REFOBJ_MENTION_USER) && reason == LEJPCB_OBJECT_START) { /* user parsing start */ + if ((path == DC_JSON_ME || path == DC_JSON_USER || path == DC_JSON_MESSAGE_AUTHOR || path == DC_JSON_MESSAGE_REFOBJ_AUTHOR || path == DC_JSON_MESSAGE_MENTION_USER || path == DC_JSON_MESSAGE_REFOBJ_MENTION_USER || path == DC_JSON_MEMBER) && reason == LEJPCB_OBJECT_START) { /* dc_user_free(pass->api_io.user); */ /* parser branches MUST ALWAYS set to 0 */ pass->api_io.user = dc_user_init(); /* and possibly free api_io members after! */ pass->api_io.user->status |= DC_IN_PROGRESS; /* if we never get here again */ } - if ((path == DC_JSON_ME || path == DC_JSON_USER || path == DC_JSON_MESSAGE_AUTHOR || path == DC_JSON_MESSAGE_REFOBJ_AUTHOR || path == DC_JSON_MESSAGE_MENTION_USER || path == DC_JSON_MESSAGE_REFOBJ_MENTION_USER) && reason == LEJPCB_OBJECT_END) { /* if it was just a user */ + if ((path == DC_JSON_ME || path == DC_JSON_USER || path == DC_JSON_MESSAGE_AUTHOR || path == DC_JSON_MESSAGE_REFOBJ_AUTHOR || path == DC_JSON_MESSAGE_MENTION_USER || path == DC_JSON_MESSAGE_REFOBJ_MENTION_USER || path == DC_JSON_MEMBER) && reason == LEJPCB_OBJECT_END) { + if (getenv("DC_R")) + assert(pass->api_io.user->id); + if (!pass->api_io.user->id) { + dc_user_free(pass->api_io.user, DC_UNSET); + pass->api_io.user = NULL; + return '\0'; + } pass->api_io.user->status &= ~DC_IN_PROGRESS; pass->api_io.user = dc_addr_user(program, DC_ISAE(program->users), pass->api_io.user, DC_MAY_FREE); - if (path != DC_JSON_MESSAGE_AUTHOR && path != DC_JSON_MESSAGE_REFOBJ_AUTHOR && path != DC_JSON_MESSAGE_MENTION_USER && path != DC_JSON_MESSAGE_REFOBJ_MENTION_USER && !getenv("DC_0") /* override if those are disabled */) /* those expect a user parsed */ + if (path != DC_JSON_MESSAGE_AUTHOR && path != DC_JSON_MESSAGE_REFOBJ_AUTHOR && path != DC_JSON_MESSAGE_MENTION_USER && path != DC_JSON_MESSAGE_REFOBJ_MENTION_USER && getenv("DC_0") /* override if those are disabled */) /* those expect a user parsed */ pass->api_io.user = NULL; /* when implementing parser: handle OBJECT_END */ else /* if something goes wrong - if those handlers do not null user, IO_MEMB_GC will */ pass->api_io.user->status |= DC_EXPLICIT_NULL; /* but will not free. */ - } /* user parsing end */ + } if ((path == DC_JSON_DM || path == DC_JSON_GUILD_CHANNEL) && reason == LEJPCB_OBJECT_START) { pass->api_io.channel = dc_channel_init(); pass->api_io.channel->status |= DC_IN_PROGRESS; @@ -119,6 +120,12 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn struct dc_channel ** channel = &client->guilds[0]->channel; /* 1. guild = DMs */ while (*channel) channel = &(*channel)->next; + if (!pass->api_io.channel->id) { + dc_channel_free(pass->api_io.channel, DC_UNSET); + pass->api_io.channel = NULL; + return '\0'; + } + pass->api_io.channel->next = NULL; pass->api_io.channel->guild = client->guilds[0]; /* client->guilds[0] always exists */ pass->api_io.channel->status &= ~DC_IN_PROGRESS; free(pass->api_io.channel->topic); @@ -130,8 +137,9 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn strcat(pass->api_io.channel->topic, pass->api_io.channel->users[i]->username); strcat(pass->api_io.channel->topic, buf); } - if ((struct dc_channel * ch = dc_find_channel(program->channels, program->channels_length, pass->api_io.channel->id))) - DC_TRANSFER_CHANNEL(pass->api_io.channel->id, ch); + struct dc_channel * ch; + if ((ch = dc_find_channel(program->channels, program->channels_length, pass->api_io.channel->id))) + DC_TRANSFER_CHANNEL(pass->api_io.channel, ch); pass->api_io.channel = dc_addr_channel(program, DC_ISAN, pass->api_io.channel, DC_MAY_FREE | DC_REPLACE); if (!dc_find_ll_channel(client->guilds[0]->channel, pass->api_io.channel->id)) { fprintf(stderr, "new DM id=%llu (:\n", pass->api_io.channel->id); @@ -141,28 +149,87 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn pass->api_io.channel = NULL; /* we're done, NULL it or PROBLEMS WILL APPEAR!!! */ } if (path == DC_JSON_GUILD_CHANNEL && reason == LEJPCB_OBJECT_END) { + if (!pass->api_io.channel->id || !DC_CHANNEL_SUPPORTED(pass->api_io.channel->type)) { + dc_channel_free(pass->api_io.channel, DC_UNSET); + pass->api_io.channel = NULL; + return '\0'; + } struct dc_channel ** channel = &pass->api_io.guild->channel; while (*channel) channel = &(*channel)->next; - pass->api_io.channel = dc_addr_channel(program, DC_ISAN, pass->api_io.channel, DC_MAY_FREE | DC_REPLACE); - pass->api_io.channel = NULL; + struct dc_channel * ch; + if ((ch = dc_find_channel(program->channels, program->channels_length, pass->api_io.channel->id))) + DC_TRANSFER_CHANNEL(pass->api_io.channel, ch); + pass->api_io.channel = *channel = dc_addr_channel(program, DC_ISAN, pass->api_io.channel, DC_MAY_FREE | DC_REPLACE); + struct dc_permission ** pe = &pass->api_io.channel->permission; + while (*pe) { /* fix all permission pointers to channel to this new channel */ + (*pe)->channel = pass->api_io.channel; + pe = &(*pe)->next; + } + pass->api_io.channel = NULL; /* we're done, NULL it or */ } - if (path == DC_JSON_GUILD && reason == LEJPCB_OBJECT_START) { /* guild parsing start */ + if (path == DC_JSON_GUILD && reason == LEJPCB_OBJECT_START) { pass->api_io.guild = dc_guild_init(); pass->api_io.guild->status |= DC_IN_PROGRESS; } if (path == DC_JSON_GUILD && reason == LEJPCB_OBJECT_END) { + if (!pass->api_io.guild->id) { + dc_guild_free(pass->api_io.guild, DC_UNSET); + pass->api_io.guild = NULL; + return '\0'; + } pass->api_io.guild->status &= ~DC_IN_PROGRESS; - pass->api_io.guild = dc_addr_guild(program, DC_ISAE(program->guilds), pass->api_io.guild, DC_MAY_FREE | DC_REPLACE); /* when we replace, the new guild's channels' ptrs */ + pass->api_io.guild = dc_addr_guild(program, DC_ISAE(program->guilds), pass->api_io.guild, DC_MAY_FREE | DC_REPLACE); /* when we replace, new guild's chs' & roles' ptrs */ struct dc_channel ** channel = &pass->api_io.guild->channel; /* to guild will be inv. */ + struct dc_role ** role = &pass->api_io.guild->role; while (*channel) { - *(channel)->guild = pass->api_io.guild; + (*channel)->guild = pass->api_io.guild; channel = &(*channel)->next; } - dc_add_guild(program, DC_ISAE(client->guilds), pass->api_io.guild, DC_UNSET); + while (*role) { + (*role)->guild = pass->api_io.guild; + role = &(*role)->next; + } + struct dc_guild * gu; + if ((gu = dc_find_guild(program->guilds, program->guilds_length, pass->api_io.guild->id))) + DC_TRANSFER_GUILD(pass->api_io.guild, gu); + dc_add_guild(DC_ISAE(client->guilds), pass->api_io.guild, DC_UNSET); pass->api_io.guild = NULL; /* we're done, NULL it or PROBLEMS WILL APPEAR!!! */ - } /* guild parsing end */ - if (reason & LEJP_FLAG_CB_IS_VALUE) + } +#if 0 + if (path == DC_JSON_GUILD_ROLE && reason == LEJPCB_OBJECT_START) { + pass->api_io.role = dc_role_init(); + pass->api_io.role->status |= DC_IN_PROGRESS; + } + if (path == DC_JSON_GUILD_ROLE && reason == LEJPCB_OBJECT_END) { + if (!pass->api_io.role->id) { + dc_role_free(pass->api_io.role, DC_UNSET); + pass->api_io.role = NULL; + return '\0'; + } + pass->api_io.role->status &= ~DC_IN_PROGRESS; + struct dc_role * ro; + if ((ro = dc_find_role(program->roles, program->roles_length, pass->api_io.role->id))) + dc_transfer_role(pass->api_io.role, ro); + pass->api_io.role = dc_addr_role(program, DC_ISAE(program->roles), pass->api_io.role, DC_MAY_FREE | DC_REPLACE); + struct dc_role ** role; + role = &pass->api_io.guild->role; + while (*role) + role = &(*role)->next; + *role = pass->api_io.guild->role; + pass->api_io.role->guild = pass->api_io.guild; + if (pass->api_io.role->name && !strncmp(pass->api_io.role->name, "@everyone", strlen(pass->api_io.role->name))) + pass->api_io.role->status |= DC_EVERYONE; + pass->api_io.role = NULL; /* we're done, NULL it or PROBLEMS WILL APPEAR!!! */ + } + if (path == DC_JSON_MEMBERSHIP && (reason == LEJPCB_OBJECT_END || reason == LEJPCB_OBJECT_START)) + pass->api_io.id = 0; +#endif + if (reason & LEJP_FLAG_CB_IS_VALUE) { + struct dc_user * user; /* this is just a var for you use */ + struct dc_role * role; /* now we rely on -Wuninitialized to detect weird stuff */ + unsigned long long int id; /* so don't initialize them here, that'd be wrong */ + int št; switch (path) { case DC_JSON_FRIEND: /* we assume we get users[] before relationships[] */ user = dc_find_user(program->users, program->users_length, strtoull(ctx->buf, NULL, 10)); @@ -176,16 +243,19 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn break; case DC_JSON_ME_USERNAME: /* we are always checking for user because evil srv */ case DC_JSON_USER_NAME: /* might insert dots in keys and prevent OBJECT_CREATE */ + case DC_JSON_MEMBER_USERNAME: if (pass->api_io.user && !pass->api_io.user->username) /* no trust srv */ pass->api_io.user->username = strdup(ctx->buf); break; case DC_JSON_ME_ID: case DC_JSON_USER_ID: + case DC_JSON_MEMBER_ID: if (pass->api_io.user) pass->api_io.user->id = strtoll(ctx->buf, NULL, 10); break; case DC_JSON_ME_DISCRIMINATOR: case DC_JSON_USER_DISCRIMINATOR: + case DC_JSON_MEMBER_DISCRIMINATOR: if (pass->api_io.user) pass->api_io.user->discriminator = atoi(ctx->buf); break; /* yeah, we don't care about nicknames */ @@ -201,11 +271,11 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn break; case DC_JSON_DM_NAME: case DC_JSON_GUILD_CHANNEL_NAME: - if (pass->api_io.channel) + if (pass->api_io.channel && !pass->api_io.channel->name) pass->api_io.channel->name = strdup(ctx->buf); break; case DC_JSON_GUILD_NAME: - if (pass->api_io.guild) + if (pass->api_io.guild && !pass->api_io.guild->name) pass->api_io.guild->name = strdup(ctx->buf); break; case DC_JSON_GUILD_ID: @@ -213,15 +283,62 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn pass->api_io.guild->id = strtoull(ctx->buf, NULL, 10); break; case DC_JSON_GUILD_CHANNEL_TOPIC: - if (pass->api_io.channel) + if (pass->api_io.channel && !pass->api_io.channel->topic) pass->api_io.channel->topic = strdup(ctx->buf); break; +#if 0 + case DC_JSON_GUILD_ROLE_ID: + if (pass->api_io.role) + pass->api_io.role->id = strtoull(ctx->buf, NULL, 10); + break; + case DC_JSON_GUILD_ROLE_NAME: + if (pass->api_io.role && !pass->api_io.role->name) + pass->api_io.role->name = strdup(ctx->buf); + break; + case DC_JSON_GUILD_ROLE_PERMISSION: + if (pass->api_io.role) { + if (atoi(ctx->buf) & DC_ADMIN) + pass->api_io.role->permissions = DC_ALL_PERMISSIONS; + else + pass->api_io.role->permissions = atoi(ctx->buf); + } + break; + case DC_JSON_MEMBERSHIP_USER: + pass->api_io.id = strtoull(ctx->buf, NULL, 10); + break; + case DC_JSON_MEMBERSHIP_ROLE: + case DC_JSON_MEMBER_ROLE: + if (!pass->api_io.id) /* we assume we get ID first */ + if (!pass->api_io.user || !(pass->api_io.id = pass->api_io.user->id)) + break; + if (!(id = strtoull(ctx->buf, NULL, 10))) + break; + št = 0; + if (!(user = dc_find_user(program->users, program->users_length, pass->api_io.id))) { + št |= 1; + user = dc_user_init(); + } + if (!(role = dc_find_role(program->roles, program->roles_length, id))) { + št |= 2; + role = dc_role_init(); + role->id = id; + } + dc_add_user(DC_ISAE(role->users), user, DC_UNSET); + if (št & 1) + dc_addr_user(program, DC_ISAE(program->users), user, DC_UNSET); + if (št & 2) + dc_addr_role(program, DC_ISAE(program->roles), role, DC_UNSET); + fprintf(stderr, "new role membership user %s#%d (usr=%llu role=%llu)\n", user->username ? user->username : "", user->discriminator, user->id, role->id); + pass->api_io.id = 0; + break; +#endif default: break; } + } if (pass->packet == DC_NONE) /* useless to do anything BELOW if we haven't recvd packet type */ return '\0'; - switch (pass->packet) { /* we fill in structs, set DC_INCOMPLETE and pass->parsing_status */ + switch (pass->packet) { case DC_MESSAGE_CREATE: break; default: @@ -476,8 +593,6 @@ void dc_api_i (struct dc_api_io i) { /* this function does not call attached fun goto ws; /* we could call dc_api_i but that just fills stack */ break; case DC_API_WS: - if (getenv("DC_R")) /* detect _WS calls */ - raise(SIGINT); fprintf(stderr, "DC_API_WS called\n"); ws: memset(&info, 0, sizeof(info)); |