diff options
Diffstat (limited to 'ToxManager.hs')
-rw-r--r-- | ToxManager.hs | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/ToxManager.hs b/ToxManager.hs index 14b2070c..793a3b8a 100644 --- a/ToxManager.hs +++ b/ToxManager.hs | |||
@@ -9,16 +9,19 @@ module ToxManager where | |||
9 | import Announcer | 9 | import Announcer |
10 | import Announcer.Tox | 10 | import Announcer.Tox |
11 | import ClientState | 11 | import ClientState |
12 | import ConfigFiles | ||
12 | import Control.Arrow | 13 | import Control.Arrow |
13 | import Control.Concurrent.STM | 14 | import Control.Concurrent.STM |
14 | import Control.Monad | 15 | import Control.Monad |
15 | import Crypto.Tox | 16 | import Crypto.Tox |
16 | import qualified Data.ByteArray as BA | ||
17 | import Data.Bits | 17 | import Data.Bits |
18 | import qualified Data.ByteArray as BA | ||
18 | import Data.Function | 19 | import Data.Function |
19 | import qualified Data.HashMap.Strict as HashMap | 20 | import qualified Data.HashMap.Strict as HashMap |
21 | import Data.List | ||
20 | import qualified Data.Map as Map | 22 | import qualified Data.Map as Map |
21 | import Data.Maybe | 23 | import Data.Maybe |
24 | import Data.Ord | ||
22 | import qualified Data.Set as Set | 25 | import qualified Data.Set as Set |
23 | import qualified Data.Text as T | 26 | import qualified Data.Text as T |
24 | ;import Data.Text (Text) | 27 | ;import Data.Text (Text) |
@@ -56,9 +59,9 @@ import Control.Concurrent.Lifted.Instrument | |||
56 | import Control.Concurrent.Lifted | 59 | import Control.Concurrent.Lifted |
57 | import GHC.Conc (labelThread) | 60 | import GHC.Conc (labelThread) |
58 | #endif | 61 | #endif |
59 | import GHC.Conc (unsafeIOToSTM) | ||
60 | import Connection | 62 | import Connection |
61 | import Connection.Tcp (TCPStatus) | 63 | import Connection.Tcp (TCPStatus) |
64 | import GHC.Conc (unsafeIOToSTM) | ||
62 | 65 | ||
63 | 66 | ||
64 | toxAnnounceSendData :: Tox.Tox JabberClients | 67 | toxAnnounceSendData :: Tox.Tox JabberClients |
@@ -476,7 +479,8 @@ realShakeHands myseckey theirpubkey theirDhtKey allsessions saddr cookie = do | |||
476 | 479 | ||
477 | 480 | ||
478 | dispatch :: ToxToXMPP -> ContactEvent -> IO () | 481 | dispatch :: ToxToXMPP -> ContactEvent -> IO () |
479 | dispatch tx (SessionEstablished theirKey) = stopConnecting tx theirKey "established" | 482 | dispatch tx (SessionEstablished theirKey) = do stopConnecting tx theirKey "established" |
483 | updateRoster tx theirKey | ||
480 | dispatch tx (SessionTerminated theirKey) = startConnecting tx theirKey "terminated" | 484 | dispatch tx (SessionTerminated theirKey) = startConnecting tx theirKey "terminated" |
481 | dispatch tx (AddrChange theirKey saddr) = gotAddr saddr tx theirKey | 485 | dispatch tx (AddrChange theirKey saddr) = gotAddr saddr tx theirKey |
482 | dispatch tx (PolicyChange theirkey TryingToConnect ) = startConnecting tx theirkey "policy" | 486 | dispatch tx (PolicyChange theirkey TryingToConnect ) = startConnecting tx theirkey "policy" |
@@ -521,6 +525,30 @@ akeyConnect announcer me them = | |||
521 | packAnnounceKey announcer $ "connect(" ++ (take 8 $ show me) ++ ")" ++ show (key2id them) | 525 | packAnnounceKey announcer $ "connect(" ++ (take 8 $ show me) ++ ")" ++ show (key2id them) |
522 | 526 | ||
523 | 527 | ||
528 | |||
529 | -- | Because Tox does not have a friend-request response, we consider an | ||
530 | -- established connection to be an implicit two-way subscription. | ||
531 | updateRoster :: ToxToXMPP -> PublicKey -> IO () | ||
532 | updateRoster tx them = do | ||
533 | let me = toPublic $ userSecret $ txAccount tx | ||
534 | profile = T.pack $ show (key2id me) ++ ".tox" | ||
535 | jid = T.pack $ show (key2id them) ++ ".tox" | ||
536 | man = manager (txPresence tx) profile | ||
537 | addr <- do | ||
538 | u <- xor24 <$> hash24 me <*> hash24 them | ||
539 | return [ addrToPeerKey . Remote . peerAddress . uniqueAsKey $ u ] | ||
540 | mp <- atomically $ Map.lookup profile <$> readTVar (clientsByProfile $ txPresence tx) | ||
541 | forM_ mp $ \LocalPresence{networkClients} -> do | ||
542 | let css = groupBy ((==) `on` clientUser) -- We treat all clients from a single user as one. | ||
543 | $ sortBy (comparing clientUser) | ||
544 | $ Map.elems networkClients | ||
545 | forM_ css $ \(c:_) -> do | ||
546 | let user = clientUser c | ||
547 | addToRosterFile man modifyBuddies user profile jid addr | ||
548 | addToRosterFile man modifySubscribers user profile jid addr | ||
549 | -- Cancel friend-request sending. | ||
550 | modifyRosterFile man modifySolicited user profile jid addr False True | ||
551 | |||
524 | -- | Returns a list of nospam values to use for friend requests to send to a | 552 | -- | Returns a list of nospam values to use for friend requests to send to a |
525 | -- remote peer. This list is non-empty only when it is desirable to send | 553 | -- remote peer. This list is non-empty only when it is desirable to send |
526 | -- friend requests. | 554 | -- friend requests. |