From 9941e9c34f8eae76292fa43799aa3da41e821eef Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 8 Sep 2019 16:09:49 +0200 Subject: [PATCH] Add a new JID Prep parser (XEP-0328). --- ChangeLog | 1 + doap.xml | 8 +++++++ src/jid_prep.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 +++ src/ns.rs | 3 +++ src/util/helpers.rs | 15 ++++++++++++ 6 files changed, 86 insertions(+) create mode 100644 src/jid_prep.rs diff --git a/ChangeLog b/ChangeLog index f1f253dc..43367a60 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,7 @@ Version NEXT: DATE Emmanuel Gil Peyrot * New parsers/serialisers: - Client Certificate Management for SASL EXTERNAL (XEP-0257) + - JID Prep (XEP-0328) - Client State Indication (XEP-0352) - OpenPGP for XMPP (XEP-0373) - Anonymous unique occupant identifiers for MUCs (XEP-0421) diff --git a/doap.xml b/doap.xml index b9fc9965..f591bfff 100644 --- a/doap.xml +++ b/doap.xml @@ -400,6 +400,14 @@ 0.13.0 + + + + complete + 0.1 + NEXT + + diff --git a/src/jid_prep.rs b/src/jid_prep.rs new file mode 100644 index 00000000..9ecff61a --- /dev/null +++ b/src/jid_prep.rs @@ -0,0 +1,56 @@ +// Copyright (c) 2019 Emmanuel Gil Peyrot +// +// 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/. + +use crate::iq::{IqGetPayload, IqResultPayload}; +use crate::util::helpers::{PlainText, JidCodec}; +use jid::Jid; + +generate_element!( + /// TODO + JidPrepQuery, "jid", JID_PREP, + text: ( + /// TODO + data: PlainText> + ) +); + +impl IqGetPayload for JidPrepQuery {} + +generate_element!( + /// TODO + JidPrepResponse, "jid", JID_PREP, + text: ( + /// TODO + jid: JidCodec + ) +); + +impl IqResultPayload for JidPrepResponse {} + +#[cfg(test)] +mod tests { + use super::*; + use minidom::Element; + use std::convert::TryFrom; + use std::str::FromStr; + + #[test] + fn test_size() { + assert_size!(JidPrepQuery, 24); + assert_size!(JidPrepResponse, 80); + } + + #[test] + fn simple() { + let elem: Element = "ROMeo@montague.lit/orchard".parse().unwrap(); + let query = JidPrepQuery::try_from(elem).unwrap(); + assert_eq!(query.data.unwrap(), "ROMeo@montague.lit/orchard"); + + let elem: Element = "romeo@montague.lit/orchard".parse().unwrap(); + let response = JidPrepResponse::try_from(elem).unwrap(); + assert_eq!(response.jid, Jid::from_str("romeo@montague.lit/orchard").unwrap()); + } +} diff --git a/src/lib.rs b/src/lib.rs index 9f6053cc..b69e56ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -180,6 +180,9 @@ pub mod idle; /// XEP-0320: Use of DTLS-SRTP in Jingle Sessions pub mod jingle_dtls_srtp; +/// XEP-0328: JID Prep +pub mod jid_prep; + /// XEP-0352: Client State Indication pub mod csi; diff --git a/src/ns.rs b/src/ns.rs index 328ed482..70d68cff 100644 --- a/src/ns.rs +++ b/src/ns.rs @@ -185,6 +185,9 @@ pub const IDLE: &str = "urn:xmpp:idle:1"; /// XEP-0320: Use of DTLS-SRTP in Jingle Sessions pub const JINGLE_DTLS: &str = "urn:xmpp:jingle:apps:dtls:0"; +/// XEP-0328: JID Prep +pub const JID_PREP: &str = "urn:xmpp:jidprep:0"; + /// XEP-0352: Client State Indication pub const CSI: &str = "urn:xmpp:csi:0"; diff --git a/src/util/helpers.rs b/src/util/helpers.rs index a455a139..a78fa7f6 100644 --- a/src/util/helpers.rs +++ b/src/util/helpers.rs @@ -5,6 +5,8 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. use crate::util::error::Error; +use jid::Jid; +use std::str::FromStr; /// Codec for plain text content. pub struct PlainText; @@ -89,3 +91,16 @@ impl ColonSeparatedHex { Some(bytes.join(":")) } } + +/// Codec for a JID. +pub struct JidCodec; + +impl JidCodec { + pub fn decode(s: &str) -> Result { + Ok(Jid::from_str(s)?) + } + + pub fn encode(jid: &Jid) -> Option { + Some(jid.to_string()) + } +}