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-01 22:49:44 +00:00
|
|
|
use std::convert::TryFrom;
|
2017-04-23 14:13:03 +00:00
|
|
|
use std::str::FromStr;
|
2017-05-06 20:38:23 +00:00
|
|
|
use std::collections::BTreeMap;
|
2017-04-23 14:13:03 +00:00
|
|
|
|
2017-05-06 20:21:34 +00:00
|
|
|
use minidom::{Element, IntoAttributeValue};
|
2017-04-23 14:13:03 +00:00
|
|
|
|
|
|
|
use jid::Jid;
|
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
|
|
|
|
use ns;
|
|
|
|
|
2017-05-06 20:13:53 +00:00
|
|
|
use stanza_error::StanzaError;
|
2017-05-06 19:33:58 +00:00
|
|
|
use chatstates::ChatState;
|
2017-05-06 19:07:03 +00:00
|
|
|
use receipts::Receipt;
|
2017-05-06 19:42:12 +00:00
|
|
|
use delay::Delay;
|
2017-05-01 22:49:44 +00:00
|
|
|
use attention::Attention;
|
2017-05-06 19:16:45 +00:00
|
|
|
use message_correct::Replace;
|
2017-05-06 20:03:42 +00:00
|
|
|
use eme::ExplicitMessageEncryption;
|
2017-04-23 14:13:03 +00:00
|
|
|
|
|
|
|
/// Lists every known payload of a `<message/>`.
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum MessagePayload {
|
2017-05-06 20:13:53 +00:00
|
|
|
StanzaError(StanzaError),
|
2017-05-06 19:33:58 +00:00
|
|
|
ChatState(ChatState),
|
2017-05-06 19:07:03 +00:00
|
|
|
Receipt(Receipt),
|
2017-05-06 19:42:12 +00:00
|
|
|
Delay(Delay),
|
2017-05-01 22:49:44 +00:00
|
|
|
Attention(Attention),
|
2017-05-06 19:16:45 +00:00
|
|
|
MessageCorrect(Replace),
|
2017-05-06 20:03:42 +00:00
|
|
|
ExplicitMessageEncryption(ExplicitMessageEncryption),
|
2017-04-23 14:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum MessageType {
|
|
|
|
Chat,
|
|
|
|
Error,
|
|
|
|
Groupchat,
|
|
|
|
Headline,
|
|
|
|
Normal,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for MessageType {
|
|
|
|
fn default() -> MessageType {
|
|
|
|
MessageType::Normal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for MessageType {
|
|
|
|
type Err = Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<MessageType, Error> {
|
|
|
|
Ok(match s {
|
|
|
|
"chat" => MessageType::Chat,
|
|
|
|
"error" => MessageType::Error,
|
|
|
|
"groupchat" => MessageType::Groupchat,
|
|
|
|
"headline" => MessageType::Headline,
|
|
|
|
"normal" => MessageType::Normal,
|
|
|
|
|
|
|
|
_ => return Err(Error::ParseError("Invalid 'type' attribute on message element.")),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoAttributeValue for MessageType {
|
|
|
|
fn into_attribute_value(self) -> Option<String> {
|
|
|
|
Some(match self {
|
|
|
|
MessageType::Chat => "chat",
|
|
|
|
MessageType::Error => "error",
|
|
|
|
MessageType::Groupchat => "groupchat",
|
|
|
|
MessageType::Headline => "headline",
|
|
|
|
MessageType::Normal => "normal",
|
|
|
|
}.to_owned())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum MessagePayloadType {
|
|
|
|
XML(Element),
|
|
|
|
Parsed(MessagePayload),
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:38:23 +00:00
|
|
|
type Lang = String;
|
|
|
|
type Body = String;
|
2017-05-07 14:10:04 +00:00
|
|
|
type Subject = String;
|
2017-05-06 20:38:23 +00:00
|
|
|
|
2017-04-23 14:13:03 +00:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Message {
|
|
|
|
pub from: Option<Jid>,
|
|
|
|
pub to: Option<Jid>,
|
|
|
|
pub id: Option<String>,
|
|
|
|
pub type_: MessageType,
|
2017-05-06 20:38:23 +00:00
|
|
|
pub bodies: BTreeMap<Lang, Body>,
|
2017-05-07 14:10:04 +00:00
|
|
|
pub subjects: BTreeMap<Lang, Subject>,
|
2017-04-23 14:13:03 +00:00
|
|
|
pub payloads: Vec<MessagePayloadType>,
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:21:34 +00:00
|
|
|
impl<'a> TryFrom<&'a Element> for Message {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(root: &'a Element) -> Result<Message, Error> {
|
|
|
|
if !root.is("message", ns::JABBER_CLIENT) {
|
|
|
|
return Err(Error::ParseError("This is not a message element."));
|
|
|
|
}
|
|
|
|
let from = root.attr("from")
|
|
|
|
.and_then(|value| value.parse().ok());
|
|
|
|
let to = root.attr("to")
|
|
|
|
.and_then(|value| value.parse().ok());
|
|
|
|
let id = root.attr("id")
|
|
|
|
.and_then(|value| value.parse().ok());
|
|
|
|
let type_ = match root.attr("type") {
|
|
|
|
Some(type_) => type_.parse()?,
|
|
|
|
None => Default::default(),
|
2017-04-23 14:13:03 +00:00
|
|
|
};
|
2017-05-06 20:38:23 +00:00
|
|
|
let mut bodies = BTreeMap::new();
|
2017-05-07 14:10:04 +00:00
|
|
|
let mut subjects = BTreeMap::new();
|
2017-05-06 20:21:34 +00:00
|
|
|
let mut payloads = vec!();
|
|
|
|
for elem in root.children() {
|
2017-05-06 20:38:23 +00:00
|
|
|
if elem.is("body", ns::JABBER_CLIENT) {
|
|
|
|
for _ in elem.children() {
|
|
|
|
return Err(Error::ParseError("Unknown child in body element."));
|
|
|
|
}
|
|
|
|
let lang = elem.attr("xml:lang").unwrap_or("").to_owned();
|
|
|
|
if let Some(_) = bodies.insert(lang, elem.text()) {
|
|
|
|
return Err(Error::ParseError("Body element present twice for the same xml:lang."));
|
|
|
|
}
|
2017-05-07 14:10:04 +00:00
|
|
|
} else if elem.is("subject", ns::JABBER_CLIENT) {
|
|
|
|
for _ in elem.children() {
|
|
|
|
return Err(Error::ParseError("Unknown child in subject element."));
|
|
|
|
}
|
|
|
|
let lang = elem.attr("xml:lang").unwrap_or("").to_owned();
|
|
|
|
if let Some(_) = subjects.insert(lang, elem.text()) {
|
|
|
|
return Err(Error::ParseError("Subject element present twice for the same xml:lang."));
|
|
|
|
}
|
2017-05-06 20:21:34 +00:00
|
|
|
} else {
|
2017-05-06 20:38:23 +00:00
|
|
|
let payload = if let Ok(stanza_error) = StanzaError::try_from(elem) {
|
|
|
|
Some(MessagePayload::StanzaError(stanza_error))
|
|
|
|
} else if let Ok(chatstate) = ChatState::try_from(elem) {
|
|
|
|
Some(MessagePayload::ChatState(chatstate))
|
|
|
|
} else if let Ok(receipt) = Receipt::try_from(elem) {
|
|
|
|
Some(MessagePayload::Receipt(receipt))
|
|
|
|
} else if let Ok(delay) = Delay::try_from(elem) {
|
|
|
|
Some(MessagePayload::Delay(delay))
|
|
|
|
} else if let Ok(attention) = Attention::try_from(elem) {
|
|
|
|
Some(MessagePayload::Attention(attention))
|
|
|
|
} else if let Ok(replace) = Replace::try_from(elem) {
|
|
|
|
Some(MessagePayload::MessageCorrect(replace))
|
|
|
|
} else if let Ok(eme) = ExplicitMessageEncryption::try_from(elem) {
|
|
|
|
Some(MessagePayload::ExplicitMessageEncryption(eme))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
payloads.push(match payload {
|
|
|
|
Some(payload) => MessagePayloadType::Parsed(payload),
|
|
|
|
None => MessagePayloadType::XML(elem.clone()),
|
|
|
|
});
|
|
|
|
}
|
2017-05-06 20:21:34 +00:00
|
|
|
}
|
|
|
|
Ok(Message {
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
id: id,
|
|
|
|
type_: type_,
|
2017-05-07 14:06:11 +00:00
|
|
|
bodies: bodies,
|
2017-05-07 14:10:04 +00:00
|
|
|
subjects: subjects,
|
2017-05-06 20:21:34 +00:00
|
|
|
payloads: payloads,
|
|
|
|
})
|
2017-04-23 14:13:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:21:34 +00:00
|
|
|
impl<'a> Into<Element> for &'a MessagePayload {
|
|
|
|
fn into(self) -> Element {
|
|
|
|
match *self {
|
|
|
|
MessagePayload::StanzaError(ref stanza_error) => stanza_error.into(),
|
|
|
|
MessagePayload::Attention(ref attention) => attention.into(),
|
|
|
|
MessagePayload::ChatState(ref chatstate) => chatstate.into(),
|
|
|
|
MessagePayload::Receipt(ref receipt) => receipt.into(),
|
|
|
|
MessagePayload::Delay(ref delay) => delay.into(),
|
|
|
|
MessagePayload::MessageCorrect(ref replace) => replace.into(),
|
|
|
|
MessagePayload::ExplicitMessageEncryption(ref eme) => eme.into(),
|
|
|
|
}
|
2017-04-23 14:13:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:21:34 +00:00
|
|
|
impl<'a> Into<Element> for &'a Message {
|
|
|
|
fn into(self) -> Element {
|
|
|
|
let mut stanza = Element::builder("message")
|
|
|
|
.ns(ns::JABBER_CLIENT)
|
|
|
|
.attr("from", self.from.clone().and_then(|value| Some(String::from(value))))
|
|
|
|
.attr("to", self.to.clone().and_then(|value| Some(String::from(value))))
|
|
|
|
.attr("id", self.id.clone())
|
|
|
|
.attr("type", self.type_.clone())
|
2017-05-07 14:10:04 +00:00
|
|
|
.append(self.subjects.iter()
|
|
|
|
.map(|(lang, subject)| {
|
|
|
|
Element::builder("subject")
|
|
|
|
.ns(ns::JABBER_CLIENT)
|
|
|
|
.attr("xml:lang", match lang.as_ref() {
|
|
|
|
"" => None,
|
|
|
|
lang => Some(lang),
|
|
|
|
})
|
|
|
|
.append(subject.clone())
|
|
|
|
.build() })
|
|
|
|
.collect::<Vec<_>>())
|
2017-05-06 20:38:23 +00:00
|
|
|
.append(self.bodies.iter()
|
|
|
|
.map(|(lang, body)| {
|
|
|
|
Element::builder("body")
|
|
|
|
.ns(ns::JABBER_CLIENT)
|
|
|
|
.attr("xml:lang", match lang.as_ref() {
|
|
|
|
"" => None,
|
|
|
|
lang => Some(lang),
|
|
|
|
})
|
|
|
|
.append(body.clone())
|
|
|
|
.build() })
|
|
|
|
.collect::<Vec<_>>())
|
2017-05-06 20:21:34 +00:00
|
|
|
.build();
|
|
|
|
for child in self.payloads.clone() {
|
|
|
|
let elem = match child {
|
|
|
|
MessagePayloadType::XML(elem) => elem,
|
|
|
|
MessagePayloadType::Parsed(payload) => (&payload).into(),
|
|
|
|
};
|
|
|
|
stanza.append_child(elem);
|
|
|
|
}
|
|
|
|
stanza
|
2017-04-29 02:50:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 14:13:03 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-05-06 20:21:34 +00:00
|
|
|
use super::*;
|
2017-04-23 14:13:03 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap();
|
2017-05-06 20:21:34 +00:00
|
|
|
let message = Message::try_from(&elem).unwrap();
|
2017-04-23 14:13:03 +00:00
|
|
|
assert_eq!(message.from, None);
|
|
|
|
assert_eq!(message.to, None);
|
|
|
|
assert_eq!(message.id, None);
|
2017-05-06 20:21:34 +00:00
|
|
|
assert_eq!(message.type_, MessageType::Normal);
|
2017-04-23 14:13:03 +00:00
|
|
|
assert!(message.payloads.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialise() {
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' type='normal'/>".parse().unwrap();
|
2017-05-06 20:21:34 +00:00
|
|
|
let message = Message {
|
2017-04-23 14:13:03 +00:00
|
|
|
from: None,
|
|
|
|
to: None,
|
|
|
|
id: None,
|
2017-05-06 20:21:34 +00:00
|
|
|
type_: MessageType::Normal,
|
2017-05-06 20:38:23 +00:00
|
|
|
bodies: BTreeMap::new(),
|
2017-05-07 14:10:04 +00:00
|
|
|
subjects: BTreeMap::new(),
|
2017-04-23 14:13:03 +00:00
|
|
|
payloads: vec!(),
|
|
|
|
};
|
2017-05-06 20:21:34 +00:00
|
|
|
let elem2 = (&message).into();
|
2017-04-23 14:13:03 +00:00
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_body() {
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
2017-05-07 14:06:11 +00:00
|
|
|
let message = Message::try_from(&elem).unwrap();
|
|
|
|
assert_eq!(message.bodies[""], "Hello world!");
|
|
|
|
|
|
|
|
let elem2 = (&message).into();
|
|
|
|
assert_eq!(elem, elem2);
|
2017-04-23 14:13:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialise_body() {
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
2017-05-06 20:38:23 +00:00
|
|
|
let mut bodies = BTreeMap::new();
|
|
|
|
bodies.insert(String::from(""), String::from("Hello world!"));
|
2017-05-06 20:21:34 +00:00
|
|
|
let message = Message {
|
2017-04-23 14:13:03 +00:00
|
|
|
from: None,
|
|
|
|
to: Some(Jid::from_str("coucou@example.org").unwrap()),
|
|
|
|
id: None,
|
2017-05-06 20:21:34 +00:00
|
|
|
type_: MessageType::Chat,
|
2017-05-06 20:38:23 +00:00
|
|
|
bodies: bodies,
|
2017-05-07 14:10:04 +00:00
|
|
|
subjects: BTreeMap::new(),
|
2017-05-06 20:38:23 +00:00
|
|
|
payloads: vec!(),
|
2017-04-23 14:13:03 +00:00
|
|
|
};
|
2017-05-06 20:21:34 +00:00
|
|
|
let elem2 = (&message).into();
|
2017-04-23 14:13:03 +00:00
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
|
|
|
|
2017-05-07 14:10:04 +00:00
|
|
|
#[test]
|
|
|
|
fn test_subject() {
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><subject>Hello world!</subject></message>".parse().unwrap();
|
|
|
|
let message = Message::try_from(&elem).unwrap();
|
|
|
|
assert_eq!(message.subjects[""], "Hello world!");
|
|
|
|
|
|
|
|
let elem2 = (&message).into();
|
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
|
|
|
|
2017-04-23 14:13:03 +00:00
|
|
|
#[test]
|
|
|
|
fn test_attention() {
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><attention xmlns='urn:xmpp:attention:0'/></message>".parse().unwrap();
|
2017-05-06 20:21:34 +00:00
|
|
|
let message = Message::try_from(&elem).unwrap();
|
|
|
|
let elem2 = (&message).into();
|
2017-04-23 14:13:03 +00:00
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
|
|
|
}
|