blob: 53ffe52ed20772324fc8092ab978c28686cdaa6b (
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
|
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
--module Main (main) where
import Control.Applicative
--import qualified Data.Attoparsec.ByteString.Char8 as A8
import qualified Data.CaseInsensitive as CI
import qualified Data.List as List
import Data.Maybe
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.MBox
import Data.Ord
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Text.Lazy as LT
import Data.Time
import Data.Tree (Tree, Forest)
import qualified Data.Tree as Tree
import qualified MappedSets
import Safe
import System.Environment
import System.Locale
import System.Process
import qualified Text.ParserCombinators.Parsec.Rfc2822 as P
import qualified Text.ParserCombinators.Parsec as P
type Ident = LT.Text
data IdentFields = IdentFields
{ messageId :: Ident
, inReplyTo :: [Ident]
, references :: [Ident]
}
deriving Show
notmuchShowMBox :: String -> IO MBox
notmuchShowMBox searchTerm =
parseMBox . LT.pack <$> readProcess
"notmuch"
[ "show"
, "--format=mbox"
, "--entire-thread=true"
, searchTerm
]
""
getMessageDate :: Message -> Maybe UTCTime
getMessageDate msg =
parseTime defaultTimeLocale rfc822DateFormat =<<
LT.unpack . snd <$>
(lastMay $
filter ((==CI.mk "Date") . CI.mk . LT.unpack . fst) $
headers 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 ((:[]) . LT.pack . show) id .
parseMsgIds headerName .
snd
) .
filter ((==CI.mk headerName) . CI.mk . LT.unpack . fst)
parseMsgIds :: P.SourceName -> LT.Text -> Either P.ParseError [LT.Text]
parseMsgIds srcName =
either Left (Right . map (LT.init . LT.tail . LT.pack)) .
P.parse obs_in_reply_to_parser srcName .
LT.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))
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
sortTree :: Tree Message -> Tree Message
sortTree t =
Tree.Node (Tree.rootLabel t) $
map sortTree $
List.sortBy (comparing $ getMessageDate . Tree.rootLabel) $
Tree.subForest t
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
unpackMBox :: MBox -> Map Ident Message
unpackMBox =
Map.fromList .
map (\msg -> (getMessageId $ headers msg, msg))
renderMessage :: Message -> String
renderMessage msg =
LT.unpack (getMessageId $ headers msg)
++ " "
++ drop (length ("From " :: String)) (LT.unpack $ fromLine msg)
main :: IO ()
main = do
-- load-env hack
maybe (return ()) (setEnv "HOME") =<< lookupEnv "OLDHOME"
notmuchShowMBox "tree1" >>=
putStrLn . Tree.drawTree .
Tree.Node "subject:tree-test" .
map (fmap renderMessage) .
toForest
|