2018-12-26 14:38:42 -05:00
|
|
|
{-
|
|
|
|
|
|
|
|
passman
|
|
|
|
Copyright (C) 2018 Jonathan Lamothe
|
|
|
|
<jlamothe1980@gmail.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this program. If not, see
|
|
|
|
<https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
2018-12-28 12:59:48 -05:00
|
|
|
module Util
|
|
|
|
( menu
|
|
|
|
, run
|
|
|
|
, withService
|
2018-12-31 15:27:36 -05:00
|
|
|
, ifServExists
|
2018-12-28 12:59:48 -05:00
|
|
|
, setService
|
|
|
|
, req
|
|
|
|
, tryReq
|
|
|
|
, confirm
|
2018-12-29 20:15:59 -05:00
|
|
|
, loadFrom
|
|
|
|
, save
|
2018-12-28 12:59:48 -05:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Lens (over, view)
|
2018-12-26 14:38:42 -05:00
|
|
|
import Control.Monad (join)
|
|
|
|
import Control.Monad.Trans.Class (lift)
|
|
|
|
import qualified Control.Monad.Trans.State as S
|
2018-12-29 20:15:59 -05:00
|
|
|
import Data.Aeson (decodeFileStrict, encodeFile)
|
2018-12-26 14:38:42 -05:00
|
|
|
import Data.Maybe (fromJust)
|
|
|
|
import System.Console.HCL
|
|
|
|
( Request
|
|
|
|
, prompt
|
|
|
|
, reqAgree
|
2018-12-28 14:37:50 -05:00
|
|
|
, reqChar
|
2018-12-30 15:27:01 -05:00
|
|
|
, reqDefault
|
2018-12-26 14:38:42 -05:00
|
|
|
, reqIf
|
2018-12-29 20:15:59 -05:00
|
|
|
, reqIO
|
2018-12-26 14:38:42 -05:00
|
|
|
, reqMenu
|
|
|
|
, required
|
|
|
|
, runRequest
|
|
|
|
)
|
|
|
|
|
2018-12-27 15:56:25 -05:00
|
|
|
import Password
|
|
|
|
|
2018-12-26 14:38:42 -05:00
|
|
|
import Types
|
|
|
|
|
|
|
|
menu
|
|
|
|
:: String
|
|
|
|
-> [(String, S.StateT Status IO a)]
|
|
|
|
-> S.StateT Status IO a
|
|
|
|
menu title = reqState . prompt ("\n*** " ++ title ++ " ***") .
|
|
|
|
reqMenu . map menuItem
|
|
|
|
|
|
|
|
menuItem :: (String, a) -> (String, Request a)
|
|
|
|
menuItem (str, x) = (str, return x)
|
|
|
|
|
|
|
|
reqState :: Request (S.StateT s IO a) -> S.StateT s IO a
|
|
|
|
reqState = join . req
|
|
|
|
|
2018-12-26 15:43:13 -05:00
|
|
|
run :: Monad m => (s -> (a, s)) -> S.StateT s m a
|
|
|
|
run f = S.StateT $ return . f
|
|
|
|
|
2018-12-27 15:56:25 -05:00
|
|
|
withService
|
|
|
|
:: String
|
|
|
|
-> S.StateT Status IO a
|
|
|
|
-> (PWData -> S.StateT Status IO a)
|
|
|
|
-> S.StateT Status IO a
|
|
|
|
withService srv fb act = do
|
|
|
|
db <- S.gets $ view database
|
|
|
|
case pwGetService srv db of
|
|
|
|
Nothing -> fb
|
|
|
|
Just x -> act x
|
|
|
|
|
2018-12-31 15:27:36 -05:00
|
|
|
ifServExists
|
|
|
|
:: String
|
|
|
|
-> S.StateT Status IO a
|
|
|
|
-> S.StateT Status IO a
|
|
|
|
-> S.StateT Status IO a
|
|
|
|
ifServExists s x y = do
|
|
|
|
db <- S.gets $ view database
|
|
|
|
if pwHasService s db
|
|
|
|
then x
|
|
|
|
else y
|
|
|
|
|
2018-12-28 12:59:48 -05:00
|
|
|
setService :: String -> PWData -> S.StateT Status IO ()
|
|
|
|
setService k = S.modify . over database . pwSetService k
|
|
|
|
|
2018-12-26 14:38:42 -05:00
|
|
|
req :: Request a -> S.StateT s IO a
|
|
|
|
req = lift . fmap fromJust . runRequest . required
|
|
|
|
|
|
|
|
tryReq :: Request a -> S.StateT s IO (Maybe a)
|
|
|
|
tryReq = lift . runRequest
|
|
|
|
|
|
|
|
confirm :: String -> Request Bool
|
2018-12-28 14:37:50 -05:00
|
|
|
confirm x = prompt (x ++ " (y/n): ") $ reqAgree Nothing $ fmap return reqChar
|
2018-12-26 14:38:42 -05:00
|
|
|
|
2018-12-29 20:15:59 -05:00
|
|
|
loadFrom :: FilePath -> Request PWDatabase
|
2018-12-30 15:27:01 -05:00
|
|
|
loadFrom path = reqDefault
|
|
|
|
(reqIO (decodeFileStrict path))
|
|
|
|
(Just newPWDatabase) >>= maybe
|
2018-12-29 20:15:59 -05:00
|
|
|
(return newPWDatabase)
|
|
|
|
return
|
|
|
|
|
|
|
|
save :: S.StateT Status IO ()
|
|
|
|
save = do
|
|
|
|
path <- S.gets $ view dbPath
|
|
|
|
db <- S.gets $ view database
|
|
|
|
lift $ encodeFile path db
|
|
|
|
|
2018-12-26 14:38:42 -05:00
|
|
|
--jl
|