summaryrefslogtreecommitdiffstats
path: root/Notmuch.hs
blob: 309f4113e1d8b6a9aa3d8737a36dc0e1e21c6da5 (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE OverloadedStrings #-}
module Notmuch where

--import Language.Haskell.TH.Ppr (bytesToString)
import Data.Aeson
--import Data.List.Split
--import Data.Attoparsec.ByteString hiding (try)
import Data.Maybe
import Data.Monoid
import Data.String
--import Data.Traversable
import Data.Tree
--import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
--import qualified Data.ByteString.Char8 as BS8
--import qualified Data.Text.Lazy as TL
import qualified Data.Text as T
--import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
--import Data.Version (Version(..), parseVersion)
import System.Process
--import System.IO
import qualified Data.Map as M

import Notmuch.SearchResult
import Notmuch.Message


import Control.Concurrent
--import Control.Concurrent.MVar
import Control.Exception
import System.IO
import Control.DeepSeq (rnf)


-- | Fork a thread while doing something else, but kill it if there's an
-- exception.
--
-- This is important in the cases above because we want to kill the thread
-- that is holding the Handle lock, because when we clean up the process we
-- try to close that handle, which could otherwise deadlock.
--
withForkWait :: IO () -> (IO () ->  IO a) -> IO a
withForkWait async body = do
  waitVar <- newEmptyMVar :: IO (MVar (Either SomeException ()))
  mask $ \restore -> do
    tid <- forkIO $ try (restore async) >>= putMVar waitVar
    let wait = takeMVar waitVar >>= either throwIO return
    restore (body wait) `onException` killThread tid




notmuch :: [String] -> IO LBS.ByteString
notmuch args = do
    (_, Just hout, _, ph) <- createProcess (proc "notmuch" args)
                                          { std_out = CreatePipe }
    output <- LBS.hGetContents hout


    withForkWait (evaluate $ rnf output) $ \waitOut -> do

      ---- now write any input
      --unless (null input) $
      --  ignoreSigPipe $ hPutStr inh input
      -- hClose performs implicit hFlush, and thus may trigger a SIGPIPE
      --ignoreSigPipe $ hClose inh

      -- wait on the output
      waitOut
      hClose hout

    -- wait on the process
    _ex <- waitForProcess ph
    --return (ex, output)

    --case ex of
    --    ExitSuccess   -> return output
    --    ExitFailure r -> processFailedException "readProcess" cmd args r

    return output



--notmuch' args = do
--    (_, Just hout, _, _) <- createProcess (proc "notmuch" args)
--                                          { std_out = CreatePipe }
--    BS.hGetContents hout


search :: String -> IO (Either String [SearchResult])
search term =
    notmuch [ "search", "--format=json", "--format-version=2", term ]
        >>= return . eitherDecode'


putSearchResults :: [SearchResult] -> IO ()
putSearchResults = mapM_ (T.putStrLn . drawSearchResult)


showThread :: String -> IO ()
showThread tid = do
    c' <- notmuch [ "show", "--format=json", "--format-version=2"
                   , "thread:" <> tid ]

    let threads = case eitherDecode' c' :: Either String [Thread] of
                      Left err -> error err
                      Right x -> x
        --threadsF = map threadForest threads
        ttt = head $ threadForest $ head $ threads

    --Prelude.putStrLn $ drawTree $ fmap drawMessage ttt
    Prelude.putStrLn $ showTree $ ttt


getThread :: String -> IO (Tree Message)
getThread tid = do
    c' <- notmuch [ "show", "--format=json", "--format-version=2"
                   , "thread:" <> tid ]

    let threads = case eitherDecode' c' :: Either String [Thread] of
                      Left err -> error err
                      Right x -> x
        --threadsF = map threadForest threads
        ttt = head $ threadForest $ head $ threads
    return ttt



setTag :: String -> String -> IO LBS.ByteString
setTag tag i = do
    notmuch [ "tag", "+" <> tag , "id:" <> i ]


unsetTag :: String -> String -> IO LBS.ByteString
unsetTag tag i = do
    notmuch [ "tag", "-" <> tag , "id:" <> i ]


openMessage :: String -> IO ()
openMessage i = do
    notmuch [ "tag", "+open" , "id:" <> i ] >> return ()


closeMessage :: String -> IO ()
closeMessage i = do
    notmuch [ "tag", "-open" , "id:" <> i ] >> return ()






---- | Neat 2-dimensional drawing of a tree.
--drawTree :: Tree String -> String
--drawTree  = unlines . draw
--
---- | Neat 2-dimensional drawing of a forest.
--drawForest :: Forest String -> String
--drawForest  = unlines . map drawTree
--
draw :: Tree Message -> [String]
draw (Node x ts0) = (show $ drawMessage x) : drawSubTrees ts0
  where
    drawSubTrees [] = []
    drawSubTrees [t] =
        -- "|" : shift "`- " "   " (draw t)
        shift "`- " "   " (draw t)

    drawSubTrees (t:ts) =
        -- "|" : shift "+- " "|  " (draw t) ++ drawSubTrees ts
        shift "+- " "|  " (draw t) ++ drawSubTrees ts

    shift first other = zipWith (++) (first : repeat other)


indentMessageInit :: [String] -> [String]
indentMessageInit [] = []
indentMessageInit (s:ss) = (red "─╴" ++ s) : map (red "  " ++) ss

