{-# LANGUAGE OverloadedStrings #-}
module Network.MPD.Applicative.ClientToClient
(
ChannelName
, MessageText
, subscribe
, unsubscribe
, channels
, readMessages
, sendMessage
) where
import Network.MPD.Commands.Arg hiding (Command)
import Network.MPD.Applicative.Internal
import Network.MPD.Applicative.Util
import Network.MPD.Util
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.UTF8 as UTF8
type ChannelName = String
type MessageText = String
subscribe :: ChannelName -> Command ()
subscribe :: [Char] -> Command ()
subscribe [Char]
name = Parser () -> [[Char]] -> Command ()
forall a. Parser a -> [[Char]] -> Command a
Command Parser ()
emptyResponse [Command
"subscribe" Command -> [Char] -> [Char]
forall a. MPDArg a => Command -> a -> [Char]
<@> [Char]
name]
unsubscribe :: ChannelName -> Command ()
unsubscribe :: [Char] -> Command ()
unsubscribe [Char]
name = Parser () -> [[Char]] -> Command ()
forall a. Parser a -> [[Char]] -> Command a
Command Parser ()
emptyResponse [Command
"unsubscribe" Command -> [Char] -> [Char]
forall a. MPDArg a => Command -> a -> [Char]
<@> [Char]
name]
channels :: Command [ChannelName]
channels :: Command [[Char]]
channels = Parser [[Char]] -> [[Char]] -> Command [[Char]]
forall a. Parser a -> [[Char]] -> Command a
Command Parser [[Char]]
p [[Char]
"channels"]
where
p :: Parser [[Char]]
p = (ByteString -> [Char]) -> [ByteString] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> [Char]
UTF8.toString ([ByteString] -> [[Char]])
-> ([ByteString] -> [ByteString]) -> [ByteString] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> [ByteString]
takeValues ([ByteString] -> [[Char]])
-> Parser [ByteString] -> Parser [[Char]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser [ByteString]
getResponse
readMessages :: Command [(ChannelName, MessageText)]
readMessages :: Command [([Char], [Char])]
readMessages = Parser [([Char], [Char])] -> [[Char]] -> Command [([Char], [Char])]
forall a. Parser a -> [[Char]] -> Command a
Command (([ByteString] -> Either [Char] [([Char], [Char])])
-> Parser [([Char], [Char])]
forall a. ([ByteString] -> Either [Char] a) -> Parser a
liftParser [ByteString] -> Either [Char] [([Char], [Char])]
p) [[Char]
"readmessages"]
where
p :: [ByteString] -> Either [Char] [([Char], [Char])]
p = ([(ByteString, ByteString)] -> Either [Char] ([Char], [Char]))
-> [[(ByteString, ByteString)]] -> Either [Char] [([Char], [Char])]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM [(ByteString, ByteString)] -> Either [Char] ([Char], [Char])
parseMessage ([[(ByteString, ByteString)]] -> Either [Char] [([Char], [Char])])
-> ([ByteString] -> [[(ByteString, ByteString)]])
-> [ByteString]
-> Either [Char] [([Char], [Char])]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString]
-> [(ByteString, ByteString)] -> [[(ByteString, ByteString)]]
splitGroups [ByteString
"channel"] ([(ByteString, ByteString)] -> [[(ByteString, ByteString)]])
-> ([ByteString] -> [(ByteString, ByteString)])
-> [ByteString]
-> [[(ByteString, ByteString)]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [ByteString] -> [(ByteString, ByteString)]
toAssocList
parseMessage :: [(ByteString, ByteString)] -> Either String (ChannelName, MessageText)
parseMessage :: [(ByteString, ByteString)] -> Either [Char] ([Char], [Char])
parseMessage [(ByteString
"channel", ByteString
ch),(ByteString
"message", ByteString
msg)] = ([Char], [Char]) -> Either [Char] ([Char], [Char])
forall a b. b -> Either a b
Right (ByteString -> [Char]
UTF8.toString ByteString
ch, ByteString -> [Char]
UTF8.toString ByteString
msg)
parseMessage [(ByteString, ByteString)]
_ = [Char] -> Either [Char] ([Char], [Char])
forall a b. a -> Either a b
Left [Char]
"Unexpected result from readMessages"
sendMessage :: ChannelName -> MessageText -> Command ()
sendMessage :: [Char] -> [Char] -> Command ()
sendMessage [Char]
name [Char]
text = Parser () -> [[Char]] -> Command ()
forall a. Parser a -> [[Char]] -> Command a
Command Parser ()
emptyResponse [Command
"sendmessage" Command -> Args -> [Char]
forall a. MPDArg a => Command -> a -> [Char]
<@> [Char]
name [Char] -> [Char] -> Args
forall a b. (MPDArg a, MPDArg b) => a -> b -> Args
<++> [Char]
text]