------------------------------------------------------------
-- |
-- Module      : Text.Crack
-- Copyright   : (c) 2008. Trevor Elliott
-- License     : GPL V2
--
-- Maintainer  : trevor@geekgateway.com
-- Stability   :
-- Portability :
--
-- Haskell binding to cracklib

module Text.Crack (
    crack
  ) where

-- Friends
import Text.Crack.Base

-- Libraries
import Control.Applicative
import Foreign
import Foreign.C.String
import Foreign.Ptr

-- | Check a password
-- Returns Right () if the password is good, or Left String where the string
-- is a message explaining the problems with the password.
crack :: String -- ^ Password
      -> String -- ^ Dictionary location (i.e. /usr/share/cracklib/pw_dict)
      -> Either String ()
crack pw dict = unsafePerformIO $
  withCString pw $ \c_pw -> withCString dict $ \c_dict -> do
  p <- c_fascist_check c_pw c_dict
  if p == nullPtr
    then return (Right ())
    else Left <$> peekCString p