diff --git a/src/lib.rs b/src/lib.rs index 1e4b975f..c9315f6e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -45,6 +45,9 @@ pub mod media_element; /// XEP-0224: Attention pub mod attention; +/// XEP-0308: Last Message Correction +pub mod message_correct; + /// XEP-0390: Entity Capabilities 2.0 pub mod ecaps2; @@ -55,6 +58,7 @@ pub enum MessagePayload { ChatState(chatstates::ChatState), Receipt(receipts::Receipt), Attention(attention::Attention), + MessageCorrect(message_correct::MessageCorrect), } /// Parse one of the payloads of a `` element, and return `Some` of a @@ -68,6 +72,8 @@ pub fn parse_message_payload(elem: &Element) -> Option { Some(MessagePayload::Receipt(receipt)) } else if let Ok(attention) = attention::parse_attention(elem) { Some(MessagePayload::Attention(attention)) + } else if let Ok(replace) = message_correct::parse_message_correct(elem) { + Some(MessagePayload::MessageCorrect(replace)) } else { None } diff --git a/src/message_correct.rs b/src/message_correct.rs new file mode 100644 index 00000000..789febb9 --- /dev/null +++ b/src/message_correct.rs @@ -0,0 +1,45 @@ +use minidom::Element; + +use error::Error; + +use ns; + +#[derive(Debug, Clone)] +pub enum MessageCorrect { + Replace(String), +} + +pub fn parse_message_correct(root: &Element) -> Result { + if !root.is("replace", ns::MESSAGE_CORRECT) { + return Err(Error::ParseError("This is not a replace element.")); + } + for _ in root.children() { + return Err(Error::ParseError("Unknown child in replace element.")); + } + let id = root.attr("id").unwrap_or("").to_owned(); + Ok(MessageCorrect::Replace(id)) +} + +#[cfg(test)] +mod tests { + use minidom::Element; + use error::Error; + use message_correct; + + #[test] + fn test_simple() { + let elem: Element = "".parse().unwrap(); + message_correct::parse_message_correct(&elem).unwrap(); + } + + #[test] + fn test_invalid_child() { + let elem: Element = "".parse().unwrap(); + let error = message_correct::parse_message_correct(&elem).unwrap_err(); + let message = match error { + Error::ParseError(string) => string, + _ => panic!(), + }; + assert_eq!(message, "Unknown child in replace element."); + } +} diff --git a/src/ns.rs b/src/ns.rs index 2b5ba3f1..df708fbe 100644 --- a/src/ns.rs +++ b/src/ns.rs @@ -28,6 +28,9 @@ pub const MEDIA_ELEMENT: &'static str = "urn:xmpp:media-element"; /// XEP-0224: Attention pub const ATTENTION: &'static str = "urn:xmpp:attention:0"; +/// XEP-0308: Last Message Correction +pub const MESSAGE_CORRECT: &'static str = "urn:xmpp:message-correct:0"; + /// XEP-0390: Entity Capabilities 2.0 pub const ECAPS2: &'static str = "urn:xmpp:caps"; /// XEP-0390: Entity Capabilities 2.0