summaryrefslogtreecommitdiffstats
path: root/test5.hs
blob: 5ee6ea81bff84d9ed012011a200d8f165d9d3132 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Main (main, mainWithArgs) where

import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy.Char8 as LBS8
import           Data.CaseInsensitive   (CI)
import qualified Data.CaseInsensitive as CI
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Tree as Tree
import qualified Data.Tree.Zipper as Z
import qualified Network.Mail.Mime as M
import qualified Notmuch
import qualified Notmuch.Message as Notmuch
import qualified Notmuch.SearchResult as Notmuch
import qualified System.Console.Terminal.Size as Term
import Control.Applicative
import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Except
import Data.Foldable (foldrM)
import Data.List (intercalate)
import Data.Maybe
import Data.Monoid
import Data.Time
import Event
import ParseMail (readMail)
import RenderTreeView (renderTreeView)
import Scanner (scan)
import Safe
import System.Directory
import System.Console.Docopt (getArgWithDefault, optionsWithUsage, shortOption)
import System.Environment
import System.Exit
import System.IO
import System.Locale (defaultTimeLocale, rfc822DateFormat)
import System.Posix.Files
import System.Posix.Signals
import System.Process
import TagUtils
import Text.Hyphenation
import Text.LineBreak
import Trammel
import TreeSearch
import TreeView
import TreeZipperUtils (modifyFirstParentLabelWhere)
import Utils

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


data State = State
    { cursor :: Z.TreePos Z.Full TreeView
    , xoffset :: Int
    , yoffset :: Int
    , flashMessage :: Trammel String
    , screenWidth :: Int
    , screenHeight :: Int
    , headBuffer :: [Trammel String]
    , treeBuffer :: [Trammel String]
    , now :: UTCTime
    , decset :: [Int]
    , decrst :: [Int]
    }

initState :: String -> IO State
initState query = do
    r_ <- either error id <$> Notmuch.search query

    return State
        { cursor = Z.fromTree $ fromSearchResults query r_
        , xoffset = 0
        , yoffset = 0
        , flashMessage = "Welcome to much; quit with ^C"
        , screenWidth = 0
        , screenHeight = 0
        , headBuffer = []
        , treeBuffer = []
        , now = UTCTime (fromGregorian 1984 5 23) 49062
        , decset =
            1000 : -- X & Y on button press and release
            1005 : -- UTF-8 mouse mode
            1049 : -- use cleared alternate screen buffer
            []
        , decrst =
            25 : -- hide cursor
            []
        }


main :: IO ()
main =
    getArgs >>= mainWithArgs


mainWithArgs :: [String] -> IO ()
mainWithArgs args = do
    args' <- optionsWithUsage usage args
    let query = getArgWithDefault args' defaultSearch (shortOption 'q')
    bracket (initState query) cleanup startup
  where
    usage = unlines
      [ "Command-line MUA using notmuch."
      , ""
      , "Usage:"
      , "  much [-q <search-term>]"
      , ""
      , "Options:"
      , "  -q <search-term>, --query=<search-term>"
      , "        Open specific search, defaults to " ++ (show defaultSearch)
      ]
    defaultSearch = "tag:inbox AND NOT tag:killed"


cleanup :: State -> IO ()
cleanup q@State{..} = do
    hSetEcho stdin True
    resetTerm q { decset = decrst, decrst = decset }


startup :: State -> IO ()
startup q0 = do

    -- load-env hack
    maybe (return ()) (setEnv "HOME") =<< lookupEnv "OLDHOME"

    -- TODO move this to resetTerm?
    hSetBuffering stdin NoBuffering
    hSetBuffering stdout (BlockBuffering $ Just 4096)

    resetTerm q0

    (putEvent, getEvent) <- do
        v <- newEmptyMVar
        return (putMVar v, takeMVar v)

    (shutdown, waitForShutdown) <- do
        v <- newEmptyMVar
        return (putMVar v (), takeMVar v)


    mapM_ (\(s, f) -> installHandler s (Catch f) Nothing)
        [ (sigINT, shutdown)
        , (28, winchHandler putEvent)
        ]

    threadIds <- mapM forkIO
        [ forever $ scan stdin >>= putEvent
        , run getEvent q0
        ]

    winchHandler putEvent

    waitForShutdown
    mapM_ killThread threadIds


