alea/main.hs

65 lines
1.8 KiB
Haskell
Raw Normal View History

2014-10-13 21:26:55 +02:00
{-# LANGUAGE DeriveDataTypeable, RecordWildCards #-}
import System.IO
import System.Console.CmdArgs
2014-10-14 19:14:44 +02:00
import Control.Monad (when)
2014-10-13 21:26:55 +02:00
2014-10-14 00:40:42 +02:00
import Paths_alea (getDataFileName)
2014-10-14 19:14:44 +02:00
import Alea.Diceware
import Alea.Random
2014-10-13 21:26:55 +02:00
_NAME = "Alea"
_VERSION = "0.1.0"
_INFO = _NAME ++ " version " ++ _VERSION
_ABOUT = "a diceware passphrase generator"
2014-10-14 00:43:24 +02:00
_COPYRIGHT = "(C) Michele Guerini Rocco 2014"
2014-10-13 21:26:55 +02:00
data Args = Args
{ interactive :: Bool
, dictionary :: FilePath
, phraseLength :: Int
, phrases :: Int
} deriving (Data, Typeable, Show, Eq)
progArgs :: Args
progArgs = Args
{ interactive = def &= help "Manually insert numbers"
, dictionary = def &= help "Specify dictionary file path"
, phraseLength = def &= help "Number of words in a passphrase"
, phrases = def &= help "Number of passphrases to generate"
}
getProgArgs :: IO Args
getProgArgs = cmdArgs $ progArgs
&= versionArg [explicit, name "version", name "v", summary _INFO]
&= summary (_INFO ++ ", " ++ _COPYRIGHT)
&= help _ABOUT
&= helpArg [explicit, name "help", name "h"]
&= program _NAME
main :: IO ()
2014-10-14 19:14:44 +02:00
main = getProgArgs >>= defaults >>= exec
-- Assign defaults value to unspecified args
defaults :: Args -> IO Args
defaults args@Args{..} = do
dictionary <- getDataFileName "dict/diceware" >>= readFile
return args
{ dictionary = dictionary
, phraseLength = if phraseLength == 0 then 6 else phraseLength
}
2014-10-13 21:26:55 +02:00
exec :: Args -> IO ()
2014-10-14 19:14:44 +02:00
exec args@Args{..} =
2014-10-13 21:26:55 +02:00
if interactive
2014-10-14 19:14:44 +02:00
then interact (unlines . map dice . lines)
2014-10-13 21:26:55 +02:00
else do
2014-10-14 19:14:44 +02:00
randWords dictSize phraseLength >>= putStrLn . unwords . map dice'
when (phrases > 1) $ exec args {phrases = phrases - 1}
2014-10-13 21:26:55 +02:00
where
-- helpers
2014-10-14 19:14:44 +02:00
dice n = readDiceware (parseDiceware dictionary) (read n :: Int)
dice' n = readDiceware' (parseDiceware dictionary) n
dictSize = size $ parseDiceware dictionary