summaryrefslogtreecommitdiffstats
path: root/ParseMail.hs
blob: bfe2837a3dd5c0c7789cb694be6eb5c402110035 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module ParseMail (readMail) where

import qualified Data.Attoparsec.ByteString.Char8 as A8
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import qualified Data.CaseInsensitive as CI
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.Encoding as LT
import qualified Network.Email.Header.Parser as P
import qualified Network.Email.Header.Types as H
import qualified Network.Mail.Mime as M
import Codec.MIME.Parse
import Codec.MIME.Type
import Control.Applicative
import Data.Monoid
import Data.Char



-- TODO eventually we want our completely own Address, i.e. w/o M.Address
data Address = Mailbox M.Address | Group T.Text [M.Address]
  deriving (Show)



readMail :: FilePath -> IO M.Mail
readMail p =
    fromMIMEValue . parseMIMEMessage <$> T.readFile p

--meh0 :: IO M.Mail
--meh0 = 
--    return . xxx . parseMIMEMessage =<< T.readFile "/home/tv/tmp/testmail"
--
--meh :: IO ()
--meh = 
--    LBS8.putStr =<< M.renderMail' =<< return . xxx . parseMIMEMessage =<< T.readFile "/home/tv/tmp/testmail"
--

fromMIMEValue :: MIMEValue -> M.Mail
fromMIMEValue val =
    let m = foldr f (M.emptyMail $ M.Address Nothing "anonymous@localhost")
              $ fromMIMEParams
              $ mime_val_headers val
    in m { M.mailParts = [[part]] }
  where

    part =
        case mime_val_content val of
            Single content ->
                M.Part
                    { M.partType = "text/plain"
                    , M.partEncoding = M.None
                    , M.partFilename = Nothing
                    , M.partHeaders = []
                    , M.partContent = LT.encodeUtf8 $ LT.fromStrict content
                    }
            _ -> error ("meh: " ++ show val)

    f :: H.Header -> M.Mail -> M.Mail
    f (k, v) m = case k of
        "from" ->
            m { M.mailFrom =
                    (\case
                        Mailbox a -> a
                        Group _ _ ->
                            error "cannot use group in from header"
                    ) $
                    either error id $
                    parseAddress $
                    LBS.toStrict v
                }
        "to" ->
            m { M.mailTo =
                  mconcat $
                      map (\case
                            Mailbox a -> [a]
                            Group _ as -> as
                        ) $
                      either error id $
                      parseAddresses $
                      LBS.toStrict v
              }
        "cc" ->
            m { M.mailCc =
                  mconcat $
                      map (\case
                            Mailbox a -> [a]
                            Group _ as -> as
                        ) $
                      either error id $
                      parseAddresses $
                      LBS.toStrict v
              }
        "bcc" ->
            m { M.mailBcc =
                  mconcat $
                      map (\case
                            Mailbox a -> [a]
                            Group _ as -> as
                        ) $
                      either error id $
                      parseAddresses $
                      LBS.toStrict v
              }
        _ ->
            m { M.mailHeaders =
                  (CI.original k, LT.toStrict $ LT.decodeUtf8 v) :
                  M.mailHeaders m
              }


parseAddress :: BS.ByteString -> Either String Address
parseAddress =
    (\x0 -> case x0 of
        A8.Done "" r -> Right r
        x -> Left $ show x
    ) .
    A8.parse (P.cfws *> address <* A8.endOfInput)


parseAddresses :: BS.ByteString -> Either String [Address]
parseAddresses =
    (\x0 -> case x0 of
        A8.Done "" r -> Right r
        x -> Left $ show x
    ) .
    A8.parse (P.cfws *> address `A8.sepBy1` A8.char ',' <* A8.endOfInput)


fromMIMEParams :: [MIMEParam] -> H.Headers
fromMIMEParams =
    map $ \(MIMEParam k v) ->
        (CI.mk $ T.encodeUtf8 k, LT.encodeUtf8 $ LT.fromStrict v)


-- TODO we should probably use email-header


-- address     =  mailbox                      ; one addressee
--             /  group                        ; named list
address :: A8.Parser Address
address =
    (A8.<?> "address") $
    Mailbox <$> mailbox
    <|>
    group


