AllCommands.cxx 12.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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/Tokenizer.hxx"
39
#include "util/Error.hxx"
Warren Dukes's avatar
Warren Dukes committed
40

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

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

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

66
/* don't be fooled, this is the command handler for "commands" command */
67
static CommandResult
68
handle_commands(Client &client, unsigned argc, char *argv[]);
69

70
static CommandResult
71
handle_not_commands(Client &client, unsigned argc, char *argv[]);
72

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

static const unsigned num_commands = sizeof(commands) / sizeof(commands[0]);

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

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

213 214 215
	return true;
}

216
/* don't be fooled, this is the command handler for "commands" command */
217
static CommandResult
218
handle_commands(Client &client,
219
		gcc_unused unsigned argc, gcc_unused char *argv[])
220
{
221
	const unsigned permission = client.GetPermission();
222 223 224 225 226
	const struct command *cmd;

	for (unsigned i = 0; i < num_commands; ++i) {
		cmd = &commands[i];

227
		if (cmd->permission == (permission & cmd->permission) &&
228
		    command_available(client.partition, cmd))
229 230 231
			client_printf(client, "command: %s\n", cmd->cmd);
	}

232
	return CommandResult::OK;
233 234
}

235
static CommandResult
236
handle_not_commands(Client &client,
237
		    gcc_unused unsigned argc, gcc_unused char *argv[])
238
{
239
	const unsigned permission = client.GetPermission();
240 241 242 243 244
	const struct command *cmd;

	for (unsigned i = 0; i < num_commands; ++i) {
		cmd = &commands[i];

Max Kellermann's avatar
Max Kellermann committed
245
		if (cmd->permission != (permission & cmd->permission))
246 247 248
			client_printf(client, "command: %s\n", cmd->cmd);
	}

249
	return CommandResult::OK;
250 251
}

Max Kellermann's avatar
Max Kellermann committed
252
void command_init(void)
Avuton Olrich's avatar
Avuton Olrich committed
253
{
254 255 256 257 258
#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
259 260
}

Max Kellermann's avatar
Max Kellermann committed
261
void command_finish(void)
Avuton Olrich's avatar
Avuton Olrich committed
262
{
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
}

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);

284
	return nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
285 286
}

287
static bool
288
command_check_request(const struct command *cmd, Client &client,
289
		      unsigned permission, unsigned argc, char *argv[])
Avuton Olrich's avatar
Avuton Olrich committed
290
{
291 292
	const unsigned min = cmd->min + 1;
	const unsigned max = cmd->max + 1;
Warren Dukes's avatar
Warren Dukes committed
293

Max Kellermann's avatar
Max Kellermann committed
294
	if (cmd->permission != (permission & cmd->permission)) {
295 296 297
		command_error(client, ACK_ERROR_PERMISSION,
			      "you don't have permission for \"%s\"",
			      cmd->cmd);
298
		return false;
Warren Dukes's avatar
Warren Dukes committed
299 300
	}

Avuton Olrich's avatar
Avuton Olrich committed
301
	if (min == 0)
302
		return true;
Warren Dukes's avatar
Warren Dukes committed
303

Avuton Olrich's avatar
Avuton Olrich committed
304
	if (min == max && max != argc) {
305 306 307
		command_error(client, ACK_ERROR_ARG,
			      "wrong number of arguments for \"%s\"",
			      argv[0]);
308
		return false;
Avuton Olrich's avatar
Avuton Olrich committed
309
	} else if (argc < min) {
310 311
		command_error(client, ACK_ERROR_ARG,
			      "too few arguments for \"%s\"", argv[0]);
312
		return false;
Avuton Olrich's avatar
Avuton Olrich committed
313
	} else if (argc > max && max /* != 0 */ ) {
314 315
		command_error(client, ACK_ERROR_ARG,
			      "too many arguments for \"%s\"", argv[0]);
316
		return false;
Avuton Olrich's avatar
Avuton Olrich committed
317
	} else
318
		return true;
Warren Dukes's avatar
Warren Dukes committed
319 320
}

321
static const struct command *
322
command_checked_lookup(Client &client, unsigned permission,
323
		       unsigned argc, char *argv[])
Warren Dukes's avatar
Warren Dukes committed
324
{
325
	const struct command *cmd;
Warren Dukes's avatar
Warren Dukes committed
326

327
	current_command = "";
Warren Dukes's avatar
Warren Dukes committed
328

329
	if (argc == 0)
330
		return nullptr;
Warren Dukes's avatar
Warren Dukes committed
331

332
	cmd = command_lookup(argv[0]);
333
	if (cmd == nullptr) {
334 335
		command_error(client, ACK_ERROR_UNKNOWN,
			      "unknown command \"%s\"", argv[0]);
336
		return nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
337
	}
Warren Dukes's avatar
Warren Dukes committed
338

Warren Dukes's avatar
Warren Dukes committed
339 340
	current_command = cmd->cmd;

341
	if (!command_check_request(cmd, client, permission, argc, argv))
342
		return nullptr;
343 344 345 346

	return cmd;
}

347
CommandResult
348
command_process(Client &client, unsigned num, char *line)
349
{
350
	Error error;
351
	char *argv[COMMAND_ARGV_MAX] = { nullptr };
352
	const struct command *cmd;
353
	CommandResult ret = CommandResult::ERROR;
354

355 356
	command_list_num = num;

357 358
	/* get the command name (first word on the line) */

359
	Tokenizer tokenizer(line);
360
	argv[0] = tokenizer.NextWord(error);
361
	if (argv[0] == nullptr) {
362
		current_command = "";
363
		if (tokenizer.IsEnd())
364 365
			command_error(client, ACK_ERROR_UNKNOWN,
				      "No command given");
366
		else
367
			command_error(client, ACK_ERROR_UNKNOWN,
368 369
				      "%s", error.GetMessage());

370
		current_command = nullptr;
371

372
		return CommandResult::ERROR;
373 374
	}

375
	unsigned argc = 1;
376 377 378

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

379
	while (argc < COMMAND_ARGV_MAX &&
380
	       (argv[argc] =
381
		tokenizer.NextParam(error)) != nullptr)
382 383 384 385 386 387 388
		++argc;

	/* some error checks; we have to set current_command because
	   command_error() expects it to be set */

	current_command = argv[0];

389
	if (argc >= COMMAND_ARGV_MAX) {
390
		command_error(client, ACK_ERROR_ARG, "Too many arguments");
391
		current_command = nullptr;
392
		return CommandResult::ERROR;
393 394
	}

395
	if (!tokenizer.IsEnd()) {
396
		command_error(client, ACK_ERROR_ARG, "%s", error.GetMessage());
397
		current_command = nullptr;
398
		return CommandResult::ERROR;
399 400 401
	}

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

403
	cmd = command_checked_lookup(client, client.GetPermission(),
Max Kellermann's avatar
Max Kellermann committed
404
				     argc, argv);
Eric Wong's avatar
Eric Wong committed
405 406
	if (cmd)
		ret = cmd->handler(client, argc, argv);
Warren Dukes's avatar
Warren Dukes committed
407

408
	current_command = nullptr;
Max Kellermann's avatar
Max Kellermann committed
409
	command_list_num = 0;
Warren Dukes's avatar
Warren Dukes committed
410

411
	return ret;
Warren Dukes's avatar
Warren Dukes committed
412
}