xmpp-rs/src/jingle.rs

561 lines
21 KiB
Rust
Raw Normal View History

2017-04-29 21:14:34 +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/.
2017-05-04 00:20:28 +00:00
use std::convert::TryFrom;
2017-04-19 01:27:42 +00:00
use std::str::FromStr;
2017-05-04 00:20:28 +00:00
use minidom::Element;
2017-04-19 01:27:42 +00:00
use error::Error;
use ns;
2017-04-19 01:27:42 +00:00
2017-04-20 23:41:15 +00:00
#[derive(Debug, Clone, PartialEq)]
2017-04-19 01:27:42 +00:00
pub enum Action {
ContentAccept,
ContentAdd,
ContentModify,
ContentReject,
ContentRemove,
DescriptionInfo,
SecurityInfo,
SessionAccept,
SessionInfo,
SessionInitiate,
SessionTerminate,
TransportAccept,
TransportInfo,
TransportReject,
TransportReplace,
}
impl FromStr for Action {
type Err = Error;
fn from_str(s: &str) -> Result<Action, Error> {
Ok(match s {
"content-accept" => Action::ContentAccept,
"content-add" => Action::ContentAdd,
"content-modify" => Action::ContentModify,
"content-reject" => Action::ContentReject,
"content-remove" => Action::ContentRemove,
"description-info" => Action::DescriptionInfo,
"security-info" => Action::SecurityInfo,
"session-accept" => Action::SessionAccept,
"session-info" => Action::SessionInfo,
"session-initiate" => Action::SessionInitiate,
"session-terminate" => Action::SessionTerminate,
"transport-accept" => Action::TransportAccept,
"transport-info" => Action::TransportInfo,
"transport-reject" => Action::TransportReject,
"transport-replace" => Action::TransportReplace,
_ => return Err(Error::ParseError("Unknown action.")),
})
2017-04-19 01:27:42 +00:00
}
}
impl From<Action> for String {
fn from(action: Action) -> String {
String::from(match action {
Action::ContentAccept => "content-accept",
Action::ContentAdd => "content-add",
Action::ContentModify => "content-modify",
Action::ContentReject => "content-reject",
Action::ContentRemove => "content-remove",
Action::DescriptionInfo => "description-info",
Action::SecurityInfo => "security-info",
Action::SessionAccept => "session-accept",
Action::SessionInfo => "session-info",
Action::SessionInitiate => "session-initiate",
Action::SessionTerminate => "session-terminate",
Action::TransportAccept => "transport-accept",
Action::TransportInfo => "transport-info",
Action::TransportReject => "transport-reject",
Action::TransportReplace => "transport-replace",
})
}
}
2017-04-19 01:27:42 +00:00
// TODO: use a real JID type.
type Jid = String;
2017-04-20 23:41:15 +00:00
#[derive(Debug, Clone, PartialEq)]
2017-04-19 01:27:42 +00:00
pub enum Creator {
Initiator,
Responder,
}
impl FromStr for Creator {
type Err = Error;
fn from_str(s: &str) -> Result<Creator, Error> {
Ok(match s {
"initiator" => Creator::Initiator,
"responder" => Creator::Responder,
_ => return Err(Error::ParseError("Unknown creator.")),
})
2017-04-19 01:27:42 +00:00
}
}
impl From<Creator> for String {
fn from(creator: Creator) -> String {
String::from(match creator {
Creator::Initiator => "initiator",
Creator::Responder => "responder",
})
}
}
2017-04-20 23:41:15 +00:00
#[derive(Debug, Clone, PartialEq)]
2017-04-19 01:27:42 +00:00
pub enum Senders {
Both,
Initiator,
None_,
Responder,
}
impl FromStr for Senders {
type Err = Error;
fn from_str(s: &str) -> Result<Senders, Error> {
Ok(match s {
"both" => Senders::Both,
"initiator" => Senders::Initiator,
"none" => Senders::None_,
"responder" => Senders::Responder,
_ => return Err(Error::ParseError("Unknown senders.")),
})
2017-04-19 01:27:42 +00:00
}
}
impl From<Senders> for String {
fn from(senders: Senders) -> String {
String::from(match senders {
Senders::Both => "both",
Senders::Initiator => "initiator",
Senders::None_ => "none",
Senders::Responder => "responder",
})
}
}
2017-04-20 23:41:15 +00:00
#[derive(Debug, Clone)]
2017-04-19 01:27:42 +00:00
pub struct Content {
pub creator: Creator,
pub disposition: String,
pub name: String,
pub senders: Senders,
pub description: Option<Element>,
pub transport: Option<Element>,
pub security: Option<Element>,
2017-04-19 01:27:42 +00:00
}
2017-04-20 23:41:15 +00:00
#[derive(Debug, Clone, PartialEq)]
2017-04-19 01:27:42 +00:00
pub enum Reason {
AlternativeSession, //(String),
Busy,
Cancel,
ConnectivityError,
Decline,
Expired,
FailedApplication,
FailedTransport,
GeneralError,
Gone,
IncompatibleParameters,
MediaError,
SecurityError,
Success,
Timeout,
UnsupportedApplications,
UnsupportedTransports,
}
impl FromStr for Reason {
type Err = Error;
fn from_str(s: &str) -> Result<Reason, Error> {
Ok(match s {
"alternative-session" => Reason::AlternativeSession,
"busy" => Reason::Busy,
"cancel" => Reason::Cancel,
"connectivity-error" => Reason::ConnectivityError,
"decline" => Reason::Decline,
"expired" => Reason::Expired,
"failed-application" => Reason::FailedApplication,
"failed-transport" => Reason::FailedTransport,
"general-error" => Reason::GeneralError,
"gone" => Reason::Gone,
"incompatible-parameters" => Reason::IncompatibleParameters,
"media-error" => Reason::MediaError,
"security-error" => Reason::SecurityError,
"success" => Reason::Success,
"timeout" => Reason::Timeout,
"unsupported-applications" => Reason::UnsupportedApplications,
"unsupported-transports" => Reason::UnsupportedTransports,
_ => return Err(Error::ParseError("Unknown reason.")),
})
2017-04-19 01:27:42 +00:00
}
}
impl Into<Element> for Reason {
2017-05-04 00:20:28 +00:00
fn into(self) -> Element {
Element::builder(match self {
Reason::AlternativeSession => "alternative-session",
Reason::Busy => "busy",
Reason::Cancel => "cancel",
Reason::ConnectivityError => "connectivity-error",
Reason::Decline => "decline",
Reason::Expired => "expired",
Reason::FailedApplication => "failed-application",
Reason::FailedTransport => "failed-transport",
Reason::GeneralError => "general-error",
Reason::Gone => "gone",
Reason::IncompatibleParameters => "incompatible-parameters",
Reason::MediaError => "media-error",
Reason::SecurityError => "security-error",
Reason::Success => "success",
Reason::Timeout => "timeout",
Reason::UnsupportedApplications => "unsupported-applications",
Reason::UnsupportedTransports => "unsupported-transports",
2017-05-04 00:20:28 +00:00
}).build()
}
}
2017-04-20 23:41:15 +00:00
#[derive(Debug, Clone)]
2017-04-19 01:27:42 +00:00
pub struct ReasonElement {
pub reason: Reason,
pub text: Option<String>,
}
2017-04-20 23:41:15 +00:00
#[derive(Debug, Clone)]
2017-04-19 01:27:42 +00:00
pub struct Jingle {
pub action: Action,
pub initiator: Option<Jid>,
pub responder: Option<Jid>,
pub sid: String,
pub contents: Vec<Content>,
pub reason: Option<ReasonElement>,
pub other: Vec<Element>,
2017-04-19 01:27:42 +00:00
}
impl TryFrom<Element> for Jingle {
2017-05-04 00:20:28 +00:00
type Error = Error;
fn try_from(root: Element) -> Result<Jingle, Error> {
2017-05-04 00:20:28 +00:00
if !root.is("jingle", ns::JINGLE) {
return Err(Error::ParseError("This is not a Jingle element."));
}
let mut contents: Vec<Content> = vec!();
let action = root.attr("action")
.ok_or(Error::ParseError("Jingle must have an 'action' attribute."))?
.parse()?;
let initiator = root.attr("initiator")
.and_then(|initiator| initiator.parse().ok());
let responder = root.attr("responder")
.and_then(|responder| responder.parse().ok());
let sid = root.attr("sid")
.ok_or(Error::ParseError("Jingle must have a 'sid' attribute."))?;
let mut reason_element = None;
let mut other = vec!();
for child in root.children() {
if child.is("content", ns::JINGLE) {
let creator = child.attr("creator")
.ok_or(Error::ParseError("Content must have a 'creator' attribute."))?
.parse()?;
let disposition = child.attr("disposition")
.unwrap_or("session");
let name = child.attr("name")
.ok_or(Error::ParseError("Content must have a 'name' attribute."))?;
let senders = child.attr("senders")
.unwrap_or("both")
.parse()?;
let mut description = None;
let mut transport = None;
let mut security = None;
for stuff in child.children() {
if stuff.name() == "description" {
if description.is_some() {
return Err(Error::ParseError("Content must not have more than one description."));
}
description = Some(stuff.clone());
2017-05-04 00:20:28 +00:00
} else if stuff.name() == "transport" {
if transport.is_some() {
return Err(Error::ParseError("Content must not have more than one transport."));
}
transport = Some(stuff.clone());
2017-05-04 00:20:28 +00:00
} else if stuff.name() == "security" {
if security.is_some() {
return Err(Error::ParseError("Content must not have more than one security."));
}
security = Some(stuff.clone());
2017-04-19 01:27:42 +00:00
}
}
2017-05-04 00:20:28 +00:00
contents.push(Content {
creator: creator,
disposition: disposition.to_owned(),
name: name.to_owned(),
senders: senders,
description: description,
transport: transport,
security: security,
});
} else if child.is("reason", ns::JINGLE) {
if reason_element.is_some() {
return Err(Error::ParseError("Jingle must not have more than one reason."));
2017-04-19 01:27:42 +00:00
}
2017-05-04 00:20:28 +00:00
let mut reason = None;
let mut text = None;
for stuff in child.children() {
if stuff.ns() != Some(ns::JINGLE) {
return Err(Error::ParseError("Reason contains a foreign element."));
}
let name = stuff.name();
if name == "text" {
if text.is_some() {
return Err(Error::ParseError("Reason must not have more than one text."));
}
text = Some(stuff.text());
} else {
reason = Some(name.parse()?);
}
2017-04-19 01:27:42 +00:00
}
2017-05-04 00:20:28 +00:00
if reason.is_none() {
return Err(Error::ParseError("Reason doesnt contain a valid reason."));
}
reason_element = Some(ReasonElement {
reason: reason.unwrap(),
text: text,
});
} else {
other.push(child.clone());
2017-04-19 01:27:42 +00:00
}
}
2017-05-04 00:20:28 +00:00
Ok(Jingle {
action: action,
initiator: initiator,
responder: responder,
sid: sid.to_owned(),
contents: contents,
reason: reason_element,
other: other,
})
}
2017-04-19 01:27:42 +00:00
}
impl Into<Element> for Content {
2017-05-04 00:20:28 +00:00
fn into(self) -> Element {
let mut root = Element::builder("content")
.ns(ns::JINGLE)
.attr("creator", String::from(self.creator.clone()))
.attr("disposition", self.disposition.clone())
.attr("name", self.name.clone())
.attr("senders", String::from(self.senders.clone()))
.build();
if let Some(description) = self.description.clone() {
root.append_child(description);
}
if let Some(transport) = self.transport.clone() {
root.append_child(transport);
}
2017-05-04 00:20:28 +00:00
if let Some(security) = self.security.clone() {
root.append_child(security);
2017-05-04 00:20:28 +00:00
}
root
2017-04-24 18:25:00 +00:00
}
}
impl Into<Element> for Jingle {
2017-05-04 00:20:28 +00:00
fn into(self) -> Element {
let mut root = Element::builder("jingle")
.ns(ns::JINGLE)
.attr("action", String::from(self.action.clone()))
.attr("initiator", self.initiator.clone())
.attr("responder", self.responder.clone())
.attr("sid", self.sid.clone())
.build();
for content in self.contents {
let content_elem = content.into();
2017-05-04 00:20:28 +00:00
root.append_child(content_elem);
}
if let Some(reason) = self.reason {
let reason2: Element = reason.reason.into();
2017-05-04 00:20:28 +00:00
let reason_elem = Element::builder("reason")
.append(reason2)
.append(reason.text)
2017-05-04 00:20:28 +00:00
.build();
root.append_child(reason_elem);
}
root
2017-04-24 18:25:00 +00:00
}
2017-05-04 00:20:28 +00:00
}
2017-04-19 01:27:42 +00:00
#[cfg(test)]
mod tests {
2017-05-04 00:20:28 +00:00
use super::*;
2017-04-19 01:27:42 +00:00
#[test]
fn test_simple() {
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'/>".parse().unwrap();
let jingle = Jingle::try_from(elem).unwrap();
2017-05-04 00:20:28 +00:00
assert_eq!(jingle.action, Action::SessionInitiate);
2017-04-19 01:27:42 +00:00
assert_eq!(jingle.sid, "coucou");
}
#[test]
fn test_invalid_jingle() {
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1'/>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Jingle must have an 'action' attribute.");
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-info'/>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Jingle must have a 'sid' attribute.");
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='coucou' sid='coucou'/>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown action.");
}
#[test]
fn test_content() {
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator' name='coucou'><description/><transport/></content></jingle>".parse().unwrap();
let jingle = Jingle::try_from(elem).unwrap();
2017-05-04 00:20:28 +00:00
assert_eq!(jingle.contents[0].creator, Creator::Initiator);
2017-04-19 01:27:42 +00:00
assert_eq!(jingle.contents[0].name, "coucou");
2017-05-04 00:20:28 +00:00
assert_eq!(jingle.contents[0].senders, Senders::Both);
2017-04-19 01:27:42 +00:00
assert_eq!(jingle.contents[0].disposition, "session");
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator' name='coucou' senders='both'><description/><transport/></content></jingle>".parse().unwrap();
let jingle = Jingle::try_from(elem).unwrap();
2017-05-04 00:20:28 +00:00
assert_eq!(jingle.contents[0].senders, Senders::Both);
2017-04-19 01:27:42 +00:00
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator' name='coucou' disposition='early-session'><description/><transport/></content></jingle>".parse().unwrap();
let jingle = Jingle::try_from(elem).unwrap();
2017-04-19 01:27:42 +00:00
assert_eq!(jingle.contents[0].disposition, "early-session");
}
#[test]
fn test_invalid_content() {
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content/></jingle>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Content must have a 'creator' attribute.");
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator'/></jingle>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Content must have a 'name' attribute.");
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='coucou' name='coucou'/></jingle>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown creator.");
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator' name='coucou' senders='coucou'/></jingle>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown senders.");
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><content creator='initiator' name='coucou' senders=''/></jingle>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown senders.");
}
#[test]
fn test_reason() {
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><success/></reason></jingle>".parse().unwrap();
let jingle = Jingle::try_from(elem).unwrap();
2017-04-19 01:27:42 +00:00
let reason = jingle.reason.unwrap();
2017-05-04 00:20:28 +00:00
assert_eq!(reason.reason, Reason::Success);
2017-04-19 01:27:42 +00:00
assert_eq!(reason.text, None);
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><success/><text>coucou</text></reason></jingle>".parse().unwrap();
let jingle = Jingle::try_from(elem).unwrap();
2017-04-19 01:27:42 +00:00
let reason = jingle.reason.unwrap();
2017-05-04 00:20:28 +00:00
assert_eq!(reason.reason, Reason::Success);
2017-04-19 01:27:42 +00:00
assert_eq!(reason.text, Some(String::from("coucou")));
}
#[test]
fn test_invalid_reason() {
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason/></jingle>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Reason doesnt contain a valid reason.");
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><a/></reason></jingle>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown reason.");
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><a xmlns='http://www.w3.org/1999/xhtml'/></reason></jingle>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Reason contains a foreign element.");
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><decline/></reason><reason/></jingle>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
2017-04-19 01:27:42 +00:00
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Jingle must not have more than one reason.");
let elem: Element = "<jingle xmlns='urn:xmpp:jingle:1' action='session-initiate' sid='coucou'><reason><decline/><text/><text/></reason></jingle>".parse().unwrap();
let error = Jingle::try_from(elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Reason must not have more than one text.");
2017-04-19 01:27:42 +00:00
}
}