Add a forwarding parser and serialiser.
This commit is contained in:
parent
d824a161b6
commit
0f92a11894
3 changed files with 84 additions and 0 deletions
78
src/forwarding.rs
Normal file
78
src/forwarding.rs
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
use minidom::{Element, IntoElements};
|
||||||
|
use minidom::convert::ElementEmitter;
|
||||||
|
|
||||||
|
use error::Error;
|
||||||
|
|
||||||
|
use delay;
|
||||||
|
use message;
|
||||||
|
|
||||||
|
use ns;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Forwarded {
|
||||||
|
pub delay: Option<delay::Delay>,
|
||||||
|
// XXX: really? Option?
|
||||||
|
pub stanza: Option<message::Message>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_forwarded(root: &Element) -> Result<Forwarded, Error> {
|
||||||
|
if !root.is("forwarded", ns::FORWARD) {
|
||||||
|
return Err(Error::ParseError("This is not a forwarded element."));
|
||||||
|
}
|
||||||
|
let mut delay = None;
|
||||||
|
let mut stanza = None;
|
||||||
|
for child in root.children() {
|
||||||
|
if child.is("delay", ns::DELAY) {
|
||||||
|
delay = Some(delay::parse_delay(child)?);
|
||||||
|
} else if child.is("message", ns::JABBER_CLIENT) {
|
||||||
|
stanza = Some(message::parse_message(child)?);
|
||||||
|
// TODO: also handle the five other possibilities.
|
||||||
|
} else {
|
||||||
|
return Err(Error::ParseError("Unknown child in forwarded element."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(Forwarded {
|
||||||
|
delay: delay,
|
||||||
|
stanza: stanza,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn serialise(forwarded: &Forwarded) -> Element {
|
||||||
|
Element::builder("forwarded")
|
||||||
|
.ns(ns::FORWARD)
|
||||||
|
.append(forwarded.delay.clone())
|
||||||
|
.append(forwarded.stanza.clone())
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use minidom::Element;
|
||||||
|
use error::Error;
|
||||||
|
use forwarding;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_simple() {
|
||||||
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
|
||||||
|
forwarding::parse_forwarded(&elem).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_invalid_child() {
|
||||||
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'><coucou/></forwarded>".parse().unwrap();
|
||||||
|
let error = forwarding::parse_forwarded(&elem).unwrap_err();
|
||||||
|
let message = match error {
|
||||||
|
Error::ParseError(string) => string,
|
||||||
|
_ => panic!(),
|
||||||
|
};
|
||||||
|
assert_eq!(message, "Unknown child in forwarded element.");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_serialise() {
|
||||||
|
let elem: Element = "<forwarded xmlns='urn:xmpp:forward:0'/>".parse().unwrap();
|
||||||
|
let forwarded = forwarding::Forwarded { delay: None, stanza: None };
|
||||||
|
let elem2 = forwarding::serialise(&forwarded);
|
||||||
|
assert_eq!(elem, elem2);
|
||||||
|
}
|
||||||
|
}
|
|
@ -68,6 +68,9 @@ pub mod jingle_ft;
|
||||||
/// XEP-0261: Jingle In-Band Bytestreams Transport Method
|
/// XEP-0261: Jingle In-Band Bytestreams Transport Method
|
||||||
pub mod jingle_ibb;
|
pub mod jingle_ibb;
|
||||||
|
|
||||||
|
/// XEP-0297: Stanza Forwarding
|
||||||
|
pub mod forwarding;
|
||||||
|
|
||||||
/// XEP-0300: Use of Cryptographic Hash Functions in XMPP
|
/// XEP-0300: Use of Cryptographic Hash Functions in XMPP
|
||||||
pub mod hashes;
|
pub mod hashes;
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,9 @@ pub const JINGLE_FT_ERROR: &'static str = "urn:xmpp:jingle:apps:file-transfer:er
|
||||||
/// XEP-0261: Jingle In-Band Bytestreams Transport Method
|
/// XEP-0261: Jingle In-Band Bytestreams Transport Method
|
||||||
pub const JINGLE_IBB: &'static str = "urn:xmpp:jingle:transports:ibb:1";
|
pub const JINGLE_IBB: &'static str = "urn:xmpp:jingle:transports:ibb:1";
|
||||||
|
|
||||||
|
/// XEP-0297: Stanza Forwarding
|
||||||
|
pub const FORWARD: &'static str = "urn:xmpp:forward:0";
|
||||||
|
|
||||||
/// XEP-0300: Use of Cryptographic Hash Functions in XMPP
|
/// XEP-0300: Use of Cryptographic Hash Functions in XMPP
|
||||||
pub const HASHES: &'static str = "urn:xmpp:hashes:2";
|
pub const HASHES: &'static str = "urn:xmpp:hashes:2";
|
||||||
/// XEP-0300: Use of Cryptographic Hash Functions in XMPP
|
/// XEP-0300: Use of Cryptographic Hash Functions in XMPP
|
||||||
|
|
Loading…
Reference in a new issue