tokio_xmpp::AsyncClient and xmpp::Agent take a fully parsed Jid (#72)

This commit is contained in:
xmppftw xmppftw 2023-06-01 11:58:04 +02:00
commit 209bab1441
8 changed files with 56 additions and 36 deletions

3
tokio-xmpp/ChangeLog Normal file
View file

@ -0,0 +1,3 @@
xxxxxxxxxx
* Breaking changes:
- AsyncClient::new takes a parsed Jid instead of string (#72)

View file

@ -2,13 +2,14 @@ use futures::stream::StreamExt;
use std::convert::TryFrom;
use std::env::args;
use std::process::exit;
use std::str::FromStr;
use tokio_xmpp::AsyncClient as Client;
use xmpp_parsers::{
disco::{DiscoInfoQuery, DiscoInfoResult},
iq::{Iq, IqType},
ns,
server_info::ServerInfo,
Element, Jid,
BareJid, Element, Jid,
};
#[tokio::main]
@ -18,12 +19,12 @@ async fn main() {
println!("Usage: {} <jid> <password> <target>", args[0]);
exit(1);
}
let jid = &args[1];
let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
let password = args[2].clone();
let target = &args[3];
// Client instance
let mut client = Client::new(jid, password).unwrap();
let mut client = Client::new(jid, password);
// Main loop, processes events
let mut wait_for_stream_end = false;

View file

@ -22,7 +22,7 @@ use xmpp_parsers::{
NodeName,
},
stanza_error::{DefinedCondition, ErrorType, StanzaError},
Element, Jid,
BareJid, Element, Jid,
};
#[tokio::main]
@ -32,11 +32,11 @@ async fn main() {
println!("Usage: {} <jid> <password>", args[0]);
exit(1);
}
let jid = &args[1];
let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
let password = args[2].clone();
// Client instance
let mut client = Client::new(jid, password).unwrap();
let mut client = Client::new(jid.clone(), password);
let disco_info = make_disco();
@ -94,7 +94,7 @@ async fn main() {
} else if let IqType::Result(Some(payload)) = iq.payload {
if payload.is("pubsub", ns::PUBSUB) {
let pubsub = PubSub::try_from(payload).unwrap();
let from = iq.from.clone().unwrap_or(Jid::from_str(jid).unwrap());
let from = iq.from.clone().unwrap_or(jid.clone().into());
handle_iq_result(pubsub, &from);
}
} else if let IqType::Set(_) = iq.payload {

View file

@ -2,11 +2,12 @@ use futures::stream::StreamExt;
use std::convert::TryFrom;
use std::env::args;
use std::process::exit;
use std::str::FromStr;
use tokio;
use tokio_xmpp::AsyncClient as Client;
use xmpp_parsers::message::{Body, Message, MessageType};
use xmpp_parsers::presence::{Presence, Show as PresenceShow, Type as PresenceType};
use xmpp_parsers::{Element, Jid};
use xmpp_parsers::{BareJid, Element, Jid};
#[tokio::main]
async fn main() {
@ -15,11 +16,11 @@ async fn main() {
println!("Usage: {} <jid> <password>", args[0]);
exit(1);
}
let jid = &args[1];
let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
let password = &args[2];
// Client instance
let mut client = Client::new(jid, password.to_owned()).unwrap();
let mut client = Client::new(jid, password.to_owned());
client.set_reconnect(true);
// Main loop, processes events

View file

@ -2,7 +2,6 @@ use futures::{sink::SinkExt, task::Poll, Future, Sink, Stream};
use sasl::common::{ChannelBinding, Credentials};
use std::mem::replace;
use std::pin::Pin;
use std::str::FromStr;
use std::task::Context;
use tokio::net::TcpStream;
use tokio::task::JoinHandle;
@ -10,7 +9,7 @@ use tokio::task::JoinHandle;
use tokio_native_tls::TlsStream;
#[cfg(feature = "tls-rust")]
use tokio_rustls::client::TlsStream;
use xmpp_parsers::{ns, Element, Jid, JidParseError};
use xmpp_parsers::{ns, Element, Jid};
use super::auth::auth;
use super::bind::bind;
@ -74,15 +73,13 @@ impl Client {
///
/// Start polling the returned instance so that it will connect
/// and yield events.
pub fn new<P: Into<String>>(jid: &str, password: P) -> Result<Self, JidParseError> {
let jid = Jid::from_str(jid)?;
pub fn new<J: Into<Jid>, P: Into<String>>(jid: J, password: P) -> Self {
let config = Config {
jid: jid.clone(),
jid: jid.into(),
password: password.into(),
server: ServerConfig::UseSrv,
};
let client = Self::new_with_config(config);
Ok(client)
Self::new_with_config(config)
}
/// Start a new client given that the JID is already parsed.