ClientProcess.cxx 3.93 KB
Newer Older
1
/*
2
 * Copyright 2003-2016 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "config.h"
Max Kellermann's avatar
Max Kellermann committed
21 22
#include "ClientInternal.hxx"
#include "protocol/Result.hxx"
23
#include "command/AllCommands.hxx"
24
#include "Log.hxx"
25
#include "util/StringAPI.hxx"
26 27 28 29 30

#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"

31
static CommandResult
32
client_process_command_list(Client &client, bool list_ok,
33
			    std::list<std::string> &&list)
34
{
35
	CommandResult ret = CommandResult::OK;
36 37
	unsigned num = 0;

38 39
	for (auto &&i : list) {
		char *cmd = &*i.begin();
40

41
		FormatDebug(client_domain, "process command \"%s\"", cmd);
42
		ret = command_process(client, num++, cmd);
43
		FormatDebug(client_domain, "command returned %i", int(ret));
44
		if (ret != CommandResult::OK || client.IsExpired())
45 46 47 48 49 50 51 52
			break;
		else if (list_ok)
			client_puts(client, "list_OK\n");
	}

	return ret;
}

53
CommandResult
54
client_process_line(Client &client, char *line)
55
{
56
	CommandResult ret;
57

58
	if (StringIsEqual(line, "noidle")) {
59
		if (client.idle_waiting) {
60
			/* send empty idle response and leave idle mode */
61
			client.idle_waiting = false;
62 63 64 65 66 67 68
			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 */

69
		return CommandResult::OK;
70
	} else if (client.idle_waiting) {
71 72
		/* during idle mode, clients must not send anything
		   except "noidle" */
73 74
		FormatWarning(client_domain,
			      "[%u] command \"%s\" during idle",
75
			      client.num, line);
76
		return CommandResult::CLOSE;
77 78
	}

79
	if (client.cmd_list.IsActive()) {
80
		if (StringIsEqual(line, CLIENT_LIST_MODE_END)) {
81 82
			FormatDebug(client_domain,
				    "[%u] process command list",
83
				    client.num);
84

85
			auto &&cmd_list = client.cmd_list.Commit();
86

87
			ret = client_process_command_list(client,
88
							  client.cmd_list.IsOKMode(),
89
							  std::move(cmd_list));
90 91
			FormatDebug(client_domain,
				    "[%u] process command "
92
				    "list returned %i", client.num, int(ret));
93

94
			if (ret == CommandResult::CLOSE ||
95
			    client.IsExpired())
96
				return CommandResult::CLOSE;
97

98
			if (ret == CommandResult::OK)
99 100
				command_success(client);

101
			client.cmd_list.Reset();
102
		} else {
103
			if (!client.cmd_list.Add(line)) {
104 105 106
				FormatWarning(client_domain,
					      "[%u] command list size "
					      "is larger than the max (%lu)",
107
					      client.num,
108
					      (unsigned long)client_max_command_list_size);
109
				return CommandResult::CLOSE;
110 111
			}

112
			ret = CommandResult::OK;
113 114
		}
	} else {
115
		if (StringIsEqual(line, CLIENT_LIST_MODE_BEGIN)) {
116
			client.cmd_list.Begin(false);
117
			ret = CommandResult::OK;
118
		} else if (StringIsEqual(line, CLIENT_LIST_OK_MODE_BEGIN)) {
119
			client.cmd_list.Begin(true);
120
			ret = CommandResult::OK;
121
		} else {
122 123
			FormatDebug(client_domain,
				    "[%u] process command \"%s\"",
124
				    client.num, line);
125
			ret = command_process(client, 0, line);
126 127
			FormatDebug(client_domain,
				    "[%u] command returned %i",
128
				    client.num, int(ret));
129

130
			if (ret == CommandResult::CLOSE ||
131
			    client.IsExpired())
132
				return CommandResult::CLOSE;
133

134
			if (ret == CommandResult::OK)
135 136 137 138 139 140
				command_success(client);
		}
	}

	return ret;
}