You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.7 KiB
80 lines
2.7 KiB
|
14 years ago
|
{-
|
||
|
|
|
||
|
13 years ago
|
|
||
|
13 years ago
|
Copyright © 2010-2012 Jon Kristensen, Philipp Balzarek
|
||
|
14 years ago
|
|
||
|
14 years ago
|
This file (EchoClient.hs) illustrates how to connect, authenticate, set a simple
|
||
|
|
presence, receive message stanzas, and echo them back to whoever is sending
|
||
|
|
them, using Pontarius. The contents of this file may be used freely, as if it is
|
||
|
|
in the public domain.
|
||
|
14 years ago
|
|
||
|
|
-}
|
||
|
|
|
||
|
|
|
||
|
14 years ago
|
{-# LANGUAGE OverloadedStrings #-}
|
||
|
|
|
||
|
13 years ago
|
module Main where
|
||
|
14 years ago
|
|
||
|
13 years ago
|
import Control.Concurrent
|
||
|
|
import Control.Monad
|
||
|
|
import Data.Maybe (fromJust)
|
||
|
|
import qualified Data.Text as Text
|
||
|
|
import Text.Printf
|
||
|
14 years ago
|
|
||
|
13 years ago
|
import Network.TLS
|
||
|
13 years ago
|
import Network.Xmpp
|
||
|
|
import Network.Xmpp.IM
|
||
|
13 years ago
|
import System.IO (stderr)
|
||
|
13 years ago
|
import System.Log.Formatter
|
||
|
|
import System.Log.Handler hiding (setLevel)
|
||
|
|
import System.Log.Handler.Simple
|
||
|
13 years ago
|
import System.Log.Logger
|
||
|
14 years ago
|
|
||
|
|
-- Server and authentication details.
|
||
|
13 years ago
|
realm = "species64739.dyndns.org"
|
||
|
13 years ago
|
username = "echo"
|
||
|
13 years ago
|
password = "pwd"
|
||
|
13 years ago
|
resource = Just "bot"
|
||
|
14 years ago
|
|
||
|
13 years ago
|
-- | Automatically accept all subscription requests from other entities
|
||
|
13 years ago
|
autoAccept :: Session -> IO ()
|
||
|
13 years ago
|
autoAccept session = forever $ do
|
||
|
|
st <- waitForPresence isPresenceSubscribe session
|
||
|
13 years ago
|
friend <- case presenceFrom st of
|
||
|
|
Just from -> do
|
||
|
|
sendPresence (presenceSubscribed from) session
|
||
|
|
return $ show from
|
||
|
|
Nothing -> return "anonymous" -- this shouldn't happen
|
||
|
|
printf "Hello %s !" friend
|
||
|
14 years ago
|
|
||
|
|
main :: IO ()
|
||
|
14 years ago
|
main = do
|
||
|
13 years ago
|
updateGlobalLogger "Pontarius.Xmpp" $ setLevel DEBUG
|
||
|
|
handler <- streamHandler stderr DEBUG >>= \h ->
|
||
|
|
return $ setFormatter h (simpleLogFormatter "$time - $loggername: $prio: $msg")
|
||
|
|
updateGlobalLogger "Pontarius.Xmpp" (addHandler handler)
|
||
|
13 years ago
|
|
||
|
|
sess' <- session
|
||
|
13 years ago
|
realm
|
||
|
13 years ago
|
Nothing -- (Just defaultParamsClient)
|
||
|
|
(Just ([scramSha1 username Nothing password], resource))
|
||
|
|
sess <- case sess' of
|
||
|
|
Left err -> error $ "Error connection to XMPP server: " ++ show err
|
||
|
|
Right (_, Just err) -> error $ "Error while authenticating: " ++ show err
|
||
|
|
Right (sess, Nothing) -> return sess
|
||
|
13 years ago
|
-- We won't be able to receive stanzas before we set out status to online
|
||
|
|
sendPresence presenceOnline sess
|
||
|
|
putStrLn "Connected."
|
||
|
|
-- We want to see all incoming stanzas in the auto-accept thread as well.
|
||
|
|
sess' <- dupSession sess
|
||
|
|
_thread <- forkIO $ autoAccept sess'
|
||
|
|
forever $ do
|
||
|
|
-- Echo all messages back to the user.
|
||
|
|
msg <- getMessage sess
|
||
|
|
sendMessage (answerIM (bodies msg) [] msg) sess
|
||
|
|
-- Print the received message to the screen.
|
||
|
13 years ago
|
let sender = show . fromJust $ messageFrom msg
|
||
|
|
let contents = maybe "nothing" Text.unpack $ body msg
|
||
|
13 years ago
|
printf "%s says \"%s\"\n" sender contents
|
||
|
14 years ago
|
return ()
|