mpd/src/command/MessageCommands.cxx

135 lines
3.4 KiB
C++
Raw Normal View History

2012-08-25 12:59:54 +02:00
/*
2018-10-31 17:54:59 +01:00
* Copyright 2003-2018 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"
#include "Request.hxx"
2014-01-24 00:26:53 +01:00
#include "client/Client.hxx"
#include "client/ClientList.hxx"
#include "client/Response.hxx"
#include "Instance.hxx"
#include "util/ConstBuffer.hxx"
2012-08-25 12:59:54 +02:00
#include <set>
#include <string>
2012-08-25 12:59:54 +02:00
#include <assert.h>
CommandResult
handle_subscribe(Client &client, Request args, Response &r)
2012-08-25 12:59:54 +02:00
{
assert(args.size == 1);
const char *const channel_name = args[0];
2012-08-25 12:59:54 +02:00
switch (client.Subscribe(channel_name)) {
case Client::SubscribeResult::OK:
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
case Client::SubscribeResult::INVALID:
r.Error(ACK_ERROR_ARG, "invalid channel name");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
case Client::SubscribeResult::ALREADY:
r.Error(ACK_ERROR_EXIST, "already subscribed to this channel");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
case Client::SubscribeResult::FULL:
r.Error(ACK_ERROR_EXIST, "subscription list is full");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
/* unreachable */
assert(false);
gcc_unreachable();
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_unsubscribe(Client &client, Request args, Response &r)
2012-08-25 12:59:54 +02:00
{
assert(args.size == 1);
const char *const channel_name = args[0];
2012-08-25 12:59:54 +02:00
if (client.Unsubscribe(channel_name))
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
else {
r.Error(ACK_ERROR_NO_EXIST, "not subscribed to this channel");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
}
CommandResult
handle_channels(Client &client, gcc_unused Request args, Response &r)
2012-08-25 12:59:54 +02: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;
2017-02-25 10:00:05 +01:00
for (const auto &c : *client.GetInstance().client_list)
channels.insert(c.subscriptions.begin(),
c.subscriptions.end());
2012-08-25 12:59:54 +02:00
2013-01-16 22:55:33 +01:00
for (const auto &channel : channels)
r.Format("channel: %s\n", channel.c_str());
2012-08-25 12:59:54 +02:00
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}
CommandResult
2013-10-19 18:48:38 +02:00
handle_read_messages(Client &client,
gcc_unused Request args, Response &r)
2012-08-25 12:59:54 +02:00
{
assert(args.empty());
2012-08-25 12:59:54 +02:00
2013-10-19 18:48:38 +02:00
while (!client.messages.empty()) {
const ClientMessage &msg = client.messages.front();
2012-08-25 12:59:54 +02:00
r.Format("channel: %s\nmessage: %s\n",
msg.GetChannel(), msg.GetMessage());
2013-10-19 18:48:38 +02:00
client.messages.pop_front();
2012-08-25 12:59:54 +02:00
}
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
}
CommandResult
handle_send_message(Client &client, Request args, Response &r)
2012-08-25 12:59:54 +02:00
{
assert(args.size == 2);
2012-08-25 12:59:54 +02:00
const char *const channel_name = args[0];
const char *const message_text = args[1];
if (!client_message_valid_channel_name(channel_name)) {
r.Error(ACK_ERROR_ARG, "invalid channel name");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
2013-01-16 22:55:33 +01:00
bool sent = false;
const ClientMessage msg(channel_name, message_text);
2017-02-25 10:00:05 +01:00
for (auto &c : *client.GetInstance().client_list)
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)
return CommandResult::OK;
2012-08-25 12:59:54 +02:00
else {
r.Error(ACK_ERROR_NO_EXIST,
"nobody is subscribed to this channel");
return CommandResult::ERROR;
2012-08-25 12:59:54 +02:00
}
}