tokio_xmpp::AsyncClient and xmpp::Agent take a fully parsed Jid (#72)
This commit is contained in:
parent
7064ef5c17
commit
209bab1441
8 changed files with 56 additions and 36 deletions
|
|
@ -1,7 +1,10 @@
|
|||
Unreleased:
|
||||
xxxxxxxxxx
|
||||
* Breaking changes:
|
||||
- ClientBuilder::new takes a parsed BareJid instead of string (#72)
|
||||
* Improvements:
|
||||
- Agent is now Send, by replacing Rc with Arc and RefCell with RwLock (#64)
|
||||
- ClientBuilder now has a set_resource method for manual resource management (#72)
|
||||
|
||||
Version 0.4.0:
|
||||
2023-05-18 [ Maxime “pep” Buquet <pep@bouah.net>, Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> ]
|
||||
|
|
|
|||
|
|
@ -6,8 +6,9 @@
|
|||
|
||||
use env_logger;
|
||||
use std::env::args;
|
||||
use std::str::FromStr;
|
||||
use xmpp::{ClientBuilder, ClientFeature, ClientType, Event};
|
||||
use xmpp_parsers::{message::MessageType, Jid};
|
||||
use xmpp_parsers::{message::MessageType, BareJid, Jid};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Option<()>> {
|
||||
|
|
@ -18,7 +19,8 @@ async fn main() -> Result<(), Option<()>> {
|
|||
println!("Usage: {} <jid> <password>", args[0]);
|
||||
return Err(None);
|
||||
}
|
||||
let jid = &args[1];
|
||||
|
||||
let jid = BareJid::from_str(&args[1]).expect(&format!("Invalid JID: {}", &args[1]));
|
||||
let password = &args[2];
|
||||
|
||||
// Client instance
|
||||
|
|
@ -29,8 +31,7 @@ async fn main() -> Result<(), Option<()>> {
|
|||
.enable_feature(ClientFeature::Avatars)
|
||||
.enable_feature(ClientFeature::ContactList)
|
||||
.enable_feature(ClientFeature::JoinRooms)
|
||||
.build()
|
||||
.unwrap();
|
||||
.build();
|
||||
|
||||
while let Some(events) = client.wait_for_events().await {
|
||||
for event in events {
|
||||
|
|
|
|||
|
|
@ -94,19 +94,19 @@ pub enum Event {
|
|||
HttpUploadedFile(String),
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ClientBuilder<'a> {
|
||||
jid: &'a str,
|
||||
jid: BareJid,
|
||||
password: &'a str,
|
||||
website: String,
|
||||
default_nick: String,
|
||||
lang: Vec<String>,
|
||||
disco: (ClientType, String),
|
||||
features: Vec<ClientFeature>,
|
||||
resource: Option<String>,
|
||||
}
|
||||
|
||||
impl ClientBuilder<'_> {
|
||||
pub fn new<'a>(jid: &'a str, password: &'a str) -> ClientBuilder<'a> {
|
||||
pub fn new<'a>(jid: BareJid, password: &'a str) -> ClientBuilder<'a> {
|
||||
ClientBuilder {
|
||||
jid,
|
||||
password,
|
||||
|
|
@ -115,9 +115,16 @@ impl ClientBuilder<'_> {
|
|||
lang: vec![String::from("en")],
|
||||
disco: (ClientType::default(), String::from("tokio-xmpp")),
|
||||
features: vec![],
|
||||
resource: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Optionally set a resource associated to this device on the client
|
||||
pub fn set_resource(mut self, resource: &str) -> Self {
|
||||
self.resource = Some(resource.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_client(mut self, type_: ClientType, name: &str) -> Self {
|
||||
self.disco = (type_, String::from(name));
|
||||
self
|
||||
|
|
@ -168,26 +175,30 @@ impl ClientBuilder<'_> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn build(self) -> Result<Agent, Error> {
|
||||
let client = TokioXmppClient::new(self.jid, self.password)?;
|
||||
Ok(self.build_impl(client)?)
|
||||
pub fn build(self) -> Agent {
|
||||
let jid: Jid = if let Some(resource) = &self.resource {
|
||||
self.jid.clone().with_resource(resource.to_string()).into()
|
||||
} else {
|
||||
self.jid.clone().into()
|
||||
};
|
||||
|
||||
let client = TokioXmppClient::new(jid, self.password);
|
||||
self.build_impl(client)
|
||||
}
|
||||
|
||||
// This function is meant to be used for testing build
|
||||
pub(crate) fn build_impl(self, client: TokioXmppClient) -> Result<Agent, Error> {
|
||||
pub(crate) fn build_impl(self, client: TokioXmppClient) -> Agent {
|
||||
let disco = self.make_disco();
|
||||
let node = self.website;
|
||||
|
||||
let agent = Agent {
|
||||
Agent {
|
||||
client,
|
||||
default_nick: Arc::new(RwLock::new(self.default_nick)),
|
||||
lang: Arc::new(self.lang),
|
||||
disco,
|
||||
node,
|
||||
uploads: Vec::new(),
|
||||
};
|
||||
|
||||
Ok(agent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -509,22 +520,25 @@ async fn handle_upload_result(
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Agent, ClientBuilder, ClientFeature, ClientType, Event};
|
||||
use super::{Agent, BareJid, ClientBuilder, ClientFeature, ClientType, Event};
|
||||
use std::str::FromStr;
|
||||
use tokio_xmpp::AsyncClient as TokioXmppClient;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_simple() {
|
||||
let client = TokioXmppClient::new("foo@bar", "meh").unwrap();
|
||||
let jid = BareJid::from_str("foo@bar").unwrap();
|
||||
|
||||
let client = TokioXmppClient::new(jid.clone(), "meh");
|
||||
|
||||
// Client instance
|
||||
let client_builder = ClientBuilder::new("foo@bar", "meh")
|
||||
let client_builder = ClientBuilder::new(jid, "meh")
|
||||
.set_client(ClientType::Bot, "xmpp-rs")
|
||||
.set_website("https://gitlab.com/xmpp-rs/xmpp-rs")
|
||||
.set_default_nick("bot")
|
||||
.enable_feature(ClientFeature::Avatars)
|
||||
.enable_feature(ClientFeature::ContactList);
|
||||
|
||||
let mut agent: Agent = client_builder.build_impl(client).unwrap();
|
||||
let mut agent: Agent = client_builder.build_impl(client);
|
||||
|
||||
while let Some(events) = agent.wait_for_events().await {
|
||||
assert!(match events[0] {
|
||||
|
|
|
|||
Loading…
Reference in a new issue