summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2013-06-17 22:16:31 -0400
committerjoe <joe@jerkface.net>2013-06-17 22:16:31 -0400
commit74c5a3f6cf2404c4907b108699cda00eda0ccfb0 (patch)
tree04e0957f009be3366004b01699f6bb4a013d2ab5
parent012a3b625c0ea236afc1ea09c89d0dcd435a66bd (diff)
delay a bit before first utmp scan so that we know active tty
-rw-r--r--Presence/main.hs29
1 files changed, 19 insertions, 10 deletions
diff --git a/Presence/main.hs b/Presence/main.hs
index ff2f18cf..7bf9d117 100644
--- a/Presence/main.hs
+++ b/Presence/main.hs
@@ -33,7 +33,9 @@ import System.Posix.User
33import qualified Data.Set as Set 33import qualified Data.Set as Set
34import Data.Set as Set (Set,(\\)) 34import Data.Set as Set (Set,(\\))
35import Control.Concurrent.MVar.Strict 35import Control.Concurrent.MVar.Strict
36import Control.Concurrent (threadDelay)
36import Control.DeepSeq 37import Control.DeepSeq
38import Debug.Trace
37 39
38import ByteStringOperators 40import ByteStringOperators
39import qualified Data.ByteString.Lazy.Char8 as L 41import qualified Data.ByteString.Lazy.Char8 as L
@@ -65,7 +67,7 @@ toJabberId host (user,tty,_) =
65 then Just (jid user host tty) 67 then Just (jid user host tty)
66 else Nothing 68 else Nothing
67 69
68track_login :: MVar (Set JID) -> t -> IO () 70track_login :: MVar (ByteString,Set JID) -> t -> IO ()
69track_login tracked e = do 71track_login tracked e = do
70#ifndef NOUTMP 72#ifndef NOUTMP
71 us <- users 73 us <- users
@@ -73,13 +75,17 @@ track_login tracked e = do
73 let us = [] 75 let us = []
74#endif 76#endif
75 let ids = Set.fromList $ mapMaybe (toJabberId "localhost") us 77 let ids = Set.fromList $ mapMaybe (toJabberId "localhost") us
76 state <- swapMVar tracked ids 78 (tty,state) <- modifyMVar tracked $ \(tty,st) ->
79 return ((tty,ids),(tty,st))
77 let arrivals = ids \\ state 80 let arrivals = ids \\ state
78 departures = state \\ ids 81 departures = state \\ ids
79 forM_ (Set.toList departures) $ \id -> do 82 forM_ (Set.toList departures) $ \id -> do
80 putStrLn $ bshow id <++> " Offline." 83 putStrLn $ bshow id <++> " Offline."
81 forM_ (Set.toList arrivals) $ \id -> do 84 forM_ (Set.toList arrivals) $ \jid -> do
82 putStrLn $ bshow id <++> " Available." 85 case fmap (==tty) $ resource jid of
86 Just True -> putStrLn $ bshow jid <++> " Available."
87 Just False -> putStrLn $ bshow jid <++> " Away."
88 Nothing -> trace "Unexpected lack of resource" $ return ()
83 89
84data UnixSession = UnixSession { 90data UnixSession = UnixSession {
85 unix_uid :: (IORef (Maybe UserID)), 91 unix_uid :: (IORef (Maybe UserID)),
@@ -118,8 +124,9 @@ instance XMPPSession UnixSession where
118 124
119on_chvt tracked vtnum = do 125on_chvt tracked vtnum = do
120 let tty = L.snoc "tty" $ intToDigit (fromIntegral vtnum) 126 let tty = L.snoc "tty" $ intToDigit (fromIntegral vtnum)
121 L.putStrLn $ "changed vt to " <++> tty 127 L.putStrLn $ "VT switch: " <++> tty
122 state <- readMVar tracked 128 state <- modifyMVar tracked $ \(_,us) -> do
129 return ((tty,us),us)
123 forM_ (Set.toList state) $ \jid -> do 130 forM_ (Set.toList state) $ \jid -> do
124 case fmap (==tty) $ resource jid of 131 case fmap (==tty) $ resource jid of
125 Just True -> putStrLn $ bshow jid <++> " Available." 132 Just True -> putStrLn $ bshow jid <++> " Available."
@@ -129,14 +136,14 @@ on_chvt tracked vtnum = do
129 136
130start :: IO () 137start :: IO ()
131start = do 138start = do
132 tracked <- newMVar Set.empty 139 tracked <- newMVar ("",Set.empty)
133 let dologin e = track_login tracked e 140 let dologin e = track_login tracked e
134 dologin :: t -> IO () 141 dologin :: t -> IO ()
135 dologin ()
136#ifndef NOUTMP 142#ifndef NOUTMP
137 installHandler sigUSR1 (Catch (dologin (userError "signaled"))) Nothing 143 installHandler sigUSR1 (Catch (dologin (userError "signaled"))) Nothing
138#endif 144#endif
139 -- installHandler sigTERM (CatchOnce (dologin (userError "term signaled"))) Nothing 145 -- installHandler sigTERM (CatchOnce (dologin (userError "term signaled"))) Nothing
146 mtty <- monitorTTY (on_chvt tracked)
140 inotify <- initINotify 147 inotify <- initINotify
141#ifndef NOUTMP 148#ifndef NOUTMP
142 wd <- addWatch 149 wd <- addWatch
@@ -145,9 +152,11 @@ start = do
145 utmp_file 152 utmp_file
146 dologin 153 dologin
147#endif 154#endif
148 mtty <- monitorTTY (on_chvt tracked)
149 sock <- listenForXmppClients UnixSessions 5222 HNil 155 sock <- listenForXmppClients UnixSessions 5222 HNil
150 putStrLn "Hit enter to terminate..." 156
157 threadDelay 1000 -- wait a moment to obtain current tty
158 dologin ()
159 putStrLn "\nHit enter to terminate...\n"
151 getLine 160 getLine
152 {- 161 {-
153 let doException (SomeException e) = Prelude.putStrLn ("\n\nexception: " ++ show e ++ "\n\n") 162 let doException (SomeException e) = Prelude.putStrLn ("\n\nexception: " ++ show e ++ "\n\n")