1// Copyright (c) 2018 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use crate::util::error::Error;
8use crate::iq::{IqResultPayload, IqSetPayload};
9use crate::ns;
10use jid::FullJid;
11use minidom::Element;
12use std::str::FromStr;
13use std::convert::TryFrom;
14
15/// The request for resource binding, which is the process by which a client
16/// can obtain a full JID and start exchanging on the XMPP network.
17///
18/// See https://xmpp.org/rfcs/rfc6120.html#bind
19#[derive(Debug, Clone, PartialEq)]
20pub struct BindQuery {
21 /// Requests this resource, the server may associate another one though.
22 ///
23 /// If this is None, we request no particular resource, and a random one
24 /// will be affected by the server.
25 resource: Option<String>,
26}
27
28impl BindQuery {
29 /// Creates a resource binding request.
30 pub fn new(resource: Option<String>) -> BindQuery {
31 BindQuery { resource }
32 }
33}
34
35impl IqSetPayload for BindQuery {}
36
37impl TryFrom<Element> for BindQuery {
38 type Error = Error;
39
40 fn try_from(elem: Element) -> Result<BindQuery, Error> {
41 check_self!(elem, "bind", BIND);
42 check_no_attributes!(elem, "bind");
43
44 let mut resource = None;
45 for child in elem.children() {
46 if resource.is_some() {
47 return Err(Error::ParseError("Bind can only have one child."));
48 }
49 if child.is("resource", ns::BIND) {
50 check_no_attributes!(child, "resource");
51 check_no_children!(child, "resource");
52 resource = Some(child.text());
53 } else {
54 return Err(Error::ParseError("Unknown element in bind request."));
55 }
56 }
57
58 Ok(BindQuery { resource })
59 }
60}
61
62impl From<BindQuery> for Element {
63 fn from(bind: BindQuery) -> Element {
64 Element::builder("bind")
65 .ns(ns::BIND)
66 .append(match bind.resource {
67 None => vec![],
68 Some(resource) => vec![Element::builder("resource")
69 .ns(ns::BIND)
70 .append(resource)
71 .build()],
72 })
73 .build()
74 }
75}
76
77/// The response for resource binding, containing the client’s full JID.
78///
79/// See https://xmpp.org/rfcs/rfc6120.html#bind
80#[derive(Debug, Clone, PartialEq)]
81pub struct BindResponse {
82 /// The full JID returned by the server for this client.
83 jid: FullJid,
84}
85
86impl IqResultPayload for BindResponse {}
87
88impl TryFrom<Element> for BindResponse {
89 type Error = Error;
90
91 fn try_from(elem: Element) -> Result<BindResponse, Error> {
92 check_self!(elem, "bind", BIND);
93 check_no_attributes!(elem, "bind");
94
95 let mut jid = None;
96 for child in elem.children() {
97 if jid.is_some() {
98 return Err(Error::ParseError("Bind can only have one child."));
99 }
100 if child.is("jid", ns::BIND) {
101 check_no_attributes!(child, "jid");
102 check_no_children!(child, "jid");
103 jid = Some(FullJid::from_str(&child.text())?);
104 } else {
105 return Err(Error::ParseError("Unknown element in bind response."));
106 }
107 }
108
109 Ok(BindResponse { jid: match jid {
110 None => return Err(Error::ParseError("Bind response must contain a jid element.")),
111 Some(jid) => jid,
112 } })
113 }
114}
115
116impl From<BindResponse> for Element {
117 fn from(bind: BindResponse) -> Element {
118 Element::builder("bind")
119 .ns(ns::BIND)
120 .append(Element::builder("jid").ns(ns::BIND).append(bind.jid).build())
121 .build()
122 }
123}
124
125#[cfg(test)]
126mod tests {
127 use super::*;
128
129 #[cfg(target_pointer_width = "32")]
130 #[test]
131 fn test_size() {
132 assert_size!(BindQuery, 12);
133 assert_size!(BindResponse, 36);
134 }
135
136 #[cfg(target_pointer_width = "64")]
137 #[test]
138 fn test_size() {
139 assert_size!(BindQuery, 24);
140 assert_size!(BindResponse, 72);
141 }
142
143 #[test]
144 fn test_simple() {
145 let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>"
146 .parse()
147 .unwrap();
148 let bind = BindQuery::try_from(elem).unwrap();
149 assert_eq!(bind.resource, None);
150
151 let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>Hello™</resource></bind>"
152 .parse()
153 .unwrap();
154 let bind = BindQuery::try_from(elem).unwrap();
155 // FIXME: “™” should be resourceprep’d into “TM” here…
156 //assert_eq!(bind.resource.unwrap(), "HelloTM");
157 assert_eq!(bind.resource.unwrap(), "Hello™");
158
159 let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>coucou@linkmauve.fr/HelloTM</jid></bind>"
160 .parse()
161 .unwrap();
162 let bind = BindResponse::try_from(elem).unwrap();
163 assert_eq!(bind.jid, FullJid::new("coucou", "linkmauve.fr", "HelloTM"));
164 }
165
166 #[cfg(not(feature = "disable-validation"))]
167 #[test]
168 fn test_invalid_resource() {
169 let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource attr='coucou'>resource</resource></bind>"
170 .parse()
171 .unwrap();
172 let error = BindQuery::try_from(elem).unwrap_err();
173 let message = match error {
174 Error::ParseError(string) => string,
175 _ => panic!(),
176 };
177 assert_eq!(message, "Unknown attribute in resource element.");
178
179 let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource><hello-world/>resource</resource></bind>"
180 .parse()
181 .unwrap();
182 let error = BindQuery::try_from(elem).unwrap_err();
183 let message = match error {
184 Error::ParseError(string) => string,
185 _ => panic!(),
186 };
187 assert_eq!(message, "Unknown child in resource element.");
188 }
189}