MessageCommands.cxx 3.45 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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 23
#include "client/Client.hxx"
#include "client/ClientList.hxx"
24
#include "Instance.hxx"
25
#include "Partition.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "protocol/Result.hxx"
27

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

31 32
#include <assert.h>

33
CommandResult
34
handle_subscribe(Client &client, gcc_unused unsigned argc, char *argv[])
35 36 37
{
	assert(argc == 2);

38 39
	switch (client.Subscribe(argv[1])) {
	case Client::SubscribeResult::OK:
40
		return CommandResult::OK;
41

42
	case Client::SubscribeResult::INVALID:
43 44
		command_error(client, ACK_ERROR_ARG,
			      "invalid channel name");
45
		return CommandResult::ERROR;
46

47
	case Client::SubscribeResult::ALREADY:
48 49
		command_error(client, ACK_ERROR_EXIST,
			      "already subscribed to this channel");
50
		return CommandResult::ERROR;
51

52
	case Client::SubscribeResult::FULL:
53 54
		command_error(client, 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, gcc_unused unsigned argc, char *argv[])
65 66 67
{
	assert(argc == 2);

68
	if (client.Unsubscribe(argv[1]))
69
		return CommandResult::OK;
70 71 72
	else {
		command_error(client, ACK_ERROR_NO_EXIST,
			      "not subscribed to this channel");
73
		return CommandResult::ERROR;
74 75 76
	}
}

77
CommandResult
78
handle_channels(Client &client,
79
		gcc_unused unsigned argc, gcc_unused char *argv[])
80 81 82
{
	assert(argc == 1);

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

88
	for (const auto &channel : channels)
89
		client_printf(client, "channel: %s\n", channel.c_str());
90

91
	return CommandResult::OK;
92 93
}

94
CommandResult
95
handle_read_messages(Client &client,
96
		     gcc_unused unsigned argc, gcc_unused char *argv[])
97 98 99
{
	assert(argc == 1);

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

		client_printf(client, "channel: %s\nmessage: %s\n",
104
			      msg.GetChannel(), msg.GetMessage());
105
		client.messages.pop_front();
106 107
	}

108
	return CommandResult::OK;
109 110
}

111
CommandResult
112
handle_send_message(Client &client,
113
		    gcc_unused unsigned argc, gcc_unused char *argv[])
114 115 116 117 118 119
{
	assert(argc == 3);

	if (!client_message_valid_channel_name(argv[1])) {
		command_error(client, ACK_ERROR_ARG,
			      "invalid channel name");
120
		return CommandResult::ERROR;
121 122
	}

123 124
	bool sent = false;
	const ClientMessage msg(argv[1], argv[2]);
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 132 133
	else {
		command_error(client, ACK_ERROR_NO_EXIST,
			      "nobody is subscribed to this channel");
134
		return CommandResult::ERROR;
135 136
	}
}