OtherCommands.cxx 10.5 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

#include "config.h"
#include "OtherCommands.hxx"
22
#include "Request.hxx"
23 24
#include "FileCommands.hxx"
#include "StorageCommands.hxx"
25
#include "CommandError.hxx"
26
#include "db/Uri.hxx"
27
#include "storage/StorageInterface.hxx"
28
#include "LocateUri.hxx"
29
#include "DetachedSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
30 31
#include "SongPrint.hxx"
#include "TagPrint.hxx"
32 33
#include "TagStream.hxx"
#include "tag/TagHandler.hxx"
Max Kellermann's avatar
Max Kellermann committed
34
#include "TimePrint.hxx"
35
#include "decoder/DecoderPrint.hxx"
Max Kellermann's avatar
Max Kellermann committed
36
#include "ls.hxx"
Max Kellermann's avatar
Max Kellermann committed
37
#include "mixer/Volume.hxx"
Max Kellermann's avatar
Max Kellermann committed
38
#include "util/UriUtil.hxx"
39
#include "util/StringAPI.hxx"
40
#include "fs/AllocatedPath.hxx"
41
#include "Stats.hxx"
42
#include "Permission.hxx"
Max Kellermann's avatar
Max Kellermann committed
43
#include "PlaylistFile.hxx"
44
#include "db/PlaylistVector.hxx"
45
#include "client/Client.hxx"
46
#include "client/Response.hxx"
47
#include "Partition.hxx"
48
#include "Instance.hxx"
Max Kellermann's avatar
Max Kellermann committed
49
#include "Idle.hxx"
50
#include "Log.hxx"
51

52 53
#ifdef ENABLE_DATABASE
#include "DatabaseCommands.hxx"
54
#include "db/Interface.hxx"
55 56 57
#include "db/update/Service.hxx"
#endif

58 59 60 61
#include <assert.h>
#include <string.h>

static void
62
print_spl_list(Response &r, const PlaylistVector &list)
63
{
64
	for (const auto &i : list) {
65
		r.Format("playlist: %s\n", i.name.c_str());
66

67
		if (i.mtime > 0)
68
			time_print(r, "Last-Modified", i.mtime);
69 70 71
	}
}

72
CommandResult
73
handle_urlhandlers(Client &client, gcc_unused Request args, Response &r)
74
{
75
	if (client.IsLocal())
76 77
		r.Format("handler: file://\n");
	print_supported_uri_schemes(r);
78
	return CommandResult::OK;
79 80
}

81
CommandResult
82 83
handle_decoders(gcc_unused Client &client, gcc_unused Request args,
		Response &r)
84
{
85
	decoder_list_print(r);
86
	return CommandResult::OK;
87 88
}

89
CommandResult
90 91
handle_tagtypes(gcc_unused Client &client, gcc_unused Request request,
		Response &r)
92
{
93
	tag_print_types(r);
94
	return CommandResult::OK;
95 96
}

97
CommandResult
98 99
handle_kill(gcc_unused Client &client, gcc_unused Request request,
	    gcc_unused Response &r)
100
{
101
	return CommandResult::KILL;
102 103
}

104
CommandResult
105 106
handle_close(gcc_unused Client &client, gcc_unused Request args,
	     gcc_unused Response &r)
107
{
108
	return CommandResult::FINISH;
109 110
}

111 112 113
static void
print_tag(TagType type, const char *value, void *ctx)
{
114
	auto &r = *(Response *)ctx;
115

116
	tag_print(r, type, value);
117 118
}

119
CommandResult
120
handle_listfiles(Client &client, Request args, Response &r)
121
{
122
	/* default is root directory */
123
	const auto uri = args.GetOptional(0, "");
124

125
	const auto located_uri = LocateUri(uri, &client
126
#ifdef ENABLE_DATABASE
127
					   , nullptr
128
#endif
129
					   );
130

131 132 133 134 135 136 137 138 139
	switch (located_uri.type) {
	case LocatedUri::Type::ABSOLUTE:
#ifdef ENABLE_DATABASE
		/* use storage plugin to list remote directory */
		return handle_listfiles_storage(r, located_uri.canonical_uri);
#else
		r.Error(ACK_ERROR_NO_EXIST, "No database");
		return CommandResult::ERROR;
#endif
140

141 142 143 144 145 146 147 148 149 150 151
	case LocatedUri::Type::RELATIVE:
#ifdef ENABLE_DATABASE
		if (client.partition.instance.storage != nullptr)
			/* if we have a storage instance, obtain a list of
			   files from it */
			return handle_listfiles_storage(r,
							*client.partition.instance.storage,
							uri);

		/* fall back to entries from database if we have no storage */
		return handle_listfiles_db(client, r, uri);
152
#else
153 154
		r.Error(ACK_ERROR_NO_EXIST, "No database");
		return CommandResult::ERROR;
155
#endif
156 157 158

	case LocatedUri::Type::PATH:
		/* list local directory */
159
		return handle_listfiles_local(r, located_uri.path);
160 161 162
	}

	gcc_unreachable();
163 164
}

