1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-15 08:13:02 +01:00
toxic/src/execute.c

275 lines
7.4 KiB
C
Raw Normal View History

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"
#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;
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 },
{ "/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 },
{ "/conference", cmd_conference },
#ifdef GAMES
2020-12-11 07:46:01 +01:00
{ "/game", cmd_game },
#endif
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
{ "/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 },
{ "/nospam", cmd_nospam },
2014-03-08 01:12:51 +01:00
{ "/q", cmd_quit },
{ "/quit", cmd_quit },
{ "/requests", cmd_requests },
2014-03-08 01:12:51 +01:00
{ "/status", cmd_status },
#ifdef AUDIO
2014-03-08 01:12:51 +01:00
{ "/lsdev", cmd_list_devices },
{ "/sdev", cmd_change_device },
#endif /* AUDIO */
#ifdef VIDEO
{ "/lsvdev", cmd_list_video_devices },
{ "/svdev", cmd_change_video_device },
#endif /* VIDEO */
#ifdef PYTHON
{ "/run", cmd_run },
#endif /* PYTHON */
{ NULL, NULL },
2013-11-10 19:23:47 +01:00
};
static struct cmd_func chat_commands[] = {
{ "/cancel", cmd_cancelfile },
{ "/invite", cmd_conference_invite },
{ "/join", cmd_conference_join },
#ifdef GAMES
{ "/play", cmd_game_join },
#endif
{ "/savefile", cmd_savefile },
{ "/sendfile", cmd_sendfile },
#ifdef AUDIO
{ "/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 */
#ifdef VIDEO
{ "/vcall", cmd_vcall },
{ "/video", cmd_video },
{ "/res", cmd_res },
#endif /* VIDEO */
{ NULL, NULL },
2014-11-15 02:13:08 +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 },
{ "/ptt", cmd_conference_push_to_talk },
2020-05-07 02:00:00 +02:00
{ "/sense", cmd_conference_sense },
#endif /* AUDIO */
{ NULL, NULL },
2013-11-10 19:23:47 +01:00
};
2013-11-10 03:43:56 +01:00
/* Special commands are commands that only take one argument even if it contains spaces */
static const char special_commands[][MAX_CMDNAME_SIZE] = {
"/add",
"/avatar",
"/nick",
"/note",
#ifdef PYTHON
"/run",
#endif /* PYTHON */
"/sendfile",
"/title",
2020-05-07 02:00:00 +02:00
"/mute",
"",
};
/* Returns true if input command is in the special_commands array. */
static bool is_special_command(const char *input)
{
const int s = char_find(0, input, ' ');
for (int i = 0; special_commands[i][0] != '\0'; ++i) {
if (strncmp(input, special_commands[i], s) == 0) {
return true;
}
}
return false;
}
/* Parses commands in the special_commands array. Unlike parse_command, this function
* does not split the input string at spaces.
*
* Returns the number of arguments.
*/
static int parse_special_command(const char *input, char (*args)[MAX_STR_SIZE])
{
int len = strlen(input);
int s = char_find(0, input, ' ');
memcpy(args[0], input, s);
args[0][s++] = '\0'; // increment to remove space after "/command "
if (s >= len) {
return 1; // No additional args
}
memcpy(args[1], input + s, len - s);
args[1][len - s] = '\0';
return 2;
}
/* Parses input command and puts args into arg array.
*
* Returns the number of arguments.
*/
static int parse_command(const char *input, char (*args)[MAX_STR_SIZE])
2013-11-10 03:43:56 +01:00
{
if (is_special_command(input)) {
return parse_special_command(input, args);
}
char *cmd = strdup(input);
2014-07-17 09:35:18 +02:00
if (cmd == NULL) {
exit_toxic_err("failed in parse_command", FATALERR_MEMORY);
}
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) {
int i = char_find(0, cmd, ' '); // index of last char in an argument
memcpy(args[num_args], cmd, i);
args[num_args++][i] = '\0';
2013-11-10 03:43:56 +01:00
if (cmd[i] == '\0') { // no more args
2014-07-17 09:35:18 +02:00
break;
}
2013-11-10 03:43:56 +01:00
char tmp[MAX_STR_SIZE];
snprintf(tmp, sizeof(tmp), "%s", &cmd[i + 1]);
strcpy(cmd, tmp); // tmp will always fit inside cmd
2013-11-10 03:43:56 +01:00
}
free(cmd);
2013-11-10 03:43:56 +01:00
return num_args;
}
/* Matches command to respective function.
*
* Returns 0 on match.
* Returns 1 on no match
*/
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
{
for (size_t i = 0; commands[i].name != NULL; ++i) {
2013-11-10 19:23:47 +01:00
if (strcmp(args[0], commands[i].name) == 0) {
(commands[i].func)(w, self, m, num_args - 1, args);
2013-11-10 19:23:47 +01:00
return 0;
}
}
return 1;
}
void execute(WINDOW *w, ToxWindow *self, Tox *m, const char *input, int mode)
2013-11-10 03:43:56 +01:00
{
if (string_is_empty(input)) {
2013-11-10 03:43:56 +01:00
return;
}
2013-11-10 03:43:56 +01:00
char args[MAX_NUM_ARGS][MAX_STR_SIZE];
int num_args = parse_command(input, args);
2013-11-10 03:43:56 +01:00
if (num_args <= 0) {
2013-11-10 04:08:25 +01:00
return;
}
2013-11-10 04:08:25 +01: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.
*
* Note: Global commands must come last in case of duplicate command names
*/
2013-11-12 07:50:04 +01:00
switch (mode) {
case CHAT_COMMAND_MODE:
if (do_command(w, self, m, num_args, chat_commands, args) == 0) {
return;
}
break;
2013-11-10 03:43:56 +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;
}
break;
2013-11-10 03:43:56 +01:00
}
if (do_command(w, self, m, num_args, global_commands, args) == 0) {
2013-11-10 19:23:47 +01:00
return;
}
2013-11-10 19:23:47 +01:00
#ifdef PYTHON
if (do_plugin_command(num_args, args) == 0) {
return;
}
#endif
line_info_add(self, false, NULL, NULL, SYS_MSG, 0, 0, "Invalid command.");
2013-11-10 03:43:56 +01:00
}