2014-02-25 08:28:24 +01:00
|
|
|
/* execute.c
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Toxic All Rights Reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Toxic.
|
|
|
|
*
|
|
|
|
* Toxic is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Toxic is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Toxic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
2013-11-10 03:43:56 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-03-08 16:36:42 +01:00
|
|
|
#include <assert.h>
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-06-22 02:18:23 +02:00
|
|
|
#include "toxic.h"
|
|
|
|
#include "windows.h"
|
2013-11-10 03:43:56 +01:00
|
|
|
#include "execute.h"
|
2013-11-10 19:23:47 +01:00
|
|
|
#include "chat_commands.h"
|
|
|
|
#include "global_commands.h"
|
2014-11-15 04:15:59 +01:00
|
|
|
#include "group_commands.h"
|
2014-03-25 08:17:22 +01:00
|
|
|
#include "line_info.h"
|
2014-06-22 03:41:38 +02:00
|
|
|
#include "misc_tools.h"
|
2014-07-21 01:12:13 +02:00
|
|
|
#include "notify.h"
|
2013-11-10 19:23:47 +01:00
|
|
|
|
|
|
|
struct cmd_func {
|
|
|
|
const char *name;
|
2013-11-19 21:32:35 +01:00
|
|
|
void (*func)(WINDOW *w, ToxWindow *, Tox *m, int argc, char (*argv)[MAX_STR_SIZE]);
|
2013-11-10 19:23:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct cmd_func global_commands[] = {
|
2014-03-08 01:12:51 +01:00
|
|
|
{ "/accept", cmd_accept },
|
|
|
|
{ "/add", cmd_add },
|
2014-09-26 09:10:44 +02:00
|
|
|
{ "/avatar", cmd_avatar },
|
2014-03-08 01:12:51 +01:00
|
|
|
{ "/clear", cmd_clear },
|
|
|
|
{ "/connect", cmd_connect },
|
2014-08-25 20:08:01 +02:00
|
|
|
{ "/decline", cmd_decline },
|
2014-03-08 01:12:51 +01:00
|
|
|
{ "/exit", cmd_quit },
|
2014-11-12 02:49:05 +01:00
|
|
|
{ "/group", cmd_groupchat },
|
2014-03-08 01:12:51 +01:00
|
|
|
{ "/help", cmd_prompt_help },
|
2015-01-04 07:56:37 +01:00
|
|
|
{ "/join", cmd_join },
|
2014-03-08 01:12:51 +01:00
|
|
|
{ "/log", cmd_log },
|
|
|
|
{ "/myid", cmd_myid },
|
|
|
|
{ "/nick", cmd_nick },
|
|
|
|
{ "/note", cmd_note },
|
|
|
|
{ "/q", cmd_quit },
|
|
|
|
{ "/quit", cmd_quit },
|
2014-08-20 02:49:29 +02:00
|
|
|
{ "/requests", cmd_requests },
|
2014-03-08 01:12:51 +01:00
|
|
|
{ "/status", cmd_status },
|
2014-09-23 03:24:45 +02:00
|
|
|
#ifdef AUDIO
|
2014-03-08 01:12:51 +01:00
|
|
|
{ "/lsdev", cmd_list_devices },
|
|
|
|
{ "/sdev", cmd_change_device },
|
2014-09-23 03:24:45 +02:00
|
|
|
#endif /* AUDIO */
|
2014-09-05 00:23:52 +02:00
|
|
|
{ NULL, NULL },
|
2013-11-10 19:23:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct cmd_func chat_commands[] = {
|
2014-08-04 07:56:43 +02:00
|
|
|
{ "/cancel", cmd_cancelfile },
|
2015-01-23 06:52:09 +01:00
|
|
|
{ "/invite", cmd_groupinvite },
|
2013-11-10 19:23:47 +01:00
|
|
|
{ "/savefile", cmd_savefile },
|
|
|
|
{ "/sendfile", cmd_sendfile },
|
2014-09-23 03:24:45 +02:00
|
|
|
#ifdef AUDIO
|
2014-03-07 03:14:04 +01:00
|
|
|
{ "/call", cmd_call },
|
|
|
|
{ "/answer", cmd_answer },
|
2014-03-23 22:54:56 +01:00
|
|
|
{ "/reject", cmd_reject },
|
2014-03-07 03:14:04 +01:00
|
|
|
{ "/hangup", cmd_hangup },
|
2014-11-15 02:13:08 +01:00
|
|
|
{ "/mute", cmd_mute },
|
|
|
|
{ "/sense", cmd_sense },
|
|
|
|
#endif /* AUDIO */
|
|
|
|
{ NULL, NULL },
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct cmd_func group_commands[] = {
|
2015-01-04 07:56:37 +01:00
|
|
|
{ "/chatid", cmd_chatid },
|
2015-01-12 21:03:23 +01:00
|
|
|
{ "/ignore", cmd_ignore },
|
2015-03-07 06:05:54 +01:00
|
|
|
{ "/rejoin", cmd_rejoin },
|
|
|
|
{ "/topic", cmd_set_topic },
|
2015-01-12 21:03:23 +01:00
|
|
|
{ "/unignore", cmd_unignore },
|
2014-11-15 02:13:08 +01:00
|
|
|
#ifdef AUDIO
|
2014-06-22 02:18:23 +02:00
|
|
|
{ "/mute", cmd_mute },
|
|
|
|
{ "/sense", cmd_sense },
|
2014-09-23 03:24:45 +02:00
|
|
|
#endif /* AUDIO */
|
2014-09-05 00:23:52 +02:00
|
|
|
{ NULL, NULL },
|
2013-11-10 19:23:47 +01:00
|
|
|
};
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2015-03-22 05:56:14 +01:00
|
|
|
#define NUM_SPECIAL_COMMANDS 9
|
|
|
|
static const char special_commands[NUM_SPECIAL_COMMANDS][MAX_CMDNAME_SIZE] = {
|
2015-01-12 21:03:23 +01:00
|
|
|
"/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;
|
|
|
|
|
2015-03-22 05:56:14 +01:00
|
|
|
for (i = 0; i < NUM_SPECIAL_COMMANDS; ++i) {
|
2015-01-12 21:03:23 +01:00
|
|
|
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.
|
2015-03-22 05:56:14 +01:00
|
|
|
*
|
|
|
|
* Returns number of arguments on success
|
|
|
|
* Returns -1 on failure
|
2015-01-12 21:03:23 +01:00
|
|
|
*/
|
|
|
|
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.
|
2015-03-22 05:56:14 +01:00
|
|
|
*
|
|
|
|
* Returns number of arguments on success
|
|
|
|
* Returns -1 on failure.
|
2015-01-12 21:03:23 +01:00
|
|
|
*/
|
2014-07-16 18:47:14 +02:00
|
|
|
static int parse_command(WINDOW *w, ToxWindow *self, const char *input, char (*args)[MAX_STR_SIZE])
|
2013-11-10 03:43:56 +01:00
|
|
|
{
|
2015-01-12 21:03:23 +01:00
|
|
|
if (is_special_command(input))
|
|
|
|
return parse_special_command(w, self, input, args);
|
|
|
|
|
2014-07-16 18:47:14 +02:00
|
|
|
char *cmd = strdup(input);
|
2014-07-17 09:35:18 +02:00
|
|
|
|
|
|
|
if (cmd == NULL)
|
|
|
|
exit_toxic_err("failed in parse_command", FATALERR_MEMORY);
|
|
|
|
|
2013-11-10 03:43:56 +01:00
|
|
|
int num_args = 0;
|
2014-07-16 18:47:14 +02:00
|
|
|
int i = 0; /* index of last char in an argument */
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2013-11-10 04:08:25 +01:00
|
|
|
/* characters wrapped in double quotes count as one arg */
|
2014-07-17 09:35:18 +02:00
|
|
|
while (num_args < MAX_NUM_ARGS) {
|
2014-07-16 18:47:14 +02:00
|
|
|
int qt_ofst = 0; /* set to 1 to offset index for quote char at end of arg */
|
|
|
|
|
2013-11-10 03:43:56 +01:00
|
|
|
if (*cmd == '\"') {
|
2014-07-16 18:47:14 +02:00
|
|
|
qt_ofst = 1;
|
|
|
|
i = char_find(1, cmd, '\"');
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-07-16 18:47:14 +02:00
|
|
|
if (cmd[i] == '\0') {
|
2014-09-05 00:23:52 +02:00
|
|
|
const char *errmsg = "Invalid argument. Did you forget a closing \"?";
|
2014-07-25 04:43:32 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, errmsg);
|
2014-07-16 18:47:14 +02:00
|
|
|
free(cmd);
|
2013-11-10 04:08:25 +01:00
|
|
|
return -1;
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
} else {
|
2014-07-16 18:47:14 +02:00
|
|
|
i = char_find(0, cmd, ' ');
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
2014-07-16 18:47:14 +02:00
|
|
|
memcpy(args[num_args], cmd, i + qt_ofst);
|
|
|
|
args[num_args++][i + qt_ofst] = '\0';
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-07-17 09:35:18 +02:00
|
|
|
if (cmd[i] == '\0') /* no more args */
|
|
|
|
break;
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-07-25 23:55:21 +02:00
|
|
|
char tmp[MAX_STR_SIZE];
|
|
|
|
snprintf(tmp, sizeof(tmp), "%s", &cmd[i + 1]);
|
2014-09-05 00:23:52 +02:00
|
|
|
strcpy(cmd, tmp); /* tmp will always fit inside cmd */
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
2014-07-16 18:47:14 +02:00
|
|
|
free(cmd);
|
2013-11-10 03:43:56 +01:00
|
|
|
return num_args;
|
|
|
|
}
|
|
|
|
|
2015-03-22 05:56:14 +01:00
|
|
|
/* Matches command to respective function.
|
|
|
|
*
|
|
|
|
* Returns 0 on match,
|
|
|
|
* Returns -1 on no match
|
|
|
|
*/
|
2014-09-02 08:23:44 +02:00
|
|
|
static int do_command(WINDOW *w, ToxWindow *self, Tox *m, int num_args, struct cmd_func *commands,
|
|
|
|
char (*args)[MAX_STR_SIZE])
|
2013-11-10 19:23:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2014-09-02 08:23:44 +02:00
|
|
|
for (i = 0; commands[i].name != NULL; ++i) {
|
2013-11-10 19:23:47 +01:00
|
|
|
if (strcmp(args[0], commands[i].name) == 0) {
|
2014-04-19 23:58:13 +02:00
|
|
|
(commands[i].func)(w, self, m, num_args - 1, args);
|
2013-11-10 19:23:47 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-22 05:56:14 +01:00
|
|
|
return -1;
|
2013-11-10 19:23:47 +01:00
|
|
|
}
|
|
|
|
|
2014-07-16 18:47:14 +02:00
|
|
|
void execute(WINDOW *w, ToxWindow *self, Tox *m, const char *input, int mode)
|
2013-11-10 03:43:56 +01:00
|
|
|
{
|
2014-07-16 18:47:14 +02:00
|
|
|
if (string_is_empty(input))
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
char args[MAX_NUM_ARGS][MAX_STR_SIZE];
|
2014-07-16 18:47:14 +02:00
|
|
|
int num_args = parse_command(w, self, input, args);
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2013-11-10 04:08:25 +01:00
|
|
|
if (num_args == -1)
|
|
|
|
return;
|
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
/* Try to match input command to command functions. If non-global command mode is specified,
|
|
|
|
try specified mode's commands first, then upon failure try global commands.
|
2013-11-15 20:38:58 +01:00
|
|
|
|
|
|
|
Note: Global commands must come last in case of duplicate command names */
|
2013-11-12 07:50:04 +01:00
|
|
|
switch (mode) {
|
2014-04-19 23:58:13 +02:00
|
|
|
case CHAT_COMMAND_MODE:
|
2014-09-02 08:23:44 +02:00
|
|
|
if (do_command(w, self, m, num_args, chat_commands, args) == 0)
|
2014-04-19 23:58:13 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
break;
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
case GROUPCHAT_COMMAND_MODE:
|
2014-11-15 02:13:08 +01:00
|
|
|
if (do_command(w, self, m, num_args, group_commands, args) == 0)
|
|
|
|
return;
|
2015-01-04 07:56:37 +01:00
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
break;
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
2014-09-02 08:23:44 +02:00
|
|
|
if (do_command(w, self, m, num_args, global_commands, args) == 0)
|
2013-11-10 19:23:47 +01:00
|
|
|
return;
|
|
|
|
|
2014-09-02 00:48:10 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid command.");
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|