2012-08-25 12:59:54 +02:00
|
|
|
/*
|
2021-01-01 19:54:25 +01:00
|
|
|
* Copyright 2003-2021 The Music Player Daemon Project
|
2012-08-25 12:59:54 +02:00
|
|
|
* 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"
|
2015-08-11 22:11:28 +02:00
|
|
|
#include "Request.hxx"
|
2014-01-24 00:26:53 +01:00
|
|
|
#include "client/Client.hxx"
|
2019-04-03 20:59:00 +02:00
|
|
|
#include "client/List.hxx"
|
2015-08-06 22:10:25 +02:00
|
|
|
#include "client/Response.hxx"
|
2014-12-06 00:08:08 +01:00
|
|
|
#include "util/ConstBuffer.hxx"
|
2020-01-20 13:16:13 +01:00
|
|
|
#include "Partition.hxx"
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2021-05-21 20:35:29 +02:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2020-03-12 23:20:59 +01:00
|
|
|
#include <cassert>
|
2013-01-03 02:40:21 +01:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_subscribe(Client &client, Request args, Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
assert(args.size == 1);
|
|
|
|
const char *const channel_name = args[0];
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
switch (client.Subscribe(channel_name)) {
|
2013-10-19 19:44:45 +02:00
|
|
|
case Client::SubscribeResult::OK:
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
case Client::SubscribeResult::INVALID:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "invalid channel name");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
case Client::SubscribeResult::ALREADY:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_EXIST, "already subscribed to this channel");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-10-19 19:44:45 +02:00
|
|
|
case Client::SubscribeResult::FULL:
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_EXIST, "subscription list is full");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* unreachable */
|
2013-10-19 19:44:45 +02:00
|
|
|
assert(false);
|
|
|
|
gcc_unreachable();
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_unsubscribe(Client &client, Request args, Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
assert(args.size == 1);
|
|
|
|
const char *const channel_name = args[0];
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
if (client.Unsubscribe(channel_name))
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
else {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST, "not subscribed to this channel");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2020-03-12 20:56:11 +01:00
|
|
|
handle_channels(Client &client, [[maybe_unused]] Request args, Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2017-11-10 19:24:33 +01:00
|
|
|
assert(args.empty());
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-01-16 22:55:33 +01:00
|
|
|
std::set<std::string> channels;
|
2020-01-20 12:44:48 +01:00
|
|
|
|
2020-01-20 13:16:13 +01:00
|
|
|
for (const auto &c : client.GetPartition().clients) {
|
2019-04-03 20:02:59 +02:00
|
|
|
const auto &subscriptions = c.GetSubscriptions();
|
|
|
|
channels.insert(subscriptions.begin(),
|
|
|
|
subscriptions.end());
|
|
|
|
}
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-01-16 22:55:33 +01:00
|
|
|
for (const auto &channel : channels)
|
2021-05-21 20:35:29 +02:00
|
|
|
r.Fmt(FMT_STRING("channel: {}\n"), channel);
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2013-10-19 18:48:38 +02:00
|
|
|
handle_read_messages(Client &client,
|
2020-03-12 20:56:11 +01:00
|
|
|
[[maybe_unused]] Request args, Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2017-11-10 19:24:33 +01:00
|
|
|
assert(args.empty());
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2019-04-03 20:02:59 +02:00
|
|
|
client.ConsumeMessages([&r](const auto &msg){
|
2021-05-21 20:35:29 +02:00
|
|
|
r.Fmt(FMT_STRING("channel: {}\nmessage: {}\n"),
|
|
|
|
msg.GetChannel(), msg.GetMessage());
|
2019-04-03 20:02:59 +02:00
|
|
|
});
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 13:10:54 +02:00
|
|
|
CommandResult
|
2015-08-13 12:48:31 +02:00
|
|
|
handle_send_message(Client &client, Request args, Response &r)
|
2012-08-25 12:59:54 +02:00
|
|
|
{
|
2014-12-06 00:08:08 +01:00
|
|
|
assert(args.size == 2);
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2014-12-06 00:08:08 +01:00
|
|
|
const char *const channel_name = args[0];
|
|
|
|
const char *const message_text = args[1];
|
|
|
|
|
|
|
|
if (!client_message_valid_channel_name(channel_name)) {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_ARG, "invalid channel name");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
|
2013-01-16 22:55:33 +01:00
|
|
|
bool sent = false;
|
2014-12-06 00:08:08 +01:00
|
|
|
const ClientMessage msg(channel_name, message_text);
|
2020-01-20 12:44:48 +01:00
|
|
|
|
2020-01-20 13:16:13 +01:00
|
|
|
for (auto &c : client.GetPartition().clients)
|
|
|
|
if (c.PushMessage(msg))
|
2013-01-16 22:55:33 +01:00
|
|
|
sent = true;
|
2012-08-25 12:59:54 +02:00
|
|
|
|
2013-01-16 22:55:33 +01:00
|
|
|
if (sent)
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::OK;
|
2012-08-25 12:59:54 +02:00
|
|
|
else {
|
2015-08-06 22:10:25 +02:00
|
|
|
r.Error(ACK_ERROR_NO_EXIST,
|
|
|
|
"nobody is subscribed to this channel");
|
2013-10-20 13:10:54 +02:00
|
|
|
return CommandResult::ERROR;
|
2012-08-25 12:59:54 +02:00
|
|
|
}
|
|
|
|
}
|