1// Copyright (c) 2017 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::jingle::SessionId;
9use crate::ns;
10use minidom::Element;
11use std::convert::TryFrom;
12
13/// Defines a protocol for broadcasting Jingle requests to all of the clients
14/// of a user.
15#[derive(Debug, Clone)]
16pub enum JingleMI {
17 /// Indicates we want to start a Jingle session.
18 Propose {
19 /// The generated session identifier, must be unique between two users.
20 sid: SessionId,
21
22 /// The application description of the proposed session.
23 // TODO: Use a more specialised type here.
24 description: Element,
25 },
26
27 /// Cancels a previously proposed session.
28 Retract(SessionId),
29
30 /// Accepts a session proposed by the other party.
31 Accept(SessionId),
32
33 /// Proceed with a previously proposed session.
34 Proceed(SessionId),
35
36 /// Rejects a session proposed by the other party.
37 Reject(SessionId),
38}
39
40fn get_sid(elem: Element) -> Result<SessionId, Error> {
41 check_no_unknown_attributes!(elem, "Jingle message", ["id"]);
42 Ok(SessionId(get_attr!(elem, "id", Required)))
43}
44
45fn check_empty_and_get_sid(elem: Element) -> Result<SessionId, Error> {
46 check_no_children!(elem, "Jingle message");
47 get_sid(elem)
48}
49
50impl TryFrom<Element> for JingleMI {
51 type Error = Error;
52
53 fn try_from(elem: Element) -> Result<JingleMI, Error> {
54 if !elem.has_ns(ns::JINGLE_MESSAGE) {
55 return Err(Error::ParseError("This is not a Jingle message element."));
56 }
57 Ok(match elem.name() {
58 "propose" => {
59 let mut description = None;
60 for child in elem.children() {
61 if child.name() != "description" {
62 return Err(Error::ParseError("Unknown child in propose element."));
63 }
64 if description.is_some() {
65 return Err(Error::ParseError("Too many children in propose element."));
66 }
67 description = Some(child.clone());
68 }
69 JingleMI::Propose {
70 sid: get_sid(elem)?,
71 description: description.ok_or(Error::ParseError(
72 "Propose element doesn’t contain a description.",
73 ))?,
74 }
75 }
76 "retract" => JingleMI::Retract(check_empty_and_get_sid(elem)?),
77 "accept" => JingleMI::Accept(check_empty_and_get_sid(elem)?),
78 "proceed" => JingleMI::Proceed(check_empty_and_get_sid(elem)?),
79 "reject" => JingleMI::Reject(check_empty_and_get_sid(elem)?),
80 _ => return Err(Error::ParseError("This is not a Jingle message element.")),
81 })
82 }
83}
84
85impl From<JingleMI> for Element {
86 fn from(jingle_mi: JingleMI) -> Element {
87 match jingle_mi {
88 JingleMI::Propose { sid, description } => Element::builder("propose")
89 .ns(ns::JINGLE_MESSAGE)
90 .attr("id", sid)
91 .append(description),
92 JingleMI::Retract(sid) => Element::builder("retract")
93 .ns(ns::JINGLE_MESSAGE)
94 .attr("id", sid),
95 JingleMI::Accept(sid) => Element::builder("accept")
96 .ns(ns::JINGLE_MESSAGE)
97 .attr("id", sid),
98 JingleMI::Proceed(sid) => Element::builder("proceed")
99 .ns(ns::JINGLE_MESSAGE)
100 .attr("id", sid),
101 JingleMI::Reject(sid) => Element::builder("reject")
102 .ns(ns::JINGLE_MESSAGE)
103 .attr("id", sid),
104 }
105 .build()
106 }
107}
108
109#[cfg(test)]
110mod tests {
111 use super::*;
112
113 #[cfg(target_pointer_width = "32")]
114 #[test]
115 fn test_size() {
116 assert_size!(JingleMI, 68);
117 }
118
119 #[cfg(target_pointer_width = "64")]
120 #[test]
121 fn test_size() {
122 assert_size!(JingleMI, 136);
123 }
124
125 #[test]
126 fn test_simple() {
127 let elem: Element = "<accept xmlns='urn:xmpp:jingle-message:0' id='coucou'/>"
128 .parse()
129 .unwrap();
130 JingleMI::try_from(elem).unwrap();
131 }
132
133 #[test]
134 fn test_invalid_child() {
135 let elem: Element =
136 "<propose xmlns='urn:xmpp:jingle-message:0' id='coucou'><coucou/></propose>"
137 .parse()
138 .unwrap();
139 let error = JingleMI::try_from(elem).unwrap_err();
140 let message = match error {
141 Error::ParseError(string) => string,
142 _ => panic!(),
143 };
144 assert_eq!(message, "Unknown child in propose element.");
145 }
146}