Filter.cxx 11.4 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3
 * http://www.musicpd.org
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.
18 19
 */

20
#include "config.h"
21
#include "Filter.hxx"
22
#include "NotSongFilter.hxx"
23 24 25 26
#include "UriSongFilter.hxx"
#include "BaseSongFilter.hxx"
#include "TagSongFilter.hxx"
#include "ModifiedSinceSongFilter.hxx"
27
#include "AudioFormatSongFilter.hxx"
28
#include "pcm/AudioParser.hxx"
29
#include "tag/ParseName.hxx"
30
#include "time/ISO8601.hxx"
31
#include "util/CharUtil.hxx"
32
#include "util/ConstBuffer.hxx"
33
#include "util/RuntimeError.hxx"
34
#include "util/StringCompare.hxx"
35
#include "util/StringStrip.hxx"
36
#include "util/StringView.hxx"
37
#include "util/ASCII.hxx"
38
#include "util/UriUtil.hxx"
39

40 41
#include <cassert>

42 43
#include <stdlib.h>

44 45
#define LOCATE_TAG_FILE_KEY     "file"
#define LOCATE_TAG_FILE_KEY_OLD "filename"
46 47
#define LOCATE_TAG_ANY_KEY      "any"

48 49 50 51 52 53 54
enum {
	/**
	 * Limit the search to files within the given directory.
	 */
	LOCATE_TAG_BASE_TYPE = TAG_NUM_OF_ITEM_TYPES + 1,

	LOCATE_TAG_MODIFIED_SINCE,
55
	LOCATE_TAG_AUDIO_FORMAT,
56 57 58
	LOCATE_TAG_FILE_TYPE,
	LOCATE_TAG_ANY_TYPE,
};
59

60 61 62 63 64
/**
 * @return #TAG_NUM_OF_ITEM_TYPES on error
 */
gcc_pure
static unsigned
65
locate_parse_type(const char *str) noexcept
66
{
67 68
	if (StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY) ||
	    StringEqualsCaseASCII(str, LOCATE_TAG_FILE_KEY_OLD))
69 70
		return LOCATE_TAG_FILE_TYPE;

71
	if (StringEqualsCaseASCII(str, LOCATE_TAG_ANY_KEY))
72 73
		return LOCATE_TAG_ANY_TYPE;

74 75 76
	if (strcmp(str, "base") == 0)
		return LOCATE_TAG_BASE_TYPE;

77 78 79
	if (strcmp(str, "modified-since") == 0)
		return LOCATE_TAG_MODIFIED_SINCE;

80 81 82
	if (StringEqualsCaseASCII(str, "AudioFormat"))
		return LOCATE_TAG_AUDIO_FORMAT;

83
	return tag_name_parse_i(str);
84 85
}

86 87
SongFilter::SongFilter(TagType tag, const char *value, bool fold_case)
{
88 89
	/* for compatibility with MPD 0.20 and older, "fold_case" also
	   switches on "substring" */
90 91
	and_filter.AddItem(std::make_unique<TagSongFilter>(tag,
							   StringFilter(value, fold_case, fold_case, false)));
92 93
}

94 95
/* this destructor exists here just so it won't get inlined */
SongFilter::~SongFilter() = default;
96 97 98 99 100 101 102

std::string
SongFilter::ToExpression() const noexcept
{
	return and_filter.ToExpression();
}

103 104
static std::chrono::system_clock::time_point
ParseTimeStamp(const char *s)
105 106 107
{
	assert(s != nullptr);

108 109 110 111 112 113 114 115 116 117 118 119 120
	try {
		/* try ISO 8601 */
		return ParseISO8601(s).first;
	} catch (...) {
		char *endptr;
		unsigned long long value = strtoull(s, &endptr, 10);
		if (*endptr == 0 && endptr > s)
			/* it's an integral UNIX time stamp */
			return std::chrono::system_clock::from_time_t((time_t)value);

		/* rethrow the ParseISO8601() error */
		throw;
	}
121 122
}

