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, Jid};
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_all((match bind.resource {
67 None => vec![],
68 Some(resource) => vec![Element::builder("resource")
69 .ns(ns::BIND)
70 .append(resource)
71 .build()],
72 }).into_iter())
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 From<BindResponse> for FullJid {
89 fn from(bind: BindResponse) -> FullJid {
90 bind.jid
91 }
92}
93
94impl From<BindResponse> for Jid {
95 fn from(bind: BindResponse) -> Jid {
96 Jid::Full(bind.jid)
97 }
98}
99
100impl TryFrom<Element> for BindResponse {
101 type Error = Error;
102
103 fn try_from(elem: Element) -> Result<BindResponse, Error> {
104 check_self!(elem, "bind", BIND);
105 check_no_attributes!(elem, "bind");
106
107 let mut jid = None;
108 for child in elem.children() {
109 if jid.is_some() {
110 return Err(Error::ParseError("Bind can only have one child."));
111 }
112 if child.is("jid", ns::BIND) {
113 check_no_attributes!(child, "jid");
114 check_no_children!(child, "jid");
115 jid = Some(FullJid::from_str(&child.text())?);
116 } else {
117 return Err(Error::ParseError("Unknown element in bind response."));
118 }
119 }
120
121 Ok(BindResponse { jid: match jid {
122 None => return Err(Error::ParseError("Bind response must contain a jid element.")),
123 Some(jid) => jid,
124 } })
125 }
126}
127
128impl From<BindResponse> for Element {
129 fn from(bind: BindResponse) -> Element {
130 Element::builder("bind")
131 .ns(ns::BIND)
132 .append(Element::builder("jid").ns(ns::BIND).append(bind.jid).build())
133 .build()
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use super::*;
140
141 #[cfg(target_pointer_width = "32")]
142 #[test]
143 fn test_size() {
144 assert_size!(BindQuery, 12);
145 assert_size!(BindResponse, 36);
146 }
147
148 #[cfg(target_pointer_width = "64")]
149 #[test]
150 fn test_size() {
151 assert_size!(BindQuery, 24);
152 assert_size!(BindResponse, 72);
153 }
154
155 #[test]
156 fn test_simple() {
157 let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/>"
158 .parse()
159 .unwrap();
160 let bind = BindQuery::try_from(elem).unwrap();
161 assert_eq!(bind.resource, None);
162
163 let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource>Hello™</resource></bind>"
164 .parse()
165 .unwrap();
166 let bind = BindQuery::try_from(elem).unwrap();
167 // FIXME: “™” should be resourceprep’d into “TM” here…
168 //assert_eq!(bind.resource.unwrap(), "HelloTM");
169 assert_eq!(bind.resource.unwrap(), "Hello™");
170
171 let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>coucou@linkmauve.fr/HelloTM</jid></bind>"
172 .parse()
173 .unwrap();
174 let bind = BindResponse::try_from(elem).unwrap();
175 assert_eq!(bind.jid, FullJid::new("coucou", "linkmauve.fr", "HelloTM"));
176 }
177
178 #[cfg(not(feature = "disable-validation"))]
179 #[test]
180 fn test_invalid_resource() {
181 let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource attr='coucou'>resource</resource></bind>"
182 .parse()
183 .unwrap();
184 let error = BindQuery::try_from(elem).unwrap_err();
185 let message = match error {
186 Error::ParseError(string) => string,
187 _ => panic!(),
188 };
189 assert_eq!(message, "Unknown attribute in resource element.");
190
191 let elem: Element = "<bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><resource><hello-world/>resource</resource></bind>"
192 .parse()
193 .unwrap();
194 let error = BindQuery::try_from(elem).unwrap_err();
195 let message = match error {
196 Error::ParseError(string) => string,
197 _ => panic!(),
198 };
199 assert_eq!(message, "Unknown child in resource element.");
200 }
201}