-- group       =  phrase ":" [#mailbox] ";"
group :: A8.Parser Address
group =
    (A8.<?> "group") $
    Group
        <$> T.intercalate "," <$> phrase
        <* A8.char ':'
        <*> mailbox `A8.sepBy` A8.many1 (A8.char ',')
        <* A8.char ';'


-- mailbox     =  addr-spec                    ; simple address
--             /  phrase route-addr            ; name & addr-spec
mailbox :: A8.Parser M.Address
mailbox =
    (A8.<?> "mailbox") $
    M.Address Nothing <$> addrSpec <|>
    M.Address . Just . T.intercalate " " <$> A8.option [] phrase <*> routeAddr


-- route-addr  =  "<" [route] addr-spec ">"
routeAddr :: A8.Parser T.Text
routeAddr =
    (A8.<?> "routeAddr") $
    P.cfws *>
    A8.char '<' *>
    -- TODO A8.option [] route <*>
    addrSpec <*
    A8.char '>'


---- route       =  1#("@" domain) ":"           ; path-relative
--route :: A8.Parser [T.Text]
--route =
--    (A8.<?> "route") $
--    A8.many1 (A8.char '@' *> domain) <* A8.char ':'


-- addr-spec   =  local-part "@" domain        ; global address
addrSpec :: A8.Parser T.Text
addrSpec =
    (A8.<?> "addrSpec") $ do
        a <- localPart
        b <- T.singleton <$> A8.char '@'
        c <- domain
        return $ a <> b <> c

-- local-part  =  word *("." word)             ; uninterpreted
--                                             ; case-preserved
localPart :: A8.Parser T.Text
localPart =
    (A8.<?> "localPart") $
     T.intercalate "." <$> (word `A8.sepBy1` A8.char '.')


-- domain      =  sub-domain *("." sub-domain)
domain :: A8.Parser T.Text
domain =
    (A8.<?> "domain") $
    mconcat <$> (subDomain `A8.sepBy1` A8.char '.')

-- sub-domain  =  domain-ref / domain-literal
subDomain :: A8.Parser T.Text
subDomain =
    (A8.<?> "subDomain") $
    domainRef <|> domainLiteral

-- domain-ref  =  atom                         ; symbolic reference
domainRef :: A8.Parser T.Text
domainRef =
    (A8.<?> "domainRef") $
    atom


-- atom        =  1*<any CHAR except specials, SPACE and CTLs>
atom :: A8.Parser T.Text
atom =
    (A8.<?> "atom") $
    P.cfws *>
    (T.pack <$> A8.many1 (A8.satisfy $ A8.notInClass atomClass))


-- domain-literal =  "[" *(dtext / quoted-pair) "]"
domainLiteral :: A8.Parser T.Text
domainLiteral =
    (A8.<?> "domainLiteral") $
    T.pack <$>
        (A8.char '[' *> A8.many' (dtext <|> quotedPair) <* A8.char ']')


-- dtext       =  <any CHAR excluding "[",     ; => may be folded
--                 "]", "\" & CR, & including
--                 linear-white-space>
dtext :: A8.Parser Char
dtext =
    (A8.<?> "dtext") $
    A8.satisfy (A8.notInClass "[]\\\CR")


-- phrase      =  1*word
phrase :: A8.Parser [T.Text]
phrase =
    (A8.<?> "phrase") $
    A8.many1 word


-- qtext       =  <any CHAR excepting <">,     ; => may be folded
--                 "\" & CR, and including
--                 linear-white-space>
qtext :: A8.Parser Char
qtext =
    (A8.<?> "qtext") $
    A8.satisfy (A8.notInClass "\"\\\CR")


-- quoted-pair =  "\" CHAR
quotedPair :: A8.Parser Char
quotedPair =
    (A8.<?> "quotedPair") $
    A8.char '\\' *> A8.anyChar


-- quoted-string = <"> *(qtext/quoted-pair) <">; Regular qtext or
--                                             ;   quoted chars.
quotedString :: A8.Parser T.Text
quotedString =
    (A8.<?> "quotedString") $
    T.pack <$> (A8.char '"' *> A8.many' (qtext <|> quotedPair) <* A8.char '"')


-- word        =  atom / quoted-string
word :: A8.Parser T.Text
word =
    (A8.<?> "word") $
    atom <|> quotedString


atomClass :: [Char]
atomClass = specialClass ++ spaceClass ++ ctlClass


specialClass :: [Char]
specialClass = "()<>@,;:\\\".[]"


spaceClass :: [Char]
spaceClass = " "


ctlClass :: [Char]
ctlClass = map chr $ [0..31] ++ [127]