aboutsummaryrefslogtreecommitdiff
path: root/src/Codec/Text/Rfc1342.hs
diff options
context:
space:
mode:
authorRaúl Benencia <rul@kalgan.cc>2013-09-05 19:57:02 -0300
committerRaúl Benencia <rul@kalgan.cc>2013-09-05 19:57:02 -0300
commit3bd3fd2c6eae2f36f69f247403421e8cf8226394 (patch)
tree0d92cd91d69cf52b9168c403af317643e88c2587 /src/Codec/Text/Rfc1342.hs
parent41b53ca04b6d52457f331930e8fea68416498882 (diff)
Moved all code to src/
Diffstat (limited to 'src/Codec/Text/Rfc1342.hs')
-rw-r--r--src/Codec/Text/Rfc1342.hs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/Codec/Text/Rfc1342.hs b/src/Codec/Text/Rfc1342.hs
new file mode 100644
index 0000000..f6d8fe2
--- /dev/null
+++ b/src/Codec/Text/Rfc1342.hs
@@ -0,0 +1,57 @@
+{- A simple RFC1342 decoder.
+ -
+ - Copyright 2013 Raúl Benencia <rul@kalgan.cc>
+ -
+ - Licensed under the GNU GPL version 3 or higher
+ -
+ -}
+module Codec.Text.Rfc1342 (decodeField) where
+
+import qualified Codec.Binary.Base64 as B64
+import qualified Codec.Binary.QuotedPrintable as QP
+
+import Data.Char (toLower, isSpace, chr)
+import Data.List(isPrefixOf)
+import Data.Word (Word8)
+
+import Data.Encoding(decodeString)
+
+-- Encoding imports. If you want to support more encodings, just add'em here.
+import Data.Encoding.UTF8
+import Data.Encoding.ISO88591
+import Data.Encoding.ISO88592
+import Data.Encoding.ISO88598
+import Data.Encoding.ISO88599
+
+decodeField :: String -> String
+decodeField ('=':'?':cs) = decodeWithCharset dec rest
+ where (encoding, rest) = span (\c -> c /= '?') cs
+ dec = case (map toLower encoding) of
+ "utf-8" -> decodeString UTF8
+ "iso-8859-1" -> decodeString ISO88591
+ "iso-8859-2" -> decodeString ISO88592
+ "iso-8859-8" -> decodeString ISO88598
+ "iso-8859-9" -> decodeString ISO88599
+ _ -> id
+decodeField [] = []
+decodeField (c:cs) = c:decodeField cs
+
+decodeWithCharset dec [] = []
+decodeWithCharset dec ('?':c:'?':cs) | toLower c == 'b' = dataDecodeWith B64.decode
+ | toLower c == 'q' = dataDecodeWith QP.decode
+ | otherwise = cs
+ where (encoded, rest') = span (\c -> c /= '?') cs
+ rest = if "?=" `isPrefixOf` rest'
+ then drop 2 rest'
+ else rest'
+ dataDecodeWith datadec = (_2spc . dec . unwrap . datadec $ encoded) ++ decodeField rest -- ++ (decodeField $ dropWhile isSpace rest)
+
+unwrap :: Maybe [Word8] -> String
+unwrap Nothing = []
+unwrap (Just str) = bytesToString str
+
+bytesToString :: [Word8] -> String
+bytesToString = map (chr . fromIntegral)
+
+-- Sometimes an underscore represents the SPACE character
+_2spc = map (\x -> if x == '_' then ' ' else x) \ No newline at end of file
nihil fit ex nihilo