delete JabberClient

This commit is contained in:
cel 🌸 2025-01-12 23:10:03 +00:00
parent e6c97ab828
commit 866e134371
2 changed files with 21 additions and 80 deletions

View File

@ -22,71 +22,16 @@ use crate::{
Connection, Error, JabberStream, Result, JID,
};
// feed it client stanzas, receive client stanzas
pub struct JabberClient {
connection: Option<BoundJabberStream<Tls>>,
jid: JID,
// TODO: have reconnection be handled by another part, so creds don't need to be stored in object
password: Arc<SASLConfig>,
server: String,
}
impl JabberClient {
pub fn new(
jid: impl TryInto<JID, Error = ParseError>,
password: impl ToString,
) -> Result<JabberClient> {
let jid = jid.try_into()?;
let sasl_config = SASLConfig::with_credentials(
None,
jid.localpart.clone().ok_or(Error::NoLocalpart)?,
password.to_string(),
)?;
Ok(JabberClient {
connection: None,
jid: jid.clone(),
password: sasl_config,
server: jid.domainpart,
})
}
pub fn jid(&self) -> JID {
self.jid.clone()
}
pub async fn connect(&mut self) -> Result<()> {
match &self.connection {
Some(_) => Ok(()),
None => {
self.connection = Some(
connect_and_login(&mut self.jid, self.password.clone(), &mut self.server)
.await?,
);
Ok(())
}
}
}
pub(crate) fn into_inner(self) -> Result<BoundJabberStream<Tls>> {
self.connection.ok_or(Error::Disconnected)
}
// 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?)
// }
// }
// }
}
pub async fn connect_and_login(
jid: &mut JID,
auth: Arc<SASLConfig>,
mut jid: &mut JID,
password: impl AsRef<str>,
server: &mut String,
) -> Result<BoundJabberStream<Tls>> {
let auth = SASLConfig::with_credentials(
None,
jid.localpart.clone().ok_or(Error::NoLocalpart)?,
password.as_ref().to_string(),
)?;
let mut conn_state = Connecting::start(&server).await?;
loop {
match conn_state {
@ -177,8 +122,8 @@ pub enum InsecureConnecting {
mod tests {
use std::{sync::Arc, time::Duration};
use super::JabberClient;
use futures::{SinkExt, StreamExt};
use jid::JID;
use stanza::{
client::{
iq::{Iq, IqType, Query},
@ -190,21 +135,25 @@ mod tests {
use tokio::{sync::Mutex, time::sleep};
use tracing::info;
use super::connect_and_login;
#[test(tokio::test)]
async fn login() {
let mut client = JabberClient::new("test@blos.sm", "slayed").unwrap();
client.connect().await.unwrap();
let mut jid: JID = "test@blos.sm".try_into().unwrap();
let client = connect_and_login(&mut jid, "slayed", &mut "blos.sm".to_string())
.await
.unwrap();
sleep(Duration::from_secs(5)).await
}
#[test(tokio::test)]
async fn ping_parallel() {
let mut client = JabberClient::new("test@blos.sm", "slayed").unwrap();
client.connect().await.unwrap();
sleep(Duration::from_secs(5)).await;
let jid = client.jid.clone();
let server = client.server.clone();
let (mut read, mut write) = client.into_inner().unwrap().split();
let mut jid: JID = "test@blos.sm".try_into().unwrap();
let mut server = "blos.sm".to_string();
let client = connect_and_login(&mut jid, "slayed", &mut server)
.await
.unwrap();
let (mut read, mut write) = client.split();
tokio::join!(
async {

View File

@ -15,15 +15,7 @@ pub use jid::JID;
pub type Result<T> = std::result::Result<T, Error>;
pub async fn login<J: AsRef<str>, P: AsRef<str>>(jid: J, password: P) -> Result<JabberStream<Tls>> {
todo!()
// Ok(Connection::connect_user(jid, password.as_ref().to_string())
// .await?
// .ensure_tls()
// .await?
// .negotiate()
// .await?)
}
pub use client::connect_and_login;
#[cfg(test)]
mod tests {