Add an Explicit Message Encryption parser.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-21 03:45:05 +01:00
parent 94380fdbd5
commit a680ab194c
3 changed files with 77 additions and 0 deletions

68
src/eme.rs Normal file
View file

@ -0,0 +1,68 @@
use minidom::Element;
use error::Error;
use ns;
#[derive(Debug, Clone)]
pub struct ExplicitMessageEncryption {
pub namespace: String,
pub name: Option<String>,
}
pub fn parse_explicit_message_encryption(root: &Element) -> Result<ExplicitMessageEncryption, Error> {
if !root.is("encryption", ns::EME) {
return Err(Error::ParseError("This is not an encryption element."));
}
for _ in root.children() {
return Err(Error::ParseError("Unknown child in encryption element."));
}
let namespace = root.attr("namespace").ok_or(Error::ParseError("Mandatory argument 'namespace' not present in encryption element."))?.to_owned();
let name = root.attr("name").and_then(|value| value.parse().ok());
Ok(ExplicitMessageEncryption {
namespace: namespace,
name: name,
})
}
#[cfg(test)]
mod tests {
use minidom::Element;
use error::Error;
use eme;
#[test]
fn test_simple() {
let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='urn:xmpp:otr:0'/>".parse().unwrap();
let encryption = eme::parse_explicit_message_encryption(&elem).unwrap();
assert_eq!(encryption.namespace, "urn:xmpp:otr:0");
assert_eq!(encryption.name, None);
let elem: Element = "<encryption xmlns='urn:xmpp:eme:0' namespace='some.unknown.mechanism' name='SuperMechanism'/>".parse().unwrap();
let encryption = eme::parse_explicit_message_encryption(&elem).unwrap();
assert_eq!(encryption.namespace, "some.unknown.mechanism");
assert_eq!(encryption.name, Some(String::from("SuperMechanism")));
}
#[test]
fn test_unknown() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
let error = eme::parse_explicit_message_encryption(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "This is not an encryption element.");
}
#[test]
fn test_invalid_child() {
let elem: Element = "<encryption xmlns='urn:xmpp:eme:0'><coucou/></encryption>".parse().unwrap();
let error = eme::parse_explicit_message_encryption(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in encryption element.");
}
}

View file

@ -49,6 +49,9 @@ pub mod attention;
/// XEP-0308: Last Message Correction /// XEP-0308: Last Message Correction
pub mod message_correct; pub mod message_correct;
/// XEP-0380: Explicit Message Encryption
pub mod eme;
/// XEP-0390: Entity Capabilities 2.0 /// XEP-0390: Entity Capabilities 2.0
pub mod ecaps2; pub mod ecaps2;
@ -60,6 +63,7 @@ pub enum MessagePayload {
Receipt(receipts::Receipt), Receipt(receipts::Receipt),
Attention(attention::Attention), Attention(attention::Attention),
MessageCorrect(message_correct::MessageCorrect), MessageCorrect(message_correct::MessageCorrect),
ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
} }
/// Parse one of the payloads of a `<message/>` element, and return `Some` of a /// Parse one of the payloads of a `<message/>` element, and return `Some` of a
@ -75,6 +79,8 @@ pub fn parse_message_payload(elem: &Element) -> Option<MessagePayload> {
Some(MessagePayload::Attention(attention)) Some(MessagePayload::Attention(attention))
} else if let Ok(replace) = message_correct::parse_message_correct(elem) { } else if let Ok(replace) = message_correct::parse_message_correct(elem) {
Some(MessagePayload::MessageCorrect(replace)) Some(MessagePayload::MessageCorrect(replace))
} else if let Ok(eme) = eme::parse_explicit_message_encryption(elem) {
Some(MessagePayload::ExplicitMessageEncryption(eme))
} else { } else {
None None
} }

View file

@ -31,6 +31,9 @@ pub const ATTENTION: &'static str = "urn:xmpp:attention:0";
/// XEP-0308: Last Message Correction /// XEP-0308: Last Message Correction
pub const MESSAGE_CORRECT: &'static str = "urn:xmpp:message-correct:0"; pub const MESSAGE_CORRECT: &'static str = "urn:xmpp:message-correct:0";
/// XEP-0380: Explicit Message Encryption
pub const EME: &'static str = "urn:xmpp:eme:0";
/// XEP-0390: Entity Capabilities 2.0 /// XEP-0390: Entity Capabilities 2.0
pub const ECAPS2: &'static str = "urn:xmpp:caps"; pub const ECAPS2: &'static str = "urn:xmpp:caps";
/// XEP-0390: Entity Capabilities 2.0 /// XEP-0390: Entity Capabilities 2.0