2019-06-26 00:06:38 +00:00
|
|
|
// Copyright (c) 2017 Maxime “pep” Buquet <pep@bouah.net>
|
2017-07-20 19:36:13 +00:00
|
|
|
// Copyright (c) 2017 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
|
2017-06-26 22:07:12 +00:00
|
|
|
//
|
|
|
|
// 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::date::DateTime;
|
2018-12-18 14:32:05 +00:00
|
|
|
use crate::presence::PresencePayload;
|
2018-05-28 16:45:29 +00:00
|
|
|
|
|
|
|
generate_element!(
|
2018-08-08 18:22:37 +00:00
|
|
|
/// Represents the query for messages before our join.
|
2019-02-21 20:00:58 +00:00
|
|
|
#[derive(PartialEq, Default)]
|
2018-05-28 16:45:29 +00:00
|
|
|
History, "history", MUC,
|
|
|
|
attributes: [
|
2018-08-08 18:22:37 +00:00
|
|
|
/// How many characters of history to send, in XML characters.
|
2019-02-24 19:26:40 +00:00
|
|
|
maxchars: Option<u32> = "maxchars",
|
2018-08-08 18:22:37 +00:00
|
|
|
|
|
|
|
/// How many messages to send.
|
2019-02-24 19:26:40 +00:00
|
|
|
maxstanzas: Option<u32> = "maxstanzas",
|
2018-08-08 18:22:37 +00:00
|
|
|
|
|
|
|
/// Only send messages received in these last seconds.
|
2019-02-24 19:26:40 +00:00
|
|
|
seconds: Option<u32> = "seconds",
|
2018-08-08 18:22:37 +00:00
|
|
|
|
|
|
|
/// Only send messages after this date.
|
2019-02-24 19:26:40 +00:00
|
|
|
since: Option<DateTime> = "since",
|
2018-05-28 16:45:29 +00:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2018-11-01 16:25:24 +00:00
|
|
|
impl History {
|
2018-11-02 15:28:40 +00:00
|
|
|
/// Create a new empty history element.
|
2018-11-01 16:25:24 +00:00
|
|
|
pub fn new() -> Self {
|
2019-02-21 20:00:58 +00:00
|
|
|
History::default()
|
2018-11-01 16:25:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 15:28:40 +00:00
|
|
|
/// Set how many characters of history to send.
|
2018-11-01 16:25:24 +00:00
|
|
|
pub fn with_maxchars(mut self, maxchars: u32) -> Self {
|
|
|
|
self.maxchars = Some(maxchars);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-11-02 15:28:40 +00:00
|
|
|
/// Set how many messages to send.
|
2018-11-01 16:25:24 +00:00
|
|
|
pub fn with_maxstanzas(mut self, maxstanzas: u32) -> Self {
|
|
|
|
self.maxstanzas = Some(maxstanzas);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-11-02 15:28:40 +00:00
|
|
|
/// Only send messages received in these last seconds.
|
2018-11-01 16:25:24 +00:00
|
|
|
pub fn with_seconds(mut self, seconds: u32) -> Self {
|
|
|
|
self.seconds = Some(seconds);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-11-02 15:28:40 +00:00
|
|
|
/// Only send messages received since this date.
|
2018-11-01 16:25:24 +00:00
|
|
|
pub fn with_since(mut self, since: DateTime) -> Self {
|
|
|
|
self.since = Some(since);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-28 14:45:13 +00:00
|
|
|
generate_element!(
|
2018-08-08 18:22:37 +00:00
|
|
|
/// Represents a room join request.
|
2019-02-21 20:00:58 +00:00
|
|
|
#[derive(PartialEq, Default)]
|
2018-05-28 14:29:51 +00:00
|
|
|
Muc, "x", MUC, children: [
|
2018-08-08 18:22:37 +00:00
|
|
|
/// Password to use when the room is protected by a password.
|
2018-05-28 16:45:29 +00:00
|
|
|
password: Option<String> = ("password", MUC) => String,
|
2018-08-08 18:22:37 +00:00
|
|
|
|
|
|
|
/// Controls how much and how old we want to receive history on join.
|
2018-05-28 16:45:29 +00:00
|
|
|
history: Option<History> = ("history", MUC) => History
|
2018-05-28 14:29:51 +00:00
|
|
|
]
|
|
|
|
);
|
2017-06-26 22:07:12 +00:00
|
|
|
|
2018-09-20 18:51:48 +00:00
|
|
|
impl PresencePayload for Muc {}
|
|
|
|
|
2018-11-01 16:25:24 +00:00
|
|
|
impl Muc {
|
2018-11-02 15:28:40 +00:00
|
|
|
/// Create a new MUC join element.
|
2018-11-01 16:25:24 +00:00
|
|
|
pub fn new() -> Self {
|
2019-02-21 20:00:58 +00:00
|
|
|
Muc::default()
|
2018-11-01 16:25:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 15:28:40 +00:00
|
|
|
/// Join a room with this password.
|
2018-11-01 16:25:24 +00:00
|
|
|
pub fn with_password(mut self, password: String) -> Self {
|
|
|
|
self.password = Some(password);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2018-11-02 15:28:40 +00:00
|
|
|
/// Join a room with only that much history.
|
2018-11-01 16:25:24 +00:00
|
|
|
pub fn with_history(mut self, history: History) -> Self {
|
|
|
|
self.history = Some(history);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-26 22:07:12 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-01-13 11:39:51 +00:00
|
|
|
use crate::util::error::Error;
|
2019-09-25 08:28:44 +00:00
|
|
|
use crate::Element;
|
2019-04-12 08:58:42 +00:00
|
|
|
use std::convert::TryFrom;
|
2019-10-22 23:32:41 +00:00
|
|
|
use std::str::FromStr;
|
2017-06-26 22:07:12 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_muc_simple() {
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<x xmlns='http://jabber.org/protocol/muc'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-06-26 22:07:12 +00:00
|
|
|
Muc::try_from(elem).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_muc_invalid_child() {
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<x xmlns='http://jabber.org/protocol/muc'><coucou/></x>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-06-26 22:07:12 +00:00
|
|
|
let error = Muc::try_from(elem).unwrap_err();
|
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Unknown child in x element.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_muc_serialise() {
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<x xmlns='http://jabber.org/protocol/muc'/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-06-26 22:07:12 +00:00
|
|
|
let muc = Muc {
|
|
|
|
password: None,
|
2018-05-28 16:45:29 +00:00
|
|
|
history: None,
|
2017-06-26 22:07:12 +00:00
|
|
|
};
|
|
|
|
let elem2 = muc.into();
|
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
|
|
|
|
2019-01-12 21:00:46 +00:00
|
|
|
#[cfg(not(feature = "disable-validation"))]
|
2017-06-26 22:07:12 +00:00
|
|
|
#[test]
|
|
|
|
fn test_muc_invalid_attribute() {
|
2018-12-18 14:32:05 +00:00
|
|
|
let elem: Element = "<x xmlns='http://jabber.org/protocol/muc' coucou=''/>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2017-06-26 22:07:12 +00:00
|
|
|
let error = Muc::try_from(elem).unwrap_err();
|
|
|
|
let message = match error {
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
assert_eq!(message, "Unknown attribute in x element.");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_muc_simple_password() {
|
2019-12-07 19:37:33 +00:00
|
|
|
let elem: Element =
|
|
|
|
"<x xmlns='http://jabber.org/protocol/muc'><password>coucou</password></x>"
|
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2018-05-28 14:24:17 +00:00
|
|
|
let elem1 = elem.clone();
|
2017-06-26 22:07:12 +00:00
|
|
|
let muc = Muc::try_from(elem).unwrap();
|
|
|
|
assert_eq!(muc.password, Some("coucou".to_owned()));
|
2018-05-28 14:24:17 +00:00
|
|
|
|
|
|
|
let elem2 = Element::from(muc);
|
2019-11-29 13:33:17 +00:00
|
|
|
assert_eq!(elem1, elem2);
|
2017-06-26 22:07:12 +00:00
|
|
|
}
|
2018-05-28 16:45:29 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn history() {
|
|
|
|
let elem: Element = "
|
|
|
|
<x xmlns='http://jabber.org/protocol/muc'>
|
|
|
|
<history maxstanzas='0'/>
|
|
|
|
</x>"
|
2018-12-18 14:32:05 +00:00
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2018-05-28 16:45:29 +00:00
|
|
|
let muc = Muc::try_from(elem).unwrap();
|
2018-11-01 16:25:24 +00:00
|
|
|
let muc2 = Muc::new().with_history(History::new().with_maxstanzas(0));
|
|
|
|
assert_eq!(muc, muc2);
|
|
|
|
|
2018-05-28 16:45:29 +00:00
|
|
|
let history = muc.history.unwrap();
|
|
|
|
assert_eq!(history.maxstanzas, Some(0));
|
|
|
|
assert_eq!(history.maxchars, None);
|
|
|
|
assert_eq!(history.seconds, None);
|
|
|
|
assert_eq!(history.since, None);
|
|
|
|
|
|
|
|
let elem: Element = "
|
|
|
|
<x xmlns='http://jabber.org/protocol/muc'>
|
|
|
|
<history since='1970-01-01T00:00:00Z'/>
|
|
|
|
</x>"
|
2018-12-18 14:32:05 +00:00
|
|
|
.parse()
|
|
|
|
.unwrap();
|
2018-05-28 16:45:29 +00:00
|
|
|
let muc = Muc::try_from(elem).unwrap();
|
2018-12-18 14:32:05 +00:00
|
|
|
assert_eq!(
|
|
|
|
muc.history.unwrap().since.unwrap(),
|
|
|
|
DateTime::from_str("1970-01-01T00:00:00+00:00").unwrap()
|
|
|
|
);
|
2018-05-28 16:45:29 +00:00
|
|
|
}
|
2017-06-26 22:07:12 +00:00
|
|
|
}
|