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-06 19:14:45 +00:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2017-04-29 05:07:00 +00:00
|
|
|
use minidom::Element;
|
|
|
|
use jid::Jid;
|
|
|
|
|
|
|
|
use error::Error;
|
|
|
|
|
|
|
|
use data_forms::DataForm;
|
|
|
|
use rsm::Set;
|
|
|
|
use forwarding::Forwarded;
|
|
|
|
|
|
|
|
use ns;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Query {
|
|
|
|
pub queryid: Option<String>,
|
|
|
|
pub node: Option<String>,
|
|
|
|
pub form: Option<DataForm>,
|
|
|
|
pub set: Option<Set>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Result_ {
|
|
|
|
pub queryid: String,
|
|
|
|
pub id: String,
|
|
|
|
pub forwarded: Forwarded,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Fin {
|
|
|
|
pub complete: bool,
|
|
|
|
pub set: Set,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum DefaultPrefs {
|
|
|
|
Always,
|
|
|
|
Never,
|
|
|
|
Roster,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Prefs {
|
|
|
|
pub default_: Option<DefaultPrefs>,
|
|
|
|
pub always: Vec<Jid>,
|
|
|
|
pub never: Vec<Jid>,
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:08:44 +00:00
|
|
|
impl<'a> TryFrom<&'a Element> for Query {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: &'a Element) -> Result<Query, Error> {
|
|
|
|
if !elem.is("query", ns::MAM) {
|
|
|
|
return Err(Error::ParseError("This is not a query element."));
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
2017-05-06 20:08:44 +00:00
|
|
|
let mut form = None;
|
|
|
|
let mut set = None;
|
|
|
|
for child in elem.children() {
|
|
|
|
if child.is("x", ns::DATA_FORMS) {
|
|
|
|
form = Some(DataForm::try_from(child)?);
|
|
|
|
} else if child.is("set", ns::RSM) {
|
|
|
|
set = Some(Set::try_from(child)?);
|
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Unknown child in query element."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let queryid = match elem.attr("queryid") {
|
|
|
|
Some(queryid) => Some(queryid.to_owned()),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
let node = match elem.attr("node") {
|
|
|
|
Some(node) => Some(node.to_owned()),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
Ok(Query { queryid, node, form, set })
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:08:44 +00:00
|
|
|
impl<'a> TryFrom<&'a Element> for Result_ {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: &'a Element) -> Result<Result_, Error> {
|
|
|
|
if !elem.is("result", ns::MAM) {
|
|
|
|
return Err(Error::ParseError("This is not a result element."));
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
2017-05-06 20:08:44 +00:00
|
|
|
let mut forwarded = None;
|
|
|
|
for child in elem.children() {
|
|
|
|
if child.is("forwarded", ns::FORWARD) {
|
|
|
|
forwarded = Some(Forwarded::try_from(child)?);
|
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Unknown child in result element."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let queryid = match elem.attr("queryid") {
|
|
|
|
Some(queryid) => queryid.to_owned(),
|
|
|
|
None => return Err(Error::ParseError("No 'queryid' attribute present in result.")),
|
|
|
|
};
|
|
|
|
let id = match elem.attr("id") {
|
|
|
|
Some(id) => id.to_owned(),
|
|
|
|
None => return Err(Error::ParseError("No 'id' attribute present in result.")),
|
|
|
|
};
|
|
|
|
if forwarded.is_none() {
|
|
|
|
return Err(Error::ParseError("Mandatory forwarded element missing in result."));
|
|
|
|
}
|
|
|
|
let forwarded = forwarded.unwrap();
|
|
|
|
Ok(Result_ {
|
|
|
|
queryid,
|
|
|
|
id,
|
|
|
|
forwarded,
|
|
|
|
})
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:08:44 +00:00
|
|
|
impl<'a> TryFrom<&'a Element> for Fin {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: &'a Element) -> Result<Fin, Error> {
|
|
|
|
if !elem.is("fin", ns::MAM) {
|
|
|
|
return Err(Error::ParseError("This is not a fin element."));
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
2017-05-06 20:08:44 +00:00
|
|
|
let mut set = None;
|
|
|
|
for child in elem.children() {
|
|
|
|
if child.is("set", ns::RSM) {
|
|
|
|
set = Some(Set::try_from(child)?);
|
|
|
|
} else {
|
|
|
|
return Err(Error::ParseError("Unknown child in fin element."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let complete = match elem.attr("complete") {
|
|
|
|
Some(complete) => complete == "true",
|
|
|
|
None => false,
|
|
|
|
};
|
|
|
|
if set.is_none() {
|
|
|
|
return Err(Error::ParseError("Mandatory set element missing in fin."));
|
|
|
|
}
|
|
|
|
let set = set.unwrap();
|
|
|
|
Ok(Fin { complete, set })
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:08:44 +00:00
|
|
|
impl<'a> TryFrom<&'a Element> for Prefs {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn try_from(elem: &'a Element) -> Result<Prefs, Error> {
|
|
|
|
if !elem.is("prefs", ns::MAM) {
|
|
|
|
return Err(Error::ParseError("This is not a prefs element."));
|
|
|
|
}
|
|
|
|
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-06 20:08:44 +00:00
|
|
|
let default_ = match elem.attr("default") {
|
|
|
|
Some("always") => Some(DefaultPrefs::Always),
|
|
|
|
Some("never") => Some(DefaultPrefs::Never),
|
|
|
|
Some("roster") => Some(DefaultPrefs::Roster),
|
|
|
|
None => None,
|
|
|
|
|
|
|
|
_ => return Err(Error::ParseError("Invalid 'default' attribute present in prefs.")),
|
|
|
|
};
|
|
|
|
Ok(Prefs { default_, always, never })
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:08:44 +00:00
|
|
|
impl<'a> Into<Element> for &'a Query {
|
|
|
|
fn into(self) -> Element {
|
|
|
|
let mut elem = Element::builder("query")
|
|
|
|
.ns(ns::MAM)
|
|
|
|
.attr("queryid", self.queryid.clone())
|
|
|
|
.attr("node", self.node.clone())
|
|
|
|
.build();
|
|
|
|
//if let Some(form) = self.form {
|
|
|
|
// elem.append_child((&form).into());
|
|
|
|
//}
|
|
|
|
if let Some(ref set) = self.set {
|
|
|
|
elem.append_child(set.into());
|
|
|
|
}
|
|
|
|
elem
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-06 20:08:44 +00:00
|
|
|
impl<'a> Into<Element> for &'a Result_ {
|
|
|
|
fn into(self) -> Element {
|
|
|
|
let mut elem = Element::builder("result")
|
|
|
|
.ns(ns::MAM)
|
|
|
|
.attr("queryid", self.queryid.clone())
|
|
|
|
.attr("id", self.id.clone())
|
|
|
|
.build();
|
|
|
|
elem.append_child((&self.forwarded).into());
|
|
|
|
elem
|
|
|
|
}
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 20:08:44 +00:00
|
|
|
impl<'a> Into<Element> for &'a Fin {
|
|
|
|
fn into(self) -> Element {
|
|
|
|
let mut elem = Element::builder("fin")
|
|
|
|
.ns(ns::MAM)
|
|
|
|
.attr("complete", match self.complete {
|
|
|
|
true => Some("true"),
|
|
|
|
false => None,
|
|
|
|
})
|
|
|
|
.build();
|
|
|
|
elem.append_child((&self.set).into());
|
|
|
|
elem
|
|
|
|
}
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
|
|
|
|
2017-05-06 20:08:44 +00:00
|
|
|
impl<'a> Into<Element> for &'a Prefs {
|
|
|
|
fn into(self) -> Element {
|
|
|
|
let mut elem = Element::builder("prefs")
|
|
|
|
.ns(ns::MAM)
|
|
|
|
.attr("default", match self.default_ {
|
|
|
|
Some(DefaultPrefs::Always) => Some("always"),
|
|
|
|
Some(DefaultPrefs::Never) => Some("never"),
|
|
|
|
Some(DefaultPrefs::Roster) => Some("roster"),
|
|
|
|
None => None,
|
|
|
|
})
|
|
|
|
.build();
|
|
|
|
if !self.always.is_empty() {
|
|
|
|
let mut always = Element::builder("always")
|
|
|
|
.ns(ns::RSM)
|
|
|
|
.build();
|
|
|
|
for jid in self.always.clone() {
|
|
|
|
always.append_child(Element::builder("jid")
|
|
|
|
.ns(ns::RSM)
|
|
|
|
.append(String::from(jid))
|
|
|
|
.build());
|
|
|
|
}
|
|
|
|
elem.append_child(always);
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
2017-05-06 20:08:44 +00:00
|
|
|
if !self.never.is_empty() {
|
|
|
|
let mut never = Element::builder("never")
|
|
|
|
.ns(ns::RSM)
|
|
|
|
.build();
|
|
|
|
for jid in self.never.clone() {
|
|
|
|
never.append_child(Element::builder("jid")
|
|
|
|
.ns(ns::RSM)
|
|
|
|
.append(String::from(jid))
|
|
|
|
.build());
|
|
|
|
}
|
|
|
|
elem.append_child(never);
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
2017-05-06 20:08:44 +00:00
|
|
|
elem
|
2017-04-29 05:07:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2017-05-06 20:08:44 +00:00
|
|
|
use super::*;
|
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-06 20:08:44 +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() {
|
|
|
|
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>
|
|
|
|
"#.parse().unwrap();
|
2017-05-06 20:08:44 +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-06 20:08:44 +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-06 20:08:44 +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-06 20:08:44 +00:00
|
|
|
Query::try_from(&elem).unwrap();
|
2017-04-29 05:41:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_prefs_get() {
|
|
|
|
let elem: Element = "<prefs xmlns='urn:xmpp:mam:2'/>".parse().unwrap();
|
2017-05-06 20:08:44 +00:00
|
|
|
Prefs::try_from(&elem).unwrap();
|
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-05-06 20:08:44 +00:00
|
|
|
Prefs::try_from(&elem).unwrap();
|
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-05-06 20:08:44 +00:00
|
|
|
Prefs::try_from(&elem).unwrap();
|
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-06 20:08:44 +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 };
|
|
|
|
let elem2 = (&replace).into();
|
2017-04-29 05:07:00 +00:00
|
|
|
assert_eq!(elem, elem2);
|
|
|
|
}
|
|
|
|
}
|