MessageCommands.cxx 3.47 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2013 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"
Max Kellermann's avatar
Max Kellermann committed
22 23
#include "ClientSubscribe.hxx"
#include "ClientInternal.hxx"
24
#include "ClientList.hxx"
25
#include "Instance.hxx"
26
#include "Main.hxx"
Max Kellermann's avatar
Max Kellermann committed
27 28
#include "protocol/Result.hxx"
#include "protocol/ArgParser.hxx"
29

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

33 34 35
#include <assert.h>

enum command_return
36
handle_subscribe(Client *client, gcc_unused int argc, char *argv[])
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
{
	assert(argc == 2);

	switch (client_subscribe(client, argv[1])) {
	case CLIENT_SUBSCRIBE_OK:
		return COMMAND_RETURN_OK;

	case CLIENT_SUBSCRIBE_INVALID:
		command_error(client, ACK_ERROR_ARG,
			      "invalid channel name");
		return COMMAND_RETURN_ERROR;

	case CLIENT_SUBSCRIBE_ALREADY:
		command_error(client, ACK_ERROR_EXIST,
			      "already subscribed to this channel");
		return COMMAND_RETURN_ERROR;

	case CLIENT_SUBSCRIBE_FULL:
		command_error(client, ACK_ERROR_EXIST,
			      "subscription list is full");
		return COMMAND_RETURN_ERROR;
	}

	/* unreachable */
	return COMMAND_RETURN_OK;
}

enum command_return
65
handle_unsubscribe(Client *client, gcc_unused int argc, char *argv[])
66 67 68 69 70 71 72 73 74 75 76 77 78
{
	assert(argc == 2);

	if (client_unsubscribe(client, argv[1]))
		return COMMAND_RETURN_OK;
	else {
		command_error(client, ACK_ERROR_NO_EXIST,
			      "not subscribed to this channel");
		return COMMAND_RETURN_ERROR;
	}
}

enum command_return
79
handle_channels(Client *client,
80
		gcc_unused int argc, gcc_unused char *argv[])
81 82 83
{
	assert(argc == 1);

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

89
	for (const auto &channel : channels)
90
		client_printf(client, "channel: %s\n", channel.c_str());
91 92 93 94 95

	return COMMAND_RETURN_OK;
}

enum command_return
96
handle_read_messages(Client *client,
97
		     gcc_unused int argc, gcc_unused char *argv[])
98 99 100
{
	assert(argc == 1);

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

		client_printf(client, "channel: %s\nmessage: %s\n",
105 106
			      msg.GetChannel(), msg.GetMessage());
		client->messages.pop_front();
107 108 109 110 111 112
	}

	return COMMAND_RETURN_OK;
}

enum command_return
113
handle_send_message(Client *client,
114
		    gcc_unused int argc, gcc_unused char *argv[])
115 116 117 118 119 120 121 122 123
{
	assert(argc == 3);

	if (!client_message_valid_channel_name(argv[1])) {
		command_error(client, ACK_ERROR_ARG,
			      "invalid channel name");
		return COMMAND_RETURN_ERROR;
	}

124 125
	bool sent = false;
	const ClientMessage msg(argv[1], argv[2]);
126
	for (const auto &c : *instance->client_list)
127 128
		if (client_push_message(c, msg))
			sent = true;
129

130
	if (sent)
131 132 133 134 135 136 137
		return COMMAND_RETURN_OK;
	else {
		command_error(client, ACK_ERROR_NO_EXIST,
			      "nobody is subscribed to this channel");
		return COMMAND_RETURN_ERROR;
	}
}