more replacement with xmpp-parsers

This commit is contained in:
Astro 2018-08-02 01:01:41 +02:00
parent 71e2b1c84f
commit 0155e3fdac
4 changed files with 29 additions and 32 deletions

View file

@ -1,12 +1,12 @@
use std::mem::replace; 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 tokio_io::{AsyncRead, AsyncWrite};
use minidom::Element;
use sasl::common::Credentials; use sasl::common::Credentials;
use sasl::common::scram::{Sha1, Sha256}; use sasl::common::scram::{Sha1, Sha256};
use sasl::client::Mechanism; use sasl::client::Mechanism;
use sasl::client::mechanisms::{Scram, Plain, Anonymous}; use sasl::client::mechanisms::{Scram, Plain, Anonymous};
use serialize::base64::{self, ToBase64, FromBase64}; use serialize::base64::{self, ToBase64, FromBase64};
use minidom::Element;
use xmpp_codec::Packet; use xmpp_codec::Packet;
use xmpp_stream::XMPPStream; use xmpp_stream::XMPPStream;

View file

@ -1,11 +1,10 @@
use std::mem::replace; use std::mem::replace;
use std::error::Error; use std::error::Error;
use std::str::FromStr; use futures::{Future, Poll, Async, sink, Stream};
use futures::{Future, Poll, Async, sink, Sink, Stream};
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
use jid::Jid; use xmpp_parsers::iq::{Iq, IqType};
use minidom::Element;
use xmpp_parsers::bind::Bind; use xmpp_parsers::bind::Bind;
use try_from::TryFrom;
use xmpp_codec::Packet; use xmpp_codec::Packet;
use xmpp_stream::XMPPStream; use xmpp_stream::XMPPStream;
@ -32,7 +31,8 @@ impl<S: AsyncWrite> ClientBind<S> {
ClientBind::Unsupported(stream), ClientBind::Unsupported(stream),
Some(_) => { Some(_) => {
let resource = stream.jid.resource.clone(); 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); let send = stream.send_stanza(iq);
ClientBind::WaitSend(send) ClientBind::WaitSend(send)
}, },
@ -66,18 +66,26 @@ impl<S: AsyncRead + AsyncWrite> Future for ClientBind<S> {
}, },
ClientBind::WaitRecv(mut stream) => { ClientBind::WaitRecv(mut stream) => {
match stream.poll() { match stream.poll() {
Ok(Async::Ready(Some(Packet::Stanza(ref iq)))) Ok(Async::Ready(Some(Packet::Stanza(stanza)))) =>
if iq.name() == "iq" match Iq::try_from(stanza) {
&& iq.attr("id") == Some(BIND_REQ_ID) => { Ok(iq) => if iq.id == Some(BIND_REQ_ID.to_string()) {
match iq.attr("type") { match iq.payload {
Some("result") => { IqType::Result(payload) => {
get_bind_response_jid(iq) payload
.map(|jid| stream.jid = jid); .and_then(|payload| Bind::try_from(payload).ok())
.map(|bind| match bind {
Bind::Jid(jid) => stream.jid = jid,
_ => {}
});
Ok(Async::Ready(stream)) Ok(Async::Ready(stream))
}, },
_ => _ =>
Err("resource bind response".to_owned()), Err("resource bind response".to_owned()),
} }
} else {
Ok(Async::NotReady)
},
_ => Ok(Async::NotReady),
}, },
Ok(Async::Ready(_)) => { Ok(Async::Ready(_)) => {
replace(self, ClientBind::WaitRecv(stream)); replace(self, ClientBind::WaitRecv(stream));
@ -96,14 +104,3 @@ impl<S: AsyncRead + AsyncWrite> Future for ClientBind<S> {
} }
} }
} }
fn get_bind_response_jid(iq: &Element) -> Option<Jid> {
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()
)
}

View file

@ -1,7 +1,6 @@
use std::mem::replace; 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 tokio_io::{AsyncRead, AsyncWrite};
use minidom::Element;
use xmpp_parsers::component::Handshake; use xmpp_parsers::component::Handshake;
use xmpp_codec::Packet; use xmpp_codec::Packet;

View file

@ -15,6 +15,7 @@ extern crate jid;
extern crate domain; extern crate domain;
extern crate idna; extern crate idna;
extern crate xmpp_parsers; extern crate xmpp_parsers;
extern crate try_from;
pub mod xmpp_codec; pub mod xmpp_codec;
pub mod xmpp_stream; pub mod xmpp_stream;