From 4fc283da8edff660e1e7a3161745a8b2f11dc356 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Thu, 21 Jun 2018 15:47:37 -0400 Subject: trivial refactor; ran autoformatter --- examples/dhtd.hs | 74 ++++++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 34 deletions(-) diff --git a/examples/dhtd.hs b/examples/dhtd.hs index fe639af1..da93bed4 100644 --- a/examples/dhtd.hs +++ b/examples/dhtd.hs @@ -1,4 +1,5 @@ {-# LANGUAGE BangPatterns #-} +{-# LANGUAGE ViewPatterns #-} {-# LANGUAGE CPP #-} {-# LANGUAGE ExistentialQuantification #-} {-# LANGUAGE FlexibleContexts #-} @@ -1233,41 +1234,46 @@ netcrypto -> IO () netcrypto _ _ h _ Nothing _ = hPutClient h "Requires Tox enabled." netcrypto _ Nothing h _ _ _ = hPutClient h "No key is selected, see k command." -netcrypto dht (Just mypubkey) h roster (Just tox) keystr | Just DHT{..} <- dht = do - let goParse = either - (\_ -> either (hPutClient h . ("Bad netcrypto target: "++)) - (goTarget . Left) - (readEither keystr)) -- attempt read as NodeInfo type - (goTarget . Right . Tox.id2key) - $ readEither keystr -- attempt read as NodeId type - goParse +netcrypto (Just (DHT {..})) (Just mypubkey) h roster (Just tox) keystr = + either + (const $ + either + (hPutClient h . ("Bad netcrypto target: " ++)) + goNodeInfo + (readEither keystr) -- attempt read as NodeInfo type + ) + (goPubkey . Tox.id2key) $ + readEither keystr -- attempt read as NodeId type where - goTarget (Left userkey_nodeinfo) = do - msec <- atomically $ do - fmap userSecret . HashMap.lookup (Tox.key2id mypubkey) <$> readTVar (accounts roster) - case msec of - Nothing -> hPutClient h "Error getting secret key" - Just sec -> do - let their_pub = Tox.id2key $ Tox.nodeId userkey_nodeinfo - their_addr = Tox.nodeAddr userkey_nodeinfo - let acsVar = accounts (Tox.toxContactInfo tox) - acsmap <- atomically $ readTVar acsVar - case HashMap.lookup (Tox.key2id mypubkey) acsmap of - Nothing -> hPutClient h "Unable to find account for selected key" - Just account -> do - now <- getPOSIXTime - atomically $ setContactAddr now their_pub their_addr account - Tox.netCrypto tox sec their_pub - hPutClient h "Handshake sent" - goTarget (Right their_pub) = do - msec <- atomically $ do - ks <- map swap <$> myKeyPairs roster - return $ Data.List.lookup mypubkey ks - case msec of - Nothing -> hPutClient h "Error getting secret key" - Just sec -> do - Tox.netCrypto tox sec their_pub - hPutClient h "Handshake sent" + goNodeInfo userkey_nodeinfo = do + msec <- + atomically $ do + fmap userSecret . HashMap.lookup (Tox.key2id mypubkey) <$> + readTVar (accounts roster) + case msec of + Nothing -> hPutClient h "Error getting secret key" + Just sec -> do + let their_pub = Tox.id2key $ Tox.nodeId userkey_nodeinfo + their_addr = Tox.nodeAddr userkey_nodeinfo + let acsVar = accounts (Tox.toxContactInfo tox) + acsmap <- atomically $ readTVar acsVar + case HashMap.lookup (Tox.key2id mypubkey) acsmap of + Nothing -> hPutClient h "Unable to find account for selected key" + Just account -> do + now <- getPOSIXTime + atomically $ setContactAddr now their_pub their_addr account + Tox.netCrypto tox sec their_pub + hPutClient h "Handshake sent" + goPubkey their_pub = do + msec <- + atomically $ do + ks <- map swap <$> myKeyPairs roster + return $ Data.List.lookup mypubkey ks + case msec of + Nothing -> hPutClient h "Error getting secret key" + Just sec -> do + Tox.netCrypto tox sec their_pub + hPutClient h "Handshake sent" readExternals :: (ni -> SockAddr) -> [TVar (BucketList ni)] -> IO [SockAddr] readExternals nodeAddr vars = do -- cgit v1.2.3