diff options
author | 2025-03-24 12:22:08 +0000 | |
---|---|---|
committer | 2025-03-24 12:31:01 +0000 | |
commit | 54ca5eb3155d1cfcadced7c0a3a405ce1d51ecf6 (patch) | |
tree | 49a59ebae7bd7b58caaa748a20b745397c37c2e3 | |
parent | 9f0a480bc4c6ee3ac8c707b05d40529afef0d78f (diff) | |
download | luz-54ca5eb3155d1cfcadced7c0a3a405ce1d51ecf6.tar.gz luz-54ca5eb3155d1cfcadced7c0a3a405ce1d51ecf6.tar.bz2 luz-54ca5eb3155d1cfcadced7c0a3a405ce1d51ecf6.zip |
feat(stanza): xep-0203
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | stanza/Cargo.toml | 4 | ||||
-rw-r--r-- | stanza/src/client/message.rs | 19 | ||||
-rw-r--r-- | stanza/src/client/presence.rs | 19 | ||||
-rw-r--r-- | stanza/src/lib.rs | 2 | ||||
-rw-r--r-- | stanza/src/xep_0203.rs | 34 |
6 files changed, 75 insertions, 4 deletions
@@ -1,4 +1,5 @@ [workspace] +resolver = "2" members = [ "luz", diff --git a/stanza/Cargo.toml b/stanza/Cargo.toml index cb498d8..f21ac1e 100644 --- a/stanza/Cargo.toml +++ b/stanza/Cargo.toml @@ -7,3 +7,7 @@ edition = "2021" peanuts = { version = "0.1.0", path = "../../peanuts" } jid = { version = "0.1.0", path = "../jid" } thiserror = "2.0.11" +chrono = { version = "0.4.40", optional = true } + +[features] +xep_0203 = ["dep:chrono"] diff --git a/stanza/src/client/message.rs b/stanza/src/client/message.rs index 893e7cf..192390b 100644 --- a/stanza/src/client/message.rs +++ b/stanza/src/client/message.rs @@ -6,6 +6,9 @@ use peanuts::{ DeserializeError, Element, XML_NS, }; +#[cfg(feature = "xep_0203")] +use crate::xep_0203::Delay; + use super::XMLNS; #[derive(Debug, Clone)] @@ -20,6 +23,8 @@ pub struct Message { pub subject: Option<Subject>, pub body: Option<Body>, pub thread: Option<Thread>, + #[cfg(feature = "xep_0203")] + pub delay: Option<Delay>, } impl FromElement for Message { @@ -37,6 +42,9 @@ impl FromElement for Message { let body = element.child_opt()?; let thread = element.child_opt()?; + #[cfg(feature = "xep_0203")] + let delay = element.child_opt()?; + Ok(Message { from, id, @@ -46,13 +54,15 @@ impl FromElement for Message { subject, body, thread, + #[cfg(feature = "xep_0203")] + delay, }) } } impl IntoElement for Message { fn builder(&self) -> peanuts::element::ElementBuilder { - Element::builder("message", Some(XMLNS)) + let builder = Element::builder("message", Some(XMLNS)) .push_attribute_opt("from", self.from.clone()) .push_attribute_opt("id", self.id.clone()) .push_attribute_opt("to", self.to.clone()) @@ -66,7 +76,12 @@ impl IntoElement for Message { .push_attribute_opt_namespaced(XML_NS, "lang", self.lang.clone()) .push_child_opt(self.subject.clone()) .push_child_opt(self.body.clone()) - .push_child_opt(self.thread.clone()) + .push_child_opt(self.thread.clone()); + + #[cfg(feature = "xep_0203")] + let builder = builder.push_child_opt(self.delay.clone()); + + builder } } diff --git a/stanza/src/client/presence.rs b/stanza/src/client/presence.rs index 1603ace..ae38756 100644 --- a/stanza/src/client/presence.rs +++ b/stanza/src/client/presence.rs @@ -6,6 +6,9 @@ use peanuts::{ DeserializeError, Element, XML_NS, }; +#[cfg(feature = "xep_0203")] +use crate::xep_0203::Delay; + use super::{error::Error, XMLNS}; #[derive(Debug, Clone)] @@ -19,6 +22,8 @@ pub struct Presence { pub show: Option<Show>, pub status: Option<Status>, pub priority: Option<Priority>, + #[cfg(feature = "xep_0203")] + pub delay: Option<Delay>, // TODO: ##other // other: Vec<Other>, pub errors: Vec<Error>, @@ -40,6 +45,9 @@ impl FromElement for Presence { let priority = element.child_opt()?; let errors = element.children()?; + #[cfg(feature = "xep_0203")] + let delay = element.child_opt()?; + Ok(Presence { from, id, @@ -50,13 +58,15 @@ impl FromElement for Presence { status, priority, errors, + #[cfg(feature = "xep_0203")] + delay, }) } } impl IntoElement for Presence { fn builder(&self) -> peanuts::element::ElementBuilder { - Element::builder("presence", Some(XMLNS)) + let builder = Element::builder("presence", Some(XMLNS)) .push_attribute_opt("from", self.from.clone()) .push_attribute_opt("id", self.id.clone()) .push_attribute_opt("to", self.to.clone()) @@ -65,7 +75,12 @@ impl IntoElement for Presence { .push_child_opt(self.show) .push_child_opt(self.status.clone()) .push_child_opt(self.priority) - .push_children(self.errors.clone()) + .push_children(self.errors.clone()); + + #[cfg(feature = "xep_0203")] + let builder = builder.push_child_opt(self.delay.clone()); + + builder } } diff --git a/stanza/src/lib.rs b/stanza/src/lib.rs index 4120207..4629c07 100644 --- a/stanza/src/lib.rs +++ b/stanza/src/lib.rs @@ -9,5 +9,7 @@ pub mod starttls; pub mod stream; pub mod stream_error; pub mod xep_0199; +#[cfg(feature = "xep_0203")] +pub mod xep_0203; pub static XML_VERSION: VersionInfo = VersionInfo::One; diff --git a/stanza/src/xep_0203.rs b/stanza/src/xep_0203.rs new file mode 100644 index 0000000..b8f9239 --- /dev/null +++ b/stanza/src/xep_0203.rs @@ -0,0 +1,34 @@ +use chrono::{DateTime, Utc}; +use jid::JID; +use peanuts::{ + element::{FromElement, IntoElement}, + Element, +}; + +pub const XMLNS: &str = "urn:xmpp:delay"; + +#[derive(Debug, Clone)] +pub struct Delay { + pub from: Option<JID>, + pub stamp: DateTime<Utc>, +} + +impl FromElement for Delay { + fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> { + element.check_name("delay")?; + element.check_namespace(XMLNS)?; + + let from = element.attribute_opt("from")?; + let stamp = element.attribute("stamp")?; + + Ok(Delay { from, stamp }) + } +} + +impl IntoElement for Delay { + fn builder(&self) -> peanuts::element::ElementBuilder { + Element::builder("delay", Some(XMLNS)) + .push_attribute_opt("from", self.from.clone()) + .push_attribute("stamp", self.stamp.format("%C%y-%m-%dT%H:%M:%S%.3f%:z")) + } +} |