2017-06-20 19:26:51 +00:00
|
|
|
use std::mem::replace;
|
|
|
|
use std::str::FromStr;
|
|
|
|
use std::error::Error;
|
2017-07-01 23:25:22 +00:00
|
|
|
use tokio_core::reactor::Handle;
|
2017-06-20 19:26:51 +00:00
|
|
|
use tokio_core::net::TcpStream;
|
|
|
|
use tokio_io::{AsyncRead, AsyncWrite};
|
|
|
|
use tokio_tls::TlsStream;
|
2017-07-20 22:39:29 +00:00
|
|
|
use futures::{future, Future, Stream, Poll, Async, Sink, StartSend, AsyncSink};
|
2017-07-17 18:53:00 +00:00
|
|
|
use minidom::Element;
|
2017-06-20 19:26:51 +00:00
|
|
|
use jid::{Jid, JidParseError};
|
|
|
|
use sasl::common::{Credentials, ChannelBinding};
|
2017-07-20 22:39:29 +00:00
|
|
|
use idna;
|
2017-06-20 19:26:51 +00:00
|
|
|
|
|
|
|
use super::xmpp_codec::Packet;
|
|
|
|
use super::xmpp_stream;
|
|
|
|
use super::starttls::{NS_XMPP_TLS, StartTlsClient};
|
2017-07-13 00:56:02 +00:00
|
|
|
use super::happy_eyeballs::Connecter;
|
2017-07-23 00:46:47 +00:00
|
|
|
use super::event::Event;
|
2017-06-20 19:26:51 +00:00
|
|
|
|
|
|
|
mod auth;
|
2017-07-18 20:54:10 +00:00
|
|
|
use self::auth::ClientAuth;
|
2017-06-20 19:26:51 +00:00
|
|
|
mod bind;
|
2017-07-18 20:54:10 +00:00
|
|
|
use self::bind::ClientBind;
|
2017-06-20 19:26:51 +00:00
|
|
|
|
2018-08-02 17:58:19 +00:00
|
|
|
/// XMPP client connection and state
|
2017-06-20 19:26:51 +00:00
|
|
|
pub struct Client {
|
2018-08-02 17:58:19 +00:00
|
|
|
/// The client's current Jabber-Id
|
2017-06-20 19:26:51 +00:00
|
|
|
pub jid: Jid,
|
|
|
|
state: ClientState,
|
|
|
|
}
|
|
|
|
|
|
|
|
type XMPPStream = xmpp_stream::XMPPStream<TlsStream<TcpStream>>;
|
2017-07-18 23:02:45 +00:00
|
|
|
const NS_JABBER_CLIENT: &str = "jabber:client";
|
2017-06-20 19:26:51 +00:00
|
|
|
|
|
|
|
enum ClientState {
|
|
|
|
Invalid,
|
|
|
|
Disconnected,
|
|
|
|
Connecting(Box<Future<Item=XMPPStream, Error=String>>),
|
|
|
|
Connected(XMPPStream),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Start a new XMPP client
|
|
|
|
///
|
|
|
|
/// Start polling the returned instance so that it will connect
|
|
|
|
/// and yield events.
|
2017-07-13 00:56:02 +00:00
|
|
|
pub fn new(jid: &str, password: &str, handle: Handle) -> Result<Self, JidParseError> {
|
2018-08-02 18:10:26 +00:00
|
|
|
let jid = Jid::from_str(jid)?;
|
2017-06-20 19:26:51 +00:00
|
|
|
let password = password.to_owned();
|
|
|
|
let connect = Self::make_connect(jid.clone(), password.clone(), handle);
|
|
|
|
Ok(Client {
|
2017-07-01 23:25:22 +00:00
|
|
|
jid,
|
2017-06-20 19:26:51 +00:00
|
|
|
state: ClientState::Connecting(connect),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-13 00:56:02 +00:00
|
|
|
fn make_connect(jid: Jid, password: String, handle: Handle) -> Box<Future<Item=XMPPStream, Error=String>> {
|
2017-06-20 19:26:51 +00:00
|
|
|
let username = jid.node.as_ref().unwrap().to_owned();
|
2017-07-18 23:02:45 +00:00
|
|
|
let jid1 = jid.clone();
|
|
|
|
let jid2 = jid.clone();
|
2017-06-20 19:26:51 +00:00
|
|
|
let password = password;
|
2017-07-20 22:39:29 +00:00
|
|
|
let domain = match idna::domain_to_ascii(&jid.domain) {
|
|
|
|
Ok(domain) =>
|
|
|
|
domain,
|
|
|
|
Err(e) =>
|
|
|
|
return Box::new(future::err(format!("{:?}", e))),
|
|
|
|
};
|
2017-06-20 19:26:51 +00:00
|
|
|
Box::new(
|
2017-07-20 22:39:29 +00:00
|
|
|
Connecter::from_lookup(handle, &domain, "_xmpp-client._tcp", 5222)
|
2017-07-13 00:56:02 +00:00
|
|
|
.expect("Connector::from_lookup")
|
2017-07-18 23:02:45 +00:00
|
|
|
.and_then(move |tcp_stream|
|
|
|
|
xmpp_stream::XMPPStream::start(tcp_stream, jid1, NS_JABBER_CLIENT.to_owned())
|
2017-07-13 00:56:02 +00:00
|
|
|
.map_err(|e| format!("{}", e))
|
2017-07-18 23:02:45 +00:00
|
|
|
).and_then(|xmpp_stream| {
|
|
|
|
if Self::can_starttls(&xmpp_stream) {
|
2017-07-20 22:39:29 +00:00
|
|
|
Ok(Self::starttls(xmpp_stream))
|
2017-07-13 00:56:02 +00:00
|
|
|
} else {
|
2017-07-20 22:39:29 +00:00
|
|
|
Err("No STARTTLS".to_owned())
|
2017-07-13 00:56:02 +00:00
|
|
|
}
|
2017-07-20 22:39:29 +00:00
|
|
|
}).and_then(|starttls|
|
|
|
|
starttls
|
|
|
|
).and_then(|tls_stream|
|
2017-07-18 23:02:45 +00:00
|
|
|
XMPPStream::start(tls_stream, jid2, NS_JABBER_CLIENT.to_owned())
|
|
|
|
.map_err(|e| format!("{}", e))
|
|
|
|
).and_then(move |xmpp_stream| {
|
|
|
|
Self::auth(xmpp_stream, username, password).expect("auth")
|
|
|
|
}).and_then(|xmpp_stream| {
|
|
|
|
Self::bind(xmpp_stream)
|
|
|
|
}).and_then(|xmpp_stream| {
|
|
|
|
println!("Bound to {}", xmpp_stream.jid);
|
|
|
|
Ok(xmpp_stream)
|
2017-07-13 00:56:02 +00:00
|
|
|
})
|
2017-06-20 19:26:51 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_starttls<S>(stream: &xmpp_stream::XMPPStream<S>) -> bool {
|
|
|
|
stream.stream_features
|
2017-07-17 18:53:00 +00:00
|
|
|
.get_child("starttls", NS_XMPP_TLS)
|
2017-06-20 19:26:51 +00:00
|
|
|
.is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn starttls<S: AsyncRead + AsyncWrite>(stream: xmpp_stream::XMPPStream<S>) -> StartTlsClient<S> {
|
|
|
|
StartTlsClient::from_stream(stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn auth<S: AsyncRead + AsyncWrite>(stream: xmpp_stream::XMPPStream<S>, username: String, password: String) -> Result<ClientAuth<S>, String> {
|
|
|
|
let creds = Credentials::default()
|
|
|
|
.with_username(username)
|
|
|
|
.with_password(password)
|
|
|
|
.with_channel_binding(ChannelBinding::None);
|
|
|
|
ClientAuth::new(stream, creds)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bind<S: AsyncWrite>(stream: xmpp_stream::XMPPStream<S>) -> ClientBind<S> {
|
|
|
|
ClientBind::new(stream)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for Client {
|
2017-07-23 00:46:47 +00:00
|
|
|
type Item = Event;
|
2017-06-20 19:26:51 +00:00
|
|
|
type Error = String;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
|
|
|
let state = replace(&mut self.state, ClientState::Invalid);
|
|
|
|
|
|
|
|
match state {
|
|
|
|
ClientState::Invalid =>
|
|
|
|
Err("invalid client state".to_owned()),
|
|
|
|
ClientState::Disconnected =>
|
2017-07-01 23:25:22 +00:00
|
|
|
Ok(Async::Ready(None)),
|
2017-06-20 19:26:51 +00:00
|
|
|
ClientState::Connecting(mut connect) => {
|
|
|
|
match connect.poll() {
|
|
|
|
Ok(Async::Ready(stream)) => {
|
|
|
|
self.state = ClientState::Connected(stream);
|
2017-07-23 00:46:47 +00:00
|
|
|
Ok(Async::Ready(Some(Event::Online)))
|
2017-06-20 19:26:51 +00:00
|
|
|
},
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
self.state = ClientState::Connecting(connect);
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
},
|
|
|
|
Err(e) =>
|
|
|
|
Err(e),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
ClientState::Connected(mut stream) => {
|
2017-08-24 18:10:58 +00:00
|
|
|
// Poll sink
|
|
|
|
match stream.poll_complete() {
|
|
|
|
Ok(Async::NotReady) => (),
|
|
|
|
Ok(Async::Ready(())) => (),
|
|
|
|
Err(e) =>
|
|
|
|
return Err(e.description().to_owned()),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Poll stream
|
2017-06-20 19:26:51 +00:00
|
|
|
match stream.poll() {
|
|
|
|
Ok(Async::Ready(None)) => {
|
|
|
|
// EOF
|
|
|
|
self.state = ClientState::Disconnected;
|
2017-07-23 00:46:47 +00:00
|
|
|
Ok(Async::Ready(Some(Event::Disconnected)))
|
2017-06-20 19:26:51 +00:00
|
|
|
},
|
|
|
|
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => {
|
|
|
|
self.state = ClientState::Connected(stream);
|
2017-07-23 00:46:47 +00:00
|
|
|
Ok(Async::Ready(Some(Event::Stanza(stanza))))
|
2017-06-20 19:26:51 +00:00
|
|
|
},
|
2017-07-20 22:19:08 +00:00
|
|
|
Ok(Async::NotReady) |
|
2017-06-20 19:26:51 +00:00
|
|
|
Ok(Async::Ready(_)) => {
|
|
|
|
self.state = ClientState::Connected(stream);
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
},
|
|
|
|
Err(e) =>
|
|
|
|
Err(e.description().to_owned()),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-01 23:25:22 +00:00
|
|
|
|
|
|
|
impl Sink for Client {
|
2017-07-17 18:53:00 +00:00
|
|
|
type SinkItem = Element;
|
2017-07-01 23:25:22 +00:00
|
|
|
type SinkError = String;
|
|
|
|
|
|
|
|
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
|
|
|
match self.state {
|
|
|
|
ClientState::Connected(ref mut stream) =>
|
|
|
|
match stream.start_send(Packet::Stanza(item)) {
|
|
|
|
Ok(AsyncSink::NotReady(Packet::Stanza(stanza))) =>
|
|
|
|
Ok(AsyncSink::NotReady(stanza)),
|
|
|
|
Ok(AsyncSink::NotReady(_)) =>
|
|
|
|
panic!("Client.start_send with stanza but got something else back"),
|
|
|
|
Ok(AsyncSink::Ready) => {
|
|
|
|
Ok(AsyncSink::Ready)
|
|
|
|
},
|
|
|
|
Err(e) =>
|
|
|
|
Err(e.description().to_owned()),
|
|
|
|
},
|
|
|
|
_ =>
|
|
|
|
Ok(AsyncSink::NotReady(item)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
|
2017-07-20 22:19:08 +00:00
|
|
|
match self.state {
|
|
|
|
ClientState::Connected(ref mut stream) =>
|
2017-07-01 23:25:22 +00:00
|
|
|
stream.poll_complete()
|
|
|
|
.map_err(|e| e.description().to_owned()),
|
|
|
|
_ =>
|
|
|
|
Ok(Async::Ready(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|