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::jingle::SessionId;
8use crate::ns;
9use crate::util::error::Error;
10use crate::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 } => {
89 Element::builder("propose", ns::JINGLE_MESSAGE)
90 .attr("id", sid)
91 .append(description)
92 }
93 JingleMI::Retract(sid) => {
94 Element::builder("retract", ns::JINGLE_MESSAGE).attr("id", sid)
95 }
96 JingleMI::Accept(sid) => Element::builder("accept", ns::JINGLE_MESSAGE).attr("id", sid),
97 JingleMI::Proceed(sid) => {
98 Element::builder("proceed", ns::JINGLE_MESSAGE).attr("id", sid)
99 }
100 JingleMI::Reject(sid) => Element::builder("reject", ns::JINGLE_MESSAGE).attr("id", sid),
101 }
102 .build()
103 }
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 #[cfg(target_pointer_width = "32")]
111 #[test]
112 fn test_size() {
113 assert_size!(JingleMI, 76);
114 }
115
116 #[cfg(target_pointer_width = "64")]
117 #[test]
118 fn test_size() {
119 assert_size!(JingleMI, 152);
120 }
121
122 #[test]
123 fn test_simple() {
124 let elem: Element = "<accept xmlns='urn:xmpp:jingle-message:0' id='coucou'/>"
125 .parse()
126 .unwrap();
127 JingleMI::try_from(elem).unwrap();
128 }
129
130 #[test]
131 fn test_invalid_child() {
132 let elem: Element =
133 "<propose xmlns='urn:xmpp:jingle-message:0' id='coucou'><coucou/></propose>"
134 .parse()
135 .unwrap();
136 let error = JingleMI::try_from(elem).unwrap_err();
137 let message = match error {
138 Error::ParseError(string) => string,
139 _ => panic!(),
140 };
141 assert_eq!(message, "Unknown child in propose element.");
142 }
143}