2 libmpd for Haskell, an MPD client library.
3 Copyright (C) 2005-2008 Ben Sinclair <bsinclai@turing.une.edu.au>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 -- | Module : Network.MPD
21 -- Copyright : (c) Ben Sinclair 2005-2008
23 -- Maintainer : bsinclai@turing.une.edu.au
25 -- Portability : Haskell 98
27 -- An MPD client library. MPD is a daemon for playing music that is
28 -- controlled over a network socket. Its site is at <http://www.musicpd.org/>.
32 MPD
, MPDError
(..), ACKType
(..), Response
,
35 module Network
.MPD
.Commands
,
38 import Network
.MPD
.Commands
39 import Network
.MPD
.Core
40 import Network
.MPD
.SocketConn
42 import Control
.Monad
(liftM)
43 import Data
.Maybe (listToMaybe)
44 import Data
.IORef
(newIORef
, atomicModifyIORef
)
45 import System
.Environment
(getEnv)
47 import System
.IO.Error
(isDoesNotExistError, ioError)
49 -- | A wrapper for 'withMPDEx' that uses localhost:6600 as the default
50 -- host:port, or whatever is found in the environment variables MPD_HOST and
51 -- MPD_PORT. If MPD_HOST is of the form \"password\@host\" the password
52 -- will be supplied as well.
56 -- > withMPD $ play Nothing
57 -- > withMPD $ add_ "" "tool" >> play Nothing >> currentSong
58 withMPD
:: MPD a
-> IO (Response a
)
60 port
<- liftM read (getEnvDefault
"MPD_PORT" "6600")
61 (pw
,host
) <- liftM (break (== '@')) (getEnvDefault
"MPD_HOST" "localhost")
62 let (host
',pw
') = if null host
then (pw
,host
) else (drop 1 host
,pw
)
63 pwGen
<- mkPasswordGen
[pw
']
64 withMPDEx host
' port pwGen m
66 getEnvDefault x dflt
=
67 catch (getEnv x
) (\e
-> if isDoesNotExistError e
68 then return dflt
else ioError e
)
70 -- | Create an action that produces passwords for a connection. You
71 -- can pass these to 'withMPDEx' and it will use them to get passwords
72 -- to send to the server until one works or it runs out of them.
74 -- > do gen <- mkPasswordGen ["password1", "password2"]
75 -- > withMPDEx "localhost" 6600 gen (update [])
76 mkPasswordGen
:: [String] -> IO (IO (Maybe String))
77 mkPasswordGen
= liftM f
. newIORef
78 where f
= flip atomicModifyIORef
$ \xs
-> (drop 1 xs
, listToMaybe xs
)