165
static constexpr TagHandler print_tag_handler = {
166 167 168 169 170
	nullptr,
	print_tag,
	nullptr,
};

171 172
static CommandResult
handle_lsinfo_absolute(Response &r, const char *uri)
173
{
174 175 176
	if (!tag_stream_scan(uri, print_tag_handler, &r)) {
		r.Error(ACK_ERROR_NO_EXIST, "No such file");
		return CommandResult::ERROR;
177 178
	}

179 180
	return CommandResult::OK;
}
181

182 183 184
static CommandResult
handle_lsinfo_relative(Client &client, Response &r, const char *uri)
{
185
#ifdef ENABLE_DATABASE
186
	CommandResult result = handle_lsinfo2(client, uri, r);
187
	if (result != CommandResult::OK)
188
		return result;
189 190
#else
	(void)client;
191
#endif
192 193

	if (isRootDirectory(uri)) {
194 195 196 197 198
		try {
			print_spl_list(r, ListPlaylistFiles());
		} catch (const std::exception &e) {
			LogError(e);
		}
199 200
	} else {
#ifndef ENABLE_DATABASE
201
		r.Error(ACK_ERROR_NO_EXIST, "No database");
202 203
		return CommandResult::ERROR;
#endif
204 205
	}

206
	return CommandResult::OK;
207 208
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
static CommandResult
handle_lsinfo_path(Client &client, Response &r,
		   const char *path_utf8, Path path_fs)
{
	DetachedSong song(path_utf8);
	if (!song.LoadFile(path_fs)) {
		r.Error(ACK_ERROR_NO_EXIST, "No such file");
		return CommandResult::ERROR;
	}

	song_print_info(r, client.partition, song);
	return CommandResult::OK;
}

CommandResult
handle_lsinfo(Client &client, Request args, Response &r)
{
	/* default is root directory */
227 228 229 230 231 232 233 234
	auto uri = args.GetOptional(0, "");
	if (StringIsEqual(uri, "/"))
		/* this URI is malformed, but some clients are buggy
		   and use "lsinfo /" to list files in the music root
		   directory, which was never intended to work, but
		   once did; in order to retain backwards
		   compatibility, work around this here */
		uri = "";
235

236
	const auto located_uri = LocateUri(uri, &client
237
#ifdef ENABLE_DATABASE
238
					   , nullptr
239
#endif
240
					   );
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

	switch (located_uri.type) {
	case LocatedUri::Type::ABSOLUTE:
		return handle_lsinfo_absolute(r, located_uri.canonical_uri);

	case LocatedUri::Type::RELATIVE:
		return handle_lsinfo_relative(client, r,
					      located_uri.canonical_uri);

	case LocatedUri::Type::PATH:
		/* print information about an arbitrary local file */
		return handle_lsinfo_path(client, r, located_uri.canonical_uri,
					  located_uri.path);
	}

	gcc_unreachable();
}

259 260 261
#ifdef ENABLE_DATABASE

static CommandResult
262
handle_update(Response &r, UpdateService &update,
263 264 265 266
	      const char *uri_utf8, bool discard)
{
	unsigned ret = update.Enqueue(uri_utf8, discard);
	if (ret > 0) {
267
		r.Format("updating_db: %i\n", ret);
268 269
		return CommandResult::OK;
	} else {
270
		r.Error(ACK_ERROR_UPDATE_ALREADY, "already updating");
271 272 273 274
		return CommandResult::ERROR;
	}
}

275
static CommandResult
276
handle_update(Response &r, Database &db,
277 278
	      const char *uri_utf8, bool discard)
{
279
	unsigned id = db.Update(uri_utf8, discard);
280
	if (id > 0) {
281
		r.Format("updating_db: %i\n", id);
282 283 284 285
		return CommandResult::OK;
	} else {
		/* Database::Update() has returned 0 without setting
		   the Error: the method is not implemented */
286
		r.Error(ACK_ERROR_NO_EXIST, "Not implemented");
287 288 289 290
		return CommandResult::ERROR;
	}
}

291 292
#endif

293
static CommandResult
294
handle_update(Client &client, Request args, Response &r, bool discard)
295
{
296
#ifdef ENABLE_DATABASE
297
	const char *path = "";
298

299 300 301
	assert(args.size <= 1);
	if (!args.IsEmpty()) {
		path = args.front();
302

303
		if (*path == 0 || StringIsEqual(path, "/"))
304
			/* backwards compatibility with MPD 0.15 */
305
			path = "";
306
		else if (!uri_safe_local(path)) {
307
			r.Error(ACK_ERROR_ARG, "Malformed path");
308
			return CommandResult::ERROR;
309 310 311
		}
	}

312
	UpdateService *update = client.partition.instance.update;
313
	if (update != nullptr)
314
		return handle_update(r, *update, path, discard);
315 316 317

	Database *db = client.partition.instance.database;
	if (db != nullptr)
318
		return handle_update(r, *db, path, discard);
319
#else
320
	(void)client;
321
	(void)args;
322
	(void)discard;
323
#endif
324

325
	r.Error(ACK_ERROR_NO_EXIST, "No database");
326
	return CommandResult::ERROR;
327 328
}

329
CommandResult
330
handle_update(Client &client, Request args, gcc_unused Response &r)
331
{
332
	return handle_update(client, args, r, false);
333
}
334

335
CommandResult
336
handle_rescan(Client &client, Request args, Response &r)
337
{
338
	return handle_update(client, args, r, true);
339 340
}

341
CommandResult
342
handle_setvol(Client &client, Request args, Response &r)
343
{
344
	unsigned level = args.ParseUnsigned(0, 100);
345

346
	if (!volume_level_change(client.partition.outputs, level)) {
347
		r.Error(ACK_ERROR_SYSTEM, "problems setting volume");
348
		return CommandResult::ERROR;
349 350
	}

351
	return CommandResult::OK;
352 353
}

354
CommandResult
355
handle_volume(Client &client, Request args, Response &r)
356
{
357
	int relative = args.ParseInt(0, -100, 100);
358

359
	const int old_volume = volume_level_get(client.partition.outputs);
360
	if (old_volume < 0) {
361
		r.Error(ACK_ERROR_SYSTEM, "No mixer");
362 363 364 365 366 367 368 369 370
		return CommandResult::ERROR;
	}

	int new_volume = old_volume + relative;
	if (new_volume < 0)
		new_volume = 0;
	else if (new_volume > 100)
		new_volume = 100;

371 372
	if (new_volume != old_volume &&
	    !volume_level_change(client.partition.outputs, new_volume)) {
373
		r.Error(ACK_ERROR_SYSTEM, "problems setting volume");
374 375 376 377 378 379
		return CommandResult::ERROR;
	}

	return CommandResult::OK;
}

380
CommandResult
381
handle_stats(Client &client, gcc_unused Request args, Response &r)
382
{
383
	stats_print(r, client.partition);
384
	return CommandResult::OK;
385 386
}

387
CommandResult
388 389
handle_ping(gcc_unused Client &client, gcc_unused Request args,
	    gcc_unused Response &r)
390
{
391
	return CommandResult::OK;
392 393
}

394
CommandResult
395
handle_password(Client &client, Request args, Response &r)
396
{
397
	unsigned permission = 0;
398
	if (getPermissionFromPassword(args.front(), &permission) < 0) {
399
		r.Error(ACK_ERROR_PASSWORD, "incorrect password");
400
		return CommandResult::ERROR;
401 402
	}

403
	client.SetPermission(permission);
404

405
	return CommandResult::OK;
406 407
}

408
CommandResult
409
handle_config(Client &client, gcc_unused Request args, Response &r)
410
{
411
	if (!client.IsLocal()) {
412 413
		r.Error(ACK_ERROR_PERMISSION,
			"Command only permitted to local clients");
414
		return CommandResult::ERROR;
415 416
	}

417
#ifdef ENABLE_DATABASE
418 419 420
	const Storage *storage = client.GetStorage();
	if (storage != nullptr) {
		const auto path = storage->MapUTF8("");
421
		r.Format("music_directory: %s\n", path.c_str());
422
	}
423
#endif
424

425
	return CommandResult::OK;
426 427
}

428
CommandResult
429
handle_idle(Client &client, Request args, Response &r)
430
{
431
	unsigned flags = 0;
432 433
	for (const char *i : args) {
		unsigned event = idle_parse_name(i);
434
		if (event == 0) {
435 436
			r.FormatError(ACK_ERROR_ARG,
				      "Unrecognized idle event: %s", i);
437
			return CommandResult::ERROR;
438
		}
439 440

		flags |= event;
441 442 443 444 445 446 447
	}

	/* No argument means that the client wants to receive everything */
	if (flags == 0)
		flags = ~0;

	/* enable "idle" mode on this client */
448
	client.IdleWait(flags);
449

450
	return CommandResult::IDLE;
451
}