Account.hs 1.2 KB

12345678910111213141516171819202122232425262728293031
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Data.SMTP.Types.Account (Account(..), PersonalName(..), AccountName(..), HostName(..), AccountTag(..), normalize) where
  3. import Data.ByteString (ByteString)
  4. import qualified Data.ByteString as BS
  5. import Data.Default.Class
  6. newtype PersonalName = PersonalName ByteString deriving (Show, Read, Eq, Ord)
  7. newtype HostName = HostName ByteString deriving (Show, Read, Eq, Ord)
  8. newtype AccountTag = AccountTag ByteString deriving (Show, Read, Eq, Ord)
  9. newtype AccountName = AccountName ByteString deriving (Show, Read, Eq, Ord)
  10. data Account = Account {fullAccount :: ByteString, personalName :: PersonalName,
  11. name :: AccountName, domain :: HostName,
  12. tags :: [AccountTag]} deriving (Show, Read)
  13. instance Eq Account where
  14. a == b = name a == name b && domain a == domain b
  15. instance Ord Account where
  16. compare a b = case compare (name a) (name b) of
  17. LT -> LT
  18. GT -> GT
  19. EQ -> compare (domain a) (domain b)
  20. instance Default Account where
  21. def = Account "<>" (PersonalName "") (AccountName "") (HostName "") []
  22. normalize :: Account -> ByteString
  23. normalize a = let
  24. AccountName an = name a
  25. HostName hn = domain a
  26. in if BS.null hn then an else BS.concat [an, "@", hn]