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-04 00:20:22 +00:00
|
|
|
use minidom::{Element, IntoElements, IntoAttributeValue, ElementEmitter};
|
2017-04-23 14:13:03 +00:00
|
|
|
|
|
|
|
use jid::Jid;
|
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
|
|
|
|
use ns;
|
|
|
|
|
|
|
|
use body;
|
2017-05-01 00:04:15 +00:00
|
|
|
use stanza_error;
|
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 {
|
|
|
|
Body(body::Body),
|
2017-05-01 00:04:15 +00:00
|
|
|
StanzaError(stanza_error::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),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Message {
|
|
|
|
pub from: Option<Jid>,
|
|
|
|
pub to: Option<Jid>,
|
|
|
|
pub id: Option<String>,
|
|
|
|
pub type_: MessageType,
|
|
|
|
pub payloads: Vec<MessagePayloadType>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn parse_message(root: &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(),
|
|
|
|
};
|
|
|
|
let mut payloads = vec!();
|
|
|
|
for elem in root.children() {
|
|
|
|
let payload = if let Ok(body) = body::parse_body(elem) {
|
|
|
|
Some(MessagePayload::Body(body))
|
2017-05-01 00:04:15 +00:00
|
|
|
} else if let Ok(stanza_error) = stanza_error::parse_stanza_error(elem) {
|
|
|
|
Some(MessagePayload::StanzaError(stanza_error))
|
2017-05-06 19:33:58 +00:00
|
|
|
} else if let Ok(chatstate) = ChatState::try_from(elem) {
|
2017-04-23 14:13:03 +00:00
|
|
|
Some(MessagePayload::ChatState(chatstate))
|
2017-05-06 19:07:03 +00:00
|
|
|
} else if let Ok(receipt) = Receipt::try_from(elem) {
|
2017-04-23 14:13:03 +00:00
|
|
|
Some(MessagePayload::Receipt(receipt))
|
2017-05-06 19:42:12 +00:00
|
|
|
} else if let Ok(delay) = Delay::try_from(elem) {
|
2017-04-23 14:13:03 +00:00
|
|
|
Some(MessagePayload::Delay(delay))
|
2017-05-01 22:49:44 +00:00
|
|
|
} else if let Ok(attention) = Attention::try_from(elem) {
|
2017-04-23 14:13:03 +00:00
|
|
|
Some(MessagePayload::Attention(attention))
|
2017-05-06 19:16:45 +00:00
|
|
|
} else if let Ok(replace) = Replace::try_from(elem) {
|
2017-04-23 14:13:03 +00:00
|
|
|
Some(MessagePayload::MessageCorrect(replace))
|
2017-05-06 20:03:42 +00:00
|
|
|
} else if let Ok(eme) = ExplicitMessageEncryption::try_from(elem) {
|
2017-04-23 14:13:03 +00:00
|
|
|
Some(MessagePayload::ExplicitMessageEncryption(eme))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
payloads.push(match payload {
|
|
|
|
Some(payload) => MessagePayloadType::Parsed(payload),
|
|
|
|
None => MessagePayloadType::XML(elem.clone()),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
Ok(Message {
|
|
|
|
from: from,
|
|
|
|
to: to,
|
|
|
|
id: id,
|
|
|
|
type_: type_,
|
|
|
|
payloads: payloads,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialise_payload(payload: &MessagePayload) -> Element {
|
|
|
|
match *payload {
|
|
|
|
MessagePayload::Body(ref body) => body::serialise(body),
|
2017-05-01 00:04:15 +00:00
|
|
|
MessagePayload::StanzaError(ref stanza_error) => stanza_error::serialise(stanza_error),
|
2017-05-01 22:49:44 +00:00
|
|
|
MessagePayload::Attention(ref attention) => attention.into(),
|
2017-05-06 19:33:58 +00:00
|
|
|
MessagePayload::ChatState(ref chatstate) => chatstate.into(),
|
2017-05-06 19:07:03 +00:00
|
|
|
MessagePayload::Receipt(ref receipt) => receipt.into(),
|
2017-05-06 19:42:12 +00:00
|
|
|
MessagePayload::Delay(ref delay) => delay.into(),
|
2017-05-06 19:16:45 +00:00
|
|
|
MessagePayload::MessageCorrect(ref replace) => replace.into(),
|
2017-05-06 20:03:42 +00:00
|
|
|
MessagePayload::ExplicitMessageEncryption(ref eme) => eme.into(),
|
2017-04-23 14:13:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn serialise(message: &Message) -> Element {
|
|
|
|
let mut stanza = Element::builder("message")
|
|
|
|
.ns(ns::JABBER_CLIENT)
|
|
|
|
.attr("from", message.from.clone().and_then(|value| Some(String::from(value))))
|
|
|
|
.attr("to", message.to.clone().and_then(|value| Some(String::from(value))))
|
|
|
|
.attr("id", message.id.clone())
|
|
|
|
.attr("type", message.type_.clone())
|
|
|
|
.build();
|
|
|
|
for child in message.payloads.clone() {
|
|
|
|
let elem = match child {
|
|
|
|
MessagePayloadType::XML(elem) => elem,
|
|
|
|
MessagePayloadType::Parsed(payload) => serialise_payload(&payload),
|
|
|
|
};
|
|
|
|
stanza.append_child(elem);
|
|
|
|
}
|
|
|
|
stanza
|
|
|
|
}
|
|
|
|
|
2017-04-29 02:50:20 +00:00
|
|
|
impl IntoElements for Message {
|
|
|
|
fn into_elements(self, emitter: &mut ElementEmitter) {
|
|
|
|
let elem = serialise(&self);
|
|
|
|
emitter.append_child(elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-23 14:13:03 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use std::str::FromStr;
|
|
|
|
use minidom::Element;
|
|
|
|
use jid::Jid;
|
|
|
|
use message;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_simple() {
|
|
|
|
let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap();
|
|
|
|
let message = message::parse_message(&elem).unwrap();
|
|
|
|
assert_eq!(message.from, None);
|
|
|
|
assert_eq!(message.to, None);
|
|
|
|
assert_eq!(message.id, None);
|
|
|
|
assert_eq!(message.type_, message::MessageType::Normal);
|
|
|
|
assert!(message.payloads.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_serialise() {
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' type='normal'/>".parse().unwrap();
|
2017-04-29 05:57:20 +00:00
|
|
|
let message = message::Message {
|
2017-04-23 14:13:03 +00:00
|
|
|
from: None,
|
|
|
|
to: None,
|
|
|
|
id: None,
|
|
|
|
type_: message::MessageType::Normal,
|
|
|
|
payloads: vec!(),
|
|
|
|
};
|
2017-04-29 05:57:20 +00:00
|
|
|
let elem2 = message::serialise(&message);
|
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-04-29 05:57:20 +00:00
|
|
|
message::parse_message(&elem).unwrap();
|
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-04-29 05:57:20 +00:00
|
|
|
let message = message::Message {
|
2017-04-23 14:13:03 +00:00
|
|
|
from: None,
|
|
|
|
to: Some(Jid::from_str("coucou@example.org").unwrap()),
|
|
|
|
id: None,
|
|
|
|
type_: message::MessageType::Chat,
|
|
|
|
payloads: vec!(
|
|
|
|
message::MessagePayloadType::Parsed(message::MessagePayload::Body("Hello world!".to_owned())),
|
|
|
|
),
|
|
|
|
};
|
2017-04-29 05:57:20 +00:00
|
|
|
let elem2 = message::serialise(&message);
|
2017-04-23 14:13:03 +00:00
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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();
|
|
|
|
let message = message::parse_message(&elem).unwrap();
|
|
|
|
let elem2 = message::serialise(&message);
|
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
|
|
|
}
|