AllCommands.cxx 13.3 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 The Music Player Daemon Project
3
 * http://www.musicpd.org
Warren Dukes's avatar
Warren Dukes committed
4 5 6 7 8 9 10 11 12 13
 *
 * 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.
14 15 16 17
 *
 * 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.
Warren Dukes's avatar
Warren Dukes committed
18 19
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21
#include "AllCommands.hxx"
22
#include "Request.hxx"
23
#include "QueueCommands.hxx"
24
#include "TagCommands.hxx"
25 26
#include "PlayerCommands.hxx"
#include "PlaylistCommands.hxx"
27
#include "StorageCommands.hxx"
28
#include "DatabaseCommands.hxx"
29
#include "FileCommands.hxx"
30 31
#include "OutputCommands.hxx"
#include "MessageCommands.hxx"
32
#include "NeighborCommands.hxx"
33
#include "OtherCommands.hxx"
34
#include "Permission.hxx"
35
#include "tag/TagType.h"
36
#include "Partition.hxx"
37
#include "client/Client.hxx"
38
#include "client/Response.hxx"
39
#include "util/Macros.hxx"
40
#include "util/Tokenizer.hxx"
41
#include "util/Error.hxx"
42
#include "util/ConstBuffer.hxx"
43
#include "util/StringAPI.hxx"
Warren Dukes's avatar
Warren Dukes committed
44

45
#ifdef ENABLE_SQLITE
Max Kellermann's avatar
Max Kellermann committed
46
#include "StickerCommands.hxx"
47
#include "sticker/StickerDatabase.hxx"
48 49
#endif

Max Kellermann's avatar
Max Kellermann committed
50
#include <assert.h>
51
#include <string.h>
Warren Dukes's avatar
Warren Dukes committed
52

53 54 55 56 57 58 59
/*
 * 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))

Warren Dukes's avatar
Warren Dukes committed
60 61
/* if min: -1 don't check args *
 * if max: -1 no max args      */
62
struct command {
Max Kellermann's avatar
Max Kellermann committed
63
	const char *cmd;
Max Kellermann's avatar
Max Kellermann committed
64
	unsigned permission;
Avuton Olrich's avatar
Avuton Olrich committed
65 66
	int min;
	int max;
67
	CommandResult (*handler)(Client &client, Request request, Response &response);
68 69
};

70
/* don't be fooled, this is the command handler for "commands" command */
71
static CommandResult
72
handle_commands(Client &client, Request request, Response &response);
73

74
static CommandResult
75
handle_not_commands(Client &client, Request request, Response &response);
76

77 78 79 80 81
/**
 * The command registry.
 *
 * This array must be sorted!
 */
