URI.hs 1.3 KB

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