diff options
Diffstat (limited to '')
-rw-r--r-- | src/client/encrypted.rs | 55 |
1 files changed, 47 insertions, 8 deletions
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<BufReader<ReadHalf<TlsStream<TcpStream>>>>, - writer: Writer<WriteHalf<TlsStream<TcpStream>>>, + pub reader: Reader<BufReader<ReadHalf<TlsStream<TcpStream>>>>, + pub writer: Writer<WriteHalf<TlsStream<TcpStream>>>, 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) + } } |