summaryrefslogtreecommitdiff
path: root/ToxToXMPP.hs
diff options
context:
space:
mode:
Diffstat (limited to 'ToxToXMPP.hs')
-rw-r--r--ToxToXMPP.hs36
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
7import Network.Tox.Crypto.Transport as Tox 7import Network.Tox.Crypto.Transport as Tox
8 8
9import Announcer 9import Announcer
10import ClientState
10import Connection 11import Connection
11import Connection.Tox as Connection 12import Connection.Tox as Connection
12import Control.Concurrent.STM 13import Control.Concurrent.STM
@@ -16,6 +17,8 @@ import Crypto.Tox
16import Data.Bits 17import Data.Bits
17import Data.Function 18import Data.Function
18import qualified Data.HashMap.Strict as HashMap 19import qualified Data.HashMap.Strict as HashMap
20import qualified Data.Map as Map
21import qualified Data.Set as Set
19import qualified Data.Set as Set 22import qualified Data.Set as Set
20import qualified Data.Text as T 23import 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
54dispatch :: Account -> Conn -> ContactEvent -> IO () 57dispatch :: Account -> PresenceState -> ContactEvent -> IO ()
55dispatch acnt conn (PolicyChange theirkey policy ) = return () -- todo 58dispatch acnt st (PolicyChange theirkey policy ) = return () -- todo
56dispatch acnt conn (OnionRouted theirkey (OnionDHTPublicKey pkey)) = return () -- todo 59dispatch acnt st (OnionRouted theirkey (OnionDHTPublicKey pkey)) = return () -- todo
57dispatch acnt conn (OnionRouted theirkey (OnionFriendRequest fr) ) = do 60dispatch 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
63forkAccountWatcher :: Account -> Tox -> Conn -> IO ThreadId 74forkAccountWatcher :: Account -> Tox -> PresenceState -> IO ThreadId
64forkAccountWatcher acc tox conn = forkIO $ do 75forkAccountWatcher 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