resetTerm :: State -> IO ()
resetTerm State{..} = do
    hSetEcho stdin False
    hPutStr stdout $ "\ESC[?" ++ intercalate ";" (map show decset) ++ "h"
    hPutStr stdout $ "\ESC[?" ++ intercalate ";" (map show decrst) ++ "l"


winchHandler :: (Event -> IO ()) -> IO ()
winchHandler putEvent =
    Term.size >>= \case
        Just (Term.Window {Term.width = w, Term.height = h}) ->
            putEvent $ EResize w h
        Nothing ->
            return ()


run :: IO Event -> State -> IO ()
run getEvent = rec where
    rec q = rec =<< do
        t <- getCurrentTime
        let q' = render q { now = t }
        redraw q' >> getEvent >>= processEvent q'


-- TODO merge EKey and EMouse?
processEvent :: State -> Event -> IO State
processEvent q = \case
    EFlash t ->
        return q { flashMessage = t }
    EKey s ->
        keymap s q
    EMouse info ->
        mousemap info q
    EResize w h ->
        return q
            { screenWidth = w, screenHeight = h
            , flashMessage = Plain $ "resize " <> show (w,h)
            }
    ev ->
        return q
            { flashMessage = SGR [31,1] $ Plain $ "unhandled event: " <> show ev
            }


render :: State -> State
render q@State{..} =
    q { treeBuffer = newTreeBuf
      , headBuffer = newHeadBuf
      }
  where
    newTreeBuf = renderTreeView now cursor (Z.root cursor)
    newHeadBuf =
        [ Plain (show screenWidth) <> "x" <> Plain (show screenHeight)
          <> " " <> Plain (show $ linearPos cursor - yoffset)
          <> " " <> Plain (show $ topOverrun q)
          <> " " <> Plain (show $ botOverrun q)
          <> " " <> flashMessage
          <> " " <> Plain (show (xoffset, yoffset))
        ]

render0 :: State -> [Trammel String]
render0 _q@State{..} = do
    let buffer =
            map (trammelTake screenWidth . trammelDrop xoffset) $
            take screenHeight $
            headBuffer ++ drop yoffset treeBuffer
    buffer ++ take (screenHeight - length buffer) (repeat "~")


redraw :: State -> IO ()
redraw q@State{..} = do
    hPutStr stdout $ map (sub '\t' ' ') $ "\ESC[H" ++ (pp $ mintercalate "\n" $ map eraseRight $ render0 q)
    hFlush stdout
  where
    sub x x' c = if c == x then x' else c
    eraseRight s =
        if len s < screenWidth
            then s <> "\ESC[K"
            else s




keymap :: String -> State -> IO State

keymap "A" = attachFilesToDraft
keymap "a" = toggleTagAtCursor "inbox"
keymap "s" = toggleTagAtCursor "unread"
keymap "&" = toggleTagAtCursor "killed"
keymap "*" = toggleTagAtCursor "star"
keymap "r" = replyToAll
keymap "e" = viewSource
keymap "t" = editTagsAtCursor
keymap "k" = moveCursorUp 1
keymap "j" = moveCursorDown 1
keymap "K" = moveTreeDown 1
keymap "J" = moveTreeUp 1
keymap "\ESC[A" = moveCursorUp 1
keymap "\ESC[B" = moveCursorDown 1
keymap "\ESC[a" = moveTreeDown 1
keymap "\ESC[b" = moveTreeUp 1
keymap "\ESC[c" = moveTreeLeft 1  -- S-Right
keymap "\ESC[d" = moveTreeRight 1 -- S-Left
keymap "\ESC[5~" = \q -> moveTreeDown (screenHeight q `div` 2) q  -- PgUp
keymap "\ESC[6~" = \q -> moveTreeUp (screenHeight q `div` 2) q    -- PgDn
keymap "\n" = toggleFold
keymap "\ESC[Z" = moveCursorUpToPrevUnread -- S-Tab
keymap "\t" = moveCursorDownToNextUnread
keymap "\DEL" = moveToParent  -- backspace

