2017-07-15 10:38:44 +00:00
|
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
|
|
|
|
//
|
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
2019-01-13 11:39:51 +00:00
|
|
|
|
use crate::util::error::Error;
|
2018-12-18 14:27:30 +00:00
|
|
|
|
use crate::jingle::SessionId;
|
|
|
|
|
use crate::ns;
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use minidom::Element;
|
|
|
|
|
use try_from::TryFrom;
|
2017-07-15 10:38:44 +00:00
|
|
|
|
|
2018-08-08 18:26:14 +00:00
|
|
|
|
/// Defines a protocol for broadcasting Jingle requests to all of the clients
|
|
|
|
|
/// of a user.
|
2017-07-15 10:38:44 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub enum JingleMI {
|
2018-08-08 18:26:14 +00:00
|
|
|
|
/// Indicates we want to start a Jingle session.
|
2017-07-15 10:38:44 +00:00
|
|
|
|
Propose {
|
2018-08-08 18:26:14 +00:00
|
|
|
|
/// The generated session identifier, must be unique between two users.
|
2017-07-15 10:38:44 +00:00
|
|
|
|
sid: SessionId,
|
2018-08-08 18:26:14 +00:00
|
|
|
|
|
|
|
|
|
/// The application description of the proposed session.
|
2017-07-15 10:38:44 +00:00
|
|
|
|
// TODO: Use a more specialised type here.
|
|
|
|
|
description: Element,
|
|
|
|
|
},
|
2018-08-08 18:26:14 +00:00
|
|
|
|
|
|
|
|
|
/// Cancels a previously proposed session.
|
2017-07-15 10:38:44 +00:00
|
|
|
|
Retract(SessionId),
|
2018-08-08 18:26:14 +00:00
|
|
|
|
|
|
|
|
|
/// Accepts a session proposed by the other party.
|
2017-07-15 10:38:44 +00:00
|
|
|
|
Accept(SessionId),
|
2018-08-08 18:26:14 +00:00
|
|
|
|
|
|
|
|
|
/// Proceed with a previously proposed session.
|
2017-07-15 10:38:44 +00:00
|
|
|
|
Proceed(SessionId),
|
2018-08-08 18:26:14 +00:00
|
|
|
|
|
|
|
|
|
/// Rejects a session proposed by the other party.
|
2017-07-15 10:38:44 +00:00
|
|
|
|
Reject(SessionId),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_sid(elem: Element) -> Result<SessionId, Error> {
|
2018-05-12 15:59:04 +00:00
|
|
|
|
check_no_unknown_attributes!(elem, "Jingle message", ["id"]);
|
2017-07-15 10:38:44 +00:00
|
|
|
|
Ok(SessionId(get_attr!(elem, "id", required)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn check_empty_and_get_sid(elem: Element) -> Result<SessionId, Error> {
|
2018-05-14 14:33:47 +00:00
|
|
|
|
check_no_children!(elem, "Jingle message");
|
2017-07-15 10:38:44 +00:00
|
|
|
|
get_sid(elem)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<Element> for JingleMI {
|
2017-07-20 19:03:15 +00:00
|
|
|
|
type Err = Error;
|
2017-07-15 10:38:44 +00:00
|
|
|
|
|
|
|
|
|
fn try_from(elem: Element) -> Result<JingleMI, Error> {
|
2017-08-27 00:04:56 +00:00
|
|
|
|
if !elem.has_ns(ns::JINGLE_MESSAGE) {
|
2017-07-15 10:38:44 +00:00
|
|
|
|
return Err(Error::ParseError("This is not a Jingle message element."));
|
|
|
|
|
}
|
|
|
|
|
Ok(match elem.name() {
|
|
|
|
|
"propose" => {
|
|
|
|
|
let mut description = None;
|
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if child.name() != "description" {
|
|
|
|
|
return Err(Error::ParseError("Unknown child in propose element."));
|
|
|
|
|
}
|
|
|
|
|
if description.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Too many children in propose element."));
|
|
|
|
|
}
|
|
|
|
|
description = Some(child.clone());
|
|
|
|
|
}
|
|
|
|
|
JingleMI::Propose {
|
|
|
|
|
sid: get_sid(elem)?,
|
2018-12-18 14:32:05 +00:00
|
|
|
|
description: description.ok_or(Error::ParseError(
|
|
|
|
|
"Propose element doesn’t contain a description.",
|
|
|
|
|
))?,
|
2017-07-15 10:38:44 +00:00
|
|
|
|
}
|
2018-12-18 14:32:05 +00:00
|
|
|
|
}
|
2017-07-15 10:38:44 +00:00
|
|
|
|
"retract" => JingleMI::Retract(check_empty_and_get_sid(elem)?),
|
|
|
|
|
"accept" => JingleMI::Accept(check_empty_and_get_sid(elem)?),
|
|
|
|
|
"proceed" => JingleMI::Proceed(check_empty_and_get_sid(elem)?),
|
|
|
|
|
"reject" => JingleMI::Reject(check_empty_and_get_sid(elem)?),
|
|
|
|
|
_ => return Err(Error::ParseError("This is not a Jingle message element.")),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<JingleMI> for Element {
|
|
|
|
|
fn from(jingle_mi: JingleMI) -> Element {
|
|
|
|
|
match jingle_mi {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
JingleMI::Propose { sid, description } => Element::builder("propose")
|
|
|
|
|
.ns(ns::JINGLE_MESSAGE)
|
|
|
|
|
.attr("id", sid)
|
|
|
|
|
.append(description),
|
|
|
|
|
JingleMI::Retract(sid) => Element::builder("retract")
|
|
|
|
|
.ns(ns::JINGLE_MESSAGE)
|
|
|
|
|
.attr("id", sid),
|
|
|
|
|
JingleMI::Accept(sid) => Element::builder("accept")
|
|
|
|
|
.ns(ns::JINGLE_MESSAGE)
|
|
|
|
|
.attr("id", sid),
|
|
|
|
|
JingleMI::Proceed(sid) => Element::builder("proceed")
|
|
|
|
|
.ns(ns::JINGLE_MESSAGE)
|
|
|
|
|
.attr("id", sid),
|
|
|
|
|
JingleMI::Reject(sid) => Element::builder("reject")
|
|
|
|
|
.ns(ns::JINGLE_MESSAGE)
|
|
|
|
|
.attr("id", sid),
|
|
|
|
|
}
|
|
|
|
|
.build()
|
2017-07-15 10:38:44 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(JingleMI, 68);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(JingleMI, 136);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 10:38:44 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element = "<accept xmlns='urn:xmpp:jingle-message:0' id='coucou'/>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-07-15 10:38:44 +00:00
|
|
|
|
JingleMI::try_from(elem).unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_child() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let elem: Element =
|
|
|
|
|
"<propose xmlns='urn:xmpp:jingle-message:0' id='coucou'><coucou/></propose>"
|
|
|
|
|
.parse()
|
|
|
|
|
.unwrap();
|
2017-07-15 10:38:44 +00:00
|
|
|
|
let error = JingleMI::try_from(elem).unwrap_err();
|
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
|
|
|
|
assert_eq!(message, "Unknown child in propose element.");
|
|
|
|
|
}
|
|
|
|
|
}
|