summaryrefslogtreecommitdiff
path: root/Presence
diff options
context:
space:
mode:
Diffstat (limited to 'Presence')
-rw-r--r--Presence/ConsoleWriter.hs42
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
16import Data.Char 16import Data.Char
17import Data.Maybe 17import Data.Maybe
18import System.Environment 18import System.Environment
19import System.Posix.Files ( getFileStatus, fileMode )
19import System.INotify ( initINotify, EventVariety(Modify), addWatch ) 20import System.INotify ( initINotify, EventVariety(Modify), addWatch )
20import Data.Word ( Word8 ) 21import Data.Word ( Word8 )
21import Data.Text ( Text ) 22import Data.Text ( Text )
22import Data.Map ( Map ) 23import Data.Map ( Map )
23import Data.List ( foldl' ) 24import Data.List ( foldl', groupBy )
25import Data.Bits ( (.&.) )
24import qualified Data.Map as Map 26import qualified Data.Map as Map
25import qualified Data.Traversable as Traversable 27import qualified Data.Traversable as Traversable
26import qualified Data.Text as Text 28import 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
185readEnvFile :: String -> FilePath -> IO (Maybe String)
186readEnvFile 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
183writeActiveTTY :: ConsoleWriter -> Stanza -> IO Bool 198writeActiveTTY :: ConsoleWriter -> Stanza -> IO Bool
184writeActiveTTY cw msg = do 199writeActiveTTY 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
214deliverGUIMessage 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
219deliverTerminalMessage 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
194writeAllPty :: ConsoleWriter -> Stanza -> IO Bool 226writeAllPty :: ConsoleWriter -> Stanza -> IO Bool