summaryrefslogtreecommitdiff
path: root/src/Network/Tox/ContactInfo.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-18 00:49:38 -0400
committerjoe <joe@jerkface.net>2018-06-18 00:49:38 -0400
commit07b1494c9d5c692371c9689a8f78f4cf7ee58732 (patch)
treeaac158efc14bdb210717018704c43f2542804bf8 /src/Network/Tox/ContactInfo.hs
parent6de7e6d299254010ebe2fd3fc5fb7c7fd6c89fc6 (diff)
Tox: Added timestamps to dhtkey and sockaddr information.
Diffstat (limited to 'src/Network/Tox/ContactInfo.hs')
-rw-r--r--src/Network/Tox/ContactInfo.hs46
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
4import ConnectionKey 4import ConnectionKey
5import Connection 5import Connection
6 6
7import Data.Time.Clock.POSIX
7import Control.Concurrent.STM 8import Control.Concurrent.STM
8import Control.Monad 9import Control.Monad
9import Crypto.PubKey.Curve25519 10import 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
37data Contact = Contact 38data 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
48newContactInfo :: IO ContactInfo 45newContactInfo :: IO ContactInfo
@@ -57,10 +54,11 @@ myKeyPairs (ContactInfo accounts) = do
57updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () 54updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO ()
58updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do 55updateContactInfo 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
66initContact :: STM Contact 64initContact :: 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
83updateAccount :: PublicKey -> Onion.OnionData -> Account -> STM () 81updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account -> STM ()
84updateAccount remoteUserKey omsg acc = do 82updateAccount 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
88onionUpdate :: OnionData -> Contact -> STM () 86onionUpdate :: POSIXTime -> OnionData -> Contact -> STM ()
89onionUpdate (Onion.OnionDHTPublicKey dhtpk) contact 87onionUpdate now (Onion.OnionDHTPublicKey dhtpk) contact
90 = writeTVar (contactKeyPacket contact) $ Just dhtpk 88 = writeTVar (contactKeyPacket contact) $ Just (now,dhtpk)
91onionUpdate (Onion.OnionFriendRequest fr) contact 89onionUpdate now (Onion.OnionFriendRequest fr) contact
92 = writeTVar (contactFriendRequest contact) $ Just fr 90 = writeTVar (contactFriendRequest contact) $ Just (now,fr)
93 91
94policyUpdate :: Policy -> Contact -> STM () 92policyUpdate :: Policy -> Contact -> STM ()
95policyUpdate policy contact = writeTVar (contactPolicy contact) $ Just policy 93policyUpdate policy contact = writeTVar (contactPolicy contact) $ Just policy
96 94
97addrUpdate :: SockAddr -> Contact -> STM () 95addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM ()
98addrUpdate addr contact = writeTVar (contactLastSeenAddr contact) $ Just addr 96addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr)
99 97
100setContactPolicy :: PublicKey -> Policy -> Account -> STM () 98setContactPolicy :: PublicKey -> Policy -> Account -> STM ()
101setContactPolicy remoteUserKey policy acc = do 99setContactPolicy 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
105setContactAddr :: PublicKey -> SockAddr -> Account -> STM () 103setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account -> STM ()
106setContactAddr remoteUserKey addr acc = do 104setContactAddr 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