123 124 125
static constexpr bool
IsTagNameChar(char ch) noexcept
{
126
	return IsAlphaASCII(ch) || ch == '_' || ch == '-';
127 128 129 130 131 132 133 134 135 136 137
}

static const char *
FirstNonTagNameChar(const char *s) noexcept
{
	while (IsTagNameChar(*s))
		++s;
	return s;
}

static auto
138
ExpectWord(const char *&s)
139
{
140
	const char *begin = s;
141 142
	const char *end = FirstNonTagNameChar(s);
	if (end == s)
143
		throw std::runtime_error("Word expected");
144 145

	s = StripLeft(end);
146 147 148 149 150 151 152
	return std::string(begin, end);
}

static auto
ExpectFilterType(const char *&s)
{
	const auto name = ExpectWord(s);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

	const auto type = locate_parse_type(name.c_str());
	if (type == TAG_NUM_OF_ITEM_TYPES)
		throw FormatRuntimeError("Unknown filter type: %s",
					 name.c_str());

	return type;
}

static constexpr bool
IsQuote(char ch) noexcept
{
	return ch == '"' || ch == '\'';
}

static std::string
ExpectQuoted(const char *&s)
{
	const char quote = *s++;
	if (!IsQuote(quote))
		throw std::runtime_error("Quoted string expected");

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
	char buffer[4096];
	size_t length = 0;

	while (*s != quote) {
		if (*s == '\\')
			/* backslash escapes the following character */
			++s;

		if (*s == 0)
			throw std::runtime_error("Closing quote not found");

		buffer[length++] = *s++;

		if (length >= sizeof(buffer))
			throw std::runtime_error("Quoted value is too long");
	}

	s = StripLeft(s + 1);
193

194
	return {buffer, length};
195 196
}

197 198 199 200 201 202 203 204 205
/**
 * Parse a string operator and its second operand and convert it to a
 * #StringFilter.
 *
 * Throws on error.
 */
static StringFilter
ParseStringFilter(const char *&s, bool fold_case)
{
206 207 208
	if (auto after_contains = StringAfterPrefixIgnoreCase(s, "contains ")) {
		s = StripLeft(after_contains);
		auto value = ExpectQuoted(s);
Rosen Penev's avatar
Rosen Penev committed
209
		return {std::move(value), fold_case, true, false};
210 211 212 213 214
	}

	if (auto after_not_contains = StringAfterPrefixIgnoreCase(s, "!contains ")) {
		s = StripLeft(after_not_contains);
		auto value = ExpectQuoted(s);
Rosen Penev's avatar
Rosen Penev committed
215
		return {std::move(value), fold_case, true, true};
216 217
	}

218
	bool negated = false;
219 220 221 222 223 224 225 226 227 228 229 230 231 232

#ifdef HAVE_PCRE
	if ((s[0] == '!' || s[0] == '=') && s[1] == '~') {
		negated = s[0] == '!';
		s = StripLeft(s + 2);
		auto value = ExpectQuoted(s);
		StringFilter f(std::move(value), fold_case, false, negated);
		f.SetRegex(std::make_shared<UniqueRegex>(f.GetValue().c_str(),
							 false, false,
							 fold_case));
		return f;
	}
#endif

233 234 235 236 237 238 239 240
	if (s[0] == '!' && s[1] == '=')
		negated = true;
	else if (s[0] != '=' || s[1] != '=')
		throw std::runtime_error("'==' or '!=' expected");

	s = StripLeft(s + 2);
	auto value = ExpectQuoted(s);

Rosen Penev's avatar
Rosen Penev committed
241
	return {std::move(value), fold_case, false, negated};
242 243
}

