diff options
Diffstat (limited to 'src/jabber.rs')
-rw-r--r-- | src/jabber.rs | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/src/jabber.rs b/src/jabber.rs index a1f6272..a1b2a2f 100644 --- a/src/jabber.rs +++ b/src/jabber.rs @@ -1,33 +1,44 @@ use std::marker::PhantomData; use std::net::{IpAddr, SocketAddr}; use std::str::FromStr; +use std::sync::Arc; use quick_xml::{Reader, Writer}; +use rsasl::prelude::SASLConfig; use tokio::io::BufReader; use tokio::net::TcpStream; use tokio_native_tls::native_tls::TlsConnector; -use crate::client; use crate::client::JabberClientType; use crate::jid::JID; +use crate::{client, JabberClient}; use crate::{JabberError, Result}; pub struct Jabber<'j> { pub jid: JID, - pub password: String, + pub auth: Arc<SASLConfig>, pub server: String, _marker: PhantomData<&'j ()>, } impl<'j> Jabber<'j> { - pub fn new(jid: JID, password: String) -> Self { + pub fn new(jid: JID, password: String) -> Result<Self> { let server = jid.domainpart.clone(); - Self { + let auth = SASLConfig::with_credentials(None, jid.as_bare().to_string(), password)?; + println!("auth: {:?}", auth); + Ok(Self { jid, - password, + auth, server, _marker: PhantomData, - } + }) + } + + pub async fn login(&'j mut self) -> Result<JabberClient<'j>> { + let mut client = self.connect().await?.ensure_tls().await?; + println!("negotiation"); + client.negotiate().await?; + Ok(client) } async fn get_sockets(&self) -> Vec<(SocketAddr, bool)> { @@ -106,9 +117,8 @@ impl<'j> Jabber<'j> { .connect(&self.server, socket) .await { - let (read, write) = tokio::io::split(stream); + let (read, writer) = tokio::io::split(stream); let reader = Reader::from_reader(BufReader::new(read)); - let writer = Writer::new(write); return Ok(JabberClientType::Encrypted( client::encrypted::JabberClient::new(reader, writer, self), )); @@ -126,6 +136,6 @@ impl<'j> Jabber<'j> { } } } - Err(JabberError::ConnectionError) + Err(JabberError::Connection) } } |