summaryrefslogtreecommitdiff
path: root/xmppServer.hs
blob: 3a16aca54fd24683db19e93fb5bec0c59996fedc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{-# LANGUAGE OverloadedStrings #-}
import System.Posix.Signals
import Control.Concurrent.STM
import Control.Monad.Trans.Resource (runResourceT)
import Control.Monad.IO.Class (MonadIO, liftIO)
import Network.Socket
    ( addrAddress
    , getAddrInfo
    , defaultHints
    , addrFlags
    , AddrInfoFlag(AI_CANONNAME)
    )
import Data.Monoid ( (<>) )
import qualified Data.Text as Text
import qualified Data.Text.IO as Text
import Control.Monad
import qualified Network.BSD as BSD


import XMPPServer
import Server

main = runResourceT $ do
    hostname <- fmap Text.pack $ liftIO BSD.getHostName
    sv <- xmppServer
        XMPPServerParameters
        { xmppChooseResourceName = \k sock desired -> return $ "nobody@" <> hostname <> "/tty666"
        , xmppTellMyNameToClient = return hostname
        , xmppTellMyNameToPeer = \addr -> return $ addrToText addr
        , xmppTellClientHisName = \k -> return $ "nobody@" <> hostname <> "/tty666"
        , xmppTellPeerHisName = return . peerKeyToText
        , xmppNewConnection = \k outchan -> return ()
        , xmppEOF = \k -> return ()
        , xmppRosterBuddies = \k -> return []
        , xmppRosterSubscribers = \k -> return []
        , xmppRosterSolicited = \k -> return []
        , xmppRosterOthers = \k -> return []
        , xmppSubscribeToRoster = \k -> return ()
        , xmppLookupPeerName = \k ->  return "localhost"
        , xmppLookupClientJID = \k -> return $ "nobody@" <> hostname <> "/tty666"
        , xmppDeliverMessage = \fail msg -> do
            let msgs = msgLangMap (stanzaType msg)
                body = fmap (maybe "" id . msgBody . snd) $ take 1 msgs
            when (not $ null body) $ do
            Text.putStrLn $ "MESSAGE " <> head body
            return ()
        , xmppInformClientPresence = \k stanza -> return ()
        }
    liftIO $ do
    let testaddr0 = "fd97:ca88:fa7c:b94b:c8b8:fad4:1021:a54d"
        -- testaddr0 = "fdef:9e0b:b502:52c3:c074:28d3:fcd7:bfb7"
    testaddr<- fmap (addrAddress . head) $
        getAddrInfo (Just $ defaultHints { addrFlags = [ AI_CANONNAME ]})
                (Just testaddr0)
                (Just "5269")
    putStrLn $ "Connecting to "++show testaddr
    addPeer sv testaddr

    quitVar <- newEmptyTMVarIO
    installHandler sigTERM (CatchOnce (atomically $ putTMVar quitVar True)) Nothing
    installHandler sigINT (CatchOnce (atomically $ putTMVar quitVar True)) Nothing
    quitMessage <- atomically $ takeTMVar quitVar

    putStrLn "goodbye."
    return ()