diff options
Diffstat (limited to 'ToxToXMPP.hs')
-rw-r--r-- | ToxToXMPP.hs | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/ToxToXMPP.hs b/ToxToXMPP.hs index 12a08901..26cfa58c 100644 --- a/ToxToXMPP.hs +++ b/ToxToXMPP.hs | |||
@@ -7,6 +7,7 @@ import Data.XML.Types as XML | |||
7 | import Network.Tox.Crypto.Transport as Tox | 7 | import Network.Tox.Crypto.Transport as Tox |
8 | 8 | ||
9 | import Announcer | 9 | import Announcer |
10 | import ClientState | ||
10 | import Connection | 11 | import Connection |
11 | import Connection.Tox as Connection | 12 | import Connection.Tox as Connection |
12 | import Control.Concurrent.STM | 13 | import Control.Concurrent.STM |
@@ -16,6 +17,8 @@ import Crypto.Tox | |||
16 | import Data.Bits | 17 | import Data.Bits |
17 | import Data.Function | 18 | import Data.Function |
18 | import qualified Data.HashMap.Strict as HashMap | 19 | import qualified Data.HashMap.Strict as HashMap |
20 | import qualified Data.Map as Map | ||
21 | import qualified Data.Set as Set | ||
19 | import qualified Data.Set as Set | 22 | import qualified Data.Set as Set |
20 | import qualified Data.Text as T | 23 | import qualified Data.Text as T |
21 | ;import Data.Text (Text) | 24 | ;import Data.Text (Text) |
@@ -51,18 +54,27 @@ key2jid nospam key = T.pack $ show $ NoSpamId nsp key | |||
51 | nlo = fromIntegral (0x0FFFF .&. nospam) :: Word16 | 54 | nlo = fromIntegral (0x0FFFF .&. nospam) :: Word16 |
52 | nhi = fromIntegral (0x0FFFF .&. (nospam `shiftR` 16)) :: Word16 | 55 | nhi = fromIntegral (0x0FFFF .&. (nospam `shiftR` 16)) :: Word16 |
53 | 56 | ||
54 | dispatch :: Account -> Conn -> ContactEvent -> IO () | 57 | dispatch :: Account -> PresenceState -> ContactEvent -> IO () |
55 | dispatch acnt conn (PolicyChange theirkey policy ) = return () -- todo | 58 | dispatch acnt st (PolicyChange theirkey policy ) = return () -- todo |
56 | dispatch acnt conn (OnionRouted theirkey (OnionDHTPublicKey pkey)) = return () -- todo | 59 | dispatch acnt st (OnionRouted theirkey (OnionDHTPublicKey pkey)) = return () -- todo |
57 | dispatch acnt conn (OnionRouted theirkey (OnionFriendRequest fr) ) = do | 60 | dispatch acnt st (OnionRouted theirkey (OnionFriendRequest fr) ) = do |
58 | let self = accountJID acnt | 61 | k2c <- atomically $ do |
59 | theirjid = key2jid (friendNoSpam fr) theirkey | 62 | refs <- readTVar (clientRefs acnt) |
60 | ask <- presenceSolicitation theirjid self | 63 | k2c <- Map.filterWithKey (\k _ -> k `Set.member` refs) <$> readTVar (keyToChan st) |
61 | sendModifiedStanzaToClient ask (connChan conn) | 64 | clients <- readTVar (clients st) |
65 | return $ Map.intersectionWith (,) k2c clients | ||
66 | let theirjid = key2jid (friendNoSpam fr) theirkey | ||
67 | forM_ k2c $ \(conn,client) -> do | ||
68 | self <- localJID (clientUser client) (clientProfile client) (clientResource client) | ||
69 | ask <- presenceSolicitation theirjid self | ||
70 | -- TODO Send friend-request text as an instant message or at least | ||
71 | -- embed it in the stanza as a <status> element. | ||
72 | sendModifiedStanzaToClient ask (connChan conn) | ||
62 | 73 | ||
63 | forkAccountWatcher :: Account -> Tox -> Conn -> IO ThreadId | 74 | forkAccountWatcher :: Account -> Tox -> PresenceState -> IO ThreadId |
64 | forkAccountWatcher acc tox conn = forkIO $ do | 75 | forkAccountWatcher acc tox st = forkIO $ do |
65 | myThreadId >>= flip labelThread "tox-account" | 76 | myThreadId >>= flip labelThread ("tox-xmpp:" |
77 | ++ show (key2id $ toPublic $ userSecret acc)) | ||
66 | (chan,contacts) <- atomically $ do | 78 | (chan,contacts) <- atomically $ do |
67 | chan <- dupTChan $ eventChan acc -- duplicate broadcast channel for reading. | 79 | chan <- dupTChan $ eventChan acc -- duplicate broadcast channel for reading. |
68 | contacts <- readTVar (contacts acc) | 80 | contacts <- readTVar (contacts acc) |
@@ -77,5 +89,5 @@ forkAccountWatcher acc tox conn = forkIO $ do | |||
77 | refs <- readTVar $ clientRefs acc | 89 | refs <- readTVar $ clientRefs acc |
78 | check $ Set.null refs | 90 | check $ Set.null refs |
79 | return Nothing | 91 | return Nothing |
80 | forM_ mev $ \ev -> dispatch acc conn ev >> loop | 92 | forM_ mev $ \ev -> dispatch acc st ev >> loop |
81 | 93 | ||