summaryrefslogtreecommitdiff
path: root/Presence
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2018-06-21 22:53:57 -0400
committerjoe <joe@jerkface.net>2018-06-21 22:53:57 -0400
commit44932179bf68377dc6a3b940ca8979bcd717beab (patch)
tree728c84abe491098ea2fbfb43f65ad2c85e033624 /Presence
parent93526173379d57db17d6d7c0287d52d2c975203c (diff)
Remember whether peers are Tox or XMPP connections.
Diffstat (limited to 'Presence')
-rw-r--r--Presence/Presence.hs30
-rw-r--r--Presence/XMPPServer.hs45
2 files changed, 43 insertions, 32 deletions
diff --git a/Presence/Presence.hs b/Presence/Presence.hs
index 678a5c99..daa93716 100644
--- a/Presence/Presence.hs
+++ b/Presence/Presence.hs
@@ -369,7 +369,7 @@ rosterGetSubscribers :: PresenceState -> ConnectionKey -> IO [Text]
369rosterGetSubscribers = rosterGetStuff ConfigFiles.getSubscribers 369rosterGetSubscribers = rosterGetStuff ConfigFiles.getSubscribers
370 370
371data Conn = Conn { connChan :: TChan Stanza 371data Conn = Conn { connChan :: TChan Stanza
372 , auxAddr :: SockAddr } 372 , auxData :: ConnectionData }
373 373
374-- Read config file as Text content rather than UTF8 bytestrings. 374-- Read config file as Text content rather than UTF8 bytestrings.
375configText :: Functor f => 375configText :: Functor f =>
@@ -448,13 +448,13 @@ sendProbesAndSolicitations state k laddr chan = do
448 atomically $ writeTChan chan stanza 448 atomically $ writeTChan chan stanza
449 -- reverse xs `seq` return () 449 -- reverse xs `seq` return ()
450 450
451newConn :: PresenceState -> ConnectionKey -> SockAddr -> TChan Stanza -> IO () 451newConn :: PresenceState -> ConnectionKey -> ConnectionData -> TChan Stanza -> IO ()
452newConn state k addr outchan = do 452newConn state k cdta outchan = do
453 atomically $ modifyTVar' (keyToChan state) 453 atomically $ modifyTVar' (keyToChan state)
454 $ Map.insert k Conn { connChan = outchan 454 $ Map.insert k Conn { connChan = outchan
455 , auxAddr = addr } 455 , auxData = cdta }
456 when (isPeerKey k) 456 when (isPeerKey k)
457 $ sendProbesAndSolicitations state k addr outchan 457 $ sendProbesAndSolicitations state k (cdAddr cdta) outchan
458 458
459delclient :: (Alternative m, Monad m) => 459delclient :: (Alternative m, Monad m) =>
460 ConnectionKey -> m LocalPresence -> m LocalPresence 460 ConnectionKey -> m LocalPresence -> m LocalPresence
@@ -582,8 +582,8 @@ deliverMessage state fail msg =
582 fromMaybe fail {- reverse lookup failure -} $ mto <&> \(to',addr) -> do 582 fromMaybe fail {- reverse lookup failure -} $ mto <&> \(to',addr) -> do
583 let k = PeerKey addr 583 let k = PeerKey addr
584 chans <- atomically $ readTVar (keyToChan state) 584 chans <- atomically $ readTVar (keyToChan state)
585 fromMaybe fail $ (Map.lookup k chans) <&> \(Conn { connChan=chan 585 fromMaybe fail $ (Map.lookup k chans) <&> \(Conn { connChan = chan
586 , auxAddr=laddr }) -> do 586 , auxData = ConnectionData laddr ctyp }) -> do
587 (n,r) <- forClient state senderk (return (Nothing,Nothing)) 587 (n,r) <- forClient state senderk (return (Nothing,Nothing))
588 $ \c -> return (Just (clientUser c), Just (clientResource c)) 588 $ \c -> return (Just (clientUser c), Just (clientResource c))
589 -- original 'from' address is discarded. 589 -- original 'from' address is discarded.
@@ -594,8 +594,8 @@ deliverMessage state fail msg =
594 NetworkOrigin senderk@(PeerKey {}) _ -> do 594 NetworkOrigin senderk@(PeerKey {}) _ -> do
595 key_to_chan <- atomically $ readTVar (keyToChan state) 595 key_to_chan <- atomically $ readTVar (keyToChan state)
596 fromMaybe fail $ (Map.lookup senderk key_to_chan) 596 fromMaybe fail $ (Map.lookup senderk key_to_chan)
597 <&> \(Conn { connChan=sender_chan 597 <&> \(Conn { connChan = sender_chan
598 , auxAddr=laddr }) -> do 598 , auxData = ConnectionData laddr ctyp }) -> do
599 fromMaybe fail $ (stanzaTo msg) <&> \to -> do 599 fromMaybe fail $ (stanzaTo msg) <&> \to -> do
600 (mine,(n,h,r)) <- rewriteJIDForClient laddr to [] 600 (mine,(n,h,r)) <- rewriteJIDForClient laddr to []
601 if not mine then fail else do 601 if not mine then fail else do
@@ -669,7 +669,7 @@ subscribedPeers user profile = do
669-- | this JID is suitable for peers, not clients. 669-- | this JID is suitable for peers, not clients.
670clientJID :: Conn -> ClientState -> Text 670clientJID :: Conn -> ClientState -> Text
671clientJID con client = unsplitJID ( Just $ clientUser client 671clientJID con client = unsplitJID ( Just $ clientUser client
672 , addrToText $ auxAddr con 672 , addrToText $ cdAddr $ auxData con
673 , Just $ clientResource client) 673 , Just $ clientResource client)
674 674
675-- | Send presence notification to subscribed peers. 675-- | Send presence notification to subscribed peers.
@@ -790,9 +790,9 @@ answerProbe state mto k chan = do
790 conn <- liftT $ Map.lookup k ktc 790 conn <- liftT $ Map.lookup k ktc
791 let (mu,h,_) = splitJID to -- TODO: currently resource-id is ignored on presence 791 let (mu,h,_) = splitJID to -- TODO: currently resource-id is ignored on presence
792 -- probes. Is this correct? Check the spec. 792 -- probes. Is this correct? Check the spec.
793 liftMT $ guardPortStrippedAddress h (auxAddr conn) 793 liftMT $ guardPortStrippedAddress h (cdAddr $ auxData conn)
794 u <- liftT mu 794 u <- liftT mu
795 let ch = addrToText (auxAddr conn) 795 let ch = addrToText (cdAddr $ auxData conn)
796 profile = fromMaybe "." $ clientProfile <$> Map.lookup k cmap 796 profile = fromMaybe "." $ clientProfile <$> Map.lookup k cmap
797 return (u,profile,conn,ch) 797 return (u,profile,conn,ch)
798 798
@@ -1004,7 +1004,7 @@ clientSubscriptionRequest state fail k stanza chan = do
1004 -- if already connected, send solicitation ... 1004 -- if already connected, send solicitation ...
1005 -- let from = clientJID con client 1005 -- let from = clientJID con client
1006 let from = unsplitJID ( Just $ clientUser client 1006 let from = unsplitJID ( Just $ clientUser client
1007 , addrToText $ auxAddr con 1007 , addrToText $ cdAddr $ auxData con
1008 , Nothing ) 1008 , Nothing )
1009 mb <- rewriteJIDForPeer to 1009 mb <- rewriteJIDForPeer to
1010 forM_ mb $ \(to',addr) -> do 1010 forM_ mb $ \(to',addr) -> do
@@ -1061,7 +1061,7 @@ peerSubscriptionRequest state fail k stanza chan = do
1061 (ktc,cmap) <- atomically $ (,) <$> readTVar (keyToChan state) 1061 (ktc,cmap) <- atomically $ (,) <$> readTVar (keyToChan state)
1062 <*> readTVar (clients state) 1062 <*> readTVar (clients state)
1063 fromMaybe fail $ (Map.lookup k ktc) 1063 fromMaybe fail $ (Map.lookup k ktc)
1064 <&> \Conn { auxAddr=laddr } -> do 1064 <&> \Conn { auxData=ConnectionData laddr ctyp } -> do
1065 (mine,totup) <- rewriteJIDForClient laddr to [] 1065 (mine,totup) <- rewriteJIDForClient laddr to []
1066 if not mine then fail else do 1066 if not mine then fail else do
1067 (_,fromtup) <- rewriteJIDForClient laddr from [] 1067 (_,fromtup) <- rewriteJIDForClient laddr from []
@@ -1205,7 +1205,7 @@ peerInformSubscription state fail k stanza = do
1205 <*> readTVar (clients state) 1205 <*> readTVar (clients state)
1206 fromMaybe fail $ (Map.lookup k ktc) 1206 fromMaybe fail $ (Map.lookup k ktc)
1207 <&> \(Conn { connChan=sender_chan 1207 <&> \(Conn { connChan=sender_chan
1208 , auxAddr=laddr }) -> do 1208 , auxData =ConnectionData laddr ctyp }) -> do
1209 (_,(from_u,from_h,_)) <- rewriteJIDForClient laddr from [] 1209 (_,(from_u,from_h,_)) <- rewriteJIDForClient laddr from []
1210 let from'' = unsplitJID (from_u,from_h,Nothing) 1210 let from'' = unsplitJID (from_u,from_h,Nothing)
1211 muser = do 1211 muser = do
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs
index 19e721b0..cf7aca70 100644
--- a/Presence/XMPPServer.hs
+++ b/Presence/XMPPServer.hs
@@ -8,6 +8,8 @@ module XMPPServer
8 ( xmppServer 8 ( xmppServer
9 , quitXmpp 9 , quitXmpp
10 , ConnectionKey(..) 10 , ConnectionKey(..)
11 , ConnectionData(..)
12 , ConnectionType(..)
11 , XMPPServerParameters(..) 13 , XMPPServerParameters(..)
12 , XMPPServer 14 , XMPPServer
13 , xmppConnections 15 , xmppConnections
@@ -202,7 +204,7 @@ data XMPPServerParameters =
202 , xmppTellMyNameToPeer :: SockAddr -> IO Text 204 , xmppTellMyNameToPeer :: SockAddr -> IO Text
203 , xmppTellClientHisName :: ConnectionKey -> IO Text 205 , xmppTellClientHisName :: ConnectionKey -> IO Text
204 , xmppTellPeerHisName :: ConnectionKey -> IO Text 206 , xmppTellPeerHisName :: ConnectionKey -> IO Text
205 , xmppNewConnection :: ConnectionKey -> SockAddr -> TChan Stanza -> IO () 207 , xmppNewConnection :: ConnectionKey -> ConnectionData -> TChan Stanza -> IO ()
206 , xmppEOF :: ConnectionKey -> IO () 208 , xmppEOF :: ConnectionKey -> IO ()
207 , xmppRosterBuddies :: ConnectionKey -> IO [Text] 209 , xmppRosterBuddies :: ConnectionKey -> IO [Text]
208 , xmppRosterSubscribers :: ConnectionKey -> IO [Text] 210 , xmppRosterSubscribers :: ConnectionKey -> IO [Text]
@@ -902,7 +904,7 @@ makePong namespace mid to from =
902 ] 904 ]
903 905
904 906
905xmppInbound :: Server ConnectionKey SockAddr releaseKey XML.Event 907xmppInbound :: Server ConnectionKey ConnectionData releaseKey XML.Event
906 -> XMPPServerParameters 908 -> XMPPServerParameters
907 -> ConnectionKey 909 -> ConnectionKey
908 -> SockAddr 910 -> SockAddr
@@ -1250,16 +1252,16 @@ slotsToSource slots nesting lastStanza needsFlush rdone =
1250 ,readTMVar rdone >> return (return ()) 1252 ,readTMVar rdone >> return (return ())
1251 ] 1253 ]
1252 1254
1253forkConnection :: Server ConnectionKey SockAddr releaseKey XML.Event 1255forkConnection :: Server ConnectionKey ConnectionData releaseKey XML.Event
1254 -> XMPPServerParameters 1256 -> XMPPServerParameters
1255 -> ConnectionKey 1257 -> ConnectionKey
1256 -> SockAddr 1258 -> ConnectionData
1257 -> FlagCommand 1259 -> FlagCommand
1258 -> Source IO XML.Event 1260 -> Source IO XML.Event
1259 -> Sink (Flush XML.Event) IO () 1261 -> Sink (Flush XML.Event) IO ()
1260 -> TChan Stanza 1262 -> TChan Stanza
1261 -> IO (TChan Stanza) 1263 -> IO (TChan Stanza)
1262forkConnection sv xmpp k laddr pingflag src snk stanzas = do 1264forkConnection sv xmpp k (ConnectionData laddr _) pingflag src snk stanzas = do
1263 let (namespace,tellmyname) = case k of 1265 let (namespace,tellmyname) = case k of
1264 ClientKey {} -> ("jabber:client", xmppTellMyNameToClient xmpp k) 1266 ClientKey {} -> ("jabber:client", xmppTellMyNameToClient xmpp k)
1265 PeerKey {} -> ("jabber:server",xmppTellMyNameToPeer xmpp laddr) 1267 PeerKey {} -> ("jabber:server",xmppTellMyNameToPeer xmpp laddr)
@@ -1404,7 +1406,7 @@ data PeerState
1404 | PeerConnected (TChan Stanza) 1406 | PeerConnected (TChan Stanza)
1405-} 1407-}
1406 1408
1407peerKey :: SocketLike sock => Maybe SockAddr -> sock -> IO (ConnectionKey,SockAddr) 1409peerKey :: SocketLike sock => Maybe SockAddr -> sock -> IO (ConnectionKey,ConnectionData)
1408peerKey outgoingPeerPort sock = do 1410peerKey outgoingPeerPort sock = do
1409 addr <- getSocketName sock 1411 addr <- getSocketName sock
1410 peer <- 1412 peer <-
@@ -1413,13 +1415,13 @@ peerKey outgoingPeerPort sock = do
1413 else return addr -- Weird hack: addr is would-be peer name 1415 else return addr -- Weird hack: addr is would-be peer name
1414 laddr <- getSocketName sock 1416 laddr <- getSocketName sock
1415 let peerport = fromMaybe 5269 $ outgoingPeerPort >>= sockAddrPort 1417 let peerport = fromMaybe 5269 $ outgoingPeerPort >>= sockAddrPort
1416 return $ (PeerKey (peer `withPort` fromIntegral peerport),laddr) 1418 return $ (PeerKey (peer `withPort` fromIntegral peerport),ConnectionData laddr XMPP)
1417 1419
1418clientKey :: SocketLike sock => sock -> IO (ConnectionKey,SockAddr) 1420clientKey :: SocketLike sock => sock -> IO (ConnectionKey,ConnectionData)
1419clientKey sock = do 1421clientKey sock = do
1420 addr <- getSocketName sock 1422 addr <- getSocketName sock
1421 paddr <- getPeerName sock 1423 paddr <- getPeerName sock
1422 return $ (ClientKey addr,paddr) 1424 return $ (ClientKey addr,ConnectionData paddr XMPP)
1423 1425
1424xmlifyRosterItems :: Monad m => Set Text -> Text -> Set Text -> ConduitM i Event m () 1426xmlifyRosterItems :: Monad m => Set Text -> Text -> Set Text -> ConduitM i Event m ()
1425xmlifyRosterItems solicited stype set = mapM_ item (Set.toList set) 1427xmlifyRosterItems solicited stype set = mapM_ item (Set.toList set)
@@ -1488,7 +1490,7 @@ sendRoster query xmpp clientKey replyto = do
1488 -} 1490 -}
1489 1491
1490 1492
1491socketFromKey :: Server ConnectionKey SockAddr releaseKey XML.Event -> ConnectionKey -> IO SockAddr 1493socketFromKey :: Server ConnectionKey ConnectionData releaseKey XML.Event -> ConnectionKey -> IO SockAddr
1492socketFromKey sv k = do 1494socketFromKey sv k = do
1493 map <- atomically $ readTVar (conmap sv) 1495 map <- atomically $ readTVar (conmap sv)
1494 let mcd = Map.lookup k map 1496 let mcd = Map.lookup k map
@@ -1498,7 +1500,7 @@ socketFromKey sv k = do
1498 PeerKey addr -> return addr 1500 PeerKey addr -> return addr
1499 -- XXX: ? wrong address 1501 -- XXX: ? wrong address
1500 -- Shouldnt happen anyway. 1502 -- Shouldnt happen anyway.
1501 Just cd -> return $ cdata cd 1503 Just cd -> return $ cdAddr $ cdata cd
1502 1504
1503class StanzaFirstTag a where 1505class StanzaFirstTag a where
1504 stanzaFirstTag :: StanzaWrap a -> IO XML.Event 1506 stanzaFirstTag :: StanzaWrap a -> IO XML.Event
@@ -1658,8 +1660,8 @@ makeErrorStanza stanza = do
1658 ] 1660 ]
1659 1661
1660monitor :: 1662monitor ::
1661 Server ConnectionKey SockAddr releaseKey XML.Event 1663 Server ConnectionKey ConnectionData releaseKey XML.Event
1662 -> ConnectionParameters ConnectionKey SockAddr 1664 -> ConnectionParameters ConnectionKey ConnectionData
1663 -> XMPPServerParameters 1665 -> XMPPServerParameters
1664 -> IO b 1666 -> IO b
1665monitor sv params xmpp = do 1667monitor sv params xmpp = do
@@ -1823,13 +1825,22 @@ monitor sv params xmpp = do
1823 where 1825 where
1824 _ = str :: String 1826 _ = str :: String
1825 1827
1828data ConnectionType = XMPP | Tox
1829 deriving (Eq,Ord,Enum,Show,Read)
1830
1831data ConnectionData = ConnectionData
1832 { cdAddr :: SockAddr
1833 , cdType :: ConnectionType
1834 }
1835 deriving (Eq,Ord,Show)
1836
1826data XMPPServer 1837data XMPPServer
1827 = forall releaseKey. 1838 = forall releaseKey.
1828 XMPPServer { _xmpp_sv :: Server ConnectionKey SockAddr releaseKey XML.Event 1839 XMPPServer { _xmpp_sv :: Server ConnectionKey ConnectionData releaseKey XML.Event
1829 , _xmpp_peer_params :: ConnectionParameters ConnectionKey SockAddr 1840 , _xmpp_peer_params :: ConnectionParameters ConnectionKey ConnectionData
1830 } 1841 }
1831 1842
1832grokPeer :: XMPPServer -> ConnectionKey -> (SockAddr, ConnectionParameters ConnectionKey SockAddr, Miliseconds) 1843grokPeer :: XMPPServer -> ConnectionKey -> (SockAddr, ConnectionParameters ConnectionKey ConnectionData, Miliseconds)
1833grokPeer sv (PeerKey addr) = (addr, _xmpp_peer_params sv, 10000) 1844grokPeer sv (PeerKey addr) = (addr, _xmpp_peer_params sv, 10000)
1834 1845
1835xmppConnections :: XMPPServer -> IO (Connection.Manager TCPStatus Text) 1846xmppConnections :: XMPPServer -> IO (Connection.Manager TCPStatus Text)
@@ -1838,7 +1849,7 @@ xmppConnections xsv@XMPPServer{_xmpp_sv=sv} = tcpManager (grokPeer xsv) (Just .
1838 resolvPeer :: Text -> IO (Maybe ConnectionKey) 1849 resolvPeer :: Text -> IO (Maybe ConnectionKey)
1839 resolvPeer str = fmap PeerKey <$> listToMaybe <$> resolvePeer str 1850 resolvPeer str = fmap PeerKey <$> listToMaybe <$> resolvePeer str
1840 1851
1841xmppEventChannel :: XMPPServer -> TChan ((ConnectionKey, SockAddr), ConnectionEvent Event) 1852xmppEventChannel :: XMPPServer -> TChan ((ConnectionKey, ConnectionData), ConnectionEvent Event)
1842xmppEventChannel XMPPServer{_xmpp_sv=sv} = serverEvent sv 1853xmppEventChannel XMPPServer{_xmpp_sv=sv} = serverEvent sv
1843 1854
1844quitXmpp :: XMPPServer -> IO () 1855quitXmpp :: XMPPServer -> IO ()