Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix set_on_command #34

Merged
merged 2 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/discord-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ struct discord_gateway {
struct discord_gateway_cmd_cbs *pool;
/** amount of command/callback pairs in pool */
size_t amt;
/** actual size of command/callback pairs in pool */
size_t cap;
/** fallback function incase prefix matches but command doesn't */
struct discord_gateway_cmd_cbs on_default;
/** user's callbacks */
Expand Down
32 changes: 23 additions & 9 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,32 @@ discord_set_on_command(struct discord *client,
client->gw.cmds.on_default.cb = callback;
return; /* EARLY RETURN */
}

size_t index = 0;
const size_t command_len = strlen(command);
for (; index < client->gw.cmds.amt; index++)
if (command_len == client->gw.cmds.pool[index].size)
if (0 == strcmp(command, client->gw.cmds.pool[index].start))
goto modify;
lcsmuller marked this conversation as resolved.
Show resolved Hide resolved
if (index == client->gw.cmds.cap) {
size_t cap = 8;
while (cap <= index) cap <<= 1;

void *tmp =
realloc(client->gw.cmds.pool, cap * sizeof(*client->gw.cmds.pool));
if (tmp) {
client->gw.cmds.pool = tmp;
client->gw.cmds.cap = cap;
} else
return;
}
++client->gw.cmds.amt;
client->gw.cmds.pool =
realloc(client->gw.cmds.pool,
client->gw.cmds.amt * sizeof(*client->gw.cmds.pool));

client->gw.cmds.pool[client->gw.cmds.amt - 1].start = command;
client->gw.cmds.pool[client->gw.cmds.amt - 1].size = strlen(command);
client->gw.cmds.pool[client->gw.cmds.amt - 1].cb = callback;
client->gw.cmds.pool[index].start = strdup(command);
client->gw.cmds.pool[index].size = command_len;
modify:
client->gw.cmds.pool[index].cb = callback;

discord_add_intents(client, DISCORD_GATEWAY_GUILD_MESSAGES
| DISCORD_GATEWAY_DIRECT_MESSAGES);
| DISCORD_GATEWAY_DIRECT_MESSAGES);
}

void
Expand Down
36 changes: 25 additions & 11 deletions src/gateway.c
Original file line number Diff line number Diff line change
Expand Up @@ -693,16 +693,27 @@ on_message_create(struct discord_gateway *gw, struct sized_buffer *data)
if (gw->cmds.pool
&& !strncmp(gw->cmds.prefix.start, msg.content, gw->cmds.prefix.size))
{
char *command_start = msg.content + gw->cmds.prefix.size;
char *command_end = command_start;
while (*command_end && !isspace(*command_end))
++command_end;
size_t command_len = command_end - command_start;
lcsmuller marked this conversation as resolved.
Show resolved Hide resolved

struct discord_gateway_cmd_cbs *cmd = NULL;
size_t i;

for (i = 0; i < gw->cmds.amt; ++i) {
/* check if command from channel matches set command */
if (!strncmp(gw->cmds.pool[i].start,
msg.content + gw->cmds.prefix.size,
gw->cmds.pool[i].size))
{
cmd = &gw->cmds.pool[i];
if (command_len == gw->cmds.pool[i].size) {
/* check if command from channel matches set command */
if (!strncmp(gw->cmds.pool[i].start,
command_start,
command_len))
{
cmd = &gw->cmds.pool[i];
if (!cmd->cb)
cmd = NULL;
break;
}
}
}
if (!cmd && gw->cmds.prefix.size) {
Expand All @@ -714,11 +725,10 @@ on_message_create(struct discord_gateway *gw, struct sized_buffer *data)
char *tmp = msg.content; /* hold original ptr */

/* skip blank characters */
msg.content += (ptrdiff_t)(gw->cmds.prefix.size + cmd->size);
while (isspace((int)msg.content[0])) {
msg.content = command_end;
while (*msg.content && isspace((int)msg.content[0]))
lcsmuller marked this conversation as resolved.
Show resolved Hide resolved
++msg.content;
}


cmd->cb(client, &msg);

msg.content = tmp; /* retrieve original ptr */
Expand Down Expand Up @@ -1552,7 +1562,11 @@ discord_gateway_cleanup(struct discord_gateway *gw)
/* cleanup client session */
free(gw->session);
/* cleanup user commands */
if (gw->cmds.pool) free(gw->cmds.pool);
if (gw->cmds.pool) {
for (size_t i = 0; i < gw->cmds.amt; i++)
free(gw->cmds.pool[i].start);
free(gw->cmds.pool);
}
if (gw->cmds.prefix.start) free(gw->cmds.prefix.start);
}

Expand Down