AllCommands.cxx 11.9 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "DatabaseCommands.hxx"
27
#include "FileCommands.hxx"
28 29
#include "OutputCommands.hxx"
#include "MessageCommands.hxx"
30
#include "OtherCommands.hxx"
31
#include "Permission.hxx"
32
#include "tag/TagType.h"
Max Kellermann's avatar
Max Kellermann committed
33 34
#include "protocol/Result.hxx"
#include "Client.hxx"
35
#include "util/Tokenizer.hxx"
36
#include "util/Error.hxx"
Warren Dukes's avatar
Warren Dukes committed
37

38
#ifdef ENABLE_SQLITE
Max Kellermann's avatar
Max Kellermann committed
39 40
#include "StickerCommands.hxx"
#include "StickerDatabase.hxx"
41 42
#endif

Max Kellermann's avatar
Max Kellermann committed
43
#include <assert.h>
44
#include <string.h>
Warren Dukes's avatar
Warren Dukes committed
45

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

63
/* don't be fooled, this is the command handler for "commands" command */
64
static CommandResult
65
handle_commands(Client &client, int argc, char *argv[]);
66

67
static CommandResult
68
handle_not_commands(Client &client, int argc, char *argv[]);
69

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

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

174
static bool
175
command_available(gcc_unused const struct command *cmd)
176 177 178 179 180 181 182 183 184
{
#ifdef ENABLE_SQLITE
	if (strcmp(cmd->cmd, "sticker") == 0)
		return sticker_enabled();
#endif

	return true;
}

185
/* don't be fooled, this is the command handler for "commands" command */
186
static CommandResult
187
handle_commands(Client &client,
188
		gcc_unused int argc, gcc_unused char *argv[])
189
{
190
	const unsigned permission = client.GetPermission();
191 192 193 194 195
	const struct command *cmd;

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

196 197
		if (cmd->permission == (permission & cmd->permission) &&
		    command_available(cmd))
198 199 200
			client_printf(client, "command: %s\n", cmd->cmd);
	}

201
	return CommandResult::OK;
202 203
}

204
static CommandResult
205
handle_not_commands(Client &client,
206
		    gcc_unused int argc, gcc_unused char *argv[])
207
{
208
	const unsigned permission = client.GetPermission();
209 210 211 212 213
	const struct command *cmd;

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

Max Kellermann's avatar
Max Kellermann committed
214
		if (cmd->permission != (permission & cmd->permission))
215 216 217
			client_printf(client, "command: %s\n", cmd->cmd);
	}

218
	return CommandResult::OK;
219 220
}

Max Kellermann's avatar
Max Kellermann committed
221
void command_init(void)
Avuton Olrich's avatar
Avuton Olrich committed
222
{
223 224 225 226 227
#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
228 229
}

Max Kellermann's avatar
Max Kellermann committed
230
void command_finish(void)
Avuton Olrich's avatar
Avuton Olrich committed
231
{
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
}

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

253
	return nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
254 255
}

256
static bool
257
command_check_request(const struct command *cmd, Client &client,
Max Kellermann's avatar
Max Kellermann committed
258
		      unsigned permission, int argc, char *argv[])
Avuton Olrich's avatar
Avuton Olrich committed
259 260 261
{
	int min = cmd->min + 1;
	int max = cmd->max + 1;
Warren Dukes's avatar
Warren Dukes committed
262

Max Kellermann's avatar
Max Kellermann committed
263
	if (cmd->permission != (permission & cmd->permission)) {
264 265 266
		command_error(client, ACK_ERROR_PERMISSION,
			      "you don't have permission for \"%s\"",
			      cmd->cmd);
267
		return false;
Warren Dukes's avatar
Warren Dukes committed
268 269
	}

Avuton Olrich's avatar
Avuton Olrich committed
270
	if (min == 0)
271
		return true;
Warren Dukes's avatar
Warren Dukes committed
272

Avuton Olrich's avatar
Avuton Olrich committed
273
	if (min == max && max != argc) {
274 275 276
		command_error(client, ACK_ERROR_ARG,
			      "wrong number of arguments for \"%s\"",
			      argv[0]);
277
		return false;
Avuton Olrich's avatar
Avuton Olrich committed
278
	} else if (argc < min) {
279 280
		command_error(client, ACK_ERROR_ARG,
			      "too few arguments for \"%s\"", argv[0]);
281
		return false;
Avuton Olrich's avatar
Avuton Olrich committed
282
	} else if (argc > max && max /* != 0 */ ) {
283 284
		command_error(client, ACK_ERROR_ARG,
			      "too many arguments for \"%s\"", argv[0]);
285
		return false;
Avuton Olrich's avatar
Avuton Olrich committed
286
	} else
287
		return true;
Warren Dukes's avatar
Warren Dukes committed
288 289
}

