summaryrefslogtreecommitdiff
path: root/xmppServer.hs
diff options
context:
space:
mode:
Diffstat (limited to 'xmppServer.hs')
-rw-r--r--xmppServer.hs48
1 files changed, 37 insertions, 11 deletions
diff --git a/xmppServer.hs b/xmppServer.hs
index a6cf2a2b..aa99c061 100644
--- a/xmppServer.hs
+++ b/xmppServer.hs
@@ -25,6 +25,8 @@ import qualified Text.XML.Stream.Parse as XML
25import Data.XML.Types as XML 25import Data.XML.Types as XML
26import Data.Maybe (catMaybes) 26import Data.Maybe (catMaybes)
27import Data.Monoid ( (<>) ) 27import Data.Monoid ( (<>) )
28import Data.Text (Text)
29import qualified Data.Text as Text (pack)
28 30
29import qualified Control.Concurrent.STM.UpdateStream as Slotted 31import qualified Control.Concurrent.STM.UpdateStream as Slotted
30import ControlMaybe 32import ControlMaybe
@@ -32,6 +34,13 @@ import NestingXML
32import EventUtil 34import EventUtil
33import Server 35import Server
34 36
37addrToText :: SockAddr -> Text
38addrToText (addr@(SockAddrInet _ _)) = Text.pack $ stripColon (show addr)
39 where stripColon s = pre where (pre,port) = break (==':') s
40addrToText (addr@(SockAddrInet6 _ _ _ _)) = Text.pack $ stripColon (show addr)
41 where stripColon s = if null bracket then pre else pre ++ "]"
42 where
43 (pre,bracket) = break (==']') s
35 44
36wlog s = putStrLn s 45wlog s = putStrLn s
37 where _ = s :: String 46 where _ = s :: String
@@ -39,14 +48,6 @@ wlogb s = Strict8.putStrLn s
39 48
40control sv = atomically . putTMVar (serverCommand sv) 49control sv = atomically . putTMVar (serverCommand sv)
41 50
42discardFlush :: Monad m => ConduitM (Flush a) a m ()
43discardFlush = awaitForever $ \x -> do
44 let unchunk (Chunk a) = a
45 ischunk (Chunk _) = True
46 ischunk _ = False
47 when (ischunk x) $ yield (unchunk x)
48
49
50xmlStream :: ReadCommand -> WriteCommand -> ( Source IO XML.Event 51xmlStream :: ReadCommand -> WriteCommand -> ( Source IO XML.Event
51 , Sink (Flush XML.Event) IO () ) 52 , Sink (Flush XML.Event) IO () )
52xmlStream conread conwrite = (xsrc,xsnk) 53xmlStream conread conwrite = (xsrc,xsnk)
@@ -57,6 +58,13 @@ xmlStream conread conwrite = (xsrc,xsnk)
57 =$= builderToByteStringFlush 58 =$= builderToByteStringFlush
58 =$= discardFlush 59 =$= discardFlush
59 =$ snk 60 =$ snk
61 where
62 discardFlush :: Monad m => ConduitM (Flush a) a m ()
63 discardFlush = awaitForever $ \x -> do
64 let unchunk (Chunk a) = a
65 ischunk (Chunk _) = True
66 ischunk _ = False
67 when (ischunk x) $ yield (unchunk x)
60 68
61 src = do 69 src = do
62 v <- lift conread 70 v <- lift conread
@@ -145,6 +153,21 @@ data XMPPState
145 deriving (Eq,Ord) 153 deriving (Eq,Ord)
146 154
147 155
156peerPing :: Maybe Text -> Text -> Text -> [XML.Event]
157peerPing mid to from =
158 [ EventBeginElement "{jabber:server}iq"
159 $ (case mid of
160 Just c -> (("id",[ContentText c]):)
161 _ -> id )
162 [ ("type",[ContentText "get"])
163 , attr "to" to
164 , attr "from" from
165 ]
166 , EventBeginElement "{urn:xmpp:ping}ping" []
167 , EventEndElement "{urn:xmpp:ping}ping"
168 , EventEndElement "{jabber:server}iq" ]
169
170
148forkConnection :: ConnectionKey 171forkConnection :: ConnectionKey
149 -> FlagCommand 172 -> FlagCommand
150 -> Source IO XML.Event 173 -> Source IO XML.Event
@@ -201,9 +224,12 @@ forkConnection k pingflag src snk stanzas = do
201 what 224 what
202 ,do pingflag >>= check 225 ,do pingflag >>= check
203 return $ do 226 return $ do
204 atomically $ Slotted.push slots (Just PingSlot) $ EventBeginElement "ping" [] 227 let to = addrToText (callBackAddress k)
205 atomically $ Slotted.push slots (Just PingSlot) $ EventEndElement "ping" 228 from = "todo" -- Look it up from Server object
206 -- TODO: fix ping 229 -- or pass it with Connection event.
230 mid = Just "ping"
231 mapM_ (atomically . Slotted.push slots (Just $ PingSlot))
232 (peerPing mid to from)
207 loop 233 loop
208 ,readTMVar rdone >> return (return ()) 234 ,readTMVar rdone >> return (return ())
209 ] 235 ]