xmpp-rs/src/client.rs

283 lines
9.9 KiB
Rust
Raw Normal View History

2017-02-18 22:16:20 +01:00
use jid::Jid;
2017-02-19 15:25:18 +01:00
use transport::{Transport, SslTransport};
2017-02-19 00:01:39 +01:00
use error::Error;
2017-02-19 15:25:18 +01:00
use ns;
2017-02-20 16:28:51 +01:00
use plugin::{Plugin, PluginProxyBinding};
use event::AbstractEvent;
use connection::{Connection, C2S};
use sasl::{ Mechanism as SaslMechanism
, Credentials as SaslCredentials
, Secret as SaslSecret
, ChannelBinding
};
use sasl::mechanisms::{Plain, Scram, Sha1, Sha256};
use components::sasl_error::SaslError;
use util::FromElement;
2017-02-20 16:28:51 +01:00
use base64;
use minidom::Element;
2017-02-19 15:25:18 +01:00
use xml::reader::XmlEvent as ReaderEvent;
2017-02-18 22:16:20 +01:00
2017-02-20 16:28:51 +01:00
use std::sync::mpsc::{Receiver, channel};
use std::collections::HashSet;
2017-02-24 17:44:11 +01:00
/// Struct that should be moved somewhere else and cleaned up.
2017-02-24 23:42:08 +01:00
#[derive(Debug)]
2017-02-24 17:44:11 +01:00
pub struct StreamFeatures {
pub sasl_mechanisms: Option<HashSet<String>>,
2017-02-24 17:44:11 +01:00
}
2017-02-21 13:55:20 +01:00
/// A builder for `Client`s.
2017-02-19 00:01:39 +01:00
pub struct ClientBuilder {
jid: Jid,
credentials: SaslCredentials,
2017-02-19 00:01:39 +01:00
host: Option<String>,
port: u16,
2017-02-18 22:16:20 +01:00
}
2017-02-19 00:01:39 +01:00
impl ClientBuilder {
/// Creates a new builder for an XMPP client that will connect to `jid` with default parameters.
2017-02-19 00:01:39 +01:00
pub fn new(jid: Jid) -> ClientBuilder {
ClientBuilder {
jid: jid,
credentials: SaslCredentials::default(),
2017-02-19 00:01:39 +01:00
host: None,
port: 5222,
}
}
/// Sets the host to connect to.
2017-02-19 00:01:39 +01:00
pub fn host(mut self, host: String) -> ClientBuilder {
self.host = Some(host);
self
}
/// Sets the port to connect to.
2017-02-19 00:01:39 +01:00
pub fn port(mut self, port: u16) -> ClientBuilder {
self.port = port;
self
}
/// Sets the password to use.
pub fn password<P: Into<String>>(mut self, password: P) -> ClientBuilder {
self.credentials = SaslCredentials {
username: Some(self.jid.node.clone().expect("JID has no node")),
secret: SaslSecret::Password(password.into()),
channel_binding: ChannelBinding::None,
};
self
}
2017-02-21 13:55:20 +01:00
/// Connects to the server and returns a `Client` when succesful.
2017-02-19 00:01:39 +01:00
pub fn connect(self) -> Result<Client, Error> {
let host = &self.host.unwrap_or(self.jid.domain.clone());
2017-02-19 15:25:18 +01:00
let mut transport = SslTransport::connect(host, self.port)?;
C2S::init(&mut transport, &self.jid.domain, "before_sasl")?;
2017-02-20 16:28:51 +01:00
let (sender_out, sender_in) = channel();
let (dispatcher_out, dispatcher_in) = channel();
let mut credentials = self.credentials;
credentials.channel_binding = transport.channel_bind();
let mut client = Client {
2017-02-19 00:01:39 +01:00
jid: self.jid,
2017-02-20 16:28:51 +01:00
transport: transport,
plugins: Vec::new(),
binding: PluginProxyBinding::new(sender_out, dispatcher_out),
sender_in: sender_in,
dispatcher_in: dispatcher_in,
};
client.connect(credentials)?;
client.bind()?;
Ok(client)
2017-02-18 22:16:20 +01:00
}
}
2017-02-19 00:01:39 +01:00
2017-02-21 13:58:24 +01:00
/// An XMPP client.
2017-02-19 00:01:39 +01:00
pub struct Client {
jid: Jid,
transport: SslTransport,
2017-02-20 16:28:51 +01:00
plugins: Vec<Box<Plugin>>,
binding: PluginProxyBinding,
sender_in: Receiver<Element>,
dispatcher_in: Receiver<AbstractEvent>,
2017-02-19 00:01:39 +01:00
}
impl Client {
/// Returns a reference to the `Jid` associated with this `Client`.
2017-02-19 15:06:14 +01:00
pub fn jid(&self) -> &Jid {
&self.jid
}
2017-02-20 16:28:51 +01:00
/// Registers a plugin.
2017-02-20 16:28:51 +01:00
pub fn register_plugin<P: Plugin + 'static>(&mut self, mut plugin: P) {
plugin.bind(self.binding.clone());
self.plugins.push(Box::new(plugin));
}
/// Returns the plugin given by the type parameter, if it exists, else panics.
2017-02-20 16:28:51 +01:00
pub fn plugin<P: Plugin>(&self) -> &P {
for plugin in &self.plugins {
let any = plugin.as_any();
if let Some(ret) = any.downcast_ref::<P>() {
return ret;
}
}
panic!("plugin does not exist!");
}
/// Returns the next event and flush the send queue.
2017-02-20 16:28:51 +01:00
pub fn next_event(&mut self) -> Result<AbstractEvent, Error> {
self.flush_send_queue()?;
loop {
if let Ok(evt) = self.dispatcher_in.try_recv() {
return Ok(evt);
}
let elem = self.transport.read_element()?;
for plugin in self.plugins.iter_mut() {
plugin.handle(&elem);
// TODO: handle plugin return
}
self.flush_send_queue()?;
}
}
/// Flushes the send queue, sending all queued up stanzas.
2017-02-20 16:28:51 +01:00
pub fn flush_send_queue(&mut self) -> Result<(), Error> { // TODO: not sure how great of an
// idea it is to flush in this
// manner…
while let Ok(elem) = self.sender_in.try_recv() {
self.transport.write_element(&elem)?;
}
Ok(())
}
fn connect(&mut self, mut credentials: SaslCredentials) -> Result<(), Error> {
let features = self.wait_for_features()?;
let ms = &features.sasl_mechanisms.ok_or(Error::SaslError(Some("no SASL mechanisms".to_owned())))?;
fn wrap_err(err: String) -> Error { Error::SaslError(Some(err)) }
// TODO: better way for selecting these, enabling anonymous auth
let mut mechanism: Box<SaslMechanism> = if ms.contains("SCRAM-SHA-256-PLUS") {
Box::new(Scram::<Sha256>::from_credentials(credentials).map_err(wrap_err)?)
}
else if ms.contains("SCRAM-SHA-1-PLUS") {
Box::new(Scram::<Sha1>::from_credentials(credentials).map_err(wrap_err)?)
}
else if ms.contains("SCRAM-SHA-256") {
credentials.channel_binding = ChannelBinding::Unsupported;
Box::new(Scram::<Sha256>::from_credentials(credentials).map_err(wrap_err)?)
}
else if ms.contains("SCRAM-SHA-1") {
credentials.channel_binding = ChannelBinding::Unsupported;
Box::new(Scram::<Sha1>::from_credentials(credentials).map_err(wrap_err)?)
}
else if ms.contains("PLAIN") {
Box::new(Plain::from_credentials(credentials).map_err(wrap_err)?)
}
else {
return Err(Error::SaslError(Some("can't find a SASL mechanism to use".to_owned())));
};
2017-02-24 23:42:08 +01:00
let auth = mechanism.initial().map_err(|x| Error::SaslError(Some(x)))?;
let mut elem = Element::builder("auth")
.ns(ns::SASL)
.attr("mechanism", mechanism.name())
.build();
if !auth.is_empty() {
elem.append_text_node(base64::encode(&auth));
2017-02-20 16:28:51 +01:00
}
self.transport.write_element(&elem)?;
2017-02-20 16:28:51 +01:00
loop {
let n = self.transport.read_element()?;
if n.is("challenge", ns::SASL) {
2017-02-24 17:16:51 +01:00
let text = n.text();
let challenge = if text == "" {
Vec::new()
}
else {
base64::decode(&text)?
};
2017-02-24 23:42:08 +01:00
let response = mechanism.response(&challenge).map_err(|x| Error::SaslError(Some(x)))?;
2017-02-24 17:16:51 +01:00
let mut elem = Element::builder("response")
.ns(ns::SASL)
.build();
2017-02-24 17:16:51 +01:00
if !response.is_empty() {
elem.append_text_node(base64::encode(&response));
2017-02-20 16:28:51 +01:00
}
2017-02-24 17:16:51 +01:00
self.transport.write_element(&elem)?;
2017-02-20 16:28:51 +01:00
}
else if n.is("success", ns::SASL) {
2017-02-24 23:42:08 +01:00
let text = n.text();
let data = if text == "" {
Vec::new()
}
else {
base64::decode(&text)?
};
mechanism.success(&data).map_err(|x| Error::SaslError(Some(x)))?;
2017-02-20 16:28:51 +01:00
self.transport.reset_stream();
C2S::init(&mut self.transport, &self.jid.domain, "after_sasl")?;
2017-02-24 23:53:54 +01:00
self.wait_for_features()?;
return Ok(());
2017-02-20 16:28:51 +01:00
}
2017-02-24 17:20:00 +01:00
else if n.is("failure", ns::SASL) {
let inner = SaslError::from_element(&n).map_err(|_| Error::SaslError(None))?;
return Err(Error::XmppSaslError(inner));
2017-02-24 17:20:00 +01:00
}
}
}
fn bind(&mut self) -> Result<(), Error> {
let mut elem = Element::builder("iq")
.attr("id", "bind")
.attr("type", "set")
.build();
2017-02-24 23:53:54 +01:00
let mut bind = Element::builder("bind")
.ns(ns::BIND)
.build();
if let Some(ref resource) = self.jid.resource {
let res = Element::builder("resource")
.ns(ns::BIND)
.text(resource.to_owned())
.build();
bind.append_child(res);
}
elem.append_child(bind);
self.transport.write_element(&elem)?;
loop {
let n = self.transport.read_element()?;
if n.is("iq", ns::CLIENT) && n.has_child("bind", ns::BIND) {
2017-02-20 16:28:51 +01:00
return Ok(());
}
}
}
2017-02-24 17:44:11 +01:00
fn wait_for_features(&mut self) -> Result<StreamFeatures, Error> {
// TODO: this is very ugly
loop {
let e = self.transport.read_event()?;
match e {
ReaderEvent::StartElement { .. } => {
break;
},
_ => (),
}
}
loop {
let n = self.transport.read_element()?;
if n.is("features", ns::STREAM) {
2017-02-24 17:44:11 +01:00
let mut features = StreamFeatures {
sasl_mechanisms: None,
};
if let Some(ms) = n.get_child("mechanisms", ns::SASL) {
let mut res = HashSet::new();
2017-02-24 17:44:11 +01:00
for cld in ms.children() {
res.insert(cld.text());
2017-02-24 17:44:11 +01:00
}
features.sasl_mechanisms = Some(res);
}
return Ok(features);
}
}
}
2017-02-19 00:01:39 +01:00
}