From ba94ee66fafbabd63d6d1ed5edf435d4c46c6796 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Fri, 20 Oct 2023 04:51:56 +0100 Subject: WIP: refactor to parse incoming stream as state machine --- src/jabber.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'src/jabber.rs') diff --git a/src/jabber.rs b/src/jabber.rs index 1a7eddb..d48eb9c 100644 --- a/src/jabber.rs +++ b/src/jabber.rs @@ -3,7 +3,7 @@ use std::net::{IpAddr, SocketAddr}; use std::str::FromStr; use std::sync::Arc; -use quick_xml::{Reader, Writer}; +use quick_xml::{NsReader, Writer}; use rsasl::prelude::SASLConfig; use tokio::io::BufReader; use tokio::net::TcpStream; @@ -22,7 +22,7 @@ pub struct Jabber<'j> { } impl<'j> Jabber<'j> { - pub fn new(jid: JID, password: String) -> Result { + pub fn user(jid: JID, password: String) -> Result { let server = jid.domainpart.clone(); let auth = SASLConfig::with_credentials(None, jid.localpart.clone().unwrap(), password)?; println!("auth: {:?}", auth); @@ -36,7 +36,7 @@ impl<'j> Jabber<'j> { pub async fn login(&'j mut self) -> Result> { let mut client = self.connect().await?.ensure_tls().await?; - println!("negotiation"); + client.start_stream().await?; client.negotiate().await?; Ok(client) } @@ -106,6 +106,7 @@ impl<'j> Jabber<'j> { socket_addrs } + /// establishes a connection to the server pub async fn connect(&'j mut self) -> Result { for (socket_addr, is_tls) in self.get_sockets().await { println!("trying {}", socket_addr); @@ -118,21 +119,18 @@ impl<'j> Jabber<'j> { .await { let (read, write) = tokio::io::split(stream); - let reader = Reader::from_reader(BufReader::new(read)); + let reader = NsReader::from_reader(BufReader::new(read)); let writer = Writer::new(write); - let mut client = client::encrypted::JabberClient::new(reader, writer, self); - client.start_stream().await?; + let client = client::encrypted::JabberClient::new(reader, writer, self); return Ok(JabberClientType::Encrypted(client)); } } false => { if let Ok(stream) = TcpStream::connect(socket_addr).await { let (read, write) = tokio::io::split(stream); - let reader = Reader::from_reader(BufReader::new(read)); + let reader = NsReader::from_reader(BufReader::new(read)); let writer = Writer::new(write); - let mut client = - client::unencrypted::JabberClient::new(reader, writer, self); - client.start_stream().await?; + let client = client::unencrypted::JabberClient::new(reader, writer, self); return Ok(JabberClientType::Unencrypted(client)); } } -- cgit