AllCommands.cxx 13.4 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 "QueueCommands.hxx"
23
#include "TagCommands.hxx"
24 25
#include "PlayerCommands.hxx"
#include "PlaylistCommands.hxx"
26
#include "StorageCommands.hxx"
27
#include "DatabaseCommands.hxx"
28
#include "FileCommands.hxx"
29 30
#include "OutputCommands.hxx"
#include "MessageCommands.hxx"
31
#include "NeighborCommands.hxx"
32
#include "OtherCommands.hxx"
33
#include "Permission.hxx"
34
#include "tag/TagType.h"
Max Kellermann's avatar
Max Kellermann committed
35
#include "protocol/Result.hxx"
36
#include "Partition.hxx"
37
#include "client/Client.hxx"
38
#include "util/Macros.hxx"
39
#include "util/Tokenizer.hxx"
40
#include "util/Error.hxx"
41
#include "util/ConstBuffer.hxx"
Warren Dukes's avatar
Warren Dukes committed
42

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

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

51 52 53 54 55 56 57
/*
 * 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
58 59
/* if min: -1 don't check args *
 * if max: -1 no max args      */
60
struct command {
Max Kellermann's avatar
Max Kellermann committed
61
	const char *cmd;
Max Kellermann's avatar
Max Kellermann committed
62
	unsigned permission;
Avuton Olrich's avatar
Avuton Olrich committed
63 64
	int min;
	int max;
65
	CommandResult (*handler)(Client &client, ConstBuffer<const char *> args);
66 67
};

68
/* don't be fooled, this is the command handler for "commands" command */
69
static CommandResult
70
handle_commands(Client &client, ConstBuffer<const char *> args);
71

72
static CommandResult
73
handle_not_commands(Client &client, ConstBuffer<const char *> args);
74

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

199
static constexpr unsigned num_commands = ARRAY_SIZE(commands);
200

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

210 211
#ifdef ENABLE_NEIGHBOR_PLUGINS
	if (strcmp(cmd->cmd, "listneighbors") == 0)
212
		return neighbor_commands_available(partition.instance);
213 214
#endif

215 216 217 218 219 220 221 222 223 224
	if (strcmp(cmd->cmd, "save") == 0 ||
	    strcmp(cmd->cmd, "rm") == 0 ||
	    strcmp(cmd->cmd, "rename") == 0 ||
	    strcmp(cmd->cmd, "playlistdelete") == 0 ||
	    strcmp(cmd->cmd, "playlistmove") == 0 ||
	    strcmp(cmd->cmd, "playlistclear") == 0 ||
	    strcmp(cmd->cmd, "playlistadd") == 0 ||
	    strcmp(cmd->cmd, "listplaylists") == 0)
		return playlist_commands_available();

225 226 227
	return true;
}

228
/* don't be fooled, this is the command handler for "commands" command */
229
static CommandResult
230
handle_commands(Client &client, gcc_unused ConstBuffer<const char *> args)
231
{
232
	const unsigned permission = client.GetPermission();
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
		    command_available(client.partition, cmd))
239 240 241
			client_printf(client, "command: %s\n", cmd->cmd);
	}

242
	return CommandResult::OK;
243 244
}

245
static CommandResult
246
handle_not_commands(Client &client, gcc_unused ConstBuffer<const char *> args)
247
{
248
	const unsigned permission = client.GetPermission();
249 250

	for (unsigned i = 0; i < num_commands; ++i) {
251
		const struct command *cmd = &commands[i];
252

Max Kellermann's avatar
Max Kellermann committed
253
		if (cmd->permission != (permission & cmd->permission))
254 255 256
			client_printf(client, "command: %s\n", cmd->cmd);
	}

257
	return CommandResult::OK;
258 259
}

Max Kellermann's avatar
Max Kellermann committed
260
void command_init(void)
Avuton Olrich's avatar
Avuton Olrich committed
261
{
262 263 264 265 266
#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
267 268
}

Max Kellermann's avatar
Max Kellermann committed
269
void command_finish(void)
Avuton Olrich's avatar
Avuton Olrich committed
270
{
271 272 273 274 275 276 277 278 279 280 281
}

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

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

282
		const auto cmp = strcmp(name, commands[i].cmd);
283 284 285 286 287 288 289 290
		if (cmp == 0)
			return &commands[i];
		else if (cmp < 0)
			b = i;
		else if (cmp > 0)
			a = i + 1;
	} while (a < b);

