summaryrefslogtreecommitdiff
path: root/Presence/main.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2013-06-15 15:07:19 -0400
committerjoe <joe@jerkface.net>2013-06-15 15:07:19 -0400
commit9fd2107e6a7469fe7ba51448e4fe195bf54d7d29 (patch)
treebb37572b478170e461990695e7d9e6ab823f7606 /Presence/main.hs
started project
Diffstat (limited to 'Presence/main.hs')
-rw-r--r--Presence/main.hs85
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
2import System.Directory
3import System.IO
4import Control.Monad
5import System.Posix.Signals
6import System.Posix.Types
7import Control.Monad.Error.Class
8import Control.Exception (throw)
9import System.Posix.Process
10import Data.Maybe
11
12import System.INotify
13import UTmp
14import FGConsole
15
16jid user host rsrc = user ++ "@" ++ host ++ "/" ++ rsrc
17
18toJabberId :: String -> (String,String,t) -> Maybe String
19toJabberId host (user,tty,_) =
20 if take 3 tty == "tty"
21 then Just (jid user host tty)
22 else Nothing
23
24utmp_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
32on_chvt vtnum = do
33 putStrLn $ "changed vt to "++ show vtnum
34
35start :: IO ()
36start = 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
52sendUSR1 pid = do
53 signalProcess sigUSR1 pid
54
55getStartupAction [] = throw (userError "pid file?") >> return (Right "")
56getStartupAction (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
74runOnce 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
82main = do
83 runOnce ["/var/run/presence.pid","/tmp/presence.pid"] start sendUSR1
84
85