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;
|
|
|
|
use futures::*;
|
|
|
|
use jid::{Jid, JidParseError};
|
|
|
|
use xml;
|
|
|
|
use sasl::common::{Credentials, ChannelBinding};
|
|
|
|
|
|
|
|
use super::xmpp_codec::Packet;
|
|
|
|
use super::xmpp_stream;
|
|
|
|
use super::tcp::TcpClient;
|
|
|
|
use super::starttls::{NS_XMPP_TLS, StartTlsClient};
|
|
|
|
|
|
|
|
mod auth;
|
|
|
|
use self::auth::*;
|
|
|
|
mod bind;
|
|
|
|
use self::bind::*;
|
|
|
|
|
|
|
|
pub struct Client {
|
|
|
|
pub jid: Jid,
|
|
|
|
state: ClientState,
|
|
|
|
}
|
|
|
|
|
|
|
|
type XMPPStream = xmpp_stream::XMPPStream<TlsStream<TcpStream>>;
|
|
|
|
|
|
|
|
enum ClientState {
|
|
|
|
Invalid,
|
|
|
|
Disconnected,
|
|
|
|
Connecting(Box<Future<Item=XMPPStream, Error=String>>),
|
|
|
|
Connected(XMPPStream),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Client {
|
|
|
|
pub fn new(jid: &str, password: &str, handle: &Handle) -> Result<Self, JidParseError> {
|
|
|
|
let jid = try!(Jid::from_str(jid));
|
|
|
|
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),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_connect(jid: Jid, password: String, handle: &Handle) -> Box<Future<Item=XMPPStream, Error=String>> {
|
2017-07-01 23:26:36 +00:00
|
|
|
// TODO: implement proper DNS SRV lookup
|
2017-06-20 19:26:51 +00:00
|
|
|
use std::net::ToSocketAddrs;
|
|
|
|
let addr = "89.238.79.220:5222"
|
|
|
|
.to_socket_addrs().unwrap()
|
|
|
|
.next().unwrap();
|
|
|
|
let username = jid.node.as_ref().unwrap().to_owned();
|
|
|
|
let password = password;
|
|
|
|
Box::new(
|
|
|
|
TcpClient::connect(
|
|
|
|
jid,
|
|
|
|
&addr,
|
|
|
|
handle
|
|
|
|
).map_err(|e| format!("{}", e)
|
|
|
|
).and_then(|stream| {
|
|
|
|
if Self::can_starttls(&stream) {
|
|
|
|
Self::starttls(stream)
|
|
|
|
} else {
|
|
|
|
panic!("No STARTTLS")
|
|
|
|
}
|
|
|
|
}).and_then(move |stream| {
|
|
|
|
Self::auth(stream, username, password).expect("auth")
|
|
|
|
}).and_then(|stream| {
|
|
|
|
Self::bind(stream)
|
|
|
|
}).and_then(|stream| {
|
|
|
|
println!("Bound to {}", stream.jid);
|
2017-07-01 23:25:22 +00:00
|
|
|
Ok(stream)
|
2017-06-20 19:26:51 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_starttls<S>(stream: &xmpp_stream::XMPPStream<S>) -> bool {
|
|
|
|
stream.stream_features
|
|
|
|
.get_child("starttls", Some(NS_XMPP_TLS))
|
|
|
|
.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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ClientEvent {
|
|
|
|
Online,
|
|
|
|
Disconnected,
|
|
|
|
Stanza(xml::Element),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stream for Client {
|
|
|
|
type Item = ClientEvent;
|
|
|
|
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-01 23:25:22 +00:00
|
|
|
Ok(Async::Ready(Some(ClientEvent::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) => {
|
|
|
|
match stream.poll() {
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
self.state = ClientState::Connected(stream);
|
|
|
|
Ok(Async::NotReady)
|
|
|
|
},
|
|
|
|
Ok(Async::Ready(None)) => {
|
|
|
|
// EOF
|
|
|
|
self.state = ClientState::Disconnected;
|
|
|
|
Ok(Async::Ready(Some(ClientEvent::Disconnected)))
|
|
|
|
},
|
|
|
|
Ok(Async::Ready(Some(Packet::Stanza(stanza)))) => {
|
|
|
|
self.state = ClientState::Connected(stream);
|
|
|
|
Ok(Async::Ready(Some(ClientEvent::Stanza(stanza))))
|
|
|
|
},
|
|
|
|
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 {
|
|
|
|
type SinkItem = xml::Element;
|
|
|
|
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> {
|
|
|
|
match &mut self.state {
|
|
|
|
&mut ClientState::Connected(ref mut stream) =>
|
|
|
|
stream.poll_complete()
|
|
|
|
.map_err(|e| e.description().to_owned()),
|
|
|
|
_ =>
|
|
|
|
Ok(Async::Ready(())),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|