message, iq, presence, stanza_error, forwarded: Add support for components hidden behind the component feature flag.

This commit is contained in:
Emmanuel Gil Peyrot 2017-07-29 06:49:02 +01:00
parent 99b9525e6f
commit 69cc83c456
8 changed files with 161 additions and 28 deletions

View file

@ -26,3 +26,7 @@ try_from = "0.2.2"
[dependencies.jid] [dependencies.jid]
version = "0.2.3" version = "0.2.3"
features = ["minidom"] features = ["minidom"]
[features]
# Build xmpp-parsers to make components instead of clients.
component = []

View file

@ -34,9 +34,9 @@ impl TryFrom<Element> for Forwarded {
for child in elem.children() { for child in elem.children() {
if child.is("delay", ns::DELAY) { if child.is("delay", ns::DELAY) {
delay = Some(Delay::try_from(child.clone())?); delay = Some(Delay::try_from(child.clone())?);
} else if child.is("message", ns::JABBER_CLIENT) { } else if child.is("message", ns::DEFAULT_NS) {
stanza = Some(Message::try_from(child.clone())?); stanza = Some(Message::try_from(child.clone())?);
// TODO: also handle the five other possibilities. // TODO: also handle the two other possibilities.
} else { } else {
return Err(Error::ParseError("Unknown child in forwarded element.")); return Err(Error::ParseError("Unknown child in forwarded element."));
} }

View file

@ -207,7 +207,7 @@ impl TryFrom<Element> for Iq {
type Err = Error; type Err = Error;
fn try_from(root: Element) -> Result<Iq, Error> { fn try_from(root: Element) -> Result<Iq, Error> {
if !root.is("iq", ns::JABBER_CLIENT) { if !root.is("iq", ns::DEFAULT_NS) {
return Err(Error::ParseError("This is not an iq element.")); return Err(Error::ParseError("This is not an iq element."));
} }
let from = get_attr!(root, "from", optional); let from = get_attr!(root, "from", optional);
@ -222,7 +222,7 @@ impl TryFrom<Element> for Iq {
return Err(Error::ParseError("Wrong number of children in iq element.")); return Err(Error::ParseError("Wrong number of children in iq element."));
} }
if type_ == "error" { if type_ == "error" {
if elem.is("error", ns::JABBER_CLIENT) { if elem.is("error", ns::DEFAULT_NS) {
if error_payload.is_some() { if error_payload.is_some() {
return Err(Error::ParseError("Wrong number of children in iq element.")); return Err(Error::ParseError("Wrong number of children in iq element."));
} }
@ -275,7 +275,7 @@ impl TryFrom<Element> for Iq {
impl From<Iq> for Element { impl From<Iq> for Element {
fn from(iq: Iq) -> Element { fn from(iq: Iq) -> Element {
let mut stanza = Element::builder("iq") let mut stanza = Element::builder("iq")
.ns(ns::JABBER_CLIENT) .ns(ns::DEFAULT_NS)
.attr("from", iq.from) .attr("from", iq.from)
.attr("to", iq.to) .attr("to", iq.to)
.attr("id", iq.id) .attr("id", iq.id)
@ -300,7 +300,10 @@ mod tests {
#[test] #[test]
fn test_require_type() { fn test_require_type() {
#[cfg(not(feature = "component"))]
let elem: Element = "<iq xmlns='jabber:client'/>".parse().unwrap(); let elem: Element = "<iq xmlns='jabber:client'/>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<iq xmlns='jabber:component:accept'/>".parse().unwrap();
let error = Iq::try_from(elem).unwrap_err(); let error = Iq::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
@ -311,11 +314,16 @@ mod tests {
#[test] #[test]
fn test_get() { fn test_get() {
#[cfg(not(feature = "component"))]
let elem: Element = "<iq xmlns='jabber:client' type='get'> let elem: Element = "<iq xmlns='jabber:client' type='get'>
<foo/> <foo xmlns='bar'/>
</iq>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<iq xmlns='jabber:component:accept' type='get'>
<foo xmlns='bar'/>
</iq>".parse().unwrap(); </iq>".parse().unwrap();
let iq = Iq::try_from(elem).unwrap(); let iq = Iq::try_from(elem).unwrap();
let query: Element = "<foo xmlns='jabber:client'/>".parse().unwrap(); let query: Element = "<foo xmlns='bar'/>".parse().unwrap();
assert_eq!(iq.from, None); assert_eq!(iq.from, None);
assert_eq!(iq.to, None); assert_eq!(iq.to, None);
assert_eq!(iq.id, None); assert_eq!(iq.id, None);
@ -327,9 +335,14 @@ mod tests {
#[test] #[test]
fn test_set() { fn test_set() {
#[cfg(not(feature = "component"))]
let elem: Element = "<iq xmlns='jabber:client' type='set'> let elem: Element = "<iq xmlns='jabber:client' type='set'>
<vCard xmlns='vcard-temp'/> <vCard xmlns='vcard-temp'/>
</iq>".parse().unwrap(); </iq>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<iq xmlns='jabber:component:accept' type='set'>
<vCard xmlns='vcard-temp'/>
</iq>".parse().unwrap();
let iq = Iq::try_from(elem).unwrap(); let iq = Iq::try_from(elem).unwrap();
let vcard: Element = "<vCard xmlns='vcard-temp'/>".parse().unwrap(); let vcard: Element = "<vCard xmlns='vcard-temp'/>".parse().unwrap();
assert_eq!(iq.from, None); assert_eq!(iq.from, None);
@ -343,7 +356,10 @@ mod tests {
#[test] #[test]
fn test_result_empty() { fn test_result_empty() {
#[cfg(not(feature = "component"))]
let elem: Element = "<iq xmlns='jabber:client' type='result'/>".parse().unwrap(); let elem: Element = "<iq xmlns='jabber:client' type='result'/>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<iq xmlns='jabber:component:accept' type='result'/>".parse().unwrap();
let iq = Iq::try_from(elem).unwrap(); let iq = Iq::try_from(elem).unwrap();
assert_eq!(iq.from, None); assert_eq!(iq.from, None);
assert_eq!(iq.to, None); assert_eq!(iq.to, None);
@ -356,9 +372,14 @@ mod tests {
#[test] #[test]
fn test_result() { fn test_result() {
#[cfg(not(feature = "component"))]
let elem: Element = "<iq xmlns='jabber:client' type='result'> let elem: Element = "<iq xmlns='jabber:client' type='result'>
<query xmlns='http://jabber.org/protocol/disco#items'/> <query xmlns='http://jabber.org/protocol/disco#items'/>
</iq>".parse().unwrap(); </iq>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<iq xmlns='jabber:component:accept' type='result'>
<query xmlns='http://jabber.org/protocol/disco#items'/>
</iq>".parse().unwrap();
let iq = Iq::try_from(elem).unwrap(); let iq = Iq::try_from(elem).unwrap();
let query: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>".parse().unwrap(); let query: Element = "<query xmlns='http://jabber.org/protocol/disco#items'/>".parse().unwrap();
assert_eq!(iq.from, None); assert_eq!(iq.from, None);
@ -372,12 +393,20 @@ mod tests {
#[test] #[test]
fn test_error() { fn test_error() {
#[cfg(not(feature = "component"))]
let elem: Element = "<iq xmlns='jabber:client' type='error'> let elem: Element = "<iq xmlns='jabber:client' type='error'>
<ping xmlns='urn:xmpp:ping'/> <ping xmlns='urn:xmpp:ping'/>
<error type='cancel'> <error type='cancel'>
<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/> <service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error> </error>
</iq>".parse().unwrap(); </iq>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<iq xmlns='jabber:component:accept' type='error'>
<ping xmlns='urn:xmpp:ping'/>
<error type='cancel'>
<service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>
</error>
</iq>".parse().unwrap();
let iq = Iq::try_from(elem).unwrap(); let iq = Iq::try_from(elem).unwrap();
assert_eq!(iq.from, None); assert_eq!(iq.from, None);
assert_eq!(iq.to, None); assert_eq!(iq.to, None);
@ -396,7 +425,10 @@ mod tests {
#[test] #[test]
fn test_children_invalid() { fn test_children_invalid() {
#[cfg(not(feature = "component"))]
let elem: Element = "<iq xmlns='jabber:client' type='error'></iq>".parse().unwrap(); let elem: Element = "<iq xmlns='jabber:client' type='error'></iq>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<iq xmlns='jabber:component:accept' type='error'></iq>".parse().unwrap();
let error = Iq::try_from(elem).unwrap_err(); let error = Iq::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
@ -407,7 +439,10 @@ mod tests {
#[test] #[test]
fn test_serialise() { fn test_serialise() {
#[cfg(not(feature = "component"))]
let elem: Element = "<iq xmlns='jabber:client' type='result'/>".parse().unwrap(); let elem: Element = "<iq xmlns='jabber:client' type='result'/>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<iq xmlns='jabber:component:accept' type='result'/>".parse().unwrap();
let iq2 = Iq { let iq2 = Iq {
from: None, from: None,
to: None, to: None,
@ -420,7 +455,10 @@ mod tests {
#[test] #[test]
fn test_disco() { fn test_disco() {
#[cfg(not(feature = "component"))]
let elem: Element = "<iq xmlns='jabber:client' type='get'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap(); let elem: Element = "<iq xmlns='jabber:client' type='get'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<iq xmlns='jabber:component:accept' type='get'><query xmlns='http://jabber.org/protocol/disco#info'/></iq>".parse().unwrap();
let iq = Iq::try_from(elem).unwrap(); let iq = Iq::try_from(elem).unwrap();
let payload = match iq.payload { let payload = match iq.payload {
IqType::Get(payload) => IqGetPayload::try_from(payload).unwrap(), IqType::Get(payload) => IqGetPayload::try_from(payload).unwrap(),

View file

@ -242,6 +242,7 @@ mod tests {
#[test] #[test]
fn test_result() { fn test_result() {
#[cfg(not(feature = "component"))]
let elem: Element = r#" let elem: Element = r#"
<result xmlns='urn:xmpp:mam:2' queryid='f27' id='28482-98726-73623'> <result xmlns='urn:xmpp:mam:2' queryid='f27' id='28482-98726-73623'>
<forwarded xmlns='urn:xmpp:forward:0'> <forwarded xmlns='urn:xmpp:forward:0'>
@ -251,6 +252,17 @@ mod tests {
</message> </message>
</forwarded> </forwarded>
</result> </result>
"#.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>
"#.parse().unwrap(); "#.parse().unwrap();
Result_::try_from(elem).unwrap(); Result_::try_from(elem).unwrap();
} }

View file

@ -49,7 +49,7 @@ impl TryFrom<Element> for MessagePayload {
fn try_from(elem: Element) -> Result<MessagePayload, Error> { fn try_from(elem: Element) -> Result<MessagePayload, Error> {
Ok(match (elem.name().as_ref(), elem.ns().unwrap().as_ref()) { Ok(match (elem.name().as_ref(), elem.ns().unwrap().as_ref()) {
("error", ns::JABBER_CLIENT) => MessagePayload::StanzaError(StanzaError::try_from(elem)?), ("error", ns::DEFAULT_NS) => MessagePayload::StanzaError(StanzaError::try_from(elem)?),
// XEP-0085 // XEP-0085
("active", ns::CHATSTATES) ("active", ns::CHATSTATES)
@ -116,9 +116,9 @@ generate_attribute!(MessageType, "type", {
type Lang = String; type Lang = String;
generate_elem_id!(Body, "body", ns::JABBER_CLIENT); generate_elem_id!(Body, "body", ns::DEFAULT_NS);
generate_elem_id!(Subject, "subject", ns::JABBER_CLIENT); generate_elem_id!(Subject, "subject", ns::DEFAULT_NS);
generate_elem_id!(Thread, "thread", ns::JABBER_CLIENT); generate_elem_id!(Thread, "thread", ns::DEFAULT_NS);
/// The main structure representing the `<message/>` stanza. /// The main structure representing the `<message/>` stanza.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -152,7 +152,7 @@ impl TryFrom<Element> for Message {
type Err = Error; type Err = Error;
fn try_from(root: Element) -> Result<Message, Error> { fn try_from(root: Element) -> Result<Message, Error> {
if !root.is("message", ns::JABBER_CLIENT) { if !root.is("message", ns::DEFAULT_NS) {
return Err(Error::ParseError("This is not a message element.")); return Err(Error::ParseError("This is not a message element."));
} }
let from = get_attr!(root, "from", optional); let from = get_attr!(root, "from", optional);
@ -164,7 +164,7 @@ impl TryFrom<Element> for Message {
let mut thread = None; let mut thread = None;
let mut payloads = vec!(); let mut payloads = vec!();
for elem in root.children() { for elem in root.children() {
if elem.is("body", ns::JABBER_CLIENT) { if elem.is("body", ns::DEFAULT_NS) {
for _ in elem.children() { for _ in elem.children() {
return Err(Error::ParseError("Unknown child in body element.")); return Err(Error::ParseError("Unknown child in body element."));
} }
@ -173,7 +173,7 @@ impl TryFrom<Element> for Message {
if bodies.insert(lang, body).is_some() { if bodies.insert(lang, body).is_some() {
return Err(Error::ParseError("Body element present twice for the same xml:lang.")); return Err(Error::ParseError("Body element present twice for the same xml:lang."));
} }
} else if elem.is("subject", ns::JABBER_CLIENT) { } else if elem.is("subject", ns::DEFAULT_NS) {
for _ in elem.children() { for _ in elem.children() {
return Err(Error::ParseError("Unknown child in subject element.")); return Err(Error::ParseError("Unknown child in subject element."));
} }
@ -182,7 +182,7 @@ impl TryFrom<Element> for Message {
if subjects.insert(lang, subject).is_some() { if subjects.insert(lang, subject).is_some() {
return Err(Error::ParseError("Subject element present twice for the same xml:lang.")); return Err(Error::ParseError("Subject element present twice for the same xml:lang."));
} }
} else if elem.is("thread", ns::JABBER_CLIENT) { } else if elem.is("thread", ns::DEFAULT_NS) {
if thread.is_some() { if thread.is_some() {
return Err(Error::ParseError("Thread element present twice.")); return Err(Error::ParseError("Thread element present twice."));
} }
@ -210,7 +210,7 @@ impl TryFrom<Element> for Message {
impl From<Message> for Element { impl From<Message> for Element {
fn from(message: Message) -> Element { fn from(message: Message) -> Element {
Element::builder("message") Element::builder("message")
.ns(ns::JABBER_CLIENT) .ns(ns::DEFAULT_NS)
.attr("from", message.from) .attr("from", message.from)
.attr("to", message.to) .attr("to", message.to)
.attr("id", message.id) .attr("id", message.id)
@ -246,7 +246,10 @@ mod tests {
#[test] #[test]
fn test_simple() { fn test_simple() {
#[cfg(not(feature = "component"))]
let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap(); let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<message xmlns='jabber:component:accept'/>".parse().unwrap();
let message = Message::try_from(elem).unwrap(); let message = Message::try_from(elem).unwrap();
assert_eq!(message.from, None); assert_eq!(message.from, None);
assert_eq!(message.to, None); assert_eq!(message.to, None);
@ -257,7 +260,10 @@ mod tests {
#[test] #[test]
fn test_serialise() { fn test_serialise() {
#[cfg(not(feature = "component"))]
let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap(); let elem: Element = "<message xmlns='jabber:client'/>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<message xmlns='jabber:component:accept'/>".parse().unwrap();
let mut message = Message::new(None); let mut message = Message::new(None);
message.type_ = MessageType::Normal; message.type_ = MessageType::Normal;
let elem2 = message.into(); let elem2 = message.into();
@ -266,7 +272,10 @@ mod tests {
#[test] #[test]
fn test_body() { fn test_body() {
#[cfg(not(feature = "component"))]
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap(); let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><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 elem1 = elem.clone(); let elem1 = elem.clone();
let message = Message::try_from(elem).unwrap(); let message = Message::try_from(elem).unwrap();
assert_eq!(message.bodies[""], Body::from_str("Hello world!").unwrap()); assert_eq!(message.bodies[""], Body::from_str("Hello world!").unwrap());
@ -277,7 +286,10 @@ mod tests {
#[test] #[test]
fn test_serialise_body() { fn test_serialise_body() {
#[cfg(not(feature = "component"))]
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><body>Hello world!</body></message>".parse().unwrap(); let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><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 mut message = Message::new(Some(Jid::from_str("coucou@example.org").unwrap())); let mut message = Message::new(Some(Jid::from_str("coucou@example.org").unwrap()));
message.bodies.insert(String::from(""), Body::from_str("Hello world!").unwrap()); message.bodies.insert(String::from(""), Body::from_str("Hello world!").unwrap());
let elem2 = message.into(); let elem2 = message.into();
@ -286,7 +298,10 @@ mod tests {
#[test] #[test]
fn test_subject() { fn test_subject() {
#[cfg(not(feature = "component"))]
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><subject>Hello world!</subject></message>".parse().unwrap(); let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><subject>Hello world!</subject></message>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<message xmlns='jabber:component:accept' to='coucou@example.org' type='chat'><subject>Hello world!</subject></message>".parse().unwrap();
let elem1 = elem.clone(); let elem1 = elem.clone();
let message = Message::try_from(elem).unwrap(); let message = Message::try_from(elem).unwrap();
assert_eq!(message.subjects[""], Subject::from_str("Hello world!").unwrap()); assert_eq!(message.subjects[""], Subject::from_str("Hello world!").unwrap());
@ -297,7 +312,10 @@ mod tests {
#[test] #[test]
fn test_attention() { fn test_attention() {
#[cfg(not(feature = "component"))]
let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><attention xmlns='urn:xmpp:attention:0'/></message>".parse().unwrap(); let elem: Element = "<message xmlns='jabber:client' to='coucou@example.org' type='chat'><attention xmlns='urn:xmpp:attention:0'/></message>".parse().unwrap();
#[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();
let elem1 = elem.clone(); let elem1 = elem.clone();
let message = Message::try_from(elem).unwrap(); let message = Message::try_from(elem).unwrap();
let elem2 = message.into(); let elem2 = message.into();

View file

@ -47,6 +47,9 @@ pub const REGISTER: &str = "jabber:iq:register";
/// XEP-0085: Chat State Notifications /// XEP-0085: Chat State Notifications
pub const CHATSTATES: &str = "http://jabber.org/protocol/chatstates"; pub const CHATSTATES: &str = "http://jabber.org/protocol/chatstates";
/// XEP-0114: Jabber Component Protocol
pub const COMPONENT_ACCEPT: &str = "jabber:component:accept";
/// XEP-0115: Entity Capabilities /// XEP-0115: Entity Capabilities
pub const CAPS: &str = "http://jabber.org/protocol/caps"; pub const CAPS: &str = "http://jabber.org/protocol/caps";
@ -119,3 +122,8 @@ pub const EME: &str = "urn:xmpp:eme:0";
pub const ECAPS2: &str = "urn:xmpp:caps"; pub const ECAPS2: &str = "urn:xmpp:caps";
/// XEP-0390: Entity Capabilities 2.0 /// XEP-0390: Entity Capabilities 2.0
pub const ECAPS2_OPTIMIZE: &str = "urn:xmpp:caps:optimize"; pub const ECAPS2_OPTIMIZE: &str = "urn:xmpp:caps:optimize";
#[cfg(not(feature = "component"))]
pub const DEFAULT_NS: &str = JABBER_CLIENT;
#[cfg(feature = "component")]
pub const DEFAULT_NS: &str = COMPONENT_ACCEPT;

View file

@ -95,7 +95,7 @@ impl TryFrom<Element> for PresencePayload {
fn try_from(elem: Element) -> Result<PresencePayload, Error> { fn try_from(elem: Element) -> Result<PresencePayload, Error> {
Ok(match (elem.name().as_ref(), elem.ns().unwrap().as_ref()) { Ok(match (elem.name().as_ref(), elem.ns().unwrap().as_ref()) {
("error", ns::JABBER_CLIENT) => PresencePayload::StanzaError(StanzaError::try_from(elem)?), ("error", ns::DEFAULT_NS) => PresencePayload::StanzaError(StanzaError::try_from(elem)?),
// XEP-0045 // XEP-0045
("x", ns::MUC) => PresencePayload::Muc(Muc::try_from(elem)?), ("x", ns::MUC) => PresencePayload::Muc(Muc::try_from(elem)?),
@ -248,7 +248,7 @@ impl TryFrom<Element> for Presence {
type Err = Error; type Err = Error;
fn try_from(root: Element) -> Result<Presence, Error> { fn try_from(root: Element) -> Result<Presence, Error> {
if !root.is("presence", ns::JABBER_CLIENT) { if !root.is("presence", ns::DEFAULT_NS) {
return Err(Error::ParseError("This is not a presence element.")); return Err(Error::ParseError("This is not a presence element."));
} }
let mut show = None; let mut show = None;
@ -264,7 +264,7 @@ impl TryFrom<Element> for Presence {
payloads: vec!(), payloads: vec!(),
}; };
for elem in root.children() { for elem in root.children() {
if elem.is("show", ns::JABBER_CLIENT) { if elem.is("show", ns::DEFAULT_NS) {
if show.is_some() { if show.is_some() {
return Err(Error::ParseError("More than one show element in a presence.")); return Err(Error::ParseError("More than one show element in a presence."));
} }
@ -275,7 +275,7 @@ impl TryFrom<Element> for Presence {
return Err(Error::ParseError("Unknown attribute in show element.")); return Err(Error::ParseError("Unknown attribute in show element."));
} }
show = Some(Show::from_str(elem.text().as_ref())?); show = Some(Show::from_str(elem.text().as_ref())?);
} else if elem.is("status", ns::JABBER_CLIENT) { } else if elem.is("status", ns::DEFAULT_NS) {
for _ in elem.children() { for _ in elem.children() {
return Err(Error::ParseError("Unknown child in status element.")); return Err(Error::ParseError("Unknown child in status element."));
} }
@ -288,7 +288,7 @@ impl TryFrom<Element> for Presence {
if presence.statuses.insert(lang, elem.text()).is_some() { if presence.statuses.insert(lang, elem.text()).is_some() {
return Err(Error::ParseError("Status element present twice for the same xml:lang.")); return Err(Error::ParseError("Status element present twice for the same xml:lang."));
} }
} else if elem.is("priority", ns::JABBER_CLIENT) { } else if elem.is("priority", ns::DEFAULT_NS) {
if priority.is_some() { if priority.is_some() {
return Err(Error::ParseError("More than one priority element in a presence.")); return Err(Error::ParseError("More than one priority element in a presence."));
} }
@ -316,7 +316,7 @@ impl TryFrom<Element> for Presence {
impl From<Presence> for Element { impl From<Presence> for Element {
fn from(presence: Presence) -> Element { fn from(presence: Presence) -> Element {
Element::builder("presence") Element::builder("presence")
.ns(ns::JABBER_CLIENT) .ns(ns::DEFAULT_NS)
.attr("from", presence.from) .attr("from", presence.from)
.attr("to", presence.to) .attr("to", presence.to)
.attr("id", presence.id) .attr("id", presence.id)
@ -343,7 +343,10 @@ mod tests {
#[test] #[test]
fn test_simple() { fn test_simple() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'/>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'/>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'/>".parse().unwrap();
let presence = Presence::try_from(elem).unwrap(); let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.from, None); assert_eq!(presence.from, None);
assert_eq!(presence.to, None); assert_eq!(presence.to, None);
@ -354,7 +357,10 @@ mod tests {
#[test] #[test]
fn test_serialise() { fn test_serialise() {
let elem: Element = "<presence xmlns='jabber:client' type='unavailable'/>".parse().unwrap(); #[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client' type='unavailable'/>/>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept' type='unavailable'/>/>".parse().unwrap();
let presence = Presence::new(Type::Unavailable); let presence = Presence::new(Type::Unavailable);
let elem2 = presence.into(); let elem2 = presence.into();
assert_eq!(elem, elem2); assert_eq!(elem, elem2);
@ -362,7 +368,10 @@ mod tests {
#[test] #[test]
fn test_show() { fn test_show() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><show>chat</show></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><show>chat</show></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><show>chat</show></presence>".parse().unwrap();
let presence = Presence::try_from(elem).unwrap(); let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.payloads.len(), 0); assert_eq!(presence.payloads.len(), 0);
assert_eq!(presence.show, Show::Chat); assert_eq!(presence.show, Show::Chat);
@ -370,8 +379,10 @@ mod tests {
#[test] #[test]
fn test_missing_show_value() { fn test_missing_show_value() {
// "online" used to be a pretty common mistake. #[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><show/></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><show/></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><show/></presence>".parse().unwrap();
let error = Presence::try_from(elem).unwrap_err(); let error = Presence::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
@ -383,7 +394,10 @@ mod tests {
#[test] #[test]
fn test_invalid_show() { fn test_invalid_show() {
// "online" used to be a pretty common mistake. // "online" used to be a pretty common mistake.
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><show>online</show></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><show>online</show></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><show>online</show></presence>".parse().unwrap();
let error = Presence::try_from(elem).unwrap_err(); let error = Presence::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
@ -394,7 +408,10 @@ mod tests {
#[test] #[test]
fn test_empty_status() { fn test_empty_status() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status/></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><status/></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><status/></presence>".parse().unwrap();
let presence = Presence::try_from(elem).unwrap(); let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.payloads.len(), 0); assert_eq!(presence.payloads.len(), 0);
assert_eq!(presence.statuses.len(), 1); assert_eq!(presence.statuses.len(), 1);
@ -403,7 +420,10 @@ mod tests {
#[test] #[test]
fn test_status() { fn test_status() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status>Here!</status></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><status>Here!</status></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><status>Here!</status></presence>".parse().unwrap();
let presence = Presence::try_from(elem).unwrap(); let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.payloads.len(), 0); assert_eq!(presence.payloads.len(), 0);
assert_eq!(presence.statuses.len(), 1); assert_eq!(presence.statuses.len(), 1);
@ -412,7 +432,10 @@ mod tests {
#[test] #[test]
fn test_multiple_statuses() { fn test_multiple_statuses() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><status>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><status>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
let presence = Presence::try_from(elem).unwrap(); let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.payloads.len(), 0); assert_eq!(presence.payloads.len(), 0);
assert_eq!(presence.statuses.len(), 2); assert_eq!(presence.statuses.len(), 2);
@ -422,7 +445,10 @@ mod tests {
#[test] #[test]
fn test_invalid_multiple_statuses() { fn test_invalid_multiple_statuses() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status xml:lang='fr'>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><status xml:lang='fr'>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><status xml:lang='fr'>Here!</status><status xml:lang='fr'>Là!</status></presence>".parse().unwrap();
let error = Presence::try_from(elem).unwrap_err(); let error = Presence::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
@ -433,7 +459,10 @@ mod tests {
#[test] #[test]
fn test_priority() { fn test_priority() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><priority>-1</priority></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><priority>-1</priority></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><priority>-1</priority></presence>".parse().unwrap();
let presence = Presence::try_from(elem).unwrap(); let presence = Presence::try_from(elem).unwrap();
assert_eq!(presence.payloads.len(), 0); assert_eq!(presence.payloads.len(), 0);
assert_eq!(presence.priority, -1i8); assert_eq!(presence.priority, -1i8);
@ -441,7 +470,10 @@ mod tests {
#[test] #[test]
fn test_invalid_priority() { fn test_invalid_priority() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><priority>128</priority></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><priority>128</priority></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><priority>128</priority></presence>".parse().unwrap();
let error = Presence::try_from(elem).unwrap_err(); let error = Presence::try_from(elem).unwrap_err();
match error { match error {
Error::ParseIntError(_) => (), Error::ParseIntError(_) => (),
@ -451,7 +483,10 @@ mod tests {
#[test] #[test]
fn test_unknown_child() { fn test_unknown_child() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><test xmlns='invalid'/></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><test xmlns='invalid'/></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><test xmlns='invalid'/></presence>".parse().unwrap();
let presence = Presence::try_from(elem).unwrap(); let presence = Presence::try_from(elem).unwrap();
let payload = &presence.payloads[0]; let payload = &presence.payloads[0];
assert!(payload.is("test", "invalid")); assert!(payload.is("test", "invalid"));
@ -459,7 +494,10 @@ mod tests {
#[test] #[test]
fn test_invalid_status_child() { fn test_invalid_status_child() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status><coucou/></status></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><status><coucou/></status></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><status><coucou/></status></presence>".parse().unwrap();
let error = Presence::try_from(elem).unwrap_err(); let error = Presence::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
@ -470,7 +508,10 @@ mod tests {
#[test] #[test]
fn test_invalid_attribute() { fn test_invalid_attribute() {
#[cfg(not(feature = "component"))]
let elem: Element = "<presence xmlns='jabber:client'><status coucou=''/></presence>".parse().unwrap(); let elem: Element = "<presence xmlns='jabber:client'><status coucou=''/></presence>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<presence xmlns='jabber:component:accept'><status coucou=''/></presence>".parse().unwrap();
let error = Presence::try_from(elem).unwrap_err(); let error = Presence::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
@ -485,7 +526,7 @@ mod tests {
let mut presence = Presence::new(Type::Unavailable); let mut presence = Presence::new(Type::Unavailable);
presence.statuses.insert(String::from(""), status); presence.statuses.insert(String::from(""), status);
let elem: Element = presence.into(); let elem: Element = presence.into();
assert!(elem.is("presence", ns::JABBER_CLIENT)); assert!(elem.is("presence", ns::DEFAULT_NS));
assert!(elem.children().next().unwrap().is("status", ns::JABBER_CLIENT)); assert!(elem.children().next().unwrap().is("status", ns::DEFAULT_NS));
} }
} }

View file

@ -125,7 +125,7 @@ impl TryFrom<Element> for StanzaError {
type Err = Error; type Err = Error;
fn try_from(elem: Element) -> Result<StanzaError, Error> { fn try_from(elem: Element) -> Result<StanzaError, Error> {
if !elem.is("error", ns::JABBER_CLIENT) { if !elem.is("error", ns::DEFAULT_NS) {
return Err(Error::ParseError("This is not an error element.")); return Err(Error::ParseError("This is not an error element."));
} }
@ -175,7 +175,7 @@ impl TryFrom<Element> for StanzaError {
impl From<StanzaError> for Element { impl From<StanzaError> for Element {
fn from(err: StanzaError) -> Element { fn from(err: StanzaError) -> Element {
let mut root = Element::builder("error") let mut root = Element::builder("error")
.ns(ns::JABBER_CLIENT) .ns(ns::DEFAULT_NS)
.attr("type", err.type_) .attr("type", err.type_)
.attr("by", err.by) .attr("by", err.by)
.append(err.defined_condition) .append(err.defined_condition)
@ -201,7 +201,10 @@ mod tests {
#[test] #[test]
fn test_simple() { fn test_simple() {
#[cfg(not(feature = "component"))]
let elem: Element = "<error xmlns='jabber:client' type='cancel'><undefined-condition xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>".parse().unwrap(); let elem: Element = "<error xmlns='jabber:client' type='cancel'><undefined-condition xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<error xmlns='jabber:component:accept' type='cancel'><undefined-condition xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error>".parse().unwrap();
let error = StanzaError::try_from(elem).unwrap(); let error = StanzaError::try_from(elem).unwrap();
assert_eq!(error.type_, ErrorType::Cancel); assert_eq!(error.type_, ErrorType::Cancel);
assert_eq!(error.defined_condition, DefinedCondition::UndefinedCondition); assert_eq!(error.defined_condition, DefinedCondition::UndefinedCondition);
@ -209,7 +212,10 @@ mod tests {
#[test] #[test]
fn test_invalid_type() { fn test_invalid_type() {
#[cfg(not(feature = "component"))]
let elem: Element = "<error xmlns='jabber:client'/>".parse().unwrap(); let elem: Element = "<error xmlns='jabber:client'/>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<error xmlns='jabber:component:accept'/>".parse().unwrap();
let error = StanzaError::try_from(elem).unwrap_err(); let error = StanzaError::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
@ -217,7 +223,10 @@ mod tests {
}; };
assert_eq!(message, "Required attribute 'type' missing."); assert_eq!(message, "Required attribute 'type' missing.");
#[cfg(not(feature = "component"))]
let elem: Element = "<error xmlns='jabber:client' type='coucou'/>".parse().unwrap(); let elem: Element = "<error xmlns='jabber:client' type='coucou'/>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<error xmlns='jabber:component:accept' type='coucou'/>".parse().unwrap();
let error = StanzaError::try_from(elem).unwrap_err(); let error = StanzaError::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,
@ -228,7 +237,10 @@ mod tests {
#[test] #[test]
fn test_invalid_condition() { fn test_invalid_condition() {
#[cfg(not(feature = "component"))]
let elem: Element = "<error xmlns='jabber:client' type='cancel'/>".parse().unwrap(); let elem: Element = "<error xmlns='jabber:client' type='cancel'/>".parse().unwrap();
#[cfg(feature = "component")]
let elem: Element = "<error xmlns='jabber:component:accept' type='cancel'/>".parse().unwrap();
let error = StanzaError::try_from(elem).unwrap_err(); let error = StanzaError::try_from(elem).unwrap_err();
let message = match error { let message = match error {
Error::ParseError(string) => string, Error::ParseError(string) => string,