|
|
|
@ -2,62 +2,62 @@ |
|
|
|
|
|
|
|
|
|
|
|
Copyright © 2010-2012 Jon Kristensen. |
|
|
|
Copyright © 2010-2012 Jon Kristensen. |
|
|
|
|
|
|
|
|
|
|
|
This file (EchoClient.hs) illustrates how to connect, authenticate, |
|
|
|
This file (EchoClient.hs) illustrates how to connect, authenticate, set a simple |
|
|
|
set a simple presence, receive message stanzas, and echo them back to |
|
|
|
presence, receive message stanzas, and echo them back to whoever is sending |
|
|
|
whoever is sending them, using Pontarius. The contents of this file |
|
|
|
them, using Pontarius. The contents of this file may be used freely, as if it is |
|
|
|
may be used freely, as if it is in the public domain. |
|
|
|
in the public domain. |
|
|
|
|
|
|
|
|
|
|
|
-} |
|
|
|
-} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module Examples.EchoClient () where |
|
|
|
{-# LANGUAGE OverloadedStrings #-} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module Main (main) where |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import Control.Monad (forever) |
|
|
|
|
|
|
|
import Control.Monad.IO.Class (liftIO) |
|
|
|
|
|
|
|
import Data.Maybe (fromJust, isJust) |
|
|
|
|
|
|
|
|
|
|
|
import Network.XMPP |
|
|
|
import Network.XMPP |
|
|
|
|
|
|
|
import Network.XMPP.IM |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Server and authentication details. |
|
|
|
-- Server and authentication details. |
|
|
|
|
|
|
|
|
|
|
|
hostName = "nejla.com" |
|
|
|
hostName = "nejla.com" |
|
|
|
portNumber = 5222 |
|
|
|
portNumber = 5222 |
|
|
|
userName = "test" |
|
|
|
userName = "jon" |
|
|
|
password = "" |
|
|
|
password = "G2D9%b4S3" -- TODO |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Start an XMPP session with the default settings, open the streams |
|
|
|
|
|
|
|
-- to the XMPP server, authenticate, send a simple presence, and start |
|
|
|
|
|
|
|
-- the `echo' XMPP thread. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- TODO: Incomplete code, needs documentation, etc. |
|
|
|
main :: IO () |
|
|
|
main :: IO () |
|
|
|
|
|
|
|
main = do |
|
|
|
main = session default $ do |
|
|
|
withNewSession $ do |
|
|
|
liftIO $ putStrLn "Welcome to the Pontarius EchoClient example!" |
|
|
|
withConnection $ do |
|
|
|
openStreamsResult <- openStreams "nejla.com" |
|
|
|
connect "xmpp.nejla.com" "nejla.com" |
|
|
|
case openStreamsResult of |
|
|
|
-- startTLS exampleParams |
|
|
|
Nothing -> do |
|
|
|
saslResponse <- auth userName password (Just "echo-client") |
|
|
|
liftIO $ putStrLn "Streams opened, now authenticating!" |
|
|
|
case saslResponse of |
|
|
|
authenticateResult <- authenticate userName password Nothing |
|
|
|
Right _ -> return () |
|
|
|
case authenticateResult of |
|
|
|
Left e -> error $ show e |
|
|
|
Right _ -> do -- Ignore XMPP address |
|
|
|
sendPresence presenceOnline |
|
|
|
liftIO $ putStrLn "Authenticating, now sending presence!" |
|
|
|
fork echo |
|
|
|
sendPresence Nothing Nothing [] Nothing -- Simple presence |
|
|
|
return () |
|
|
|
liftIO $ putStrLn "Echoing..." |
|
|
|
return () |
|
|
|
fork echo |
|
|
|
|
|
|
|
Left error -> liftIO $ putStrLn "Error: " ++ $ show exception |
|
|
|
|
|
|
|
Just error -> liftIO $ putStrLn "Error: " ++ $ show exception |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Pull message stanzas, verify that they originate from a `full' XMPP |
|
|
|
-- Pull message stanzas, verify that they originate from a `full' XMPP |
|
|
|
-- address, and, if so, `echo' the message back. |
|
|
|
-- address, and, if so, `echo' the message back. |
|
|
|
|
|
|
|
echo :: XMPP () |
|
|
|
echo :: XMPPThread () -- TODO: XMPP ()? XMPP? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
echo = forever $ do |
|
|
|
echo = forever $ do |
|
|
|
result <- pullMessage |
|
|
|
result <- pullMessage |
|
|
|
case result of |
|
|
|
case result of |
|
|
|
Right message -> |
|
|
|
Right message -> |
|
|
|
if messageFrom message /= Nothing && isFull $ messageFrom message |
|
|
|
if (isJust $ messageFrom message) && |
|
|
|
then do |
|
|
|
(isFull $ fromJust $ messageFrom message) then do |
|
|
|
sendMessage (messageFrom message) (messageType message) Nothing [] |
|
|
|
-- TODO: May not set from. |
|
|
|
liftIO $ putStrLn "Message echoed!" |
|
|
|
sendMessage $ Message Nothing (messageTo message) (messageFrom message) Nothing (messageType message) (messagePayload message) |
|
|
|
else liftIO $ putStrLn "Message sender is not set or is bare!" |
|
|
|
liftIO $ putStrLn "Message echoed!" |
|
|
|
Left exception -> liftIO $ putStrLn "Error: " ++ $ show exception |
|
|
|
else liftIO $ putStrLn "Message sender is not set or is bare!" |
|
|
|
|
|
|
|
Left exception -> liftIO $ putStrLn "Error: " |