summaryrefslogtreecommitdiff
path: root/Presence/XMPPServer.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Presence/XMPPServer.hs')
-rw-r--r--Presence/XMPPServer.hs48
1 files changed, 33 insertions, 15 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs
index 421648fb..45dc282e 100644
--- a/Presence/XMPPServer.hs
+++ b/Presence/XMPPServer.hs
@@ -10,6 +10,8 @@ module XMPPServer
10 , StanzaOrigin(..) 10 , StanzaOrigin(..)
11 , cloneStanza 11 , cloneStanza
12 , LangSpecificMessage(..) 12 , LangSpecificMessage(..)
13 , peerKeyToText
14 , addrToText
13 ) where 15 ) where
14import Debug.Trace 16import Debug.Trace
15import Control.Monad.Trans.Resource (runResourceT) 17import Control.Monad.Trans.Resource (runResourceT)
@@ -126,6 +128,8 @@ data XMPPServerParameters =
126 { xmppChooseResourceName :: ConnectionKey -> SockAddr -> Maybe Text -> IO Text 128 { xmppChooseResourceName :: ConnectionKey -> SockAddr -> Maybe Text -> IO Text
127 , xmppTellMyNameToClient :: IO Text 129 , xmppTellMyNameToClient :: IO Text
128 , xmppTellMyNameToPeer :: SockAddr -> IO Text 130 , xmppTellMyNameToPeer :: SockAddr -> IO Text
131 , xmppTellClientHisName :: ConnectionKey -> IO Text
132 , xmppTellPeerHisName :: ConnectionKey -> IO Text
129 , xmppNewConnection :: ConnectionKey -> TChan Stanza -> IO () 133 , xmppNewConnection :: ConnectionKey -> TChan Stanza -> IO ()
130 , xmppEOF :: ConnectionKey -> IO () 134 , xmppEOF :: ConnectionKey -> IO ()
131 , xmppRosterBuddies :: ConnectionKey -> IO [Text] 135 , xmppRosterBuddies :: ConnectionKey -> IO [Text]
@@ -153,6 +157,10 @@ addrToText (addr@(SockAddrInet6 _ _ _ _)) = Text.pack $ stripColon (show addr)
153 where 157 where
154 (pre,bracket) = break (==']') s 158 (pre,bracket) = break (==']') s
155 159
160peerKeyToText :: ConnectionKey -> Text
161peerKeyToText (PeerKey { callBackAddress=addr }) = addrToText addr
162peerKeyToText (ClientKey { localAddress=addr }) = "ErrorClIeNt"
163
156wlog s = putStrLn s 164wlog s = putStrLn s
157 where _ = s :: String 165 where _ = s :: String
158wlogb s = Strict8.putStrLn s 166wlogb s = Strict8.putStrLn s
@@ -402,17 +410,27 @@ grokStanza "jabber:client" stanzaTag =
402 _ -> return $ Just Unrecognized 410 _ -> return $ Just Unrecognized
403 411
404xmppInbound :: Server ConnectionKey SockAddr 412xmppInbound :: Server ConnectionKey SockAddr
413 -> XMPPServerParameters
405 -> ConnectionKey 414 -> ConnectionKey
415 -> SockAddr
406 -> FlagCommand 416 -> FlagCommand
407 -> Source IO XML.Event 417 -> Source IO XML.Event
408 -> TChan Stanza 418 -> TChan Stanza
409 -> TChan Stanza 419 -> TChan Stanza
410 -> TMVar () 420 -> TMVar ()
411 -> Sink XML.Event IO () 421 -> Sink XML.Event IO ()
412xmppInbound sv k pingflag src stanzas output donevar = doNestingXML $ do 422xmppInbound sv xmpp k laddr pingflag src stanzas output donevar = doNestingXML $ do
413 let namespace = case k of 423 let (namespace,tellmyname,tellyourname) = case k of
414 ClientKey {} -> "jabber:client" 424 ClientKey {} -> ( "jabber:client"
415 PeerKey {} -> "jabber:server" 425 , xmppTellMyNameToClient xmpp
426 , xmppTellClientHisName xmpp k
427 )
428 PeerKey {} -> ( "jabber:server"
429 , xmppTellMyNameToPeer xmpp laddr
430 , xmppTellPeerHisName xmpp k
431 )
432 me <- liftIO tellmyname
433 you <- liftIO tellyourname
416 withXML $ \begindoc -> do 434 withXML $ \begindoc -> do
417 when (begindoc==EventBeginDocument) $ do 435 when (begindoc==EventBeginDocument) $ do
418 whenJust nextElement $ \xml -> do 436 whenJust nextElement $ \xml -> do
@@ -447,13 +465,13 @@ xmppInbound sv k pingflag src stanzas output donevar = doNestingXML $ do
447 ioWriteChan stanzas s 465 ioWriteChan stanzas s
448 flip (maybe $ unrecog) dispatch $ \dispatch -> 466 flip (maybe $ unrecog) dispatch $ \dispatch ->
449 case dispatch of 467 case dispatch of
450 Ping -> do 468 -- Checking that the to-address matches this server.
451 -- TODO: check that the to-address matches this server. 469 -- Otherwise it could be a client-to-client ping or a
452 -- Otherwise it could be a client-to-client ping or a 470 -- client-to-server for some other server.
453 -- client-to-server for some other server. 471 -- For now, assuming its for the immediate connection.
454 -- For now, assuming its for the immediate connection. 472 Ping | mto==Just me || mto==Nothing -> do
455 let pongto = maybe "todo" id mfrom 473 let pongto = maybe you id mfrom
456 pongfrom = maybe "todo" id mto 474 pongfrom = maybe me id mto
457 pong = makePong namespace mid pongto pongfrom 475 pong = makePong namespace mid pongto pongfrom
458 sendReply donevar Pong pong output 476 sendReply donevar Pong pong output
459 -- TODO: Remove this, it is only to generate a debug print 477 -- TODO: Remove this, it is only to generate a debug print
@@ -679,9 +697,9 @@ forkConnection sv xmpp k laddr pingflag src snk stanzas = do
679 loop 697 loop
680 ,do pingflag >>= check 698 ,do pingflag >>= check
681 return $ do 699 return $ do
682 let to = addrToText (callBackAddress k) 700 to <- xmppTellPeerHisName xmpp k -- addrToText (callBackAddress k)
683 from = "todo" -- Look it up from Server object 701 let from = me -- Look it up from Server object
684 -- or pass it with Connection event. 702 -- or pass it with Connection event.
685 mid = Just "ping" 703 mid = Just "ping"
686 ping = makePing namespace mid to from 704 ping = makePing namespace mid to from
687 mapM_ (atomically . Slotted.push slots (Just $ PingSlot)) 705 mapM_ (atomically . Slotted.push slots (Just $ PingSlot))
@@ -697,7 +715,7 @@ forkConnection sv xmpp k laddr pingflag src snk stanzas = do
697 wlog $ "end pre-queue fork: " ++ show k 715 wlog $ "end pre-queue fork: " ++ show k
698 forkIO $ do 716 forkIO $ do
699 -- src $$ awaitForever (lift . putStrLn . takeWhile (/=' ') . show) 717 -- src $$ awaitForever (lift . putStrLn . takeWhile (/=' ') . show)
700 src $$ xmppInbound sv k pingflag src stanzas output rdone 718 src $$ xmppInbound sv xmpp k laddr pingflag src stanzas output rdone
701 atomically $ putTMVar rdone () 719 atomically $ putTMVar rdone ()
702 wlog $ "end reader fork: " ++ show k 720 wlog $ "end reader fork: " ++ show k
703 return output 721 return output