summaryrefslogtreecommitdiff
path: root/xmppServer.hs
diff options
context:
space:
mode:
Diffstat (limited to 'xmppServer.hs')
-rw-r--r--xmppServer.hs47
1 files changed, 47 insertions, 0 deletions
diff --git a/xmppServer.hs b/xmppServer.hs
new file mode 100644
index 00000000..b0a53e8b
--- /dev/null
+++ b/xmppServer.hs
@@ -0,0 +1,47 @@
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE TupleSections #-}
3{-# LANGUAGE LambdaCase #-}
4import Control.Concurrent
5import Control.Concurrent.STM
6import Control.Monad.Fix
7import Control.Monad.IO.Class
8import Control.Monad.Trans.Resource (runResourceT)
9import Data.Monoid
10import System.Environment
11import System.Posix.Signals
12
13import ConsoleWriter
14import Presence
15import XMPPServer
16
17main :: IO ()
18main = runResourceT $ do
19 args <- liftIO getArgs
20 let verbosity = getSum $ flip foldMap args $ \case
21 ('-':xs) -> Sum $ length (filter (=='-') xs)
22 _ -> mempty
23 cw <- liftIO newConsoleWriter
24 state <- liftIO $ newPresenceState cw
25 sv <- xmppServer (presenceHooks state verbosity)
26 liftIO $ do
27 atomically $ putTMVar (server state) sv
28
29 quitVar <- newEmptyTMVarIO
30 installHandler sigTERM (CatchOnce (atomically $ putTMVar quitVar True)) Nothing
31 installHandler sigINT (CatchOnce (atomically $ putTMVar quitVar True)) Nothing
32
33 forkIO $ do
34 let console = cwPresenceChan $ consoleWriter state
35 fix $ \loop -> do
36 what <- atomically
37 $ orElse (do (client,stanza) <- takeTMVar console
38 return $ do informClientPresence0 state Nothing client stanza
39 loop)
40 (do readTMVar quitVar
41 return $ return ())
42 what
43
44 quitMessage <- atomically $ takeTMVar quitVar
45
46 putStrLn "goodbye."
47 return ()