diff options
author | joe <joe@jerkface.net> | 2018-06-18 00:49:38 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-18 00:49:38 -0400 |
commit | 07b1494c9d5c692371c9689a8f78f4cf7ee58732 (patch) | |
tree | aac158efc14bdb210717018704c43f2542804bf8 /src/Network/Tox/ContactInfo.hs | |
parent | 6de7e6d299254010ebe2fd3fc5fb7c7fd6c89fc6 (diff) |
Tox: Added timestamps to dhtkey and sockaddr information.
Diffstat (limited to 'src/Network/Tox/ContactInfo.hs')
-rw-r--r-- | src/Network/Tox/ContactInfo.hs | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/src/Network/Tox/ContactInfo.hs b/src/Network/Tox/ContactInfo.hs index 7f74caff..47c07237 100644 --- a/src/Network/Tox/ContactInfo.hs +++ b/src/Network/Tox/ContactInfo.hs | |||
@@ -4,6 +4,7 @@ module Network.Tox.ContactInfo where | |||
4 | import ConnectionKey | 4 | import ConnectionKey |
5 | import Connection | 5 | import Connection |
6 | 6 | ||
7 | import Data.Time.Clock.POSIX | ||
7 | import Control.Concurrent.STM | 8 | import Control.Concurrent.STM |
8 | import Control.Monad | 9 | import Control.Monad |
9 | import Crypto.PubKey.Curve25519 | 10 | import Crypto.PubKey.Curve25519 |
@@ -35,14 +36,10 @@ data ContactEvent = OnionRouted { contact :: PublicKey, onionRouted :: OnionDat | |||
35 | | AddrChange { contact :: PublicKey, addrChange :: SockAddr } | 36 | | AddrChange { contact :: PublicKey, addrChange :: SockAddr } |
36 | 37 | ||
37 | data Contact = Contact | 38 | data Contact = Contact |
38 | { contactKeyPacket :: TVar (Maybe (DHT.DHTPublicKey)) | 39 | { contactKeyPacket :: TVar (Maybe (POSIXTime,DHT.DHTPublicKey)) |
39 | , contactLastSeenAddr :: TVar (Maybe SockAddr) | 40 | , contactLastSeenAddr :: TVar (Maybe (POSIXTime,SockAddr)) |
40 | , contactFriendRequest :: TVar (Maybe (DHT.FriendRequest)) | 41 | , contactFriendRequest :: TVar (Maybe (POSIXTime,DHT.FriendRequest)) |
41 | , contactPolicy :: TVar (Maybe (Connection.Policy)) | 42 | , contactPolicy :: TVar (Maybe Connection.Policy) |
42 | -- Possible semantics | ||
43 | -- RefusingToConnect : rejected friend-request or blocked or unknown. | ||
44 | -- OpenToConnect : pending friend-request. | ||
45 | -- TryingToConnect : roster entry. | ||
46 | } | 43 | } |
47 | 44 | ||
48 | newContactInfo :: IO ContactInfo | 45 | newContactInfo :: IO ContactInfo |
@@ -57,10 +54,11 @@ myKeyPairs (ContactInfo accounts) = do | |||
57 | updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () | 54 | updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () |
58 | updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do | 55 | updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do |
59 | hPutStrLn stderr "updateContactInfo!!!" | 56 | hPutStrLn stderr "updateContactInfo!!!" |
57 | now <- getPOSIXTime | ||
60 | atomically $ do | 58 | atomically $ do |
61 | as <- readTVar (accounts roster) | 59 | as <- readTVar (accounts roster) |
62 | maybe (return ()) | 60 | maybe (return ()) |
63 | (updateAccount remoteUserKey omsg) | 61 | (updateAccount now remoteUserKey omsg) |
64 | $ HashMap.lookup (key2id localUserKey) as | 62 | $ HashMap.lookup (key2id localUserKey) as |
65 | 63 | ||
66 | initContact :: STM Contact | 64 | initContact :: STM Contact |
@@ -80,31 +78,31 @@ updateAccount' remoteUserKey acc updater = do | |||
80 | return contact | 78 | return contact |
81 | updater contact | 79 | updater contact |
82 | 80 | ||
83 | updateAccount :: PublicKey -> Onion.OnionData -> Account -> STM () | 81 | updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account -> STM () |
84 | updateAccount remoteUserKey omsg acc = do | 82 | updateAccount now remoteUserKey omsg acc = do |
85 | updateAccount' remoteUserKey acc $ onionUpdate omsg | 83 | updateAccount' remoteUserKey acc $ onionUpdate now omsg |
86 | writeTChan (eventChan acc) $ OnionRouted remoteUserKey omsg | 84 | writeTChan (eventChan acc) $ OnionRouted remoteUserKey omsg |
87 | 85 | ||
88 | onionUpdate :: OnionData -> Contact -> STM () | 86 | onionUpdate :: POSIXTime -> OnionData -> Contact -> STM () |
89 | onionUpdate (Onion.OnionDHTPublicKey dhtpk) contact | 87 | onionUpdate now (Onion.OnionDHTPublicKey dhtpk) contact |
90 | = writeTVar (contactKeyPacket contact) $ Just dhtpk | 88 | = writeTVar (contactKeyPacket contact) $ Just (now,dhtpk) |
91 | onionUpdate (Onion.OnionFriendRequest fr) contact | 89 | onionUpdate now (Onion.OnionFriendRequest fr) contact |
92 | = writeTVar (contactFriendRequest contact) $ Just fr | 90 | = writeTVar (contactFriendRequest contact) $ Just (now,fr) |
93 | 91 | ||
94 | policyUpdate :: Policy -> Contact -> STM () | 92 | policyUpdate :: Policy -> Contact -> STM () |
95 | policyUpdate policy contact = writeTVar (contactPolicy contact) $ Just policy | 93 | policyUpdate policy contact = writeTVar (contactPolicy contact) $ Just policy |
96 | 94 | ||
97 | addrUpdate :: SockAddr -> Contact -> STM () | 95 | addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM () |
98 | addrUpdate addr contact = writeTVar (contactLastSeenAddr contact) $ Just addr | 96 | addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr) |
99 | 97 | ||
100 | setContactPolicy :: PublicKey -> Policy -> Account -> STM () | 98 | setContactPolicy :: PublicKey -> Policy -> Account -> STM () |
101 | setContactPolicy remoteUserKey policy acc = do | 99 | setContactPolicy remoteUserKey policy acc = do |
102 | updateAccount' remoteUserKey acc $ policyUpdate policy | 100 | updateAccount' remoteUserKey acc $ policyUpdate policy |
103 | writeTChan (eventChan acc) $ PolicyChange remoteUserKey policy | 101 | writeTChan (eventChan acc) $ PolicyChange remoteUserKey policy |
104 | 102 | ||
105 | setContactAddr :: PublicKey -> SockAddr -> Account -> STM () | 103 | setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account -> STM () |
106 | setContactAddr remoteUserKey addr acc = do | 104 | setContactAddr now remoteUserKey addr acc = do |
107 | updateAccount' remoteUserKey acc $ addrUpdate addr | 105 | updateAccount' remoteUserKey acc $ addrUpdate now addr |
108 | writeTChan (eventChan acc) $ AddrChange remoteUserKey addr | 106 | writeTChan (eventChan acc) $ AddrChange remoteUserKey addr |
109 | 107 | ||
110 | 108 | ||
@@ -129,7 +127,7 @@ dnsPresentation (ContactInfo accsvar) = do | |||
129 | cs <- readTVar cvar | 127 | cs <- readTVar cvar |
130 | rs <- forM (HashMap.toList cs) $ \(nid,c) -> do | 128 | rs <- forM (HashMap.toList cs) $ \(nid,c) -> do |
131 | mkpkt <- readTVar (contactKeyPacket c) | 129 | mkpkt <- readTVar (contactKeyPacket c) |
132 | return $ fmap ((,) nid) mkpkt | 130 | return $ fmap (\(_,d) -> (nid,d)) mkpkt |
133 | return $ | 131 | return $ |
134 | "; local key = " ++ show (key2id $ toPublic sec) ++ "\n" | 132 | "; local key = " ++ show (key2id $ toPublic sec) ++ "\n" |
135 | ++ concatMap dnsPresentation1 (catMaybes rs) | 133 | ++ concatMap dnsPresentation1 (catMaybes rs) |
@@ -150,6 +148,6 @@ friendRequests (ContactInfo roster) = do | |||
150 | cs <- readTVar cvar | 148 | cs <- readTVar cvar |
151 | rs <- forM (HashMap.toList cs) $ \(nid,c) -> do | 149 | rs <- forM (HashMap.toList cs) $ \(nid,c) -> do |
152 | mfr <- readTVar (contactFriendRequest c) | 150 | mfr <- readTVar (contactFriendRequest c) |
153 | return $ fmap ((,) nid) mfr | 151 | return $ fmap (\(_,x) -> (nid,x)) mfr |
154 | return $ catMaybes rs | 152 | return $ catMaybes rs |
155 | 153 | ||