82
static constexpr struct command commands[] = {
Max Kellermann's avatar
Max Kellermann committed
83 84
	{ "add", PERMISSION_ADD, 1, 1, handle_add },
	{ "addid", PERMISSION_ADD, 1, 2, handle_addid },
85
	{ "addtagid", PERMISSION_ADD, 3, 3, handle_addtagid },
86
	{ "channels", PERMISSION_READ, 0, 0, handle_channels },
Max Kellermann's avatar
Max Kellermann committed
87 88
	{ "clear", PERMISSION_CONTROL, 0, 0, handle_clear },
	{ "clearerror", PERMISSION_CONTROL, 0, 0, handle_clearerror },
89
	{ "cleartagid", PERMISSION_ADD, 1, 2, handle_cleartagid },
Max Kellermann's avatar
Max Kellermann committed
90 91
	{ "close", PERMISSION_NONE, -1, -1, handle_close },
	{ "commands", PERMISSION_NONE, 0, 0, handle_commands },
92
	{ "config", PERMISSION_ADMIN, 0, 0, handle_config },
93
	{ "consume", PERMISSION_CONTROL, 1, 1, handle_consume },
94
#ifdef ENABLE_DATABASE
Max Kellermann's avatar
Max Kellermann committed
95
	{ "count", PERMISSION_READ, 2, -1, handle_count },
96
#endif
Max Kellermann's avatar
Max Kellermann committed
97 98
	{ "crossfade", PERMISSION_CONTROL, 1, 1, handle_crossfade },
	{ "currentsong", PERMISSION_READ, 0, 0, handle_currentsong },
99
	{ "decoders", PERMISSION_READ, 0, 0, handle_decoders },
Max Kellermann's avatar
Max Kellermann committed
100 101 102 103
	{ "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 },
104
#ifdef ENABLE_DATABASE
Max Kellermann's avatar
Max Kellermann committed
105
	{ "find", PERMISSION_READ, 2, -1, handle_find },
106
	{ "findadd", PERMISSION_ADD, 2, -1, handle_findadd},
107
#endif
108
	{ "idle", PERMISSION_READ, 0, -1, handle_idle },
Max Kellermann's avatar
Max Kellermann committed
109
	{ "kill", PERMISSION_ADMIN, -1, -1, handle_kill },
110
#ifdef ENABLE_DATABASE
Max Kellermann's avatar
Max Kellermann committed
111 112 113
	{ "list", PERMISSION_READ, 1, -1, handle_list },
	{ "listall", PERMISSION_READ, 0, 1, handle_listall },
	{ "listallinfo", PERMISSION_READ, 0, 1, handle_listallinfo },
114 115 116
#endif
	{ "listfiles", PERMISSION_READ, 0, 1, handle_listfiles },
#ifdef ENABLE_DATABASE
117
	{ "listmounts", PERMISSION_READ, 0, 0, handle_listmounts },
118
#endif
119 120 121
#ifdef ENABLE_NEIGHBOR_PLUGINS
	{ "listneighbors", PERMISSION_READ, 0, 0, handle_listneighbors },
#endif
Max Kellermann's avatar
Max Kellermann committed
122 123 124
	{ "listplaylist", PERMISSION_READ, 1, 1, handle_listplaylist },
	{ "listplaylistinfo", PERMISSION_READ, 1, 1, handle_listplaylistinfo },
	{ "listplaylists", PERMISSION_READ, 0, 0, handle_listplaylists },
125
	{ "load", PERMISSION_ADD, 1, 2, handle_load },
Max Kellermann's avatar
Max Kellermann committed
126
	{ "lsinfo", PERMISSION_READ, 0, 1, handle_lsinfo },
127 128
	{ "mixrampdb", PERMISSION_CONTROL, 1, 1, handle_mixrampdb },
	{ "mixrampdelay", PERMISSION_CONTROL, 1, 1, handle_mixrampdelay },
129 130 131
#ifdef ENABLE_DATABASE
	{ "mount", PERMISSION_ADMIN, 2, 2, handle_mount },
#endif
Max Kellermann's avatar
Max Kellermann committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	{ "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 },
151 152
	{ "plchanges", PERMISSION_READ, 1, 2, handle_plchanges },
	{ "plchangesposid", PERMISSION_READ, 1, 2, handle_plchangesposid },
Max Kellermann's avatar
Max Kellermann committed
153
	{ "previous", PERMISSION_CONTROL, 0, 0, handle_previous },
154 155
	{ "prio", PERMISSION_CONTROL, 2, -1, handle_prio },
	{ "prioid", PERMISSION_CONTROL, 2, -1, handle_prioid },
Max Kellermann's avatar
Max Kellermann committed
156
	{ "random", PERMISSION_CONTROL, 1, 1, handle_random },
157
	{ "rangeid", PERMISSION_ADD, 2, 2, handle_rangeid },
158
	{ "readcomments", PERMISSION_READ, 1, 1, handle_read_comments },
159
	{ "readmessages", PERMISSION_READ, 0, 0, handle_read_messages },
Max Kellermann's avatar
Max Kellermann committed
160 161
	{ "rename", PERMISSION_CONTROL, 2, 2, handle_rename },
	{ "repeat", PERMISSION_CONTROL, 1, 1, handle_repeat },
162 163 164 165
	{ "replay_gain_mode", PERMISSION_CONTROL, 1, 1,
	  handle_replay_gain_mode },
	{ "replay_gain_status", PERMISSION_READ, 0, 0,
	  handle_replay_gain_status },
166
	{ "rescan", PERMISSION_CONTROL, 0, 1, handle_rescan },
Max Kellermann's avatar
Max Kellermann committed
167 168
	{ "rm", PERMISSION_CONTROL, 1, 1, handle_rm },
	{ "save", PERMISSION_CONTROL, 1, 1, handle_save },
169
#ifdef ENABLE_DATABASE
Max Kellermann's avatar
Max Kellermann committed
170
	{ "search", PERMISSION_READ, 2, -1, handle_search },
171 172
	{ "searchadd", PERMISSION_ADD, 2, -1, handle_searchadd },
	{ "searchaddpl", PERMISSION_CONTROL, 3, -1, handle_searchaddpl },
173
#endif
Max Kellermann's avatar
Max Kellermann committed
174
	{ "seek", PERMISSION_CONTROL, 2, 2, handle_seek },
175
	{ "seekcur", PERMISSION_CONTROL, 1, 1, handle_seekcur },
Max Kellermann's avatar
Max Kellermann committed
176
	{ "seekid", PERMISSION_CONTROL, 2, 2, handle_seekid },
177
	{ "sendmessage", PERMISSION_CONTROL, 2, 2, handle_send_message },
Max Kellermann's avatar
Max Kellermann committed
178
	{ "setvol", PERMISSION_CONTROL, 1, 1, handle_setvol },
179
	{ "shuffle", PERMISSION_CONTROL, 0, 1, handle_shuffle },
180
	{ "single", PERMISSION_CONTROL, 1, 1, handle_single },
Max Kellermann's avatar
Max Kellermann committed
181 182
	{ "stats", PERMISSION_READ, 0, 0, handle_stats },
	{ "status", PERMISSION_READ, 0, 0, handle_status },
183 184 185
#ifdef ENABLE_SQLITE
	{ "sticker", PERMISSION_ADMIN, 3, -1, handle_sticker },
#endif
Max Kellermann's avatar
Max Kellermann committed
186
	{ "stop", PERMISSION_CONTROL, 0, 0, handle_stop },
187
	{ "subscribe", PERMISSION_READ, 1, 1, handle_subscribe },
Max Kellermann's avatar
Max Kellermann committed
188 189 190
	{ "swap", PERMISSION_CONTROL, 2, 2, handle_swap },
	{ "swapid", PERMISSION_CONTROL, 2, 2, handle_swapid },
	{ "tagtypes", PERMISSION_READ, 0, 0, handle_tagtypes },
191
	{ "toggleoutput", PERMISSION_ADMIN, 1, 1, handle_toggleoutput },
192 193 194
#ifdef ENABLE_DATABASE
	{ "unmount", PERMISSION_ADMIN, 1, 1, handle_unmount },
#endif
195
	{ "unsubscribe", PERMISSION_READ, 1, 1, handle_unsubscribe },
196
	{ "update", PERMISSION_CONTROL, 0, 1, handle_update },
Max Kellermann's avatar
Max Kellermann committed
197
	{ "urlhandlers", PERMISSION_READ, 0, 0, handle_urlhandlers },
198
	{ "volume", PERMISSION_CONTROL, 1, 1, handle_volume },
199 200
};

201
static constexpr unsigned num_commands = ARRAY_SIZE(commands);
202

203
static bool
204 205
command_available(gcc_unused const Partition &partition,
		  gcc_unused const struct command *cmd)
206 207
{
#ifdef ENABLE_SQLITE
208
	if (StringIsEqual(cmd->cmd, "sticker"))
209 210 211
		return sticker_enabled();
#endif

212
#ifdef ENABLE_NEIGHBOR_PLUGINS
213
	if (StringIsEqual(cmd->cmd, "listneighbors"))
214
		return neighbor_commands_available(partition.instance);
215 216
#endif

217 218 219 220 221 222 223 224
	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"))
225 226
		return playlist_commands_available();

227 228 229
	return true;
}

230
static CommandResult
231 232
PrintAvailableCommands(Response &r, const Partition &partition,
		     unsigned permission)
233 234
{
	for (unsigned i = 0; i < num_commands; ++i) {
235
		const struct command *cmd = &commands[i];
236

237
		if (cmd->permission == (permission & cmd->permission) &&
238 239
		    command_available(partition, cmd))
			r.Format("command: %s\n", cmd->cmd);
240 241
	}

242
	return CommandResult::OK;
243 244
}

245
static CommandResult
246
PrintUnavailableCommands(Response &r, unsigned permission)
247 248
{
	for (unsigned i = 0; i < num_commands; ++i) {
249
		const struct command *cmd = &commands[i];
250

Max Kellermann's avatar
Max Kellermann committed
251
		if (cmd->permission != (permission & cmd->permission))
252
			r.Format("command: %s\n", cmd->cmd);
253 254
	}

255
	return CommandResult::OK;
256 257
}

258 259
/* don't be fooled, this is the command handler for "commands" command */
static CommandResult
260
handle_commands(Client &client, gcc_unused Request request, Response &r)
261 262 263 264 265 266
{
	return PrintAvailableCommands(r, client.partition,
				      client.GetPermission());
}

static CommandResult
267
handle_not_commands(Client &client, gcc_unused Request request, Response &r)
268 269 270 271
{
	return PrintUnavailableCommands(r, client.GetPermission());
}

272 273
void
command_init()
Avuton Olrich's avatar
Avuton Olrich committed
274
{
275 276 277 278 279
#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
Avuton Olrich's avatar
Avuton Olrich committed
280 281
}

282 283
void
command_finish()
Avuton Olrich's avatar
Avuton Olrich committed
284
{
285 286 287 288 289 290 291 292 293 294 295
}

static const struct command *
command_lookup(const char *name)
{
	unsigned a = 0, b = num_commands, i;

	/* binary search */
	do {
		i = (a + b) / 2;

296
		const auto cmp = strcmp(name, commands[i].cmd);
297 298 299 300 301 302 303 304
		if (cmp == 0)
			return &commands[i];
		else if (cmp < 0)
			b = i;
		else if (cmp > 0)
			a = i + 1;
	} while (a < b);

305
	return nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
306 307
}

308
static bool
309
command_check_request(const struct command *cmd, Response &r,
310
		      unsigned permission, Request args)
Avuton Olrich's avatar
Avuton Olrich committed
311
{
Max Kellermann's avatar
Max Kellermann committed
312
	if (cmd->permission != (permission & cmd->permission)) {
313
		r.FormatError(ACK_ERROR_PERMISSION,
314 315
			      "you don't have permission for \"%s\"",
			      cmd->cmd);
316
		return false;
Warren Dukes's avatar
Warren Dukes committed
317 318
	}

319 320 321 322
	const int min = cmd->min;
	const int max = cmd->max;

	if (min < 0)
323
		return true;
Warren Dukes's avatar
Warren Dukes committed
324

325
	if (min == max && unsigned(max) != args.size) {
326
		r.FormatError(ACK_ERROR_ARG,
327
			      "wrong number of arguments for \"%s\"",
328
			      cmd->cmd);
329
		return false;
330
	} else if (args.size < unsigned(min)) {
331
		r.FormatError(ACK_ERROR_ARG,
332
			      "too few arguments for \"%s\"", cmd->cmd);
333
		return false;
334
	} else if (max >= 0 && args.size > unsigned(max)) {
335
		r.FormatError(ACK_ERROR_ARG,
336
			      "too many arguments for \"%s\"", cmd->cmd);
337
		return false;
Avuton Olrich's avatar
Avuton Olrich committed
338
	} else
339
		return true;
Warren Dukes's avatar
Warren Dukes committed
340 341
}

342
static const struct command *
343
command_checked_lookup(Response &r, unsigned permission,
344
		       const char *cmd_name, Request args)
Warren Dukes's avatar
Warren Dukes committed
345
{
346
	const struct command *cmd = command_lookup(cmd_name);
347
	if (cmd == nullptr) {
348
		r.FormatError(ACK_ERROR_UNKNOWN,
349
			      "unknown command \"%s\"", cmd_name);
350
		return nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
351
	}
Warren Dukes's avatar
Warren Dukes committed
352

353
	r.SetCommand(cmd->cmd);
Warren Dukes's avatar
Warren Dukes committed
354

355
	if (!command_check_request(cmd, r, permission, args))
356
		return nullptr;
357 358 359 360

	return cmd;
}

361
CommandResult
362
command_process(Client &client, unsigned num, char *line)
363
{
364
	Response r(client, num);
365
	Error error;
366

367
	/* get the command name (first word on the line) */
368
	/* we have to set current_command because Response::Error()
369
	   expects it to be set */
370

371
	Tokenizer tokenizer(line);
372

373
	const char *const cmd_name = tokenizer.NextWord(error);
374
	if (cmd_name == nullptr) {
375
		if (tokenizer.IsEnd())
376
			r.FormatError(ACK_ERROR_UNKNOWN, "No command given");
377
		else
378
			r.Error(ACK_ERROR_UNKNOWN, error.GetMessage());
379

380 381 382
		/* this client does not speak the MPD protocol; kick
		   the connection */
		return CommandResult::FINISH;
383 384
	}

385
	char *argv[COMMAND_ARGV_MAX];
386
	Request args(argv, 0);
387 388 389

	/* now parse the arguments (quoted or unquoted) */

390
	while (true) {
391
		if (args.size == COMMAND_ARGV_MAX) {
392
			r.Error(ACK_ERROR_ARG, "Too many arguments");
393 394 395 396 397 398 399 400
			return CommandResult::ERROR;
		}

		char *a = tokenizer.NextParam(error);
		if (a == nullptr) {
			if (tokenizer.IsEnd())
				break;

401
			r.Error(ACK_ERROR_UNKNOWN, error.GetMessage());
402 403 404
			return CommandResult::ERROR;
		}

405
		argv[args.size++] = a;
406 407 408
	}

	/* look up and invoke the command handler */
409

410
	const struct command *cmd =
411
		command_checked_lookup(r, client.GetPermission(),
412
				       cmd_name, args);
413 414

	CommandResult ret = cmd
415
		? cmd->handler(client, args, r)
416
		: CommandResult::ERROR;
Warren Dukes's avatar
Warren Dukes committed
417

418
	return ret;
Warren Dukes's avatar
Warren Dukes committed
419
}