2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2019-04-03 21:31:32 +02:00
|
|
|
#include "Client.hxx"
|
|
|
|
#include "Config.hxx"
|
2019-04-03 21:26:16 +02:00
|
|
|
#include "Domain.hxx"
|
2013-10-20 13:41:04 +02:00
|
|
|
#include "command/AllCommands.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2015-08-14 19:48:30 +02:00
|
|
|
#include "util/StringAPI.hxx"
|
2014-08-12 14:39:30 +02:00
|
|
|
#include "util/CharUtil.hxx"
|
2009-07-28 17:17:23 +02:00
|
|
|
|
|
|
|
#define CLIENT_LIST_MODE_BEGIN "command_list_begin"
|
|
|
|
#define CLIENT_LIST_OK_MODE_BEGIN "command_list_ok_begin"
|
|
|
|
#define CLIENT_LIST_MODE_END "command_list_end"
|
|
|
|
|
2019-04-03 20:03:17 +02:00
|
|
|
inline CommandResult
|
|
|
|
Client::ProcessCommandList(bool list_ok,
|
|
|
|
std::list<std::string> &&list) noexcept
|
2009-07-29 08:00:01 +02:00
|
|
|
{
|
2019-04-03 20:03:17 +02:00
|
|
|
unsigned n = 0;
|
2009-07-29 08:00:01 +02:00
|
|
|
|
2013-01-04 01:17:25 +01:00
|
|
|
for (auto &&i : list) {
|
|
|
|
char *cmd = &*i.begin();
|
2009-07-29 08:00:01 +02:00
|
|
|
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtDebug(client_domain, "process command \"{}\"", cmd);
|
2019-04-03 22:31:49 +02:00
|
|
|
auto ret = command_process(*this, n++, cmd);
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtDebug(client_domain, "command returned {}", unsigned(ret));
|
2019-08-22 09:07:41 +02:00
|
|
|
if (IsExpired())
|
|
|
|
return CommandResult::CLOSE;
|
|
|
|
else if (ret != CommandResult::OK)
|
2019-04-03 22:31:49 +02:00
|
|
|
return ret;
|
2009-07-29 08:00:01 +02:00
|
|
|
else if (list_ok)
|
2019-04-03 20:03:17 +02:00
|
|
|
Write("list_OK\n");
|
2009-07-29 08:00:01 +02:00
|
|
|
}
|
|
|
|
|
2019-04-03 22:31:49 +02:00
|
|
|
return CommandResult::OK;
|
2009-07-29 08:00:01 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2019-04-03 20:03:17 +02:00
|
|
|
Client::ProcessLine(char *line) noexcept
|
2009-07-28 17:17:23 +02:00
|
|
|
{
|
2019-04-03 14:31:57 +02:00
|
|
|
assert(!background_command);
|
|
|
|
|
2019-04-03 22:28:45 +02:00
|
|
|
if (!IsLowerAlphaASCII(*line)) {
|
|
|
|
/* all valid MPD commands begin with a lower case
|
2019-04-03 22:27:41 +02:00
|
|
|
letter; this could be a badly routed HTTP
|
|
|
|
request */
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtWarning(client_domain,
|
|
|
|
"[{}] malformed command \"{}\"",
|
|
|
|
num, line);
|
2019-04-03 22:27:41 +02:00
|
|
|
return CommandResult::CLOSE;
|
|
|
|
}
|
|
|
|
|
2015-08-14 19:48:30 +02:00
|
|
|
if (StringIsEqual(line, "noidle")) {
|
2019-04-03 20:03:17 +02:00
|
|
|
if (idle_waiting) {
|
2009-07-28 17:17:23 +02:00
|
|
|
/* send empty idle response and leave idle mode */
|
2019-04-03 20:03:17 +02:00
|
|
|
idle_waiting = false;
|
2021-10-22 11:51:12 +02:00
|
|
|
WriteOK();
|
2009-07-28 17:17:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* do nothing if the client wasn't idling: the client
|
|
|
|
has already received the full idle response from
|
2019-04-03 22:30:18 +02:00
|
|
|
IdleNotify(), which he can now evaluate */
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2019-04-03 20:03:17 +02:00
|
|
|
} else if (idle_waiting) {
|
2009-07-28 17:17:23 +02:00
|
|
|
/* during idle mode, clients must not send anything
|
|
|
|
except "noidle" */
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtWarning(client_domain,
|
|
|
|
"[{}] command \"{}\" during idle",
|
|
|
|
num, line);
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::CLOSE;
|
2009-07-28 17:17:23 +02:00
|
|
|
}
|
|
|
|
|
2019-04-03 20:03:17 +02:00
|
|
|
if (cmd_list.IsActive()) {
|
2015-08-14 19:48:30 +02:00
|
|
|
if (StringIsEqual(line, CLIENT_LIST_MODE_END)) {
|
2019-04-04 10:37:38 +02:00
|
|
|
const unsigned id = num;
|
|
|
|
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtDebug(client_domain,
|
|
|
|
"[{}] process command list",
|
|
|
|
id);
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2019-04-03 22:38:26 +02:00
|
|
|
const bool ok_mode = cmd_list.IsOKMode();
|
|
|
|
auto list = cmd_list.Commit();
|
|
|
|
cmd_list.Reset();
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2019-04-03 22:38:26 +02:00
|
|
|
auto ret = ProcessCommandList(ok_mode,
|
2019-04-03 22:30:38 +02:00
|
|
|
std::move(list));
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtDebug(client_domain,
|
|
|
|
"[{}] process command "
|
|
|
|
"list returned {}", id, unsigned(ret));
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
if (ret == CommandResult::OK)
|
2021-10-22 11:51:12 +02:00
|
|
|
WriteOK();
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2019-04-03 22:30:38 +02:00
|
|
|
return ret;
|
2009-07-28 17:17:23 +02:00
|
|
|
} else {
|
2019-04-03 20:03:17 +02:00
|
|
|
if (!cmd_list.Add(line)) {
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtWarning(client_domain,
|
|
|
|
"[{}] command list size "
|
|
|
|
"is larger than the max ({})",
|
|
|
|
num, client_max_command_list_size);
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::CLOSE;
|
2009-07-28 17:17:23 +02:00
|
|
|
}
|
|
|
|
|
2019-04-03 22:30:38 +02:00
|
|
|
return CommandResult::OK;
|
2009-07-28 17:17:23 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-08-14 19:48:30 +02:00
|
|
|
if (StringIsEqual(line, CLIENT_LIST_MODE_BEGIN)) {
|
2019-04-03 20:03:17 +02:00
|
|
|
cmd_list.Begin(false);
|
2019-04-03 22:30:38 +02:00
|
|
|
return CommandResult::OK;
|
2015-08-14 19:48:30 +02:00
|
|
|
} else if (StringIsEqual(line, CLIENT_LIST_OK_MODE_BEGIN)) {
|
2019-04-03 20:03:17 +02:00
|
|
|
cmd_list.Begin(true);
|
2019-04-03 22:30:38 +02:00
|
|
|
return CommandResult::OK;
|
2009-07-28 17:17:23 +02:00
|
|
|
} else {
|
2019-04-04 10:37:38 +02:00
|
|
|
const unsigned id = num;
|
|
|
|
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtDebug(client_domain,
|
|
|
|
"[{}] process command \"{}\"",
|
|
|
|
id, line);
|
2019-04-03 22:30:38 +02:00
|
|
|
auto ret = command_process(*this, 0, line);
|
2021-06-24 20:22:48 +02:00
|
|
|
FmtDebug(client_domain,
|
|
|
|
"[{}] command returned {}",
|
|
|
|
id, unsigned(ret));
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2019-08-22 09:07:41 +02:00
|
|
|
if (IsExpired())
|
|
|
|
return CommandResult::CLOSE;
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
if (ret == CommandResult::OK)
|
2021-10-22 11:51:12 +02:00
|
|
|
WriteOK();
|
2019-04-03 22:30:38 +02:00
|
|
|
|
|
|
|
return ret;
|
2009-07-28 17:17:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|