summaryrefslogtreecommitdiff
path: root/src/Network/Tox/ContactInfo.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 /src/Network/Tox/ContactInfo.hs
parent06229147ebfa72349baec5a2b55081341ff61908 (diff)
Parameterized Account to hold arbitrary information.
Diffstat (limited to 'src/Network/Tox/ContactInfo.hs')
-rw-r--r--src/Network/Tox/ContactInfo.hs52
1 files changed, 26 insertions, 26 deletions
diff --git a/src/Network/Tox/ContactInfo.hs b/src/Network/Tox/ContactInfo.hs
index 9f29d587..64ea861b 100644
--- a/src/Network/Tox/ContactInfo.hs
+++ b/src/Network/Tox/ContactInfo.hs
@@ -19,16 +19,16 @@ import Network.Tox.NodeId (id2key)
19import Network.Tox.Onion.Transport as Onion 19import Network.Tox.Onion.Transport as Onion
20import System.IO 20import System.IO
21 21
22newtype ContactInfo = ContactInfo 22newtype ContactInfo extra = ContactInfo
23 -- | Map our toxid public key to an Account record. 23 -- | Map our toxid public key to an Account record.
24 { accounts :: TVar (HashMap NodeId{-my userkey-} Account) 24 { accounts :: TVar (HashMap NodeId{-my userkey-} (Account extra))
25 } 25 }
26 26
27data Account = Account 27data Account extra = Account
28 { userSecret :: SecretKey -- local secret key 28 { userSecret :: SecretKey -- local secret key
29 , contacts :: TVar (HashMap NodeId{-friend's userkey-} Contact) -- received contact info 29 , contacts :: TVar (HashMap NodeId{-friend's userkey-} Contact) -- received contact info
30 , clientRefs :: TVar (Set ConnectionKey) -- reference count so we know when to send offline etc 30 , accountExtra :: TVar extra
31 , eventChan :: TChan ContactEvent 31 , eventChan :: TChan ContactEvent
32 } 32 }
33 33
34data ContactEvent = OnionRouted { contact :: PublicKey, onionRouted :: OnionData } 34data ContactEvent = OnionRouted { contact :: PublicKey, onionRouted :: OnionData }
@@ -44,16 +44,16 @@ data Contact = Contact
44 , contactPolicy :: TVar (Maybe Connection.Policy) 44 , contactPolicy :: TVar (Maybe Connection.Policy)
45 } 45 }
46 46
47newContactInfo :: IO ContactInfo 47newContactInfo :: IO (ContactInfo extra)
48newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty 48newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty
49 49
50myKeyPairs :: ContactInfo -> STM [(SecretKey,PublicKey)] 50myKeyPairs :: ContactInfo extra -> STM [(SecretKey,PublicKey)]
51myKeyPairs (ContactInfo accounts) = do 51myKeyPairs (ContactInfo accounts) = do
52 acnts <- readTVar accounts 52 acnts <- readTVar accounts
53 forM (HashMap.toList acnts) $ \(nid, Account{userSecret}) -> do 53 forM (HashMap.toList acnts) $ \(nid, Account{userSecret}) -> do
54 return (userSecret,id2key nid) 54 return (userSecret,id2key nid)
55 55
56updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () 56updateContactInfo :: ContactInfo extra -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO ()
57updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do 57updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do
58 hPutStrLn stderr "updateContactInfo!!!" 58 hPutStrLn stderr "updateContactInfo!!!"
59 now <- getPOSIXTime 59 now <- getPOSIXTime
@@ -69,7 +69,7 @@ initContact = Contact <$> newTVar Nothing
69 <*> newTVar Nothing 69 <*> newTVar Nothing
70 <*> newTVar Nothing 70 <*> newTVar Nothing
71 71
72updateAccount' :: PublicKey -> Account -> (Contact -> STM ()) -> STM () 72updateAccount' :: PublicKey -> Account extra -> (Contact -> STM ()) -> STM ()
73updateAccount' remoteUserKey acc updater = do 73updateAccount' remoteUserKey acc updater = do
74 let rkey = key2id remoteUserKey 74 let rkey = key2id remoteUserKey
75 cmap <- readTVar (contacts acc) 75 cmap <- readTVar (contacts acc)
@@ -80,7 +80,7 @@ updateAccount' remoteUserKey acc updater = do
80 return contact 80 return contact
81 updater contact 81 updater contact
82 82
83updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account -> STM () 83updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account extra -> STM ()
84updateAccount now remoteUserKey omsg acc = do 84updateAccount now remoteUserKey omsg acc = do
85 updateAccount' remoteUserKey acc $ onionUpdate now omsg 85 updateAccount' remoteUserKey acc $ onionUpdate now omsg
86 writeTChan (eventChan acc) $ OnionRouted remoteUserKey omsg 86 writeTChan (eventChan acc) $ OnionRouted remoteUserKey omsg
@@ -97,39 +97,39 @@ policyUpdate policy contact = writeTVar (contactPolicy contact) $ Just policy
97addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM () 97addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM ()
98addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr) 98addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr)
99 99
100setContactPolicy :: PublicKey -> Policy -> Account -> STM () 100setContactPolicy :: PublicKey -> Policy -> Account extra -> STM ()
101setContactPolicy remoteUserKey policy acc = do 101setContactPolicy remoteUserKey policy acc = do
102 updateAccount' remoteUserKey acc $ policyUpdate policy 102 updateAccount' remoteUserKey acc $ policyUpdate policy
103 writeTChan (eventChan acc) $ PolicyChange remoteUserKey policy 103 writeTChan (eventChan acc) $ PolicyChange remoteUserKey policy
104 104
105setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account -> STM () 105setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account extra -> STM ()
106setContactAddr now remoteUserKey addr acc = do 106setContactAddr now remoteUserKey addr acc = do
107 updateAccount' remoteUserKey acc $ addrUpdate now addr 107 updateAccount' remoteUserKey acc $ addrUpdate now addr
108 writeTChan (eventChan acc) $ AddrChange remoteUserKey addr 108 writeTChan (eventChan acc) $ AddrChange remoteUserKey addr
109 109
110setEstablished :: POSIXTime -> PublicKey -> Account -> STM () 110setEstablished :: POSIXTime -> PublicKey -> Account extra -> STM ()
111setEstablished now remoteUserKey acc = 111setEstablished now remoteUserKey acc =
112 writeTChan (eventChan acc) $ SessionEstablished remoteUserKey 112 writeTChan (eventChan acc) $ SessionEstablished remoteUserKey
113 113
114setTerminated :: POSIXTime -> PublicKey -> Account -> STM () 114setTerminated :: POSIXTime -> PublicKey -> Account extra -> STM ()
115setTerminated now remoteUserKey acc = 115setTerminated now remoteUserKey acc =
116 writeTChan (eventChan acc) $ SessionTerminated remoteUserKey 116 writeTChan (eventChan acc) $ SessionTerminated remoteUserKey
117 117
118 118
119addContactInfo :: ContactInfo -> SecretKey -> STM () 119addContactInfo :: ContactInfo extra -> SecretKey -> extra -> STM ()
120addContactInfo (ContactInfo as) sk = do 120addContactInfo (ContactInfo as) sk extra = do
121 a <- newAccount sk 121 a <- newAccount sk extra
122 modifyTVar' as $ HashMap.insert (key2id $ toPublic sk) a 122 modifyTVar' as $ HashMap.insert (key2id $ toPublic sk) a
123 123
124delContactInfo :: ContactInfo -> PublicKey -> STM () 124delContactInfo :: ContactInfo extra -> PublicKey -> STM ()
125delContactInfo (ContactInfo as) pk = modifyTVar' as $ HashMap.delete (key2id pk) 125delContactInfo (ContactInfo as) pk = modifyTVar' as $ HashMap.delete (key2id pk)
126 126
127newAccount :: SecretKey -> STM Account 127newAccount :: SecretKey -> extra -> STM (Account extra)
128newAccount sk = Account sk <$> newTVar HashMap.empty 128newAccount sk extra = Account sk <$> newTVar HashMap.empty
129 <*> newTVar Set.empty 129 <*> newTVar extra
130 <*> newBroadcastTChan 130 <*> newBroadcastTChan
131 131
132dnsPresentation :: ContactInfo -> STM String 132dnsPresentation :: ContactInfo extra -> STM String
133dnsPresentation (ContactInfo accsvar) = do 133dnsPresentation (ContactInfo accsvar) = do
134 accs <- readTVar accsvar 134 accs <- readTVar accsvar
135 ms <- forM accs $ \Account { userSecret = sec, contacts = cvar } -> do 135 ms <- forM accs $ \Account { userSecret = sec, contacts = cvar } -> do
@@ -150,7 +150,7 @@ dnsPresentation1 (nid,dk) = unlines
150type LocalKey = NodeId 150type LocalKey = NodeId
151type RemoteKey = NodeId 151type RemoteKey = NodeId
152 152
153friendRequests :: ContactInfo -> STM (HashMap LocalKey [(RemoteKey,DHT.FriendRequest)]) 153friendRequests :: ContactInfo extra -> STM (HashMap LocalKey [(RemoteKey,DHT.FriendRequest)])
154friendRequests (ContactInfo roster) = do 154friendRequests (ContactInfo roster) = do
155 accs <- readTVar roster 155 accs <- readTVar roster
156 forM accs $ \Account { userSecret = sec, contacts = cvar } -> do 156 forM accs $ \Account { userSecret = sec, contacts = cvar } -> do