2009-03-13 18:43:16 +01:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 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"
|
2015-12-15 23:17:34 +01:00
|
|
|
#include "CommandError.hxx"
|
2015-08-11 22:11:28 +02:00
|
|
|
#include "Request.hxx"
|
2012-08-25 12:59:54 +02:00
|
|
|
#include "QueueCommands.hxx"
|
2013-12-02 12:00:32 +01:00
|
|
|
#include "TagCommands.hxx"
|
2012-08-25 12:59:54 +02:00
|
|
|
#include "PlayerCommands.hxx"
|
|
|
|
#include "PlaylistCommands.hxx"
|
2014-02-09 08:24:16 +01:00
|
|
|
#include "StorageCommands.hxx"
|
2012-08-07 21:55:11 +02:00
|
|
|
#include "DatabaseCommands.hxx"
|
2013-10-25 19:35:40 +02:00
|
|
|
#include "FileCommands.hxx"
|
2012-08-25 12:59:54 +02:00
|
|
|
#include "OutputCommands.hxx"
|
|
|
|
#include "MessageCommands.hxx"
|
2014-01-18 16:36:42 +01:00
|
|
|
#include "NeighborCommands.hxx"
|
2017-02-08 07:39:40 +01:00
|
|
|
#include "ClientCommands.hxx"
|
2017-02-17 23:07:31 +01:00
|
|
|
#include "PartitionCommands.hxx"
|
2019-04-05 12:38:49 +02:00
|
|
|
#include "FingerprintCommands.hxx"
|
2012-09-25 11:44:49 +02:00
|
|
|
#include "OtherCommands.hxx"
|
2013-01-03 03:06:45 +01:00
|
|
|
#include "Permission.hxx"
|
2017-02-08 08:26:58 +01:00
|
|
|
#include "tag/Type.h"
|
2014-02-04 11:33:53 +01:00
|
|
|
#include "Partition.hxx"
|
2019-04-24 15:18:01 +02:00
|
|
|
#include "Instance.hxx"
|
2014-01-24 00:26:53 +01:00
|
|
|
#include "client/Client.hxx"
|
2015-08-06 22:10:25 +02:00
|
|
|
#include "client/Response.hxx"
|
2013-04-08 23:51:39 +02:00
|
|
|
#include "util/Tokenizer.hxx"
|
2015-08-14 19:48:30 +02:00
|
|
|
#include "util/StringAPI.hxx"
|
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"
|
2009-01-19 19:09:49 +01:00
|
|
|
#endif
|
|
|
|
|
2021-05-21 20:35:29 +02:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2019-08-03 13:10:49 +02:00
|
|
|
#include <iterator>
|
|
|
|
|
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;
|
2015-08-13 12:48:31 +02:00
|
|
|
CommandResult (*handler)(Client &client, Request request, Response &response);
|
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 */
|
2013-10-20 13:10:54 +02:00
|
|
|
static CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_commands(Client &client, Request request, Response &response);
|
2004-11-08 19:01:36 +01:00
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
static CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_not_commands(Client &client, Request request, Response &response);
|
2004-11-23 20:46:33 +01:00
|
|
|
|
2008-10-22 10:08:14 +02:00
|
|
|
/**
|
|
|
|
* The command registry.
|
|
|
|
*
|
|
|
|
* This array must be sorted!
|
|
|
|
*/
|
2014-12-06 00:07:07 +01:00
|
|
|
static constexpr struct command commands[] = {
|
2021-10-23 13:00:34 +02:00
|
|
|
{ "add", PERMISSION_ADD, 1, 2, handle_add },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "addid", PERMISSION_ADD, 1, 2, handle_addid },
|
2013-12-02 12:00:32 +01:00
|
|
|
{ "addtagid", PERMISSION_ADD, 3, 3, handle_addtagid },
|
2017-07-21 11:49:28 +02:00
|
|
|
{ "albumart", PERMISSION_READ, 2, 2, handle_album_art },
|
2021-01-21 16:27:52 +01:00
|
|
|
{ "binarylimit", PERMISSION_NONE, 1, 1, handle_binary_limit },
|
2011-01-29 09:26:22 +01:00
|
|
|
{ "channels", PERMISSION_READ, 0, 0, handle_channels },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "clear", PERMISSION_PLAYER, 0, 0, handle_clear },
|
|
|
|
{ "clearerror", PERMISSION_PLAYER, 0, 0, handle_clearerror },
|
2013-12-02 12:00:32 +01:00
|
|
|
{ "cleartagid", PERMISSION_ADD, 1, 2, handle_cleartagid },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "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 },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "consume", PERMISSION_PLAYER, 1, 1, handle_consume },
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2018-07-24 19:21:51 +02:00
|
|
|
{ "count", PERMISSION_READ, 1, -1, handle_count },
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "crossfade", PERMISSION_PLAYER, 1, 1, handle_crossfade },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "currentsong", PERMISSION_READ, 0, 0, handle_currentsong },
|
2009-11-07 15:57:22 +01:00
|
|
|
{ "decoders", PERMISSION_READ, 0, 0, handle_decoders },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "delete", PERMISSION_PLAYER, 1, 1, handle_delete },
|
|
|
|
{ "deleteid", PERMISSION_PLAYER, 1, 1, handle_deleteid },
|
2020-01-20 09:10:02 +01:00
|
|
|
{ "delpartition", PERMISSION_ADMIN, 1, 1, handle_delpartition },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "disableoutput", PERMISSION_ADMIN, 1, 1, handle_disableoutput },
|
|
|
|
{ "enableoutput", PERMISSION_ADMIN, 1, 1, handle_enableoutput },
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2018-07-24 19:21:51 +02:00
|
|
|
{ "find", PERMISSION_READ, 1, -1, handle_find },
|
|
|
|
{ "findadd", PERMISSION_ADD, 1, -1, handle_findadd},
|
2019-04-05 12:38:49 +02:00
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_CHROMAPRINT
|
|
|
|
{ "getfingerprint", PERMISSION_READ, 1, 1, handle_getfingerprint },
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
2020-10-16 17:46:58 +02:00
|
|
|
{ "getvol", PERMISSION_READ, 0, 0, handle_getvol },
|
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 },
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "list", PERMISSION_READ, 1, -1, handle_list },
|
|
|
|
{ "listall", PERMISSION_READ, 0, 1, handle_listall },
|
|
|
|
{ "listallinfo", PERMISSION_READ, 0, 1, handle_listallinfo },
|
2014-02-28 19:02:23 +01:00
|
|
|
#endif
|
|
|
|
{ "listfiles", PERMISSION_READ, 0, 1, handle_listfiles },
|
|
|
|
#ifdef ENABLE_DATABASE
|
2014-02-12 21:46:32 +01:00
|
|
|
{ "listmounts", PERMISSION_READ, 0, 0, handle_listmounts },
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
2014-01-18 16:36:42 +01:00
|
|
|
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
|
|
|
{ "listneighbors", PERMISSION_READ, 0, 0, handle_listneighbors },
|
|
|
|
#endif
|
2017-02-17 23:07:31 +01:00
|
|
|
{ "listpartitions", PERMISSION_READ, 0, 0, handle_listpartitions },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "listplaylist", PERMISSION_READ, 1, 1, handle_listplaylist },
|
|
|
|
{ "listplaylistinfo", PERMISSION_READ, 1, 1, handle_listplaylistinfo },
|
|
|
|
{ "listplaylists", PERMISSION_READ, 0, 0, handle_listplaylists },
|
2021-10-14 15:11:04 +02:00
|
|
|
{ "load", PERMISSION_ADD, 1, 3, handle_load },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "lsinfo", PERMISSION_READ, 0, 1, handle_lsinfo },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "mixrampdb", PERMISSION_PLAYER, 1, 1, handle_mixrampdb },
|
|
|
|
{ "mixrampdelay", PERMISSION_PLAYER, 1, 1, handle_mixrampdelay },
|
2014-02-09 08:24:16 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
{ "mount", PERMISSION_ADMIN, 2, 2, handle_mount },
|
|
|
|
#endif
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "move", PERMISSION_PLAYER, 2, 2, handle_move },
|
|
|
|
{ "moveid", PERMISSION_PLAYER, 2, 2, handle_moveid },
|
2019-09-26 13:02:34 +02:00
|
|
|
{ "moveoutput", PERMISSION_ADMIN, 1, 1, handle_moveoutput },
|
2017-02-17 23:38:30 +01:00
|
|
|
{ "newpartition", PERMISSION_ADMIN, 1, 1, handle_newpartition },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "next", PERMISSION_PLAYER, 0, 0, handle_next },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "notcommands", PERMISSION_NONE, 0, 0, handle_not_commands },
|
|
|
|
{ "outputs", PERMISSION_READ, 0, 0, handle_devices },
|
2017-12-19 08:45:34 +01:00
|
|
|
{ "outputset", PERMISSION_ADMIN, 3, 3, handle_outputset },
|
2017-02-20 12:37:14 +01:00
|
|
|
{ "partition", PERMISSION_READ, 1, 1, handle_partition },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "password", PERMISSION_NONE, 1, 1, handle_password },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "pause", PERMISSION_PLAYER, 0, 1, handle_pause },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "ping", PERMISSION_NONE, 0, 0, handle_ping },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "play", PERMISSION_PLAYER, 0, 1, handle_play },
|
|
|
|
{ "playid", PERMISSION_PLAYER, 0, 1, handle_playid },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "playlist", PERMISSION_READ, 0, 0, handle_playlist },
|
2021-10-25 08:43:42 +02:00
|
|
|
{ "playlistadd", PERMISSION_CONTROL, 2, 3, handle_playlistadd },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "playlistclear", PERMISSION_CONTROL, 1, 1, handle_playlistclear },
|
|
|
|
{ "playlistdelete", PERMISSION_CONTROL, 2, 2, handle_playlistdelete },
|
2018-07-24 19:21:51 +02:00
|
|
|
{ "playlistfind", PERMISSION_READ, 1, -1, handle_playlistfind },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "playlistid", PERMISSION_READ, 0, 1, handle_playlistid },
|
|
|
|
{ "playlistinfo", PERMISSION_READ, 0, 1, handle_playlistinfo },
|
|
|
|
{ "playlistmove", PERMISSION_CONTROL, 3, 3, handle_playlistmove },
|
2018-07-24 19:21:51 +02:00
|
|
|
{ "playlistsearch", PERMISSION_READ, 1, -1, handle_playlistsearch },
|
2015-10-27 20:35:40 +01:00
|
|
|
{ "plchanges", PERMISSION_READ, 1, 2, handle_plchanges },
|
|
|
|
{ "plchangesposid", PERMISSION_READ, 1, 2, handle_plchangesposid },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "previous", PERMISSION_PLAYER, 0, 0, handle_previous },
|
|
|
|
{ "prio", PERMISSION_PLAYER, 2, -1, handle_prio },
|
|
|
|
{ "prioid", PERMISSION_PLAYER, 2, -1, handle_prioid },
|
|
|
|
{ "random", PERMISSION_PLAYER, 1, 1, handle_random },
|
2014-07-12 03:00:01 +02:00
|
|
|
{ "rangeid", PERMISSION_ADD, 2, 2, handle_rangeid },
|
2013-10-25 19:35:40 +02:00
|
|
|
{ "readcomments", PERMISSION_READ, 1, 1, handle_read_comments },
|
2011-01-29 09:26:22 +01:00
|
|
|
{ "readmessages", PERMISSION_READ, 0, 0, handle_read_messages },
|
2019-08-12 14:01:14 +02:00
|
|
|
{ "readpicture", PERMISSION_READ, 2, 2, handle_read_picture },
|
2008-10-22 21:40:44 +02:00
|
|
|
{ "rename", PERMISSION_CONTROL, 2, 2, handle_rename },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "repeat", PERMISSION_PLAYER, 1, 1, handle_repeat },
|
|
|
|
{ "replay_gain_mode", PERMISSION_PLAYER, 1, 1,
|
2009-10-17 22:58:19 +02:00
|
|
|
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 },
|
2014-01-30 20:29:48 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
2018-07-24 19:21:51 +02:00
|
|
|
{ "search", PERMISSION_READ, 1, -1, handle_search },
|
|
|
|
{ "searchadd", PERMISSION_ADD, 1, -1, handle_searchadd },
|
|
|
|
{ "searchaddpl", PERMISSION_CONTROL, 2, -1, handle_searchaddpl },
|
2014-01-30 20:29:48 +01:00
|
|
|
#endif
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "seek", PERMISSION_PLAYER, 2, 2, handle_seek },
|
|
|
|
{ "seekcur", PERMISSION_PLAYER, 1, 1, handle_seekcur },
|
|
|
|
{ "seekid", PERMISSION_PLAYER, 2, 2, handle_seekid },
|
2011-01-29 09:26:22 +01:00
|
|
|
{ "sendmessage", PERMISSION_CONTROL, 2, 2, handle_send_message },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "setvol", PERMISSION_PLAYER, 1, 1, handle_setvol },
|
|
|
|
{ "shuffle", PERMISSION_PLAYER, 0, 1, handle_shuffle },
|
|
|
|
{ "single", PERMISSION_PLAYER, 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
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "stop", PERMISSION_PLAYER, 0, 0, handle_stop },
|
2011-01-29 09:26:22 +01:00
|
|
|
{ "subscribe", PERMISSION_READ, 1, 1, handle_subscribe },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "swap", PERMISSION_PLAYER, 2, 2, handle_swap },
|
|
|
|
{ "swapid", PERMISSION_PLAYER, 2, 2, handle_swapid },
|
2020-04-30 13:07:42 +02:00
|
|
|
{ "tagtypes", PERMISSION_NONE, 0, -1, handle_tagtypes },
|
2013-08-04 23:20:56 +02:00
|
|
|
{ "toggleoutput", PERMISSION_ADMIN, 1, 1, handle_toggleoutput },
|
2014-02-12 21:45:04 +01:00
|
|
|
#ifdef ENABLE_DATABASE
|
|
|
|
{ "unmount", PERMISSION_ADMIN, 1, 1, handle_unmount },
|
|
|
|
#endif
|
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 },
|
2021-10-14 14:16:34 +02:00
|
|
|
{ "volume", PERMISSION_PLAYER, 1, 1, handle_volume },
|
2008-10-22 10:08:14 +02:00
|
|
|
};
|
|
|
|
|
2019-08-03 13:10:49 +02:00
|
|
|
static constexpr unsigned num_commands = std::size(commands);
|
2008-10-22 10:08:14 +02:00
|
|
|
|
2019-08-20 20:27:15 +02:00
|
|
|
gcc_pure
|
2009-01-23 18:51:14 +01:00
|
|
|
static bool
|
2020-03-12 20:56:11 +01:00
|
|
|
command_available([[maybe_unused]] const Partition &partition,
|
|
|
|
[[maybe_unused]] const struct command *cmd) noexcept
|
2009-01-23 18:51:14 +01:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_SQLITE
|
2015-08-14 19:48:30 +02:00
|
|
|
if (StringIsEqual(cmd->cmd, "sticker"))
|
2019-04-24 15:18:01 +02:00
|
|
|
return partition.instance.HasStickerDatabase();
|
2009-01-23 18:51:14 +01:00
|
|
|
#endif
|
|
|
|
|
2014-01-18 16:36:42 +01:00
|
|
|
#ifdef ENABLE_NEIGHBOR_PLUGINS
|
2015-08-14 19:48:30 +02:00
|
|
|
if (StringIsEqual(cmd->cmd, "listneighbors"))
|
2014-02-04 11:33:53 +01:00
|
|
|
return neighbor_commands_available(partition.instance);
|
2014-01-18 16:36:42 +01:00
|
|
|
#endif
|
|
|
|
|
2015-08-14 19:48:30 +02:00
|
|
|
if (StringIsEqual(cmd->cmd, "save") ||
|
|
|
|
StringIsEqual(cmd->cmd, "rm") ||
|
|
|
|
StringIsEqual(cmd->cmd, "rename") ||
|
|
|
|
StringIsEqual(cmd->cmd, "playlistdelete") ||
|
|
|
|
StringIsEqual(cmd->cmd, "playlistmove") ||
|
|
|
|
StringIsEqual(cmd->cmd, "playlistclear") ||
|
|
|
|
StringIsEqual(cmd->cmd, "playlistadd") ||
|
|
|
|
StringIsEqual(cmd->cmd, "listplaylists"))
|
2014-10-25 23:49:35 +02:00
|
|
|
return playlist_commands_available();
|
|
|
|
|
2009-01-23 18:51:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
static CommandResult
|
2015-08-06 22:10:25 +02:00
|
|
|
PrintAvailableCommands(Response &r, const Partition &partition,
|
2019-08-20 20:27:15 +02:00
|
|
|
unsigned permission) noexcept
|
2008-10-22 10:08:14 +02:00
|
|
|
{
|
2020-02-01 05:09:15 +01:00
|
|
|
for (const auto & i : commands) {
|
|
|
|
const struct command *cmd = &i;
|
2008-10-22 10:08:14 +02:00
|
|
|
|
2009-01-23 18:51:14 +01:00
|
|
|
if (cmd->permission == (permission & cmd->permission) &&
|
2015-08-06 22:10:25 +02:00
|
|
|
command_available(partition, cmd))
|
2021-05-21 20:35:29 +02:00
|
|
|
r.Fmt(FMT_STRING("command: {}\n"), cmd->cmd);
|
2008-10-22 10:08:14 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2008-10-22 10:08:14 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
static CommandResult
|
2019-08-20 20:27:15 +02:00
|
|
|
PrintUnavailableCommands(Response &r, unsigned permission) noexcept
|
2008-10-22 10:08:14 +02:00
|
|
|
{
|
2020-02-01 05:09:15 +01:00
|
|
|
for (const auto & i : commands) {
|
|
|
|
const struct command *cmd = &i;
|
2008-10-22 10:08:14 +02:00
|
|
|
|
2008-10-22 21:40:44 +02:00
|
|
|
if (cmd->permission != (permission & cmd->permission))
|
2021-05-21 20:35:29 +02:00
|
|
|
r.Fmt(FMT_STRING("command: {}\n"), cmd->cmd);
|
2008-10-22 10:08:14 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2008-10-22 10:08:14 +02:00
|
|
|
}
|
|
|
|
|
2015-08-06 22:10:25 +02:00
|
|
|
/* don't be fooled, this is the command handler for "commands" command */
|
|
|
|
static CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_commands(Client &client, [[maybe_unused]] Request request, Response &r)
|
2015-08-06 22:10:25 +02:00
|
|
|
{
|
2017-02-25 09:58:58 +01:00
|
|
|
return PrintAvailableCommands(r, client.GetPartition(),
|
2015-08-06 22:10:25 +02:00
|
|
|
client.GetPermission());
|
|
|
|
}
|
|
|
|
|
|
|
|
static CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_not_commands(Client &client, [[maybe_unused]] Request request, Response &r)
|
2015-08-06 22:10:25 +02:00
|
|
|
{
|
|
|
|
return PrintUnavailableCommands(r, client.GetPermission());
|
|
|
|
}
|
|
|
|
|
2015-03-03 20:05:08 +01:00
|
|
|
void
|
2019-08-20 20:27:15 +02:00
|
|
|
command_init() noexcept
|
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
|
|
|
}
|
|
|
|
|
2019-08-20 20:27:15 +02:00
|
|
|
gcc_pure
|
2008-10-22 10:08:14 +02:00
|
|
|
static const struct command *
|
2019-08-20 20:27:15 +02:00
|
|
|
command_lookup(const char *name) noexcept
|
2008-10-22 10:08:14 +02:00
|
|
|
{
|
|
|
|
unsigned a = 0, b = num_commands, i;
|
|
|
|
|
|
|
|
/* binary search */
|
|
|
|
do {
|
|
|
|
i = (a + b) / 2;
|
|
|
|
|
2014-12-06 00:10:34 +01:00
|
|
|
const auto cmp = strcmp(name, commands[i].cmd);
|
2008-10-22 10:08:14 +02:00
|
|
|
if (cmp == 0)
|
|
|
|
return &commands[i];
|
|
|
|
else if (cmp < 0)
|
|
|
|
b = i;
|
|
|
|
else if (cmp > 0)
|
|
|
|
a = i + 1;
|
|
|
|
} while (a < b);
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
|
2008-10-22 21:41:54 +02:00
|
|
|
static bool
|
2015-08-13 12:48:31 +02:00
|
|
|
command_check_request(const struct command *cmd, Response &r,
|
2019-08-20 20:27:15 +02:00
|
|
|
unsigned permission, Request args) noexcept
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-22 21:40:44 +02:00
|
|
|
if (cmd->permission != (permission & cmd->permission)) {
|
2021-05-25 16:11:35 +02:00
|
|
|
r.FmtError(ACK_ERROR_PERMISSION,
|
|
|
|
FMT_STRING("you don't have permission for \"{}\""),
|
|
|
|
cmd->cmd);
|
2008-10-22 21:41:54 +02:00
|
|
|
return false;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
const int min = cmd->min;
|
|
|
|
const int max = cmd->max;
|
|
|
|
|
|
|
|
if (min < 0)
|
2008-10-22 21:41:54 +02:00
|
|
|
return true;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
if (min == max && unsigned(max) != args.size) {
|
2021-05-25 16:11:35 +02:00
|
|
|
r.FmtError(ACK_ERROR_ARG,
|
|
|
|
FMT_STRING("wrong number of arguments for \"{}\""),
|
|
|
|
cmd->cmd);
|
2008-10-22 21:41:54 +02:00
|
|
|
return false;
|
2014-12-06 00:08:08 +01:00
|
|
|
} else if (args.size < unsigned(min)) {
|
2021-05-25 16:11:35 +02:00
|
|
|
r.FmtError(ACK_ERROR_ARG,
|
|
|
|
FMT_STRING("too few arguments for \"{}\""),
|
|
|
|
cmd->cmd);
|
2008-10-22 21:41:54 +02:00
|
|
|
return false;
|
2014-12-06 00:08:08 +01:00
|
|
|
} else if (max >= 0 && args.size > unsigned(max)) {
|
2021-05-25 16:11:35 +02:00
|
|
|
r.FmtError(ACK_ERROR_ARG,
|
|
|
|
FMT_STRING("too many arguments for \"{}\""),
|
|
|
|
cmd->cmd);
|
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 *
|
2015-08-13 12:48:31 +02:00
|
|
|
command_checked_lookup(Response &r, unsigned permission,
|
2019-08-20 20:27:15 +02:00
|
|
|
const char *cmd_name, Request args) noexcept
|
2004-02-24 00:41:20 +01:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
const struct command *cmd = command_lookup(cmd_name);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (cmd == nullptr) {
|
2021-05-25 16:11:35 +02:00
|
|
|
r.FmtError(ACK_ERROR_UNKNOWN,
|
|
|
|
FMT_STRING("unknown command \"{}\""), cmd_name);
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2015-08-14 19:02:08 +02:00
|
|
|
r.SetCommand(cmd->cmd);
|
2004-06-04 03:58:31 +02:00
|
|
|
|
2015-08-13 12:48:31 +02:00
|
|
|
if (!command_check_request(cmd, r, permission, args))
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2004-04-11 19:37:47 +02:00
|
|
|
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2019-08-20 20:27:15 +02:00
|
|
|
command_process(Client &client, unsigned num, char *line) noexcept
|
2019-08-20 20:31:36 +02:00
|
|
|
{
|
2015-08-14 19:00:02 +02:00
|
|
|
Response r(client, num);
|
2004-04-12 03:44:52 +02:00
|
|
|
|
2009-07-19 15:15:36 +02:00
|
|
|
/* get the command name (first word on the line) */
|
2015-08-06 22:10:25 +02:00
|
|
|
/* we have to set current_command because Response::Error()
|
2014-12-08 13:25:00 +01:00
|
|
|
expects it to be set */
|
2009-07-19 15:15:36 +02:00
|
|
|
|
2013-04-08 23:51:39 +02:00
|
|
|
Tokenizer tokenizer(line);
|
2014-12-06 00:10:34 +01:00
|
|
|
|
2015-12-16 11:34:26 +01:00
|
|
|
const char *cmd_name;
|
|
|
|
try {
|
|
|
|
cmd_name = tokenizer.NextWord();
|
|
|
|
if (cmd_name == nullptr) {
|
|
|
|
r.Error(ACK_ERROR_UNKNOWN, "No command given");
|
|
|
|
/* this client does not speak the MPD
|
|
|
|
protocol; kick the connection */
|
|
|
|
return CommandResult::FINISH;
|
|
|
|
}
|
|
|
|
} catch (const std::exception &e) {
|
|
|
|
r.Error(ACK_ERROR_UNKNOWN, e.what());
|
2014-08-12 15:33:45 +02:00
|
|
|
/* this client does not speak the MPD protocol; kick
|
|
|
|
the connection */
|
|
|
|
return CommandResult::FINISH;
|
2009-07-19 15:15:36 +02:00
|
|
|
}
|
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
char *argv[COMMAND_ARGV_MAX];
|
2015-08-11 22:11:28 +02:00
|
|
|
Request args(argv, 0);
|
2009-07-19 15:15:36 +02:00
|
|
|
|
2019-08-20 20:31:36 +02:00
|
|
|
try {
|
|
|
|
/* now parse the arguments (quoted or unquoted) */
|
2014-12-08 13:25:41 +01:00
|
|
|
|
2019-08-20 20:31:36 +02:00
|
|
|
while (true) {
|
|
|
|
if (args.size == COMMAND_ARGV_MAX) {
|
|
|
|
r.Error(ACK_ERROR_ARG, "Too many arguments");
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
2014-12-08 13:25:41 +01:00
|
|
|
|
2019-08-20 20:31:36 +02:00
|
|
|
char *a = tokenizer.NextParam();
|
|
|
|
if (a == nullptr)
|
|
|
|
break;
|
2009-07-19 15:15:36 +02:00
|
|
|
|
2019-08-20 20:31:36 +02:00
|
|
|
argv[args.size++] = a;
|
|
|
|
}
|
2004-04-11 19:37:47 +02:00
|
|
|
|
2019-08-20 20:31:36 +02:00
|
|
|
/* look up and invoke the command handler */
|
2014-12-06 00:10:34 +01:00
|
|
|
|
2019-08-20 20:31:36 +02:00
|
|
|
const struct command *cmd =
|
|
|
|
command_checked_lookup(r, client.GetPermission(),
|
|
|
|
cmd_name, args);
|
|
|
|
if (cmd == nullptr)
|
|
|
|
return CommandResult::ERROR;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2019-08-20 20:31:36 +02:00
|
|
|
return cmd->handler(client, args, r);
|
|
|
|
} catch (...) {
|
|
|
|
PrintError(r, std::current_exception());
|
|
|
|
return CommandResult::ERROR;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|