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
|
|
|
*/
|
|
|
|
|
2020-04-18 02:00:00 +02:00
|
|
|
#include <assert.h>
|
2013-11-10 03:43:56 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2020-04-18 02:00:00 +02:00
|
|
|
#include "api.h"
|
2013-11-10 19:23:47 +01:00
|
|
|
#include "chat_commands.h"
|
2020-04-18 02:00:00 +02:00
|
|
|
#include "execute.h"
|
2013-11-10 19:23:47 +01:00
|
|
|
#include "global_commands.h"
|
2020-11-08 16:08:24 +01:00
|
|
|
#include "conference_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"
|
2020-04-18 02:00:00 +02:00
|
|
|
#include "toxic.h"
|
|
|
|
#include "windows.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 },
|
2020-11-08 16:08:24 +01:00
|
|
|
{ "/conference", cmd_conference },
|
2014-03-08 01:12:51 +01:00
|
|
|
{ "/help", cmd_prompt_help },
|
|
|
|
{ "/log", cmd_log },
|
|
|
|
{ "/myid", cmd_myid },
|
2018-06-16 05:07:16 +02:00
|
|
|
#ifdef QRCODE
|
2015-11-09 04:51:46 +01:00
|
|
|
{ "/myqr", cmd_myqr },
|
2018-06-16 05:07:16 +02:00
|
|
|
#endif /* QRCODE */
|
2014-03-08 01:12:51 +01:00
|
|
|
{ "/nick", cmd_nick },
|
|
|
|
{ "/note", cmd_note },
|
2015-10-23 01:44:05 +02:00
|
|
|
{ "/nospam", cmd_nospam },
|
2014-03-08 01:12:51 +01:00
|
|
|
{ "/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 */
|
2015-07-30 08:49:27 +02:00
|
|
|
#ifdef VIDEO
|
2020-11-08 16:08:24 +01:00
|
|
|
{ "/lsvdev", cmd_list_video_devices },
|
|
|
|
{ "/svdev", cmd_change_video_device },
|
2015-07-30 08:49:27 +02:00
|
|
|
#endif /* VIDEO */
|
2017-05-16 02:17:23 +02:00
|
|
|
#ifdef PYTHON
|
|
|
|
{ "/run", cmd_run },
|
|
|
|
#endif /* PYTHON */
|
2014-09-05 00:23:52 +02:00
|
|
|
{ NULL, NULL },
|
2013-11-10 19:23:47 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct cmd_func chat_commands[] = {
|
2020-11-08 16:08:24 +01:00
|
|
|
{ "/cancel", cmd_cancelfile },
|
|
|
|
{ "/invite", cmd_conference_invite },
|
|
|
|
{ "/join", cmd_conference_join },
|
|
|
|
{ "/savefile", cmd_savefile },
|
|
|
|
{ "/sendfile", cmd_sendfile },
|
2014-09-23 03:24:45 +02:00
|
|
|
#ifdef AUDIO
|
2020-11-08 16:08:24 +01:00
|
|
|
{ "/call", cmd_call },
|
|
|
|
{ "/answer", cmd_answer },
|
|
|
|
{ "/reject", cmd_reject },
|
|
|
|
{ "/hangup", cmd_hangup },
|
|
|
|
{ "/mute", cmd_mute },
|
|
|
|
{ "/sense", cmd_sense },
|
|
|
|
{ "/bitrate", cmd_bitrate },
|
2014-11-15 02:13:08 +01:00
|
|
|
#endif /* AUDIO */
|
2015-07-30 08:49:27 +02:00
|
|
|
#ifdef VIDEO
|
2020-04-14 02:00:00 +02:00
|
|
|
{ "/vcall", cmd_vcall },
|
2020-11-08 16:08:24 +01:00
|
|
|
{ "/video", cmd_video },
|
2020-04-14 02:00:00 +02:00
|
|
|
{ "/res", cmd_res },
|
2015-07-30 08:49:27 +02:00
|
|
|
#endif /* VIDEO */
|
2020-11-08 16:08:24 +01:00
|
|
|
{ NULL, NULL },
|
2014-11-15 02:13:08 +01:00
|
|
|
};
|
|
|
|
|
2020-11-08 16:08:24 +01:00
|
|
|
static struct cmd_func conference_commands[] = {
|
|
|
|
{ "/title", cmd_conference_set_title },
|
2014-11-15 04:15:59 +01:00
|
|
|
|
2014-11-15 02:13:08 +01:00
|
|
|
#ifdef AUDIO
|
2020-05-07 02:00:00 +02:00
|
|
|
{ "/audio", cmd_enable_audio },
|
|
|
|
{ "/mute", cmd_conference_mute },
|
2020-05-07 02:00:00 +02:00
|
|
|
{ "/sense", cmd_conference_sense },
|
2014-09-23 03:24:45 +02:00
|
|
|
#endif /* AUDIO */
|
2020-04-14 02:00:00 +02:00
|
|
|
{ NULL, NULL },
|
2013-11-10 19:23:47 +01:00
|
|
|
};
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2018-10-08 06:47:51 +02:00
|
|
|
|
|
|
|
#ifdef PYTHON
|
2020-05-07 02:00:00 +02:00
|
|
|
#define SPECIAL_COMMANDS 7
|
2018-10-08 06:47:51 +02:00
|
|
|
#else
|
2020-05-07 02:00:00 +02:00
|
|
|
#define SPECIAL_COMMANDS 6
|
2018-10-08 06:47:51 +02:00
|
|
|
#endif /* PYTHON */
|
|
|
|
|
2018-10-07 23:30:43 +02:00
|
|
|
/* Special commands are commands that only take one argument even if it contains spaces */
|
|
|
|
static const char special_commands[SPECIAL_COMMANDS][MAX_CMDNAME_SIZE] = {
|
2018-10-08 06:47:51 +02:00
|
|
|
"/avatar",
|
2018-10-07 23:30:43 +02:00
|
|
|
"/nick",
|
|
|
|
"/note",
|
2018-10-08 06:47:51 +02:00
|
|
|
#ifdef PYTHON
|
|
|
|
"/run",
|
|
|
|
#endif /* PYTHON */
|
|
|
|
"/sendfile",
|
2018-10-28 21:18:17 +01:00
|
|
|
"/title",
|
2020-05-07 02:00:00 +02:00
|
|
|
"/mute",
|
2018-10-07 23:30:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Returns true if input command is in the special_commands array. */
|
|
|
|
static bool is_special_command(const char *input)
|
|
|
|
{
|
2018-10-09 04:28:36 +02:00
|
|
|
const int s = char_find(0, input, ' ');
|
2018-10-07 23:30:43 +02:00
|
|
|
|
2018-10-09 04:28:36 +02:00
|
|
|
for (int i = 0; i < SPECIAL_COMMANDS; ++i) {
|
2018-10-08 06:47:51 +02:00
|
|
|
if (strncmp(input, special_commands[i], s) == 0) {
|
2018-10-07 23:30:43 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parses commands in the special_commands array. Unlike parse_command, this function
|
|
|
|
* does not split the input string at spaces.
|
|
|
|
*
|
2018-10-08 06:47:51 +02:00
|
|
|
* Returns the number of arguments.
|
2018-10-07 23:30:43 +02:00
|
|
|
*/
|
2020-03-15 19:57:00 +01:00
|
|
|
static int parse_special_command(const char *input, char (*args)[MAX_STR_SIZE])
|
2018-10-07 23:30:43 +02:00
|
|
|
{
|
|
|
|
int len = strlen(input);
|
|
|
|
int s = char_find(0, input, ' ');
|
|
|
|
|
2018-10-08 06:47:51 +02:00
|
|
|
memcpy(args[0], input, s);
|
|
|
|
args[0][s++] = '\0'; // increment to remove space after "/command "
|
|
|
|
|
|
|
|
if (s >= len) {
|
|
|
|
return 1; // No additional args
|
2018-10-07 23:30:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(args[1], input + s, len - s);
|
|
|
|
args[1][len - s] = '\0';
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
/* Parses input command and puts args into arg array.
|
2018-10-08 06:47:51 +02:00
|
|
|
*
|
|
|
|
* Returns the number of arguments.
|
|
|
|
*/
|
2020-03-15 19:57:00 +01:00
|
|
|
static int parse_command(const char *input, char (*args)[MAX_STR_SIZE])
|
2013-11-10 03:43:56 +01:00
|
|
|
{
|
2018-10-07 23:30:43 +02:00
|
|
|
if (is_special_command(input)) {
|
2020-03-15 19:57:00 +01:00
|
|
|
return parse_special_command(input, args);
|
2018-10-07 23:30:43 +02:00
|
|
|
}
|
|
|
|
|
2014-07-16 18:47:14 +02:00
|
|
|
char *cmd = strdup(input);
|
2014-07-17 09:35:18 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (cmd == NULL) {
|
2015-07-04 07:19:16 +02:00
|
|
|
exit_toxic_err("failed in parse_command", FATALERR_MEMORY);
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-07-17 09:35:18 +02:00
|
|
|
|
2013-11-10 03:43:56 +01:00
|
|
|
int num_args = 0;
|
|
|
|
|
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) {
|
2018-10-08 19:47:08 +02:00
|
|
|
int i = char_find(0, cmd, ' '); // index of last char in an argument
|
2018-10-08 06:47:51 +02:00
|
|
|
memcpy(args[num_args], cmd, i);
|
|
|
|
args[num_args++][i] = '\0';
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2018-10-08 06:47:51 +02:00
|
|
|
if (cmd[i] == '\0') { // no more args
|
2014-07-17 09:35:18 +02:00
|
|
|
break;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
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]);
|
2018-10-08 06:47:51 +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;
|
|
|
|
}
|
|
|
|
|
2018-10-08 06:47:51 +02: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2018-07-18 17:33:16 +02:00
|
|
|
if (string_is_empty(input)) {
|
2013-11-10 03:43:56 +01:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2014-07-07 04:15:35 +02:00
|
|
|
char args[MAX_NUM_ARGS][MAX_STR_SIZE];
|
2020-03-15 19:57:00 +01:00
|
|
|
int num_args = parse_command(input, args);
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2018-10-08 06:47:51 +02:00
|
|
|
if (num_args <= 0) {
|
2013-11-10 04:08:25 +01:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2013-11-10 04:08:25 +01:00
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
/* Try to match input command to command functions. If non-global command mode is specified,
|
2018-10-08 06:47:51 +02:00
|
|
|
* try specified mode's commands first, then upon failure try global commands.
|
|
|
|
*
|
|
|
|
* 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:
|
2018-07-18 17:33:16 +02:00
|
|
|
if (do_command(w, self, m, num_args, chat_commands, args) == 0) {
|
2014-04-19 23:58:13 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2014-04-19 23:58:13 +02:00
|
|
|
|
|
|
|
break;
|
2013-11-10 03:43:56 +01:00
|
|
|
|
2020-11-08 16:08:24 +01:00
|
|
|
case CONFERENCE_COMMAND_MODE:
|
|
|
|
if (do_command(w, self, m, num_args, conference_commands, args) == 0) {
|
2014-11-15 02:13:08 +01:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2016-09-25 03:07:04 +02:00
|
|
|
|
2014-04-19 23:58:13 +02:00
|
|
|
break;
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (do_command(w, self, m, num_args, global_commands, args) == 0) {
|
2013-11-10 19:23:47 +01:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2013-11-10 19:23:47 +01:00
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
#ifdef PYTHON
|
2017-06-01 22:46:12 +02:00
|
|
|
|
2018-07-18 17:33:16 +02:00
|
|
|
if (do_plugin_command(num_args, args) == 0) {
|
2017-05-17 02:31:23 +02:00
|
|
|
return;
|
2018-07-18 17:33:16 +02:00
|
|
|
}
|
2017-06-01 22:46:12 +02:00
|
|
|
|
2017-05-17 02:31:23 +02:00
|
|
|
#endif
|
|
|
|
|
2015-07-04 07:19:16 +02:00
|
|
|
line_info_add(self, NULL, NULL, NULL, SYS_MSG, 0, 0, "Invalid command.");
|
2013-11-10 03:43:56 +01:00
|
|
|
}
|