Process.cxx 3.98 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
		FmtDebug(client_domain, "process command \"{}\"", cmd);
43
		auto ret = command_process(*this, n++, cmd);
44
		FmtDebug(client_domain, "command returned {}", unsigned(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
		   letter; this could be a badly routed HTTP
		   request */
65 66 67
		FmtWarning(client_domain,
			   "[{}] malformed command \"{}\"",
			   num, line);
68 69 70
		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 88
		FmtWarning(client_domain,
			   "[{}] command \"{}\" during idle",
			   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 98
			FmtDebug(client_domain,
				 "[{}] process command list",
				 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 108
			FmtDebug(client_domain,
				 "[{}] process command "
				 "list returned {}", id, unsigned(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 119
				FmtWarning(client_domain,
					   "[{}] command list size "
					   "is larger than the max ({})",
					   num, client_max_command_list_size);
120
				return CommandResult::CLOSE;
121 122
			}

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

135 136 137
			FmtDebug(client_domain,
				 "[{}] process command \"{}\"",
				 id, line);
138
			auto ret = command_process(*this, 0, line);
139 140 141
			FmtDebug(client_domain,
				 "[{}] command returned {}",
				 id, unsigned(ret));
142

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

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

			return ret;
150 151 152
		}
	}
}