aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@bunny.garden>2025-01-12 23:10:03 +0000
committerLibravatar cel 🌸 <cel@bunny.garden>2025-01-12 23:10:03 +0000
commit866e134371a88c221862d3646f67e6cff6624a30 (patch)
treea3687e5d8779c7c8f6d339c901aa8a4ba4f4f5d0
parente6c97ab82880ad4cd12b05bc1c8f2a0a3413735c (diff)
downloadluz-866e134371a88c221862d3646f67e6cff6624a30.tar.gz
luz-866e134371a88c221862d3646f67e6cff6624a30.tar.bz2
luz-866e134371a88c221862d3646f67e6cff6624a30.zip
delete JabberClient
-rw-r--r--jabber/src/client.rs91
-rw-r--r--jabber/src/lib.rs10
2 files changed, 21 insertions, 80 deletions
diff --git a/jabber/src/client.rs b/jabber/src/client.rs
index 9d32682..f741352 100644
--- a/jabber/src/client.rs
+++ b/jabber/src/client.rs
@@ -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 {
diff --git a/jabber/src/lib.rs b/jabber/src/lib.rs
index bcd63db..7258f38 100644
--- a/jabber/src/lib.rs
+++ b/jabber/src/lib.rs
@@ -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 {