[gitconv @ Prim.MPDError: redundant parens in deriving clause.]
[libmpd-haskell.git] / Network / MPD.hs
blob096e9450ab5f2d20fe78341bcbf43ff8fa8f99a2
1 {-
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
22 -- License : LGPL
23 -- Maintainer : bsinclai@turing.une.edu.au
24 -- Stability : alpha
25 -- Portability : Haskell 98
27 -- MPD client library.
29 module Network.MPD (
30 -- * Basic data types
31 MPD, MPDError(..), ACKType(..), Response,
32 -- * Connections
33 withMPD, withMPDEx,
34 module Network.MPD.Commands,
35 ) where
37 import Network.MPD.Commands
38 import Network.MPD.Prim
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)
45 import System.IO
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.
53 -- Examples:
55 -- > withMPD $ play Nothing
56 -- > withMPD $ add_ Nothing "tool" >> play Nothing >> currentSong
57 withMPD :: MPD a -> IO (Response a)
58 withMPD m = do
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
64 where
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)