244 245
ISongFilterPtr
SongFilter::ParseExpression(const char *&s, bool fold_case)
246 247 248 249 250
{
	assert(*s == '(');

	s = StripLeft(s + 1);

251 252 253 254 255 256 257
	if (*s == '(') {
		auto first = ParseExpression(s, fold_case);
		if (*s == ')') {
			++s;
			return first;
		}

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
		if (ExpectWord(s) != "AND")
			throw std::runtime_error("'AND' expected");

		auto and_filter = std::make_unique<AndSongFilter>();
		and_filter->AddItem(std::move(first));

		while (true) {
			and_filter->AddItem(ParseExpression(s, fold_case));

			if (*s == ')') {
				++s;
				return and_filter;
			}

			if (ExpectWord(s) != "AND")
				throw std::runtime_error("'AND' expected");
		}
275
	}
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290
	if (*s == '!') {
		s = StripLeft(s + 1);

		if (*s != '(')
			throw std::runtime_error("'(' expected");

		auto inner = ParseExpression(s, fold_case);
		if (*s != ')')
			throw std::runtime_error("')' expected");
		s = StripLeft(s + 1);

		return std::make_unique<NotSongFilter>(std::move(inner));
	}

291
	auto type = ExpectFilterType(s);
292 293 294 295 296

	if (type == LOCATE_TAG_MODIFIED_SINCE) {
		const auto value_s = ExpectQuoted(s);
		if (*s != ')')
			throw std::runtime_error("')' expected");
297 298
		s = StripLeft(s + 1);
		return std::make_unique<ModifiedSinceSongFilter>(ParseTimeStamp(value_s.c_str()));
299 300 301 302
	} else if (type == LOCATE_TAG_BASE_TYPE) {
		auto value = ExpectQuoted(s);
		if (*s != ')')
			throw std::runtime_error("')' expected");
303
		s = StripLeft(s + 1);
304

305
		return std::make_unique<BaseSongFilter>(std::move(value));
306
	} else if (type == LOCATE_TAG_AUDIO_FORMAT) {
307 308 309 310 311 312 313
		bool mask;
		if (s[0] == '=' && s[1] == '=')
			mask = false;
		else if (s[0] == '=' && s[1] == '~')
			mask = true;
		else
			throw std::runtime_error("'==' or '=~' expected");
314 315 316 317

		s = StripLeft(s + 2);

		const auto value = ParseAudioFormat(ExpectQuoted(s).c_str(),
318
						    mask);
319 320 321 322 323 324

		if (*s != ')')
			throw std::runtime_error("')' expected");
		s = StripLeft(s + 1);

		return std::make_unique<AudioFormatSongFilter>(value);
325
	} else {
326
		auto string_filter = ParseStringFilter(s, fold_case);
327 328 329
		if (*s != ')')
			throw std::runtime_error("')' expected");

330 331 332 333 334 335
		s = StripLeft(s + 1);

		if (type == LOCATE_TAG_ANY_TYPE)
			type = TAG_NUM_OF_ITEM_TYPES;

		if (type == LOCATE_TAG_FILE_TYPE)
336
			return std::make_unique<UriSongFilter>(std::move(string_filter));
337 338

		return std::make_unique<TagSongFilter>(TagType(type),
339
						       std::move(string_filter));
340 341 342
	}
}

343
void
344 345 346
SongFilter::Parse(const char *tag_string, const char *value, bool fold_case)
{
	unsigned tag = locate_parse_type(tag_string);
347 348 349

	switch (tag) {
	case TAG_NUM_OF_ITEM_TYPES:
350
		throw std::runtime_error("Unknown filter type");
351

352
	case LOCATE_TAG_BASE_TYPE:
353
		if (!uri_safe_local(value))
354
			throw std::runtime_error("Bad URI");
355

356
		and_filter.AddItem(std::make_unique<BaseSongFilter>(value));
357 358 359
		break;

	case LOCATE_TAG_MODIFIED_SINCE:
360
		and_filter.AddItem(std::make_unique<ModifiedSinceSongFilter>(ParseTimeStamp(value)));
361
		break;
362

363
	case LOCATE_TAG_FILE_TYPE:
364 365
		/* for compatibility with MPD 0.20 and older,
		   "fold_case" also switches on "substring" */
366 367 368 369
		and_filter.AddItem(std::make_unique<UriSongFilter>(StringFilter(value,
										fold_case,
										fold_case,
										false)));
370 371 372 373 374 375
		break;

	default:
		if (tag == LOCATE_TAG_ANY_TYPE)
			tag = TAG_NUM_OF_ITEM_TYPES;

376 377
		/* for compatibility with MPD 0.20 and older,
		   "fold_case" also switches on "substring" */
378
		and_filter.AddItem(std::make_unique<TagSongFilter>(TagType(tag),
379 380 381 382
								   StringFilter(value,
										fold_case,
										fold_case,
										false)));
383 384
		break;
	}
385 386
}

