Compare commits
No commits in common. "1b91ff690488b65b552c90bd5392b9a300c8c981" and "4886396044356d2676a77c3900af796fe7641f42" have entirely different histories.
1b91ff6904
...
4886396044
32
Cargo.toml
32
Cargo.toml
|
@ -1,7 +1,27 @@
|
|||
[workspace]
|
||||
[package]
|
||||
name = "luz"
|
||||
authors = ["cel <cel@blos.sm>"]
|
||||
version = "0.0.1"
|
||||
edition = "2021"
|
||||
|
||||
members = [
|
||||
"luz",
|
||||
"jabber",
|
||||
"stanza", "jid",
|
||||
]
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
async-recursion = "1.0.4"
|
||||
async-trait = "0.1.68"
|
||||
lazy_static = "1.4.0"
|
||||
nanoid = "0.4.0"
|
||||
# TODO: remove unneeded features
|
||||
rsasl = { version = "2.0.1", path = "../rsasl", default_features = false, features = ["provider_base64", "plain", "config_builder", "scram-sha-1"] }
|
||||
tokio = { version = "1.28", features = ["full"] }
|
||||
tokio-native-tls = "0.3.1"
|
||||
tracing = "0.1.40"
|
||||
trust-dns-resolver = "0.22.0"
|
||||
try_map = "0.3.1"
|
||||
peanuts = { version = "0.1.0", path = "../peanuts" }
|
||||
futures = "0.3.31"
|
||||
|
||||
[dev-dependencies]
|
||||
test-log = { version = "0.2", features = ["trace"] }
|
||||
env_logger = "*"
|
||||
tracing-subscriber = {version = "0.3", default-features = false, features = ["env-filter", "fmt"]}
|
||||
|
|
10
README.md
10
README.md
|
@ -1,10 +0,0 @@
|
|||
# jabber client library
|
||||
|
||||
## TODO:
|
||||
|
||||
- [ ] error states for all negotiation parts
|
||||
- [ ] better errors
|
||||
- [x] rename structs
|
||||
- [x] remove commented code
|
||||
- [ ] asynchronous connect (with take_mut?)
|
||||
- [ ] split into separate crates: stanza, jabber, and luz
|
File diff suppressed because it is too large
Load Diff
|
@ -1,29 +0,0 @@
|
|||
[package]
|
||||
name = "jabber"
|
||||
authors = ["cel <cel@bunny.garden>"]
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
async-recursion = "1.0.4"
|
||||
async-trait = "0.1.68"
|
||||
lazy_static = "1.4.0"
|
||||
nanoid = "0.4.0"
|
||||
# TODO: remove unneeded features
|
||||
rsasl = { version = "2.0.1", path = "../../rsasl", default_features = false, features = ["provider_base64", "plain", "config_builder", "scram-sha-1"] }
|
||||
tokio = { version = "1.28", features = ["full"] }
|
||||
tokio-native-tls = "0.3.1"
|
||||
tracing = "0.1.40"
|
||||
trust-dns-resolver = "0.22.0"
|
||||
try_map = "0.3.1"
|
||||
stanza = { version = "0.1.0", path = "../stanza" }
|
||||
peanuts = { version = "0.1.0", path = "../../peanuts" }
|
||||
jid = { version = "0.1.0", path = "../jid" }
|
||||
futures = "0.3.31"
|
||||
|
||||
[dev-dependencies]
|
||||
test-log = { version = "0.2", features = ["trace"] }
|
||||
env_logger = "*"
|
||||
tracing-subscriber = {version = "0.3", default-features = false, features = ["env-filter", "fmt"]}
|
|
@ -1,6 +0,0 @@
|
|||
[package]
|
||||
name = "jid"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
|
@ -1,6 +0,0 @@
|
|||
[package]
|
||||
name = "luz"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
|
@ -1,3 +0,0 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
|
@ -1,16 +1,16 @@
|
|||
use std::{pin::pin, sync::Arc, task::Poll};
|
||||
|
||||
use futures::{Sink, Stream, StreamExt};
|
||||
use jid::ParseError;
|
||||
use rsasl::config::SASLConfig;
|
||||
use stanza::{
|
||||
client::Stanza,
|
||||
sasl::Mechanisms,
|
||||
stream::{Feature, Features},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
connection::{Tls, Unencrypted},
|
||||
jid::ParseError,
|
||||
stanza::{
|
||||
client::Stanza,
|
||||
sasl::Mechanisms,
|
||||
stream::{Feature, Features},
|
||||
},
|
||||
Connection, Error, JabberStream, Result, JID,
|
||||
};
|
||||
|
||||
|
@ -44,8 +44,6 @@ impl JabberClient {
|
|||
pub async fn connect(&mut self) -> Result<()> {
|
||||
match &self.connection {
|
||||
ConnectionState::Disconnected => {
|
||||
// TODO: actually set the self.connection as it is connecting, make more asynchronous (mutex while connecting?)
|
||||
// perhaps use take_mut?
|
||||
self.connection = ConnectionState::Disconnected
|
||||
.connect(&mut self.jid, self.password.clone(), &mut self.server)
|
||||
.await?;
|
||||
|
@ -55,16 +53,6 @@ impl JabberClient {
|
|||
ConnectionState::Connected(_jabber_stream) => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_stanza(&mut self, stanza: &Stanza) -> Result<()> {
|
||||
match &mut self.connection {
|
||||
ConnectionState::Disconnected => return Err(Error::Disconnected),
|
||||
ConnectionState::Connecting(_connecting) => return Err(Error::Connecting),
|
||||
ConnectionState::Connected(jabber_stream) => {
|
||||
Ok(jabber_stream.send_stanza(stanza).await?)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for JabberClient {
|
||||
|
@ -113,7 +101,7 @@ impl ConnectionState {
|
|||
))
|
||||
}
|
||||
Connecting::InsecureGotFeatures((features, jabber_stream)) => {
|
||||
match features.negotiate().ok_or(Error::Negotiation)? {
|
||||
match features.negotiate()? {
|
||||
Feature::StartTls(_start_tls) => {
|
||||
self =
|
||||
ConnectionState::Connecting(Connecting::StartTls(jabber_stream))
|
||||
|
@ -138,7 +126,7 @@ impl ConnectionState {
|
|||
))
|
||||
}
|
||||
Connecting::GotFeatures((features, jabber_stream)) => {
|
||||
match features.negotiate().ok_or(Error::Negotiation)? {
|
||||
match features.negotiate()? {
|
||||
Feature::StartTls(_start_tls) => return Err(Error::AlreadyTls),
|
||||
Feature::Sasl(mechanisms) => {
|
||||
self = ConnectionState::Connecting(Connecting::Sasl(
|
||||
|
@ -190,6 +178,35 @@ impl Connecting {
|
|||
}
|
||||
}
|
||||
|
||||
impl Features {
|
||||
pub fn negotiate(self) -> Result<Feature> {
|
||||
if let Some(Feature::StartTls(s)) = self
|
||||
.features
|
||||
.iter()
|
||||
.find(|feature| matches!(feature, Feature::StartTls(_s)))
|
||||
{
|
||||
// TODO: avoid clone
|
||||
return Ok(Feature::StartTls(s.clone()));
|
||||
} else if let Some(Feature::Sasl(mechanisms)) = self
|
||||
.features
|
||||
.iter()
|
||||
.find(|feature| matches!(feature, Feature::Sasl(_)))
|
||||
{
|
||||
// TODO: avoid clone
|
||||
return Ok(Feature::Sasl(mechanisms.clone()));
|
||||
} else if let Some(Feature::Bind) = self
|
||||
.features
|
||||
.into_iter()
|
||||
.find(|feature| matches!(feature, Feature::Bind))
|
||||
{
|
||||
Ok(Feature::Bind)
|
||||
} else {
|
||||
// TODO: better error
|
||||
return Err(Error::Negotiation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub enum InsecureConnecting {
|
||||
Disconnected,
|
||||
ConnectionEstablished(Connection),
|
||||
|
@ -200,6 +217,38 @@ pub enum InsecureConnecting {
|
|||
Bound(JabberStream<Tls>),
|
||||
}
|
||||
|
||||
impl Sink<Stanza> for JabberClient {
|
||||
type Error = Error;
|
||||
|
||||
fn poll_ready(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::result::Result<(), Self::Error>> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn start_send(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
item: Stanza,
|
||||
) -> std::result::Result<(), Self::Error> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn poll_flush(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::result::Result<(), Self::Error>> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn poll_close(
|
||||
self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<std::result::Result<(), Self::Error>> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::time::Duration;
|
|
@ -1,10 +1,9 @@
|
|||
use std::str::Utf8Error;
|
||||
|
||||
use jid::ParseError;
|
||||
use rsasl::mechname::MechanismNameError;
|
||||
use stanza::client::error::Error as ClientError;
|
||||
use stanza::sasl::Failure;
|
||||
use stanza::stream::Error as StreamError;
|
||||
|
||||
use crate::stanza::client::error::Error as ClientError;
|
||||
use crate::{jid::ParseError, stanza::sasl::Failure};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
|
@ -23,10 +22,7 @@ pub enum Error {
|
|||
JID(ParseError),
|
||||
Authentication(Failure),
|
||||
ClientError(ClientError),
|
||||
StreamError(StreamError),
|
||||
MissingError,
|
||||
Disconnected,
|
||||
Connecting,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
|
@ -2,25 +2,26 @@ use std::pin::pin;
|
|||
use std::str::{self, FromStr};
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_recursion::async_recursion;
|
||||
use futures::StreamExt;
|
||||
use jid::JID;
|
||||
use peanuts::element::{FromContent, IntoElement};
|
||||
use peanuts::{Reader, Writer};
|
||||
use rsasl::prelude::{Mechname, SASLClient, SASLConfig};
|
||||
use stanza::bind::{Bind, BindType, FullJidType, ResourceType};
|
||||
use stanza::client::iq::{Iq, IqType, Query};
|
||||
use stanza::client::Stanza;
|
||||
use stanza::sasl::{Auth, Challenge, Mechanisms, Response, ServerResponse};
|
||||
use stanza::starttls::{Proceed, StartTls};
|
||||
use stanza::stream::{Features, Stream};
|
||||
use stanza::XML_VERSION;
|
||||
use tokio::io::{AsyncRead, AsyncWrite, ReadHalf, WriteHalf};
|
||||
use tokio_native_tls::native_tls::TlsConnector;
|
||||
use tracing::{debug, instrument};
|
||||
|
||||
use crate::connection::{Tls, Unencrypted};
|
||||
use crate::error::Error;
|
||||
use crate::Result;
|
||||
use crate::stanza::bind::{Bind, BindType, FullJidType, ResourceType};
|
||||
use crate::stanza::client::iq::{Iq, IqType, Query};
|
||||
use crate::stanza::client::Stanza;
|
||||
use crate::stanza::sasl::{Auth, Challenge, Mechanisms, Response, ServerResponse};
|
||||
use crate::stanza::starttls::{Proceed, StartTls};
|
||||
use crate::stanza::stream::{Feature, Features, Stream};
|
||||
use crate::stanza::XML_VERSION;
|
||||
use crate::JID;
|
||||
use crate::{Connection, Result};
|
||||
|
||||
// open stream (streams started)
|
||||
pub struct JabberStream<S> {
|
||||
|
@ -49,7 +50,6 @@ where
|
|||
S: AsyncRead + AsyncWrite + Unpin + Send + std::fmt::Debug,
|
||||
JabberStream<S>: std::fmt::Debug,
|
||||
{
|
||||
#[instrument]
|
||||
pub async fn sasl(mut self, mechanisms: Mechanisms, sasl_config: Arc<SASLConfig>) -> Result<S> {
|
||||
let sasl = SASLClient::new(sasl_config);
|
||||
let mut offered_mechs: Vec<&Mechname> = Vec::new();
|
||||
|
@ -139,7 +139,6 @@ where
|
|||
Ok(stream)
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
pub async fn bind(mut self, jid: &mut JID) -> Result<Self> {
|
||||
let iq_id = nanoid::nanoid!();
|
||||
if let Some(resource) = &jid.resourcepart {
|
||||
|
@ -267,7 +266,6 @@ where
|
|||
Ok(Self { reader, writer })
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
pub async fn get_features(mut self) -> Result<(Features, Self)> {
|
||||
debug!("getting features");
|
||||
let features: Features = self.reader.read().await?;
|
||||
|
@ -278,16 +276,91 @@ where
|
|||
pub fn into_inner(self) -> S {
|
||||
self.reader.into_inner().unsplit(self.writer.into_inner())
|
||||
}
|
||||
|
||||
pub async fn send_stanza(&mut self, stanza: &Stanza) -> Result<()> {
|
||||
self.writer.write(stanza).await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl JabberStream<Unencrypted> {
|
||||
#[instrument]
|
||||
pub async fn starttls(mut self, domain: impl AsRef<str> + std::fmt::Debug) -> Result<Tls> {
|
||||
// pub async fn negotiate<S: AsyncRead + AsyncWrite + Unpin>(
|
||||
// mut self,
|
||||
// features: Features,
|
||||
// ) -> Result<Feature> {
|
||||
// // TODO: timeout
|
||||
// if let Some(Feature::StartTls(_)) = features
|
||||
// .features
|
||||
// .iter()
|
||||
// .find(|feature| matches!(feature, Feature::StartTls(_s)))
|
||||
// {
|
||||
// return Ok(self);
|
||||
// } else {
|
||||
// // TODO: better error
|
||||
// return Err(Error::TlsRequired);
|
||||
// }
|
||||
// }
|
||||
|
||||
// #[async_recursion]
|
||||
// pub async fn negotiate_tls_optional(mut self) -> Result<Connection> {
|
||||
// self.start_stream().await?;
|
||||
// // TODO: timeout
|
||||
// let features = self.get_features().await?.features;
|
||||
// if let Some(Feature::StartTls(_)) = features
|
||||
// .iter()
|
||||
// .find(|feature| matches!(feature, Feature::StartTls(_s)))
|
||||
// {
|
||||
// let jabber = self.starttls().await?;
|
||||
// let jabber = jabber.negotiate().await?;
|
||||
// return Ok(Connection::Encrypted(jabber));
|
||||
// } else if let (Some(sasl_config), Some(Feature::Sasl(mechanisms))) = (
|
||||
// self.auth.clone(),
|
||||
// features
|
||||
// .iter()
|
||||
// .find(|feature| matches!(feature, Feature::Sasl(_))),
|
||||
// ) {
|
||||
// self.sasl(mechanisms.clone(), sasl_config).await?;
|
||||
// let jabber = self.negotiate_tls_optional().await?;
|
||||
// Ok(jabber)
|
||||
// } else if let Some(Feature::Bind) = features
|
||||
// .iter()
|
||||
// .find(|feature| matches!(feature, Feature::Bind))
|
||||
// {
|
||||
// self.bind().await?;
|
||||
// Ok(Connection::Unencrypted(self))
|
||||
// } else {
|
||||
// // TODO: better error
|
||||
// return Err(Error::Negotiation);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
impl JabberStream<Tls> {
|
||||
// #[async_recursion]
|
||||
// pub async fn negotiate(mut self) -> Result<JabberStream<Tls>> {
|
||||
// self.start_stream().await?;
|
||||
// let features = self.get_features().await?.features;
|
||||
|
||||
// if let (Some(sasl_config), Some(Feature::Sasl(mechanisms))) = (
|
||||
// self.auth.clone(),
|
||||
// features
|
||||
// .iter()
|
||||
// .find(|feature| matches!(feature, Feature::Sasl(_))),
|
||||
// ) {
|
||||
// // TODO: avoid clone
|
||||
// self.sasl(mechanisms.clone(), sasl_config).await?;
|
||||
// let jabber = self.negotiate().await?;
|
||||
// Ok(jabber)
|
||||
// } else if let Some(Feature::Bind) = features
|
||||
// .iter()
|
||||
// .find(|feature| matches!(feature, Feature::Bind))
|
||||
// {
|
||||
// self.bind().await?;
|
||||
// Ok(self)
|
||||
// } else {
|
||||
// // TODO: better error
|
||||
// return Err(Error::Negotiation);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
impl JabberStream<Unencrypted> {
|
||||
pub async fn starttls(mut self, domain: impl AsRef<str>) -> Result<Tls> {
|
||||
self.writer
|
||||
.write_full(&StartTls { required: false })
|
||||
.await?;
|
||||
|
@ -299,6 +372,8 @@ impl JabberStream<Unencrypted> {
|
|||
.connect(domain.as_ref(), stream)
|
||||
.await
|
||||
{
|
||||
// let (read, write) = tokio::io::split(tlsstream);
|
||||
// let client = JabberStream::new(read, write);
|
||||
return Ok(tls_stream);
|
||||
} else {
|
||||
return Err(Error::Connection);
|
|
@ -19,6 +19,15 @@ pub enum ParseError {
|
|||
Malformed(String),
|
||||
}
|
||||
|
||||
impl From<ParseError> for peanuts::Error {
|
||||
fn from(e: ParseError) -> Self {
|
||||
match e {
|
||||
ParseError::Empty => peanuts::Error::DeserializeError("".to_string()),
|
||||
ParseError::Malformed(e) => peanuts::Error::DeserializeError(e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl JID {
|
||||
pub fn new(
|
||||
localpart: Option<String>,
|
|
@ -5,12 +5,14 @@
|
|||
pub mod client;
|
||||
pub mod connection;
|
||||
pub mod error;
|
||||
pub mod jabber_stream;
|
||||
pub mod jabber;
|
||||
pub mod jid;
|
||||
pub mod stanza;
|
||||
|
||||
pub use connection::Connection;
|
||||
use connection::Tls;
|
||||
pub use error::Error;
|
||||
pub use jabber_stream::JabberStream;
|
||||
pub use jabber::JabberStream;
|
||||
pub use jid::JID;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
|
@ -1,9 +1,10 @@
|
|||
use jid::JID;
|
||||
use peanuts::{
|
||||
element::{FromElement, IntoElement},
|
||||
Element,
|
||||
};
|
||||
|
||||
use crate::JID;
|
||||
|
||||
pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-bind";
|
||||
|
||||
#[derive(Clone)]
|
|
@ -3,8 +3,8 @@ use std::str::FromStr;
|
|||
use peanuts::element::{FromElement, IntoElement};
|
||||
use peanuts::{DeserializeError, Element};
|
||||
|
||||
use crate::stanza_error::Error as StanzaError;
|
||||
use crate::stanza_error::Text;
|
||||
use crate::stanza::stanza_error::Error as StanzaError;
|
||||
use crate::stanza::stanza_error::Text;
|
||||
|
||||
use super::XMLNS;
|
||||
|
|
@ -1,14 +1,16 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use jid::JID;
|
||||
use peanuts::{
|
||||
element::{FromElement, IntoElement},
|
||||
DeserializeError, Element, XML_NS,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
stanza::{
|
||||
bind::{self, Bind},
|
||||
client::error::Error,
|
||||
},
|
||||
JID,
|
||||
};
|
||||
|
||||
use super::XMLNS;
|
|
@ -1,11 +1,12 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use jid::JID;
|
||||
use peanuts::{
|
||||
element::{FromElement, IntoElement},
|
||||
DeserializeError, Element, XML_NS,
|
||||
};
|
||||
|
||||
use crate::JID;
|
||||
|
||||
use super::XMLNS;
|
||||
|
||||
pub struct Message {
|
|
@ -1,11 +1,12 @@
|
|||
use std::str::FromStr;
|
||||
|
||||
use jid::JID;
|
||||
use peanuts::{
|
||||
element::{FromElement, IntoElement},
|
||||
DeserializeError, Element, XML_NS,
|
||||
};
|
||||
|
||||
use crate::JID;
|
||||
|
||||
use super::{error::Error, XMLNS};
|
||||
|
||||
pub struct Presence {
|
|
@ -4,6 +4,7 @@ use peanuts::{
|
|||
element::{FromElement, IntoElement},
|
||||
DeserializeError, Element,
|
||||
};
|
||||
use tracing::debug;
|
||||
|
||||
pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-sasl";
|
||||
|
||||
|
@ -16,11 +17,15 @@ impl FromElement for Mechanisms {
|
|||
fn from_element(mut element: Element) -> peanuts::element::DeserializeResult<Self> {
|
||||
element.check_name("mechanisms")?;
|
||||
element.check_namespace(XMLNS)?;
|
||||
debug!("getting mechanisms");
|
||||
let mechanisms: Vec<Mechanism> = element.pop_children()?;
|
||||
debug!("gottting mechanisms");
|
||||
let mechanisms = mechanisms
|
||||
.into_iter()
|
||||
.map(|Mechanism(mechanism)| mechanism)
|
||||
.collect();
|
||||
debug!("gottting mechanisms");
|
||||
|
||||
Ok(Mechanisms { mechanisms })
|
||||
}
|
||||
}
|
||||
|
@ -130,6 +135,7 @@ pub enum ServerResponse {
|
|||
|
||||
impl FromElement for ServerResponse {
|
||||
fn from_element(element: Element) -> peanuts::element::DeserializeResult<Self> {
|
||||
debug!("identification: {:?}", element.identify());
|
||||
match element.identify() {
|
||||
(Some(XMLNS), "challenge") => {
|
||||
Ok(ServerResponse::Challenge(Challenge::from_element(element)?))
|
|
@ -1,10 +1,12 @@
|
|||
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::bind;
|
||||
use crate::stanza::bind;
|
||||
use crate::JID;
|
||||
|
||||
use super::sasl::{self, Mechanisms};
|
||||
use super::starttls::{self, StartTls};
|
||||
|
@ -97,34 +99,6 @@ 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())
|
||||
|
@ -138,7 +112,9 @@ 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 })
|
||||
}
|
||||
|
@ -165,20 +141,29 @@ 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") => Ok(Feature::Bind),
|
||||
_ => Ok(Feature::Unknown),
|
||||
(Some(bind::XMLNS), "bind") => {
|
||||
debug!("identified bind");
|
||||
Ok(Feature::Bind)
|
||||
}
|
||||
_ => {
|
||||
debug!("identified unknown feature");
|
||||
Ok(Feature::Unknown)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error {
|
||||
error: StreamError,
|
||||
text: Option<Text>,
|
|
@ -5,7 +5,7 @@ use peanuts::{
|
|||
|
||||
pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-streams";
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone)]
|
||||
pub enum Error {
|
||||
BadFormat,
|
||||
BadNamespacePrefix,
|
||||
|
@ -110,7 +110,7 @@ impl IntoElement for Error {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone)]
|
||||
pub struct Text {
|
||||
text: Option<String>,
|
||||
lang: Option<String>,
|
|
@ -1,8 +0,0 @@
|
|||
[package]
|
||||
name = "stanza"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
peanuts = { version = "0.1.0", path = "../../peanuts" }
|
||||
jid = { version = "0.1.0", path = "../jid" }
|
Loading…
Reference in New Issue