2 libmpd for Haskell, an MPD client library.
3 Copyright (C) 2005-2007 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-2007
23 -- Maintainer : bsinclai@turing.une.edu.au
25 -- Portability : Haskell 98
27 -- MPD client library.
31 MPD
, MPDError
(..), ACKType
(..), Response
,
34 module Network
.MPD
.Commands
,
37 import Network
.MPD
.Commands
38 import Network
.MPD
.Core
39 import Network
.MPD
.SocketConn
41 import Control
.Monad
(liftM)
42 import Data
.Maybe (listToMaybe)
43 import Data
.IORef
(newIORef
, atomicModifyIORef
)
44 import System
.Environment
(getEnv)
46 import System
.IO.Error
(isDoesNotExistError, ioError)
48 -- | A wrapper for 'withMPDEx' that uses localhost:6600 as the default
49 -- host:port, or whatever is found in the environment variables MPD_HOST and
50 -- MPD_PORT. If MPD_HOST is of the form \"password\@host\" the password
51 -- will be supplied as well.
55 -- > withMPD $ play Nothing
56 -- > withMPD $ add_ Nothing "tool" >> play Nothing >> currentSong
57 withMPD
:: MPD a
-> IO (Response a
)
59 port
<- liftM read (getEnvDefault
"MPD_PORT" "6600")
60 (pw
,host
) <- liftM (break (== '@')) (getEnvDefault
"MPD_HOST" "localhost")
61 let (host
',pw
') = if null host
then (pw
,host
) else (drop 1 host
,pw
)
62 pwGen
<- mkPasswordGen
[pw
']
63 withMPDEx host
' port pwGen m
65 getEnvDefault x dflt
=
66 catch (getEnv x
) (\e
-> if isDoesNotExistError e
67 then return dflt
else ioError e
)
69 -- | Create an action that produces passwords for a connection. You
70 -- can pass these to 'withMPDEx' and it will use them to get passwords
71 -- to send to the server until one works or it runs out of them.
73 -- > do gen <- mkPasswordGen ["password1", "password2"]
74 -- > withMPDEx "localhost" 6600 gen (update [])
75 mkPasswordGen
:: [String] -> IO (IO (Maybe String))
76 mkPasswordGen
= liftM f
. newIORef
77 where f
= flip atomicModifyIORef
$ \xs
-> (drop 1 xs
, listToMaybe xs
)