URI.hs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. {-# LANGUAGE OverloadedStrings #-}
  2. module Data.SMTP.Types.URI where
  3. import qualified Network.URI as N
  4. import Data.SMTP.Account
  5. import Data.List
  6. import Text.StringConvert
  7. newtype Path = Path [String] deriving (Eq, Ord, Read, Show)
  8. newtype Revision = Revision String deriving (Eq, Ord, Read, Show)
  9. data Parameter = Parameter String String deriving (Eq, Ord, Read, Show)
  10. data URI = URI {account :: Account, path :: Path, revision :: Maybe Revision, parameters :: [Parameter]}
  11. deriving (Eq, Ord, Read)
  12. fullPath :: URI -> String
  13. fullPath URI{path=Path p} = intercalate "/" $ map uriEncode p
  14. fullURI :: URI -> String
  15. fullURI u@(URI{account=a, revision=r, parameters=pp}) =
  16. concat $ ["FCMTP://", s . fullAccount $ a, fullPath u] ++ (
  17. case r of
  18. Nothing -> []
  19. Just (Revision r') -> [":", r']
  20. ) ++ (
  21. if null pp
  22. then []
  23. else ["?", intercalate "&" $ map formatParameter pp]
  24. )
  25. where
  26. formatParameter (Parameter nm vl) = concat [uriEncode nm, "=", uriEncode vl]
  27. uriEncode = N.escapeURIString N.isUnescapedInURIComponent
  28. instance Show URI where
  29. show = fullURI