summaryrefslogtreecommitdiffstats
path: root/src/api.c
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2021-09-24 22:53:01 +0200
committerAnton Luka Šijanec <anton@sijanec.eu>2021-09-24 22:53:01 +0200
commit605cb0ca371517c7a5cff79b4ef2dfdfe22ab4fe (patch)
treeebe76db9117a7f64f7e958fb264c05f45825d4c6 /src/api.c
parentshit (diff)
downloaddiscord.c-605cb0ca371517c7a5cff79b4ef2dfdfe22ab4fe.tar
discord.c-605cb0ca371517c7a5cff79b4ef2dfdfe22ab4fe.tar.gz
discord.c-605cb0ca371517c7a5cff79b4ef2dfdfe22ab4fe.tar.bz2
discord.c-605cb0ca371517c7a5cff79b4ef2dfdfe22ab4fe.tar.lz
discord.c-605cb0ca371517c7a5cff79b4ef2dfdfe22ab4fe.tar.xz
discord.c-605cb0ca371517c7a5cff79b4ef2dfdfe22ab4fe.tar.zst
discord.c-605cb0ca371517c7a5cff79b4ef2dfdfe22ab4fe.zip
Diffstat (limited to 'src/api.c')
-rw-r--r--src/api.c47
1 files changed, 34 insertions, 13 deletions
diff --git a/src/api.c b/src/api.c
index 43aca84..c405cff 100644
--- a/src/api.c
+++ b/src/api.c
@@ -107,10 +107,27 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn
}
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 */
- 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. */
+ switch (path) {
+ case DC_JSON_MESSAGE_AUTHOR:
+ if (pass->api_io.message)
+ pass->api_io.message->user = pass->api_io.user;
+ break;
+ case DC_JSON_MESSAGE_REFOBJ_AUTHOR:
+ if (pass->api_io.message && pass->api_io.message->reply)
+ pass->api_io.message->reply->user = pass->api_io.user;
+ break;
+ case DC_JSON_MESSAGE_MENTION_USER:
+ if (pass->api_io.message)
+ dc_add_user(DC_ISAE(pass->api_io.message->users), pass->api_io.user, DC_UNSET);
+ break;
+ case DC_JSON_MESSAGE_REFOBJ_MENTION_USER:
+ if (pass->api_io.message && pass->api_io.message->reply)
+ dc_add_user(DC_ISAE(pass->api_io.message->reply->users), pass->api_io.user, DC_UNSET);
+ break;
+ default:
+ break;
+ }
+ pass->api_io.user = NULL;
}
if ((path == DC_JSON_DM || path == DC_JSON_GUILD_CHANNEL) && reason == LEJPCB_OBJECT_START) {
pass->api_io.channel = dc_channel_init();
@@ -140,9 +157,10 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn
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);
+ pass->api_io.channel = dc_addr_channel(program, DC_ISAE(program->channels), 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);
+ pass->api_io.channel->next = NULL;
*channel = pass->api_io.channel;
}
pass->api_io.channel->guild = client->guilds[0];
@@ -160,7 +178,12 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn
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);
+ pass->api_io.channel = dc_addr_channel(program, DC_ISAE(program->channels), pass->api_io.channel, DC_MAY_FREE | DC_REPLACE);
+ if (!dc_find_ll_channel(pass->api_io.guild->channel, pass->api_io.channel->id)) {
+ fprintf(stderr, "new channel id=%llu (:\n", pass->api_io.channel->id);
+ pass->api_io.channel->next = NULL;
+ *channel = pass->api_io.channel;
+ }
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;
@@ -196,7 +219,6 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn
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!!! */
}
-#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;
@@ -211,7 +233,7 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn
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);
+ pass->api_io.role = dc_add_role(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)
@@ -224,7 +246,6 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn
}
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 */
@@ -286,7 +307,6 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn
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);
@@ -297,10 +317,10 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn
break;
case DC_JSON_GUILD_ROLE_PERMISSION:
if (pass->api_io.role) {
- if (atoi(ctx->buf) & DC_ADMIN)
+ if (strtoull(ctx->buf, NULL, 10) & DC_ADMIN)
pass->api_io.role->permissions = DC_ALL_PERMISSIONS;
else
- pass->api_io.role->permissions = atoi(ctx->buf);
+ pass->api_io.role->permissions = strtoull(ctx->buf, NULL, 10);
}
break;
case DC_JSON_MEMBERSHIP_USER:
@@ -331,7 +351,6 @@ signed char dc_json_cb (struct lejp_ctx * ctx, char reason) { /* to prevent warn
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;
}
@@ -439,6 +458,8 @@ static int dc_lws_cb (struct lws * wsi, enum lws_callback_reasons rs, void * us,
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 */