OtherCommands.cxx 10.8 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2015 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/Error.hxx"
40
#include "util/ConstBuffer.hxx"
41
#include "util/StringAPI.hxx"
42
#include "fs/AllocatedPath.hxx"
43
#include "Stats.hxx"
44
#include "Permission.hxx"
Max Kellermann's avatar
Max Kellermann committed
45
#include "PlaylistFile.hxx"
46
#include "db/PlaylistVector.hxx"
47
#include "client/Client.hxx"
48
#include "client/Response.hxx"
49
#include "Partition.hxx"
50
#include "Instance.hxx"
Max Kellermann's avatar
Max Kellermann committed
51
#include "Idle.hxx"
52
#include "Log.hxx"
53

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

60 61 62 63
#include <assert.h>
#include <string.h>

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

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

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

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

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

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

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

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

118
	tag_print(r, type, value);
119 120
}

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

127 128
	Error error;
	const auto located_uri = LocateUri(uri, &client,
129
#ifdef ENABLE_DATABASE
130 131 132
					   nullptr,
#endif
					   error);
133

134 135 136
	switch (located_uri.type) {
	case LocatedUri::Type::UNKNOWN:
		return print_error(r, error);
137

138 139 140 141 142 143 144 145
	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
146

147 148 149 150 151 152 153 154 155 156 157
	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);
158
#else
159 160
		r.Error(ACK_ERROR_NO_EXIST, "No database");
		return CommandResult::ERROR;
161
#endif
162 163 164

	case LocatedUri::Type::PATH:
		/* list local directory */
165
		return handle_listfiles_local(r, located_uri.path);
166 167 168
	}

	gcc_unreachable();
169 170
}

171 172 173 174 175 176
static constexpr tag_handler print_tag_handler = {
	nullptr,
	print_tag,
	nullptr,
};

177 178
static CommandResult
handle_lsinfo_absolute(Response &r, const char *uri)
179
{
180 181 182
	if (!tag_stream_scan(uri, print_tag_handler, &r)) {
		r.Error(ACK_ERROR_NO_EXIST, "No such file");
		return CommandResult::ERROR;
183 184
	}

185 186
	return CommandResult::OK;
}
187

