diff options
author | Joe Crayne <joe@jerkface.net> | 2019-12-19 11:06:13 -0500 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 23:27:24 -0500 |
commit | 7895f56e787823af98197981dbdd6c11f2f25597 (patch) | |
tree | cea9fad3d83de2dd098284cd20918a7846315882 | |
parent | b6676d7c3339e46752cadfc1198886062f5c666d (diff) |
Improved 'c' command.
-rw-r--r-- | dht/Presence/XMPPServer.hs | 4 | ||||
-rw-r--r-- | dht/ToxManager.hs | 4 | ||||
-rw-r--r-- | dht/examples/dhtd.hs | 54 | ||||
-rw-r--r-- | dht/src/Network/Tox/AggregateSession.hs | 3 | ||||
-rw-r--r-- | dht/src/Network/Tox/NodeId.hs | 4 |
5 files changed, 54 insertions, 15 deletions
diff --git a/dht/Presence/XMPPServer.hs b/dht/Presence/XMPPServer.hs index 3bafd33c..2f2a1b4b 100644 --- a/dht/Presence/XMPPServer.hs +++ b/dht/Presence/XMPPServer.hs | |||
@@ -1775,9 +1775,9 @@ xmppServer allocate bind_addr = do | |||
1775 | { pingInterval = 15000 + fuzz | 1775 | { pingInterval = 15000 + fuzz |
1776 | , timeout = 2000 | 1776 | , timeout = 2000 |
1777 | , duplex = False } | 1777 | , duplex = False } |
1778 | tcp <- tcpManager (\(PeerAddress addr) -> (addr, peer_params, 10000)) sv | 1778 | tcp <- tcpManager (\(PeerAddress addr) -> (addr, peer_params, 10000)) sv -- TODO: unused |
1779 | return XMPPServer { _xmpp_sv = sv | 1779 | return XMPPServer { _xmpp_sv = sv |
1780 | , _xmpp_man = tcp | 1780 | , _xmpp_man = tcp -- TODO: unused |
1781 | , _xmpp_peer_params = peer_params | 1781 | , _xmpp_peer_params = peer_params |
1782 | , _xmpp_peer_bind = peer_bind | 1782 | , _xmpp_peer_bind = peer_bind |
1783 | } | 1783 | } |
diff --git a/dht/ToxManager.hs b/dht/ToxManager.hs index c4440409..34cdcb6f 100644 --- a/dht/ToxManager.hs +++ b/dht/ToxManager.hs | |||
@@ -147,7 +147,7 @@ toxman ssvar announcer toxbkts tox presence = ToxManager | |||
147 | return () | 147 | return () |
148 | 148 | ||
149 | , deactivateAccount = \k pubname -> do | 149 | , deactivateAccount = \k pubname -> do |
150 | dput XMan $ "toxman DECTIVATE (todo) 1 " ++ show pubname | 150 | dput XMan $ "toxman DEACTIVATE " ++ show pubname |
151 | let ContactInfo{ accounts } = Tox.toxContactInfo tox | 151 | let ContactInfo{ accounts } = Tox.toxContactInfo tox |
152 | mpubid = stripSuffix ".tox" pubname >>= readMaybe . T.unpack | 152 | mpubid = stripSuffix ".tox" pubname >>= readMaybe . T.unpack |
153 | bStopped <- fmap (fromMaybe Nothing) $ atomically $ do | 153 | bStopped <- fmap (fromMaybe Nothing) $ atomically $ do |
@@ -225,7 +225,7 @@ toxman ssvar announcer toxbkts tox presence = ToxManager | |||
225 | m <- forM (keyAsUnique paddr >>= (`Map.lookup` ss)) $ \c -> do | 225 | m <- forM (keyAsUnique paddr >>= (`Map.lookup` ss)) $ \c -> do |
226 | fmap (uncurry ToxContact . (key2id *** key2id)) <$> compatibleKeys c | 226 | fmap (uncurry ToxContact . (key2id *** key2id)) <$> compatibleKeys c |
227 | return $ maybeToList (join m) | 227 | return $ maybeToList (join m) |
228 | } | 228 | } :: Connection.Manager ToxProgress ToxContact |
229 | , resolveToxPeer = \me them -> do | 229 | , resolveToxPeer = \me them -> do |
230 | let m = do meid <- readMaybe $ T.unpack me | 230 | let m = do meid <- readMaybe $ T.unpack me |
231 | themid <- readMaybe $ T.unpack them | 231 | themid <- readMaybe $ T.unpack them |
diff --git a/dht/examples/dhtd.hs b/dht/examples/dhtd.hs index c01d50cd..99ff746c 100644 --- a/dht/examples/dhtd.hs +++ b/dht/examples/dhtd.hs | |||
@@ -35,6 +35,7 @@ import Data.Bits (xor) | |||
35 | import Data.Char | 35 | import Data.Char |
36 | import Data.Conduit as C | 36 | import Data.Conduit as C |
37 | import qualified Data.Conduit.List as C | 37 | import qualified Data.Conduit.List as C |
38 | import Data.Data | ||
38 | import Data.Dependent.Sum | 39 | import Data.Dependent.Sum |
39 | import Data.Function | 40 | import Data.Function |
40 | import Data.Functor.Identity | 41 | import Data.Functor.Identity |
@@ -298,7 +299,7 @@ reportSearchResults meth h DHTSearch{searchShowTok,searchState,searchResults} = | |||
298 | ns' = map showN ns | 299 | ns' = map showN ns |
299 | reportResult meth id (const Nothing) id h (Right (ns',rs, Just ())) | 300 | reportResult meth id (const Nothing) id h (Right (ns',rs, Just ())) |
300 | 301 | ||
301 | data ConnectionManager = forall status k. ConnectionManager { typedManager :: Connection.Manager status k } | 302 | data ConnectionManager = forall status k. Data k => ConnectionManager { typedManager :: Connection.Manager status k } |
302 | 303 | ||
303 | data Session = Session | 304 | data Session = Session |
304 | { netname :: String | 305 | { netname :: String |
@@ -314,6 +315,7 @@ data Session = Session | |||
314 | , announcer :: Announcer | 315 | , announcer :: Announcer |
315 | , signalQuit :: IO () | 316 | , signalQuit :: IO () |
316 | , mbTox :: Maybe (Tox.Tox JabberClients) | 317 | , mbTox :: Maybe (Tox.Tox JabberClients) |
318 | , sessionsVar :: TVar (Map.Map Uniq24 AggregateSession) | ||
317 | } | 319 | } |
318 | 320 | ||
319 | exceptionsToClient :: ClientHandle -> IO () -> IO () | 321 | exceptionsToClient :: ClientHandle -> IO () -> IO () |
@@ -360,6 +362,15 @@ waitOn bg nid ni = do | |||
360 | bg nid ni $ putMVar mvar | 362 | bg nid ni $ putMVar mvar |
361 | takeMVar mvar | 363 | takeMVar mvar |
362 | 364 | ||
365 | getSessions :: TVar (Map.Map Uniq24 AggregateSession) -> Uniq24 -> STM [Tox.Session] | ||
366 | getSessions ssvar u24 = do | ||
367 | agmap <- readTVar ssvar | ||
368 | case Map.lookup u24 agmap of | ||
369 | Nothing -> return [] | ||
370 | Just agg -> do | ||
371 | smap <- readTVar $ contactSession agg | ||
372 | return $ map singleSession $ IntMap.elems smap | ||
373 | |||
363 | clientSession :: Session -> t1 -> t -> ClientHandle -> IO () | 374 | clientSession :: Session -> t1 -> t -> ClientHandle -> IO () |
364 | clientSession s@Session{..} sock cnum h = do | 375 | clientSession s@Session{..} sock cnum h = do |
365 | line <- dropWhile isSpace <$> hGetClientLine h | 376 | line <- dropWhile isSpace <$> hGetClientLine h |
@@ -1090,7 +1101,12 @@ clientSession s@Session{..} sock cnum h = do | |||
1090 | ] | 1101 | ] |
1091 | rs = map mkrow cs | 1102 | rs = map mkrow cs |
1092 | return $ do | 1103 | return $ do |
1093 | hPutClient h $ "connections\n" ++ showColumns rs | 1104 | us <- mapM (mapM aggSessionKey . getToxContacts . fst) cs |
1105 | sessionss <- atomically $ mapM (mapM (getSessions sessionsVar)) us | ||
1106 | let ls = do | ||
1107 | (sessions,row) <- zip sessionss (lines $ showColumns rs) | ||
1108 | row : map (mappend " " . show . sTheirAddr) (concat sessions) | ||
1109 | hPutClient h $ unlines $ ("connections ("++show(length rs)++")") : ls | ||
1094 | 1110 | ||
1095 | ("help", s) | Just DHT{..} <- Map.lookup netname dhts | 1111 | ("help", s) | Just DHT{..} <- Map.lookup netname dhts |
1096 | -> cmd0 $ do | 1112 | -> cmd0 $ do |
@@ -1325,6 +1341,14 @@ onNewToxSession sv ssvar invc ContactInfo{accounts} addrTox netcrypto = do | |||
1325 | 1341 | ||
1326 | return () | 1342 | return () |
1327 | 1343 | ||
1344 | getToxContacts :: Data d => d -> [Tox.ToxContact] | ||
1345 | getToxContacts a = case cast a of | ||
1346 | Just t -> [t] | ||
1347 | Nothing -> concat $ gmapQ getToxContacts a | ||
1348 | |||
1349 | aggSessionKey :: Tox.ToxContact -> IO Uniq24 | ||
1350 | aggSessionKey (Tox.ToxContact me them) = xor24 <$> hash24 (Tox.id2key me) <*> hash24 (Tox.id2key them) | ||
1351 | |||
1328 | selectManager :: Maybe (t -> ToxManager clientAddress) -> Manager Tcp.TCPStatus T.Text -> T.Text -> Manager Pending T.Text | 1352 | selectManager :: Maybe (t -> ToxManager clientAddress) -> Manager Tcp.TCPStatus T.Text -> T.Text -> Manager Pending T.Text |
1329 | selectManager mtman tcp profile = case stripSuffix ".tox" profile of | 1353 | selectManager mtman tcp profile = case stripSuffix ".tox" profile of |
1330 | Just k | Just tman <- mtman | 1354 | Just k | Just tman <- mtman |
@@ -1366,7 +1390,12 @@ selectManager mtman tcp profile = case stripSuffix ".tox" profile of | |||
1366 | , connections = let valid (Tox.ToxContact local them) = do | 1390 | , connections = let valid (Tox.ToxContact local them) = do |
1367 | guard $ T.pack (show local) == k | 1391 | guard $ T.pack (show local) == k |
1368 | return $ T.pack (show them ++ ".tox") | 1392 | return $ T.pack (show them ++ ".tox") |
1369 | in fmap (mapMaybe valid) $ connections tox | 1393 | in fmap (mapMaybe valid) $ do -- fmap (map (T.pack . show)) $ |
1394 | cs <- connections tox | ||
1395 | let ncs = length cs | ||
1396 | nms = length $ mapMaybe valid cs | ||
1397 | tput XMan $ "Manager{Tox} (all,valid)=" ++ show (ncs,nms) | ||
1398 | return cs | ||
1370 | , stringToKey = \s -> Just $ T.pack (s ++ ".tox") | 1399 | , stringToKey = \s -> Just $ T.pack (s ++ ".tox") |
1371 | , showProgress = \(ToxStatus stat) -> showProgress tox stat | 1400 | , showProgress = \(ToxStatus stat) -> showProgress tox stat |
1372 | } | 1401 | } |
@@ -1381,7 +1410,10 @@ selectManager mtman tcp profile = case stripSuffix ".tox" profile of | |||
1381 | , showKey = showKey tcp | 1410 | , showKey = showKey tcp |
1382 | , setPolicy = setPolicy tcp | 1411 | , setPolicy = setPolicy tcp |
1383 | , status = \k -> fmap XMPPStatus <$> status tcp k | 1412 | , status = \k -> fmap XMPPStatus <$> status tcp k |
1384 | , connections = connections tcp | 1413 | , connections = do |
1414 | cs <- connections tcp | ||
1415 | tput XMan $ "Manager{TCP} cons=" ++ show (length cs) | ||
1416 | return cs | ||
1385 | , stringToKey = stringToKey tcp | 1417 | , stringToKey = stringToKey tcp |
1386 | , showProgress = \(XMPPStatus stat) -> showProgress tcp stat | 1418 | , showProgress = \(XMPPStatus stat) -> showProgress tcp stat |
1387 | } | 1419 | } |
@@ -1668,9 +1700,14 @@ initJabber opts ssvar announcer mbtox toxdhts toxchat = case portxmpp opts of | |||
1668 | xmpp_thread <- forkXmpp sv (presenceHooks state chats (verbosity opts) (Just cport) (Just sport)) | 1700 | xmpp_thread <- forkXmpp sv (presenceHooks state chats (verbosity opts) (Just cport) (Just sport)) |
1669 | let conns :: ConnectionManager -- Manager (Either Pending TCPStatus) (Either T.Text T.Text) | 1701 | let conns :: ConnectionManager -- Manager (Either Pending TCPStatus) (Either T.Text T.Text) |
1670 | conns = fromMaybe (ConnectionManager tcp) $ do | 1702 | conns = fromMaybe (ConnectionManager tcp) $ do |
1671 | sel <- tman | 1703 | sel <- fmap ($ error "PresenseState") tman |
1672 | Just $ ConnectionManager $ addManagers (selectManager (Just sel) tcp "auto.tox") tcp | 1704 | let _ = sel :: ToxManager ClientAddress |
1673 | return (Just sv, Just conns, Just state, killThread xmpp_thread >> quitChatService) | 1705 | mantox = toxConnections sel :: Connection.Manager Tox.ToxProgress Tox.ToxContact |
1706 | Just $ ConnectionManager mantox -- addManagers (toxConnections sel) tcp | ||
1707 | return ( Just sv | ||
1708 | , Just conns | ||
1709 | , Just state | ||
1710 | , killThread xmpp_thread >> quitChatService) | ||
1674 | 1711 | ||
1675 | main :: IO () | 1712 | main :: IO () |
1676 | main = do | 1713 | main = do |
@@ -1784,7 +1821,7 @@ main = do | |||
1784 | 1821 | ||
1785 | keysdb <- Tox.newKeysDatabase | 1822 | keysdb <- Tox.newKeysDatabase |
1786 | 1823 | ||
1787 | ssvar <- atomically $ newTVar Map.empty | 1824 | ssvar <- atomically $ newTVar Map.empty :: IO ( TVar (Map.Map Uniq24 AggregateSession) ) |
1788 | rec (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr]),msv,mconns,mstate) <- do | 1825 | rec (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr]),msv,mconns,mstate) <- do |
1789 | 1826 | ||
1790 | (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr])) <- initTox opts ssvar keysdb msv invc | 1827 | (mbtox,quitTox,toxdhts,toxips,(taddrs::[SockAddr])) <- initTox opts ssvar keysdb msv invc |
@@ -1826,6 +1863,7 @@ main = do | |||
1826 | , externalAddresses = liftM2 (++) btips toxips | 1863 | , externalAddresses = liftM2 (++) btips toxips |
1827 | , announcer = announcer | 1864 | , announcer = announcer |
1828 | , mbTox = mbtox | 1865 | , mbTox = mbtox |
1866 | , sessionsVar = ssvar | ||
1829 | } | 1867 | } |
1830 | srv <- streamServer (withSession session) [SockAddrUnix "dht.sock"] | 1868 | srv <- streamServer (withSession session) [SockAddrUnix "dht.sock"] |
1831 | return ( do atomically $ readTVar signalQuit >>= check | 1869 | return ( do atomically $ readTVar signalQuit >>= check |
diff --git a/dht/src/Network/Tox/AggregateSession.hs b/dht/src/Network/Tox/AggregateSession.hs index 999c7399..faf512d0 100644 --- a/dht/src/Network/Tox/AggregateSession.hs +++ b/dht/src/Network/Tox/AggregateSession.hs | |||
@@ -6,7 +6,8 @@ | |||
6 | {-# LANGUAGE LambdaCase #-} | 6 | {-# LANGUAGE LambdaCase #-} |
7 | {-# LANGUAGE PatternSynonyms #-} | 7 | {-# LANGUAGE PatternSynonyms #-} |
8 | module Network.Tox.AggregateSession | 8 | module Network.Tox.AggregateSession |
9 | ( AggregateSession | 9 | ( AggregateSession(contactSession) |
10 | , SingleCon(singleSession) | ||
10 | , newAggregateSession | 11 | , newAggregateSession |
11 | , aggregateStatus | 12 | , aggregateStatus |
12 | , checkCompatible | 13 | , checkCompatible |
diff --git a/dht/src/Network/Tox/NodeId.hs b/dht/src/Network/Tox/NodeId.hs index 8567e77d..a3ddd617 100644 --- a/dht/src/Network/Tox/NodeId.hs +++ b/dht/src/Network/Tox/NodeId.hs | |||
@@ -626,12 +626,12 @@ solveBase64NoSpamID b64digits pub = do | |||
626 | -- even though they are long-term keys rather than the public keys of Tox DHT | 626 | -- even though they are long-term keys rather than the public keys of Tox DHT |
627 | -- nodes. | 627 | -- nodes. |
628 | data ToxContact = ToxContact NodeId{-me-} NodeId{-them-} | 628 | data ToxContact = ToxContact NodeId{-me-} NodeId{-them-} |
629 | deriving (Eq,Ord) | 629 | deriving (Eq,Ord,Data) |
630 | 630 | ||
631 | instance Show ToxContact where show = show . showToxContact_ | 631 | instance Show ToxContact where show = show . showToxContact_ |
632 | 632 | ||
633 | showToxContact_ :: ToxContact -> String | 633 | showToxContact_ :: ToxContact -> String |
634 | showToxContact_ (ToxContact me them) = show me ++ ":" ++ show them | 634 | showToxContact_ (ToxContact me them) = "(" ++ take 8 (show me) ++ ")" ++ show them |
635 | 635 | ||
636 | -- | This type indicates the progress of a tox encrypted friend link | 636 | -- | This type indicates the progress of a tox encrypted friend link |
637 | -- connection. Two scenarios are illustrated below. The parenthesis show the | 637 | -- connection. Two scenarios are illustrated below. The parenthesis show the |