summaryrefslogtreecommitdiff
path: root/Presence/XMPPServer.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2013-06-24 13:48:20 -0400
committerjoe <joe@jerkface.net>2013-06-24 13:48:20 -0400
commit4ae6bf78a836cf35450387431aea93d522ce8f84 (patch)
tree433f37eebe2dc868329a1ed29353919ce75580ab /Presence/XMPPServer.hs
parent7bb61539e0db00f91a2c5bc3740492ef9319c17b (diff)
announcePresence interface
Diffstat (limited to 'Presence/XMPPServer.hs')
-rw-r--r--Presence/XMPPServer.hs32
1 files changed, 28 insertions, 4 deletions
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs
index 4f61646f..387b223e 100644
--- a/Presence/XMPPServer.hs
+++ b/Presence/XMPPServer.hs
@@ -59,6 +59,7 @@ import qualified Data.Map as Map
59import GHC.Conc 59import GHC.Conc
60import Network.BSD 60import Network.BSD
61import Control.Concurrent.Async 61import Control.Concurrent.Async
62import qualified Data.Set as Set
62 63
63-- | Jabber ID (JID) datatype 64-- | Jabber ID (JID) datatype
64data JID = JID { name :: Maybe ByteString 65data JID = JID { name :: Maybe ByteString
@@ -109,6 +110,7 @@ class XMPPSession session where
109 getJID :: session -> IO JID 110 getJID :: session -> IO JID
110 closeSession :: session -> IO () 111 closeSession :: session -> IO ()
111 subscribe :: session -> Maybe JID -> IO (TChan Presence) 112 subscribe :: session -> Maybe JID -> IO (TChan Presence)
113 announcePresence :: session -> Presence -> IO ()
112 114
113class XMPPConfig config where 115class XMPPConfig config where
114 getBuddies :: config -> ByteString -> IO [ByteString] 116 getBuddies :: config -> ByteString -> IO [ByteString]
@@ -354,10 +356,31 @@ startPeer session_factory sock st = do
354 name <- fmap bshow $ getPeerName sock 356 name <- fmap bshow $ getPeerName sock
355 L.putStrLn $ "REMOTE-IN: connected " <++> name 357 L.putStrLn $ "REMOTE-IN: connected " <++> name
356 let quit = L.putStrLn $ "REMOTE-IN: disconnected " <++> name 358 let quit = L.putStrLn $ "REMOTE-IN: disconnected " <++> name
357 return ( ConnectionFinalizer quit .*. st ) 359 session <- newSession session_factory sock h
360 return ( session .*. ConnectionFinalizer quit .*. st )
358 361
359doPeer st elem cont = do 362doPeer st elem cont = do
363 let session = hHead st
360 L.putStrLn $ "REMOTE-IN: received " <++> bshow elem 364 L.putStrLn $ "REMOTE-IN: received " <++> bshow elem
365 case elem of
366 Element e@(Elem (N "presence") attrs content) -> do
367 let jid = fmap pack (lookup (N "from") attrs >>= unattr)
368 typ = fmap pack (lookup (N "type") attrs >>= unattr)
369 case (jid,typ) of
370 (Just jid,Just "unavailable") -> do
371 L.putStrLn $ "INBOUND PRESENCE! Offline jid=" <++> jid
372 announcePresence session (Presence (parseJID jid) Offline)
373 (Just jid,Just typ) ->
374 -- possible probe, ignored for now
375 L.putStrLn $ "INBOUND PRESENCE! "<++>typ<++>" jid="<++>jid
376 (Just jid,Nothing) -> do
377 let string (CString _ s _) = [s]
378 show = listToMaybe . Prelude.concatMap string . tagcontent "show" $ content
379
380 -- Available or Away.
381 L.putStrLn $ "INBOUND PRESENCE! avail/away jid=" <++> jid
382 -- todo: announcePresence
383 _ -> return () -- putStrLn $ "inbound unhandled: "++show v
361 cont () 384 cont ()
362 385
363xmlLexPartial name cs = 386xmlLexPartial name cs =
@@ -448,9 +471,11 @@ connect_to_server chan peer = (>> return ()) . runMaybeT $ do
448 cached_map <- readIORef cached 471 cached_map <- readIORef cached
449 writeIORef cached (Map.insert jid st cached_map) 472 writeIORef cached (Map.insert jid st cached_map)
450 loop 473 loop
474 {-
451 Left event -> do 475 Left event -> do
452 L.putStrLn $ "REMOTE-OUT DISCARDED: " <++> bshow event 476 L.putStrLn $ "REMOTE-OUT DISCARDED: " <++> bshow event
453 loop 477 loop
478 -}
454 Right sock -> return sock 479 Right sock -> return sock
455 480
456 liftIO $ do 481 liftIO $ do
@@ -534,9 +559,8 @@ seekRemotePeers is_peer config chan = do
534 u <- MaybeT . return $ name jid 559 u <- MaybeT . return $ name jid
535 subscribers <- liftIO $ getSubscribers config u 560 subscribers <- liftIO $ getSubscribers config u
536 liftIO . L.putStrLn $ "subscribers: " <++> bshow subscribers 561 liftIO . L.putStrLn $ "subscribers: " <++> bshow subscribers
537 forM_ subscribers $ \bjid -> do 562 let peers = Set.map (server . parseJID) (Set.fromList subscribers)
538 let jid = parseJID bjid 563 forM_ (Set.toList peers) $ \peer -> do
539 peer = server jid
540 when (is_peer peer) $ 564 when (is_peer peer) $
541 liftIO $ sendMessage server_connections (OutBoundPresence p) peer 565 liftIO $ sendMessage server_connections (OutBoundPresence p) peer
542 loop 566 loop