summaryrefslogtreecommitdiff
path: root/dht/Presence/Presence.hs
diff options
context:
space:
mode:
Diffstat (limited to 'dht/Presence/Presence.hs')
-rw-r--r--dht/Presence/Presence.hs39
1 files changed, 27 insertions, 12 deletions
diff --git a/dht/Presence/Presence.hs b/dht/Presence/Presence.hs
index 866aad78..c54c3659 100644
--- a/dht/Presence/Presence.hs
+++ b/dht/Presence/Presence.hs
@@ -13,6 +13,7 @@ import Control.Concurrent
13import Control.Concurrent.Lifted.Instrument 13import Control.Concurrent.Lifted.Instrument
14#endif 14#endif
15 15
16import Control.Arrow
16import Control.Concurrent.STM 17import Control.Concurrent.STM
17import Control.Monad.Trans 18import Control.Monad.Trans
18import Network.Socket ( SockAddr(..) ) 19import Network.Socket ( SockAddr(..) )
@@ -133,17 +134,21 @@ newPresenceState cw toxman sv man = atomically $ do
133 return $ st { toxManager = fmap ($ st) toxman } 134 return $ st { toxManager = fmap ($ st) toxman }
134 135
135 136
136nameForClient :: PresenceState stat -> ClientAddress -> IO Text 137nameForClient' :: PresenceState stat -> Maybe Text -> Maybe Text -> ClientAddress -> IO Text
137nameForClient state k = do 138nameForClient' state mbNameForMe mbTheirName k = do
138 mc <- atomically $ do 139 mc <- atomically $ do
139 cmap <- readTVar (clients state) 140 cmap <- readTVar (clients state)
140 return $ Map.lookup k cmap 141 return $ Map.lookup k cmap
141 case mc of 142 case mc of
142 Nothing -> textHostName 143 Nothing -> textHostName -- TODO: We can use mbNameForMe to initialize the clientProfile
143 Just client -> case clientProfile client of 144 Just client -> case clientProfile client of
144 "." -> textHostName 145 "." -> textHostName
145 profile -> return profile 146 profile -> return profile
146 147
148nameForClient :: PresenceState stat -> ClientAddress -> IO Text
149nameForClient state k = nameForClient' state Nothing Nothing k
150
151
147presenceHooks :: PresenceState stat -> Map Text MUC 152presenceHooks :: PresenceState stat -> Map Text MUC
148 -> Int 153 -> Int
149 -> Maybe SockAddr -- ^ client-to-server bind address 154 -> Maybe SockAddr -- ^ client-to-server bind address
@@ -152,7 +157,7 @@ presenceHooks :: PresenceState stat -> Map Text MUC
152presenceHooks state chats verbosity mclient mpeer = XMPPServerParameters 157presenceHooks state chats verbosity mclient mpeer = XMPPServerParameters
153 { xmppChooseResourceName = chooseResourceName state 158 { xmppChooseResourceName = chooseResourceName state
154 , xmppTellClientHisName = tellClientHisName state 159 , xmppTellClientHisName = tellClientHisName state
155 , xmppTellMyNameToClient = nameForClient state 160 , xmppTellMyNameToClient = nameForClient' state
156 , xmppTellMyNameToPeer = \(Local addr) -> return $ addrToText addr 161 , xmppTellMyNameToPeer = \(Local addr) -> return $ addrToText addr
157 , xmppTellPeerHisName = return . peerKeyToText 162 , xmppTellPeerHisName = return . peerKeyToText
158 , xmppNewConnection = newConn state 163 , xmppNewConnection = newConn state
@@ -726,8 +731,12 @@ deliverMessage state fail msg =
726 fromMaybe (do dput XJabber $ "Unknown peer " ++ show senderk 731 fromMaybe (do dput XJabber $ "Unknown peer " ++ show senderk
727 fail) 732 fail)
728 $ Map.lookup senderk pchans 733 $ Map.lookup senderk pchans
729 <&> \(Conn { connChan = sender_chan 734 <&> \Conn { connChan = sender_chan
730 , auxData = ConnectionData (Left laddr) ctyp cprof _ }) -> do 735 , auxData = ConnectionData { cdAddr = Left laddr
736 , cdType = ctyp
737 , cdProfile = cprof
738 }
739 } -> do
731 fromMaybe (do dput XJabber $ "Message missing \"to\" attribute." 740 fromMaybe (do dput XJabber $ "Message missing \"to\" attribute."
732 fail) 741 fail)
733 $ (stanzaTo msg) <&> \to -> do 742 $ (stanzaTo msg) <&> \to -> do
@@ -916,9 +925,7 @@ informPeerPresence state k stanza = do
916 dput XJabber $ "xmppInformPeerPresence (length clients="++show (length clients)++")" 925 dput XJabber $ "xmppInformPeerPresence (length clients="++show (length clients)++")"
917 (ctyp,cprof) <- atomically $ do 926 (ctyp,cprof) <- atomically $ do
918 mconn <- Map.lookup k <$> readTVar (pkeyToChan state) 927 mconn <- Map.lookup k <$> readTVar (pkeyToChan state)
919 return $ fromMaybe (XMPP,".") $ do 928 return $ fromMaybe (XMPP,".") $ (cdType &&& cdProfile) . auxData <$> mconn
920 ConnectionData _ ctyp cprof _ <- auxData <$> mconn
921 return (ctyp,cprof)
922 forM_ clients $ \(ck,con,client) -> do 929 forM_ clients $ \(ck,con,client) -> do
923 -- (TODO: appropriately authorized clients only.) 930 -- (TODO: appropriately authorized clients only.)
924 -- For now, all "available" clients (available = sent initial presence) 931 -- For now, all "available" clients (available = sent initial presence)
@@ -1263,7 +1270,11 @@ peerSubscriptionRequest state fail k stanza chan = do
1263 cmap <- readTVar (clients state) 1270 cmap <- readTVar (clients state)
1264 return (pktc,cktc,cmap) 1271 return (pktc,cktc,cmap)
1265 fromMaybe fail $ (Map.lookup k pktc) 1272 fromMaybe fail $ (Map.lookup k pktc)
1266 <&> \Conn { auxData=ConnectionData (Left laddr) ctyp profile _ } -> do 1273 <&> \Conn { auxData = ConnectionData { cdAddr = Left laddr
1274 , cdType = ctyp
1275 , cdProfile = profile
1276 }
1277 } -> do
1267 (mine,totup) <- case (ctyp,profile) of 1278 (mine,totup) <- case (ctyp,profile) of
1268 (Tox,p) -> let (u,h,r) = splitJID to 1279 (Tox,p) -> let (u,h,r) = splitJID to
1269 in return ( h == p, (u,h,r) ) 1280 in return ( h == p, (u,h,r) )
@@ -1412,8 +1423,12 @@ peerInformSubscription state fail k stanza = do
1412 cmap <- readTVar (clients state) 1423 cmap <- readTVar (clients state)
1413 return (pktc,cktc,cmap) 1424 return (pktc,cktc,cmap)
1414 fromMaybe fail $ Map.lookup k ktc 1425 fromMaybe fail $ Map.lookup k ktc
1415 <&> \(Conn { connChan=sender_chan 1426 <&> \Conn { connChan = sender_chan
1416 , auxData =ConnectionData (Left laddr) ctyp profile _ }) -> do 1427 , auxData = ConnectionData { cdAddr = Left laddr
1428 , cdType = ctyp
1429 , cdProfile = profile }
1430 } -> do
1431
1417 let man = manager state profile 1432 let man = manager state profile
1418 (from_u,from_h,_) <- case ctyp of 1433 (from_u,from_h,_) <- case ctyp of
1419 Tox -> return $ splitJID from 1434 Tox -> return $ splitJID from