AllCommands.cxx 13.3 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 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 "CommandError.hxx"
23
#include "Request.hxx"
24
#include "QueueCommands.hxx"
25
#include "TagCommands.hxx"
26 27
#include "PlayerCommands.hxx"
#include "PlaylistCommands.hxx"
28
#include "StorageCommands.hxx"
29
#include "DatabaseCommands.hxx"
30
#include "FileCommands.hxx"
31 32
#include "OutputCommands.hxx"
#include "MessageCommands.hxx"
33
#include "NeighborCommands.hxx"
34
#include "OtherCommands.hxx"
35
#include "Permission.hxx"
36
#include "tag/TagType.h"
37
#include "Partition.hxx"
38
#include "client/Client.hxx"
39
#include "client/Response.hxx"
40
#include "util/Macros.hxx"
41
#include "util/Tokenizer.hxx"
42
#include "util/StringAPI.hxx"
Warren Dukes's avatar
Warren Dukes committed
43

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

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

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

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

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

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

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

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

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

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

226 227 228
	return true;
}

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

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

241
	return CommandResult::OK;
242 243
}

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

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

254
	return CommandResult::OK;
255 256
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return cmd;
}

360
CommandResult
361
command_process(Client &client, unsigned num, char *line)
362
try {
363
	Response r(client, num);
364

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

369
	Tokenizer tokenizer(line);
370

371 372 373 374 375 376 377 378 379 380 381
	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());
382 383 384
		/* this client does not speak the MPD protocol; kick
		   the connection */
		return CommandResult::FINISH;
385 386
	}

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

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

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

398 399 400
		char *a = tokenizer.NextParam();
		if (a == nullptr)
			break;
401

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

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

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

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

415 416 417
	return ret;
} catch (const std::exception &e) {
	Response r(client, num);
418
	PrintError(r, std::current_exception());
419
	return CommandResult::ERROR;
Warren Dukes's avatar
Warren Dukes committed
420
}