diff options
Diffstat (limited to 'xmppServer.hs')
-rw-r--r-- | xmppServer.hs | 48 |
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 | |||
25 | import Data.XML.Types as XML | 25 | import Data.XML.Types as XML |
26 | import Data.Maybe (catMaybes) | 26 | import Data.Maybe (catMaybes) |
27 | import Data.Monoid ( (<>) ) | 27 | import Data.Monoid ( (<>) ) |
28 | import Data.Text (Text) | ||
29 | import qualified Data.Text as Text (pack) | ||
28 | 30 | ||
29 | import qualified Control.Concurrent.STM.UpdateStream as Slotted | 31 | import qualified Control.Concurrent.STM.UpdateStream as Slotted |
30 | import ControlMaybe | 32 | import ControlMaybe |
@@ -32,6 +34,13 @@ import NestingXML | |||
32 | import EventUtil | 34 | import EventUtil |
33 | import Server | 35 | import Server |
34 | 36 | ||
37 | addrToText :: SockAddr -> Text | ||
38 | addrToText (addr@(SockAddrInet _ _)) = Text.pack $ stripColon (show addr) | ||
39 | where stripColon s = pre where (pre,port) = break (==':') s | ||
40 | addrToText (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 | ||
36 | wlog s = putStrLn s | 45 | wlog s = putStrLn s |
37 | where _ = s :: String | 46 | where _ = s :: String |
@@ -39,14 +48,6 @@ wlogb s = Strict8.putStrLn s | |||
39 | 48 | ||
40 | control sv = atomically . putTMVar (serverCommand sv) | 49 | control sv = atomically . putTMVar (serverCommand sv) |
41 | 50 | ||
42 | discardFlush :: Monad m => ConduitM (Flush a) a m () | ||
43 | discardFlush = 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 | |||
50 | xmlStream :: ReadCommand -> WriteCommand -> ( Source IO XML.Event | 51 | xmlStream :: ReadCommand -> WriteCommand -> ( Source IO XML.Event |
51 | , Sink (Flush XML.Event) IO () ) | 52 | , Sink (Flush XML.Event) IO () ) |
52 | xmlStream conread conwrite = (xsrc,xsnk) | 53 | xmlStream 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 | ||
156 | peerPing :: Maybe Text -> Text -> Text -> [XML.Event] | ||
157 | peerPing 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 | |||
148 | forkConnection :: ConnectionKey | 171 | forkConnection :: 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 | ] |