diff options
author | joe <joe@jerkface.net> | 2013-06-15 15:07:19 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2013-06-15 15:07:19 -0400 |
commit | 9fd2107e6a7469fe7ba51448e4fe195bf54d7d29 (patch) | |
tree | bb37572b478170e461990695e7d9e6ab823f7606 /Presence/main.hs |
started project
Diffstat (limited to 'Presence/main.hs')
-rw-r--r-- | Presence/main.hs | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/Presence/main.hs b/Presence/main.hs new file mode 100644 index 00000000..99f58ee4 --- /dev/null +++ b/Presence/main.hs | |||
@@ -0,0 +1,85 @@ | |||
1 | |||
2 | import System.Directory | ||
3 | import System.IO | ||
4 | import Control.Monad | ||
5 | import System.Posix.Signals | ||
6 | import System.Posix.Types | ||
7 | import Control.Monad.Error.Class | ||
8 | import Control.Exception (throw) | ||
9 | import System.Posix.Process | ||
10 | import Data.Maybe | ||
11 | |||
12 | import System.INotify | ||
13 | import UTmp | ||
14 | import FGConsole | ||
15 | |||
16 | jid user host rsrc = user ++ "@" ++ host ++ "/" ++ rsrc | ||
17 | |||
18 | toJabberId :: String -> (String,String,t) -> Maybe String | ||
19 | toJabberId host (user,tty,_) = | ||
20 | if take 3 tty == "tty" | ||
21 | then Just (jid user host tty) | ||
22 | else Nothing | ||
23 | |||
24 | utmp_event e = do | ||
25 | -- print e | ||
26 | putStrLn "---" | ||
27 | us <- users | ||
28 | let ids = mapMaybe (toJabberId "localhost") us | ||
29 | ids :: [String] | ||
30 | forM_ ids putStrLn | ||
31 | |||
32 | on_chvt vtnum = do | ||
33 | putStrLn $ "changed vt to "++ show vtnum | ||
34 | |||
35 | start :: IO () | ||
36 | start = do | ||
37 | installHandler sigUSR1 (Catch (utmp_event (userError "signaled"))) Nothing | ||
38 | inotify <- initINotify | ||
39 | print inotify | ||
40 | wd <- addWatch | ||
41 | inotify | ||
42 | [CloseWrite] -- [Open,Close,Access,Modify,Move] | ||
43 | utmp_file | ||
44 | utmp_event | ||
45 | print wd | ||
46 | mtty <- monitorTTY on_chvt | ||
47 | putStrLn "Hit enter to terminate..." | ||
48 | getLine | ||
49 | unmonitorTTY mtty | ||
50 | removeWatch wd | ||
51 | |||
52 | sendUSR1 pid = do | ||
53 | signalProcess sigUSR1 pid | ||
54 | |||
55 | getStartupAction [] = throw (userError "pid file?") >> return (Right "") | ||
56 | getStartupAction (p:ps) = do | ||
57 | catch | ||
58 | ( do | ||
59 | pid <- fmap CPid (readFile p >>= readIO) | ||
60 | -- signal pid | ||
61 | return (Left pid) ) | ||
62 | onEr | ||
63 | where | ||
64 | onEr e = do | ||
65 | pid <- getProcessID | ||
66 | putStrLn $ "starting pid = "++show pid | ||
67 | catch (do | ||
68 | writeFile p (show pid) | ||
69 | putStrLn $ "writing "++show p | ||
70 | -- start daemon | ||
71 | return (Right p) ) | ||
72 | (\_ -> getStartupAction ps) | ||
73 | |||
74 | runOnce ps run notify = getStartupAction ps >>= doit | ||
75 | where | ||
76 | doit (Left pid ) = notify pid | ||
77 | doit (Right pidfile ) = do | ||
78 | run | ||
79 | removeFile pidfile | ||
80 | |||
81 | |||
82 | main = do | ||
83 | runOnce ["/var/run/presence.pid","/tmp/presence.pid"] start sendUSR1 | ||
84 | |||
85 | |||