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 19:14:45 +00:00
|
|
|
|
|
2018-05-14 14:12:56 +00:00
|
|
|
|
use minidom::Element;
|
2017-04-29 05:07:00 +00:00
|
|
|
|
use jid::Jid;
|
|
|
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
|
|
2018-09-20 18:58:27 +00:00
|
|
|
|
use message::MessagePayload;
|
2018-05-16 13:08:17 +00:00
|
|
|
|
use iq::{IqGetPayload, IqSetPayload, IqResultPayload};
|
2017-04-29 05:07:00 +00:00
|
|
|
|
use data_forms::DataForm;
|
2018-08-02 18:35:15 +00:00
|
|
|
|
use rsm::{SetQuery, SetResult};
|
2017-04-29 05:07:00 +00:00
|
|
|
|
use forwarding::Forwarded;
|
2018-08-02 18:05:35 +00:00
|
|
|
|
use pubsub::NodeName;
|
2017-04-29 05:07:00 +00:00
|
|
|
|
|
|
|
|
|
use ns;
|
|
|
|
|
|
2018-08-02 18:05:35 +00:00
|
|
|
|
generate_id!(
|
|
|
|
|
/// An identifier matching a result message to the query requesting it.
|
|
|
|
|
QueryId
|
|
|
|
|
);
|
|
|
|
|
|
2018-05-28 14:45:13 +00:00
|
|
|
|
generate_element!(
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// Starts a query to the archive.
|
2018-05-28 14:29:51 +00:00
|
|
|
|
Query, "query", MAM,
|
|
|
|
|
attributes: [
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// An optional identifier for matching forwarded messages to this
|
|
|
|
|
/// query.
|
2018-08-02 18:05:35 +00:00
|
|
|
|
queryid: Option<QueryId> = "queryid" => optional,
|
2018-08-02 18:05:51 +00:00
|
|
|
|
|
|
|
|
|
/// Must be set to Some when querying a PubSub node’s archive.
|
2018-08-02 18:05:35 +00:00
|
|
|
|
node: Option<NodeName> = "node" => optional
|
2018-05-28 14:29:51 +00:00
|
|
|
|
],
|
|
|
|
|
children: [
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// Used for filtering the results.
|
2018-05-28 14:29:51 +00:00
|
|
|
|
form: Option<DataForm> = ("x", DATA_FORMS) => DataForm,
|
2018-08-02 18:05:51 +00:00
|
|
|
|
|
|
|
|
|
/// Used for paging through results.
|
2018-08-02 18:35:15 +00:00
|
|
|
|
set: Option<SetQuery> = ("set", RSM) => SetQuery
|
2018-05-28 14:29:51 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
2017-04-29 05:07:00 +00:00
|
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
|
impl IqGetPayload for Query {}
|
|
|
|
|
impl IqSetPayload for Query {}
|
|
|
|
|
impl IqResultPayload for Query {}
|
|
|
|
|
|
2018-05-28 14:45:13 +00:00
|
|
|
|
generate_element!(
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// The wrapper around forwarded stanzas.
|
2018-05-15 00:06:38 +00:00
|
|
|
|
Result_, "result", MAM,
|
|
|
|
|
attributes: [
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// The stanza-id under which the archive stored this stanza.
|
2018-05-15 00:06:38 +00:00
|
|
|
|
id: String = "id" => required,
|
2018-08-02 18:05:51 +00:00
|
|
|
|
|
|
|
|
|
/// The same queryid as the one requested in the
|
|
|
|
|
/// [query](struct.Query.html).
|
2018-08-02 18:05:35 +00:00
|
|
|
|
queryid: Option<QueryId> = "queryid" => optional,
|
2018-05-15 00:06:38 +00:00
|
|
|
|
],
|
2018-05-28 14:29:51 +00:00
|
|
|
|
children: [
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// The actual stanza being forwarded.
|
2018-05-28 14:29:51 +00:00
|
|
|
|
forwarded: Required<Forwarded> = ("forwarded", FORWARD) => Forwarded
|
|
|
|
|
]
|
2018-05-15 00:06:38 +00:00
|
|
|
|
);
|
2017-04-29 05:07:00 +00:00
|
|
|
|
|
2018-09-20 18:58:27 +00:00
|
|
|
|
impl MessagePayload for Result_ {}
|
|
|
|
|
|
2018-05-28 14:29:51 +00:00
|
|
|
|
generate_attribute!(
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// True when the end of a MAM query has been reached.
|
2018-05-28 14:29:51 +00:00
|
|
|
|
Complete, "complete", bool
|
|
|
|
|
);
|
|
|
|
|
|
2018-05-28 14:45:13 +00:00
|
|
|
|
generate_element!(
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// Notes the end of a page in a query.
|
2018-05-28 14:29:51 +00:00
|
|
|
|
Fin, "fin", MAM,
|
|
|
|
|
attributes: [
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// True when the end of a MAM query has been reached.
|
2018-05-28 14:29:51 +00:00
|
|
|
|
complete: Complete = "complete" => default
|
|
|
|
|
],
|
|
|
|
|
children: [
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// Describes the current page, it should contain at least [first]
|
|
|
|
|
/// (with an [index]) and [last], and generally [count].
|
|
|
|
|
///
|
2018-08-02 18:35:15 +00:00
|
|
|
|
/// [first]: ../rsm/struct.SetResult.html#structfield.first
|
|
|
|
|
/// [index]: ../rsm/struct.SetResult.html#structfield.first_index
|
|
|
|
|
/// [last]: ../rsm/struct.SetResult.html#structfield.last
|
|
|
|
|
/// [count]: ../rsm/struct.SetResult.html#structfield.count
|
|
|
|
|
set: Required<SetResult> = ("set", RSM) => SetResult
|
2018-05-28 14:29:51 +00:00
|
|
|
|
]
|
|
|
|
|
);
|
2017-04-29 05:07:00 +00:00
|
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
|
impl IqResultPayload for Fin {}
|
|
|
|
|
|
2018-08-02 18:05:51 +00:00
|
|
|
|
generate_attribute!(
|
|
|
|
|
/// Notes the default archiving preference for the user.
|
|
|
|
|
DefaultPrefs, "default", {
|
|
|
|
|
/// The default is to always log messages in the archive.
|
|
|
|
|
Always => "always",
|
|
|
|
|
|
|
|
|
|
/// The default is to never log messages in the archive.
|
|
|
|
|
Never => "never",
|
|
|
|
|
|
|
|
|
|
/// The default is to log messages in the archive only for contacts
|
|
|
|
|
/// present in the user’s [roster](../roster/index.html).
|
|
|
|
|
Roster => "roster",
|
|
|
|
|
}
|
|
|
|
|
);
|
2017-05-21 19:45:50 +00:00
|
|
|
|
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// Controls the archiving preferences of the user.
|
2017-04-29 05:07:00 +00:00
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
|
pub struct Prefs {
|
2018-08-02 18:05:51 +00:00
|
|
|
|
/// The default preference for JIDs in neither
|
|
|
|
|
/// [always](#structfield.always) or [never](#structfield.never) lists.
|
2017-05-21 19:45:50 +00:00
|
|
|
|
pub default_: DefaultPrefs,
|
2018-08-02 18:05:51 +00:00
|
|
|
|
|
|
|
|
|
/// The set of JIDs for which to always store messages in the archive.
|
2017-04-29 05:07:00 +00:00
|
|
|
|
pub always: Vec<Jid>,
|
2018-08-02 18:05:51 +00:00
|
|
|
|
|
|
|
|
|
/// The set of JIDs for which to never store messages in the archive.
|
2017-04-29 05:07:00 +00:00
|
|
|
|
pub never: Vec<Jid>,
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 13:08:17 +00:00
|
|
|
|
impl IqGetPayload for Prefs {}
|
|
|
|
|
impl IqSetPayload for Prefs {}
|
|
|
|
|
impl IqResultPayload for Prefs {}
|
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
impl TryFrom<Element> for Prefs {
|
2017-07-20 19:03:15 +00:00
|
|
|
|
type Err = Error;
|
2017-05-06 20:08:44 +00:00
|
|
|
|
|
2017-05-23 22:31:33 +00:00
|
|
|
|
fn try_from(elem: Element) -> Result<Prefs, Error> {
|
2018-05-14 14:30:28 +00:00
|
|
|
|
check_self!(elem, "prefs", MAM);
|
2017-10-31 21:10:04 +00:00
|
|
|
|
check_no_unknown_attributes!(elem, "prefs", ["default"]);
|
2017-05-06 20:08:44 +00:00
|
|
|
|
let mut always = vec!();
|
|
|
|
|
let mut never = vec!();
|
|
|
|
|
for child in elem.children() {
|
|
|
|
|
if child.is("always", ns::MAM) {
|
|
|
|
|
for jid_elem in child.children() {
|
|
|
|
|
if !jid_elem.is("jid", ns::MAM) {
|
|
|
|
|
return Err(Error::ParseError("Invalid jid element in always."));
|
|
|
|
|
}
|
|
|
|
|
always.push(jid_elem.text().parse()?);
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
2017-05-06 20:08:44 +00:00
|
|
|
|
} else if child.is("never", ns::MAM) {
|
|
|
|
|
for jid_elem in child.children() {
|
|
|
|
|
if !jid_elem.is("jid", ns::MAM) {
|
|
|
|
|
return Err(Error::ParseError("Invalid jid element in never."));
|
|
|
|
|
}
|
|
|
|
|
never.push(jid_elem.text().parse()?);
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
2017-05-06 20:08:44 +00:00
|
|
|
|
} else {
|
|
|
|
|
return Err(Error::ParseError("Unknown child in prefs element."));
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-21 19:45:50 +00:00
|
|
|
|
let default_ = get_attr!(elem, "default", required);
|
2017-05-06 20:08:44 +00:00
|
|
|
|
Ok(Prefs { default_, always, never })
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-31 21:10:04 +00:00
|
|
|
|
fn serialise_jid_list(name: &str, jids: Vec<Jid>) -> Option<Element> {
|
|
|
|
|
if jids.is_empty() {
|
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(Element::builder(name)
|
|
|
|
|
.ns(ns::MAM)
|
|
|
|
|
.append(jids.into_iter()
|
|
|
|
|
.map(|jid| Element::builder("jid")
|
|
|
|
|
.ns(ns::MAM)
|
2017-10-31 22:21:27 +00:00
|
|
|
|
.append(jid)
|
2017-10-31 21:10:04 +00:00
|
|
|
|
.build())
|
|
|
|
|
.collect::<Vec<_>>())
|
|
|
|
|
.build())
|
2017-05-06 20:08:44 +00:00
|
|
|
|
}
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-20 19:36:13 +00:00
|
|
|
|
impl From<Prefs> for Element {
|
|
|
|
|
fn from(prefs: Prefs) -> Element {
|
2017-10-31 21:10:04 +00:00
|
|
|
|
Element::builder("prefs")
|
|
|
|
|
.ns(ns::MAM)
|
|
|
|
|
.attr("default", prefs.default_)
|
|
|
|
|
.append(serialise_jid_list("always", prefs.always))
|
|
|
|
|
.append(serialise_jid_list("never", prefs.never))
|
|
|
|
|
.build()
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2017-05-06 20:08:44 +00:00
|
|
|
|
use super::*;
|
2018-05-14 14:11:22 +00:00
|
|
|
|
use std::str::FromStr;
|
2017-04-29 05:07:00 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
2017-04-29 05:41:55 +00:00
|
|
|
|
fn test_query() {
|
|
|
|
|
let elem: Element = "<query xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
Query::try_from(elem).unwrap();
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2017-04-29 05:41:55 +00:00
|
|
|
|
fn test_result() {
|
2017-07-29 05:49:02 +00:00
|
|
|
|
#[cfg(not(feature = "component"))]
|
2017-04-29 05:41:55 +00:00
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<result xmlns='urn:xmpp:mam:2' queryid='f27' id='28482-98726-73623'>
|
|
|
|
|
<forwarded xmlns='urn:xmpp:forward:0'>
|
|
|
|
|
<delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:08:25Z'/>
|
|
|
|
|
<message xmlns='jabber:client' from="witch@shakespeare.lit" to="macbeth@shakespeare.lit">
|
|
|
|
|
<body>Hail to thee</body>
|
|
|
|
|
</message>
|
|
|
|
|
</forwarded>
|
|
|
|
|
</result>
|
2017-07-29 05:49:02 +00:00
|
|
|
|
"#.parse().unwrap();
|
|
|
|
|
#[cfg(feature = "component")]
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<result xmlns='urn:xmpp:mam:2' queryid='f27' id='28482-98726-73623'>
|
|
|
|
|
<forwarded xmlns='urn:xmpp:forward:0'>
|
|
|
|
|
<delay xmlns='urn:xmpp:delay' stamp='2010-07-10T23:08:25Z'/>
|
|
|
|
|
<message xmlns='jabber:component:accept' from="witch@shakespeare.lit" to="macbeth@shakespeare.lit">
|
|
|
|
|
<body>Hail to thee</body>
|
|
|
|
|
</message>
|
|
|
|
|
</forwarded>
|
|
|
|
|
</result>
|
2017-04-29 05:41:55 +00:00
|
|
|
|
"#.parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
Result_::try_from(elem).unwrap();
|
2017-04-29 05:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_fin() {
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<fin xmlns='urn:xmpp:mam:2'>
|
|
|
|
|
<set xmlns='http://jabber.org/protocol/rsm'>
|
|
|
|
|
<first index='0'>28482-98726-73623</first>
|
|
|
|
|
<last>09af3-cc343-b409f</last>
|
|
|
|
|
</set>
|
|
|
|
|
</fin>
|
|
|
|
|
"#.parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
Fin::try_from(elem).unwrap();
|
2017-04-29 05:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_query_x() {
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<query xmlns='urn:xmpp:mam:2'>
|
|
|
|
|
<x xmlns='jabber:x:data' type='submit'>
|
|
|
|
|
<field var='FORM_TYPE' type='hidden'>
|
|
|
|
|
<value>urn:xmpp:mam:2</value>
|
|
|
|
|
</field>
|
|
|
|
|
<field var='with'>
|
|
|
|
|
<value>juliet@capulet.lit</value>
|
|
|
|
|
</field>
|
|
|
|
|
</x>
|
|
|
|
|
</query>
|
|
|
|
|
"#.parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
Query::try_from(elem).unwrap();
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2017-04-29 05:41:55 +00:00
|
|
|
|
fn test_query_x_set() {
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<query xmlns='urn:xmpp:mam:2'>
|
|
|
|
|
<x xmlns='jabber:x:data' type='submit'>
|
|
|
|
|
<field var='FORM_TYPE' type='hidden'>
|
|
|
|
|
<value>urn:xmpp:mam:2</value>
|
|
|
|
|
</field>
|
|
|
|
|
<field var='start'>
|
|
|
|
|
<value>2010-08-07T00:00:00Z</value>
|
|
|
|
|
</field>
|
|
|
|
|
</x>
|
|
|
|
|
<set xmlns='http://jabber.org/protocol/rsm'>
|
|
|
|
|
<max>10</max>
|
|
|
|
|
</set>
|
|
|
|
|
</query>
|
|
|
|
|
"#.parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
Query::try_from(elem).unwrap();
|
2017-04-29 05:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_prefs_get() {
|
2017-05-21 19:45:50 +00:00
|
|
|
|
let elem: Element = "<prefs xmlns='urn:xmpp:mam:2' default='always'/>".parse().unwrap();
|
2017-10-31 21:10:04 +00:00
|
|
|
|
let prefs = Prefs::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(prefs.always, vec!());
|
|
|
|
|
assert_eq!(prefs.never, vec!());
|
2017-04-29 05:41:55 +00:00
|
|
|
|
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<prefs xmlns='urn:xmpp:mam:2' default='roster'>
|
|
|
|
|
<always/>
|
|
|
|
|
<never/>
|
|
|
|
|
</prefs>
|
|
|
|
|
"#.parse().unwrap();
|
2017-10-31 21:10:04 +00:00
|
|
|
|
let prefs = Prefs::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(prefs.always, vec!());
|
|
|
|
|
assert_eq!(prefs.never, vec!());
|
2017-04-29 05:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_prefs_result() {
|
|
|
|
|
let elem: Element = r#"
|
|
|
|
|
<prefs xmlns='urn:xmpp:mam:2' default='roster'>
|
|
|
|
|
<always>
|
|
|
|
|
<jid>romeo@montague.lit</jid>
|
|
|
|
|
</always>
|
|
|
|
|
<never>
|
|
|
|
|
<jid>montague@montague.lit</jid>
|
|
|
|
|
</never>
|
|
|
|
|
</prefs>
|
|
|
|
|
"#.parse().unwrap();
|
2017-10-31 21:10:04 +00:00
|
|
|
|
let prefs = Prefs::try_from(elem).unwrap();
|
|
|
|
|
assert_eq!(prefs.always, vec!(Jid::from_str("romeo@montague.lit").unwrap()));
|
|
|
|
|
assert_eq!(prefs.never, vec!(Jid::from_str("montague@montague.lit").unwrap()));
|
|
|
|
|
|
|
|
|
|
let elem2 = Element::from(prefs.clone());
|
|
|
|
|
println!("{:?}", elem2);
|
|
|
|
|
let prefs2 = Prefs::try_from(elem2).unwrap();
|
|
|
|
|
assert_eq!(prefs.default_, prefs2.default_);
|
|
|
|
|
assert_eq!(prefs.always, prefs2.always);
|
|
|
|
|
assert_eq!(prefs.never, prefs2.never);
|
2017-04-29 05:41:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_invalid_child() {
|
|
|
|
|
let elem: Element = "<query xmlns='urn:xmpp:mam:2'><coucou/></query>".parse().unwrap();
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let error = Query::try_from(elem).unwrap_err();
|
2017-04-29 05:07:00 +00:00
|
|
|
|
let message = match error {
|
|
|
|
|
Error::ParseError(string) => string,
|
|
|
|
|
_ => panic!(),
|
|
|
|
|
};
|
2017-04-29 05:41:55 +00:00
|
|
|
|
assert_eq!(message, "Unknown child in query element.");
|
2017-04-29 05:07:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_serialise() {
|
2017-04-29 05:41:55 +00:00
|
|
|
|
let elem: Element = "<query xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
|
2017-05-06 20:08:44 +00:00
|
|
|
|
let replace = Query { queryid: None, node: None, form: None, set: None };
|
2017-05-23 22:31:33 +00:00
|
|
|
|
let elem2 = replace.into();
|
2017-04-29 05:07:00 +00:00
|
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
|
}
|
|
|
|
|
}
|