module Autoproc.Procmail where
import Data.List (sort)
data PExp = PExp [RecipeFlag] [Condition] Action
data RecipeFlag = CheckHeader | CheckBody
| CaseSensitive | Chain
| ElseIf | PipeAsFilter | Copy
| Wait | IgnoreErrors | RawWrite | NeedLock Bool deriving (Eq, Ord)
data Condition = Condition ConditionFlag String
data ConditionFlag = Normal | Invert | Eval | UseExitCode
| LessThan | GreaterThan | Var String
data Action = Forward [String] | Pipe String | File String
| Nest [PExp]
showLn :: (Show a) => a -> String
showLn = (++ "\n") . show
instance Show PExp where
show (PExp fs cs a) = ":0"++showFlags fs++"\n"
++(cs >>= (\x -> if show x == "" then ""
else showLn x))
++show a++"\n"
where
showFlags [NeedLock b] = show (NeedLock b)
showFlags hs = " " ++ ((show =<<) . sort) hs
instance Show RecipeFlag where
show CheckHeader = "H"
show CheckBody = "B"
show CaseSensitive = "D"
show Chain = "A"
show ElseIf = "E"
show PipeAsFilter = "f"
show Copy = "c"
show Wait = "w"
show IgnoreErrors = "i"
show RawWrite = "r"
show (NeedLock True) = ":"
show (NeedLock False) = ""
instance Show Condition where
show (Condition _ []) = ""
show (Condition cf s) = "* " ++ show cf ++ s
instance Show ConditionFlag where
show Normal = ""
show Invert = "!"
show Eval = "$"
show UseExitCode = "?"
show LessThan = "<"
show GreaterThan = ">"
show (Var s) = s++" ?? "
instance Show Action where
show (Nest es) = "{ \n"++(es >>= showLn)++"}"
show (File s) = s
show (Forward es) = "! "++(es >>= (++ " "))
show (Pipe s) = "| "++s