summaryrefslogtreecommitdiff
path: root/examples/dhtd.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-05-30 23:53:44 -0400
committerjoe <joe@jerkface.net>2018-05-30 23:53:44 -0400
commit514d0cad3f2ccaf0e89aadb4ab3067884ec20a6c (patch)
treea7d15e0d3592f123b10f2da3a458c023bd74de5b /examples/dhtd.hs
parent2cffde93a0d814ebb54bdcbd3d6e598cbaae6ee1 (diff)
tox: Removed obsolete "userKeys" TVar, use keys from ContactInfo.
Diffstat (limited to 'examples/dhtd.hs')
-rw-r--r--examples/dhtd.hs35
1 files changed, 11 insertions, 24 deletions
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index b24a90fb..09817d4e 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -412,7 +412,6 @@ data Session = Session
412 , swarms :: Mainline.SwarmsDatabase 412 , swarms :: Mainline.SwarmsDatabase
413 , cryptosessions :: Tox.NetCryptoSessions 413 , cryptosessions :: Tox.NetCryptoSessions
414 , toxkeys :: TVar Tox.AnnouncedKeys 414 , toxkeys :: TVar Tox.AnnouncedKeys
415 , userkeys :: TVar [(SecretKey,PublicKey)]
416 , roster :: Tox.ContactInfo 415 , roster :: Tox.ContactInfo
417 , announceToLan :: IO () 416 , announceToLan :: IO ()
418 , connectionManager :: Maybe ConnectionManager 417 , connectionManager :: Maybe ConnectionManager
@@ -451,13 +450,6 @@ clientSession0 s sock cnum h = do
451 `catch` \e -> if isEOFError e then return () 450 `catch` \e -> if isEOFError e then return ()
452 else throwIO e 451 else throwIO e
453 452
454readKeys :: TVar [(SecretKey, PublicKey)]
455 -> TVar (HashMap.HashMap Tox.NodeId Account) -- ContactInfo { accounts }
456 -> STM [(SecretKey, PublicKey)]
457readKeys userkeys roster = do
458 uks <- readTVar userkeys
459 as <- readTVar roster
460 return $ uks ++ map ((userSecret *** Tox.id2key) . swap) (HashMap.toList as)
461 453
462clientSession :: Session -> t1 -> t -> ClientHandle -> IO () 454clientSession :: Session -> t1 -> t -> ClientHandle -> IO ()
463clientSession s@Session{..} sock cnum h = do 455clientSession s@Session{..} sock cnum h = do
@@ -644,7 +636,7 @@ clientSession s@Session{..} sock cnum h = do
644 -- k secrets (list key pairs, including secret keys) 636 -- k secrets (list key pairs, including secret keys)
645 637
646 ("k", s) | "" <- strp s -> cmd0 $ do 638 ("k", s) | "" <- strp s -> cmd0 $ do
647 ks <- atomically $ readKeys userkeys (accounts roster) 639 ks <- atomically $ myKeyPairs roster
648 let spaces k | Just sel <- selectedKey, (sel == k) = " *" 640 let spaces k | Just sel <- selectedKey, (sel == k) = " *"
649 | otherwise = " " 641 | otherwise = " "
650 hPutClient h $ unlines $ map (\(_,k) -> mappend (spaces k) . show . Tox.key2id $ k) ks 642 hPutClient h $ unlines $ map (\(_,k) -> mappend (spaces k) . show . Tox.key2id $ k) ks
@@ -652,8 +644,7 @@ clientSession s@Session{..} sock cnum h = do
652 secret <- generateSecretKey 644 secret <- generateSecretKey
653 let pubkey = toPublic secret 645 let pubkey = toPublic secret
654 oldks <- atomically $ do 646 oldks <- atomically $ do
655 ks <- readTVar userkeys 647 ks <- myKeyPairs roster
656 modifyTVar userkeys ((secret,pubkey):)
657 Tox.addContactInfo roster secret 648 Tox.addContactInfo roster secret
658 return ks 649 return ks
659 let asString = show . Tox.key2id 650 let asString = show . Tox.key2id
@@ -661,14 +652,14 @@ clientSession s@Session{..} sock cnum h = do
661 ++ [mappend " *" . show . Tox.key2id $ pubkey] 652 ++ [mappend " *" . show . Tox.key2id $ pubkey]
662 switchKey $ Just pubkey 653 switchKey $ Just pubkey
663 | "secrets" <- strp s -> cmd0 $ do 654 | "secrets" <- strp s -> cmd0 $ do
664 ks <- atomically $ readKeys userkeys (accounts roster) 655 ks <- atomically $ myKeyPairs roster
665 skey <- maybe (return Nothing) (atomically . dhtSecretKey) 656 skey <- maybe (return Nothing) (atomically . dhtSecretKey)
666 $ Map.lookup netname dhts 657 $ Map.lookup netname dhts
667 hPutClient h . showReport $ map mkrow ks ++ case skey >>= encodeSecret of 658 hPutClient h . showReport $ map mkrow ks ++ case skey >>= encodeSecret of
668 Just x -> [("",""),("dht-key:",B.unpack x)] 659 Just x -> [("",""),("dht-key:",B.unpack x)]
669 Nothing -> [] 660 Nothing -> []
670 | ("sel",_:expr) <- break isSpace s -> do 661 | ("sel",_:expr) <- break isSpace s -> do
671 ks <- atomically $ map (show . Tox.key2id . snd) <$> readKeys userkeys (accounts roster) 662 ks <- atomically $ map (show . Tox.key2id . snd) <$> myKeyPairs roster
672 case find (isInfixOf expr) ks of 663 case find (isInfixOf expr) ks of
673 Just k -> do 664 Just k -> do
674 hPutClient h $ "Selected key: "++k 665 hPutClient h $ "Selected key: "++k
@@ -682,8 +673,7 @@ clientSession s@Session{..} sock cnum h = do
682 let toPair x = (x,toPublic x) 673 let toPair x = (x,toPublic x)
683 pairs = map (toPair . f) mbSecs 674 pairs = map (toPair . f) mbSecs
684 oldks <- atomically $ do 675 oldks <- atomically $ do
685 oldks <- readTVar userkeys 676 oldks <- myKeyPairs roster
686 modifyTVar userkeys (pairs ++)
687 forM pairs $ \(sk,_) -> Tox.addContactInfo roster sk 677 forM pairs $ \(sk,_) -> Tox.addContactInfo roster sk
688 return oldks 678 return oldks
689 hPutClient h $ unlines $ map (mappend " " . show . Tox.key2id . snd) oldks 679 hPutClient h $ unlines $ map (mappend " " . show . Tox.key2id . snd) oldks
@@ -697,9 +687,8 @@ clientSession s@Session{..} sock cnum h = do
697 let toPair x = (x,toPublic x) 687 let toPair x = (x,toPublic x)
698 pairs = map (toPair . f) mbSecs 688 pairs = map (toPair . f) mbSecs
699 ks <- atomically $ do 689 ks <- atomically $ do
700 modifyTVar userkeys (filter (`notElem` pairs) )
701 forM pairs $ \(_,pk) -> Tox.delContactInfo roster pk 690 forM pairs $ \(_,pk) -> Tox.delContactInfo roster pk
702 readTVar userkeys 691 myKeyPairs roster
703 hPutClient h . showReport $ map mkrow ks 692 hPutClient h . showReport $ map mkrow ks
704 switchKey $ do 693 switchKey $ do
705 k <- selectedKey 694 k <- selectedKey
@@ -946,7 +935,7 @@ clientSession s@Session{..} sock cnum h = do
946 $ readEither nidstr 935 $ readEither nidstr
947 goTarget (Left nodeinfo) = do 936 goTarget (Left nodeinfo) = do
948 msec <- atomically $ do 937 msec <- atomically $ do
949 ks <- map swap <$> readKeys userkeys (accounts roster) 938 ks <- map swap <$> myKeyPairs roster
950 return $ Data.List.lookup mypubkey ks 939 return $ Data.List.lookup mypubkey ks
951 case mbTox of 940 case mbTox of
952 Nothing -> hPutClient h "Requires Tox enabled." 941 Nothing -> hPutClient h "Requires Tox enabled."
@@ -966,7 +955,7 @@ clientSession s@Session{..} sock cnum h = do
966 hPutClient h "Handshake sent" 955 hPutClient h "Handshake sent"
967 goTarget (Right nid) = do 956 goTarget (Right nid) = do
968 msec <- atomically $ do 957 msec <- atomically $ do
969 ks <- map swap <$> readKeys userkeys (accounts roster) 958 ks <- map swap <$> myKeyPairs roster
970 return $ Data.List.lookup mypubkey ks 959 return $ Data.List.lookup mypubkey ks
971 case mbTox of 960 case mbTox of
972 Nothing -> hPutClient h "Requires Tox enabled." 961 Nothing -> hPutClient h "Requires Tox enabled."
@@ -1931,13 +1920,12 @@ main = runResourceT $ liftBaseWith $ \resT -> do
1931 installHandler sigTERM (CatchOnce (atomically $ writeTVar signalQuit True)) Nothing 1920 installHandler sigTERM (CatchOnce (atomically $ writeTVar signalQuit True)) Nothing
1932 installHandler sigINT (CatchOnce (atomically $ writeTVar signalQuit True)) Nothing 1921 installHandler sigINT (CatchOnce (atomically $ writeTVar signalQuit True)) Nothing
1933 let defaultToxData = do 1922 let defaultToxData = do
1934 toxids <- atomically $ newTVar []
1935 rster <- Tox.newContactInfo 1923 rster <- Tox.newContactInfo
1936 orouter <- newOnionRouter (hPutStrLn stderr) 1924 orouter <- newOnionRouter (hPutStrLn stderr)
1937 return (toxids, rster, orouter) 1925 return (rster, orouter)
1938 (toxids,rstr,orouter) <- fromMaybe defaultToxData $ do 1926 (rstr,orouter) <- fromMaybe defaultToxData $ do
1939 tox <- mbtox 1927 tox <- mbtox
1940 return $ return ( userKeys (Tox.toxCryptoKeys tox), Tox.toxContactInfo tox, Tox.toxOnionRoutes tox ) 1928 return $ return ( Tox.toxContactInfo tox, Tox.toxOnionRoutes tox )
1941 let session = clientSession0 $ Session 1929 let session = clientSession0 $ Session
1942 { netname = concat $ take 1 $ Map.keys dhts -- initial default DHT 1930 { netname = concat $ take 1 $ Map.keys dhts -- initial default DHT
1943 , selectedKey = Nothing 1931 , selectedKey = Nothing
@@ -1946,7 +1934,6 @@ main = runResourceT $ liftBaseWith $ \resT -> do
1946 , swarms = swarms 1934 , swarms = swarms
1947 , cryptosessions = netCryptoSessionsState 1935 , cryptosessions = netCryptoSessionsState
1948 , toxkeys = keysdb 1936 , toxkeys = keysdb
1949 , userkeys = toxids
1950 , roster = rstr 1937 , roster = rstr
1951 , announceToLan = fromMaybe (return ()) $ Tox.toxAnnounceToLan <$> mbtox 1938 , announceToLan = fromMaybe (return ()) $ Tox.toxAnnounceToLan <$> mbtox
1952 , connectionManager = ConnectionManager <$> mconns 1939 , connectionManager = ConnectionManager <$> mconns