summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-11-03 00:39:27 -0400
committerJoe Crayne <joe@jerkface.net>2018-11-03 00:39:27 -0400
commit42061ac9d48a09de2cc3c09e2f6d9c60a7ec45b6 (patch)
treec098db98426f9c8f9a26cae0b65d0f7ab80abfa4
parent72bbe42ea51261d306b45fc0fddef57d57c54cef (diff)
More uniform handling for Tox and XMPP peers.
This is a continuation of the work done in the following commits: 38505a54 Provide a different roster resolver for tox-peers to Presence. fbf9890a Moved resolving duty to Connection manager.
-rw-r--r--ToxManager.hs10
-rw-r--r--examples/dhtd.hs39
2 files changed, 39 insertions, 10 deletions
diff --git a/ToxManager.hs b/ToxManager.hs
index 793a3b8a..44b7a5ef 100644
--- a/ToxManager.hs
+++ b/ToxManager.hs
@@ -63,6 +63,8 @@ import Connection
63import Connection.Tcp (TCPStatus) 63import Connection.Tcp (TCPStatus)
64import GHC.Conc (unsafeIOToSTM) 64import GHC.Conc (unsafeIOToSTM)
65 65
66data Pending = ToxStatus ToxProgress | XMPPStatus TCPStatus
67
66 68
67toxAnnounceSendData :: Tox.Tox JabberClients 69toxAnnounceSendData :: Tox.Tox JabberClients
68 -> PublicKey 70 -> PublicKey
@@ -96,7 +98,7 @@ toxman :: TVar (Map.Map Uniq24 AggregateSession)
96 -> Announcer 98 -> Announcer
97 -> [(String,TVar (BucketList Tox.NodeInfo))] 99 -> [(String,TVar (BucketList Tox.NodeInfo))]
98 -> Tox.Tox JabberClients 100 -> Tox.Tox JabberClients
99 -> PresenceState TCPStatus 101 -> PresenceState Pending
100 -> ToxManager ClientAddress 102 -> ToxManager ClientAddress
101toxman ssvar announcer toxbkts tox presence = ToxManager 103toxman ssvar announcer toxbkts tox presence = ToxManager
102 { activateAccount = \k pubname seckey -> do 104 { activateAccount = \k pubname seckey -> do
@@ -248,7 +250,7 @@ initPerClient = do
248data ToxToXMPP = ToxToXMPP 250data ToxToXMPP = ToxToXMPP
249 { txAnnouncer :: Announcer 251 { txAnnouncer :: Announcer
250 , txAccount :: Account JabberClients 252 , txAccount :: Account JabberClients
251 , txPresence :: PresenceState TCPStatus 253 , txPresence :: PresenceState Pending
252 , txTox :: Tox JabberClients 254 , txTox :: Tox JabberClients
253 , txSessions :: TVar (Map.Map Uniq24 AggregateSession) 255 , txSessions :: TVar (Map.Map Uniq24 AggregateSession)
254 } 256 }
@@ -552,7 +554,7 @@ updateRoster tx them = do
552-- | Returns a list of nospam values to use for friend requests to send to a 554-- | Returns a list of nospam values to use for friend requests to send to a
553-- remote peer. This list is non-empty only when it is desirable to send 555-- remote peer. This list is non-empty only when it is desirable to send
554-- friend requests. 556-- friend requests.
555checkSoliciting :: PresenceState TCPStatus -> PublicKey -> PublicKey -> Contact -> IO [NoSpam] 557checkSoliciting :: PresenceState Pending -> PublicKey -> PublicKey -> Contact -> IO [NoSpam]
556checkSoliciting presence me them contact = do 558checkSoliciting presence me them contact = do
557 let theirhost = T.pack $ show (key2id them) ++ ".tox" 559 let theirhost = T.pack $ show (key2id them) ++ ".tox"
558 myhost = T.pack $ show (key2id me) ++ ".tox" 560 myhost = T.pack $ show (key2id me) ++ ".tox"
@@ -658,7 +660,7 @@ stopConnecting ToxToXMPP{txAnnouncer=announcer,txAccount=acnt} them reason = do
658 cancel announcer akeyD 660 cancel announcer akeyD
659 661
660forkAccountWatcher :: TVar (Map.Map Uniq24 AggregateSession) 662forkAccountWatcher :: TVar (Map.Map Uniq24 AggregateSession)
661 -> Account JabberClients -> Tox JabberClients -> PresenceState TCPStatus -> Announcer -> IO ThreadId 663 -> Account JabberClients -> Tox JabberClients -> PresenceState Pending -> Announcer -> IO ThreadId
662forkAccountWatcher ssvar acc tox st announcer = forkIO $ do 664forkAccountWatcher ssvar acc tox st announcer = forkIO $ do
663 myThreadId >>= flip labelThread ("online:" 665 myThreadId >>= flip labelThread ("online:"
664 ++ show (key2id $ toPublic $ userSecret acc)) 666 ++ show (key2id $ toPublic $ userSecret acc))
diff --git a/examples/dhtd.hs b/examples/dhtd.hs
index 481129b3..892fc88c 100644
--- a/examples/dhtd.hs
+++ b/examples/dhtd.hs
@@ -122,7 +122,7 @@ import XMPPServer
122import Connection 122import Connection
123import ToxToXMPP 123import ToxToXMPP
124import XMPPToTox 124import XMPPToTox
125import qualified Connection.Tcp as Tcp (ConnectionEvent(..),noCleanUp) 125import qualified Connection.Tcp as Tcp (ConnectionEvent(..),noCleanUp,TCPStatus)
126import Control.Concurrent.Supply 126import Control.Concurrent.Supply
127import qualified Data.CyclicBuffer as CB 127import qualified Data.CyclicBuffer as CB
128import DPut 128import DPut
@@ -1690,10 +1690,19 @@ onNewToxSession sv ssvar ContactInfo{accounts} addrTox netcrypto = do
1690 1690
1691 return () 1691 return ()
1692 1692
1693selectManager :: Maybe (t -> ToxManager k) -> Manager status T.Text -> T.Text -> Manager status T.Text 1693selectManager :: Maybe (t -> ToxManager clientAddress) -> Manager Tcp.TCPStatus T.Text -> T.Text -> Manager Pending T.Text
1694selectManager (Just tman) tcp profile = case T.splitAt 43 profile of 1694selectManager mtman tcp profile = case T.splitAt 43 profile of
1695 (k,".tox") -> let _ = _ 1695 (k,".tox") | Just tman <- mtman
1696 -> let _ = _
1697 -- The following error call is safe because the toxConnections field
1698 -- does not make use of the PresenceState passed to tman.
1696 tox = toxConnections $ tman $ error "PresenseState" 1699 tox = toxConnections $ tman $ error "PresenseState"
1700 tkey them = do
1701 me <- readMaybe (T.unpack k)
1702 them <- case T.splitAt 43 them of
1703 (them0,".tox") -> readMaybe (T.unpack them0)
1704 _ -> Nothing
1705 return (Tox.ToxContact me them)
1697 in Manager 1706 in Manager
1698 { resolvePeer = \themhost -> do 1707 { resolvePeer = \themhost -> do
1699 r <- fromMaybe (return []) $ do 1708 r <- fromMaybe (return []) $ do
@@ -1714,15 +1723,33 @@ selectManager (Just tman) tcp profile = case T.splitAt 43 profile of
1714 _ -> Nothing) 1723 _ -> Nothing)
1715 dput XMan $ "reverseAddress(tox)" ++ show (T.take 8 k,paddr) ++ ": " ++ show r 1724 dput XMan $ "reverseAddress(tox)" ++ show (T.take 8 k,paddr) ++ ": " ++ show r
1716 return r 1725 return r
1726
1727 , setPolicy = \them -> case tkey them of
1728 Just tk -> \p -> setPolicy tox tk p
1729 Nothing -> \p -> return ()
1730 , status = \them -> case tkey them of
1731 Just tk -> fmap ToxStatus <$> status tox tk
1732 Nothing -> return $ Connection Dormant RefusingToConnect
1733 , connections = let valid (Tox.ToxContact local them) = do
1734 guard $ T.pack (show local) == k
1735 return $ T.pack (show them ++ ".tox")
1736 in fmap (mapMaybe valid) $ connections tox
1737 , stringToKey = \s -> Just $ T.pack (s ++ ".tox")
1738 , showProgress = \(ToxStatus stat) -> showProgress tox stat
1717 } 1739 }
1718 _ -> tcp 1740 _ -> Manager
1719selectManager _ tcp profile = tcp
1720 { resolvePeer = \themhost -> do 1741 { resolvePeer = \themhost -> do
1721 dput XMan $ "resolvePeer(tcp) " ++ show (profile,themhost) 1742 dput XMan $ "resolvePeer(tcp) " ++ show (profile,themhost)
1722 resolvePeer tcp themhost 1743 resolvePeer tcp themhost
1723 , reverseAddress = \paddr -> do 1744 , reverseAddress = \paddr -> do
1724 dput XMan $ "reverseAddress(tcp) " ++ show (profile,paddr) 1745 dput XMan $ "reverseAddress(tcp) " ++ show (profile,paddr)
1725 reverseAddress tcp paddr 1746 reverseAddress tcp paddr
1747
1748 , setPolicy = setPolicy tcp
1749 , status = \k -> fmap XMPPStatus <$> status tcp k
1750 , connections = connections tcp
1751 , stringToKey = stringToKey tcp
1752 , showProgress = \(XMPPStatus stat) -> showProgress tcp stat
1726 } 1753 }
1727 1754
1728 1755