OtherCommands.cxx 9.9 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
#include "TagStream.hxx"
33
#include "tag/Handler.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"
38
#include "util/ChronoUtil.hxx"
Max Kellermann's avatar
Max Kellermann committed
39
#include "util/UriUtil.hxx"
40
#include "util/StringAPI.hxx"
41
#include "fs/AllocatedPath.hxx"
42
#include "Stats.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 (!IsNegative(i.mtime))
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_kill(gcc_unused Client &client, gcc_unused Request request,
	    gcc_unused Response &r)
92
{
93
	return CommandResult::KILL;
94 95
}

96
CommandResult
97
handle_listfiles(Client &client, Request args, Response &r)
98
{
99
	/* default is root directory */
100
	const auto uri = args.GetOptional(0, "");
101

102
	const auto located_uri = LocateUri(uri, &client
103
#ifdef ENABLE_DATABASE
104
					   , nullptr
105
#endif
106
					   );
107

108 109 110 111
	switch (located_uri.type) {
	case LocatedUri::Type::ABSOLUTE:
#ifdef ENABLE_DATABASE
		/* use storage plugin to list remote directory */
112 113
		return handle_listfiles_storage(client, r,
						located_uri.canonical_uri);
114 115 116 117
#else
		r.Error(ACK_ERROR_NO_EXIST, "No database");
		return CommandResult::ERROR;
#endif
118

119 120
	case LocatedUri::Type::RELATIVE:
#ifdef ENABLE_DATABASE
121
		if (client.GetInstance().storage != nullptr)
122 123 124
			/* if we have a storage instance, obtain a list of
			   files from it */
			return handle_listfiles_storage(r,
125
							*client.GetInstance().storage,
126 127 128 129
							uri);

		/* fall back to entries from database if we have no storage */
		return handle_listfiles_db(client, r, uri);
130
#else
131 132
		r.Error(ACK_ERROR_NO_EXIST, "No database");
		return CommandResult::ERROR;
133
#endif
134 135 136

	case LocatedUri::Type::PATH:
		/* list local directory */
137
		return handle_listfiles_local(r, located_uri.path);
138 139 140
	}

	gcc_unreachable();
141 142
}

143 144 145 146 147 148 149 150 151 152 153
class PrintTagHandler final : public NullTagHandler {
	Response &response;

public:
	explicit PrintTagHandler(Response &_response) noexcept
		:NullTagHandler(WANT_TAG), response(_response) {}

	void OnTag(TagType type, const char *value) noexcept override {
		if (response.GetClient().tag_mask.Test(type))
			tag_print(response, type, value);
	}
154 155
};

156 157
static CommandResult
handle_lsinfo_absolute(Response &r, const char *uri)
158
{
159 160
	PrintTagHandler h(r);
	if (!tag_stream_scan(uri, h)) {
161 162
		r.Error(ACK_ERROR_NO_EXIST, "No such file");
		return CommandResult::ERROR;
163 164
	}

165 166
	return CommandResult::OK;
}
167

168 169 170
static CommandResult
handle_lsinfo_relative(Client &client, Response &r, const char *uri)
{
171
#ifdef ENABLE_DATABASE
172
	CommandResult result = handle_lsinfo2(client, uri, r);
173
	if (result != CommandResult::OK)
174
		return result;
175 176
#else
	(void)client;
177
#endif
178 179

	if (isRootDirectory(uri)) {
180 181 182 183 184
		try {
			print_spl_list(r, ListPlaylistFiles());
		} catch (const std::exception &e) {
			LogError(e);
		}
185 186
	} else {
#ifndef ENABLE_DATABASE
187
		r.Error(ACK_ERROR_NO_EXIST, "No database");
188 189
		return CommandResult::ERROR;
#endif
190 191
	}

192
	return CommandResult::OK;
193 194
}

195
static CommandResult
196
handle_lsinfo_path(Client &, Response &r,
197 198 199 200 201 202 203 204
		   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;
	}

205
	song_print_info(r, song);
206 207 208 209 210 211 212
	return CommandResult::OK;
}

