2018-02-20 16:43:19 +00:00
|
|
|
// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
use try_from::TryFrom;
|
|
|
|
|
|
|
|
use minidom::Element;
|
|
|
|
|
2018-12-18 14:27:30 +00:00
|
|
|
use crate::error::Error;
|
2018-02-20 16:43:19 +00:00
|
|
|
use jid::Jid;
|
2018-12-18 14:27:30 +00:00
|
|
|
use crate::ns;
|
|
|
|
use crate::iq::{IqSetPayload, IqResultPayload};
|
2018-02-20 16:43:19 +00:00
|
|
|
|
2018-08-08 17:44:55 +00:00
|
|
|
/// The request for resource binding, which is the process by which a client
|
|
|
|
/// can obtain a full JID and start exchanging on the XMPP network.
|
|
|
|
///
|
|
|
|
/// See https://xmpp.org/rfcs/rfc6120.html#bind
|
2018-02-23 11:38:40 +00:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum Bind {
|
2018-08-08 17:44:55 +00:00
|
|
|
/// Requests no particular resource, a random one will be affected by the
|
|
|
|
/// server.
|
2018-02-23 11:38:40 +00:00
|
|
|
None,
|
2018-08-08 17:44:55 +00:00
|
|
|
|
|
|
|
/// Requests this resource, the server may associate another one though.
|
2018-02-23 11:38:40 +00:00
|
|
|
Resource(String),
|
2018-08-08 17:44:55 +00:00
|
|
|
|
|
|
|
/// The full JID returned by the server for this client.
|
2018-02-23 11:38:40 +00:00
|
|
|
Jid(Jid),
|
2018-02-20 16:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Bind {
|
2018-08-08 17:44:55 +00:00
|
|
|
/// Creates a resource binding request.
|
2018-02-20 16:43:19 +00:00
|
|
|
pub fn new(resource: Option<String>) -> Bind {
|
2018-02-23 11:38:40 +00:00
|
|
|
match resource {
|
|
|
|
None => Bind::None,
|
|
|
|
Some(resource) => Bind::Resource(resource),
|
2018-02-20 16:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
impl IqSetPayload for Bind {}
|
|
|
|
impl IqResultPayload for Bind {}
|
|
|
|
|
2018-02-20 16:43:19 +00:00
|
|
|
impl TryFrom<Element> for Bind {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<Bind, Error> {
|
2018-05-14 14:30:28 +00:00
|
|
|
check_self!(elem, "bind", BIND);
|
2018-02-20 16:43:19 +00:00
|
|
|
check_no_attributes!(elem, "bind");
|
|
|
|
|
2018-02-23 11:38:40 +00:00
|
|
|
let mut bind = Bind::None;
|
2018-02-20 16:43:19 +00:00
|
|
|
for child in elem.children() {
|
2018-02-23 11:38:40 +00:00
|
|
|
if bind != Bind::None {
|
2018-02-20 16:43:19 +00:00
|
|
|
return Err(Error::ParseError("Bind can only have one child."));
|
|
|
|
}
|
|
|
|
if child.is("resource", ns::BIND) {
|
|
|
|
check_no_children!(child, "resource");
|
2018-02-23 11:38:40 +00:00
|
|
|
bind = Bind::Resource(child.text());
|
2018-02-20 16:43:19 +00:00
|
|
|
} else if child.is("jid", ns::BIND) {
|
|
|
|
check_no_children!(child, "jid");
|
2018-02-23 11:38:40 +00:00
|
|
|
bind = Bind::Jid(Jid::from_str(&child.text())?);
|
2018-02-20 16:43:19 +00:00
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Unknown element in bind."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(bind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Bind> for Element {
|
|
|
|
fn from(bind: Bind) -> Element {
|
|
|
|
Element::builder("bind")
|
|
|
|
.ns(ns::BIND)
|
2018-02-23 11:38:40 +00:00
|
|
|
.append(match bind {
|
|
|
|
Bind::None => vec!(),
|
|
|
|
Bind::Resource(resource) => vec!(
|
|
|
|
Element::builder("resource")
|
|
|
|
.ns(ns::BIND)
|
|
|
|
.append(resource)
|
|
|
|
.build()
|
|
|
|
),
|
|
|
|
Bind::Jid(jid) => vec!(
|
|
|
|
Element::builder("jid")
|
|
|
|
.ns(ns::BIND)
|
|
|
|
.append(jid)
|
|
|
|
.build()
|
|
|
|
),
|
|
|
|
})
|
2018-02-20 16:43:19 +00:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(Bind, 40);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
#[test]
|
|
|
|
fn test_size() {
|
|
|
|
assert_size!(Bind, 80);
|
|
|
|
}
|
|
|
|
|
2018-02-20 16:43:19 +00:00
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>".parse().unwrap();
|
|
|
|
let bind = Bind::try_from(elem).unwrap();
|
2018-02-23 11:38:40 +00:00
|
|
|
assert_eq!(bind, Bind::None);
|
2018-02-20 16:43:19 +00:00
|
|
|
}
|
|
|
|
}
|