From c08d7e75b72306334bc82c726f300f8ec4b5c72d Mon Sep 17 00:00:00 2001 From: Jon Kristensen Date: Thu, 14 Feb 2013 22:12:56 +0100 Subject: [PATCH] Moved two stream-related pickler/unpickler's to Marshal --- source/Network/Xmpp/Marshal.hs | 42 +++++++++++++++++++++++++++++++++- source/Network/Xmpp/Stream.hs | 39 ------------------------------- 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/source/Network/Xmpp/Marshal.hs b/source/Network/Xmpp/Marshal.hs index 38e9b0e..93fcdc6 100644 --- a/source/Network/Xmpp/Marshal.hs +++ b/source/Network/Xmpp/Marshal.hs @@ -11,6 +11,8 @@ module Network.Xmpp.Marshal where import Data.XML.Pickle import Data.XML.Types +import Data.Text + import Network.Xmpp.Types xpStreamStanza :: PU [Node] (Either StreamErrorInfo Stanza) @@ -222,7 +224,7 @@ unpickleElem :: PU [Node] a -> Element -> Either UnpickleError a unpickleElem p x = unpickle (xpNodeElem p) x xpNodeElem :: PU [Node] a -> PU Element a -xpNodeElem xp = PU { pickleTree = \x -> head $ (pickleTree xp x) >>= \y -> +xpNodeElem xp = PU { pickleTree = \x -> Prelude.head $ (pickleTree xp x) >>= \y -> case y of NodeElement e -> [e] _ -> [] @@ -238,3 +240,41 @@ mbl Nothing = [] lmb :: [t] -> Maybe [t] lmb [] = Nothing lmb x = Just x + +xpStream :: PU [Node] (Text, Maybe Jid, Maybe Jid, Maybe Text, Maybe LangTag) +xpStream = xpElemAttrs + (Name "stream" (Just "http://etherx.jabber.org/streams") (Just "stream")) + (xp5Tuple + (xpAttr "version" xpId) + (xpAttrImplied "from" xpPrim) + (xpAttrImplied "to" xpPrim) + (xpAttrImplied "id" xpId) + xpLangTag + ) + +-- Pickler/Unpickler for the stream features - TLS, SASL, and the rest. +xpStreamFeatures :: PU [Node] ServerFeatures +xpStreamFeatures = xpWrap + (\(tls, sasl, rest) -> SF tls (mbl sasl) rest) + (\(SF tls sasl rest) -> (tls, lmb sasl, rest)) + (xpElemNodes + (Name + "features" + (Just "http://etherx.jabber.org/streams") + (Just "stream") + ) + (xpTriple + (xpOption pickleTlsFeature) + (xpOption pickleSaslFeature) + (xpAll xpElemVerbatim) + ) + ) + where + pickleTlsFeature :: PU [Node] Bool + pickleTlsFeature = xpElemNodes "{urn:ietf:params:xml:ns:xmpp-tls}starttls" + (xpElemExists "required") + pickleSaslFeature :: PU [Node] [Text] + pickleSaslFeature = xpElemNodes + "{urn:ietf:params:xml:ns:xmpp-sasl}mechanisms" + (xpAll $ xpElemNodes + "{urn:ietf:params:xml:ns:xmpp-sasl}mechanism" (xpContent xpId)) diff --git a/source/Network/Xmpp/Stream.hs b/source/Network/Xmpp/Stream.hs index 614e522..f947692 100644 --- a/source/Network/Xmpp/Stream.hs +++ b/source/Network/Xmpp/Stream.hs @@ -229,45 +229,6 @@ streamS expectedTo = do Nothing -> throwError XmppOtherFailure Just r -> streamUnpickleElem xpStreamFeatures r - -xpStream :: PU [Node] (Text, Maybe Jid, Maybe Jid, Maybe Text, Maybe LangTag) -xpStream = xpElemAttrs - (Name "stream" (Just "http://etherx.jabber.org/streams") (Just "stream")) - (xp5Tuple - (xpAttr "version" xpId) - (xpAttrImplied "from" xpPrim) - (xpAttrImplied "to" xpPrim) - (xpAttrImplied "id" xpId) - xpLangTag - ) - --- Pickler/Unpickler for the stream features - TLS, SASL, and the rest. -xpStreamFeatures :: PU [Node] ServerFeatures -xpStreamFeatures = xpWrap - (\(tls, sasl, rest) -> SF tls (mbl sasl) rest) - (\(SF tls sasl rest) -> (tls, lmb sasl, rest)) - (xpElemNodes - (Name - "features" - (Just "http://etherx.jabber.org/streams") - (Just "stream") - ) - (xpTriple - (xpOption pickleTlsFeature) - (xpOption pickleSaslFeature) - (xpAll xpElemVerbatim) - ) - ) - where - pickleTlsFeature :: PU [Node] Bool - pickleTlsFeature = xpElemNodes "{urn:ietf:params:xml:ns:xmpp-tls}starttls" - (xpElemExists "required") - pickleSaslFeature :: PU [Node] [Text] - pickleSaslFeature = xpElemNodes - "{urn:ietf:params:xml:ns:xmpp-sasl}mechanisms" - (xpAll $ xpElemNodes - "{urn:ietf:params:xml:ns:xmpp-sasl}mechanism" (xpContent xpId)) - -- | Connects to the XMPP server and opens the XMPP stream against the given -- host name, port, and realm. openStream :: HostName -> PortID -> Text -> IO (Either XmppFailure (TMVar Stream))