diff options
author | joe <joe@jerkface.net> | 2017-10-24 20:46:35 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-10-24 20:46:35 -0400 |
commit | f0f355d6ff8a68b5240301f882f6d5a9a77fdba1 (patch) | |
tree | bfb415ea0ecdd51650e28fc60c9a8ba4dcc7f150 /Roster.hs | |
parent | c31ed656d55bbdb387d91464e51840e90503223a (diff) |
Added Roster data and dhtkey handler.
Diffstat (limited to 'Roster.hs')
-rw-r--r-- | Roster.hs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/Roster.hs b/Roster.hs new file mode 100644 index 00000000..94ab462d --- /dev/null +++ b/Roster.hs | |||
@@ -0,0 +1,55 @@ | |||
1 | {-# LANGUAGE NamedFieldPuns #-} | ||
2 | module Roster where | ||
3 | |||
4 | import Crypto.PubKey.Curve25519 | ||
5 | import Network.Tox.Onion.Transport as Onion | ||
6 | import Network.Tox.DHT.Transport as DHT | ||
7 | import Network.Tox.NodeId | ||
8 | import Control.Concurrent.STM | ||
9 | import qualified Data.HashMap.Strict as HashMap | ||
10 | import Data.HashMap.Strict (HashMap) | ||
11 | |||
12 | newtype Roster = Roster { accounts :: TVar (HashMap NodeId Account) } | ||
13 | |||
14 | data Account = Account | ||
15 | { userSecret :: SecretKey -- local secret key | ||
16 | , contacts :: TVar (HashMap NodeId DHT.DHTPublicKey) -- received contact info | ||
17 | } | ||
18 | |||
19 | newRoster :: IO Roster | ||
20 | newRoster = atomically $ Roster <$> newTVar HashMap.empty | ||
21 | |||
22 | newAccount :: SecretKey -> STM Account | ||
23 | newAccount sk = Account sk <$> newTVar HashMap.empty | ||
24 | |||
25 | addRoster :: Roster -> SecretKey -> STM () | ||
26 | addRoster (Roster as) sk = do | ||
27 | a <- newAccount sk | ||
28 | modifyTVar' as $ HashMap.insert (key2id $ toPublic sk) a | ||
29 | |||
30 | delRoster :: Roster -> PublicKey -> STM () | ||
31 | delRoster (Roster as) pk = modifyTVar' as $ HashMap.delete (key2id pk) | ||
32 | |||
33 | updateRoster :: Roster -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () | ||
34 | updateRoster roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do | ||
35 | atomically $ do | ||
36 | as <- readTVar (accounts roster) | ||
37 | maybe (return ()) | ||
38 | (updateAccount remoteUserKey omsg) | ||
39 | $ HashMap.lookup (key2id localUserKey) as | ||
40 | |||
41 | |||
42 | updateConcts :: DHT.DHTPublicKey -> Maybe DHT.DHTPublicKey -> Maybe DHT.DHTPublicKey | ||
43 | updateConcts new (Just old) | DHT.dhtpkNonce old < DHT.dhtpkNonce new = Just new | ||
44 | updateConcts new Nothing = Just new | ||
45 | updateConcts _ old = old | ||
46 | |||
47 | |||
48 | updateAccount :: PublicKey -> Onion.OnionData -> Account -> STM () | ||
49 | updateAccount remoteUserKey (Onion.OnionDHTPublicKey dhtpk) acc = do | ||
50 | modifyTVar' (contacts acc) $ HashMap.alter (updateConcts dhtpk) | ||
51 | (key2id remoteUserKey) | ||
52 | |||
53 | updateAccount remoteUserKey (Onion.OnionFriendRequest fr) acc = do | ||
54 | -- TODO | ||
55 | return () | ||