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/.
|
|
|
|
|
|
2018-12-18 14:27:30 +00:00
|
|
|
|
use crate::ns;
|
2019-10-22 23:32:41 +00:00
|
|
|
|
use crate::util::error::Error;
|
2019-09-25 08:28:44 +00:00
|
|
|
|
use crate::Element;
|
2019-10-22 23:32:41 +00:00
|
|
|
|
use jid::Jid;
|
2018-12-18 14:32:05 +00:00
|
|
|
|
use std::collections::BTreeMap;
|
2017-04-23 14:13:03 +00:00
|
|
|
|
|
2018-09-20 18:58:27 +00:00
|
|
|
|
/// Should be implemented on every known payload of a `<message/>`.
|
|
|
|
|
pub trait MessagePayload: TryFrom<Element> + Into<Element> {}
|
2017-05-18 22:06:22 +00:00
|
|
|
|
|
2018-08-08 18:02:17 +00:00
|
|
|
|
generate_attribute!(
|
|
|
|
|
/// The type of a message.
|
|
|
|
|
MessageType, "type", {
|
|
|
|
|
/// Standard instant messaging message.
|
|
|
|
|
Chat => "chat",
|
|
|
|
|
|
|
|
|
|
/// Notifies that an error happened.
|
|
|
|
|
Error => "error",
|
|
|
|
|
|
|
|
|
|
/// Standard group instant messaging message.
|
|
|
|
|
Groupchat => "groupchat",
|
|
|
|
|
|
|
|
|
|
/// Used by servers to notify users when things happen.
|
|
|
|
|
Headline => "headline",
|
|
|
|
|
|
|
|
|
|
/// This is an email-like message, it usually contains a
|
|
|
|
|
/// [subject](struct.Subject.html).
|
|
|
|
|
Normal => "normal",
|
|
|
|
|
}, 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-08-08 18:02:17 +00:00
|
|
|
|
generate_elem_id!(
|
|
|
|
|
/// Represents one `<body/>` element, that is the free form text content of
|
|
|
|
|
/// a message.
|
2018-12-18 14:32:05 +00:00
|
|
|
|
Body,
|
|
|
|
|
"body",
|
|
|
|
|
DEFAULT_NS
|
2018-08-08 18:02:17 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_elem_id!(
|
|
|
|
|
/// Defines the subject of a room, or of an email-like normal message.
|
2018-12-18 14:32:05 +00:00
|
|
|
|
Subject,
|
|
|
|
|
"subject",
|
|
|
|
|
DEFAULT_NS
|
2018-08-08 18:02:17 +00:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
generate_elem_id!(
|
|
|
|
|
/// A thread identifier, so that other people can specify to which message
|
|
|
|
|
/// they are replying.
|
2018-12-18 14:32:05 +00:00
|
|
|
|
Thread,
|
|
|
|
|
"thread",
|
|
|
|
|
DEFAULT_NS
|
2018-08-08 18:02:17 +00:00
|
|
|
|
);
|
2017-05-06 20:38:23 +00:00
|
|
|
|
|
2017-07-20 22:10:13 +00:00
|
|
|
|
/// The main structure representing the `<message/>` stanza.
|
2020-11-29 20:17:51 +00:00
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2017-04-23 14:13:03 +00:00
|
|
|
|
pub struct Message {
|
2018-08-08 18:02:17 +00:00
|
|
|
|
/// The JID emitting this stanza.
|
2017-04-23 14:13:03 +00:00
|
|
|
|
pub from: Option<Jid>,
|
2018-08-08 18:02:17 +00:00
|
|
|
|
|
|
|
|
|
/// The recipient of this stanza.
|
2017-04-23 14:13:03 +00:00
|
|
|
|
pub to: Option<Jid>,
|
2018-08-08 18:02:17 +00:00
|
|
|
|
|
|
|
|
|
/// The @id attribute of this stanza, which is required in order to match a
|
|
|
|
|
/// request with its response.
|
2017-04-23 14:13:03 +00:00
|
|
|
|
pub id: Option<String>,
|
2018-08-08 18:02:17 +00:00
|
|
|
|
|
|
|
|
|
/// The type of this message.
|
2017-04-23 14:13:03 +00:00
|
|
|
|
pub type_: MessageType,
|
2018-08-08 18:02:17 +00:00
|
|
|
|
|
|
|
|
|
/// A list of bodies, sorted per language. Use
|
|
|
|
|
/// [get_best_body()](#method.get_best_body) to access them on reception.
|
2017-05-06 20:38:23 +00:00
|
|
|
|
pub bodies: BTreeMap<Lang, Body>,
|
2018-08-08 18:02:17 +00:00
|
|
|
|
|
|
|
|
|
/// A list of subjects, sorted per language. Use
|
|
|
|
|
/// [get_best_subject()](#method.get_best_subject) to access them on
|
|
|
|
|
/// reception.
|
2017-05-07 14:10:04 +00:00
|
|
|
|
pub subjects: BTreeMap<Lang, Subject>,
|
2018-08-08 18:02:17 +00:00
|
|
|
|
|
|
|
|
|
/// An optional thread identifier, so that other people can reply directly
|
|
|
|
|
/// to this message.
|
2017-05-07 14:09:18 +00:00
|
|
|
|
pub thread: Option<Thread>,
|
2018-08-08 18:02:17 +00:00
|
|
|
|
|
|
|
|
|
/// A list of the extension payloads contained in this stanza.
|
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 {
|
2023-06-03 09:23:32 +00:00
|
|
|
|
/// Creates a new `<message/>` stanza of type Chat for the given recipient.
|
|
|
|
|
/// This is equivalent to the [`Message::chat`] method.
|
2020-12-10 19:45:30 +00:00
|
|
|
|
pub fn new<J: Into<Option<Jid>>>(to: J) -> Message {
|
2017-06-26 18:47:26 +00:00
|
|
|
|
Message {
|
|
|
|
|
from: None,
|
2020-12-10 19:45:30 +00:00
|
|
|
|
to: to.into(),
|
2017-06-26 18:47:26 +00:00
|
|
|
|
id: None,
|
|
|
|
|
type_: MessageType::Chat,
|
|
|
|
|
bodies: BTreeMap::new(),
|
|
|
|
|
subjects: BTreeMap::new(),
|
|
|
|
|
thread: None,
|
2018-12-18 14:32:05 +00:00
|
|
|
|
payloads: vec![],
|
2017-06-26 18:47:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-12 15:22:05 +00:00
|
|
|
|
|
2023-06-03 09:23:32 +00:00
|
|
|
|
/// Creates a new `<message/>` stanza of a certain type for the given recipient.
|
|
|
|
|
pub fn new_with_type<J: Into<Option<Jid>>>(type_: MessageType, to: J) -> Message {
|
|
|
|
|
Message {
|
|
|
|
|
from: None,
|
|
|
|
|
to: to.into(),
|
|
|
|
|
id: None,
|
|
|
|
|
type_,
|
|
|
|
|
bodies: BTreeMap::new(),
|
|
|
|
|
subjects: BTreeMap::new(),
|
|
|
|
|
thread: None,
|
|
|
|
|
payloads: vec![],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a Message of type Chat
|
|
|
|
|
pub fn chat<J: Into<Option<Jid>>>(to: J) -> Message {
|
|
|
|
|
Self::new_with_type(MessageType::Chat, to)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a Message of type Error
|
|
|
|
|
pub fn error<J: Into<Option<Jid>>>(to: J) -> Message {
|
|
|
|
|
Self::new_with_type(MessageType::Error, to)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a Message of type Groupchat
|
|
|
|
|
pub fn groupchat<J: Into<Option<Jid>>>(to: J) -> Message {
|
|
|
|
|
Self::new_with_type(MessageType::Groupchat, to)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a Message of type Headline
|
|
|
|
|
pub fn headline<J: Into<Option<Jid>>>(to: J) -> Message {
|
|
|
|
|
Self::new_with_type(MessageType::Headline, to)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a Message of type Normal
|
|
|
|
|
pub fn normal<J: Into<Option<Jid>>>(to: J) -> Message {
|
|
|
|
|
Self::new_with_type(MessageType::Normal, to)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Appends a body in given lang to the Message
|
|
|
|
|
pub fn with_body(mut self, lang: Lang, body: String) -> Message {
|
|
|
|
|
self.bodies.insert(lang, Body(body));
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-07 14:45:01 +00:00
|
|
|
|
/// Set a payload inside this message.
|
|
|
|
|
pub fn with_payload<P: MessagePayload>(mut self, payload: P) -> Message {
|
|
|
|
|
self.payloads.push(payload.into());
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set the payloads of this message.
|
|
|
|
|
pub fn with_payloads(mut self, payloads: Vec<Element>) -> Message {
|
|
|
|
|
self.payloads = payloads;
|
|
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-18 14:32:05 +00:00
|
|
|
|
fn get_best<'a, T>(
|
|
|
|
|
map: &'a BTreeMap<Lang, T>,
|
|
|
|
|
preferred_langs: Vec<&str>,
|
|
|
|
|
) -> Option<(Lang, &'a T)> {
|
2018-05-12 15:22:05 +00:00
|
|
|
|
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 {
|
2019-04-12 08:58:42 +00:00
|
|
|
|
type Error = 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);
|
2019-02-24 19:48:19 +00:00
|
|
|
|
let from = get_attr!(root, "from", Option);
|
|
|
|
|
let to = get_attr!(root, "to", Option);
|
|
|
|
|
let id = get_attr!(root, "id", Option);
|
|
|
|
|
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;
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let mut payloads = vec![];
|
2017-05-06 20:21:34 +00:00
|
|
|
|
for elem in root.children() {
|
2017-07-29 05:49:02 +00:00
|
|
|
|
if elem.is("body", ns::DEFAULT_NS) {
|
2018-05-14 14:33:47 +00:00
|
|
|
|
check_no_children!(elem, "body");
|
2019-02-24 19:48:19 +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() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"Body element present twice for the same xml:lang.",
|
|
|
|
|
));
|
2017-05-06 20:38:23 +00:00
|
|
|
|
}
|
2017-07-29 05:49:02 +00:00
|
|
|
|
} else if elem.is("subject", ns::DEFAULT_NS) {
|
2018-05-14 14:33:47 +00:00
|
|
|
|
check_no_children!(elem, "subject");
|
2019-02-24 19:48:19 +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() {
|
2018-12-18 14:32:05 +00:00
|
|
|
|
return Err(Error::ParseError(
|
|
|
|
|
"Subject element present twice for the same xml:lang.",
|
|
|
|
|
));
|
2017-05-07 14:10:04 +00:00
|
|
|
|
}
|
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."));
|
|
|
|
|
}
|
2018-05-14 14:33:47 +00:00
|
|
|
|
check_no_children!(elem, "thread");
|
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 {
|
2019-02-21 20:00:58 +00:00
|
|
|
|
from,
|
|
|
|
|
to,
|
|
|
|
|
id,
|
|
|
|
|
type_,
|
|
|
|
|
bodies,
|
|
|
|
|
subjects,
|
|
|
|
|
thread,
|
|
|
|
|
payloads,
|
2017-05-06 20:21:34 +00:00
|
|
|
|
})
|
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 {
|
2020-03-28 12:07:26 +00:00
|
|
|
|
Element::builder("message", ns::DEFAULT_NS)
|
2018-12-18 14:32:05 +00:00
|
|
|
|
.attr("from", message.from)
|
|
|
|
|
.attr("to", message.to)
|
|
|
|
|
.attr("id", message.id)
|
|
|
|
|
.attr("type", message.type_)
|
2019-10-22 23:32:41 +00:00
|
|
|
|
.append_all(message.subjects.into_iter().map(|(lang, subject)| {
|
|
|
|
|
let mut subject = Element::from(subject);
|
|
|
|
|
subject.set_attr(
|
|
|
|
|
"xml:lang",
|
|
|
|
|
match lang.as_ref() {
|
|
|
|
|
"" => None,
|
|
|
|
|
lang => Some(lang),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
subject
|
|
|
|
|
}))
|
|
|
|
|
.append_all(message.bodies.into_iter().map(|(lang, body)| {
|
|
|
|
|
let mut body = Element::from(body);
|
|
|
|
|
body.set_attr(
|
|
|
|
|
"xml:lang",
|
|
|
|
|
match lang.as_ref() {
|
|
|
|
|
"" => None,
|
|
|
|
|
lang => Some(lang),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
body
|
|
|
|
|
}))
|
2023-12-15 19:15:35 +00:00
|
|
|
|
.append_all(message.payloads)
|
2018-12-18 14:32:05 +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-12-18 14:32:05 +00:00
|
|
|
|
use std::str::FromStr;
|
2017-04-23 14:13:03 +00:00
|
|
|
|
|
2018-10-28 12:10:48 +00:00
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(MessageType, 1);
|
|
|
|
|
assert_size!(Body, 12);
|
|
|
|
|
assert_size!(Subject, 12);
|
|
|
|
|
assert_size!(Thread, 12);
|
2023-06-20 12:14:15 +00:00
|
|
|
|
assert_size!(Message, 104);
|
2018-10-28 12:10:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
2018-10-26 12:26:16 +00:00
|
|
|
|
#[test]
|
|
|
|
|
fn test_size() {
|
|
|
|
|
assert_size!(MessageType, 1);
|
|
|
|
|
assert_size!(Body, 24);
|
|
|
|
|
assert_size!(Subject, 24);
|
|
|
|
|
assert_size!(Thread, 24);
|
2023-06-20 12:08:58 +00:00
|
|
|
|
assert_size!(Message, 208);
|
2018-10-26 12:26:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
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")]
|
2018-12-18 14:32:05 +00:00
|
|
|
|
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")]
|
2018-12-18 14:32:05 +00:00
|
|
|
|
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
|
|
|
|
{
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let (lang, body) = message.get_best_body(vec!["en"]).unwrap();
|
2018-05-12 15:22:05 +00:00
|
|
|
|
assert_eq!(lang, "");
|
|
|
|
|
assert_eq!(body, &Body::from_str("Hello world!").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem2 = message.into();
|
2019-11-29 13:33:17 +00:00
|
|
|
|
assert_eq!(elem1, 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();
|
2023-06-20 12:07:50 +00:00
|
|
|
|
let mut message = Message::new(Jid::new("coucou@example.org").unwrap());
|
2018-12-18 14:32:05 +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();
|
2019-11-29 13:33:17 +00:00
|
|
|
|
assert_eq!(elem, 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();
|
2018-12-18 14:32:05 +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
|
|
|
|
{
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let (lang, subject) = message.get_best_subject(vec!["en"]).unwrap();
|
2018-05-12 15:22:05 +00:00
|
|
|
|
assert_eq!(lang, "");
|
|
|
|
|
assert_eq!(subject, &Subject::from_str("Hello world!").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem2 = message.into();
|
2019-11-29 13:33:17 +00:00
|
|
|
|
assert_eq!(elem1, 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.
|
|
|
|
|
{
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let (lang, body) = message.get_best_body(vec!["fr"]).unwrap();
|
2018-05-12 15:22:05 +00:00
|
|
|
|
assert_eq!(lang, "fr");
|
|
|
|
|
assert_eq!(body, &Body::from_str("Salut le monde !").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests order.
|
|
|
|
|
{
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let (lang, body) = message.get_best_body(vec!["en", "de"]).unwrap();
|
2018-05-12 15:22:05 +00:00
|
|
|
|
assert_eq!(lang, "de");
|
|
|
|
|
assert_eq!(body, &Body::from_str("Hallo Welt!").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests fallback.
|
|
|
|
|
{
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let (lang, body) = message.get_best_body(vec![]).unwrap();
|
2018-05-12 15:22:05 +00:00
|
|
|
|
assert_eq!(lang, "");
|
|
|
|
|
assert_eq!(body, &Body::from_str("Hello world!").unwrap());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tests fallback.
|
|
|
|
|
{
|
2018-12-18 14:32:05 +00:00
|
|
|
|
let (lang, body) = message.get_best_body(vec!["ja"]).unwrap();
|
2018-05-12 15:22:05 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|