summaryrefslogtreecommitdiff
path: root/src/client/ClientProcess.cxx
blob: c7c71ab47a6c6e9459f14d045a8fd21d99aae4af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright 2003-2017 The Music Player Daemon Project
 * 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 "ClientInternal.hxx"
#include "protocol/Result.hxx"
#include "command/AllCommands.hxx"
#include "Log.hxx"
#include "util/StringAPI.hxx"
#include "util/CharUtil.hxx"

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

static CommandResult
client_process_command_list(Client &client, bool list_ok,
			    std::list<std::string> &&list)
{
	CommandResult ret = CommandResult::OK;
	unsigned num = 0;

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

		FormatDebug(client_domain, "process command \"%s\"", cmd);
		ret = command_process(client, num++, cmd);
		FormatDebug(client_domain, "command returned %i", int(ret));
		if (ret != CommandResult::OK || client.IsExpired())
			break;
		else if (list_ok)
			client.Write("list_OK\n");
	}

	return ret;
}

CommandResult
client_process_line(Client &client, char *line)
{
	CommandResult ret;

	if (StringIsEqual(line, "noidle")) {
		if (client.idle_waiting) {
			/* send empty idle response and leave idle mode */
			client.idle_waiting = false;
			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 */

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

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

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

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

			if (ret == CommandResult::CLOSE ||
			    client.IsExpired())
				return CommandResult::CLOSE;

			if (ret == CommandResult::OK)
				command_success(client);

			client.cmd_list.Reset();
		} else {
			if (!client.cmd_list.Add(line)) {
				FormatWarning(client_domain,
					      "[%u] command list size "
					      "is larger than the max (%lu)",
					      client.num,
					      (unsigned long)client_max_command_list_size);
				return CommandResult::CLOSE;
			}

			ret = CommandResult::OK;
		}
	} else {
		if (StringIsEqual(line, CLIENT_LIST_MODE_BEGIN)) {
			client.cmd_list.Begin(false);
			ret = CommandResult::OK;
		} else if (StringIsEqual(line, CLIENT_LIST_OK_MODE_BEGIN)) {
			client.cmd_list.Begin(true);
			ret = CommandResult::OK;
		} else if (IsUpperAlphaASCII(*line)) {
			/* no valid MPD command begins with an upper
			   case letter; this could be a badly routed
			   HTTP request */
			FormatWarning(client_domain,
				      "[%u] malformed command \"%s\"",
				      client.num, line);
			ret = CommandResult::CLOSE;
		} else {
			FormatDebug(client_domain,
				    "[%u] process command \"%s\"",
				    client.num, line);
			ret = command_process(client, 0, line);
			FormatDebug(client_domain,
				    "[%u] command returned %i",
				    client.num, int(ret));

			if (ret == CommandResult::CLOSE ||
			    client.IsExpired())
				return CommandResult::CLOSE;

			if (ret == CommandResult::OK)
				command_success(client);
		}
	}

	return ret;
}