1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-12-08 07:16:35 +01:00

improve command parsing so you don't need quotes & add group ignoring

This commit is contained in:
Jfreegman
2015-01-12 15:03:23 -05:00
parent 89637e7d2f
commit 7ee858110c
8 changed files with 150 additions and 39 deletions

View File

@@ -83,6 +83,8 @@ static struct cmd_func chat_commands[] = {
static struct cmd_func group_commands[] = {
{ "/topic", cmd_set_topic },
{ "/chatid", cmd_chatid },
{ "/ignore", cmd_ignore },
{ "/unignore", cmd_unignore },
#ifdef AUDIO
{ "/mute", cmd_mute },
{ "/sense", cmd_sense },
@@ -90,10 +92,65 @@ static struct cmd_func group_commands[] = {
{ NULL, NULL },
};
/* Parses input command and puts args into arg array.
Returns number of arguments on success, -1 on failure. */
#define SPECIAL_COMMANDS 9
static const char special_commands[SPECIAL_COMMANDS][MAX_CMDNAME_SIZE] = {
"/ban",
"/deop",
"/group",
"/ignore",
"/nick",
"/note",
"/op",
"/topic",
"/unignore"
};
/* return true if input command is in the special_commands array. False otherwise.*/
static bool is_special_command(const char *input)
{
int s = char_find(0, input, ' ');
if (s == strlen(input))
return false;
int i;
for (i = 0; i < SPECIAL_COMMANDS; ++i) {
if (strncmp(input, special_commands[i], s) == 0)
return true;
}
return false;
}
/* Parses commands in the special_commands array which take exactly one argument that may contain spaces.
* Unlike parse_command, this function does not split the input string at spaces.
* Returns number of arguments on success, returns -1 on failure
*/
static int parse_special_command(WINDOW *w, ToxWindow *self, const char *input, char (*args)[MAX_STR_SIZE])
{
int len = strlen(input);
int s = char_find(0, input, ' ');
if (s + 1 >= len)
return -1;
memcpy(args[0], input, s);
args[0][s++] = '\0'; /* increment to remove space after /command */
memcpy(args[1], input + s, len - s);
args[1][len - s] = '\0';
return 2;
}
/* Parses input command and puts args (split by spaces) into args array.
* Returns number of arguments on success, -1 on failure.
*/
static int parse_command(WINDOW *w, ToxWindow *self, const char *input, char (*args)[MAX_STR_SIZE])
{
if (is_special_command(input))
return parse_special_command(w, self, input, args);
char *cmd = strdup(input);
if (cmd == NULL)