1// Copyright (c) 2021 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::data_forms::DataForm;
8use crate::date::DateTime;
9use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload};
10
11generate_attribute!(
12 /// When sending a push update, the action value indicates if the service is being added or
13 /// deleted from the set of known services (or simply being modified).
14 Action, "action", {
15 /// The service is being added from the set of known services.
16 Add => "add",
17
18 /// The service is being removed from the set of known services.
19 Remove => "remove",
20
21 /// The service is being modified.
22 Modify => "modify",
23 }, Default = Add
24);
25
26generate_attribute!(
27 /// TODO
28 Transport, "transport", {
29 /// TODO
30 Tcp => "tcp",
31
32 /// TODO
33 Udp => "udp",
34 }
35);
36
37generate_attribute!(
38 /// TODO
39 Type, "type", {
40 /// A server that provides Session Traversal Utilities for NAT (STUN).
41 Stun => "stun",
42
43 /// A server that provides Traversal Using Relays around NAT (TURN).
44 Turn => "turn",
45 }
46);
47
48generate_attribute!(
49 /// TODO
50 Restricted,
51 "restricted",
52 bool
53);
54
55generate_element!(
56 /// Structure representing a `<service xmlns='urn:xmpp:extdisco:2'/>` element.
57 Service, "service", EXT_DISCO,
58 attributes: [
59 /// When sending a push update, the action value indicates if the service is being added or
60 /// deleted from the set of known services (or simply being modified).
61 action: Default<Action> = "action",
62
63 /// A timestamp indicating when the provided username and password credentials will expire.
64 expires: Option<DateTime> = "expires",
65
66 /// Either a fully qualified domain name (FQDN) or an IP address (IPv4 or IPv6).
67 host: Required<String> = "host",
68
69 /// A friendly (human-readable) name or label for the service.
70 name: Option<String> = "name",
71
72 /// A service- or server-generated password for use at the service.
73 password: Option<String> = "password",
74
75 /// The communications port to be used at the host.
76 port: Option<u16> = "port",
77
78 /// A boolean value indicating that username and password credentials are required and will
79 /// need to be requested if not already provided.
80 restricted: Default<Restricted> = "restricted",
81
82 /// The underlying transport protocol to be used when communicating with the service (typically
83 /// either TCP or UDP).
84 transport: Option<Transport> = "transport",
85
86 /// The service type as registered with the XMPP Registrar.
87 type_: Required<Type> = "type",
88
89 /// A service- or server-generated username for use at the service.
90 username: Option<String> = "username",
91 ], children: [
92 /// Extended information
93 ext_info: Vec<DataForm> = ("x", DATA_FORMS) => DataForm
94 ]
95);
96
97impl IqGetPayload for Service {}
98
99generate_element!(
100 /// Structure representing a `<services xmlns='urn:xmpp:extdisco:2'/>` element.
101 ServicesQuery, "services", EXT_DISCO,
102 attributes: [
103 /// TODO
104 type_: Option<Type> = "type",
105 ]
106);
107
108impl IqGetPayload for ServicesQuery {}
109
110generate_element!(
111 /// Structure representing a `<services xmlns='urn:xmpp:extdisco:2'/>` element.
112 ServicesResult, "services", EXT_DISCO,
113 attributes: [
114 /// TODO
115 type_: Option<Type> = "type",
116 ],
117 children: [
118 /// List of services.
119 services: Vec<Service> = ("service", EXT_DISCO) => Service
120 ]
121);
122
123impl IqResultPayload for ServicesResult {}
124impl IqSetPayload for ServicesResult {}
125
126generate_element!(
127 /// Structure representing a `<credentials xmlns='urn:xmpp:extdisco:2'/>` element.
128 Credentials, "credentials", EXT_DISCO,
129 children: [
130 /// List of services.
131 services: Vec<Service> = ("service", EXT_DISCO) => Service
132 ]
133);
134
135impl IqGetPayload for Credentials {}
136impl IqResultPayload for Credentials {}
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141 use crate::ns;
142 use crate::Element;
143 use std::convert::TryFrom;
144
145 #[cfg(target_pointer_width = "32")]
146 #[test]
147 fn test_size() {
148 assert_size!(Action, 1);
149 assert_size!(Transport, 1);
150 assert_size!(Restricted, 1);
151 assert_size!(Type, 1);
152 assert_size!(Service, 100); // XXX
153 assert_size!(ServicesQuery, 1);
154 assert_size!(ServicesResult, 16);
155 assert_size!(Credentials, 12);
156 }
157
158 #[cfg(target_pointer_width = "64")]
159 #[test]
160 fn test_size() {
161 assert_size!(Action, 1);
162 assert_size!(Transport, 1);
163 assert_size!(Restricted, 1);
164 assert_size!(Type, 1);
165 assert_size!(Service, 152);
166 assert_size!(ServicesQuery, 1);
167 assert_size!(ServicesResult, 32);
168 assert_size!(Credentials, 24);
169 }
170
171 #[test]
172 fn test_simple() {
173 let elem: Element = "<service xmlns='urn:xmpp:extdisco:2' host='stun.shakespeare.lit' port='9998' transport='udp' type='stun'/>".parse().unwrap();
174 let service = Service::try_from(elem).unwrap();
175 assert_eq!(service.action, Action::Add);
176 assert!(service.expires.is_none());
177 assert_eq!(service.host, "stun.shakespeare.lit");
178 assert!(service.name.is_none());
179 assert!(service.password.is_none());
180 assert_eq!(service.port.unwrap(), 9998);
181 assert_eq!(service.restricted, Restricted::False);
182 assert_eq!(service.transport.unwrap(), Transport::Udp);
183 assert_eq!(service.type_, Type::Stun);
184 assert!(service.username.is_none());
185 assert!(service.ext_info.is_empty());
186 }
187
188 #[test]
189 fn test_service_query() {
190 let query = ServicesQuery { type_: None };
191 let elem = Element::from(query);
192 assert!(elem.is("services", ns::EXT_DISCO));
193 assert_eq!(elem.attrs().next(), None);
194 assert_eq!(elem.nodes().next(), None);
195 }
196
197 #[test]
198 fn test_service_result() {
199 let elem: Element = "<services xmlns='urn:xmpp:extdisco:2' type='stun'><service host='stun.shakespeare.lit' port='9998' transport='udp' type='stun'/></services>".parse().unwrap();
200 let services = ServicesResult::try_from(elem).unwrap();
201 assert_eq!(services.type_, Some(Type::Stun));
202 assert_eq!(services.services.len(), 1);
203 }
204}