module Network.Mail.Parse.Utils where
import Network.Mail.Parse.Types
import Data.Attoparsec.ByteString
import qualified Data.Attoparsec.ByteString as AP
import qualified Data.ByteString.Char8 as BS
import Data.Word8
import Data.List
import Data.Text (Text)
import qualified Data.Text as T
import Data.Either.Utils (maybeToEither)
hadCRLF :: Word8 -> Word8 -> Maybe Word8
hadCRLF prev current =
if prev == _cr && current == _lf
then Nothing
else Just current
consumeTillEndLine :: Parser BS.ByteString
consumeTillEndLine = scan 0 hadCRLF <* satisfy (== _lf)
isWhitespace :: Word8 -> Bool
isWhitespace x = x == 9 || x == 32
isConsequentHeaderLine :: Parser BS.ByteString
isConsequentHeaderLine = satisfy isWhitespace *>
AP.takeWhile isWhitespace *>
consumeTillEndLine
commentRemover :: Text -> Text
commentRemover contents = T.strip withoutComment
where splitAtComment = T.split (\c -> c == '(' || c == ')') contents
withoutComment = if length splitAtComment > 1
then T.append (head splitAtComment) (last splitAtComment)
else head splitAtComment
findHeader :: Text -> [Header] -> Either ErrorMessage Header
findHeader hdr headers = maybeToEither notFound header
where notFound = T.concat ["Cound not find header '", T.pack . show $ hdr, "'"]
eigenHeader = T.toLower hdr
header = find (\x -> T.toLower (headerName x) == eigenHeader) headers
eitherToMaybe :: Either a b -> Maybe b
eitherToMaybe (Right a) = Just a
eitherToMaybe (Left _) = Nothing