387
void
388
SongFilter::Parse(ConstBuffer<const char *> args, bool fold_case)
389
{
390
	if (args.empty())
391
		throw std::runtime_error("Incorrect number of filter arguments");
392

393
	do {
394 395
		if (*args.front() == '(') {
			const char *s = args.shift();
396 397
			const char *end = s;
			auto f = ParseExpression(end, fold_case);
398 399 400
			if (*end != 0)
				throw std::runtime_error("Unparsed garbage after expression");

401
			and_filter.AddItem(std::move(f));
402 403 404
			continue;
		}

405 406 407 408 409 410 411
		if (args.size < 2)
			throw std::runtime_error("Incorrect number of filter arguments");

		const char *tag = args.shift();
		const char *value = args.shift();
		Parse(tag, value, fold_case);
	} while (!args.empty());
412 413
}

414 415 416 417 418 419
void
SongFilter::Optimize() noexcept
{
	OptimizeSongFilter(and_filter);
}

420
bool
421
SongFilter::Match(const LightSong &song) const noexcept
422
{
423
	return and_filter.Match(song);
424 425
}

426 427 428
bool
SongFilter::HasFoldCase() const noexcept
{
429 430 431 432 433 434 435 436
	return std::any_of(
		and_filter.GetItems().begin(), and_filter.GetItems().end(),
		[](const auto &item) {
			if (auto t = dynamic_cast<const TagSongFilter *>(item.get()))
				return t->GetFoldCase();

			if (auto u = dynamic_cast<const UriSongFilter *>(item.get()))
				return u->GetFoldCase();
437

438 439
			return false;
		});
440 441
}

442
bool
443
SongFilter::HasOtherThanBase() const noexcept
444
{
445 446 447 448 449
	return std::any_of(and_filter.GetItems().begin(), and_filter.GetItems().end(),
			   [=](const auto &item) {
				   return !dynamic_cast<const BaseSongFilter *>(
					   item.get());
			   });
450 451
}

452
const char *
453
SongFilter::GetBase() const noexcept
454
{
455
	for (const auto &i : and_filter.GetItems()) {
456 457 458 459
		const auto *f = dynamic_cast<const BaseSongFilter *>(i.get());
		if (f != nullptr)
			return f->GetValue();
	}
460

461
	return nullptr;
462
}
463 464

SongFilter
465
SongFilter::WithoutBasePrefix(const std::string_view prefix) const noexcept
466 467 468
{
	SongFilter result;

469
	for (const auto &i : and_filter.GetItems()) {
470 471 472
		const auto *f = dynamic_cast<const BaseSongFilter *>(i.get());
		if (f != nullptr) {
			const char *s = StringAfterPrefix(f->GetValue(), prefix);
473 474 475 476 477 478 479 480
			if (s != nullptr) {
				if (*s == 0)
					continue;

				if (*s == '/') {
					++s;

					if (*s != 0)
481
						result.and_filter.AddItem(std::make_unique<BaseSongFilter>(s));
482 483 484 485 486 487

					continue;
				}
			}
		}

488
		result.and_filter.AddItem(i->Clone());
489 490 491 492
	}

	return result;
}