diff options
author | joe <joe@jerkface.net> | 2014-03-16 02:30:09 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2014-03-16 02:30:09 -0400 |
commit | 0658b76ea0874d0ea789659effed102002486d01 (patch) | |
tree | 90b83aa0c41e221fe99638e5dd54abed895071e4 /Presence/ConsoleWriter.hs | |
parent | 246ef43e0b7ecb143a042d9909b1b85640d55bf9 (diff) |
read environment from proc for DISPLAY and check g+w of tty
Diffstat (limited to 'Presence/ConsoleWriter.hs')
-rw-r--r-- | Presence/ConsoleWriter.hs | 42 |
1 files changed, 37 insertions, 5 deletions
diff --git a/Presence/ConsoleWriter.hs b/Presence/ConsoleWriter.hs index 90849727..2024131b 100644 --- a/Presence/ConsoleWriter.hs +++ b/Presence/ConsoleWriter.hs | |||
@@ -16,11 +16,13 @@ import Data.Monoid | |||
16 | import Data.Char | 16 | import Data.Char |
17 | import Data.Maybe | 17 | import Data.Maybe |
18 | import System.Environment | 18 | import System.Environment |
19 | import System.Posix.Files ( getFileStatus, fileMode ) | ||
19 | import System.INotify ( initINotify, EventVariety(Modify), addWatch ) | 20 | import System.INotify ( initINotify, EventVariety(Modify), addWatch ) |
20 | import Data.Word ( Word8 ) | 21 | import Data.Word ( Word8 ) |
21 | import Data.Text ( Text ) | 22 | import Data.Text ( Text ) |
22 | import Data.Map ( Map ) | 23 | import Data.Map ( Map ) |
23 | import Data.List ( foldl' ) | 24 | import Data.List ( foldl', groupBy ) |
25 | import Data.Bits ( (.&.) ) | ||
24 | import qualified Data.Map as Map | 26 | import qualified Data.Map as Map |
25 | import qualified Data.Traversable as Traversable | 27 | import qualified Data.Traversable as Traversable |
26 | import qualified Data.Text as Text | 28 | import qualified Data.Text as Text |
@@ -180,15 +182,45 @@ messageText msg = do | |||
180 | ts = catMaybes [subj, msgBody choice] | 182 | ts = catMaybes [subj, msgBody choice] |
181 | return $ Text.intercalate "\n\n" ts | 183 | return $ Text.intercalate "\n\n" ts |
182 | 184 | ||
185 | readEnvFile :: String -> FilePath -> IO (Maybe String) | ||
186 | readEnvFile var file = fmap parse $ readFile file | ||
187 | where | ||
188 | parse xs = listToMaybe $ map (drop 1 . concat . drop 1) $ filter ofinterest bs | ||
189 | where | ||
190 | bs = map (groupBy (\_ x -> x/='=')) $ split (/='\0') xs | ||
191 | ofinterest (k:vs) | k==var = True | ||
192 | ofinterest _ = False | ||
193 | |||
194 | split pred xs = take 1 gs ++ map (drop 1) (drop 1 gs) | ||
195 | where | ||
196 | gs = groupBy (\_ x -> pred x) xs | ||
197 | |||
183 | writeActiveTTY :: ConsoleWriter -> Stanza -> IO Bool | 198 | writeActiveTTY :: ConsoleWriter -> Stanza -> IO Bool |
184 | writeActiveTTY cw msg = do | 199 | writeActiveTTY cw msg = do |
185 | tty <- atomically $ do | 200 | (tty, mbu) <- atomically $ do |
186 | num <- readTVar $ csActiveTTY cw | 201 | num <- readTVar $ csActiveTTY cw |
187 | return $ "/dev/tty" <> tshow num | 202 | utmp <- readTVar $ csUtmp cw |
188 | -- TODO: verify mode g+w | 203 | mbu <- maybe (return Nothing) readTVar |
189 | -- TODO: deliver to active console if not x | 204 | $ Map.lookup ("tty"<>tshow num) utmp |
205 | return ( "/dev/tty" <> tshow num | ||
206 | , mbu ) | ||
207 | flip (maybe $ return False) mbu $ \utmp -> do | ||
208 | display <- fmap (fmap Text.pack) | ||
209 | $ readEnvFile "DISPLAY" ("/proc/" ++ show (utmpPid utmp) ++ "/environ") | ||
210 | case fmap (==utmpHost utmp) display of | ||
211 | Just True -> deliverGUIMessage cw tty utmp msg | ||
212 | _ -> deliverTerminalMessage cw tty utmp msg | ||
213 | |||
214 | deliverGUIMessage cw tty utmp msg = do | ||
190 | -- TODO: deliver to active x (notify-send of libnotify package) | 215 | -- TODO: deliver to active x (notify-send of libnotify package) |
191 | -- chpst seems neccessary for notify-send to work | 216 | -- chpst seems neccessary for notify-send to work |
217 | return False | ||
218 | |||
219 | deliverTerminalMessage cw tty utmp msg = do | ||
220 | mode <- fmap fileMode (getFileStatus $ Text.unpack tty) | ||
221 | let mesgy = mode .&. 0o020 /= 0 -- verify mode g+w | ||
222 | if not mesgy then return False else do | ||
223 | -- TODO: deliver to active console | ||
192 | return False -- return True if a message was delivered | 224 | return False -- return True if a message was delivered |
193 | 225 | ||
194 | writeAllPty :: ConsoleWriter -> Stanza -> IO Bool | 226 | writeAllPty :: ConsoleWriter -> Stanza -> IO Bool |