indentMessageLast :: [String] -> [String]
indentMessageLast [] = []
indentMessageLast (s:ss) = (red "┬╴" ++ s) : map (red "│ " ++) ss

indentInit :: [String] -> [String]
indentInit [] = []
indentInit (s:ss) = (red "├╴" ++ s) : map (red "│ " ++) ss

indentLast :: [String] -> [String]
indentLast [] = []
indentLast (s:ss) = (red "└╴" ++ s) : map (red "  " ++) ss

indentChildren :: [[String]] -> [[String]]
indentChildren [] = []
indentChildren ns = map indentInit (init ns) ++ [indentLast (last ns)]

appLast :: [String] -> String -> [String]
appLast ss s = init ss ++ [last ss ++ s]

showTree' :: Tree Message -> [String]
showTree' (Node n ns) =
    -- (if length ns == 0
    --     then indentMessageInit $ drawMessage n
    --     else indentMessageLast $ drawMessage n)
    drawMessage n
    ++
    concat (indentChildren (map showTree' ns))

-- | Show a 'Tree' using Unicode art
showTree :: Tree Message -> String
showTree = unlines . showTree'






drawMessage :: Message -> [String]
drawMessage Message{..} =
    ----unMessageID messageId
    --show messageTime
    --  -- <> " " <> T.unpack messageDateRel
    --  <> "\n" <> show (fromJust $ M.lookup "From" messageHeaders)
    --  <> "\n" <> show (fromJust $ M.lookup "Subject" messageHeaders)
    [ gray (unMessageID messageId)
      <> " " <> T.unpack (fromJust $ M.lookup "From" messageHeaders)
      <> " " <> gray (show messageDateRel)
      <> " " <> T.unpack (T.intercalate ", " $ map magenta messageTags)
    -- , T.unpack $ fromJust $ M.lookup "Subject" messageHeaders
    ]
    ++
    (if "open" `elem` messageTags
        then concat (map (map green . lines . drawMessagePart) messageBody)
        else [])
    --map drawMessagePart messageBody

drawMessagePart :: MessagePart -> String
drawMessagePart p = drawPartContent (partContent p)

drawPartContent :: MessageContent -> String
drawPartContent (ContentText t) = T.unpack t
--drawPartContent (ContentText t) = "ContentText"
drawPartContent (ContentMultipart _) = "ContentMultipart"
drawPartContent (ContentMsgRFC822 _) = "ContentMsgRFC822"

-- otherAuthors are non-matched authors in the same thread
drawSearchResult :: SearchResult -> T.Text
drawSearchResult SearchResult{..} = do
    let (matchedAuthors, otherAuthors) =
            case T.splitOn "| " searchAuthors of
                [a,b] -> (T.splitOn ", " a, T.splitOn ", " b)
                [a] -> (T.splitOn ", " a, [])
                x -> error $ "drawSearchResult: error " <> show x

        a' = map green matchedAuthors
        b' = map red otherAuthors
        --qa = maybe [] (T.splitOn " ") (searchQuery !! 0)
        --qb = maybe [] (T.splitOn " ") (searchQuery !! 1)
        ThreadID tid = searchThread

    (T.pack tid)
        -- <> " " <> (T.pack $ show $ searchTime)
        <> " " <> "[" <> (T.pack $ show searchMatched) <> "/"
                      <> (T.pack $ show searchTotal) <> "]"
        <> " " <> searchDateRel
        <> " " <> searchSubject
        <> " " <> T.intercalate ", " (a' <> b')
        <> " " <> T.intercalate ", " (map magenta searchTags)




red, green, magenta, gray :: (Monoid m, IsString m) => m -> m
red = ("\ESC[31m"<>) . (<>"\ESC[m")
green = ("\ESC[32m"<>) . (<>"\ESC[m")
magenta = ("\ESC[35m"<>) . (<>"\ESC[m")
gray = ("\ESC[30;1m"<>) . (<>"\ESC[39;22m")




    --case fromJSON c of
    --    Error e -> error e
    --    Success x -> return x

--    c <- hGetContents hout
--
--    let v = 
--
--
--    putStrLn $ show c

--    let fixTags :: Char -> Char
--        fixTags '+' = '-'
--        fixTags '~' = '-'
--        fixTags c   = c
--    let vStr = map fixTags $ words out !! 1
--    let vs = filter (\(_,r) -> r == "") $ readP_to_S parseVersion vStr
--    case vs of
--        ((v,_):_) -> return v
--        _ -> throw $ NotmuchError $ "Unable to parse version: " ++ vStr



-- | The version of notmuch
--notmuchVersion :: MonadIO m => m Version
--notmuchVersion = do
--    out <- liftIO $ readProcess "notmuch" ["--version"] ""
--    let fixTags :: Char -> Char
--        fixTags '+' = '-'
--        fixTags '~' = '-'
--        fixTags c   = c
--    let vStr = map fixTags $ words out !! 1
--    let vs = filter (\(_,r) -> r == "") $ readP_to_S parseVersion vStr
--    case vs of
--        ((v,_):_) -> return v
--        _ -> throw $ NotmuchError $ "Unable to parse version: " ++ vStr
--
--
-- r <- createProcess (proc "ls" [])
--
--proc