From cd7bb95c0a31d187bfe25bad15043f0b33b111cf Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Wed, 2 Aug 2023 00:56:38 +0100 Subject: implement resource binding --- src/client/encrypted.rs | 55 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 8 deletions(-) (limited to 'src/client') diff --git a/src/client/encrypted.rs b/src/client/encrypted.rs index e8b7271..86aba13 100644 --- a/src/client/encrypted.rs +++ b/src/client/encrypted.rs @@ -11,19 +11,22 @@ use tokio::net::TcpStream; use tokio_native_tls::TlsStream; use crate::stanza::{ - sasl::{Auth, Response}, - stream::{Stream, StreamFeature}, -}; -use crate::stanza::{ + bind::Bind, + iq::IQ, sasl::{Challenge, Success}, Element, }; +use crate::stanza::{ + sasl::{Auth, Response}, + stream::{Stream, StreamFeature}, +}; use crate::Jabber; +use crate::JabberError; use crate::Result; pub struct JabberClient<'j> { - reader: Reader>>>, - writer: Writer>>, + pub reader: Reader>>>, + pub writer: Writer>>, jabber: &'j mut Jabber<'j>, } @@ -64,15 +67,19 @@ impl<'j> JabberClient<'j> { pub async fn negotiate(&mut self) -> Result<()> { loop { - println!("loop"); + println!("negotiate loop"); let features = self.get_features().await?; println!("features: {:?}", features); + match &features[0] { StreamFeature::Sasl(sasl) => { println!("sasl?"); self.sasl(&sasl).await?; } - StreamFeature::Bind => todo!(), + StreamFeature::Bind => { + self.bind().await?; + return Ok(()); + } x => println!("{:?}", x), } } @@ -165,4 +172,36 @@ impl<'j> JabberClient<'j> { self.start_stream().await?; Ok(()) } + + pub async fn bind(&mut self) -> Result<()> { + match &self.jabber.jid.resourcepart { + Some(resource) => { + println!("setting resource"); + let bind = Bind { + resource: Some(resource.clone()), + jid: None, + }; + let result: Bind = IQ::set(self, None, None, bind).await?.try_into()?; + if let Some(jid) = result.jid { + println!("{}", jid); + self.jabber.jid = jid; + return Ok(()); + } + } + None => { + println!("not setting resource"); + let bind = Bind { + resource: None, + jid: None, + }; + let result: Bind = IQ::set(self, None, None, bind).await?.try_into()?; + if let Some(jid) = result.jid { + println!("{}", jid); + self.jabber.jid = jid; + return Ok(()); + } + } + } + Err(JabberError::BindError) + } } -- cgit