2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2012-08-25 12:59:54 +02:00
|
|
|
* Copyright (C) 2003-2012 The Music Player Daemon Project
|
2009-03-13 18:43:16 +01:00
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* This program 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 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-03 10:33:04 +01:00
|
|
|
#include "AllCommands.hxx"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "command.h"
|
2012-08-25 12:59:54 +02:00
|
|
|
#include "QueueCommands.hxx"
|
|
|
|
#include "PlayerCommands.hxx"
|
|
|
|
#include "PlaylistCommands.hxx"
|
2012-08-07 21:55:11 +02:00
|
|
|
#include "DatabaseCommands.hxx"
|
2012-08-25 12:59:54 +02:00
|
|
|
#include "OutputCommands.hxx"
|
|
|
|
#include "MessageCommands.hxx"
|
2012-09-25 11:44:49 +02:00
|
|
|
#include "OtherCommands.hxx"
|
2013-01-03 03:06:45 +01:00
|
|
|
#include "Permission.hxx"
|
2008-09-06 20:28:31 +02:00
|
|
|
#include "tag.h"
|
2013-01-03 10:33:04 +01:00
|
|
|
#include "protocol/Result.hxx"
|
|
|
|
#include "Client.hxx"
|
2012-09-25 11:56:37 +02:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "tokenizer.h"
|
2013-01-02 22:25:17 +01:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2009-01-19 19:09:49 +01:00
|
|
|
#ifdef ENABLE_SQLITE
|
2013-01-02 22:25:17 +01:00
|
|
|
#include "StickerCommands.hxx"
|
|
|
|
#include "StickerDatabase.hxx"
|
2009-01-19 19:09:49 +01:00
|
|
|
#endif
|
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
2012-08-25 12:59:54 +02:00
|
|
|
#include <string.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-10-06 12:33:27 +02:00
|
|
|
/*
|
|
|
|
* The most we ever use is for search/find, and that limits it to the
|
|
|
|
* number of tags we can have. Add one for the command, and one extra
|
|
|
|
* to catch errors clients may send us
|
|
|
|
*/
|
|
|
|
#define COMMAND_ARGV_MAX (2+(TAG_NUM_OF_ITEM_TYPES*2))
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
/* if min: -1 don't check args *
|
|
|
|
* if max: -1 no max args */
|
2008-10-22 09:58:13 +02:00
|
|
|
struct command {
|
2008-02-05 11:17:33 +01:00
|
|
|
const char *cmd;
|
2008-10-22 21:40:44 +02:00
|
|
|
unsigned permission;
|
2006-07-20 18:02:40 +02:00
|
|
|
int min;
|
|
|
|
int max;
|
2013-01-03 17:27:26 +01:00
|
|
|
enum command_return (*handler)(Client *client, int argc, char **argv);
|
2004-04-11 19:37:47 +02:00
|
|
|
};
|
|
|
|
|
2004-11-08 19:01:36 +01:00
|
|
|
/* don't be fooled, this is the command handler for "commands" command */
|
2008-10-22 21:41:49 +02:00
|
|
|
static enum command_return
|
2013-01-03 17:27:26 +01:00
|
|
|
handle_commands(Client *client,
|
2008-11-24 14:55:32 +01:00
|
|
|
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]);
|
2004-11-08 19:01:36 +01:00
|
|
|
|
2008-10-22 21:41:49 +02:00
|
|
|
static enum command_return
|
2013-01-03 17:27:26 +01:00
|
|
|
handle_not_commands(Client *client,
|
2008-11-24 14:55:32 +01:00
|
|
|
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[]);
|
2004-11-23 20:46:33 +01:00
|
|
|
|
2008-10-22 10:08:14 +02:00
|
|
|
/**
|
|
|
|
* The command registry.
|
|
|
|
*
|
|
|
|
* This array must be sorted!
|
|
|
|
*/
|
|
|
|
static const struct command commands[] = {
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "add", PERMISSION_ADD, 1, 1, handle_add },
|
|
|
|
{ "addid", PERMISSION_ADD, 1, 2, handle_addid },
|
2011-01-29 09:26:22 +01:00
|
|
|
{ "channels", PERMISSION_READ, 0, 0, handle_channels },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "clear", PERMISSION_CONTROL, 0, 0, handle_clear },
|
|
|
|
{ "clearerror", PERMISSION_CONTROL, 0, 0, handle_clearerror },
|
|
|
|
{ "close", PERMISSION_NONE, -1, -1, handle_close },
|
|
|
|
{ "commands", PERMISSION_NONE, 0, 0, handle_commands },
|
2012-02-13 19:59:41 +01:00
|
|
|
{ "config", PERMISSION_ADMIN, 0, 0, handle_config },
|
2009-03-30 00:01:02 +02:00
|
|
|
{ "consume", PERMISSION_CONTROL, 1, 1, handle_consume },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "count", PERMISSION_READ, 2, -1, handle_count },
|
|
|
|
{ "crossfade", PERMISSION_CONTROL, 1, 1, handle_crossfade },
|
|
|
|
{ "currentsong", PERMISSION_READ, 0, 0, handle_currentsong },
|
2009-11-07 15:57:22 +01:00
|
|
|
{ "decoders", PERMISSION_READ, 0, 0, handle_decoders },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "delete", PERMISSION_CONTROL, 1, 1, handle_delete },
|
|
|
|
{ "deleteid", PERMISSION_CONTROL, 1, 1, handle_deleteid },
|
|
|
|
{ "disableoutput", PERMISSION_ADMIN, 1, 1, handle_disableoutput },
|
|
|
|
{ "enableoutput", PERMISSION_ADMIN, 1, 1, handle_enableoutput },
|
|
|
|
{ "find", PERMISSION_READ, 2, -1, handle_find },
|
2009-08-25 13:43:22 +02:00
|
|
|
{ "findadd", PERMISSION_READ, 2, -1, handle_findadd},
|
2008-11-22 13:26:21 +01:00
|
|
|
{ "idle", PERMISSION_READ, 0, -1, handle_idle },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "kill", PERMISSION_ADMIN, -1, -1, handle_kill },
|
|
|
|
{ "list", PERMISSION_READ, 1, -1, handle_list },
|
|
|
|
{ "listall", PERMISSION_READ, 0, 1, handle_listall },
|
|
|
|
{ "listallinfo", PERMISSION_READ, 0, 1, handle_listallinfo },
|
|
|
|
{ "listplaylist", PERMISSION_READ, 1, 1, handle_listplaylist },
|
|
|
|
{ "listplaylistinfo", PERMISSION_READ, 1, 1, handle_listplaylistinfo },
|
|
|
|
{ "listplaylists", PERMISSION_READ, 0, 0, handle_listplaylists },
|
2012-02-09 23:52:51 +01:00
|
|
|
{ "load", PERMISSION_ADD, 1, 2, handle_load },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "lsinfo", PERMISSION_READ, 0, 1, handle_lsinfo },
|
2010-03-21 18:21:47 +01:00
|
|
|
{ "mixrampdb", PERMISSION_CONTROL, 1, 1, handle_mixrampdb },
|
|
|
|
{ "mixrampdelay", PERMISSION_CONTROL, 1, 1, handle_mixrampdelay },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "move", PERMISSION_CONTROL, 2, 2, handle_move },
|
|
|
|
{ "moveid", PERMISSION_CONTROL, 2, 2, handle_moveid },
|
|
|
|
{ "next", PERMISSION_CONTROL, 0, 0, handle_next },
|
|
|
|
{ "notcommands", PERMISSION_NONE, 0, 0, handle_not_commands },
|
|
|
|
{ "outputs", PERMISSION_READ, 0, 0, handle_devices },
|
|
|
|
{ "password", PERMISSION_NONE, 1, 1, handle_password },
|
|
|
|
{ "pause", PERMISSION_CONTROL, 0, 1, handle_pause },
|
|
|
|
{ "ping", PERMISSION_NONE, 0, 0, handle_ping },
|
|
|
|
{ "play", PERMISSION_CONTROL, 0, 1, handle_play },
|
|
|
|
{ "playid", PERMISSION_CONTROL, 0, 1, handle_playid },
|
|
|
|
{ "playlist", PERMISSION_READ, 0, 0, handle_playlist },
|
|
|
|
{ "playlistadd", PERMISSION_CONTROL, 2, 2, handle_playlistadd },
|
|
|
|
{ "playlistclear", PERMISSION_CONTROL, 1, 1, handle_playlistclear },
|
|
|
|
{ "playlistdelete", PERMISSION_CONTROL, 2, 2, handle_playlistdelete },
|
|
|
|
{ "playlistfind", PERMISSION_READ, 2, -1, handle_playlistfind },
|
|
|
|
{ "playlistid", PERMISSION_READ, 0, 1, handle_playlistid },
|
|
|
|
{ "playlistinfo", PERMISSION_READ, 0, 1, handle_playlistinfo },
|
|
|
|
{ "playlistmove", PERMISSION_CONTROL, 3, 3, handle_playlistmove },
|
|
|
|
{ "playlistsearch", PERMISSION_READ, 2, -1, handle_playlistsearch },
|
|
|
|
{ "plchanges", PERMISSION_READ, 1, 1, handle_plchanges },
|
|
|
|
{ "plchangesposid", PERMISSION_READ, 1, 1, handle_plchangesposid },
|
|
|
|
{ "previous", PERMISSION_CONTROL, 0, 0, handle_previous },
|
2011-07-19 00:34:33 +02:00
|
|
|
{ "prio", PERMISSION_CONTROL, 2, -1, handle_prio },
|
|
|
|
{ "prioid", PERMISSION_CONTROL, 2, -1, handle_prioid },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "random", PERMISSION_CONTROL, 1, 1, handle_random },
|
2011-01-29 09:26:22 +01:00
|
|
|
{ "readmessages", PERMISSION_READ, 0, 0, handle_read_messages },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "rename", PERMISSION_CONTROL, 2, 2, handle_rename },
|
|
|
|
{ "repeat", PERMISSION_CONTROL, 1, 1, handle_repeat },
|
2009-10-17 22:58:19 +02:00
|
|
|
{ "replay_gain_mode", PERMISSION_CONTROL, 1, 1,
|
|
|
|
handle_replay_gain_mode },
|
|
|
|
{ "replay_gain_status", PERMISSION_READ, 0, 0,
|
|
|
|
handle_replay_gain_status },
|
2011-02-27 23:35:00 +01:00
|
|
|
{ "rescan", PERMISSION_CONTROL, 0, 1, handle_rescan },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "rm", PERMISSION_CONTROL, 1, 1, handle_rm },
|
|
|
|
{ "save", PERMISSION_CONTROL, 1, 1, handle_save },
|
|
|
|
{ "search", PERMISSION_READ, 2, -1, handle_search },
|
2012-06-29 00:05:33 +02:00
|
|
|
{ "searchadd", PERMISSION_ADD, 2, -1, handle_searchadd },
|
|
|
|
{ "searchaddpl", PERMISSION_CONTROL, 3, -1, handle_searchaddpl },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "seek", PERMISSION_CONTROL, 2, 2, handle_seek },
|
2011-12-24 11:20:02 +01:00
|
|
|
{ "seekcur", PERMISSION_CONTROL, 1, 1, handle_seekcur },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "seekid", PERMISSION_CONTROL, 2, 2, handle_seekid },
|
2011-01-29 09:26:22 +01:00
|
|
|
{ "sendmessage", PERMISSION_CONTROL, 2, 2, handle_send_message },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "setvol", PERMISSION_CONTROL, 1, 1, handle_setvol },
|
2009-02-13 10:38:32 +01:00
|
|
|
{ "shuffle", PERMISSION_CONTROL, 0, 1, handle_shuffle },
|
2009-03-27 15:28:49 +01:00
|
|
|
{ "single", PERMISSION_CONTROL, 1, 1, handle_single },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "stats", PERMISSION_READ, 0, 0, handle_stats },
|
|
|
|
{ "status", PERMISSION_READ, 0, 0, handle_status },
|
2009-01-19 19:09:49 +01:00
|
|
|
#ifdef ENABLE_SQLITE
|
|
|
|
{ "sticker", PERMISSION_ADMIN, 3, -1, handle_sticker },
|
|
|
|
#endif
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "stop", PERMISSION_CONTROL, 0, 0, handle_stop },
|
2011-01-29 09:26:22 +01:00
|
|
|
{ "subscribe", PERMISSION_READ, 1, 1, handle_subscribe },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "swap", PERMISSION_CONTROL, 2, 2, handle_swap },
|
|
|
|
{ "swapid", PERMISSION_CONTROL, 2, 2, handle_swapid },
|
|
|
|
{ "tagtypes", PERMISSION_READ, 0, 0, handle_tagtypes },
|
2011-01-29 09:26:22 +01:00
|
|
|
{ "unsubscribe", PERMISSION_READ, 1, 1, handle_unsubscribe },
|
2011-02-27 23:35:00 +01:00
|
|
|
{ "update", PERMISSION_CONTROL, 0, 1, handle_update },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "urlhandlers", PERMISSION_READ, 0, 0, handle_urlhandlers },
|
2008-10-22 10:08:14 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const unsigned num_commands = sizeof(commands) / sizeof(commands[0]);
|
|
|
|
|
2009-01-23 18:51:14 +01:00
|
|
|
static bool
|
2009-01-24 11:13:58 +01:00
|
|
|
command_available(G_GNUC_UNUSED const struct command *cmd)
|
2009-01-23 18:51:14 +01:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_SQLITE
|
|
|
|
if (strcmp(cmd->cmd, "sticker") == 0)
|
|
|
|
return sticker_enabled();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-22 10:08:14 +02:00
|
|
|
/* don't be fooled, this is the command handler for "commands" command */
|
2008-10-22 21:41:49 +02:00
|
|
|
static enum command_return
|
2013-01-03 17:27:26 +01:00
|
|
|
handle_commands(Client *client,
|
2008-11-24 14:55:32 +01:00
|
|
|
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
|
2008-10-22 10:08:14 +02:00
|
|
|
{
|
|
|
|
const unsigned permission = client_get_permission(client);
|
|
|
|
const struct command *cmd;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < num_commands; ++i) {
|
|
|
|
cmd = &commands[i];
|
|
|
|
|
2009-01-23 18:51:14 +01:00
|
|
|
if (cmd->permission == (permission & cmd->permission) &&
|
|
|
|
command_available(cmd))
|
2008-10-22 10:08:14 +02:00
|
|
|
client_printf(client, "command: %s\n", cmd->cmd);
|
|
|
|
}
|
|
|
|
|
2008-10-22 21:41:49 +02:00
|
|
|
return COMMAND_RETURN_OK;
|
2008-10-22 10:08:14 +02:00
|
|
|
}
|
|
|
|
|
2008-10-22 21:41:49 +02:00
|
|
|
static enum command_return
|
2013-01-03 17:27:26 +01:00
|
|
|
handle_not_commands(Client *client,
|
2008-11-24 14:55:32 +01:00
|
|
|
G_GNUC_UNUSED int argc, G_GNUC_UNUSED char *argv[])
|
2008-10-22 10:08:14 +02:00
|
|
|
{
|
|
|
|
const unsigned permission = client_get_permission(client);
|
|
|
|
const struct command *cmd;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < num_commands; ++i) {
|
|
|
|
cmd = &commands[i];
|
|
|
|
|
2008-10-22 21:40:44 +02:00
|
|
|
if (cmd->permission != (permission & cmd->permission))
|
2008-10-22 10:08:14 +02:00
|
|
|
client_printf(client, "command: %s\n", cmd->cmd);
|
|
|
|
}
|
|
|
|
|
2008-10-22 21:41:49 +02:00
|
|
|
return COMMAND_RETURN_OK;
|
2008-10-22 10:08:14 +02:00
|
|
|
}
|
|
|
|
|
2008-10-22 21:40:44 +02:00
|
|
|
void command_init(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-22 10:08:14 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
/* ensure that the command list is sorted */
|
|
|
|
for (unsigned i = 0; i < num_commands - 1; ++i)
|
|
|
|
assert(strcmp(commands[i].cmd, commands[i + 1].cmd) < 0);
|
|
|
|
#endif
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
2008-10-22 21:40:44 +02:00
|
|
|
void command_finish(void)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-22 10:08:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct command *
|
|
|
|
command_lookup(const char *name)
|
|
|
|
{
|
|
|
|
unsigned a = 0, b = num_commands, i;
|
|
|
|
int cmp;
|
|
|
|
|
|
|
|
/* binary search */
|
|
|
|
do {
|
|
|
|
i = (a + b) / 2;
|
|
|
|
|
|
|
|
cmp = strcmp(name, commands[i].cmd);
|
|
|
|
if (cmp == 0)
|
|
|
|
return &commands[i];
|
|
|
|
else if (cmp < 0)
|
|
|
|
b = i;
|
|
|
|
else if (cmp > 0)
|
|
|
|
a = i + 1;
|
|
|
|
} while (a < b);
|
|
|
|
|
|
|
|
return NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
2008-10-22 21:41:54 +02:00
|
|
|
static bool
|
2013-01-03 17:27:26 +01:00
|
|
|
command_check_request(const struct command *cmd, Client *client,
|
2008-10-22 21:40:44 +02:00
|
|
|
unsigned permission, int argc, char *argv[])
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
int min = cmd->min + 1;
|
|
|
|
int max = cmd->max + 1;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-22 21:40:44 +02:00
|
|
|
if (cmd->permission != (permission & cmd->permission)) {
|
2008-09-07 13:52:36 +02:00
|
|
|
if (client != NULL)
|
|
|
|
command_error(client, ACK_ERROR_PERMISSION,
|
|
|
|
"you don't have permission for \"%s\"",
|
|
|
|
cmd->cmd);
|
2008-10-22 21:41:54 +02:00
|
|
|
return false;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (min == 0)
|
2008-10-22 21:41:54 +02:00
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (min == max && max != argc) {
|
2008-09-07 13:52:36 +02:00
|
|
|
if (client != NULL)
|
|
|
|
command_error(client, ACK_ERROR_ARG,
|
|
|
|
"wrong number of arguments for \"%s\"",
|
|
|
|
argv[0]);
|
2008-10-22 21:41:54 +02:00
|
|
|
return false;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (argc < min) {
|
2008-09-07 13:52:36 +02:00
|
|
|
if (client != NULL)
|
|
|
|
command_error(client, ACK_ERROR_ARG,
|
|
|
|
"too few arguments for \"%s\"", argv[0]);
|
2008-10-22 21:41:54 +02:00
|
|
|
return false;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (argc > max && max /* != 0 */ ) {
|
2008-09-07 13:52:36 +02:00
|
|
|
if (client != NULL)
|
|
|
|
command_error(client, ACK_ERROR_ARG,
|
|
|
|
"too many arguments for \"%s\"", argv[0]);
|
2008-10-22 21:41:54 +02:00
|
|
|
return false;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else
|
2008-10-22 21:41:54 +02:00
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-10-22 09:59:01 +02:00
|
|
|
static const struct command *
|
2013-01-03 17:27:26 +01:00
|
|
|
command_checked_lookup(Client *client, unsigned permission,
|
2008-10-22 21:40:44 +02:00
|
|
|
int argc, char *argv[])
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2008-10-22 09:59:01 +02:00
|
|
|
const struct command *cmd;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2012-02-14 18:34:04 +01:00
|
|
|
current_command = "";
|
2004-06-04 03:58:31 +02:00
|
|
|
|
2006-07-29 20:54:56 +02:00
|
|
|
if (argc == 0)
|
2006-07-20 18:02:40 +02:00
|
|
|
return NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-22 10:08:14 +02:00
|
|
|
cmd = command_lookup(argv[0]);
|
|
|
|
if (cmd == NULL) {
|
2008-09-07 13:52:36 +02:00
|
|
|
if (client != NULL)
|
|
|
|
command_error(client, ACK_ERROR_UNKNOWN,
|
|
|
|
"unknown command \"%s\"", argv[0]);
|
2006-07-20 18:02:40 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-06-04 03:58:31 +02:00
|
|
|
current_command = cmd->cmd;
|
|
|
|
|
2008-10-22 21:41:54 +02:00
|
|
|
if (!command_check_request(cmd, client, permission, argc, argv))
|
2004-04-11 19:37:47 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2008-10-22 21:41:49 +02:00
|
|
|
enum command_return
|
2013-01-03 17:27:26 +01:00
|
|
|
command_process(Client *client, unsigned num, char *line)
|
2004-04-11 19:37:47 +02:00
|
|
|
{
|
2009-07-19 15:15:36 +02:00
|
|
|
GError *error = NULL;
|
2006-10-06 12:33:27 +02:00
|
|
|
int argc;
|
|
|
|
char *argv[COMMAND_ARGV_MAX] = { NULL };
|
2008-10-22 09:59:01 +02:00
|
|
|
const struct command *cmd;
|
2008-10-22 21:41:49 +02:00
|
|
|
enum command_return ret = COMMAND_RETURN_ERROR;
|
2004-04-12 03:44:52 +02:00
|
|
|
|
2009-07-29 08:00:01 +02:00
|
|
|
command_list_num = num;
|
|
|
|
|
2009-07-19 15:15:36 +02:00
|
|
|
/* get the command name (first word on the line) */
|
|
|
|
|
|
|
|
argv[0] = tokenizer_next_word(&line, &error);
|
|
|
|
if (argv[0] == NULL) {
|
|
|
|
current_command = "";
|
|
|
|
if (*line == 0)
|
|
|
|
command_error(client, ACK_ERROR_UNKNOWN,
|
|
|
|
"No command given");
|
|
|
|
else {
|
|
|
|
command_error(client, ACK_ERROR_UNKNOWN,
|
|
|
|
"%s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
}
|
|
|
|
current_command = NULL;
|
|
|
|
|
|
|
|
return COMMAND_RETURN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
argc = 1;
|
|
|
|
|
|
|
|
/* now parse the arguments (quoted or unquoted) */
|
|
|
|
|
|
|
|
while (argc < (int)G_N_ELEMENTS(argv) &&
|
|
|
|
(argv[argc] =
|
2009-09-25 00:53:15 +02:00
|
|
|
tokenizer_next_param(&line, &error)) != NULL)
|
2009-07-19 15:15:36 +02:00
|
|
|
++argc;
|
|
|
|
|
|
|
|
/* some error checks; we have to set current_command because
|
|
|
|
command_error() expects it to be set */
|
|
|
|
|
|
|
|
current_command = argv[0];
|
|
|
|
|
|
|
|
if (argc >= (int)G_N_ELEMENTS(argv)) {
|
|
|
|
command_error(client, ACK_ERROR_ARG, "Too many arguments");
|
|
|
|
current_command = NULL;
|
|
|
|
return COMMAND_RETURN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*line != 0) {
|
|
|
|
command_error(client, ACK_ERROR_ARG,
|
|
|
|
"%s", error->message);
|
|
|
|
current_command = NULL;
|
|
|
|
g_error_free(error);
|
|
|
|
return COMMAND_RETURN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look up and invoke the command handler */
|
2004-04-11 19:37:47 +02:00
|
|
|
|
2008-10-22 21:40:44 +02:00
|
|
|
cmd = command_checked_lookup(client, client_get_permission(client),
|
|
|
|
argc, argv);
|
2008-10-06 18:39:33 +02:00
|
|
|
if (cmd)
|
|
|
|
ret = cmd->handler(client, argc, argv);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-06-04 03:58:31 +02:00
|
|
|
current_command = NULL;
|
2008-10-22 21:40:44 +02:00
|
|
|
command_list_num = 0;
|
2004-06-04 03:58:31 +02:00
|
|
|
|
2004-04-12 03:44:52 +02:00
|
|
|
return ret;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|