summaryrefslogtreecommitdiff
path: root/main/Main.hs
blob: d5b06eaca2884bfed677573c845763920832b079 (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
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Control.Arrow (first)
import Control.Concurrent
import Control.Exception
import Control.Monad
import Control.Monad.Except
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Trans.Maybe

import Crypto.Random

import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as BL
import Data.Char
import Data.List
import Data.Maybe
import Data.Ord
import Data.Text (Text)
import Data.Text qualified as T
import Data.Text.Encoding qualified as T
import Data.Text.IO qualified as T
import Data.Time.Format
import Data.Time.LocalTime
import Data.Typeable

import Network.Socket

import System.Console.GetOpt
import System.Console.Haskeline
import System.Environment
import System.Exit
import System.IO

import Erebos.Attach
import Erebos.Contact
import Erebos.Chatroom
import Erebos.Conversation
#ifdef ENABLE_ICE_SUPPORT
import Erebos.Discovery
import Erebos.ICE
#endif
import Erebos.Identity
import Erebos.Message hiding (formatMessage)
import Erebos.Network
import Erebos.PubKey
import Erebos.Service
import Erebos.Set
import Erebos.State
import Erebos.Storage
import Erebos.Storage.Merge
import Erebos.Sync

import Test
import Version

data Options = Options
    { optServer :: ServerOptions
    , optServices :: [ServiceOption]
    , optChatroomAutoSubscribe :: Maybe Int
    , optDmBotEcho :: Maybe Text
    , optShowHelp :: Bool
    , optShowVersion :: Bool
    }

data ServiceOption = ServiceOption
    { soptName :: String
    , soptService :: SomeService
    , soptEnabled :: Bool
    , soptDescription :: String
    }

defaultOptions :: Options
defaultOptions = Options
    { optServer = defaultServerOptions
    , optServices = availableServices
    , optChatroomAutoSubscribe = Nothing
    , optDmBotEcho = Nothing
    , optShowHelp = False
    , optShowVersion = False
    }

availableServices :: [ServiceOption]
availableServices =
    [ ServiceOption "attach" (someService @AttachService Proxy)
        True "attach (to) other devices"
    , ServiceOption "sync" (someService @SyncService Proxy)
        True "synchronization with attached devices"
    , ServiceOption "chatroom" (someService @ChatroomService Proxy)
        True "chatrooms with multiple participants"
    , ServiceOption "contact" (someService @ContactService Proxy)
        True "create contacts with network peers"
    , ServiceOption "dm" (someService @DirectMessage Proxy)
        True "direct messages"
#ifdef ENABLE_ICE_SUPPORT
    , ServiceOption "discovery" (someService @DiscoveryService Proxy)
        True "peer discovery"
#endif
    ]

options :: [OptDescr (Options -> Options)]
options =
    [ Option ['p'] ["port"]
        (ReqArg (\p -> so $ \opts -> opts { serverPort = read p }) "<port>")
        "local port to bind"
    , Option ['s'] ["silent"]
        (NoArg (so $ \opts -> opts { serverLocalDiscovery = False }))
        "do not send announce packets for local discovery"
    , Option [] ["chatroom-auto-subscribe"]
        (ReqArg (\count -> \opts -> opts { optChatroomAutoSubscribe = Just (read count) }) "<count>")
        "automatically subscribe for up to <count> chatrooms"
    , Option [] ["dm-bot-echo"]
        (ReqArg (\prefix -> \opts -> opts { optDmBotEcho = Just (T.pack prefix) }) "<prefix>")
        "automatically reply to direct messages with the same text prefixed with <prefix>"
    , Option ['h'] ["help"]
        (NoArg $ \opts -> opts { optShowHelp = True })
        "show this help and exit"
    , Option ['V'] ["version"]
        (NoArg $ \opts -> opts { optShowVersion = True })
        "show version and exit"
    ]
    where so f opts = opts { optServer = f $ optServer opts }

servicesOptions :: [OptDescr (Options -> Options)]
servicesOptions = concatMap helper $ "all" : map soptName availableServices
  where
    helper name =
        [ Option [] ["enable-" <> name] (NoArg $ so $ change name $ \sopt -> sopt { soptEnabled = True }) ""
        , Option [] ["disable-" <> name] (NoArg $ so $ change name $ \sopt -> sopt { soptEnabled = False }) ""
        ]
    so f opts = opts { optServices = f $ optServices opts }
    change :: String -> (ServiceOption -> ServiceOption) -> [ServiceOption] -> [ServiceOption]
    change name f (s : ss)
        | soptName s == name || name == "all"
                    = f s : change name f ss
        | otherwise =   s : change name f ss
    change _ _ []   = []

main :: IO ()
main = do
    st <- liftIO $ openStorage . fromMaybe "./.erebos" =<< lookupEnv "EREBOS_DIR"
    getArgs >>= \case
        ["cat-file", sref] -> do
            readRef st (BC.pack sref) >>= \case
                Nothing -> error "ref does not exist"
                Just ref -> BL.putStr $ lazyLoadBytes ref

        ("cat-file" : objtype : srefs@(_:_)) -> do
            sequence <$> (mapM (readRef st . BC.pack) srefs) >>= \case
                Nothing -> error "ref does not exist"
                Just refs -> case objtype of
                    "signed" -> forM_ refs $ \ref -> do
                        let signed = load ref :: Signed Object
                        BL.putStr $ lazyLoadBytes $ storedRef $ signedData signed
                        forM_ (signedSignature signed) $ \sig -> do
                            putStr $ "SIG "
                            BC.putStrLn $ showRef $ storedRef $ sigKey $ fromStored sig
                    "identity" -> case validateIdentityF (wrappedLoad <$> refs) of
                        Just identity -> do
                            let disp :: Identity m -> IO ()
                                disp idt = do
                                    maybe (return ()) (T.putStrLn . (T.pack "Name: " `T.append`)) $ idName idt
                                    BC.putStrLn . (BC.pack "KeyId: " `BC.append`) . showRefDigest . refDigest . storedRef $ idKeyIdentity idt
                                    BC.putStrLn . (BC.pack "KeyMsg: " `BC.append`) . showRefDigest . refDigest . storedRef $ idKeyMessage idt
                                    case idOwner idt of
                                         Nothing -> return ()
                                         Just owner -> do
                                             mapM_ (putStrLn . ("OWNER " ++) . BC.unpack . showRefDigest . refDigest . storedRef) $ idDataF owner
                                             disp owner
                            disp identity
                        Nothing -> putStrLn $ "Identity verification failed"
                    _ -> error $ "unknown object type '" ++ objtype ++ "'"

        ["show-generation", sref] -> readRef st (BC.pack sref) >>= \case
            Nothing -> error "ref does not exist"
            Just ref -> print $ storedGeneration (wrappedLoad ref :: Stored Object)

        ["update-identity"] -> either fail return <=< runExceptT $ do
            runReaderT updateSharedIdentity =<< loadLocalStateHead st

        ("update-identity" : srefs) -> do
            sequence <$> mapM (readRef st . BC.pack) srefs >>= \case
                Nothing -> error "ref does not exist"
                Just refs
                    | Just idt <- validateIdentityF $ map wrappedLoad refs -> do
                        BC.putStrLn . showRefDigest . refDigest . storedRef . idData =<<
                            (either fail return <=< runExceptT $ runReaderT (interactiveIdentityUpdate idt) st)
                    | otherwise -> error "invalid identity"

        ["test"] -> runTestTool st

        args -> case getOpt Permute (options ++ servicesOptions) args of
            (o, [], []) -> do
                let opts = foldl (flip id) defaultOptions o
                    header = "Usage: erebos [OPTION...]"
                    serviceDesc ServiceOption {..} = padService ("  " <> soptName) <> soptDescription

                    padTo n str = str <> replicate (n - length str) ' '
                    padOpt = padTo 37
                    padService = padTo 16

                if | optShowHelp opts -> putStr $ usageInfo header options <> unlines
                      (
                        [ padOpt "  --enable-<service>"  <> "enable network service <service>"
                        , padOpt "  --disable-<service>" <> "disable network service <service>"
                        , padOpt "  --enable-all"        <> "enable all network services"
                        , padOpt "  --disable-all"       <> "disable all network services"
                        , ""
                        , "Available network services:"
                        ] ++ map serviceDesc availableServices
                      )
                   | optShowVersion opts -> putStrLn versionLine
                   | otherwise -> interactiveLoop st opts
            (_, _, errs) -> do
                progName <- getProgName
                hPutStrLn stderr $ concat errs <> "Try `" <> progName <> " --help' for more information."
                exitFailure


inputSettings :: Settings IO
inputSettings = setComplete commandCompletion $ defaultSettings

interactiveLoop :: Storage -> Options -> IO ()
interactiveLoop st opts = runInputT inputSettings $ do
    erebosHead <- liftIO $ loadLocalStateHead st
    outputStrLn $ T.unpack $ displayIdentity $ headLocalIdentity erebosHead

    tui <- haveTerminalUI
    extPrint <- getExternalPrint
    let extPrintLn str = extPrint $ case reverse str of ('\n':_) -> str
                                                        _ -> str ++ "\n";

    let getInputLinesTui eprompt = do
            prompt <- case eprompt of
                Left cstate -> do
                    pname <- case csContext cstate of
                        NoContext -> return ""
                        SelectedPeer peer -> peerIdentity peer >>= return . \case
                            PeerIdentityFull pid -> maybe "<unnamed>" T.unpack $ idName $ finalOwner pid
                            PeerIdentityRef wref _ -> "<" ++ BC.unpack (showRefDigest $ wrDigest wref) ++ ">"
                            PeerIdentityUnknown _  -> "<unknown>"
                        SelectedContact contact -> return $ T.unpack $ contactName contact
                        SelectedChatroom rstate -> return $ T.unpack $ fromMaybe (T.pack "<unnamed>") $ roomName =<< roomStateRoom rstate
                        SelectedConversation conv -> return $ T.unpack $ conversationName conv
                    return $ pname ++ "> "
                Right prompt -> return prompt
            Just input <- lift $ getInputLine prompt
            case reverse input of
                 _ | all isSpace input -> getInputLinesTui eprompt
                 '\\':rest -> (reverse ('\n':rest) ++) <$> getInputLinesTui (Right ">> ")
                 _         -> return input

        getInputCommandTui cstate = do
            input <- getInputLinesTui cstate
            let (CommandM cmd, line) = case input of
                    '/':rest -> let (scmd, args) = dropWhile isSpace <$> span (\c -> isAlphaNum c || c == '-') rest
                                 in if not (null scmd) && all isDigit scmd
                                       then (cmdSelectContext, scmd)
                                       else (fromMaybe (cmdUnknown scmd) $ lookup scmd commands, args)
                    _        -> (cmdSend, input)
            return (cmd, line)

        getInputLinesPipe = do
            lift (getInputLine "") >>= \case
                Just input -> return input
                Nothing -> liftIO $ forever $ threadDelay 100000000

        getInputCommandPipe _ = do
            input <- getInputLinesPipe
            let (scmd, args) = dropWhile isSpace <$> span (\c -> isAlphaNum c || c == '-') input
            let (CommandM cmd, line) = (fromMaybe (cmdUnknown scmd) $ lookup scmd commands, args)
            return (cmd, line)

    let getInputCommand = if tui then getInputCommandTui . Left
                                 else getInputCommandPipe

    _ <- liftIO $ do
        tzone <- getCurrentTimeZone
        watchReceivedMessages erebosHead $ \smsg -> do
            let msg = fromStored smsg
            extPrintLn $ formatDirectMessage tzone msg
            case optDmBotEcho opts of
                Nothing -> return ()
                Just prefix -> do
                    res <- runExceptT $ flip runReaderT erebosHead $ sendDirectMessage (msgFrom msg) (prefix <> msgText msg)
                    case res of
                        Right reply -> extPrintLn $ formatDirectMessage tzone $ fromStored reply
                        Left err -> extPrintLn $ "Failed to send dm echo: " <> err

    peers <- liftIO $ newMVar []
    contextOptions <- liftIO $ newMVar []
    chatroomSetVar <- liftIO $ newEmptyMVar

    let autoSubscribe = optChatroomAutoSubscribe opts
        chatroomList = fromSetBy (comparing roomStateData) . lookupSharedValue . lsShared . headObject $ erebosHead
    watched <- if isJust autoSubscribe || any roomStateSubscribe chatroomList
        then fmap Just $ liftIO $ watchChatroomsForCli extPrintLn erebosHead chatroomSetVar contextOptions autoSubscribe
        else return Nothing

    server <- liftIO $ do
        startServer (optServer opts) erebosHead extPrintLn $
            map soptService $ filter soptEnabled $ optServices opts

    void $ liftIO $ forkIO $ void $ forever $ do
        peer <- getNextPeerChange server
        peerIdentity peer >>= \case
            pid@(PeerIdentityFull _) -> do
                dropped <- isPeerDropped peer
                let shown = showPeer pid $ peerAddress peer
                let update [] = ([(peer, shown)], (Nothing, "NEW"))
                    update ((p,s):ps)
                        | p == peer && dropped = (ps, (Nothing, "DEL"))
                        | p == peer = ((peer, shown) : ps, (Just s, "UPD"))
                        | otherwise = first ((p,s):) $ update ps
                let ctxUpdate n [] = ([SelectedPeer peer], n)
                    ctxUpdate n (ctx:ctxs)
                        | SelectedPeer p <- ctx, p == peer = (ctx:ctxs, n)
                        | otherwise = first (ctx:) $ ctxUpdate (n + 1) ctxs
                (op, updateType) <- modifyMVar peers (return . update)
                let updateType' = if dropped then "DEL" else updateType
                idx <- modifyMVar contextOptions (return . ctxUpdate (1 :: Int))
                when (Just shown /= op) $ extPrintLn $ "[" <> show idx <> "] PEER " <> updateType' <> " " <> shown
            _ -> return ()

    let process :: CommandState -> MaybeT (InputT IO) CommandState
        process cstate = do
            (cmd, line) <- getInputCommand cstate
            h <- liftIO (reloadHead $ csHead cstate) >>= \case
                Just h  -> return h
                Nothing -> do lift $ lift $ extPrintLn "current head deleted"
                              mzero
            res <- liftIO $ runExceptT $ flip execStateT cstate { csHead = h } $ runReaderT cmd CommandInput
                { ciServer = server
                , ciLine = line
                , ciPrint = extPrintLn
                , ciOptions = opts
                , ciPeers = liftIO $ modifyMVar peers $ \ps -> do
                    ps' <- filterM (fmap not . isPeerDropped . fst) ps
                    return (ps', ps')
                , ciContextOptions = liftIO $ readMVar contextOptions
                , ciSetContextOptions = \ctxs -> liftIO $ modifyMVar_ contextOptions $ const $ return ctxs
                , ciContextOptionsVar = contextOptions
                , ciChatroomSetVar = chatroomSetVar
                }
            case res of
                Right cstate'
                    | csQuit cstate' -> mzero
                    | otherwise      -> return cstate'
                Left err -> do
                    lift $ lift $ extPrintLn $ "Error: " ++ err
                    return cstate

    let loop (Just cstate) = runMaybeT (process cstate) >>= loop
        loop Nothing = return ()
    loop $ Just $ CommandState
        { csHead = erebosHead
        , csContext = NoContext
#ifdef ENABLE_ICE_SUPPORT
        , csIceSessions = []
#endif
        , csIcePeer = Nothing
        , csWatchChatrooms = watched
        , csQuit = False
        }


data CommandInput = CommandInput
    { ciServer :: Server
    , ciLine :: String
    , ciPrint :: String -> IO ()
    , ciOptions :: Options
    , ciPeers :: CommandM [(Peer, String)]
    , ciContextOptions :: CommandM [CommandContext]
    , ciSetContextOptions :: [CommandContext] -> Command
    , ciContextOptionsVar :: MVar [ CommandContext ]
    , ciChatroomSetVar :: MVar (Set ChatroomState)
    }

data CommandState = CommandState
    { csHead :: Head LocalState
    , csContext :: CommandContext
#ifdef ENABLE_ICE_SUPPORT
    , csIceSessions :: [IceSession]
#endif
    , csIcePeer :: Maybe Peer
    , csWatchChatrooms :: Maybe WatchedHead
    , csQuit :: Bool
    }

data CommandContext = NoContext
                    | SelectedPeer Peer
                    | SelectedContact Contact
                    | SelectedChatroom ChatroomState
                    | SelectedConversation Conversation

newtype CommandM a = CommandM (ReaderT CommandInput (StateT CommandState (ExceptT String IO)) a)
    deriving (Functor, Applicative, Monad, MonadReader CommandInput, MonadState CommandState, MonadError String)

instance MonadFail CommandM where
    fail = throwError

instance MonadIO CommandM where
    liftIO act = CommandM (liftIO (try act)) >>= \case
        Left (e :: SomeException) -> throwError (show e)
        Right x -> return x

instance MonadRandom CommandM where
    getRandomBytes = liftIO . getRandomBytes

instance MonadStorage CommandM where
    getStorage = gets $ headStorage . csHead

instance MonadHead LocalState CommandM where
    updateLocalHead f = do
        h <- gets csHead
        (Just h', x) <- maybe (fail "failed to reload head") (flip updateHead f) =<< reloadHead h
        modify $ \s -> s { csHead = h' }
        return x

type Command = CommandM ()

getSelectedPeer :: CommandM Peer
getSelectedPeer = gets csContext >>= \case
    SelectedPeer peer -> return peer
    _ -> throwError "no peer selected"

getSelectedConversation :: CommandM Conversation
getSelectedConversation = gets csContext >>= \case
    SelectedPeer peer -> peerIdentity peer >>= \case
        PeerIdentityFull pid -> directMessageConversation $ finalOwner pid
        _ -> throwError "incomplete peer identity"
    SelectedContact contact -> case contactIdentity contact of
        Just cid -> directMessageConversation cid
        Nothing -> throwError "contact without erebos identity"
    SelectedChatroom rstate ->
        chatroomConversation rstate >>= \case
            Just conv -> return conv
            Nothing -> throwError "invalid chatroom"
    SelectedConversation conv -> reloadConversation conv
    _ -> throwError "no contact, peer or conversation selected"

commands :: [(String, Command)]
commands =
    [ ("history", cmdHistory)
    , ("peers", cmdPeers)
    , ("peer-add", cmdPeerAdd)
    , ("peer-add-public", cmdPeerAddPublic)
    , ("peer-drop", cmdPeerDrop)
    , ("send", cmdSend)
    , ("update-identity", cmdUpdateIdentity)
    , ("attach", cmdAttach)
    , ("attach-accept", cmdAttachAccept)
    , ("attach-reject", cmdAttachReject)
    , ("chatrooms", cmdChatrooms)
    , ("chatroom-create-public", cmdChatroomCreatePublic)
    , ("contacts", cmdContacts)
    , ("contact-add", cmdContactAdd)
    , ("contact-accept", cmdContactAccept)
    , ("contact-reject", cmdContactReject)
    , ("conversations", cmdConversations)
    , ("details", cmdDetails)
#ifdef ENABLE_ICE_SUPPORT
    , ("discovery-init", cmdDiscoveryInit)
    , ("discovery", cmdDiscovery)
    , ("ice-create", cmdIceCreate)
    , ("ice-destroy", cmdIceDestroy)
    , ("ice-show", cmdIceShow)
    , ("ice-connect", cmdIceConnect)
    , ("ice-send", cmdIceSend)
#endif
    , ("select", cmdSelectContext)
    , ("quit", cmdQuit)
    ]

commandCompletion :: CompletionFunc IO
commandCompletion = completeWordWithPrev Nothing [ ' ', '\t', '\n', '\r' ] $ curry $ \case
    ([], '/':pref) -> return . map (simpleCompletion . ('/':)) . filter (pref `isPrefixOf`) $ sortedCommandNames
    _ -> return []
  where
    sortedCommandNames = sort $ map fst commands


cmdUnknown :: String -> Command
cmdUnknown cmd = liftIO $ putStrLn $ "Unknown command: " ++ cmd

cmdPeers :: Command
cmdPeers = do
    peers <- join $ asks ciPeers
    set <- asks ciSetContextOptions
    set $ map (SelectedPeer . fst) peers
    forM_ (zip [1..] peers) $ \(i :: Int, (_, name)) -> do
        liftIO $ putStrLn $ "[" ++ show i ++ "] " ++ name

cmdPeerAdd :: Command
cmdPeerAdd = void $ do
    server <- asks ciServer
    (hostname, port) <- (words <$> asks ciLine) >>= \case
        hostname:p:_ -> return (hostname, p)
        [hostname] -> return (hostname, show discoveryPort)
        [] -> throwError "missing peer address"
    addr:_ <- liftIO $ getAddrInfo (Just $ defaultHints { addrSocketType = Datagram }) (Just hostname) (Just port)
    liftIO $ serverPeer server (addrAddress addr)

cmdPeerAddPublic :: Command
cmdPeerAddPublic = do
    server <- asks ciServer
    addr:_ <- liftIO $ getAddrInfo (Just $ defaultHints { addrSocketType = Datagram }) (Just "discovery1.erebosprotocol.net") (Just (show discoveryPort))
    void $ liftIO $ serverPeer server (addrAddress addr)

cmdPeerDrop :: Command
cmdPeerDrop = do
    dropPeer =<< getSelectedPeer
    modify $ \s -> s { csContext = NoContext }

showPeer :: PeerIdentity -> PeerAddress -> String
showPeer pidentity paddr =
    let name = case pidentity of
                    PeerIdentityUnknown _  -> "<noid>"
                    PeerIdentityRef wref _ -> "<" ++ BC.unpack (showRefDigest $ wrDigest wref) ++ ">"
                    PeerIdentityFull pid   -> T.unpack $ displayIdentity pid
     in name ++ " [" ++ show paddr ++ "]"

cmdSelectContext :: Command
cmdSelectContext = do
    n <- read <$> asks ciLine
    join (asks ciContextOptions) >>= \ctxs -> if
        | n > 0, (ctx : _) <- drop (n - 1) ctxs -> do
            modify $ \s -> s { csContext = ctx }
            case ctx of
                SelectedChatroom rstate -> do
                    when (not (roomStateSubscribe rstate)) $ do
                        chatroomSetSubscribe (head $ roomStateData rstate) True
                _ -> return ()
        | otherwise -> throwError "invalid index"

cmdSend :: Command
cmdSend = void $ do
    text <- asks ciLine
    conv <- getSelectedConversation
    sendMessage conv (T.pack text) >>= \case
        Just msg -> do
            tzone <- liftIO $ getCurrentTimeZone
            liftIO $ putStrLn $ formatMessage tzone msg
        Nothing -> return ()

cmdHistory :: Command
cmdHistory = void $ do
    conv <- getSelectedConversation
    case conversationHistory conv of
        thread@(_:_) -> do
            tzone <- liftIO $ getCurrentTimeZone
            liftIO $ mapM_ (putStrLn . formatMessage tzone) $ reverse $ take 50 thread
        [] -> do
            liftIO $ putStrLn $ "<empty history>"

cmdUpdateIdentity :: Command
cmdUpdateIdentity = void $ do
    runReaderT updateSharedIdentity =<< gets csHead

cmdAttach :: Command
cmdAttach = attachToOwner =<< getSelectedPeer

cmdAttachAccept :: Command
cmdAttachAccept = attachAccept =<< getSelectedPeer

cmdAttachReject :: Command
cmdAttachReject = attachReject =<< getSelectedPeer

watchChatroomsForCli :: (String -> IO ()) -> Head LocalState -> MVar (Set ChatroomState) -> MVar [ CommandContext ] -> Maybe Int -> IO WatchedHead
watchChatroomsForCli eprint h chatroomSetVar contextVar autoSubscribe = do
    subscribedNumVar <- newEmptyMVar

    let ctxUpdate updateType (idx :: Int) rstate = \case
            SelectedChatroom rstate' : rest
                | currentRoots <- filterAncestors (concatMap storedRoots $ roomStateData rstate)
                , any ((`intersectsSorted` currentRoots) . storedRoots) $ roomStateData rstate'
                -> do
                    eprint $ "[" <> show idx <> "] CHATROOM " <> updateType <> " " <> name
                    return (SelectedChatroom rstate : rest)
            selected : rest
                -> do
                    (selected : ) <$> ctxUpdate updateType (idx + 1) rstate rest
            []
                -> do
                    eprint $ "[" <> show idx <> "] CHATROOM " <> updateType <> " " <> name
                    return [ SelectedChatroom rstate ]
          where
            name = maybe "<unnamed>" T.unpack $ roomName =<< roomStateRoom rstate

    watchChatrooms h $ \set -> \case
        Nothing -> do
            let chatroomList = fromSetBy (comparing roomStateData) set
                (subscribed, notSubscribed) = partition roomStateSubscribe chatroomList
                subscribedNum = length subscribed

            putMVar chatroomSetVar set
            putMVar subscribedNumVar subscribedNum

            case autoSubscribe of
                Nothing -> return ()
                Just num -> do
                    forM_ (take (num - subscribedNum) notSubscribed) $ \rstate -> do
                        (runExceptT $ flip runReaderT h $ chatroomSetSubscribe (head $ roomStateData rstate) True) >>= \case
                             Right () -> return ()
                             Left err -> eprint err

        Just diff -> do
            modifyMVar_ chatroomSetVar $ return . const set
            forM_ diff $ \case
                AddedChatroom rstate -> do
                    modifyMVar_ contextVar $ ctxUpdate "NEW" 1 rstate
                    modifyMVar_ subscribedNumVar $ return . if roomStateSubscribe rstate then (+ 1) else id

                RemovedChatroom rstate -> do
                    modifyMVar_ contextVar $ ctxUpdate "DEL" 1 rstate
                    modifyMVar_ subscribedNumVar $ return . if roomStateSubscribe rstate then subtract 1 else id

                UpdatedChatroom oldroom rstate -> do
                    when (any ((\rsd -> not (null (rsdRoom rsd))) . fromStored) (roomStateData rstate)) $ do
                        modifyMVar_ contextVar $ ctxUpdate "UPD" 1 rstate
                    when (any (not . null . rsdMessages . fromStored) (roomStateData rstate)) $ do
                        tzone <- getCurrentTimeZone
                        forM_ (reverse $ getMessagesSinceState rstate oldroom) $ \msg -> do
                            eprint $ concat $
                                [ maybe "<unnamed>" T.unpack $ roomName =<< cmsgRoom msg
                                , formatTime defaultTimeLocale " [%H:%M] " $ utcToLocalTime tzone $ zonedTimeToUTC $ cmsgTime msg
                                , maybe "<unnamed>" T.unpack $ idName $ cmsgFrom msg
                                , ": "
                                , maybe "<no message>" T.unpack $ cmsgText msg
                                ]
                    modifyMVar_ subscribedNumVar $ return
                        . (if roomStateSubscribe rstate then (+ 1) else id)
                        . (if roomStateSubscribe oldroom then subtract 1 else id)

ensureWatchedChatrooms :: Command
ensureWatchedChatrooms = do
    gets csWatchChatrooms >>= \case
        Nothing -> do
            eprint <- asks ciPrint
            h <- gets csHead
            chatroomSetVar <- asks ciChatroomSetVar
            contextVar <- asks ciContextOptionsVar
            autoSubscribe <- asks $ optChatroomAutoSubscribe . ciOptions
            watched <- liftIO $ watchChatroomsForCli eprint h chatroomSetVar contextVar autoSubscribe
            modify $ \s -> s { csWatchChatrooms = Just watched }
        Just _ -> return ()

cmdChatrooms :: Command
cmdChatrooms = do
    ensureWatchedChatrooms
    chatroomSetVar <- asks ciChatroomSetVar
    chatroomList <- fromSetBy (comparing roomStateData) <$> liftIO (readMVar chatroomSetVar)
    set <- asks ciSetContextOptions
    set $ map SelectedChatroom chatroomList
    forM_ (zip [1..] chatroomList) $ \(i :: Int, rstate) -> do
        liftIO $ putStrLn $ "[" ++ show i ++ "] " ++ maybe "<unnamed>" T.unpack (roomName =<< roomStateRoom rstate)

cmdChatroomCreatePublic :: Command
cmdChatroomCreatePublic = do
    name <- asks ciLine >>= \case
        line | not (null line) -> return $ T.pack line
        _ -> liftIO $ do
            T.putStr $ T.pack "Name: "
            hFlush stdout
            T.getLine

    ensureWatchedChatrooms
    void $ createChatroom
        (if T.null name then Nothing else Just name)
        Nothing


cmdContacts :: Command
cmdContacts = do
    args <- words <$> asks ciLine
    ehead <- gets csHead
    let contacts = fromSetBy (comparing contactName) $ lookupSharedValue $ lsShared $ headObject ehead
        verbose = "-v" `elem` args
    set <- asks ciSetContextOptions
    set $ map SelectedContact contacts
    forM_ (zip [1..] contacts) $ \(i :: Int, c) -> liftIO $ do
        T.putStrLn $ T.concat
            [ "[", T.pack (show i), "] ", contactName c
            , case contactIdentity c of
                   Just idt | cname <- displayIdentity idt
                            , cname /= contactName c
                            -> " (" <> cname <> ")"
                   _ -> ""
            , if verbose then " " <> (T.unwords $ map (T.decodeUtf8 . showRef . storedRef) $ maybe [] idDataF $ contactIdentity c)
                         else ""
            ]

cmdContactAdd :: Command
cmdContactAdd = contactRequest =<< getSelectedPeer

cmdContactAccept :: Command
cmdContactAccept = contactAccept =<< getSelectedPeer

cmdContactReject :: Command
cmdContactReject = contactReject =<< getSelectedPeer

cmdConversations :: Command
cmdConversations = do
    conversations <- lookupConversations
    set <- asks ciSetContextOptions
    set $ map SelectedConversation conversations
    forM_ (zip [1..] conversations) $ \(i :: Int, conv) -> do
        liftIO $ putStrLn $ "[" ++ show i ++ "] " ++ T.unpack (conversationName conv)

cmdDetails :: Command
cmdDetails = do
    gets csContext >>= \case
        SelectedPeer peer -> do
            liftIO $ putStr $ unlines
                [ "Network peer:"
                , "  " <> show (peerAddress peer)
                ]
            peerIdentity peer >>= \case
                PeerIdentityUnknown _ -> liftIO $ do
                    putStrLn $ "unknown identity"
                PeerIdentityRef wref _ -> liftIO $ do
                    putStrLn $ "Identity ref:"
                    putStrLn $ "  " <> BC.unpack (showRefDigest $ wrDigest wref)
                PeerIdentityFull pid -> printContactOrIdentityDetails pid

        SelectedContact contact -> do
            printContactDetails contact

        SelectedChatroom rstate -> do
            liftIO $ putStrLn $ "Chatroom: " <> (T.unpack $ fromMaybe (T.pack "<unnamed>") $ roomName =<< roomStateRoom rstate)

        SelectedConversation conv -> do
            case conversationPeer conv of
                Just pid -> printContactOrIdentityDetails pid
                Nothing -> liftIO $ putStrLn $ "(conversation without peer)"

        NoContext -> liftIO $ putStrLn "nothing selected"
  where
    printContactOrIdentityDetails cid = do
        contacts <- fromSetBy (comparing contactName) . lookupSharedValue . lsShared . fromStored <$> getLocalHead
        case find (maybe False (sameIdentity cid) . contactIdentity) contacts of
            Just contact -> printContactDetails contact
            Nothing -> printIdentityDetails cid

    printContactDetails contact = liftIO $ do
        putStrLn $ "Contact:"
        prefix <- case contactCustomName contact of
            Just name -> do
                putStrLn $ "  " <> T.unpack name
                return $ Just "alias of"
            Nothing -> do
                return $ Nothing

        case contactIdentity contact of
            Just cid -> do
                printIdentityDetailsBody prefix cid
            Nothing -> do
                putStrLn $ "  (without erebos identity)"

    printIdentityDetails identity = liftIO $ do
        putStrLn $ "Identity:"
        printIdentityDetailsBody Nothing identity

    printIdentityDetailsBody prefix identity = do
        forM_ (zip (False : repeat True) $ unfoldOwners identity) $ \(owned, cpid) -> do
            putStrLn $ unwords $ concat
                [ [ "  " ]
                , if owned then [ "owned by" ] else maybeToList prefix
                , [ maybe "<unnamed>" T.unpack (idName cpid) ]
                , map (BC.unpack . showRefDigest . refDigest . storedRef) $ idExtDataF cpid
                ]

#ifdef ENABLE_ICE_SUPPORT

cmdDiscoveryInit :: Command
cmdDiscoveryInit = void $ do
    server <- asks ciServer

    (hostname, port) <- (words <$> asks ciLine) >>= return . \case
        hostname:p:_ -> (hostname, p)
        [hostname] -> (hostname, show discoveryPort)
        [] -> ("discovery.erebosprotocol.net", show discoveryPort)
    addr:_ <- liftIO $ getAddrInfo (Just $ defaultHints { addrSocketType = Datagram }) (Just hostname) (Just port)
    peer <- liftIO $ serverPeer server (addrAddress addr)
    sendToPeer peer $ DiscoverySelf (T.pack "ICE") 0
    modify $ \s -> s { csIcePeer = Just peer }

cmdDiscovery :: Command
cmdDiscovery = void $ do
    Just peer <- gets csIcePeer
    st <- getStorage
    sref <- asks ciLine
    eprint <- asks ciPrint
    liftIO $ readRef st (BC.pack sref) >>= \case
        Nothing -> error "ref does not exist"
        Just ref -> do
            res <- runExceptT $ sendToPeer peer $ DiscoverySearch ref
            case res of
                 Right _ -> return ()
                 Left err -> eprint err

cmdIceCreate :: Command
cmdIceCreate = do
    role <- asks ciLine >>= return . \case
        'm':_ -> PjIceSessRoleControlling
        's':_ -> PjIceSessRoleControlled
        _ -> PjIceSessRoleUnknown
    eprint <- asks ciPrint
    sess <- liftIO $ iceCreate role $ eprint <=< iceShow
    modify $ \s -> s { csIceSessions = sess : csIceSessions s }

cmdIceDestroy :: Command
cmdIceDestroy = do
    s:ss <- gets csIceSessions
    modify $ \st -> st { csIceSessions = ss }
    liftIO $ iceDestroy s

cmdIceShow :: Command
cmdIceShow = do
    sess <- gets csIceSessions
    eprint <- asks ciPrint
    liftIO $ forM_ (zip [1::Int ..] sess) $ \(i, s) -> do
        eprint $ "[" ++ show i ++ "]"
        eprint =<< iceShow s

cmdIceConnect :: Command
cmdIceConnect = do
    s:_ <- gets csIceSessions
    server <- asks ciServer
    let loadInfo = BC.getLine >>= \case line | BC.null line -> return []
                                             | otherwise    -> (line:) <$> loadInfo
    Right remote <- liftIO $ do
        st <- memoryStorage
        pst <- derivePartialStorage st
        rbytes <- (BL.fromStrict . BC.unlines) <$> loadInfo
        copyRef st =<< storeRawBytes pst (BL.fromChunks [ BC.pack "rec ", BC.pack (show (BL.length rbytes)), BC.singleton '\n' ] `BL.append` rbytes)
    liftIO $ iceConnect s (load remote) $ void $ serverPeerIce server s

cmdIceSend :: Command
cmdIceSend = void $ do
    s:_ <- gets csIceSessions
    server <- asks ciServer
    liftIO $ serverPeerIce server s

#endif

cmdQuit :: Command
cmdQuit = modify $ \s -> s { csQuit = True }


intersectsSorted :: Ord a => [a] -> [a] -> Bool
intersectsSorted (x:xs) (y:ys) | x < y     = intersectsSorted xs (y:ys)
                               | x > y     = intersectsSorted (x:xs) ys
                               | otherwise = True
intersectsSorted _ _ = False