CommandResult
handle_lsinfo(Client &client, Request args, Response &r)
{
	/* default is root directory */
213 214 215 216 217 218 219 220
	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 = "";
221

222
	const auto located_uri = LocateUri(uri, &client
223
#ifdef ENABLE_DATABASE
224
					   , nullptr
225
#endif
226
					   );
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

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

245 246 247
#ifdef ENABLE_DATABASE

static CommandResult
248
handle_update(Response &r, UpdateService &update,
249 250 251 252
	      const char *uri_utf8, bool discard)
{
	unsigned ret = update.Enqueue(uri_utf8, discard);
	if (ret > 0) {
253
		r.Format("updating_db: %i\n", ret);
254 255
		return CommandResult::OK;
	} else {
256
		r.Error(ACK_ERROR_UPDATE_ALREADY, "already updating");
257 258 259 260
		return CommandResult::ERROR;
	}
}

261
static CommandResult
262
handle_update(Response &r, Database &db,
263 264
	      const char *uri_utf8, bool discard)
{
265
	unsigned id = db.Update(uri_utf8, discard);
266
	if (id > 0) {
267
		r.Format("updating_db: %i\n", id);
268 269 270 271
		return CommandResult::OK;
	} else {
		/* Database::Update() has returned 0 without setting
		   the Error: the method is not implemented */
272
		r.Error(ACK_ERROR_NO_EXIST, "Not implemented");
273 274 275 276
		return CommandResult::ERROR;
	}
}

277 278
#endif

279
static CommandResult
280
handle_update(Client &client, Request args, Response &r, bool discard)
281
{
282
#ifdef ENABLE_DATABASE
283
	const char *path = "";
284

285
	assert(args.size <= 1);
286
	if (!args.empty()) {
287
		path = args.front();
288

289
		if (*path == 0 || StringIsEqual(path, "/"))
290
			/* backwards compatibility with MPD 0.15 */
291
			path = "";
292
		else if (!uri_safe_local(path)) {
293
			r.Error(ACK_ERROR_ARG, "Malformed path");
294
			return CommandResult::ERROR;
295 296 297
		}
	}

298
	UpdateService *update = client.GetInstance().update;
299
	if (update != nullptr)
300
		return handle_update(r, *update, path, discard);
301

302
	Database *db = client.GetInstance().database;
303
	if (db != nullptr)
304
		return handle_update(r, *db, path, discard);
305
#else
306
	(void)client;
307
	(void)args;
308
	(void)discard;
309
#endif
310

311
	r.Error(ACK_ERROR_NO_EXIST, "No database");
312
	return CommandResult::ERROR;
313 314
}

315
CommandResult
316
handle_update(Client &client, Request args, gcc_unused Response &r)
317
{
318
	return handle_update(client, args, r, false);
319
}
320

321
CommandResult
322
handle_rescan(Client &client, Request args, Response &r)
323
{
324
	return handle_update(client, args, r, true);
325 326
}

327
CommandResult
328
handle_setvol(Client &client, Request args, Response &r)
329
{
330
	unsigned level = args.ParseUnsigned(0, 100);
331

332
	if (!volume_level_change(client.GetPartition().outputs, level)) {
333
		r.Error(ACK_ERROR_SYSTEM, "problems setting volume");
334
		return CommandResult::ERROR;
335 336
	}

337
	return CommandResult::OK;
338 339
}

340
CommandResult
341
handle_volume(Client &client, Request args, Response &r)
342
{
343
	int relative = args.ParseInt(0, -100, 100);
344

345 346 347
	auto &outputs = client.GetPartition().outputs;

	const int old_volume = volume_level_get(outputs);
348
	if (old_volume < 0) {
349
		r.Error(ACK_ERROR_SYSTEM, "No mixer");
350 351 352 353 354 355 356 357 358
		return CommandResult::ERROR;
	}

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

359
	if (new_volume != old_volume &&
360
	    !volume_level_change(outputs, new_volume)) {
361
		r.Error(ACK_ERROR_SYSTEM, "problems setting volume");
362 363 364 365 366 367
		return CommandResult::ERROR;
	}

	return CommandResult::OK;
}

368
CommandResult
369
handle_stats(Client &client, gcc_unused Request args, Response &r)
370
{
371
	stats_print(r, client.GetPartition());
372
	return CommandResult::OK;
373 374
}

375
CommandResult
376
handle_config(Client &client, gcc_unused Request args, Response &r)
377
{
378
	if (!client.IsLocal()) {
379 380
		r.Error(ACK_ERROR_PERMISSION,
			"Command only permitted to local clients");
381
		return CommandResult::ERROR;
382 383
	}

384
#ifdef ENABLE_DATABASE
385 386 387
	const Storage *storage = client.GetStorage();
	if (storage != nullptr) {
		const auto path = storage->MapUTF8("");
388
		r.Format("music_directory: %s\n", path.c_str());
389
	}
390
#endif
391

392
	return CommandResult::OK;
393 394
}

395
CommandResult
396
handle_idle(Client &client, Request args, Response &r)
397
{
398
	unsigned flags = 0;
399 400
	for (const char *i : args) {
		unsigned event = idle_parse_name(i);
401
		if (event == 0) {
402 403
			r.FormatError(ACK_ERROR_ARG,
				      "Unrecognized idle event: %s", i);
404
			return CommandResult::ERROR;
405
		}
406 407

		flags |= event;
408 409 410 411 412 413 414
	}

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

	/* enable "idle" mode on this client */
415
	client.IdleWait(flags);
416

417
	return CommandResult::IDLE;
418
}