-- TODO wrap/unwrap to separate module
keymap "=" = \q@State{..} ->
    let cursor' = case Z.label cursor of
            TVMessageLine a b c s ->
                wrap (TVMessageLine a b c) cursor s
            _ -> cursor
    in return q { cursor = cursor' }
  where

    --unwrap = error "WIP"
        -- 1. get current id (must be TVMessageLine)
        -- 2. find first adjoined TVMessageLine with same id
        -- 3. find last adjoined TVMessageLine with same id
        -- 4. join lines (with space?)

    wrap ctor loc s =
        fromMaybe (error "die hard") $
        Z.nextTree $
        foldr (insert . ctor)
              (Z.delete loc)
              $ hy s

    insert a =
        Z.prevSpace . Z.insert (Tree.Node a [])

    hy s =
        breakStringLn bf s
      where
        shy = '\173'
        hyp = Just german_1996
        bf = BreakFormat 80 8 shy hyp

keymap "\ESCq" = \q@State{..} ->
    let parse = filter (/='\n') -- TODO proper parse
        draft = fromMaybe "" $ getSearchTerm $ Z.label $ Z.root cursor
    in editString q draft >>= \case
        Left err -> return q { flashMessage = Plain err }
        Right s' -> Notmuch.search s' >>= \case
            Left err ->
                return q { flashMessage = Plain err }
            Right result ->
                return q { cursor = Z.fromTree $ fromSearchResults (parse s') result }

-- <F1>
keymap "\ESC[11~" = \q@State{..} ->
    return q { flashMessage = Plain $ show $ treeViewId $ Z.label cursor }

-- <F2>
keymap "\ESC[12~" = \q@State{..} ->
    return q { flashMessage =
                  Plain $
                  show $
                  maybe Nothing (Just . Notmuch.messageFilename) $
                  getMessage $
                  Z.label cursor
              }

-- TODO Stuff Vim sends after exit (also there is more...)
keymap "\ESC[2;2R" = \q -> return q { flashMessage = flashMessage q <> " " <> Plain "stupid" }
keymap "\ESC[>85;95;0c" = \q -> return q { flashMessage = flashMessage q <> " " <> Plain "stupid" }

keymap s = \q ->
    return q { flashMessage = Plain $ show s }


mousemap :: MouseInfo -> State -> IO State

mousemap MouseInfo{mouseButton=1,mouseY=y} = defaultMouse1Click y
mousemap MouseInfo{mouseButton=3,mouseY=y} = \q -> defaultMouse1Click y q >>= toggleFold
mousemap MouseInfo{mouseButton=4} = moveTreeDown 3
mousemap MouseInfo{mouseButton=5} = moveTreeUp 3
mousemap MouseInfo{mouseButton=0} = return
mousemap info = \q ->
    return q { flashMessage = SGR [38,5,202] $ Plain $ show info }


defaultMouse1Click :: Monad m => Int -> State -> m State
defaultMouse1Click y q@State{..} = do
    let linearClickPos =
            let i = (y - length headBuffer + yoffset) - 1 {-zero-based-}
            in if 0 <= i && i < length treeBuffer
                then Just i
                else Nothing
    case linearClickPos of
        Nothing ->
            return q
                { flashMessage = Plain $ "nothing to click"
                }
        Just i ->
            return q
                { cursor = findNextN i $ Z.root cursor
                }



topOverrun :: State -> Int
topOverrun State{..} =
    max 0 (- (linearPos cursor - yoffset))


botOverrun :: State -> Int
botOverrun State{..} =
    max 0 (linearPos cursor - yoffset - (screenHeight - (length headBuffer) - 1))



moveCursorDown :: Monad m => Int -> State -> m State
moveCursorDown n q@State{..} =
    let cursor' = findNextN n cursor
        q' = q { cursor = cursor' }
    in case botOverrun q' of
        0 -> return q'
        i -> moveTreeUp i q'


moveCursorUp :: Monad m => Int -> State -> m State
moveCursorUp n q@State{..} =
    let cursor' = findPrevN n cursor
        q' = q { cursor = cursor' }
    in case topOverrun q' of
        0 -> return q'
        i -> moveTreeDown i q'


moveTreeUp :: Monad m => Int -> State -> m State
moveTreeUp n q@State{..} =
    let q' = q { yoffset = min (length treeBuffer - 1) $ max 0 (yoffset + n) }
    in case topOverrun q' of
        0 -> return q'
        i -> moveCursorDown i q'


moveTreeDown :: Monad m => Int -> State -> m State
moveTreeDown n q@State{..} =
    let q' = q { yoffset = min (length treeBuffer - 1) $ max 0 (yoffset - n) }
    in case botOverrun q' of
        0 -> return q'
        i -> moveCursorUp i q'


moveTreeLeft :: Monad m => Int -> State -> m State
moveTreeLeft n q@State{..} =
    return q { xoffset = xoffset + n }

moveTreeRight :: Monad m => Int -> State -> m State
moveTreeRight n q@State{..} =
    return q { xoffset = max 0 (xoffset - n) }


moveToParent :: Monad m => State -> m State
moveToParent q@State{..} =
    case Z.parent cursor of
        Nothing -> return q { flashMessage = "cannot go further up" }
        Just cursor' ->
            let q' = q { cursor = cursor' }
            in case topOverrun q' of
                0 -> return q'
                i -> moveTreeDown i q'


