diff options
author | joe <joe@jerkface.net> | 2013-06-17 22:16:31 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2013-06-17 22:16:31 -0400 |
commit | 74c5a3f6cf2404c4907b108699cda00eda0ccfb0 (patch) | |
tree | 04e0957f009be3366004b01699f6bb4a013d2ab5 | |
parent | 012a3b625c0ea236afc1ea09c89d0dcd435a66bd (diff) |
delay a bit before first utmp scan so that we know active tty
-rw-r--r-- | Presence/main.hs | 29 |
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 | |||
33 | import qualified Data.Set as Set | 33 | import qualified Data.Set as Set |
34 | import Data.Set as Set (Set,(\\)) | 34 | import Data.Set as Set (Set,(\\)) |
35 | import Control.Concurrent.MVar.Strict | 35 | import Control.Concurrent.MVar.Strict |
36 | import Control.Concurrent (threadDelay) | ||
36 | import Control.DeepSeq | 37 | import Control.DeepSeq |
38 | import Debug.Trace | ||
37 | 39 | ||
38 | import ByteStringOperators | 40 | import ByteStringOperators |
39 | import qualified Data.ByteString.Lazy.Char8 as L | 41 | import 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 | ||
68 | track_login :: MVar (Set JID) -> t -> IO () | 70 | track_login :: MVar (ByteString,Set JID) -> t -> IO () |
69 | track_login tracked e = do | 71 | track_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 | ||
84 | data UnixSession = UnixSession { | 90 | data UnixSession = UnixSession { |
85 | unix_uid :: (IORef (Maybe UserID)), | 91 | unix_uid :: (IORef (Maybe UserID)), |
@@ -118,8 +124,9 @@ instance XMPPSession UnixSession where | |||
118 | 124 | ||
119 | on_chvt tracked vtnum = do | 125 | on_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 | ||
130 | start :: IO () | 137 | start :: IO () |
131 | start = do | 138 | start = 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") |