From 985974c264804ff788b3b5242fef707d4b7fa9a6 Mon Sep 17 00:00:00 2001 From: evuez Date: Mon, 1 Apr 2024 15:17:30 +0200 Subject: Initial commit --- src/Mail/Header.hs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/Mail/Header.hs (limited to 'src/Mail/Header.hs') diff --git a/src/Mail/Header.hs b/src/Mail/Header.hs new file mode 100644 index 0000000..e7265b2 --- /dev/null +++ b/src/Mail/Header.hs @@ -0,0 +1,45 @@ +module Mail.Header + ( ContentType (..) + , Header + , contentType + , contentTransferEncoding + ) +where + +import Common (findVal, split2, splitOn, toLower, trim, trimWith) +import Intro + +type Header = (String, String) + +data ContentType = ContentType + { mime :: String + , boundary :: Maybe String + , charset :: Maybe String + } + deriving (Show) + +newContentType :: String -> ContentType +newContentType m = ContentType{mime = m, boundary = Nothing, charset = Nothing} + +contentType :: [Header] -> Maybe ContentType +contentType xs = do + v <- findValIns "content-type" xs + case (fmap trim . splitOn (== ';')) v of + [y] -> pure (newContentType y) + y : ys -> + let kv = fmap (split2 (== '=')) ys + in pure + $ (newContentType y) + { boundary = trimQuotes <$> findValIns "boundary" kv + , charset = findValIns "charset" kv + } + [] -> Nothing + +contentTransferEncoding :: [Header] -> Maybe String +contentTransferEncoding = findValIns "content-transfer-encoding" + +findValIns :: String -> [Header] -> Maybe String +findValIns k = findVal ((== k) . toLower) + +trimQuotes :: String -> String +trimQuotes = trimWith (\x -> x == '"' || x == ' ') -- cgit v1.2.3