diff options
Diffstat (limited to 'xmppServer.hs')
-rw-r--r-- | xmppServer.hs | 47 |
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 #-} | ||
4 | import Control.Concurrent | ||
5 | import Control.Concurrent.STM | ||
6 | import Control.Monad.Fix | ||
7 | import Control.Monad.IO.Class | ||
8 | import Control.Monad.Trans.Resource (runResourceT) | ||
9 | import Data.Monoid | ||
10 | import System.Environment | ||
11 | import System.Posix.Signals | ||
12 | |||
13 | import ConsoleWriter | ||
14 | import Presence | ||
15 | import XMPPServer | ||
16 | |||
17 | main :: IO () | ||
18 | main = 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 () | ||