290
static const struct command *
291
command_checked_lookup(Client &client, unsigned permission,
Max Kellermann's avatar
Max Kellermann committed
292
		       int argc, char *argv[])
Warren Dukes's avatar
Warren Dukes committed
293
{
294
	const struct command *cmd;
Warren Dukes's avatar
Warren Dukes committed
295

296
	current_command = "";
Warren Dukes's avatar
Warren Dukes committed
297

298
	if (argc == 0)
299
		return nullptr;
Warren Dukes's avatar
Warren Dukes committed
300

301
	cmd = command_lookup(argv[0]);
302
	if (cmd == nullptr) {
303 304
		command_error(client, ACK_ERROR_UNKNOWN,
			      "unknown command \"%s\"", argv[0]);
305
		return nullptr;
Avuton Olrich's avatar
Avuton Olrich committed
306
	}
Warren Dukes's avatar
Warren Dukes committed
307

Warren Dukes's avatar
Warren Dukes committed
308 309
	current_command = cmd->cmd;

310
	if (!command_check_request(cmd, client, permission, argc, argv))
311
		return nullptr;
312 313 314 315

	return cmd;
}

316
CommandResult
317
command_process(Client &client, unsigned num, char *line)
318
{
319
	Error error;
320
	char *argv[COMMAND_ARGV_MAX] = { nullptr };
321
	const struct command *cmd;
322
	CommandResult ret = CommandResult::ERROR;
323

324 325
	command_list_num = num;

326 327
	/* get the command name (first word on the line) */

328
	Tokenizer tokenizer(line);
329
	argv[0] = tokenizer.NextWord(error);
330
	if (argv[0] == nullptr) {
331
		current_command = "";
332
		if (tokenizer.IsEnd())
333 334
			command_error(client, ACK_ERROR_UNKNOWN,
				      "No command given");
335
		else
336
			command_error(client, ACK_ERROR_UNKNOWN,
337 338
				      "%s", error.GetMessage());

339
		current_command = nullptr;
340

341
		return CommandResult::ERROR;
342 343
	}

344
	unsigned argc = 1;
345 346 347

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

348
	while (argc < COMMAND_ARGV_MAX &&
349
	       (argv[argc] =
350
		tokenizer.NextParam(error)) != nullptr)
351 352 353 354 355 356 357
		++argc;

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

	current_command = argv[0];

358
	if (argc >= COMMAND_ARGV_MAX) {
359
		command_error(client, ACK_ERROR_ARG, "Too many arguments");
360
		current_command = nullptr;
361
		return CommandResult::ERROR;
362 363
	}

364
	if (!tokenizer.IsEnd()) {
365
		command_error(client, ACK_ERROR_ARG, "%s", error.GetMessage());
366
		current_command = nullptr;
367
		return CommandResult::ERROR;
368 369 370
	}

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

372
	cmd = command_checked_lookup(client, client.GetPermission(),
Max Kellermann's avatar
Max Kellermann committed
373
				     argc, argv);
Eric Wong's avatar
Eric Wong committed
374 375
	if (cmd)
		ret = cmd->handler(client, argc, argv);
Warren Dukes's avatar
Warren Dukes committed
376

377
	current_command = nullptr;
Max Kellermann's avatar
Max Kellermann committed
378
	command_list_num = 0;
Warren Dukes's avatar
Warren Dukes committed
379

380
	return ret;
Warren Dukes's avatar
Warren Dukes committed
381
}