module Codec.MIME.Decode where
import Data.Char
import Codec.MIME.QuotedPrintable as QP
import Codec.MIME.Base64 as Base64
decodeBody :: String -> String -> String
decodeBody enc body =
case map toLower enc of
"base64" -> Base64.decodeToString body
"quoted-printable" -> QP.decode body
_ -> body
decodeWord :: String -> Maybe (String, String)
decodeWord str =
case str of
'=':'?':xs ->
case dropLang $ break (\ch -> ch =='?' || ch == '*') xs of
(cs,_:x:'?':bs)
| isKnownCharset (map toLower cs) ->
case toLower x of
'q' -> decodeQ cs (break (=='?') bs)
'b' -> decodeB cs (break (=='?') bs)
_ -> Nothing
_ -> Nothing
_ -> Nothing
where
isKnownCharset cs = cs `elem` ["iso-8859-1", "us-ascii"]
dropLang (as,'*':bs) = (as,dropWhile (/='?') bs)
dropLang (as,bs) = (as,bs)
decodeQ cset (fs,'?':'=':rs) = Just (fromCharset cset (QP.decode fs),rs)
decodeQ _ _ = Nothing
decodeB cset (fs,'?':'=':rs) =
Just (fromCharset cset (Base64.decodeToString fs),rs)
decodeB _ _ = Nothing
fromCharset _cset cs = cs
decodeWords :: String -> String
decodeWords "" = ""
decodeWords (x:xs)
| isSpace x = x : decodeWords xs
| otherwise =
case decodeWord (x:xs) of
Nothing -> x : decodeWords xs
Just (as,bs) -> as ++ decodeWords bs