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"
|
2014-10-16 23:21:03 +02:00
|
|
|
_VERSION = "0.2.0"
|
2014-10-13 21:26:55 +02:00
|
|
|
_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
|
|
|
|
|
2014-10-16 23:21:03 +02:00
|
|
|
-- Assign default values to unspecified args
|
2014-10-14 19:14:44 +02:00
|
|
|
defaults :: Args -> IO Args
|
|
|
|
defaults args@Args{..} = do
|
2014-10-16 23:21:03 +02:00
|
|
|
defaultDict <- getDataFileName "dict/diceware" >>= readFile
|
|
|
|
dict <- readFile dictionary
|
2014-10-14 19:14:44 +02:00
|
|
|
return args
|
2014-10-16 23:21:03 +02:00
|
|
|
{ dictionary = if null dict then defaultDict else dict
|
2014-10-14 19:14:44 +02:00
|
|
|
, phraseLength = if phraseLength == 0 then 6 else phraseLength
|
|
|
|
}
|
2014-10-13 21:26:55 +02:00
|
|
|
|
2014-10-16 23:21:03 +02:00
|
|
|
-- Main function
|
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
|
2014-10-16 23:21:03 +02:00
|
|
|
dictSize = length $ parseDiceware dictionary
|