291
	return nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
292 293
}

294
static bool
295
command_check_request(const struct command *cmd, Client &client,
296
		      unsigned permission, ConstBuffer<const char *> args)
Avuton Olrich's avatar
Avuton Olrich committed
297
{
Max Kellermann's avatar
Max Kellermann committed
298
	if (cmd->permission != (permission & cmd->permission)) {
299 300 301
		command_error(client, ACK_ERROR_PERMISSION,
			      "you don't have permission for \"%s\"",
			      cmd->cmd);
302
		return false;
Warren Dukes's avatar
Warren Dukes committed
303 304
	}

305 306 307 308
	const int min = cmd->min;
	const int max = cmd->max;

	if (min < 0)
309
		return true;
Warren Dukes's avatar
Warren Dukes committed
310

311
	if (min == max && unsigned(max) != args.size) {
312 313
		command_error(client, ACK_ERROR_ARG,
			      "wrong number of arguments for \"%s\"",
314
			      cmd->cmd);
315
		return false;
316
	} else if (args.size < unsigned(min)) {
317
		command_error(client, ACK_ERROR_ARG,
318
			      "too few arguments for \"%s\"", cmd->cmd);
319
		return false;
320
	} else if (max >= 0 && args.size > unsigned(max)) {
321
		command_error(client, ACK_ERROR_ARG,
322
			      "too many arguments for \"%s\"", cmd->cmd);
323
		return false;
Avuton Olrich's avatar
Avuton Olrich committed
324
	} else
325
		return true;
Warren Dukes's avatar
Warren Dukes committed
326 327
}

328
static const struct command *
329
command_checked_lookup(Client &client, unsigned permission,
330
		       const char *cmd_name, ConstBuffer<const char *> args)
Warren Dukes's avatar
Warren Dukes committed
331
{
332
	current_command = "";
Warren Dukes's avatar
Warren Dukes committed
333

334
	const struct command *cmd = command_lookup(cmd_name);
335
	if (cmd == nullptr) {
336
		command_error(client, ACK_ERROR_UNKNOWN,
337
			      "unknown command \"%s\"", cmd_name);
338
		return nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
339
	}
Warren Dukes's avatar
Warren Dukes committed
340

Warren Dukes's avatar
Warren Dukes committed
341 342
	current_command = cmd->cmd;

343
	if (!command_check_request(cmd, client, permission, args))
344
		return nullptr;
345 346 347 348

	return cmd;
}

349
CommandResult
350
command_process(Client &client, unsigned num, char *line)
351
{
352
	Error error;
353

354 355
	command_list_num = num;

356
	/* get the command name (first word on the line) */
357 358
	/* we have to set current_command because command_error()
	   expects it to be set */
359

360
	Tokenizer tokenizer(line);
361

362 363 364
	const char *const cmd_name = current_command =
		tokenizer.NextWord(error);
	if (cmd_name == nullptr) {
365
		current_command = "";
366
		if (tokenizer.IsEnd())
367 368
			command_error(client, ACK_ERROR_UNKNOWN,
				      "No command given");
369
		else
370
			command_error(client, ACK_ERROR_UNKNOWN,
371 372
				      "%s", error.GetMessage());

373
		current_command = nullptr;
374

375 376 377
		/* this client does not speak the MPD protocol; kick
		   the connection */
		return CommandResult::FINISH;
378 379
	}

380 381
	char *argv[COMMAND_ARGV_MAX];
	ConstBuffer<const char *> args(argv, 0);
382 383 384

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

385
	while (true) {
386
		if (args.size == COMMAND_ARGV_MAX) {
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
			command_error(client, ACK_ERROR_ARG,
				      "Too many arguments");
			current_command = nullptr;
			return CommandResult::ERROR;
		}

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

			command_error(client, ACK_ERROR_ARG, "%s", error.GetMessage());
			current_command = nullptr;
			return CommandResult::ERROR;
		}

403
		argv[args.size++] = a;
404 405 406
	}

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

408 409
	const struct command *cmd =
		command_checked_lookup(client, client.GetPermission(),
410
				       cmd_name, args);
411 412

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

416
	current_command = nullptr;
Max Kellermann's avatar
Max Kellermann committed
417
	command_list_num = 0;
Warren Dukes's avatar
Warren Dukes committed
418

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