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/.
|
|
|
|
|
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use crate::iq::{IqResultPayload, IqSetPayload};
|
2018-12-18 14:27:30 +00:00
|
|
|
|
use crate::ns;
|
2019-10-22 23:32:41 +00:00
|
|
|
|
use crate::util::error::Error;
|
2019-09-25 08:28:44 +00:00
|
|
|
|
use crate::Element;
|
2019-10-22 23:32:41 +00:00
|
|
|
|
use jid::{FullJid, Jid};
|
2019-04-12 08:58:42 +00:00
|
|
|
|
use std::convert::TryFrom;
|
2019-10-22 23:32:41 +00:00
|
|
|
|
use std::str::FromStr;
|
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.
|
|
|
|
|
///
|
2022-12-27 15:31:58 +00:00
|
|
|
|
/// See <https://xmpp.org/rfcs/rfc6120.html#bind>
|
2018-02-23 11:38:40 +00:00
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2019-07-31 11:52:08 +00:00
|
|
|
|
pub struct BindQuery {
|
2018-08-08 17:44:55 +00:00
|
|
|
|
/// Requests this resource, the server may associate another one though.
|
2019-07-30 19:25:27 +00:00
|
|
|
|
///
|
|
|
|
|
/// If this is None, we request no particular resource, and a random one
|
|
|
|
|
/// will be affected by the server.
|
|
|
|
|
resource: Option<String>,
|
2018-02-20 16:43:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 11:52:08 +00:00
|
|
|
|
impl BindQuery {
|
2018-08-08 17:44:55 +00:00
|
|
|
|
/// Creates a resource binding request.
|
2019-07-31 11:52:08 +00:00
|
|
|
|
pub fn new(resource: Option<String>) -> BindQuery {
|
|
|
|
|
BindQuery { resource }
|
2018-02-20 16:43:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 11:52:08 +00:00
|
|
|
|
impl IqSetPayload for BindQuery {}
|
2018-05-16 13:08:17 +00:00
|
|
|
|
|
2019-07-31 11:52:08 +00:00
|
|
|
|
impl TryFrom<Element> for BindQuery {
|
2019-04-12 08:58:42 +00:00
|
|
|
|
type Error = Error;
|
2018-02-20 16:43:19 +00:00
|
|
|
|
|
2019-07-31 11:52:08 +00:00
|
|
|
|
fn try_from(elem: Element) -> Result<BindQuery, 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");
|
|
|
|
|
|
2019-07-30 19:25:27 +00:00
|
|
|
|
let mut resource = None;
|
2018-02-20 16:43:19 +00:00
|
|
|
|
for child in elem.children() {
|
2019-07-30 19:25:27 +00:00
|
|
|
|
if resource.is_some() {
|
2018-02-20 16:43:19 +00:00
|
|
|
|
return Err(Error::ParseError("Bind can only have one child."));
|
|
|
|
|
}
|
|
|
|
|
if child.is("resource", ns::BIND) {
|
2018-12-18 15:00:25 +00:00
|
|
|
|
check_no_attributes!(child, "resource");
|
2018-02-20 16:43:19 +00:00
|
|
|
|
check_no_children!(child, "resource");
|
2019-07-30 19:25:27 +00:00
|
|
|
|
resource = Some(child.text());
|
2018-02-20 16:43:19 +00:00
|
|
|
|
} else {
|
2019-07-30 19:25:27 +00:00
|
|
|
|
return Err(Error::ParseError("Unknown element in bind request."));
|
2018-02-20 16:43:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 11:52:08 +00:00
|
|
|
|
Ok(BindQuery { resource })
|
2018-02-20 16:43:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-31 11:52:08 +00:00
|
|
|
|
impl From<BindQuery> for Element {
|
|
|
|
|
fn from(bind: BindQuery) -> Element {
|
2020-03-28 12:07:26 +00:00
|
|
|
|
Element::builder("bind", ns::BIND)
|
2019-10-22 23:32:41 +00:00
|
|
|
|
.append_all(
|
|
|
|
|
bind.resource
|
2020-03-28 12:07:26 +00:00
|
|
|
|
.map(|resource| Element::builder("resource", ns::BIND).append(resource)),
|
2019-10-22 23:32:41 +00:00
|
|
|
|
)
|
2018-12-18 14:32:05 +00:00
|
|
|
|
.build()
|
2018-02-20 16:43:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 19:25:27 +00:00
|
|
|
|
/// The response for resource binding, containing the client’s full JID.
|
|
|
|
|
///
|
2022-12-27 15:31:58 +00:00
|
|
|
|
/// See <https://xmpp.org/rfcs/rfc6120.html#bind>
|
2019-07-30 19:25:27 +00:00
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
|
pub struct BindResponse {
|
|
|
|
|
/// The full JID returned by the server for this client.
|
|
|
|
|
jid: FullJid,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IqResultPayload for BindResponse {}
|
|
|
|
|
|
2019-09-06 09:45:04 +00:00
|
|
|
|
impl From<BindResponse> for FullJid {
|
|
|
|
|
fn from(bind: BindResponse) -> FullJid {
|
|
|
|
|
bind.jid
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<BindResponse> for Jid {
|
|
|
|
|
fn from(bind: BindResponse) -> Jid {
|
|
|
|
|
Jid::Full(bind.jid)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 19:25:27 +00:00
|
|
|
|
impl TryFrom<Element> for BindResponse {
|
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<BindResponse, Error> {
|
|
|
|
|
check_self!(elem, "bind", BIND);
|
|
|
|
|
check_no_attributes!(elem, "bind");
|
|
|
|
|
|
|
|
|
|
let mut jid = None;
|
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if jid.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Bind can only have one child."));
|
|
|
|
|
}
|
|
|
|
|
if child.is("jid", ns::BIND) {
|
|
|
|
|
check_no_attributes!(child, "jid");
|
|
|
|
|
check_no_children!(child, "jid");
|
|
|
|
|
jid = Some(FullJid::from_str(&child.text())?);
|
|
|
|
|
} else {
|
|
|
|
|
return Err(Error::ParseError("Unknown element in bind response."));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-22 23:32:41 +00:00
|
|
|
|
Ok(BindResponse {
|
|
|
|
|
jid: match jid {
|
|
|
|
|
None => {
|
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"Bind response must contain a jid element.",
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
Some(jid) => jid,
|
|
|
|
|
},
|
|
|
|
|
})
|
2019-07-30 19:25:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<BindResponse> for Element {
|
|
|
|
|
fn from(bind: BindResponse) -> Element {
|
2020-03-28 12:07:26 +00:00
|
|
|
|
Element::builder("bind", ns::BIND)
|
|
|
|
|
.append(Element::builder("jid", ns::BIND).append(bind.jid))
|
2019-07-30 19:25:27 +00:00
|
|
|
|
.build()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 16:43:19 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
2019-07-31 11:52:08 +00:00
|
|
|
|
assert_size!(BindQuery, 12);
|
2019-07-30 19:25:27 +00:00
|
|
|
|
assert_size!(BindResponse, 36);
|
2018-10-28 12:10:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
2019-07-31 11:52:08 +00:00
|
|
|
|
assert_size!(BindQuery, 24);
|
2019-07-30 19:25:27 +00:00
|
|
|
|
assert_size!(BindResponse, 72);
|
2018-10-26 12:26:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-20 16:43:19 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2019-07-31 11:52:08 +00:00
|
|
|
|
let bind = BindQuery::try_from(elem).unwrap();
|
2019-07-30 19:25:27 +00:00
|
|
|
|
assert_eq!(bind.resource, None);
|
|
|
|
|
|
2019-10-22 23:32:41 +00:00
|
|
|
|
let elem: Element =
|
|
|
|
|
"<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>Hello™</resource></bind>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2019-07-31 11:52:08 +00:00
|
|
|
|
let bind = BindQuery::try_from(elem).unwrap();
|
2019-07-30 19:25:27 +00:00
|
|
|
|
// FIXME: “™” should be resourceprep’d into “TM” here…
|
|
|
|
|
//assert_eq!(bind.resource.unwrap(), "HelloTM");
|
|
|
|
|
assert_eq!(bind.resource.unwrap(), "Hello™");
|
|
|
|
|
|
2023-06-20 12:07:50 +00:00
|
|
|
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>coucou@linkmauve.fr/Hello™</jid></bind>"
|
2019-07-30 19:25:27 +00:00
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let bind = BindResponse::try_from(elem).unwrap();
|
2023-06-20 12:07:50 +00:00
|
|
|
|
assert_eq!(
|
|
|
|
|
bind.jid,
|
|
|
|
|
FullJid::new("coucou@linkmauve.fr/HelloTM").unwrap()
|
|
|
|
|
);
|
2018-02-20 16:43:19 +00:00
|
|
|
|
}
|
2018-12-18 15:00:25 +00:00
|
|
|
|
|
2019-01-12 21:00:46 +00:00
|
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2018-12-18 15:00:25 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_resource() {
|
|
|
|
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource attr='coucou'>resource</resource></bind>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2019-07-31 11:52:08 +00:00
|
|
|
|
let error = BindQuery::try_from(elem).unwrap_err();
|
2018-12-18 15:00:25 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown attribute in resource element.");
|
|
|
|
|
|
|
|
|
|
let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource><hello-world/>resource</resource></bind>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2019-07-31 11:52:08 +00:00
|
|
|
|
let error = BindQuery::try_from(elem).unwrap_err();
|
2018-12-18 15:00:25 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown child in resource element.");
|
|
|
|
|
}
|
2018-02-20 16:43:19 +00:00
|
|
|
|
}
|