Process.cxx 3.95 KB
Newer Older
1
/*
2
 * Copyright 2003-2019 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
		if (ret != CommandResult::OK)
46
			return ret;
47
		else if (list_ok)
48
			Write("list_OK\n");
49 50
	}

51
	return CommandResult::OK;
52 53
}

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

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

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

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

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

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

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

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

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

108
			if (ret == CommandResult::OK)
109
				command_success(*this);
110

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

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

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

142
			if (ret == CommandResult::OK)
143
				command_success(*this);
144 145

			return ret;
146 147 148
		}
	}
}