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-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 },
|
|
|
|
{ "/clear", cmd_clear },
|
|
|
|
{ "/connect", cmd_connect },
|
|
|
|
{ "/exit", cmd_quit },
|
|
|
|
{ "/groupchat", cmd_groupchat },
|
|
|
|
{ "/help", cmd_prompt_help },
|
|
|
|
{ "/log", cmd_log },
|
|
|
|
{ "/myid", cmd_myid },
|
|
|
|
{ "/nick", cmd_nick },
|
|
|
|
{ "/note", cmd_note },
|
|
|
|
{ "/q", cmd_quit },
|
|
|
|
{ "/quit", cmd_quit },
|
|
|
|
{ "/status", cmd_status },
|
2014-06-22 02:18:23 +02:00
|
|
|
|
2014-07-21 03:25:21 +02:00
|
|
|
#ifdef _AUDIO
|
2014-03-08 01:12:51 +01:00
|
|
|
{ "/lsdev", cmd_list_devices },
|
|
|
|
{ "/sdev", cmd_change_device },
|
2014-07-21 03:25:21 +02:00
|
|
|
#endif /* _AUDIO */
|
2013-11-10 19:23:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct cmd_func chat_commands[] = {
|
|
|
|
{ "/invite", cmd_groupinvite },
|
|
|
|
{ "/join", cmd_join_group },
|
|
|
|
{ "/savefile", cmd_savefile },
|
|
|
|
{ "/sendfile", cmd_sendfile },
|
2014-04-19 23:58:13 +02:00
|
|
|
|
2014-07-21 03:25:21 +02:00
|
|
|
#ifdef _AUDIO
|
2014-03-07 03:14:04 +01:00
|
|
|
{ "/call", cmd_call },
|
|
|
|
{ "/cancel", cmd_cancel },
|
|
|
|
{ "/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-06-22 02:18:23 +02:00
|
|
|
{ "/sdev", cmd_ccur_device },
|
|
|
|
{ "/mute", cmd_mute },
|
|
|
|
{ "/sense", cmd_sense },
|
2014-07-21 03:25:21 +02:00
|
|
|
#endif /* _AUDIO */
|
2013-11-10 19:23:47 +01:00
|
|
|
};
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
/* Parses input command and puts args into arg array.
|
2013-11-10 04:08:25 +01:00
|
|
|
Returns number of arguments on success, -1 on failure. */
|
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
|
|
|
{
|
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-07-07 04:15:35 +02:00
|
|
|
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]);
|
|
|
|
strcpy(cmd, tmp);
|
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;
|
|
|
|
}
|
|
|
|
|
2013-11-10 19:23:47 +01:00
|
|
|
/* Matches command to respective function. Returns 0 on match, 1 on no match */
|
2013-11-19 21:32:35 +01:00
|
|
|
static int do_command(WINDOW *w, ToxWindow *self, Tox *m, int num_args, int num_cmds,
|
2013-11-11 05:17:46 +01:00
|
|
|
struct cmd_func *commands, char (*args)[MAX_STR_SIZE])
|
2013-11-10 19:23:47 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_cmds; ++i) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
if (do_command(w, self, m, num_args, CHAT_NUM_COMMANDS, chat_commands, args) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
break;
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
case GROUPCHAT_COMMAND_MODE:
|
|
|
|
break;
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:32:35 +01:00
|
|
|
if (do_command(w, self, m, num_args, GLOBAL_NUM_COMMANDS, global_commands, args) == 0)
|
2013-11-10 19:23:47 +01:00
|
|
|
return;
|
|
|
|
|
2014-07-21 01:12:13 +02:00
|
|
|
/* Just play sound instead */
|
|
|
|
/*line_info_add(self, NULL, NULL, NULL, "Invalid command.", SYS_MSG, 0, 0);*/
|
|
|
|
notify(self, error, 0);
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|