summaryrefslogtreecommitdiff
path: root/custom/notify-user.hs
diff options
context:
space:
mode:
Diffstat (limited to 'custom/notify-user.hs')
-rw-r--r--custom/notify-user.hs50
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
3import System.FilePath.Glob (glob)
4import System.Environment (setEnv, getArgs)
5import System.Process (spawnProcess, waitForProcess)
6import System.Exit (exitWith, ExitCode(..))
7
8import Data.List (isPrefixOf, dropWhile, dropWhileEnd, init)
9import Data.Char (isSpace, toLower, toUpper)
10
11import Control.Monad (forM_, void)
12
13import qualified Libnotify as Notify
14import Data.Monoid
15
16import System.Console.GetOpt.Simple
17
18import qualified Data.Map as Map
19
20import Data.Maybe
21import Text.Read (readMaybe)
22
23deriving instance Read Notify.Urgency
24
25main :: IO ()
26main = 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