aboutsummaryrefslogtreecommitdiff
path: root/src/Mail/Header.hs
blob: e7265b2fc43d8dde0db34bfa9f11b03fcefabead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 == ' ')