From 10b204093e00f997d7b7c812a1c84689a77cb861 Mon Sep 17 00:00:00 2001 From: Werner Kroneman Date: Wed, 10 Jan 2024 23:22:16 +0100 Subject: [PATCH] Added vCard update to parsers --- parsers/src/lib.rs | 3 ++ parsers/src/ns.rs | 3 ++ parsers/src/vcard.rs | 3 ++ parsers/src/vcard_update.rs | 76 +++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 parsers/src/vcard_update.rs diff --git a/parsers/src/lib.rs b/parsers/src/lib.rs index ddad57f..399fc8d 100644 --- a/parsers/src/lib.rs +++ b/parsers/src/lib.rs @@ -112,6 +112,9 @@ pub mod caps; /// XEP-0118: User Tune pub mod tune; +///XEP-0153: vCard-Based Avatars +pub mod vcard_update; + /// XEP-0157: Contact Addresses for XMPP Services pub mod server_info; diff --git a/parsers/src/ns.rs b/parsers/src/ns.rs index 1534f3e..8ba86ea 100644 --- a/parsers/src/ns.rs +++ b/parsers/src/ns.rs @@ -97,6 +97,9 @@ pub const CAPS: &str = "http://jabber.org/protocol/caps"; /// XEP-0118: User Tune pub const TUNE: &str = "http://jabber.org/protocol/tune"; +/// XEP-0153: vCard-Based Avatars +pub const VCARD_UPDATE: &str = "vcard-temp:x:update"; + /// XEP-0157: Contact Addresses for XMPP Services pub const SERVER_INFO: &str = "http://jabber.org/network/serverinfo"; diff --git a/parsers/src/vcard.rs b/parsers/src/vcard.rs index 0a40030..887f1e4 100644 --- a/parsers/src/vcard.rs +++ b/parsers/src/vcard.rs @@ -9,6 +9,9 @@ //! //! Only the element is supported as a member of this legacy vCard. For more modern and complete //! user profile management, see [XEP-0292](https://xmpp.org/extensions/xep-0292.html): vCard4 Over XMPP. +//! +//! For vCard updates defined in [XEP-0153](https://xmpp.org/extensions/xep-0153.html), +//! see [`vcard_update`][crate::vcard_update] module. use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload}; use crate::util::text_node_codecs::{Base64, Codec, Text}; diff --git a/parsers/src/vcard_update.rs b/parsers/src/vcard_update.rs new file mode 100644 index 0000000..9f7a08b --- /dev/null +++ b/parsers/src/vcard_update.rs @@ -0,0 +1,76 @@ +// Copyright (c) 2024 xmpp-rs contributors. +// +// 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/. + +//! This module implements vCard avatar updates defined in +//! [XEP-0153](https://xmpp.org/extensions/xep-0153.html). +//! +//! Specifically, as it appears in stanzas, as shown in this example: +//! https://xmpp.org/extensions/xep-0153.html#example-3 +//! +//! For [XEP-0054](https://xmpp.org/extensions/xep-0054.html) vCard support, +//! see [`vcard`][crate::vcard] module. + +use crate::util::text_node_codecs::{Codec, FixedHex, OptionalCodec}; + +generate_element!( + /// The presence payload for an avatar VCard update + VCardUpdate, "x", VCARD_UPDATE, + children: [ + /// The photo element. Is empty if "a client is not yet ready to advertise an image". + photo: Option = ("photo", VCARD_UPDATE) => Photo, + ] +); + +generate_element!( + /// The photo element containing the avatar metadata + Photo, "photo", VCARD_UPDATE, + text: ( + /// The SHA1 hash of the avatar. Empty when there is no photo. + data: OptionalCodec> + ) +); + +#[cfg(test)] +mod tests { + use super::*; + use crate::Element; + use std::str::FromStr; + + #[test] + fn test_vcard_update() { + // Test xml stolen from https://xmpp.org/extensions/xep-0153.html#example-3 + // Changes: I did set the last d to uppercase to try to trip up a potentially case-sensitive parser. + let test_vcard = r" + 01b87fcd030b72895ff8e88db57ec525450f000D + "; + + let test_vcard = Element::from_str(&test_vcard).expect("Failed to parse XML"); + let test_vcard = VCardUpdate::try_from(test_vcard).expect("Failed to parse vCardUpdate"); + + let photo = test_vcard.photo.expect("No photo found"); + + assert_eq!( + photo.data, + Some([ + 0x01, 0xb8, 0x7f, 0xcd, 0x03, 0x0b, 0x72, 0x89, 0x5f, 0xf8, 0xe8, 0x8d, 0xb5, 0x7e, + 0xc5, 0x25, 0x45, 0x0f, 0x00, 0x0d + ]) + ); + } + + #[test] + fn test_vcard_update_empty() { + // Test xml stolen from https://xmpp.org/extensions/xep-0153.html#example-7 + let test_vcard = r""; + + let test_vcard = Element::from_str(&test_vcard).expect("Failed to parse XML"); + let test_vcard = VCardUpdate::try_from(test_vcard).expect("Failed to parse vCardUpdate"); + + let photo = test_vcard.photo.expect("No photo found"); + + assert_eq!(photo.data, None) + } +}