summaryrefslogtreecommitdiffstats
path: root/MBox.hs
blob: 0bd3889c527ac25219b9e8db90d1f4462dcd5f51 (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
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module MBox
    (
      -- TODO don't re-export MBox but use our own Message type
      module Export
    , getMessageId
    , toForest
    ) where

import qualified Data.MBox as Export

import           Control.Applicative
import qualified Data.CaseInsensitive as CI
import qualified Data.List as List
import           Data.Map.Strict   (Map)
import qualified Data.Map.Strict as Map
import           Data.Maybe
import           Data.MBox
import           Data.Ord
import           Data.Set   (Set)
import qualified Data.Set as Set
import           Data.Text.Lazy   (Text)
import           Data.Time
import           Data.Tree   (Tree, Forest)
import qualified Data.Tree as Tree
import qualified MappedSets
import qualified Data.Text.Lazy as Text
import           Safe
import           System.Locale
import qualified Text.ParserCombinators.Parsec.Rfc2822 as P
import qualified Text.ParserCombinators.Parsec as P


type Ident = Text


data IdentFields = IdentFields
    { messageId :: Ident
    , inReplyTo :: [Ident]
    , references :: [Ident]
    }
  deriving Show


toForest :: MBox -> Forest Message
toForest mbox =
    map (sortTree . fmap (\i -> fromMaybe (error "meh") $ Map.lookup i msgs)) $
    concatMap (Tree.subForest . mkSubTree) (Set.toList $ roots refs)
  where

    mkSubTree rootLabel =
        Tree.Node rootLabel $
            map mkSubTree (maybe [] Set.toList $ Map.lookup rootLabel backRefs)

    refs = mboxRefs mbox
    backRefs = MappedSets.invert refs
    msgs = unpackMBox mbox


-- TODO finde a new home for roots
roots :: Ord a => Map a (Set a) -> Set a
roots refs =
    Set.unions $ Map.elems $ Map.filter p refs
  where
    messageIDs = Set.fromList $ Map.keys refs
    p = Set.null . Set.intersection messageIDs


-- TODO finde a new home for sortTree
sortTree :: Tree Message -> Tree Message
sortTree t =
    Tree.Node (Tree.rootLabel t) $
        map sortTree $
        List.sortBy (comparing $ getMessageDate . Tree.rootLabel) $
        Tree.subForest t


getMessageDate :: Message -> Maybe UTCTime
getMessageDate msg =
    parseTime defaultTimeLocale rfc822DateFormat =<<
    Text.unpack . snd <$>
        (lastMay $
         filter ((==CI.mk "Date") . CI.mk . Text.unpack . fst) $
         headers msg)


unpackMBox :: MBox -> Map Ident Message
unpackMBox =
    Map.fromList .
    map (\msg -> (getMessageId $ headers msg, msg))


getIdentFields :: Message -> IdentFields
getIdentFields m =
    IdentFields
        { messageId = getMessageId hdrs
        , inReplyTo = getInReplyTo hdrs
        , references = getReferences hdrs
        }
  where
    hdrs = headers m


-- TODO generate default Message-ID if not present
getMessageId :: [Header] -> Ident
getMessageId =
    head .
    headerMessageIds "Message-ID"


getInReplyTo :: [Header] -> [Ident]
getInReplyTo =
    headerMessageIds "In-Reply-To"


getReferences :: [Header] -> [Ident]
getReferences =
    headerMessageIds "References"


headerMessageIds :: P.SourceName -> [Header] -> [Ident]
headerMessageIds headerName =
    concatMap (
        either ((:[]) . Text.pack . show) id .
        parseMsgIds headerName .
        snd
    ) .
    filter ((==CI.mk headerName) . CI.mk . Text.unpack . fst)


parseMsgIds :: P.SourceName -> Text -> Either P.ParseError [Ident]
parseMsgIds srcName =
    either Left (Right . map (Text.init . Text.tail . Text.pack)) .
    P.parse obs_in_reply_to_parser srcName .
    Text.unpack
  where
    --obs_in_reply_to_parser :: CharParser a [String]
    obs_in_reply_to_parser =
        --filter (not . null) <$> P.many (P.phrase >> return [] <|> P.msg_id)
        P.many1 P.msg_id


messageRefs :: IdentFields -> [Ident]
messageRefs IdentFields{..} =
    if null inReplyTo
        then maybe [""] (:[]) (lastMay references)
        else inReplyTo


mboxRefs :: MBox -> Map Ident (Set Ident)
mboxRefs =
    MappedSets.mk .
    map (\m ->
          let x = getIdentFields m
          in (messageId x, messageRefs x))