From f3c9a58862942fafdfdda3facd3753739173e048 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Thu, 20 Apr 2017 00:43:33 +0100 Subject: [PATCH] Add a receipts parser. --- src/lib.rs | 4 ++++ src/ns.rs | 1 + src/receipts.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/receipts.rs diff --git a/src/lib.rs b/src/lib.rs index c6f93d93..f7608cd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ pub mod jingle; pub mod ping; pub mod chatstates; pub mod ibb; +pub mod receipts; use minidom::Element; @@ -19,6 +20,7 @@ use minidom::Element; pub enum MessagePayload { Body(body::Body), ChatState(chatstates::ChatState), + Receipt(receipts::Receipt), } pub fn parse_message_payload(elem: &Element) -> Option { @@ -26,6 +28,8 @@ pub fn parse_message_payload(elem: &Element) -> Option { Some(MessagePayload::Body(body)) } else if let Ok(chatstate) = chatstates::parse_chatstate(elem) { Some(MessagePayload::ChatState(chatstate)) + } else if let Ok(receipt) = receipts::parse_receipt(elem) { + Some(MessagePayload::Receipt(receipt)) } else { None } diff --git a/src/ns.rs b/src/ns.rs index 4f18ccc8..49293f4f 100644 --- a/src/ns.rs +++ b/src/ns.rs @@ -6,3 +6,4 @@ pub const JINGLE_NS: &'static str = "urn:xmpp:jingle:1"; pub const PING_NS: &'static str = "urn:xmpp:ping"; pub const CHATSTATES_NS: &'static str = "http://jabber.org/protocol/chatstates"; pub const IBB_NS: &'static str = "http://jabber.org/protocol/ibb"; +pub const RECEIPTS_NS: &'static str = "urn:xmpp:receipts"; diff --git a/src/receipts.rs b/src/receipts.rs new file mode 100644 index 00000000..a19dff1c --- /dev/null +++ b/src/receipts.rs @@ -0,0 +1,44 @@ +use minidom::Element; + +use error::Error; + +use ns::RECEIPTS_NS; + +#[derive(Debug)] +pub enum Receipt { + Request, + Received(String), +} + +pub fn parse_receipt(root: &Element) -> Result { + for _ in root.children() { + return Err(Error::ParseError("Unknown child in receipt element.")); + } + if root.is("request", RECEIPTS_NS) { + Ok(Receipt::Request) + } else if root.is("received", RECEIPTS_NS) { + let id = root.attr("id").unwrap_or("").to_owned(); + Ok(Receipt::Received(id)) + } else { + Err(Error::ParseError("This is not a receipt element.")) + } +} + +#[cfg(test)] +mod tests { + use minidom::Element; + //use error::Error; + use receipts; + + #[test] + fn test_simple() { + let elem: Element = "".parse().unwrap(); + receipts::parse_receipt(&elem).unwrap(); + + let elem: Element = "".parse().unwrap(); + receipts::parse_receipt(&elem).unwrap(); + + let elem: Element = "".parse().unwrap(); + receipts::parse_receipt(&elem).unwrap(); + } +}