diff options
Diffstat (limited to 'custom/notify-user.hs')
-rw-r--r-- | custom/notify-user.hs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/custom/notify-user.hs b/custom/notify-user.hs new file mode 100644 index 00000000..f9cc2369 --- /dev/null +++ b/custom/notify-user.hs | |||
@@ -0,0 +1,50 @@ | |||
1 | {-# LANGUAGE ViewPatterns, StandaloneDeriving #-} | ||
2 | |||
3 | import System.FilePath.Glob (glob) | ||
4 | import System.Environment (setEnv, getArgs) | ||
5 | import System.Process (spawnProcess, waitForProcess) | ||
6 | import System.Exit (exitWith, ExitCode(..)) | ||
7 | |||
8 | import Data.List (isPrefixOf, dropWhile, dropWhileEnd, init) | ||
9 | import Data.Char (isSpace, toLower, toUpper) | ||
10 | |||
11 | import Control.Monad (forM_, void) | ||
12 | |||
13 | import qualified Libnotify as Notify | ||
14 | import Data.Monoid | ||
15 | |||
16 | import System.Console.GetOpt.Simple | ||
17 | |||
18 | import qualified Data.Map as Map | ||
19 | |||
20 | import Data.Maybe | ||
21 | import Text.Read (readMaybe) | ||
22 | |||
23 | deriving instance Read Notify.Urgency | ||
24 | |||
25 | main :: IO () | ||
26 | main = do | ||
27 | envFiles <- glob "@userHome@/.dbus/session-bus/*" | ||
28 | forM_ envFiles $ \envFile -> do | ||
29 | sessionAddr <- unQuote . tail . snd . break (== '=') . head . filter ("DBUS_SESSION_BUS_ADDRESS=" `isPrefixOf`) . lines <$> readFile envFile | ||
30 | setEnv "DBUS_SESSION_BUS_ADDRESS" sessionAddr | ||
31 | lines <- lines <$> getContents | ||
32 | case lines of | ||
33 | ((trim -> summary):(trim . unlines -> contents)) -> do | ||
34 | (opts, _) <- flip getUsingConf [] [ (arg, "urgency", Optional, "") | ||
35 | , (arg, "app-name", Optional, "") | ||
36 | , (arg, "category", Optional, "") | ||
37 | ] | ||
38 | let | ||
39 | urgency = fromMaybe Notify.Normal $ readMaybe . caseForRead =<< Map.lookup "urgency" opts | ||
40 | appName = fromMaybe "notify-@userName@" $ Map.lookup "app-name" opts | ||
41 | category = fromMaybe "" $ Map.lookup "category" opts | ||
42 | Notify.display_ $ Notify.summary summary <> Notify.body contents <> Notify.appName appName <> Notify.urgency urgency <> Notify.category category | ||
43 | _ -> exitWith $ ExitFailure 2 | ||
44 | where | ||
45 | trim = dropWhileEnd isSpace . dropWhile isSpace | ||
46 | unQuote ('\'':xs) = init xs | ||
47 | unQuote ('"':xs) = init xs | ||
48 | unQuote xs = xs | ||
49 | caseForRead [] = [] | ||
50 | caseForRead (x:xs) = toUpper x : map toLower xs | ||