moveCursorToUnread
    :: (Num a, Monad m, Eq a)
    => (Z.TreePos Z.Full TreeView -> Maybe (Z.TreePos Z.Full TreeView))
    -> (State -> a)
    -> (a -> State -> m State)
    -> State -> m State
moveCursorToUnread cursorMove getTreeMoveCount treeMove q@State{..} =
    case cursorMove cursor >>= rec of
        Just cursor' ->
            let q' = q { cursor = cursor' }
            in case getTreeMoveCount q' of
                0 -> return q'
                i -> treeMove i q'
        Nothing ->
            return q { flashMessage = "no unread message in sight" }
  where
    rec loc =
        if hasTag "unread" loc
            then Just loc
            else cursorMove loc >>= rec
    hasTag tag loc =
        case Z.label loc of
            TVSearchResult sr ->
                tag `elem` Notmuch.searchTags sr
            TVMessage m ->
                tag `elem` Notmuch.messageTags m
            _ ->
                False

moveCursorUpToPrevUnread :: Monad m => State -> m State
moveCursorUpToPrevUnread =
    moveCursorToUnread findPrev topOverrun moveTreeDown

moveCursorDownToNextUnread :: Monad m => State -> m State
moveCursorDownToNextUnread =
    moveCursorToUnread findNext botOverrun moveTreeUp


setSubForest :: Tree.Forest a -> Tree.Tree a -> Tree.Tree a
setSubForest sf t = t { Tree.subForest = sf }


toggleFold :: State -> IO State
toggleFold q@State{..} =
    getNewSubForest >>= return . \case
        Left err ->
            q { flashMessage = SGR [31] $ Plain err }
        Right sf ->
            q { cursor = Z.modifyTree (setSubForest sf) cursor }
  where
    getNewSubForest =
        if hasUnloadedSubForest (Z.tree cursor)
            then loadSubForest (Z.label cursor)
            else return $ Right $ unloadSubForest (Z.tree cursor)


