delete JabberClient
This commit is contained in:
parent
e6c97ab828
commit
866e134371
|
@ -22,71 +22,16 @@ use crate::{
|
||||||
Connection, Error, JabberStream, Result, JID,
|
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(
|
pub async fn connect_and_login(
|
||||||
jid: &mut JID,
|
mut jid: &mut JID,
|
||||||
auth: Arc<SASLConfig>,
|
password: impl AsRef<str>,
|
||||||
server: &mut String,
|
server: &mut String,
|
||||||
) -> Result<BoundJabberStream<Tls>> {
|
) -> 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?;
|
let mut conn_state = Connecting::start(&server).await?;
|
||||||
loop {
|
loop {
|
||||||
match conn_state {
|
match conn_state {
|
||||||
|
@ -177,8 +122,8 @@ pub enum InsecureConnecting {
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{sync::Arc, time::Duration};
|
use std::{sync::Arc, time::Duration};
|
||||||
|
|
||||||
use super::JabberClient;
|
|
||||||
use futures::{SinkExt, StreamExt};
|
use futures::{SinkExt, StreamExt};
|
||||||
|
use jid::JID;
|
||||||
use stanza::{
|
use stanza::{
|
||||||
client::{
|
client::{
|
||||||
iq::{Iq, IqType, Query},
|
iq::{Iq, IqType, Query},
|
||||||
|
@ -190,21 +135,25 @@ mod tests {
|
||||||
use tokio::{sync::Mutex, time::sleep};
|
use tokio::{sync::Mutex, time::sleep};
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
|
use super::connect_and_login;
|
||||||
|
|
||||||
#[test(tokio::test)]
|
#[test(tokio::test)]
|
||||||
async fn login() {
|
async fn login() {
|
||||||
let mut client = JabberClient::new("test@blos.sm", "slayed").unwrap();
|
let mut jid: JID = "test@blos.sm".try_into().unwrap();
|
||||||
client.connect().await.unwrap();
|
let client = connect_and_login(&mut jid, "slayed", &mut "blos.sm".to_string())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
sleep(Duration::from_secs(5)).await
|
sleep(Duration::from_secs(5)).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test(tokio::test)]
|
#[test(tokio::test)]
|
||||||
async fn ping_parallel() {
|
async fn ping_parallel() {
|
||||||
let mut client = JabberClient::new("test@blos.sm", "slayed").unwrap();
|
let mut jid: JID = "test@blos.sm".try_into().unwrap();
|
||||||
client.connect().await.unwrap();
|
let mut server = "blos.sm".to_string();
|
||||||
sleep(Duration::from_secs(5)).await;
|
let client = connect_and_login(&mut jid, "slayed", &mut server)
|
||||||
let jid = client.jid.clone();
|
.await
|
||||||
let server = client.server.clone();
|
.unwrap();
|
||||||
let (mut read, mut write) = client.into_inner().unwrap().split();
|
let (mut read, mut write) = client.split();
|
||||||
|
|
||||||
tokio::join!(
|
tokio::join!(
|
||||||
async {
|
async {
|
||||||
|
|
|
@ -15,15 +15,7 @@ pub use jid::JID;
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Error>;
|
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>> {
|
pub use client::connect_and_login;
|
||||||
todo!()
|
|
||||||
// Ok(Connection::connect_user(jid, password.as_ref().to_string())
|
|
||||||
// .await?
|
|
||||||
// .ensure_tls()
|
|
||||||
// .await?
|
|
||||||
// .negotiate()
|
|
||||||
// .await?)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
Loading…
Reference in New Issue