MessageCommands.cxx 3.47 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 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 "MessageCommands.hxx"
22
#include "Request.hxx"
23 24
#include "client/Client.hxx"
#include "client/ClientList.hxx"
25
#include "client/Response.hxx"
26
#include "Instance.hxx"
27
#include "Partition.hxx"
28
#include "util/ConstBuffer.hxx"
29

30 31 32
#include <set>
#include <string>

33 34
#include <assert.h>

35
CommandResult
36
handle_subscribe(Client &client, Request args, Response &r)
37
{
38 39
	assert(args.size == 1);
	const char *const channel_name = args[0];
40

41
	switch (client.Subscribe(channel_name)) {
42
	case Client::SubscribeResult::OK:
43
		return CommandResult::OK;
44

45
	case Client::SubscribeResult::INVALID:
46
		r.Error(ACK_ERROR_ARG, "invalid channel name");
47
		return CommandResult::ERROR;
48

49
	case Client::SubscribeResult::ALREADY:
50
		r.Error(ACK_ERROR_EXIST, "already subscribed to this channel");
51
		return CommandResult::ERROR;
52

53
	case Client::SubscribeResult::FULL:
54
		r.Error(ACK_ERROR_EXIST, "subscription list is full");
55
		return CommandResult::ERROR;
56 57 58
	}

	/* unreachable */
59 60
	assert(false);
	gcc_unreachable();
61 62
}

63
CommandResult
64
handle_unsubscribe(Client &client, Request args, Response &r)
65
{
66 67
	assert(args.size == 1);
	const char *const channel_name = args[0];
68

69
	if (client.Unsubscribe(channel_name))
70
		return CommandResult::OK;
71
	else {
72
		r.Error(ACK_ERROR_NO_EXIST, "not subscribed to this channel");
73
		return CommandResult::ERROR;
74 75 76
	}
}

77
CommandResult
78
handle_channels(Client &client, gcc_unused Request args, Response &r)
79
{
80
	assert(args.IsEmpty());
81

82
	std::set<std::string> channels;
83
	for (const auto &c : *client.partition.instance.client_list)
84 85
		channels.insert(c.subscriptions.begin(),
				c.subscriptions.end());
86

87
	for (const auto &channel : channels)
88
		r.Format("channel: %s\n", channel.c_str());
89

90
	return CommandResult::OK;
91 92
}

93
CommandResult
94
handle_read_messages(Client &client,
95
		     gcc_unused Request args, Response &r)
96
{
97
	assert(args.IsEmpty());
98

99 100
	while (!client.messages.empty()) {
		const ClientMessage &msg = client.messages.front();
101

102 103
		r.Format("channel: %s\nmessage: %s\n",
			 msg.GetChannel(), msg.GetMessage());
104
		client.messages.pop_front();
105 106
	}

107
	return CommandResult::OK;
108 109
}

110
CommandResult
111
handle_send_message(Client &client, Request args, Response &r)
112
{
113
	assert(args.size == 2);
114

115 116 117 118
	const char *const channel_name = args[0];
	const char *const message_text = args[1];

	if (!client_message_valid_channel_name(channel_name)) {
119
		r.Error(ACK_ERROR_ARG, "invalid channel name");
120
		return CommandResult::ERROR;
121 122
	}

123
	bool sent = false;
124
	const ClientMessage msg(channel_name, message_text);
125 126
	for (auto &c : *client.partition.instance.client_list)
		if (c.PushMessage(msg))
127
			sent = true;
128

129
	if (sent)
130
		return CommandResult::OK;
131
	else {
132 133
		r.Error(ACK_ERROR_NO_EXIST,
			"nobody is subscribed to this channel");
134
		return CommandResult::ERROR;
135 136
	}
}