188 189 190
static CommandResult
handle_lsinfo_relative(Client &client, Response &r, const char *uri)
{
191
#ifdef ENABLE_DATABASE
192
	CommandResult result = handle_lsinfo2(client, uri, r);
193
	if (result != CommandResult::OK)
194
		return result;
195 196
#else
	(void)client;
197
#endif
198 199

	if (isRootDirectory(uri)) {
200 201 202 203 204
		try {
			print_spl_list(r, ListPlaylistFiles());
		} catch (const std::exception &e) {
			LogError(e);
		}
205 206
	} else {
#ifndef ENABLE_DATABASE
207
		r.Error(ACK_ERROR_NO_EXIST, "No database");
208 209
		return CommandResult::ERROR;
#endif
210 211
	}

212
	return CommandResult::OK;
213 214
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
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 */
233 234 235 236 237 238 239 240
	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 = "";
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268

	Error error;
	const auto located_uri = LocateUri(uri, &client,
#ifdef ENABLE_DATABASE
					   nullptr,
#endif
					   error);

	switch (located_uri.type) {
	case LocatedUri::Type::UNKNOWN:
		return print_error(r, error);

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

269 270 271
#ifdef ENABLE_DATABASE

static CommandResult
272
handle_update(Response &r, UpdateService &update,
273 274 275 276
	      const char *uri_utf8, bool discard)
{
	unsigned ret = update.Enqueue(uri_utf8, discard);
	if (ret > 0) {
277
		r.Format("updating_db: %i\n", ret);
278 279
		return CommandResult::OK;
	} else {
280
		r.Error(ACK_ERROR_UPDATE_ALREADY, "already updating");
281 282 283 284
		return CommandResult::ERROR;
	}
}

285
static CommandResult
286
handle_update(Response &r, Database &db,
287 288 289 290 291
	      const char *uri_utf8, bool discard)
{
	Error error;
	unsigned id = db.Update(uri_utf8, discard, error);
	if (id > 0) {
292
		r.Format("updating_db: %i\n", id);
293 294
		return CommandResult::OK;
	} else if (error.IsDefined()) {
295
		return print_error(r, error);
296 297 298
	} else {
		/* Database::Update() has returned 0 without setting
		   the Error: the method is not implemented */
299
		r.Error(ACK_ERROR_NO_EXIST, "Not implemented");
300 301 302 303
		return CommandResult::ERROR;
	}
}

304 305
#endif

306
static CommandResult
307
handle_update(Client &client, Request args, Response &r, bool discard)
308
{
309
#ifdef ENABLE_DATABASE
310
	const char *path = "";
311

312 313 314
	assert(args.size <= 1);
	if (!args.IsEmpty()) {
		path = args.front();
315

316
		if (*path == 0 || StringIsEqual(path, "/"))
317
			/* backwards compatibility with MPD 0.15 */
318
			path = "";
319
		else if (!uri_safe_local(path)) {
320
			r.Error(ACK_ERROR_ARG, "Malformed path");
321
			return CommandResult::ERROR;
322 323 324
		}
	}

325
	UpdateService *update = client.partition.instance.update;
326
	if (update != nullptr)
327
		return handle_update(r, *update, path, discard);
328 329 330

	Database *db = client.partition.instance.database;
	if (db != nullptr)
331
		return handle_update(r, *db, path, discard);
332
#else
333
	(void)client;
334
	(void)args;
335
	(void)discard;
336
#endif
337

338
	r.Error(ACK_ERROR_NO_EXIST, "No database");
339
	return CommandResult::ERROR;
340 341
}

342
CommandResult
343
handle_update(Client &client, Request args, gcc_unused Response &r)
344
{
345
	return handle_update(client, args, r, false);
346
}
347

348
CommandResult
349
handle_rescan(Client &client, Request args, Response &r)
350
{
351
	return handle_update(client, args, r, true);
352 353
}

354
CommandResult
355
handle_setvol(Client &client, Request args, Response &r)
356
{
357
	unsigned level = args.ParseUnsigned(0, 100);
358

359
	if (!volume_level_change(client.partition.outputs, level)) {
360
		r.Error(ACK_ERROR_SYSTEM, "problems setting volume");
361
		return CommandResult::ERROR;
362 363
	}

364
	return CommandResult::OK;
365 366
}

367
CommandResult
368
handle_volume(Client &client, Request args, Response &r)
369
{
370
	int relative = args.ParseInt(0, -100, 100);
371

372
	const int old_volume = volume_level_get(client.partition.outputs);
373
	if (old_volume < 0) {
374
		r.Error(ACK_ERROR_SYSTEM, "No mixer");
375 376 377 378 379 380 381 382 383
		return CommandResult::ERROR;
	}

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

384 385
	if (new_volume != old_volume &&
	    !volume_level_change(client.partition.outputs, new_volume)) {
386
		r.Error(ACK_ERROR_SYSTEM, "problems setting volume");
387 388 389 390 391 392
		return CommandResult::ERROR;
	}

	return CommandResult::OK;
}

393
CommandResult
394
handle_stats(Client &client, gcc_unused Request args, Response &r)
395
{
396
	stats_print(r, client.partition);
397
	return CommandResult::OK;
398 399
}

400
CommandResult
401 402
handle_ping(gcc_unused Client &client, gcc_unused Request args,
	    gcc_unused Response &r)
403
{
404
	return CommandResult::OK;
405 406
}

407
CommandResult
408
handle_password(Client &client, Request args, Response &r)
409
{
410
	unsigned permission = 0;
411
	if (getPermissionFromPassword(args.front(), &permission) < 0) {
412
		r.Error(ACK_ERROR_PASSWORD, "incorrect password");
413
		return CommandResult::ERROR;
414 415
	}

416
	client.SetPermission(permission);
417

418
	return CommandResult::OK;
419 420
}

421
CommandResult
422
handle_config(Client &client, gcc_unused Request args, Response &r)
423
{
424
	if (!client.IsLocal()) {
425 426
		r.Error(ACK_ERROR_PERMISSION,
			"Command only permitted to local clients");
427
		return CommandResult::ERROR;
428 429
	}

430
#ifdef ENABLE_DATABASE
431 432 433
	const Storage *storage = client.GetStorage();
	if (storage != nullptr) {
		const auto path = storage->MapUTF8("");
434
		r.Format("music_directory: %s\n", path.c_str());
435
	}
436
#endif
437

438
	return CommandResult::OK;
439 440
}

441
CommandResult
442
handle_idle(Client &client, Request args, Response &r)
443
{
444
	unsigned flags = 0;
445 446
	for (const char *i : args) {
		unsigned event = idle_parse_name(i);
447
		if (event == 0) {
448 449
			r.FormatError(ACK_ERROR_ARG,
				      "Unrecognized idle event: %s", i);
450
			return CommandResult::ERROR;
451
		}
452 453

		flags |= event;
454 455 456 457 458 459 460
	}

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

	/* enable "idle" mode on this client */
461
	client.IdleWait(flags);
462

463
	return CommandResult::IDLE;
464
}