Browse Source

updated example, cleanup

master
Philipp Balzarek 14 years ago
parent
commit
5eab69b427
  1. 32
      src/Main.hs
  2. 25
      src/Network/XMPP.hs
  3. 5
      src/Network/XMPP/Bind.hs
  4. 2
      src/Network/XMPP/Concurrent.hs
  5. 16
      src/Network/XMPP/Monad.hs
  6. 4
      src/Network/XMPP/TLS.hs

32
src/Main.hs

@ -39,41 +39,21 @@ mirror = forever $ do
(Just $ "you wrote: " `T.append` bd) thr [] (Just $ "you wrote: " `T.append` bd) thr []
_ -> return () _ -> return ()
-- killer = forever $ do
-- st <- readChanS
-- case st of
-- Message _ _ id tp subject "kill" thr _ ->
-- killConnection >> return ()
-- _ -> return ()
main :: IO () main :: IO ()
main = do main = do
putStrLn "hello world" sessionConnect "localhost" "species64739.dyndns.org" "bot" Nothing $ do
wait <- newEmptyMVar singleThreaded $ xmppStartTLS exampleParams
connectXMPP "localhost" "species64739.dyndns.org" "bot" (Just "botsi") "pwd" singleThreaded $ xmppSASL "pwd"
$ do singleThreaded $ xmppBind (Just "botsi")
liftIO $ putStrLn "----------------------------" singleThreaded $ xmppSession
-- sendS . SPresence $
-- Presence Nothing Nothing Nothing Nothing (Just Available) Nothing Nothing []
forkXMPP autoAccept forkXMPP autoAccept
forkXMPP mirror forkXMPP mirror
-- withNewThread killer
sendS . SPresence $ Presence Nothing Nothing Nothing Nothing sendS . SPresence $ Presence Nothing Nothing Nothing Nothing
(Just Available) Nothing Nothing [] (Just Available) Nothing Nothing []
liftIO $ putStrLn "----------------------------"
sendS . SMessage $ Message Nothing philonous Nothing Nothing Nothing sendS . SMessage $ Message Nothing philonous Nothing Nothing Nothing
(Just "bla") Nothing [] (Just "bla") Nothing []
-- forever $ pullMessage >>= liftIO . print liftIO . forever $ threadDelay 1000000
-- withNewThread . void $ (liftIO $ threadDelay 15000000) >> killConnection
-- forever $ do
-- next <- nextM
-- outStanza $ Message Nothing philonous "" Chat "" "pong!" "" []
-- liftIO $ print next
liftIO $ putMVar wait ()
return () return ()
takeMVar wait
return () return ()

25
src/Network/XMPP.hs

@ -1,5 +1,16 @@
{-# LANGUAGE NoMonomorphismRestriction, OverloadedStrings #-} {-# LANGUAGE NoMonomorphismRestriction, OverloadedStrings #-}
module Network.XMPP where module Network.XMPP
( module Network.XMPP.Bind
, module Network.XMPP.Concurrent
, module Network.XMPP.Monad
, module Network.XMPP.SASL
, module Network.XMPP.Session
, module Network.XMPP.Stream
, module Network.XMPP.TLS
, module Network.XMPP.Types
, connectXMPP
, sessionConnect
) where
import Control.Monad import Control.Monad
import Control.Monad.IO.Class import Control.Monad.IO.Class
@ -31,7 +42,7 @@ fromHandle handle hostname username resource password a =
-- on it's own -- on it's own
xmppStartTLS exampleParams xmppStartTLS exampleParams
xmppSASL password xmppSASL password
xmppBind xmppBind resource
xmppSession xmppSession
runThreaded a runThreaded a
return () return ()
@ -47,7 +58,7 @@ fromHandle' handle hostname username resource password a =
-- on it's own -- on it's own
singleThreaded $ xmppStartTLS exampleParams singleThreaded $ xmppStartTLS exampleParams
singleThreaded $ xmppSASL password singleThreaded $ xmppSASL password
singleThreaded $ xmppBind singleThreaded $ xmppBind resource
singleThreaded $ xmppSession singleThreaded $ xmppSession
a a
return () return ()
@ -58,3 +69,11 @@ connectXMPP host hostname username resource passwd a = do
con <- connectTo host (PortNumber 5222) con <- connectTo host (PortNumber 5222)
hSetBuffering con NoBuffering hSetBuffering con NoBuffering
fromHandle' con hostname username resource passwd a fromHandle' con hostname username resource passwd a
sessionConnect :: HostName -> Text -> Text
-> Maybe Text -> XMPPThread a -> IO (a, XMPPState)
sessionConnect host hostname username resource a = do
con <- connectTo host (PortNumber 5222)
hSetBuffering con NoBuffering
xmppFromHandle con hostname username resource $
xmppStartStream >> runThreaded a

5
src/Network/XMPP/Bind.hs

@ -27,9 +27,8 @@ bindReqIQ resource= SIQ $ IQ Nothing Nothing "bind" Set
jidP :: PU [Node] JID jidP :: PU [Node] JID
jidP = bindP $ xpElemNodes "jid" (xpContent xpPrim) jidP = bindP $ xpElemNodes "jid" (xpContent xpPrim)
xmppBind :: XMPPMonad () xmppBind :: Maybe Text -> XMPPMonad ()
xmppBind = do xmppBind res = do
res <- gets sResource
push $ bindReqIQ res push $ bindReqIQ res
answer <- pull answer <- pull
let SIQ (IQ Nothing Nothing _ Result b) = answer let SIQ (IQ Nothing Nothing _ Result b) = answer

2
src/Network/XMPP/Concurrent.hs

@ -334,6 +334,6 @@ sendIQ to tp body = do -- TODO: add timeout
-- TODO: Check for id collisions (shouldn't happen?) -- TODO: Check for id collisions (shouldn't happen?)
return resRef return resRef
sendS . SIQ $ IQ Nothing (Just to) newId tp body sendS . SIQ $ IQ Nothing (Just to) newId tp body
return (readTMVar ref) return ref

16
src/Network/XMPP/Monad.hs

@ -88,19 +88,3 @@ xmppFromHandle handle hostname username resource f = runResourceT $ do
resource resource
runStateT f st runStateT f st
xml =
[ "<?xml version='1.0'?>"
, "<stream:stream xmlns='jabber:client' "
, "xmlns:stream='http://etherx.jabber.org/streams' id='1365401808' "
, "from='examplehost.org' version='1.0' xml:lang='en'>"
, "<stream:features>"
, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"
, error "Booh!"
] :: [BS.ByteString]
main :: IO ()
main = (runResourceT $ CL.sourceList xml $= XP.parseBytes def $$ CL.take 2 )
>>= print

4
src/Network/XMPP/TLS.hs

@ -33,7 +33,7 @@ starttlsE =
exampleParams :: TLSParams exampleParams :: TLSParams
exampleParams = TLS.defaultParams {TLS.pCiphers = TLS.ciphersuite_strong} exampleParams = TLS.defaultParams {TLS.pCiphers = TLS.ciphersuite_strong}
xmppStartTLS :: TLSParams -> XMPPMonad Bool xmppStartTLS :: TLSParams -> XMPPMonad ()
xmppStartTLS params = do xmppStartTLS params = do
features <- gets sFeatures features <- gets sFeatures
unless (stls features == Nothing) $ do unless (stls features == Nothing) $ do
@ -51,5 +51,5 @@ xmppStartTLS params = do
}) })
xmppRestartStream xmppRestartStream
modify (\s -> s{sHaveTLS = True}) modify (\s -> s{sHaveTLS = True})
gets sHaveTLS return ()

Loading…
Cancel
Save