packages feed

WL500gPControl-0.3.1: src/Common.hs

module Common 
    (
     defaultCredsFile
    ,parseCredsFile
    )where

import System.IO.Unsafe (unsafePerformIO)
import System.Environment (getEnv)
import Text.Printf (printf)
import qualified Network.Asus.WL500gP as W
import Data.Char (toLower)
import Control.Monad (liftM)
    
homeDirectory = unsafePerformIO $ getEnv "HOME"

defaultCredsFile = printf "%s/.wl500gp" homeDirectory :: String


parseCredsFile :: String -> IO (Maybe W.Connection)
parseCredsFile fname = 
    let toPair (a:b:[]) = (map toLower a, b)
        toPair _ = ("", "")
        parse = filter (/= ("", "")) . map (toPair . words) . lines
    in do
      contentsMap <- liftM parse $ readFile fname
      return $ do (host:passwd:user:[]) <- 
                      mapM (`lookup` contentsMap) ["host:"
                                                  ,"password:"
                                                  ,"user:"]
                  return $ W.Connection {W.user = user
                                        ,W.password = passwd
                                        ,W.hostname = host}