summaryrefslogtreecommitdiff
path: root/ToxManager.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-20 21:30:47 -0400
committerjoe <joe@jerkface.net>2018-06-20 21:30:47 -0400
commit7a16f326fbe7429792b155c4a963bad1f50dcbda (patch)
treebc134b0d5989a66205b30df5dc78879dfe2011e6 /ToxManager.hs
parent06229147ebfa72349baec5a2b55081341ff61908 (diff)
Parameterized Account to hold arbitrary information.
Diffstat (limited to 'ToxManager.hs')
-rw-r--r--ToxManager.hs32
1 files changed, 19 insertions, 13 deletions
diff --git a/ToxManager.hs b/ToxManager.hs
index 460b2fe5..cd835983 100644
--- a/ToxManager.hs
+++ b/ToxManager.hs
@@ -11,8 +11,8 @@ import Control.Concurrent.STM
11import Control.Monad 11import Control.Monad
12import Crypto.Tox 12import Crypto.Tox
13import qualified Data.HashMap.Strict as HashMap 13import qualified Data.HashMap.Strict as HashMap
14import qualified Data.Map as Map
14import Data.Maybe 15import Data.Maybe
15import qualified Data.Set as Set
16import qualified Data.Text as T 16import qualified Data.Text as T
17import Data.Time.Clock.POSIX 17import Data.Time.Clock.POSIX
18import Network.Address 18import Network.Address
@@ -38,10 +38,11 @@ import Control.Concurrent.Lifted
38import GHC.Conc (labelThread) 38import GHC.Conc (labelThread)
39#endif 39#endif
40 40
41toxAnnounceSendData :: Tox.Tox -> PublicKey 41toxAnnounceSendData :: Tox.Tox JabberClients
42 -> Nonce32 42 -> PublicKey
43 -> Maybe Tox.NodeInfo 43 -> Nonce32
44 -> IO (Maybe (Tox.Rendezvous, Tox.AnnounceResponse)) 44 -> Maybe Tox.NodeInfo
45 -> IO (Maybe (Tox.Rendezvous, Tox.AnnounceResponse))
45toxAnnounceSendData tox pubkey token = \case 46toxAnnounceSendData tox pubkey token = \case
46 Just ni -> Tox.putRendezvous (Tox.onionTimeout tox) 47 Just ni -> Tox.putRendezvous (Tox.onionTimeout tox)
47 (Tox.toxCryptoKeys tox) 48 (Tox.toxCryptoKeys tox)
@@ -56,7 +57,11 @@ toxAnnounceSendData tox pubkey token = \case
56-- 57--
57-- These hooks will be invoked in order to connect to *.tox hosts in a user's 58-- These hooks will be invoked in order to connect to *.tox hosts in a user's
58-- XMPP roster. 59-- XMPP roster.
59toxman :: Announcer -> [(String,TVar (BucketList Tox.NodeInfo))] -> Tox.Tox -> PresenceState -> ToxManager ConnectionKey 60toxman :: Announcer
61 -> [(String,TVar (BucketList Tox.NodeInfo))]
62 -> Tox.Tox JabberClients
63 -> PresenceState
64 -> ToxManager ConnectionKey
60toxman announcer toxbkts tox presence = ToxManager 65toxman announcer toxbkts tox presence = ToxManager
61 { activateAccount = \k pubname seckey -> do 66 { activateAccount = \k pubname seckey -> do
62 hPutStrLn stderr $ "toxman ACTIVATE " ++ show (Tox.key2id $ toPublic seckey) 67 hPutStrLn stderr $ "toxman ACTIVATE " ++ show (Tox.key2id $ toPublic seckey)
@@ -65,11 +70,12 @@ toxman announcer toxbkts tox presence = ToxManager
65 pubid = Tox.key2id pub 70 pubid = Tox.key2id pub
66 (acnt,newlyActive) <- atomically $ do 71 (acnt,newlyActive) <- atomically $ do
67 macnt <- HashMap.lookup pubid <$> readTVar accounts 72 macnt <- HashMap.lookup pubid <$> readTVar accounts
68 acnt <- maybe (newAccount seckey) return macnt 73 acnt <- maybe (newAccount seckey Map.empty) return macnt
69 rs <- readTVar $ clientRefs acnt 74 rs <- readTVar $ accountExtra acnt
70 writeTVar (clientRefs acnt) $! Set.insert k rs 75 perclient <- initPerClient
76 writeTVar (accountExtra acnt) $! Map.insert k perclient rs
71 modifyTVar accounts (HashMap.insert pubid acnt) 77 modifyTVar accounts (HashMap.insert pubid acnt)
72 if not (Set.null rs) 78 if not (Map.null rs)
73 then return (acnt,Nothing) 79 then return (acnt,Nothing)
74 else return (acnt,Just $ \nid -> foldr interweave [] 80 else return (acnt,Just $ \nid -> foldr interweave []
75 . map (R.kclosest (searchSpace (toxQSearch tox)) 81 . map (R.kclosest (searchSpace (toxQSearch tox))
@@ -101,11 +107,11 @@ toxman announcer toxbkts tox presence = ToxManager
101 forM mpubid $ \pubid -> do 107 forM mpubid $ \pubid -> do
102 refs <- do 108 refs <- do
103 macnt <- HashMap.lookup pubid <$> readTVar accounts 109 macnt <- HashMap.lookup pubid <$> readTVar accounts
104 rs <- fromMaybe Set.empty <$> mapM (readTVar . clientRefs) macnt 110 rs <- fromMaybe Map.empty <$> mapM (readTVar . accountExtra) macnt
105 forM_ macnt $ \acnt -> do 111 forM_ macnt $ \acnt -> do
106 modifyTVar' (clientRefs acnt) $ Set.delete k 112 modifyTVar' (accountExtra acnt) $ Map.delete k
107 return rs 113 return rs
108 if (Set.null $ refs Set.\\ Set.singleton k) then do 114 if (Map.null $ Map.delete k refs) then do
109 -- TODO 115 -- TODO
110 -- If this is the last reference to a non-connected contact: 116 -- If this is the last reference to a non-connected contact:
111 -- Stop the recurring search for that contact 117 -- Stop the recurring search for that contact