From 0155e3fdac3223a65adea16d8316c3d6846bc696 Mon Sep 17 00:00:00 2001 From: Astro Date: Thu, 2 Aug 2018 01:01:41 +0200 Subject: [PATCH] more replacement with xmpp-parsers --- src/client/auth.rs | 4 ++-- src/client/bind.rs | 53 ++++++++++++++++++++----------------------- src/component/auth.rs | 3 +-- src/lib.rs | 1 + 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/client/auth.rs b/src/client/auth.rs index 36f66059..d50a6934 100644 --- a/src/client/auth.rs +++ b/src/client/auth.rs @@ -1,12 +1,12 @@ use std::mem::replace; -use futures::{Future, Poll, Async, sink, Sink, Stream}; +use futures::{Future, Poll, Async, sink, Stream}; use tokio_io::{AsyncRead, AsyncWrite}; -use minidom::Element; use sasl::common::Credentials; use sasl::common::scram::{Sha1, Sha256}; use sasl::client::Mechanism; use sasl::client::mechanisms::{Scram, Plain, Anonymous}; use serialize::base64::{self, ToBase64, FromBase64}; +use minidom::Element; use xmpp_codec::Packet; use xmpp_stream::XMPPStream; diff --git a/src/client/bind.rs b/src/client/bind.rs index 348fc307..1ef268cc 100644 --- a/src/client/bind.rs +++ b/src/client/bind.rs @@ -1,11 +1,10 @@ use std::mem::replace; use std::error::Error; -use std::str::FromStr; -use futures::{Future, Poll, Async, sink, Sink, Stream}; +use futures::{Future, Poll, Async, sink, Stream}; use tokio_io::{AsyncRead, AsyncWrite}; -use jid::Jid; -use minidom::Element; +use xmpp_parsers::iq::{Iq, IqType}; use xmpp_parsers::bind::Bind; +use try_from::TryFrom; use xmpp_codec::Packet; use xmpp_stream::XMPPStream; @@ -32,7 +31,8 @@ impl ClientBind { ClientBind::Unsupported(stream), Some(_) => { let resource = stream.jid.resource.clone(); - let iq = Bind::new(resource); + let iq = Iq::from_set(Bind::new(resource)) + .with_id(BIND_REQ_ID.to_string()); let send = stream.send_stanza(iq); ClientBind::WaitSend(send) }, @@ -66,18 +66,26 @@ impl Future for ClientBind { }, ClientBind::WaitRecv(mut stream) => { match stream.poll() { - Ok(Async::Ready(Some(Packet::Stanza(ref iq)))) - if iq.name() == "iq" - && iq.attr("id") == Some(BIND_REQ_ID) => { - match iq.attr("type") { - Some("result") => { - get_bind_response_jid(iq) - .map(|jid| stream.jid = jid); - Ok(Async::Ready(stream)) - }, - _ => - Err("resource bind response".to_owned()), - } + Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => + match Iq::try_from(stanza) { + Ok(iq) => if iq.id == Some(BIND_REQ_ID.to_string()) { + match iq.payload { + IqType::Result(payload) => { + payload + .and_then(|payload| Bind::try_from(payload).ok()) + .map(|bind| match bind { + Bind::Jid(jid) => stream.jid = jid, + _ => {} + }); + Ok(Async::Ready(stream)) + }, + _ => + Err("resource bind response".to_owned()), + } + } else { + Ok(Async::NotReady) + }, + _ => Ok(Async::NotReady), }, Ok(Async::Ready(_)) => { replace(self, ClientBind::WaitRecv(stream)); @@ -96,14 +104,3 @@ impl Future for ClientBind { } } } - -fn get_bind_response_jid(iq: &Element) -> Option { - iq.get_child("bind", NS_XMPP_BIND) - .and_then(|bind_el| - bind_el.get_child("jid", NS_XMPP_BIND) - ) - .and_then(|jid_el| - Jid::from_str(&jid_el.text()) - .ok() - ) -} diff --git a/src/component/auth.rs b/src/component/auth.rs index d53cc67b..7801c92f 100644 --- a/src/component/auth.rs +++ b/src/component/auth.rs @@ -1,7 +1,6 @@ use std::mem::replace; -use futures::{Future, Poll, Async, sink, Sink, Stream}; +use futures::{Future, Poll, Async, sink, Stream}; use tokio_io::{AsyncRead, AsyncWrite}; -use minidom::Element; use xmpp_parsers::component::Handshake; use xmpp_codec::Packet; diff --git a/src/lib.rs b/src/lib.rs index 222b3c47..6ee15d1d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ extern crate jid; extern crate domain; extern crate idna; extern crate xmpp_parsers; +extern crate try_from; pub mod xmpp_codec; pub mod xmpp_stream;