diff options
author | joe <joe@jerkface.net> | 2013-06-26 02:54:08 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2013-06-26 02:54:08 -0400 |
commit | b0f6ac7137db6324d378a27ce46d8186b60fe9ca (patch) | |
tree | 53dc8528d304f32b721f138265431aa7372462a2 /Presence | |
parent | 46010b91c762fcba786e3a8c68e4445dc16b152f (diff) |
Send remote presence to clients.
Implmented -4 switch to select ipv4, otherwise ipv6
Diffstat (limited to 'Presence')
-rw-r--r-- | Presence/AdaptServer.hs | 2 | ||||
-rw-r--r-- | Presence/Server.hs | 29 | ||||
-rw-r--r-- | Presence/XMPPServer.hs | 30 | ||||
-rw-r--r-- | Presence/main.hs | 21 |
4 files changed, 36 insertions, 46 deletions
diff --git a/Presence/AdaptServer.hs b/Presence/AdaptServer.hs index eca4cb1e..dd453ba9 100644 --- a/Presence/AdaptServer.hs +++ b/Presence/AdaptServer.hs | |||
@@ -15,7 +15,7 @@ adaptStartCon start sock st = do | |||
15 | return (HCons rsRef st') | 15 | return (HCons rsRef st') |
16 | 16 | ||
17 | adaptDoCon showlex (dropTill,lex,parse) g st bs cont = do | 17 | adaptDoCon showlex (dropTill,lex,parse) g st bs cont = do |
18 | -- putStrLn $ "packet: " ++ show bs | 18 | putStrLn $ "packet: " ++ show bs |
19 | let (HCons rsRef st') = st | 19 | let (HCons rsRef st') = st |
20 | rs <- readIORef rsRef | 20 | rs <- readIORef rsRef |
21 | 21 | ||
diff --git a/Presence/Server.hs b/Presence/Server.hs index b611fae8..55a8b57b 100644 --- a/Presence/Server.hs +++ b/Presence/Server.hs | |||
@@ -35,33 +35,8 @@ newtype ConnectionFinalizer = ConnectionFinalizer (IO ()) | |||
35 | 35 | ||
36 | getPacket h = do { hWaitForInput h (-1) ; fmap (fromChunks . (:[])) $ hGetNonBlocking h 1024 } | 36 | getPacket h = do { hWaitForInput h (-1) ; fmap (fromChunks . (:[])) $ hGetNonBlocking h 1024 } |
37 | 37 | ||
38 | {- | 38 | doServer addrfamily port g startCon = do |
39 | doServer :: | 39 | doServer' addrfamily port g startCon |
40 | HList st | ||
41 | => PortNumber :*: st | ||
42 | -> ( Handle :*: ConnId :*: PortNumber :*: st | ||
43 | -> S.ByteString | ||
44 | -> (() -> IO ()) | ||
45 | -> IO () ) | ||
46 | -> IO b | ||
47 | -} | ||
48 | {- | ||
49 | doServer | ||
50 | :: (HOccursFst ConnectionFinalizer l, HList t) => | ||
51 | HCons PortNumber t | ||
52 | -> (l | ||
53 | -> ByteString | ||
54 | -> (() -> IO ()) | ||
55 | -> IO ()) | ||
56 | -> (Socket | ||
57 | -> HCons | ||
58 | Handle (HCons ConnId (HCons PortNumber t)) | ||
59 | -> IO l) | ||
60 | -> IO Socket | ||
61 | -} | ||
62 | doServer port g startCon = do | ||
63 | doServer' AF_INET6 port g startCon | ||
64 | -- doServer' AF_INET port g startCon | ||
65 | 40 | ||
66 | doServer' family port g startCon = runServer2 port (runConn2 g) | 41 | doServer' family port g startCon = runServer2 port (runConn2 g) |
67 | where | 42 | where |
diff --git a/Presence/XMPPServer.hs b/Presence/XMPPServer.hs index 58dcf430..be59ac02 100644 --- a/Presence/XMPPServer.hs +++ b/Presence/XMPPServer.hs | |||
@@ -229,7 +229,7 @@ startCon session_factory sock st = do | |||
229 | pchan <- subscribe session Nothing | 229 | pchan <- subscribe session Nothing |
230 | cmdChan <- atomically newTChan | 230 | cmdChan <- atomically newTChan |
231 | reader <- forkIO $ | 231 | reader <- forkIO $ |
232 | handle (\(SomeException _) -> L.putStrLn "quit reader via exception.") $ | 232 | handle (\(SomeException e) -> L.putStrLn $ "quit reader via exception: "<++>bshow e) $ |
233 | fix $ \loop -> do | 233 | fix $ \loop -> do |
234 | event <- atomically $ | 234 | event <- atomically $ |
235 | (fmap Left $ readTChan pchan) | 235 | (fmap Left $ readTChan pchan) |
@@ -464,14 +464,15 @@ showTokenT (TokFreeText s) = "TokFreeText "++s | |||
464 | showtoks ts = Prelude.show $ map (showTokenT . snd) ts | 464 | showtoks ts = Prelude.show $ map (showTokenT . snd) ts |
465 | 465 | ||
466 | 466 | ||
467 | listenForXmppClients session_factory port st = do | 467 | listenForXmppClients addr_family session_factory port st = do |
468 | -- standard port: 5222 | 468 | -- standard port: 5222 |
469 | let (start,dopkt) = | 469 | let (start,dopkt) = |
470 | adaptServer showtoks ( dropTill | 470 | adaptServer showtoks ( dropTill |
471 | , xmlLexPartial "local-client" . unpack | 471 | , xmlLexPartial "local-client" . unpack |
472 | , xmppParse) | 472 | , xmppParse) |
473 | (startCon session_factory,doCon) | 473 | (startCon session_factory,doCon) |
474 | doServer (port .*. st) | 474 | doServer addr_family |
475 | (port .*. st) | ||
475 | dopkt | 476 | dopkt |
476 | start | 477 | start |
477 | 478 | ||
@@ -503,10 +504,19 @@ doPeer st elem cont = do | |||
503 | (Just jid,Nothing) -> do | 504 | (Just jid,Nothing) -> do |
504 | let string (CString _ s _) = [s] | 505 | let string (CString _ s _) = [s] |
505 | stat = listToMaybe . Prelude.concatMap string . tagcontent "show" $ content | 506 | stat = listToMaybe . Prelude.concatMap string . tagcontent "show" $ content |
507 | stat' = case stat of | ||
508 | Nothing -> Available | ||
509 | Just "away" -> Away | ||
510 | Just "xa" -> Away -- TODO: xa | ||
511 | Just "dnd" -> Away -- TODO: dnd | ||
512 | Just "chat" -> Available | ||
513 | _ -> Available | ||
506 | -- Available or Away. | 514 | -- Available or Away. |
507 | names <- parseAddressJID jid >>= getNamesForPeer . peer | 515 | pjid <- parseAddressJID jid |
508 | L.putStrLn $ "IN peer: PRESENCE! "<++>bshow (stat,names)<++>" avail/away jid=" <++> jid | 516 | names <- getNamesForPeer (peer pjid) |
509 | -- todo: announcePresence | 517 | -- L.putStrLn $ "IN peer: PRESENCE! "<++>bshow (stat,names)<++>" avail/away jid=" <++> jid |
518 | announcePresence session (Presence pjid stat') | ||
519 | L.putStrLn $ "IN peer: " <++> bshow (Presence pjid stat') | ||
510 | _ -> return () -- putStrLn $ "inbound unhandled: "++show v | 520 | _ -> return () -- putStrLn $ "inbound unhandled: "++show v |
511 | cont () | 521 | cont () |
512 | 522 | ||
@@ -520,14 +530,15 @@ xmlLexPartial name cs = | |||
520 | else gs | 530 | else gs |
521 | 531 | ||
522 | 532 | ||
523 | listenForRemotePeers session_factory port st = do | 533 | listenForRemotePeers addrfamily session_factory port st = do |
524 | -- standard port: 5269 | 534 | -- standard port: 5269 |
525 | let (start,dopkt) = | 535 | let (start,dopkt) = |
526 | adaptServer showtoks ( dropTill | 536 | adaptServer showtoks ( dropTill |
527 | , xmlLexPartial "remote-inbound" . unpack | 537 | , xmlLexPartial "remote-inbound" . unpack |
528 | , xmppParse) | 538 | , xmppParse) |
529 | (startPeer session_factory,doPeer) | 539 | (startPeer session_factory,doPeer) |
530 | doServer (port .*. st) | 540 | doServer addrfamily |
541 | (port .*. st) | ||
531 | dopkt | 542 | dopkt |
532 | start | 543 | start |
533 | 544 | ||
@@ -736,7 +747,7 @@ seekRemotePeers config chan = do | |||
736 | fix $ \loop -> do | 747 | fix $ \loop -> do |
737 | event <- atomically $ readTChan chan | 748 | event <- atomically $ readTChan chan |
738 | case event of | 749 | case event of |
739 | p@(Presence jid stat) -> do | 750 | p@(Presence jid stat) | not (is_remote (peer jid)) -> do |
740 | -- L.putStrLn $ "seekRemotePeers: " <++> L.show jid <++> " " <++> bshow stat | 751 | -- L.putStrLn $ "seekRemotePeers: " <++> L.show jid <++> " " <++> bshow stat |
741 | runMaybeT $ do | 752 | runMaybeT $ do |
742 | u <- MaybeT . return $ name jid | 753 | u <- MaybeT . return $ name jid |
@@ -748,4 +759,5 @@ seekRemotePeers config chan = do | |||
748 | forM_ (Set.toList peers) $ \peer -> do | 759 | forM_ (Set.toList peers) $ \peer -> do |
749 | when (is_remote peer) $ | 760 | when (is_remote peer) $ |
750 | liftIO $ sendMessage server_connections (OutBoundPresence p) peer | 761 | liftIO $ sendMessage server_connections (OutBoundPresence p) peer |
762 | _ -> return (Just ()) | ||
751 | loop | 763 | loop |
diff --git a/Presence/main.hs b/Presence/main.hs index 24d240cd..5897d79e 100644 --- a/Presence/main.hs +++ b/Presence/main.hs | |||
@@ -49,6 +49,7 @@ import qualified Prelude | |||
49 | import Prelude hiding (putStrLn) | 49 | import Prelude hiding (putStrLn) |
50 | import System.Environment | 50 | import System.Environment |
51 | import qualified Text.Show.ByteString as L | 51 | import qualified Text.Show.ByteString as L |
52 | import Network.Socket (Family(AF_INET,AF_INET6)) | ||
52 | 53 | ||
53 | 54 | ||
54 | data UnixSession = UnixSession { | 55 | data UnixSession = UnixSession { |
@@ -93,8 +94,11 @@ instance XMPPSession UnixSession where | |||
93 | let tvar = subscriberMap (presence_state session) | 94 | let tvar = subscriberMap (presence_state session) |
94 | atomically $ subscribeToMap tvar jid | 95 | atomically $ subscribeToMap tvar jid |
95 | announcePresence session (Presence jid status) = do | 96 | announcePresence session (Presence jid status) = do |
96 | subs <- readTVarIO $ subscriberMap (presence_state session) | 97 | (greedy,subs) <- atomically $ do |
97 | update_presence Nothing (fmap snd subs) (Set.singleton jid) (const status) | 98 | subs <- readTVar $ subscriberMap (presence_state session) |
99 | greedy <- fmap snd $ readTMVar $ localSubscriber (presence_state session) | ||
100 | return (greedy,subs) | ||
101 | update_presence (Just greedy) (fmap snd subs) (Set.singleton jid) (const status) | ||
98 | 102 | ||
99 | 103 | ||
100 | subscribeToChan tmvar = | 104 | subscribeToChan tmvar = |
@@ -198,8 +202,8 @@ instance XMPPConfig UnixConfig where | |||
198 | getBuddies _ user = ConfigFiles.getBuddies user | 202 | getBuddies _ user = ConfigFiles.getBuddies user |
199 | getSubscribers _ user = ConfigFiles.getSubscribers user | 203 | getSubscribers _ user = ConfigFiles.getSubscribers user |
200 | 204 | ||
201 | start :: ByteString -> IO () | 205 | start :: Network.Socket.Family -> IO () |
202 | start host = do | 206 | start ip4or6 = do |
203 | let host = LocalHost | 207 | let host = LocalHost |
204 | tracked <- newPresenceState host | 208 | tracked <- newPresenceState host |
205 | let dologin e = track_login host tracked e | 209 | let dologin e = track_login host tracked e |
@@ -219,8 +223,8 @@ start host = do | |||
219 | utmp_file | 223 | utmp_file |
220 | dologin | 224 | dologin |
221 | #endif | 225 | #endif |
222 | sockLocals <- listenForXmppClients (UnixSessions tracked) 5222 HNil | 226 | sockLocals <- listenForXmppClients ip4or6 (UnixSessions tracked) 5222 HNil |
223 | sockRemotes <- listenForRemotePeers (UnixSessions tracked) 5269 HNil | 227 | sockRemotes <- listenForRemotePeers ip4or6 (UnixSessions tracked) 5269 HNil |
224 | 228 | ||
225 | threadDelay 1000 -- wait a moment to obtain current tty | 229 | threadDelay 1000 -- wait a moment to obtain current tty |
226 | dologin () | 230 | dologin () |
@@ -278,8 +282,7 @@ getOptions (x0:xs) = getOptions xs | |||
278 | 282 | ||
279 | main = do | 283 | main = do |
280 | opts <- fmap getOptions getArgs | 284 | opts <- fmap getOptions getArgs |
281 | let hostname = maybe "localhost" id (Map.lookup "n" opts) | 285 | let use_ip4 = if isJust (Map.lookup "4" opts) then AF_INET else AF_INET6 |
282 | L.putStrLn $ "hostname = " <++> hostname | 286 | runOnce ["/var/run/presence.pid","/tmp/presence.pid"] (start use_ip4) sendUSR1 |
283 | runOnce ["/var/run/presence.pid","/tmp/presence.pid"] (start hostname) sendUSR1 | ||
284 | 287 | ||
285 | 288 | ||