diff options
author | joe <joe@jerkface.net> | 2018-06-20 21:30:47 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2018-06-20 21:30:47 -0400 |
commit | 7a16f326fbe7429792b155c4a963bad1f50dcbda (patch) | |
tree | bc134b0d5989a66205b30df5dc78879dfe2011e6 /src/Network/Tox/ContactInfo.hs | |
parent | 06229147ebfa72349baec5a2b55081341ff61908 (diff) |
Parameterized Account to hold arbitrary information.
Diffstat (limited to 'src/Network/Tox/ContactInfo.hs')
-rw-r--r-- | src/Network/Tox/ContactInfo.hs | 52 |
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) | |||
19 | import Network.Tox.Onion.Transport as Onion | 19 | import Network.Tox.Onion.Transport as Onion |
20 | import System.IO | 20 | import System.IO |
21 | 21 | ||
22 | newtype ContactInfo = ContactInfo | 22 | newtype 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 | ||
27 | data Account = Account | 27 | data 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 | ||
34 | data ContactEvent = OnionRouted { contact :: PublicKey, onionRouted :: OnionData } | 34 | data 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 | ||
47 | newContactInfo :: IO ContactInfo | 47 | newContactInfo :: IO (ContactInfo extra) |
48 | newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty | 48 | newContactInfo = atomically $ ContactInfo <$> newTVar HashMap.empty |
49 | 49 | ||
50 | myKeyPairs :: ContactInfo -> STM [(SecretKey,PublicKey)] | 50 | myKeyPairs :: ContactInfo extra -> STM [(SecretKey,PublicKey)] |
51 | myKeyPairs (ContactInfo accounts) = do | 51 | myKeyPairs (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 | ||
56 | updateContactInfo :: ContactInfo -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () | 56 | updateContactInfo :: ContactInfo extra -> Onion.AnnouncedRendezvous -> (PublicKey,Onion.OnionData) -> IO () |
57 | updateContactInfo roster Onion.AnnouncedRendezvous{remoteUserKey} (localUserKey,omsg) = do | 57 | updateContactInfo 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 | ||
72 | updateAccount' :: PublicKey -> Account -> (Contact -> STM ()) -> STM () | 72 | updateAccount' :: PublicKey -> Account extra -> (Contact -> STM ()) -> STM () |
73 | updateAccount' remoteUserKey acc updater = do | 73 | updateAccount' 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 | ||
83 | updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account -> STM () | 83 | updateAccount :: POSIXTime -> PublicKey -> Onion.OnionData -> Account extra -> STM () |
84 | updateAccount now remoteUserKey omsg acc = do | 84 | updateAccount 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 | |||
97 | addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM () | 97 | addrUpdate :: POSIXTime -> SockAddr -> Contact -> STM () |
98 | addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr) | 98 | addrUpdate now addr contact = writeTVar (contactLastSeenAddr contact) $ Just (now,addr) |
99 | 99 | ||
100 | setContactPolicy :: PublicKey -> Policy -> Account -> STM () | 100 | setContactPolicy :: PublicKey -> Policy -> Account extra -> STM () |
101 | setContactPolicy remoteUserKey policy acc = do | 101 | setContactPolicy 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 | ||
105 | setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account -> STM () | 105 | setContactAddr :: POSIXTime -> PublicKey -> SockAddr -> Account extra -> STM () |
106 | setContactAddr now remoteUserKey addr acc = do | 106 | setContactAddr 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 | ||
110 | setEstablished :: POSIXTime -> PublicKey -> Account -> STM () | 110 | setEstablished :: POSIXTime -> PublicKey -> Account extra -> STM () |
111 | setEstablished now remoteUserKey acc = | 111 | setEstablished now remoteUserKey acc = |
112 | writeTChan (eventChan acc) $ SessionEstablished remoteUserKey | 112 | writeTChan (eventChan acc) $ SessionEstablished remoteUserKey |
113 | 113 | ||
114 | setTerminated :: POSIXTime -> PublicKey -> Account -> STM () | 114 | setTerminated :: POSIXTime -> PublicKey -> Account extra -> STM () |
115 | setTerminated now remoteUserKey acc = | 115 | setTerminated now remoteUserKey acc = |
116 | writeTChan (eventChan acc) $ SessionTerminated remoteUserKey | 116 | writeTChan (eventChan acc) $ SessionTerminated remoteUserKey |
117 | 117 | ||
118 | 118 | ||
119 | addContactInfo :: ContactInfo -> SecretKey -> STM () | 119 | addContactInfo :: ContactInfo extra -> SecretKey -> extra -> STM () |
120 | addContactInfo (ContactInfo as) sk = do | 120 | addContactInfo (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 | ||
124 | delContactInfo :: ContactInfo -> PublicKey -> STM () | 124 | delContactInfo :: ContactInfo extra -> PublicKey -> STM () |
125 | delContactInfo (ContactInfo as) pk = modifyTVar' as $ HashMap.delete (key2id pk) | 125 | delContactInfo (ContactInfo as) pk = modifyTVar' as $ HashMap.delete (key2id pk) |
126 | 126 | ||
127 | newAccount :: SecretKey -> STM Account | 127 | newAccount :: SecretKey -> extra -> STM (Account extra) |
128 | newAccount sk = Account sk <$> newTVar HashMap.empty | 128 | newAccount sk extra = Account sk <$> newTVar HashMap.empty |
129 | <*> newTVar Set.empty | 129 | <*> newTVar extra |
130 | <*> newBroadcastTChan | 130 | <*> newBroadcastTChan |
131 | 131 | ||
132 | dnsPresentation :: ContactInfo -> STM String | 132 | dnsPresentation :: ContactInfo extra -> STM String |
133 | dnsPresentation (ContactInfo accsvar) = do | 133 | dnsPresentation (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 | |||
150 | type LocalKey = NodeId | 150 | type LocalKey = NodeId |
151 | type RemoteKey = NodeId | 151 | type RemoteKey = NodeId |
152 | 152 | ||
153 | friendRequests :: ContactInfo -> STM (HashMap LocalKey [(RemoteKey,DHT.FriendRequest)]) | 153 | friendRequests :: ContactInfo extra -> STM (HashMap LocalKey [(RemoteKey,DHT.FriendRequest)]) |
154 | friendRequests (ContactInfo roster) = do | 154 | friendRequests (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 |