MessageCommands.cxx 3.39 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 20
 * 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 "MessageCommands.hxx"
21
#include "Request.hxx"
22
#include "client/Client.hxx"
23
#include "client/List.hxx"
24
#include "client/Response.hxx"
25
#include "util/ConstBuffer.hxx"
26
#include "Partition.hxx"
27

28
#include <cassert>
29 30 31
#include <set>
#include <string>

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

38
	switch (client.Subscribe(channel_name)) {
39
	case Client::SubscribeResult::OK:
40
		return CommandResult::OK;
41

42
	case Client::SubscribeResult::INVALID:
43
		r.Error(ACK_ERROR_ARG, "invalid channel name");
44
		return CommandResult::ERROR;
45

46
	case Client::SubscribeResult::ALREADY:
47
		r.Error(ACK_ERROR_EXIST, "already subscribed to this channel");
48
		return CommandResult::ERROR;
49

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

	/* unreachable */
56 57
	assert(false);
	gcc_unreachable();
58 59
}

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

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

74
CommandResult
Rosen Penev's avatar
Rosen Penev committed
75
handle_channels(Client &client, [[maybe_unused]] Request args, Response &r)
76
{
77
	assert(args.empty());
78

79
	std::set<std::string> channels;
80

81
	for (const auto &c : client.GetPartition().clients) {
82 83 84 85
		const auto &subscriptions = c.GetSubscriptions();
		channels.insert(subscriptions.begin(),
				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,
Rosen Penev's avatar
Rosen Penev committed
95
		     [[maybe_unused]] Request args, Response &r)
96
{
97
	assert(args.empty());
98

99
	client.ConsumeMessages([&r](const auto &msg){
100 101
		r.Format("channel: %s\nmessage: %s\n",
			 msg.GetChannel(), msg.GetMessage());
102
	});
103

104
	return CommandResult::OK;
105 106
}

107
CommandResult
108
handle_send_message(Client &client, Request args, Response &r)
109
{
110
	assert(args.size == 2);
111

112 113 114 115
	const char *const channel_name = args[0];
	const char *const message_text = args[1];

	if (!client_message_valid_channel_name(channel_name)) {
116
		r.Error(ACK_ERROR_ARG, "invalid channel name");
117
		return CommandResult::ERROR;
118 119
	}

120
	bool sent = false;
121
	const ClientMessage msg(channel_name, message_text);
122

123 124
	for (auto &c : client.GetPartition().clients)
		if (c.PushMessage(msg))
125
			sent = true;
126

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