aboutsummaryrefslogtreecommitdiffstats
path: root/stanza/src/stream.rs
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@bunny.garden>2024-12-04 18:18:37 +0000
committerLibravatar cel 🌸 <cel@bunny.garden>2024-12-04 18:18:37 +0000
commit1b91ff690488b65b552c90bd5392b9a300c8c981 (patch)
tree9c290f69b26eba0393d7bbc05ba29c28ea74a26e /stanza/src/stream.rs
parent03764f8cedb3f0a55a61be0f0a59faaa6357a83a (diff)
downloadluz-1b91ff690488b65b552c90bd5392b9a300c8c981.tar.gz
luz-1b91ff690488b65b552c90bd5392b9a300c8c981.tar.bz2
luz-1b91ff690488b65b552c90bd5392b9a300c8c981.zip
use cargo workspace
Diffstat (limited to '')
-rw-r--r--stanza/src/stream.rs (renamed from src/stanza/stream.rs)50
1 files changed, 32 insertions, 18 deletions
diff --git a/src/stanza/stream.rs b/stanza/src/stream.rs
index 84d62d9..89b03d8 100644
--- a/src/stanza/stream.rs
+++ b/stanza/src/stream.rs
@@ -1,12 +1,10 @@
use std::collections::{HashMap, HashSet};
+use jid::JID;
use peanuts::element::{Content, ElementBuilder, FromElement, IntoElement, NamespaceDeclaration};
-use peanuts::XML_NS;
use peanuts::{element::Name, Element};
-use tracing::debug;
-use crate::stanza::bind;
-use crate::JID;
+use crate::bind;
use super::sasl::{self, Mechanisms};
use super::starttls::{self, StartTls};
@@ -99,6 +97,34 @@ pub struct Features {
pub features: Vec<Feature>,
}
+impl Features {
+ pub fn negotiate(self) -> Option<Feature> {
+ if let Some(Feature::StartTls(s)) = self
+ .features
+ .iter()
+ .find(|feature| matches!(feature, Feature::StartTls(_s)))
+ {
+ // TODO: avoid clone
+ return Some(Feature::StartTls(s.clone()));
+ } else if let Some(Feature::Sasl(mechanisms)) = self
+ .features
+ .iter()
+ .find(|feature| matches!(feature, Feature::Sasl(_)))
+ {
+ // TODO: avoid clone
+ return Some(Feature::Sasl(mechanisms.clone()));
+ } else if let Some(Feature::Bind) = self
+ .features
+ .into_iter()
+ .find(|feature| matches!(feature, Feature::Bind))
+ {
+ Some(Feature::Bind)
+ } else {
+ return None;
+ }
+ }
+}
+
impl IntoElement for Features {
fn builder(&self) -> ElementBuilder {
Element::builder("features", Some(XMLNS)).push_children(self.features.clone())
@@ -112,9 +138,7 @@ impl FromElement for Features {
element.check_namespace(XMLNS)?;
element.check_name("features")?;
- debug!("got features stanza");
let features = element.children()?;
- debug!("got features period");
Ok(Features { features })
}
@@ -141,25 +165,15 @@ impl IntoElement for Feature {
impl FromElement for Feature {
fn from_element(element: Element) -> peanuts::element::DeserializeResult<Self> {
- let identity = element.identify();
- debug!("identity: {:?}", identity);
match element.identify() {
(Some(starttls::XMLNS), "starttls") => {
- debug!("identified starttls");
Ok(Feature::StartTls(StartTls::from_element(element)?))
}
(Some(sasl::XMLNS), "mechanisms") => {
- debug!("identified mechanisms");
Ok(Feature::Sasl(Mechanisms::from_element(element)?))
}
- (Some(bind::XMLNS), "bind") => {
- debug!("identified bind");
- Ok(Feature::Bind)
- }
- _ => {
- debug!("identified unknown feature");
- Ok(Feature::Unknown)
- }
+ (Some(bind::XMLNS), "bind") => Ok(Feature::Bind),
+ _ => Ok(Feature::Unknown),
}
}
}