toggleTagAtCursor :: Tag -> State -> IO State
toggleTagAtCursor tag q@State{..} = case Z.label cursor of

    TVSearchResult sr -> do
        let tagOp =
                if tag `elem` Notmuch.searchTags sr
                    then DelTag
                    else AddTag
            tagOps = [tagOp tag]
        Notmuch.notmuchTag tagOps sr
        let cursor' = Z.modifyTree (patchTreeTags tagOps) cursor
        return q { cursor = cursor' }

    TVMessage m -> do
        let tagOp =
                if tag `elem` Notmuch.messageTags m
                    then DelTag
                    else AddTag
            tagOps = [tagOp tag]
        Notmuch.notmuchTag tagOps m
        let cursor' =
                -- TODO this needs a nice name
                modifyFirstParentLabelWhere isTVSearchResult f $
                Z.modifyLabel f cursor
            f = patchTags tagOps
        return q { cursor = cursor' }

    _ -> return q { flashMessage = "nothing happened" }


attachFilesToDraft :: State -> IO State
attachFilesToDraft q0 =
    runExceptT (go q0) >>= return . \result ->
        q0 { flashMessage = Plain (show result) }
  where
    go :: State -> ExceptT String IO String
    go State{cursor=cursor,now=now} = do
        msg <- maybe (throwE "not a message") return $
            getMessage (Z.label cursor)

        unless (Notmuch.hasTag "draft" msg) $
            throwE "message has no draft tag"

        paths <- filter (not . null) . lines <$> promptE "add files"
        when (null paths) (throwE "Aborted")

        newTags <-
            lines . LBS8.unpack . fst <$>
            readNotmuchE ["config", "get", "new.tags"] ""

        let tagOps =
                map AddTag ("attachment" : Notmuch.messageTags msg) ++
                map DelTag (map T.pack newTags)

        loadMailE (Notmuch.messageId msg)

            >>= attachFilesE paths
                -- ^ this will catch invalid paths
                -- TODO if it fails, then re-prompt

            >>= return . (addDateHeader now . removeHeader "Date")

            >>= liftE . M.renderMail' . removeHeaders
                [ "Content-Type"
                , "Content-Transfer-Encoding"
                , "MIME-Version"
                ]
            >>= readNotmuchE ("insert" : "--no-hooks" : tagOpsToArgs tagOps)
            >>  liftE (removeFile $ Notmuch.messageFilename msg)
            >>  readNotmuchE ["new", "--no-hooks", "--quiet"] ""

            >>  return "files attached"


readNotmuchE
    :: [String]
    -> LBS8.ByteString
    -> ExceptT String IO (LBS8.ByteString, LBS8.ByteString)
readNotmuchE args input = do
    (exitCode, out, err) <- liftE $ do
        (Just hin, Just hout, Just herr, ph) <-
            createProcess (proc "notmuch" args)
                { std_in = CreatePipe
                , std_out = CreatePipe
                , std_err = CreatePipe
                }
        LBS8.hPut hin input
        hClose hin

        out <- LBS8.hGetContents hout
        err <- LBS8.hGetContents herr

        withForkWait (evaluate $ rnf out) $ \waitOut -> do
            withForkWait (evaluate $ rnf err) $ \waitErr -> 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
              waitErr
              hClose hout
              hClose herr

        -- wait on the process
        exitCode <- waitForProcess ph

        return (exitCode, out, err)

    case exitCode of
        ExitSuccess -> return (out, err)
        ExitFailure code ->
            throwE $ "notmuch " ++ intercalate " " args ++
                     " exit code: " ++ show code ++ "; stderr:\n" ++
                     LBS8.unpack err


liftE :: IO a -> ExceptT String IO a
liftE io =
    lift (try io) >>= either (throwE . transformException) return
  where
    transformException :: IOException -> String
    transformException = ("IO: "++) . show


editMailE :: String -> ExceptT String IO String
editMailE ps =
    liftE (editMail ps) >>= \case
        Right r -> return r
        Left code -> throwE $ "edit mail error: " ++ show code


editTagsE :: [Tag] -> ExceptT String IO [TagOp]
editTagsE ps =
    liftE (editTags ps) >>= \case
        Right r -> return r
        Left code -> throwE $ "edit tags error: " ++ show code


viewMailE :: String -> ExceptT String IO ()
viewMailE ps =
    liftE (viewMail ps) >>= \case
        Right r -> return r
        Left code -> throwE $ "view mail error: " ++ show code


promptE :: String -> ExceptT String IO String
promptE ps =
    liftE (prompt ps) >>= \case
        Right r -> return r
        Left code -> throwE $ "prompt error: " ++ show code


loadMailE :: Notmuch.MessageID -> ExceptT String IO M.Mail
loadMailE msgId =
    liftE (Notmuch.notmuchShowMail $ Notmuch.unMessageID msgId) >>= \case
        Right m -> return m
        Left err -> throwE $ "load mail error: " ++ show err


attachFilesE :: [FilePath] -> M.Mail -> ExceptT String IO M.Mail
attachFilesE paths =
    liftE . attachFiles paths


attachFiles :: [FilePath] -> M.Mail -> IO M.Mail
attachFiles filenames mail0 =
    foldrM attachFile mail0 filenames


attachFile :: FilePath -> M.Mail -> IO M.Mail
attachFile filePath mail = do
    fileType <- fromMaybe "application/octet-stream" <$> getFileType filePath
    M.addAttachment (T.pack fileType) filePath mail


getFileType :: FilePath -> IO (Maybe String)
getFileType path =
    -- XXX GNU file's exit code is 0 even if path doesn't exist
    doesFileExist path >>= \case
        True -> do
            (_, out, _) <- readProcessWithExitCode "file" ["-Lib", path] ""
            return $ Just (init out)
        False ->
            return Nothing


replyToAll :: State -> IO State
replyToAll q0 =
    runExceptT (go q0) >>= \result ->
        toggleFold q0 { flashMessage = Plain (show result) }
  where
    go :: State -> ExceptT String IO String
    go State{cursor=cursor,now=now} = do
        msg <- maybe (throwE "not a message") return $
            getMessage (Z.label cursor)

        let msgId = Notmuch.unMessageID $ Notmuch.messageId msg

        newTags <-
            lines . LBS8.unpack . fst <$>
            readNotmuchE ["config", "get", "new.tags"] ""

        let tagOps =
                map AddTag ["draft"] ++
                map DelTag (map T.pack newTags)

        readNotmuchE ["reply", msgId] ""
            >>= return . LBS8.unpack . fst

            -- TODO abort if nothing has been edited
            >>= editMailE

            >>= return . T.pack
            >>= return . readMail
            >>= return . addDateHeader now
            >>= liftE . M.renderMail'
            >>= readNotmuchE ("insert" : "--no-hooks" : tagOpsToArgs tagOps)
            >>  return "draft created"


viewSource :: State -> IO State
viewSource q0 =
    runExceptT (go q0) >>= return . \result ->
        q0 { flashMessage = Plain (show result) }
  where
    go State{cursor=cursor} = do
        msg <- maybe (throwE "not a message") return $
            getMessage (Z.label cursor)

        liftE (readFile $ Notmuch.messageFilename msg) >>= viewMailE


editTagsAtCursor :: State -> IO State
editTagsAtCursor q0 =
    runExceptT (go q0) >>= return . \case
        Right q' -> q'
        Left err -> q0 { flashMessage = Plain $ "error: " ++ show err }
  where
    go :: State -> ExceptT String IO State
    go q@State{..} = do
        -- TODO does this scream for a type class? :)
        (searchTerm, tags, patch) <- case Z.label cursor of
            TVSearchResult sr -> return
                ( Notmuch.unThreadID $ Notmuch.searchThread sr
                , Notmuch.searchTags sr
                , patchSearchResult
                )
            TVMessage m -> return
                ( Notmuch.unMessageID $ Notmuch.messageId m
                , Notmuch.messageTags m
                , patchMessage
                )
            _ -> throwE "cannot edit tags here"

        tagOps <- editTagsE tags
        when (null tagOps) (throwE "nothing happened")

        _ <- readNotmuchE ("tag" : tagOpsToArgs tagOps ++ ["--", searchTerm]) ""

        return q { cursor = select (==Z.label cursor) (patch tagOps cursor) }


patchMessage
    :: [TagOp] -> Z.TreePos Z.Full TreeView -> Z.TreePos Z.Full TreeView
patchMessage tagOps loc =
    Z.modifyTree (patchTreeTags tagOps) loc


patchSearchResult
     :: [TagOp] -> Z.TreePos Z.Full TreeView -> Z.TreePos Z.Full TreeView
patchSearchResult tagOps loc =
    -- TODO this needs test cases
    let
        -- patch message
        loc' = Z.modifyTree (patchRootLabelTags tagOps) loc

        -- find search result of message
        srloc = fromMaybe (error "could not find search result of message")
                          (findParent isTVSearchResult loc')

        -- patch search result
        srloc' = Z.modifyTree (patchRootLabelTags tagOps) srloc
    in
        -- return message
        fromMaybe (error "could not find message again")
                  (findTree (==Z.label loc) srloc')


-- TODO rename select
select :: (a -> Bool) -> Z.TreePos Z.Full a -> Z.TreePos Z.Full a
select p loc =
    let root = Z.root loc
    in fromMaybe root $ findTree p root


editString :: State -> String -> IO (Either String String)
editString q s =
    withTempFile' ".string" $ \(path, h) -> do
        hPutStr stdout "\ESC[?1049h" -- TODO geht besser
        hPutStr stdout "\ESC[?25l" -- TODO war mal besser
        setFileMode path 0o600

        hPutStr h s

        hClose h

        runEditor' path q >>= \case
            ExitFailure code ->
                return . Left $ "error exit code = " <> show code
            ExitSuccess ->
                Right <$> readFile path



runEditor' :: [Char] -> State -> IO ExitCode
runEditor' path q@State{..} = do
    editor <- getEnv "EDITOR"
    system (editor ++ " " ++ path) <* resetTerm q


withTempFile' :: FilePath -> ((FilePath, Handle) -> IO a) -> IO a
withTempFile' s f = do
    logname <- getEnv "LOGNAME"
    tmpdir <- getTemporaryDirectory
    withTempFile tmpdir (logname ++ "_much_" ++ s) f


addDateHeader :: UTCTime -> M.Mail -> M.Mail
addDateHeader t m@M.Mail{..} = do
    m { M.mailHeaders =
          ( "Date"
          , T.pack $
            formatTime defaultTimeLocale
                       rfc822DateFormat
                       t
          ) :
          mailHeaders
      }

removeHeader :: CI BS.ByteString -> M.Mail -> M.Mail
removeHeader h m@M.Mail{..} =
    m { M.mailHeaders = filter (\(k, _) -> CI.mk k /= h) mailHeaders }

removeHeaders :: [CI BS.ByteString] -> M.Mail -> M.Mail
removeHeaders hs m@M.Mail{..} =
    m { M.mailHeaders = filter (\(k, _) -> CI.mk k `notElem` hs) mailHeaders }

editMail :: String -> IO (Either ExitCode String)
editMail s =
    withTempFile' "edit.mail" $ \(path, h_tempFile) -> do
        hPutStr h_tempFile s
        hClose h_tempFile
        editor <- getEnv "EDITOR"
        runInteractive editor [path] >>= \case
            ExitSuccess -> Right <$> readFile path
            code -> return (Left code)


editTags :: [Tag] -> IO (Either ExitCode [TagOp])
editTags tags =
    withTempFile' "edit.tags" $ \(path, h_tempFile) -> do
        T.hPutStrLn h_tempFile $ T.intercalate " " tags
        hClose h_tempFile
        editor <- getEnv "EDITOR"
        runInteractive editor [path] >>= \case
            ExitSuccess -> Right . diffTags tags . parseTags <$> readFile path
                                                   -- ^ TODO parseTags can fail
            code -> return (Left code)


viewMail :: String -> IO (Either ExitCode ())
viewMail s = do
    pager <- getEnv "PAGER"
    (Just h_in, _, _, h_proc) <-
        createProcess (shell pager) { std_in = CreatePipe }
    hPutStr h_in s
    hClose h_in
    waitForProcess h_proc >>= \case
        ExitSuccess -> return (Right ())
        code -> return (Left code)


prompt :: String -> IO (Either ExitCode String)
prompt ps =
    withTempFile' "prompt" $ \(path, h_tempFile) -> do
        mapM_ (hPutStrLn h_tempFile) $ "" : map comment (lines ps)
        hClose h_tempFile
        editor <- getEnv "EDITOR"
        runInteractive editor [path] >>= \case
            ExitSuccess -> Right . removeComments <$> readFile path
            code -> return (Left code)
  where
    comment = ("# "++)
    removeComments =
        unlines .
        filter (maybe True (/='#') . headMay) .
        lines


runInteractive :: FilePath -> [String] -> IO ExitCode
runInteractive cmd args = do
    (_, _, _, h_proc) <- createProcess (proc cmd args)
    waitForProcess h_proc