diff options
-rw-r--r-- | examples/dhtd.hs | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs index 088e0c67..9e7f4582 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs | |||
@@ -159,8 +159,7 @@ data DHTAnnouncable nid = forall dta tok ni r. | |||
159 | { announceParseData :: String -> Either String dta | 159 | { announceParseData :: String -> Either String dta |
160 | , announceParseToken :: dta -> String -> Either String tok | 160 | , announceParseToken :: dta -> String -> Either String tok |
161 | , announceParseAddress :: String -> Either String ni | 161 | , announceParseAddress :: String -> Either String ni |
162 | , announceSendData :: Either (dta -> r -> IO (Maybe r)) -- TODO ( String {- search name -} | 162 | , announceSendData :: Either ( String {- search name -} , PublicKey {- me -} -> dta -> r -> IO ()) |
163 | -- , PublicKey {- me -} -> dta -> r -> IO ()) | ||
164 | (dta -> tok -> Maybe ni -> IO (Maybe r)) | 163 | (dta -> tok -> Maybe ni -> IO (Maybe r)) |
165 | , announceInterval :: POSIXTime | 164 | , announceInterval :: POSIXTime |
166 | , announceTarget :: dta -> nid | 165 | , announceTarget :: dta -> nid |
@@ -383,6 +382,7 @@ data ConnectionManager = forall status k. ConnectionManager { typedManager :: Co | |||
383 | 382 | ||
384 | data Session = Session | 383 | data Session = Session |
385 | { netname :: String | 384 | { netname :: String |
385 | , selectedKey :: Maybe PublicKey | ||
386 | , dhts :: Map.Map String DHT | 386 | , dhts :: Map.Map String DHT |
387 | , externalAddresses :: IO [SockAddr] | 387 | , externalAddresses :: IO [SockAddr] |
388 | , swarms :: Mainline.SwarmsDatabase | 388 | , swarms :: Mainline.SwarmsDatabase |
@@ -433,6 +433,7 @@ clientSession s@Session{..} sock cnum h = do | |||
433 | cmd0 action = exceptionsToClient h action >> clientSession s sock cnum h | 433 | cmd0 action = exceptionsToClient h action >> clientSession s sock cnum h |
434 | switchNetwork dest = do hPutClient h ("Network: "++dest) | 434 | switchNetwork dest = do hPutClient h ("Network: "++dest) |
435 | clientSession s{netname=dest} sock cnum h | 435 | clientSession s{netname=dest} sock cnum h |
436 | switchKey key = clientSession s { selectedKey = key } sock cnum h | ||
436 | strp = B.unpack . fst . until snd dropEnd . (,False) . B.dropWhile isSpace . B.pack | 437 | strp = B.unpack . fst . until snd dropEnd . (,False) . B.dropWhile isSpace . B.pack |
437 | where | 438 | where |
438 | dropEnd (x,_) = | 439 | dropEnd (x,_) = |
@@ -567,8 +568,10 @@ clientSession s@Session{..} sock cnum h = do | |||
567 | 568 | ||
568 | ("k", s) | "" <- strp s -> cmd0 $ do | 569 | ("k", s) | "" <- strp s -> cmd0 $ do |
569 | ks <- atomically $ readTVar userkeys | 570 | ks <- atomically $ readTVar userkeys |
570 | hPutClient h $ unlines $ map (mappend " " . show . Tox.key2id . snd) ks | 571 | let spaces k | Just sel <- selectedKey, (sel == k) = " *" |
571 | | "gen" <- strp s -> cmd0 $ do | 572 | | otherwise = " " |
573 | hPutClient h $ unlines $ map (\(_,k) -> mappend (spaces k) . show . Tox.key2id $ k) ks | ||
574 | | "gen" <- strp s -> do | ||
572 | secret <- generateSecretKey | 575 | secret <- generateSecretKey |
573 | let pubkey = toPublic secret | 576 | let pubkey = toPublic secret |
574 | oldks <- atomically $ do | 577 | oldks <- atomically $ do |
@@ -579,6 +582,7 @@ clientSession s@Session{..} sock cnum h = do | |||
579 | let asString = show . Tox.key2id | 582 | let asString = show . Tox.key2id |
580 | hPutClient h $ unlines $ map (mappend " " . show . Tox.key2id . snd) oldks | 583 | hPutClient h $ unlines $ map (mappend " " . show . Tox.key2id . snd) oldks |
581 | ++ [mappend " *" . show . Tox.key2id $ pubkey] | 584 | ++ [mappend " *" . show . Tox.key2id $ pubkey] |
585 | switchKey $ Just pubkey | ||
582 | | "secrets" <- strp s -> cmd0 $ do | 586 | | "secrets" <- strp s -> cmd0 $ do |
583 | ks <- atomically $ readTVar userkeys | 587 | ks <- atomically $ readTVar userkeys |
584 | skey <- maybe (return Nothing) (atomically . dhtSecretKey) | 588 | skey <- maybe (return Nothing) (atomically . dhtSecretKey) |
@@ -586,10 +590,16 @@ clientSession s@Session{..} sock cnum h = do | |||
586 | hPutClient h . showReport $ map mkrow ks ++ case skey >>= encodeSecret of | 590 | hPutClient h . showReport $ map mkrow ks ++ case skey >>= encodeSecret of |
587 | Just x -> [("",""),("dht-key:",B.unpack x)] | 591 | Just x -> [("",""),("dht-key:",B.unpack x)] |
588 | Nothing -> [] | 592 | Nothing -> [] |
589 | -- TODO | ("sel":secs) <- words s -> select active key | 593 | | ("sel",_:expr) <- break isSpace s -> do |
594 | ks <- atomically $ map (show . Tox.key2id . snd) <$> readTVar userkeys | ||
595 | case find (isInfixOf expr) ks of | ||
596 | Just k -> do | ||
597 | hPutClient h $ "Selected key: "++k | ||
598 | switchKey $ Just $ Tox.id2key $ read k | ||
599 | Nothing -> cmd0 $ hPutClient h "no match." | ||
590 | | ("add":secs) <- words s | 600 | | ("add":secs) <- words s |
591 | , mbSecs <- map (decodeSecret . B.pack) secs | 601 | , mbSecs <- map (decodeSecret . B.pack) secs |
592 | , all isJust mbSecs -> cmd0 $ do | 602 | , all isJust mbSecs -> do |
593 | let f (Just b) = b | 603 | let f (Just b) = b |
594 | f x = error (concat ["Assertion fail at ", __FILE__, ":", show __LINE__]) | 604 | f x = error (concat ["Assertion fail at ", __FILE__, ":", show __LINE__]) |
595 | let toPair x = (x,toPublic x) | 605 | let toPair x = (x,toPublic x) |
@@ -601,9 +611,10 @@ clientSession s@Session{..} sock cnum h = do | |||
601 | return oldks | 611 | return oldks |
602 | hPutClient h $ unlines $ map (mappend " " . show . Tox.key2id . snd) oldks | 612 | hPutClient h $ unlines $ map (mappend " " . show . Tox.key2id . snd) oldks |
603 | ++ map (mappend " *" . show . Tox.key2id .snd) pairs | 613 | ++ map (mappend " *" . show . Tox.key2id .snd) pairs |
614 | switchKey $ listToMaybe $ map snd pairs | ||
604 | | ("del":secs) <- words s | 615 | | ("del":secs) <- words s |
605 | , mbSecs <- map (decodeSecret . B.pack) secs | 616 | , mbSecs <- map (decodeSecret . B.pack) secs |
606 | , all isJust mbSecs -> cmd0 $ do | 617 | , all isJust mbSecs -> do |
607 | let f (Just b) = b | 618 | let f (Just b) = b |
608 | f x = error (concat ["Assertion fail at ", __FILE__, ":", show __LINE__]) | 619 | f x = error (concat ["Assertion fail at ", __FILE__, ":", show __LINE__]) |
609 | let toPair x = (x,toPublic x) | 620 | let toPair x = (x,toPublic x) |
@@ -613,6 +624,10 @@ clientSession s@Session{..} sock cnum h = do | |||
613 | forM pairs $ \(_,pk) -> Tox.delContactInfo roster pk | 624 | forM pairs $ \(_,pk) -> Tox.delContactInfo roster pk |
614 | readTVar userkeys | 625 | readTVar userkeys |
615 | hPutClient h . showReport $ map mkrow ks | 626 | hPutClient h . showReport $ map mkrow ks |
627 | switchKey $ do | ||
628 | k <- selectedKey | ||
629 | guard $ k `notElem` map snd pairs | ||
630 | Just k | ||
616 | 631 | ||
617 | ("roster", s) -> cmd0 $ join $ atomically $ do | 632 | ("roster", s) -> cmd0 $ join $ atomically $ do |
618 | dns <- dnsPresentation roster | 633 | dns <- dnsPresentation roster |
@@ -1418,6 +1433,7 @@ main = runResourceT $ liftBaseWith $ \resT -> do | |||
1418 | return $ return ( userKeys (Tox.toxCryptoKeys tox), Tox.toxContactInfo tox, Tox.toxOnionRoutes tox ) | 1433 | return $ return ( userKeys (Tox.toxCryptoKeys tox), Tox.toxContactInfo tox, Tox.toxOnionRoutes tox ) |
1419 | let session = clientSession0 $ Session | 1434 | let session = clientSession0 $ Session |
1420 | { netname = concat $ take 1 $ Map.keys dhts -- initial default DHT | 1435 | { netname = concat $ take 1 $ Map.keys dhts -- initial default DHT |
1436 | , selectedKey = Nothing | ||
1421 | , dhts = dhts -- all DHTs | 1437 | , dhts = dhts -- all DHTs |
1422 | , signalQuit = quitCommand | 1438 | , signalQuit = quitCommand |
1423 | , swarms = swarms | 1439 | , swarms = swarms |