summaryrefslogtreecommitdiff
path: root/dht
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2020-01-11 21:24:17 -0500
committerJoe Crayne <joe@jerkface.net>2020-01-11 21:24:17 -0500
commit6be7ff71f850e90f61c8f3b80b2b513b34891c57 (patch)
tree11cbbe9ebebccc1c1c0b5d0a495f9e9eb926e2f6 /dht
parentddf30becc00ee476ec0044e1e5c7257d5e741a20 (diff)
Removed tput hack.
Diffstat (limited to 'dht')
-rw-r--r--dht/examples/dhtd.hs53
1 files changed, 32 insertions, 21 deletions
diff --git a/dht/examples/dhtd.hs b/dht/examples/dhtd.hs
index 984afeed..c36508ef 100644
--- a/dht/examples/dhtd.hs
+++ b/dht/examples/dhtd.hs
@@ -687,20 +687,22 @@ clientSession s@Session{..} sock cnum h = do
687 cf <- readTVar $ contactFriendRequest c 687 cf <- readTVar $ contactFriendRequest c
688 cp <- readTVar $ contactPolicy c 688 cp <- readTVar $ contactPolicy c
689 let summarizeNodeId | nosummary = id 689 let summarizeNodeId | nosummary = id
690 | otherwise = take 6 690 | otherwise = take 20
691 summarizeAddr | nosummary = id 691 summarizeAddr | nosummary = id
692 | otherwise = reverse . take 20 . reverse 692 | otherwise = reverse . take 20 . reverse
693 return $ [ maybe "/" showPolicy cp 693 return $ [ maybe "/" showPolicy cp
694 , maybe "" (summarizeNodeId . show . Tox.key2id . Tox.dhtpk . snd) ck 694 , maybe (maybe "" (summarizeNodeId . show . Tox.key2id . Tox.dhtpk . snd) ck)
695 , maybe "" (summarizeAddr . show . snd) ca 695 (summarizeAddr . show . snd)
696 ca
696 , maybe "" (show . T.decodeUtf8 . Tox.friendRequestText . snd) cf 697 , maybe "" (show . T.decodeUtf8 . Tox.friendRequestText . snd) cf
697 ] 698 ]
698 return $ do 699 return $ do
699 forM_ (HashMap.toList css) $ \(me,xss) -> do 700 forM_ (HashMap.toList css) $ \(me,xss) -> do
700 let cs = map (\(toxid,xs) -> show toxid : xs) 701 let cs = map (\(toxid,xs) -> show toxid : xs)
701 $ HashMap.toList xss 702 $ HashMap.toList xss
702 hPutClientChunk h $ unlines [ show me, map (const '-') (show me) ] 703 hPutClientChunk h $ let header = show me ++ if nosummary then "" else " (pass -v for more)"
703 hPutClientChunk h $ showColumns $ ["ToxID","","NodeID","Address","FR text"] 704 in unlines [ header, map (const '-') header ]
705 hPutClientChunk h $ showColumns $ ["ToxID","","Address","FR text"]
704 : cs 706 : cs
705 hPutClient h "" 707 hPutClient h ""
706 708
@@ -1315,14 +1317,15 @@ newXmmpSink session@(Tox.NCrypto { ncOutgoingQueue = outGoingQVar }) = C.awaitFo
1315-} 1317-}
1316 1318
1317 1319
1318onNewToxSession :: XMPPServer 1320onNewToxSession :: (IO () -> STM ())
1321 -> XMPPServer
1319 -> TVar (Map.Map Uniq24 AggregateSession) 1322 -> TVar (Map.Map Uniq24 AggregateSession)
1320 -> InviteCache IO 1323 -> InviteCache IO
1321 -> ContactInfo extra 1324 -> ContactInfo extra
1322 -> SockAddr 1325 -> SockAddr
1323 -> Tox.Session 1326 -> Tox.Session
1324 -> IO () 1327 -> IO ()
1325onNewToxSession sv ssvar invc ContactInfo{accounts} addrTox netcrypto = do 1328onNewToxSession runio sv ssvar invc ContactInfo{accounts} addrTox netcrypto = do
1326 let them s = Tox.longTermKey $ runIdentity cookie -- remote tox key 1329 let them s = Tox.longTermKey $ runIdentity cookie -- remote tox key
1327 where Tox.Cookie _ cookie = Tox.handshakeCookie (sReceivedHandshake s) 1330 where Tox.Cookie _ cookie = Tox.handshakeCookie (sReceivedHandshake s)
1328 1331
@@ -1331,9 +1334,10 @@ onNewToxSession sv ssvar invc ContactInfo{accounts} addrTox netcrypto = do
1331 onStatusChange :: (Tox.Session -> Tcp.ConnectionEvent XML.Event -> STM ()) 1334 onStatusChange :: (Tox.Session -> Tcp.ConnectionEvent XML.Event -> STM ())
1332 -> AggregateSession -> Tox.Session -> Status Tox.ToxProgress -> STM () 1335 -> AggregateSession -> Tox.Session -> Status Tox.ToxProgress -> STM ()
1333 onStatusChange announce c s Established = onConnect announce c s 1336 onStatusChange announce c s Established = onConnect announce c s
1334 onStatusChange announce _ s _ = onEOF announce s 1337 onStatusChange announce _ s status = onEOF announce s status
1335 1338
1336 onEOF announce s = do 1339 onEOF announce s status = do
1340 runio $ dput XMan $ "EOF(" ++ take 16 (showKey256 $ them s) ++ "): " ++ show status
1337 HashMap.lookup (Tox.key2id $ me s) <$> readTVar accounts 1341 HashMap.lookup (Tox.key2id $ me s) <$> readTVar accounts
1338 >>= mapM_ (setTerminated $ them s) 1342 >>= mapM_ (setTerminated $ them s)
1339 announce s Tcp.EOF 1343 announce s Tcp.EOF
@@ -1391,8 +1395,8 @@ getToxContacts a = case cast a of
1391aggSessionKey :: Tox.ToxContact -> IO Uniq24 1395aggSessionKey :: Tox.ToxContact -> IO Uniq24
1392aggSessionKey (Tox.ToxContact me them) = xor24 <$> hash24 (Tox.id2key me) <*> hash24 (Tox.id2key them) 1396aggSessionKey (Tox.ToxContact me them) = xor24 <$> hash24 (Tox.id2key me) <*> hash24 (Tox.id2key them)
1393 1397
1394selectManager :: Maybe (t -> ToxManager clientAddress) -> Manager Tcp.TCPStatus T.Text -> T.Text -> Manager Pending T.Text 1398selectManager :: Announcer -> Maybe (t -> ToxManager clientAddress) -> Manager Tcp.TCPStatus T.Text -> T.Text -> Manager Pending T.Text
1395selectManager mtman tcp profile = case stripSuffix ".tox" profile of 1399selectManager announcer mtman tcp profile = case stripSuffix ".tox" profile of
1396 Just k | Just tman <- mtman 1400 Just k | Just tman <- mtman
1397 -> let -- The following error call is safe because the toxConnections field 1401 -> let -- The following error call is safe because the toxConnections field
1398 -- does not make use of the PresenceState passed to tman. 1402 -- does not make use of the PresenceState passed to tman.
@@ -1436,7 +1440,8 @@ selectManager mtman tcp profile = case stripSuffix ".tox" profile of
1436 cs <- connections tox 1440 cs <- connections tox
1437 let ncs = length cs 1441 let ncs = length cs
1438 nms = length $ mapMaybe valid cs 1442 nms = length $ mapMaybe valid cs
1439 tput XMan $ "Manager{Tox} (all,valid)=" ++ show (ncs,nms) 1443 runAction announcer "Tox.connections" $ do
1444 dput XMan $ "Manager{Tox} (all,valid)=" ++ show (ncs,nms)
1440 return cs 1445 return cs
1441 , stringToKey = \s -> Just $ T.pack (s ++ ".tox") 1446 , stringToKey = \s -> Just $ T.pack (s ++ ".tox")
1442 , showProgress = \(ToxStatus stat) -> showProgress tox stat 1447 , showProgress = \(ToxStatus stat) -> showProgress tox stat
@@ -1454,14 +1459,16 @@ selectManager mtman tcp profile = case stripSuffix ".tox" profile of
1454 , status = \k -> fmap XMPPStatus <$> status tcp k 1459 , status = \k -> fmap XMPPStatus <$> status tcp k
1455 , connections = do 1460 , connections = do
1456 cs <- connections tcp 1461 cs <- connections tcp
1457 tput XMan $ "Manager{TCP} cons=" ++ show (length cs) 1462 runAction announcer "TCP.connections" $ do
1463 dput XMan $ "Manager{TCP} cons=" ++ show (length cs)
1458 return cs 1464 return cs
1459 , stringToKey = stringToKey tcp 1465 , stringToKey = stringToKey tcp
1460 , showProgress = \(XMPPStatus stat) -> showProgress tcp stat 1466 , showProgress = \(XMPPStatus stat) -> showProgress tcp stat
1461 } 1467 }
1462 1468
1463 1469
1464initTox :: Options 1470initTox :: (IO () -> STM ())
1471 -> Options
1465 -> TVar (Map.Map Uniq24 AggregateSession) 1472 -> TVar (Map.Map Uniq24 AggregateSession)
1466 -> TVar Tox.AnnouncedKeys 1473 -> TVar Tox.AnnouncedKeys
1467 -> Maybe XMPPServer 1474 -> Maybe XMPPServer
@@ -1470,7 +1477,7 @@ initTox :: Options
1470 , Map.Map String DHT 1477 , Map.Map String DHT
1471 , IO [SockAddr] 1478 , IO [SockAddr]
1472 , [SockAddr]) 1479 , [SockAddr])
1473initTox opts ssvar keysdb mbxmpp invc = case porttox opts of 1480initTox runio opts ssvar keysdb mbxmpp invc = case porttox opts of
1474 [""] -> return (Nothing,return (), Map.empty, return [],[]) 1481 [""] -> return (Nothing,return (), Map.empty, return [],[])
1475 toxport -> do 1482 toxport -> do
1476 dput XMisc $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts)) 1483 dput XMisc $ "Supplied key: " ++ show (fmap (Tox.key2id . toPublic) (dhtkey opts))
@@ -1479,7 +1486,7 @@ initTox opts ssvar keysdb mbxmpp invc = case porttox opts of
1479 toxport 1486 toxport
1480 (case mbxmpp of 1487 (case mbxmpp of
1481 Nothing -> \_ _ _ -> return () 1488 Nothing -> \_ _ _ -> return ()
1482 Just xmpp -> onNewToxSession xmpp ssvar invc) 1489 Just xmpp -> onNewToxSession runio xmpp ssvar invc)
1483 crypto 1490 crypto
1484 (enableTCPDHT opts) 1491 (enableTCPDHT opts)
1485 -- addrTox <- getBindAddress toxport (ip6tox opts) 1492 -- addrTox <- getBindAddress toxport (ip6tox opts)
@@ -1730,7 +1737,7 @@ initJabber opts ssvar announcer mbtox toxchat = case portxmpp opts of
1730 sv <- xmppServer Tcp.noCleanUp (Just sport) 1737 sv <- xmppServer Tcp.noCleanUp (Just sport)
1731 tcp <- xmppConnections sv -- :: IO ( Manager TCPStatus T.Text ) 1738 tcp <- xmppConnections sv -- :: IO ( Manager TCPStatus T.Text )
1732 let tman = toxman ssvar announcer <$> mbtox 1739 let tman = toxman ssvar announcer <$> mbtox
1733 state <- newPresenceState cw tman sv (selectManager tman tcp) 1740 state <- newPresenceState cw tman sv (selectManager announcer tman tcp)
1734 chat <- atomically newMUC 1741 chat <- atomically newMUC
1735 quitChatService <- forkLocalChat chat 1742 quitChatService <- forkLocalChat chat
1736 let chats = Map.fromList [ ("local", chat) 1743 let chats = Map.fromList [ ("local", chat)
@@ -1866,9 +1873,10 @@ main = do
1866 keysdb <- Tox.newKeysDatabase 1873 keysdb <- Tox.newKeysDatabase
1867 1874
1868 ssvar <- atomically $ newTVar Map.empty :: IO ( TVar (Map.Map Uniq24 AggregateSession) ) 1875 ssvar <- atomically $ newTVar Map.empty :: IO ( TVar (Map.Map Uniq24 AggregateSession) )
1876 ioChan <- atomically newTChan
1869 rec (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr]),msv,mconns,mstate) <- do 1877 rec (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr]),msv,mconns,mstate) <- do
1870 1878
1871 (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr])) <- initTox opts ssvar keysdb msv invc 1879 (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr])) <- initTox (writeTChan ioChan) opts ssvar keysdb msv invc
1872 1880
1873 (msv,mconns,mstate,quitChat) <- initJabber opts ssvar announcer mbtox toxchat 1881 (msv,mconns,mstate,quitChat) <- initJabber opts ssvar announcer mbtox toxchat
1874 1882
@@ -1910,8 +1918,8 @@ main = do
1910 , sessionsVar = ssvar 1918 , sessionsVar = ssvar
1911 } 1919 }
1912 srv <- forkStreamServer (withSession session) [SockAddrUnix "dht.sock"] 1920 srv <- forkStreamServer (withSession session) [SockAddrUnix "dht.sock"]
1913 return ( do atomically $ readTVar signalQuit >>= check 1921 return ( do readTVar signalQuit >>= check
1914 quitListening srv 1922 return $ quitListening srv
1915 , readTVar signalQuit >>= check 1923 , readTVar signalQuit >>= check
1916 ) 1924 )
1917 1925
@@ -1950,7 +1958,10 @@ main = do
1950 1958
1951 -- Wait for DHT and XMPP threads to finish. 1959 -- Wait for DHT and XMPP threads to finish.
1952 -- Use ResourceT to clean-up XMPP server. 1960 -- Use ResourceT to clean-up XMPP server.
1953 waitForSignal 1961 fix $ \loop ->
1962 join $ atomically $ orElse waitForSignal $ do
1963 action <- readTChan ioChan
1964 return $ action >> loop
1954 1965
1955 forM_ mstate $ \PresenceState{server=xmpp} -> do 1966 forM_ mstate $ \PresenceState{server=xmpp} -> do
1956 quitXmpp xmpp 1967 quitXmpp xmpp