2009-07-28 17:17:23 +02:00
|
|
|
/*
|
2013-01-03 10:33:04 +01:00
|
|
|
* Copyright (C) 2003-2013 The Music Player Daemon Project
|
2009-07-28 17:17:23 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-03 10:33:04 +01:00
|
|
|
#include "ClientInternal.hxx"
|
|
|
|
#include "protocol/Result.hxx"
|
2013-10-20 13:41:04 +02:00
|
|
|
#include "command/AllCommands.hxx"
|
2013-09-27 22:31:24 +02:00
|
|
|
#include "Log.hxx"
|
2009-07-28 17:17:23 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
static CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
client_process_command_list(Client &client, bool list_ok,
|
2013-01-04 01:17:25 +01:00
|
|
|
std::list<std::string> &&list)
|
2009-07-29 08:00:01 +02:00
|
|
|
{
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult ret = CommandResult::OK;
|
2009-07-29 08:00:01 +02:00
|
|
|
unsigned num = 0;
|
|
|
|
|
2013-01-04 01:17:25 +01:00
|
|
|
for (auto &&i : list) {
|
|
|
|
char *cmd = &*i.begin();
|
2009-07-29 08:00:01 +02:00
|
|
|
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(client_domain, "process command \"%s\"", cmd);
|
2009-07-29 08:00:01 +02:00
|
|
|
ret = command_process(client, num++, cmd);
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(client_domain, "command returned %i", ret);
|
2013-10-20 13:10:54 +02:00
|
|
|
if (ret != CommandResult::OK || client.IsExpired())
|
2009-07-29 08:00:01 +02:00
|
|
|
break;
|
|
|
|
else if (list_ok)
|
|
|
|
client_puts(client, "list_OK\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
client_process_line(Client &client, char *line)
|
2009-07-28 17:17:23 +02:00
|
|
|
{
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult ret;
|
2009-07-28 17:17:23 +02:00
|
|
|
|
|
|
|
if (strcmp(line, "noidle") == 0) {
|
2013-10-19 18:48:38 +02:00
|
|
|
if (client.idle_waiting) {
|
2009-07-28 17:17:23 +02:00
|
|
|
/* send empty idle response and leave idle mode */
|
2013-10-19 18:48:38 +02:00
|
|
|
client.idle_waiting = false;
|
2009-07-28 17:17:23 +02:00
|
|
|
command_success(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do nothing if the client wasn't idling: the client
|
|
|
|
has already received the full idle response from
|
|
|
|
client_idle_notify(), which he can now evaluate */
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2013-10-19 18:48:38 +02:00
|
|
|
} else if (client.idle_waiting) {
|
2009-07-28 17:17:23 +02:00
|
|
|
/* during idle mode, clients must not send anything
|
|
|
|
except "noidle" */
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(client_domain,
|
|
|
|
"[%u] command \"%s\" during idle",
|
2013-10-19 18:48:38 +02:00
|
|
|
client.num, line);
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::CLOSE;
|
2009-07-28 17:17:23 +02:00
|
|
|
}
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
if (client.cmd_list.IsActive()) {
|
2009-07-28 17:17:23 +02:00
|
|
|
if (strcmp(line, CLIENT_LIST_MODE_END) == 0) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(client_domain,
|
|
|
|
"[%u] process command list",
|
2013-10-19 18:48:38 +02:00
|
|
|
client.num);
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
auto &&cmd_list = client.cmd_list.Commit();
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2009-07-29 08:00:01 +02:00
|
|
|
ret = client_process_command_list(client,
|
2013-10-19 18:48:38 +02:00
|
|
|
client.cmd_list.IsOKMode(),
|
2013-01-04 01:17:25 +01:00
|
|
|
std::move(cmd_list));
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(client_domain,
|
|
|
|
"[%u] process command "
|
2013-10-19 18:48:38 +02:00
|
|
|
"list returned %i", client.num, ret);
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
if (ret == CommandResult::CLOSE ||
|
2013-10-19 18:48:38 +02:00
|
|
|
client.IsExpired())
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::CLOSE;
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
if (ret == CommandResult::OK)
|
2009-07-28 17:17:23 +02:00
|
|
|
command_success(client);
|
|
|
|
|
2013-10-19 18:48:38 +02:00
|
|
|
client.cmd_list.Reset();
|
2009-07-28 17:17:23 +02:00
|
|
|
} else {
|
2013-10-19 18:48:38 +02:00
|
|
|
if (!client.cmd_list.Add(line)) {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatWarning(client_domain,
|
|
|
|
"[%u] command list size "
|
|
|
|
"is larger than the max (%lu)",
|
2013-10-19 18:48:38 +02:00
|
|
|
client.num,
|
2013-09-27 22:31:24 +02:00
|
|
|
(unsigned long)client_max_command_list_size);
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::CLOSE;
|
2009-07-28 17:17:23 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
ret = CommandResult::OK;
|
2009-07-28 17:17:23 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (strcmp(line, CLIENT_LIST_MODE_BEGIN) == 0) {
|
2013-10-19 18:48:38 +02:00
|
|
|
client.cmd_list.Begin(false);
|
2013-10-20 13:10:54 +02:00
|
|
|
ret = CommandResult::OK;
|
2009-07-28 17:17:23 +02:00
|
|
|
} else if (strcmp(line, CLIENT_LIST_OK_MODE_BEGIN) == 0) {
|
2013-10-19 18:48:38 +02:00
|
|
|
client.cmd_list.Begin(true);
|
2013-10-20 13:10:54 +02:00
|
|
|
ret = CommandResult::OK;
|
2009-07-28 17:17:23 +02:00
|
|
|
} else {
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(client_domain,
|
|
|
|
"[%u] process command \"%s\"",
|
2013-10-19 18:48:38 +02:00
|
|
|
client.num, line);
|
2009-07-29 08:00:01 +02:00
|
|
|
ret = command_process(client, 0, line);
|
2013-09-27 22:31:24 +02:00
|
|
|
FormatDebug(client_domain,
|
|
|
|
"[%u] command returned %i",
|
2013-10-19 18:48:38 +02:00
|
|
|
client.num, ret);
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
if (ret == CommandResult::CLOSE ||
|
2013-10-19 18:48:38 +02:00
|
|
|
client.IsExpired())
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::CLOSE;
|
2009-07-28 17:17:23 +02:00
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
if (ret == CommandResult::OK)
|
2009-07-28 17:17:23 +02:00
|
|
|
command_success(client);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|