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-07-20 19:03:15 +00:00
|
|
|
|
use try_from::TryFrom;
|
2017-05-06 20:38:23 +00:00
|
|
|
|
use std::collections::BTreeMap;
|
2017-04-23 14:13:03 +00:00
|
|
|
|
|
2018-05-14 14:12:56 +00:00
|
|
|
|
use minidom::Element;
|
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-07-29 03:00:25 +00:00
|
|
|
|
use receipts::{Request as ReceiptRequest, Received as ReceiptReceived};
|
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-07-29 02:51:41 +00:00
|
|
|
|
use stanza_id::{StanzaId, OriginId};
|
2017-05-19 01:58:35 +00:00
|
|
|
|
use mam::Result_ as MamResult;
|
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-07-29 03:00:25 +00:00
|
|
|
|
ReceiptRequest(ReceiptRequest),
|
|
|
|
|
ReceiptReceived(ReceiptReceived),
|
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-05-18 22:09:30 +00:00
|
|
|
|
StanzaId(StanzaId),
|
2017-07-29 02:51:41 +00:00
|
|
|
|
OriginId(OriginId),
|
2017-05-19 01:58:35 +00:00
|
|
|
|
MamResult(MamResult),
|
2017-05-19 01:09:23 +00:00
|
|
|
|
|
|
|
|
|
Unknown(Element),
|
2017-04-23 14:13:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
impl TryFrom<Element> for MessagePayload {
|
2017-07-20 19:03:15 +00:00
|
|
|
|
type Err = Error;
|
2017-05-18 22:06:22 +00:00
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
fn try_from(elem: Element) -> Result<MessagePayload, Error> {
|
2017-05-18 22:06:22 +00:00
|
|
|
|
Ok(match (elem.name().as_ref(), elem.ns().unwrap().as_ref()) {
|
2017-07-29 05:49:02 +00:00
|
|
|
|
("error", ns::DEFAULT_NS) => MessagePayload::StanzaError(StanzaError::try_from(elem)?),
|
2017-05-18 22:06:22 +00:00
|
|
|
|
|
|
|
|
|
// XEP-0085
|
2017-05-18 22:14:07 +00:00
|
|
|
|
("active", ns::CHATSTATES)
|
|
|
|
|
| ("inactive", ns::CHATSTATES)
|
|
|
|
|
| ("composing", ns::CHATSTATES)
|
|
|
|
|
| ("paused", ns::CHATSTATES)
|
|
|
|
|
| ("gone", ns::CHATSTATES) => MessagePayload::ChatState(ChatState::try_from(elem)?),
|
2017-05-18 22:06:22 +00:00
|
|
|
|
|
|
|
|
|
// XEP-0184
|
2017-07-29 03:00:25 +00:00
|
|
|
|
("request", ns::RECEIPTS) => MessagePayload::ReceiptRequest(ReceiptRequest::try_from(elem)?),
|
|
|
|
|
("received", ns::RECEIPTS) => MessagePayload::ReceiptReceived(ReceiptReceived::try_from(elem)?),
|
2017-05-18 22:06:22 +00:00
|
|
|
|
|
|
|
|
|
// XEP-0203
|
|
|
|
|
("delay", ns::DELAY) => MessagePayload::Delay(Delay::try_from(elem)?),
|
|
|
|
|
|
|
|
|
|
// XEP-0224
|
|
|
|
|
("attention", ns::ATTENTION) => MessagePayload::Attention(Attention::try_from(elem)?),
|
|
|
|
|
|
|
|
|
|
// XEP-0308
|
|
|
|
|
("replace", ns::MESSAGE_CORRECT) => MessagePayload::MessageCorrect(Replace::try_from(elem)?),
|
|
|
|
|
|
2017-05-19 01:58:35 +00:00
|
|
|
|
// XEP-0313
|
|
|
|
|
("result", ns::MAM) => MessagePayload::MamResult(MamResult::try_from(elem)?),
|
|
|
|
|
|
2017-05-18 22:06:22 +00:00
|
|
|
|
// XEP-0359
|
2017-07-29 02:51:41 +00:00
|
|
|
|
("stanza-id", ns::SID) => MessagePayload::StanzaId(StanzaId::try_from(elem)?),
|
|
|
|
|
("origin-id", ns::SID) => MessagePayload::OriginId(OriginId::try_from(elem)?),
|
2017-05-18 22:06:22 +00:00
|
|
|
|
|
|
|
|
|
// XEP-0380
|
|
|
|
|
("encryption", ns::EME) => MessagePayload::ExplicitMessageEncryption(ExplicitMessageEncryption::try_from(elem)?),
|
|
|
|
|
|
2017-05-24 20:32:04 +00:00
|
|
|
|
_ => MessagePayload::Unknown(elem),
|
2017-05-18 22:06:22 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
|
impl From<MessagePayload> for Element {
|
|
|
|
|
fn from(payload: MessagePayload) -> Element {
|
|
|
|
|
match payload {
|
2017-05-23 22:31:33 +00:00
|
|
|
|
MessagePayload::StanzaError(stanza_error) => stanza_error.into(),
|
|
|
|
|
MessagePayload::Attention(attention) => attention.into(),
|
|
|
|
|
MessagePayload::ChatState(chatstate) => chatstate.into(),
|
2017-07-29 03:00:25 +00:00
|
|
|
|
MessagePayload::ReceiptRequest(request) => request.into(),
|
|
|
|
|
MessagePayload::ReceiptReceived(received) => received.into(),
|
2017-05-23 22:31:33 +00:00
|
|
|
|
MessagePayload::Delay(delay) => delay.into(),
|
|
|
|
|
MessagePayload::MessageCorrect(replace) => replace.into(),
|
|
|
|
|
MessagePayload::ExplicitMessageEncryption(eme) => eme.into(),
|
|
|
|
|
MessagePayload::StanzaId(stanza_id) => stanza_id.into(),
|
2017-07-29 02:51:41 +00:00
|
|
|
|
MessagePayload::OriginId(origin_id) => origin_id.into(),
|
2017-05-23 22:31:33 +00:00
|
|
|
|
MessagePayload::MamResult(result) => result.into(),
|
|
|
|
|
|
2017-05-24 20:40:11 +00:00
|
|
|
|
MessagePayload::Unknown(elem) => elem,
|
2017-05-18 22:06:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 23:50:57 +00:00
|
|
|
|
generate_attribute!(MessageType, "type", {
|
|
|
|
|
Chat => "chat",
|
|
|
|
|
Error => "error",
|
|
|
|
|
Groupchat => "groupchat",
|
|
|
|
|
Headline => "headline",
|
|
|
|
|
Normal => "normal",
|
2017-06-13 23:53:18 +00:00
|
|
|
|
}, Default = Normal);
|
2017-04-23 14:13:03 +00:00
|
|
|
|
|
2017-05-06 20:38:23 +00:00
|
|
|
|
type Lang = String;
|
2017-07-29 05:11:48 +00:00
|
|
|
|
|
2018-05-14 14:30:28 +00:00
|
|
|
|
generate_elem_id!(Body, "body", DEFAULT_NS);
|
|
|
|
|
generate_elem_id!(Subject, "subject", DEFAULT_NS);
|
|
|
|
|
generate_elem_id!(Thread, "thread", DEFAULT_NS);
|
2017-05-06 20:38:23 +00:00
|
|
|
|
|
2017-07-20 22:10:13 +00:00
|
|
|
|
/// The main structure representing the `<message/>` stanza.
|
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-05-07 14:09:18 +00:00
|
|
|
|
pub thread: Option<Thread>,
|
2017-05-18 22:06:22 +00:00
|
|
|
|
pub payloads: Vec<Element>,
|
2017-04-23 14:13:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-26 18:47:26 +00:00
|
|
|
|
impl Message {
|
|
|
|
|
pub fn new(to: Option<Jid>) -> Message {
|
|
|
|
|
Message {
|
|
|
|
|
from: None,
|
|
|
|
|
to: to,
|
|
|
|
|
id: None,
|
|
|
|
|
type_: MessageType::Chat,
|
|
|
|
|
bodies: BTreeMap::new(),
|
|
|
|
|
subjects: BTreeMap::new(),
|
|
|
|
|
thread: None,
|
|
|
|
|
payloads: vec!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-12 15:22:05 +00:00
|
|
|
|
|
|
|
|
|
fn get_best<'a, T>(map: &'a BTreeMap<Lang, T>, preferred_langs: Vec<&str>) -> Option<(Lang, &'a T)> {
|
|
|
|
|
if map.is_empty() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
for lang in preferred_langs {
|
2018-05-12 15:42:49 +00:00
|
|
|
|
if let Some(value) = map.get(lang) {
|
|
|
|
|
return Some((Lang::from(lang), value));
|
2018-05-12 15:22:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-12 15:42:49 +00:00
|
|
|
|
if let Some(value) = map.get("") {
|
|
|
|
|
return Some((Lang::new(), value));
|
2018-05-12 15:22:05 +00:00
|
|
|
|
}
|
2018-05-12 15:42:49 +00:00
|
|
|
|
map.iter().map(|(lang, value)| (lang.clone(), value)).next()
|
2018-05-12 15:22:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the best matching body from a list of languages.
|
|
|
|
|
///
|
|
|
|
|
/// For instance, if a message contains both an xml:lang='de', an xml:lang='fr' and an English
|
|
|
|
|
/// body without an xml:lang attribute, and you pass ["fr", "en"] as your preferred languages,
|
|
|
|
|
/// `Some(("fr", the_second_body))` will be returned.
|
|
|
|
|
///
|
|
|
|
|
/// If no body matches, an undefined body will be returned.
|
|
|
|
|
pub fn get_best_body(&self, preferred_langs: Vec<&str>) -> Option<(Lang, &Body)> {
|
|
|
|
|
Message::get_best::<Body>(&self.bodies, preferred_langs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the best matching subject from a list of languages.
|
|
|
|
|
///
|
|
|
|
|
/// For instance, if a message contains both an xml:lang='de', an xml:lang='fr' and an English
|
|
|
|
|
/// subject without an xml:lang attribute, and you pass ["fr", "en"] as your preferred
|
|
|
|
|
/// languages, `Some(("fr", the_second_subject))` will be returned.
|
|
|
|
|
///
|
|
|
|
|
/// If no subject matches, an undefined subject will be returned.
|
|
|
|
|
pub fn get_best_subject(&self, preferred_langs: Vec<&str>) -> Option<(Lang, &Subject)> {
|
|
|
|
|
Message::get_best::<Subject>(&self.subjects, preferred_langs)
|
|
|
|
|
}
|
2017-06-26 18:47:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
impl TryFrom<Element> for Message {
|
2017-07-20 19:03:15 +00:00
|
|
|
|
type Err = Error;
|
2017-05-06 20:21:34 +00:00
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
fn try_from(root: Element) -> Result<Message, Error> {
|
2018-05-14 14:30:28 +00:00
|
|
|
|
check_self!(root, "message", DEFAULT_NS);
|
2017-05-22 18:00:04 +00:00
|
|
|
|
let from = get_attr!(root, "from", optional);
|
|
|
|
|
let to = get_attr!(root, "to", optional);
|
|
|
|
|
let id = get_attr!(root, "id", optional);
|
|
|
|
|
let type_ = get_attr!(root, "type", default);
|
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-07 14:09:18 +00:00
|
|
|
|
let mut thread = None;
|
2017-05-06 20:21:34 +00:00
|
|
|
|
let mut payloads = vec!();
|
|
|
|
|
for elem in root.children() {
|
2017-07-29 05:49:02 +00:00
|
|
|
|
if elem.is("body", ns::DEFAULT_NS) {
|
2017-05-06 20:38:23 +00:00
|
|
|
|
for _ in elem.children() {
|
|
|
|
|
return Err(Error::ParseError("Unknown child in body element."));
|
|
|
|
|
}
|
2017-08-27 00:21:07 +00:00
|
|
|
|
let lang = get_attr!(elem, "xml:lang", default);
|
2017-07-29 05:11:48 +00:00
|
|
|
|
let body = Body(elem.text());
|
|
|
|
|
if bodies.insert(lang, body).is_some() {
|
2017-05-06 20:38:23 +00:00
|
|
|
|
return Err(Error::ParseError("Body element present twice for the same xml:lang."));
|
|
|
|
|
}
|
2017-07-29 05:49:02 +00:00
|
|
|
|
} else if elem.is("subject", ns::DEFAULT_NS) {
|
2017-05-07 14:10:04 +00:00
|
|
|
|
for _ in elem.children() {
|
|
|
|
|
return Err(Error::ParseError("Unknown child in subject element."));
|
|
|
|
|
}
|
2017-08-27 00:21:07 +00:00
|
|
|
|
let lang = get_attr!(elem, "xml:lang", default);
|
2017-07-29 05:11:48 +00:00
|
|
|
|
let subject = Subject(elem.text());
|
|
|
|
|
if subjects.insert(lang, subject).is_some() {
|
2017-05-07 14:10:04 +00:00
|
|
|
|
return Err(Error::ParseError("Subject element present twice for the same xml:lang."));
|
|
|
|
|
}
|
2017-07-29 05:49:02 +00:00
|
|
|
|
} else if elem.is("thread", ns::DEFAULT_NS) {
|
2017-05-07 14:09:18 +00:00
|
|
|
|
if thread.is_some() {
|
|
|
|
|
return Err(Error::ParseError("Thread element present twice."));
|
|
|
|
|
}
|
|
|
|
|
for _ in elem.children() {
|
|
|
|
|
return Err(Error::ParseError("Unknown child in thread element."));
|
|
|
|
|
}
|
2017-07-29 05:11:48 +00:00
|
|
|
|
thread = Some(Thread(elem.text()));
|
2017-05-06 20:21:34 +00:00
|
|
|
|
} else {
|
2017-05-18 22:06:22 +00:00
|
|
|
|
payloads.push(elem.clone())
|
2017-05-06 20:38:23 +00:00
|
|
|
|
}
|
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-07 14:09:18 +00:00
|
|
|
|
thread: thread,
|
2017-05-06 20:21:34 +00:00
|
|
|
|
payloads: payloads,
|
|
|
|
|
})
|
2017-04-23 14:13:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
|
impl From<Message> for Element {
|
|
|
|
|
fn from(message: Message) -> Element {
|
2017-05-24 20:40:11 +00:00
|
|
|
|
Element::builder("message")
|
2017-07-29 05:49:02 +00:00
|
|
|
|
.ns(ns::DEFAULT_NS)
|
2017-07-29 05:28:20 +00:00
|
|
|
|
.attr("from", message.from)
|
|
|
|
|
.attr("to", message.to)
|
2017-07-20 19:36:13 +00:00
|
|
|
|
.attr("id", message.id)
|
|
|
|
|
.attr("type", message.type_)
|
2017-07-20 22:09:22 +00:00
|
|
|
|
.append(message.subjects.into_iter()
|
2017-07-20 19:36:13 +00:00
|
|
|
|
.map(|(lang, subject)| {
|
2017-07-29 05:11:48 +00:00
|
|
|
|
let mut subject = Element::from(subject);
|
|
|
|
|
subject.set_attr("xml:lang", match lang.as_ref() {
|
|
|
|
|
"" => None,
|
|
|
|
|
lang => Some(lang),
|
|
|
|
|
});
|
|
|
|
|
subject
|
|
|
|
|
})
|
2017-07-20 19:36:13 +00:00
|
|
|
|
.collect::<Vec<_>>())
|
2017-07-20 22:09:22 +00:00
|
|
|
|
.append(message.bodies.into_iter()
|
2017-07-20 19:36:13 +00:00
|
|
|
|
.map(|(lang, body)| {
|
2017-07-29 05:11:48 +00:00
|
|
|
|
let mut body = Element::from(body);
|
|
|
|
|
body.set_attr("xml:lang", match lang.as_ref() {
|
|
|
|
|
"" => None,
|
|
|
|
|
lang => Some(lang),
|
|
|
|
|
});
|
|
|
|
|
body
|
|
|
|
|
})
|
2017-07-20 19:36:13 +00:00
|
|
|
|
.collect::<Vec<_>>())
|
|
|
|
|
.append(message.payloads)
|
2017-05-24 20:40:11 +00:00
|
|
|
|
.build()
|
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::*;
|
2018-05-14 14:11:22 +00:00
|
|
|
|
use std::str::FromStr;
|
2017-08-18 23:04:18 +00:00
|
|
|
|
use compare_elements::NamespaceAwareCompare;
|
2017-04-23 14:13:03 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_simple() {
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-23 14:13:03 +00:00
|
|
|
|
let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
let elem: Element = "<message xmlns='jabber:component:accept'/>".parse().unwrap();
|
2017-05-23 22:31:33 +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() {
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2017-06-14 00:57:02 +00:00
|
|
|
|
let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
let elem: Element = "<message xmlns='jabber:component:accept'/>".parse().unwrap();
|
2017-06-26 18:47:26 +00:00
|
|
|
|
let mut message = Message::new(None);
|
|
|
|
|
message.type_ = MessageType::Normal;
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem2 = message.into();
|
2017-04-23 14:13:03 +00:00
|
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_body() {
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-23 14:13:03 +00:00
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
let elem: Element = "<message xmlns='jabber:component:accept' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem1 = elem.clone();
|
|
|
|
|
let message = Message::try_from(elem).unwrap();
|
2017-07-29 05:11:48 +00:00
|
|
|
|
assert_eq!(message.bodies[""], Body::from_str("Hello world!").unwrap());
|
2017-05-07 14:06:11 +00:00
|
|
|
|
|
2018-05-12 15:22:05 +00:00
|
|
|
|
{
|
|
|
|
|
let (lang, body) = message.get_best_body(vec!("en")).unwrap();
|
|
|
|
|
assert_eq!(lang, "");
|
|
|
|
|
assert_eq!(body, &Body::from_str("Hello world!").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem2 = message.into();
|
2017-08-18 23:04:18 +00:00
|
|
|
|
assert!(elem1.compare_to(&elem2));
|
2017-04-23 14:13:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_serialise_body() {
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-23 14:13:03 +00:00
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
let elem: Element = "<message xmlns='jabber:component:accept' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
2017-06-26 18:47:26 +00:00
|
|
|
|
let mut message = Message::new(Some(Jid::from_str("coucou@example.org").unwrap()));
|
2017-07-29 05:11:48 +00:00
|
|
|
|
message.bodies.insert(String::from(""), Body::from_str("Hello world!").unwrap());
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem2 = message.into();
|
2017-08-18 23:04:18 +00:00
|
|
|
|
assert!(elem.compare_to(&elem2));
|
2017-04-23 14:13:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-07 14:10:04 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_subject() {
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2017-05-07 14:10:04 +00:00
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><subject>Hello world!</subject></message>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
let elem: Element = "<message xmlns='jabber:component:accept' to='coucou@example.org' type='chat'><subject>Hello world!</subject></message>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem1 = elem.clone();
|
|
|
|
|
let message = Message::try_from(elem).unwrap();
|
2017-07-29 05:11:48 +00:00
|
|
|
|
assert_eq!(message.subjects[""], Subject::from_str("Hello world!").unwrap());
|
2017-05-07 14:10:04 +00:00
|
|
|
|
|
2018-05-12 15:22:05 +00:00
|
|
|
|
{
|
|
|
|
|
let (lang, subject) = message.get_best_subject(vec!("en")).unwrap();
|
|
|
|
|
assert_eq!(lang, "");
|
|
|
|
|
assert_eq!(subject, &Subject::from_str("Hello world!").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem2 = message.into();
|
2017-08-18 23:04:18 +00:00
|
|
|
|
assert!(elem1.compare_to(&elem2));
|
2017-05-07 14:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-12 15:22:05 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn get_best_body() {
|
|
|
|
|
#[cfg(not(feature = "component"))]
|
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body xml:lang='de'>Hallo Welt!</body><body xml:lang='fr'>Salut le monde !</body><body>Hello world!</body></message>".parse().unwrap();
|
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
let elem: Element = "<message xmlns='jabber:component:accept' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap();
|
|
|
|
|
let message = Message::try_from(elem).unwrap();
|
|
|
|
|
|
|
|
|
|
// Tests basic feature.
|
|
|
|
|
{
|
|
|
|
|
let (lang, body) = message.get_best_body(vec!("fr")).unwrap();
|
|
|
|
|
assert_eq!(lang, "fr");
|
|
|
|
|
assert_eq!(body, &Body::from_str("Salut le monde !").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests order.
|
|
|
|
|
{
|
|
|
|
|
let (lang, body) = message.get_best_body(vec!("en", "de")).unwrap();
|
|
|
|
|
assert_eq!(lang, "de");
|
|
|
|
|
assert_eq!(body, &Body::from_str("Hallo Welt!").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests fallback.
|
|
|
|
|
{
|
|
|
|
|
let (lang, body) = message.get_best_body(vec!()).unwrap();
|
|
|
|
|
assert_eq!(lang, "");
|
|
|
|
|
assert_eq!(body, &Body::from_str("Hello world!").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests fallback.
|
|
|
|
|
{
|
|
|
|
|
let (lang, body) = message.get_best_body(vec!("ja")).unwrap();
|
|
|
|
|
assert_eq!(lang, "");
|
|
|
|
|
assert_eq!(body, &Body::from_str("Hello world!").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let message = Message::new(None);
|
|
|
|
|
|
|
|
|
|
// Tests without a body.
|
|
|
|
|
assert_eq!(message.get_best_body(vec!("ja")), None);
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-23 14:13:03 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_attention() {
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-23 14:13:03 +00:00
|
|
|
|
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><attention xmlns='urn:xmpp:attention:0'/></message>".parse().unwrap();
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
let elem: Element = "<message xmlns='jabber:component:accept' to='coucou@example.org' type='chat'><attention xmlns='urn:xmpp:attention:0'/></message>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem1 = elem.clone();
|
|
|
|
|
let message = Message::try_from(elem).unwrap();
|
|
|
|
|
let elem2 = message.into();
|
|
|
|
|
assert_eq!(elem1, elem2);
|
2017-04-23 14:13:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|