Process.cxx 4.06 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 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 21
#include "Client.hxx"
#include "Config.hxx"
22
#include "Domain.hxx"
Max Kellermann's avatar
Max Kellermann committed
23
#include "protocol/Result.hxx"
24
#include "command/AllCommands.hxx"
25
#include "Log.hxx"
26
#include "util/StringAPI.hxx"
27
#include "util/CharUtil.hxx"
28 29 30 31 32

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

33 34 35
inline CommandResult
Client::ProcessCommandList(bool list_ok,
			   std::list<std::string> &&list) noexcept
36
{
37
	unsigned n = 0;
38

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

42
		FormatDebug(client_domain, "process command \"%s\"", cmd);
43
		auto ret = command_process(*this, n++, cmd);
44
		FormatDebug(client_domain, "command returned %i", int(ret));
45 46 47
		if (IsExpired())
			return CommandResult::CLOSE;
		else if (ret != CommandResult::OK)
48
			return ret;
49
		else if (list_ok)
50
			Write("list_OK\n");
51 52
	}

53
	return CommandResult::OK;
54 55
}

56
CommandResult
57
Client::ProcessLine(char *line) noexcept
58
{
59 60
	assert(!background_command);

61 62
	if (!IsLowerAlphaASCII(*line)) {
		/* all valid MPD commands begin with a lower case
63 64 65 66 67 68 69 70
		   letter; this could be a badly routed HTTP
		   request */
		FormatWarning(client_domain,
			      "[%u] malformed command \"%s\"",
			      num, line);
		return CommandResult::CLOSE;
	}

71
	if (StringIsEqual(line, "noidle")) {
72
		if (idle_waiting) {
73
			/* send empty idle response and leave idle mode */
74 75
			idle_waiting = false;
			command_success(*this);
76 77 78 79
		}

		/* do nothing if the client wasn't idling: the client
		   has already received the full idle response from
80
		   IdleNotify(), which he can now evaluate */
81

82
		return CommandResult::OK;
83
	} else if (idle_waiting) {
84 85
		/* during idle mode, clients must not send anything
		   except "noidle" */
86 87
		FormatWarning(client_domain,
			      "[%u] command \"%s\" during idle",
88
			      num, line);
89
		return CommandResult::CLOSE;
90 91
	}

92
	if (cmd_list.IsActive()) {
93
		if (StringIsEqual(line, CLIENT_LIST_MODE_END)) {
94 95
			const unsigned id = num;

96 97
			FormatDebug(client_domain,
				    "[%u] process command list",
98
				    id);
99

100 101 102
			const bool ok_mode = cmd_list.IsOKMode();
			auto list = cmd_list.Commit();
			cmd_list.Reset();
103

104
			auto ret = ProcessCommandList(ok_mode,
105
						      std::move(list));
106 107
			FormatDebug(client_domain,
				    "[%u] process command "
108
				    "list returned %i", id, int(ret));
109

110
			if (ret == CommandResult::OK)
111
				command_success(*this);
112

113
			return ret;
114
		} else {
115
			if (!cmd_list.Add(line)) {
116 117 118
				FormatWarning(client_domain,
					      "[%u] command list size "
					      "is larger than the max (%lu)",
119
					      num,
120
					      (unsigned long)client_max_command_list_size);
121
				return CommandResult::CLOSE;
122 123
			}

124
			return CommandResult::OK;
125 126
		}
	} else {
127
		if (StringIsEqual(line, CLIENT_LIST_MODE_BEGIN)) {
128
			cmd_list.Begin(false);
129
			return CommandResult::OK;
130
		} else if (StringIsEqual(line, CLIENT_LIST_OK_MODE_BEGIN)) {
131
			cmd_list.Begin(true);
132
			return CommandResult::OK;
133
		} else {
134 135
			const unsigned id = num;

136 137
			FormatDebug(client_domain,
				    "[%u] process command \"%s\"",
138
				    id, line);
139
			auto ret = command_process(*this, 0, line);
140 141
			FormatDebug(client_domain,
				    "[%u] command returned %i",
142
				    id, int(ret));
143

144 145 146
			if (IsExpired())
				return CommandResult::CLOSE;

147
			if (ret == CommandResult::OK)
148
				command_success(*this);
149 150

			return ret;
151 152 153
		}
	}
}