diff options
author | evuez <julien@mulga.net> | 2024-04-01 15:17:30 +0200 |
---|---|---|
committer | evuez <julien@mulga.net> | 2024-04-03 22:45:36 +0200 |
commit | 985974c264804ff788b3b5242fef707d4b7fa9a6 (patch) | |
tree | d80f83db178c3fd1b83b3b749793d47236dde35d /src/Mail/Header.hs | |
download | webmaild-985974c264804ff788b3b5242fef707d4b7fa9a6.tar.gz |
Initial commit
Diffstat (limited to 'src/Mail/Header.hs')
-rw-r--r-- | src/Mail/Header.hs | 45 |
1 files changed, 45 insertions, 0 deletions
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 == ' ') |