From 38bfba4a1879fb3035bddc85514c3bf2ff65ac52 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Sun, 31 Dec 2023 01:17:29 -0500 Subject: [PATCH] xmpp crate now supports ServerConnector --- xmpp/src/agent.rs | 11 ++++---- xmpp/src/builder.rs | 38 ++++++++++++++++++++------ xmpp/src/disco/mod.rs | 13 +++++++-- xmpp/src/event_loop.rs | 3 +- xmpp/src/iq/get.rs | 5 ++-- xmpp/src/iq/mod.rs | 3 +- xmpp/src/iq/result.rs | 5 ++-- xmpp/src/iq/set.rs | 5 ++-- xmpp/src/lib.rs | 7 ++--- xmpp/src/message/receive/chat.rs | 5 ++-- xmpp/src/message/receive/group_chat.rs | 5 ++-- xmpp/src/message/receive/mod.rs | 6 +++- xmpp/src/message/send.rs | 5 ++-- xmpp/src/muc/private_message.rs | 5 ++-- xmpp/src/muc/room.rs | 9 +++--- xmpp/src/presence/receive.rs | 6 +++- xmpp/src/pubsub/avatar.rs | 5 ++-- xmpp/src/pubsub/mod.rs | 21 +++++++++----- xmpp/src/upload/receive.rs | 5 ++-- xmpp/src/upload/send.rs | 7 ++++- 20 files changed, 115 insertions(+), 54 deletions(-) diff --git a/xmpp/src/agent.rs b/xmpp/src/agent.rs index 08e0e42..a0e20fa 100644 --- a/xmpp/src/agent.rs +++ b/xmpp/src/agent.rs @@ -6,14 +6,15 @@ use std::path::{Path, PathBuf}; use std::sync::{Arc, RwLock}; +use tokio_xmpp::connect::ServerConnector; pub use tokio_xmpp::parsers; use tokio_xmpp::parsers::{disco::DiscoInfoResult, message::MessageType}; -pub use tokio_xmpp::{BareJid, Element, FullJid, Jid}; +pub use tokio_xmpp::{AsyncClient as TokioXmppClient, BareJid, Element, FullJid, Jid}; -use crate::{event_loop, message, muc, upload, Error, Event, RoomNick, TokioXmppClient}; +use crate::{event_loop, message, muc, upload, Error, Event, RoomNick}; -pub struct Agent { - pub(crate) client: TokioXmppClient, +pub struct Agent { + pub(crate) client: TokioXmppClient, pub(crate) default_nick: Arc>, pub(crate) lang: Arc>, pub(crate) disco: DiscoInfoResult, @@ -22,7 +23,7 @@ pub struct Agent { pub(crate) awaiting_disco_bookmarks_type: bool, } -impl Agent { +impl Agent { pub async fn disconnect(&mut self) -> Result<(), Error> { self.client.send_end().await } diff --git a/xmpp/src/builder.rs b/xmpp/src/builder.rs index a7c0350..7c8a38e 100644 --- a/xmpp/src/builder.rs +++ b/xmpp/src/builder.rs @@ -5,15 +5,16 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. use std::sync::{Arc, RwLock}; +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::{ disco::{DiscoInfoResult, Feature, Identity}, ns, }, - BareJid, Jid, + AsyncClient as TokioXmppClient, AsyncConfig, BareJid, Jid, }; -use crate::{Agent, ClientFeature, TokioXmppClient}; +use crate::{Agent, ClientFeature}; #[derive(Debug)] pub enum ClientType { @@ -36,9 +37,10 @@ impl ToString for ClientType { } } -pub struct ClientBuilder<'a> { +pub struct ClientBuilder<'a, C: ServerConnector> { jid: BareJid, password: &'a str, + server_connector: C, website: String, default_nick: String, lang: Vec, @@ -47,11 +49,26 @@ pub struct ClientBuilder<'a> { resource: Option, } -impl ClientBuilder<'_> { - pub fn new<'a>(jid: BareJid, password: &'a str) -> ClientBuilder<'a> { +#[cfg(any(feature = "starttls-rust", feature = "starttls-native"))] +impl ClientBuilder<'_, tokio_xmpp::starttls::ServerConfig> { + pub fn new<'a>( + jid: BareJid, + password: &'a str, + ) -> ClientBuilder<'a, tokio_xmpp::starttls::ServerConfig> { + Self::new_with_server(jid, password, tokio_xmpp::starttls::ServerConfig::UseSrv) + } +} + +impl ClientBuilder<'_, C> { + pub fn new_with_server<'a>( + jid: BareJid, + password: &'a str, + server_connector: C, + ) -> ClientBuilder<'a, C> { ClientBuilder { jid, password, + server_connector, website: String::from("https://gitlab.com/xmpp-rs/tokio-xmpp"), default_nick: String::from("xmpp-rs"), lang: vec![String::from("en")], @@ -117,19 +134,24 @@ impl ClientBuilder<'_> { } } - pub fn build(self) -> Agent { + pub fn build(self) -> Agent { let jid: Jid = if let Some(resource) = &self.resource { self.jid.with_resource_str(resource).unwrap().into() } else { self.jid.clone().into() }; - let client = TokioXmppClient::new(jid, self.password); + let config = AsyncConfig { + jid, + password: self.password.into(), + server: self.server_connector.clone(), + }; + let client = TokioXmppClient::new_with_config(config); self.build_impl(client) } // This function is meant to be used for testing build - pub(crate) fn build_impl(self, client: TokioXmppClient) -> Agent { + pub(crate) fn build_impl(self, client: TokioXmppClient) -> Agent { let disco = self.make_disco(); let node = self.website; diff --git a/xmpp/src/disco/mod.rs b/xmpp/src/disco/mod.rs index 24de0e6..eee104a 100644 --- a/xmpp/src/disco/mod.rs +++ b/xmpp/src/disco/mod.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::{ bookmarks, @@ -23,7 +24,11 @@ use crate::Agent; // FIXME: To be removed in the future // The server doesn't return disco#info feature when querying the account // so we add it manually because we know it's true -pub async fn handle_disco_info_result_payload(agent: &mut Agent, payload: Element, from: Jid) { +pub async fn handle_disco_info_result_payload( + agent: &mut Agent, + payload: Element, + from: Jid, +) { match DiscoInfoResult::try_from(payload.clone()) { Ok(disco) => { handle_disco_info_result(agent, disco, from).await; @@ -55,7 +60,11 @@ pub async fn handle_disco_info_result_payload(agent: &mut Agent, payload: Elemen } } -pub async fn handle_disco_info_result(agent: &mut Agent, disco: DiscoInfoResult, from: Jid) { +pub async fn handle_disco_info_result( + agent: &mut Agent, + disco: DiscoInfoResult, + from: Jid, +) { // Safe unwrap because no DISCO is received when we are not online if from == agent.client.bound_jid().unwrap().to_bare() && agent.awaiting_disco_bookmarks_type { info!("Received disco info about bookmarks type"); diff --git a/xmpp/src/event_loop.rs b/xmpp/src/event_loop.rs index fb9ca04..a0c1774 100644 --- a/xmpp/src/event_loop.rs +++ b/xmpp/src/event_loop.rs @@ -5,6 +5,7 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. use futures::StreamExt; +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::{ disco::DiscoInfoQuery, iq::Iq, message::Message, presence::Presence, roster::Roster, @@ -20,7 +21,7 @@ use crate::{iq, message, presence, Agent, Event}; /// /// - `Some(events)` if there are new events; multiple may be returned at once. /// - `None` if the underlying stream is closed. -pub async fn wait_for_events(agent: &mut Agent) -> Option> { +pub async fn wait_for_events(agent: &mut Agent) -> Option> { if let Some(event) = agent.client.next().await { let mut events = Vec::new(); diff --git a/xmpp/src/iq/get.rs b/xmpp/src/iq/get.rs index 5b45f24..f64a950 100644 --- a/xmpp/src/iq/get.rs +++ b/xmpp/src/iq/get.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::{ disco::DiscoInfoQuery, @@ -16,8 +17,8 @@ use tokio_xmpp::{ use crate::{Agent, Event}; -pub async fn handle_iq_get( - agent: &mut Agent, +pub async fn handle_iq_get( + agent: &mut Agent, _events: &mut Vec, from: Jid, _to: Option, diff --git a/xmpp/src/iq/mod.rs b/xmpp/src/iq/mod.rs index 54db1bd..3d097bb 100644 --- a/xmpp/src/iq/mod.rs +++ b/xmpp/src/iq/mod.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::parsers::iq::{Iq, IqType}; use crate::{Agent, Event}; @@ -12,7 +13,7 @@ pub mod get; pub mod result; pub mod set; -pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec { +pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec { let mut events = vec![]; let from = iq .from diff --git a/xmpp/src/iq/result.rs b/xmpp/src/iq/result.rs index 8cf9a51..da88960 100644 --- a/xmpp/src/iq/result.rs +++ b/xmpp/src/iq/result.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::{ns, private::Query as PrivateXMLQuery, roster::Roster}, Element, Jid, @@ -11,8 +12,8 @@ use tokio_xmpp::{ use crate::{disco, pubsub, upload, Agent, Event}; -pub async fn handle_iq_result( - agent: &mut Agent, +pub async fn handle_iq_result( + agent: &mut Agent, events: &mut Vec, from: Jid, _to: Option, diff --git a/xmpp/src/iq/set.rs b/xmpp/src/iq/set.rs index 763f888..4793d8d 100644 --- a/xmpp/src/iq/set.rs +++ b/xmpp/src/iq/set.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::{ iq::Iq, @@ -14,8 +15,8 @@ use tokio_xmpp::{ use crate::{Agent, Event}; -pub async fn handle_iq_set( - agent: &mut Agent, +pub async fn handle_iq_set( + agent: &mut Agent, _events: &mut Vec, from: Jid, _to: Option, diff --git a/xmpp/src/lib.rs b/xmpp/src/lib.rs index b347866..95cac93 100644 --- a/xmpp/src/lib.rs +++ b/xmpp/src/lib.rs @@ -7,7 +7,6 @@ #![deny(bare_trait_objects)] pub use tokio_xmpp::parsers; -use tokio_xmpp::AsyncClient; pub use tokio_xmpp::{BareJid, Element, FullJid, Jid}; #[macro_use] extern crate log; @@ -32,15 +31,13 @@ pub use builder::{ClientBuilder, ClientType}; pub use event::Event; pub use feature::ClientFeature; -type TokioXmppClient = AsyncClient; - pub type Error = tokio_xmpp::Error; pub type Id = Option; pub type RoomNick = String; #[cfg(test)] mod tests { - use super::{Agent, BareJid, ClientBuilder, ClientFeature, ClientType, Event}; + use super::{BareJid, ClientBuilder, ClientFeature, ClientType, Event}; use std::str::FromStr; use tokio_xmpp::AsyncClient as TokioXmppClient; @@ -60,7 +57,7 @@ mod tests { #[cfg(feature = "avatars")] let client_builder = client_builder.enable_feature(ClientFeature::Avatars); - let mut agent: Agent = client_builder.build_impl(client); + let mut agent = client_builder.build_impl(client); while let Some(events) = agent.wait_for_events().await { assert!(match events[0] { diff --git a/xmpp/src/message/receive/chat.rs b/xmpp/src/message/receive/chat.rs index 685eea5..198ad6a 100644 --- a/xmpp/src/message/receive/chat.rs +++ b/xmpp/src/message/receive/chat.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::{message::Message, muc::user::MucUser}, Jid, @@ -11,8 +12,8 @@ use tokio_xmpp::{ use crate::{delay::StanzaTimeInfo, Agent, Event}; -pub async fn handle_message_chat( - agent: &mut Agent, +pub async fn handle_message_chat( + agent: &mut Agent, events: &mut Vec, from: Jid, message: &Message, diff --git a/xmpp/src/message/receive/group_chat.rs b/xmpp/src/message/receive/group_chat.rs index 417794f..9fa17cd 100644 --- a/xmpp/src/message/receive/group_chat.rs +++ b/xmpp/src/message/receive/group_chat.rs @@ -4,12 +4,13 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{parsers::message::Message, Jid}; use crate::{delay::StanzaTimeInfo, Agent, Event}; -pub async fn handle_message_group_chat( - agent: &mut Agent, +pub async fn handle_message_group_chat( + agent: &mut Agent, events: &mut Vec, from: Jid, message: &Message, diff --git a/xmpp/src/message/receive/mod.rs b/xmpp/src/message/receive/mod.rs index 0b03112..5dbe860 100644 --- a/xmpp/src/message/receive/mod.rs +++ b/xmpp/src/message/receive/mod.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::parsers::{ message::{Message, MessageType}, ns, @@ -14,7 +15,10 @@ use crate::{delay::message_time_info, pubsub, Agent, Event}; pub mod chat; pub mod group_chat; -pub async fn handle_message(agent: &mut Agent, message: Message) -> Vec { +pub async fn handle_message( + agent: &mut Agent, + message: Message, +) -> Vec { let mut events = vec![]; let from = message.from.clone().unwrap(); let time_info = message_time_info(&message); diff --git a/xmpp/src/message/send.rs b/xmpp/src/message/send.rs index 539c5a7..877aca8 100644 --- a/xmpp/src/message/send.rs +++ b/xmpp/src/message/send.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::message::{Body, Message, MessageType}, Jid, @@ -11,8 +12,8 @@ use tokio_xmpp::{ use crate::Agent; -pub async fn send_message( - agent: &mut Agent, +pub async fn send_message( + agent: &mut Agent, recipient: Jid, type_: MessageType, lang: &str, diff --git a/xmpp/src/muc/private_message.rs b/xmpp/src/muc/private_message.rs index cdf9e26..7b5883f 100644 --- a/xmpp/src/muc/private_message.rs +++ b/xmpp/src/muc/private_message.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::{ message::{Body, Message, MessageType}, @@ -14,8 +15,8 @@ use tokio_xmpp::{ use crate::{Agent, RoomNick}; -pub async fn send_room_private_message( - agent: &mut Agent, +pub async fn send_room_private_message( + agent: &mut Agent, room: BareJid, recipient: RoomNick, lang: &str, diff --git a/xmpp/src/muc/room.rs b/xmpp/src/muc/room.rs index 03c9d1f..8c6aa01 100644 --- a/xmpp/src/muc/room.rs +++ b/xmpp/src/muc/room.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::{ muc::Muc, @@ -14,8 +15,8 @@ use tokio_xmpp::{ use crate::{Agent, RoomNick}; -pub async fn join_room( - agent: &mut Agent, +pub async fn join_room( + agent: &mut Agent, room: BareJid, nick: Option, password: Option, @@ -57,8 +58,8 @@ pub async fn join_room( /// * `nickname`: The nickname to use in the room. /// * `lang`: The language of the status message. /// * `status`: The status message to send. -pub async fn leave_room( - agent: &mut Agent, +pub async fn leave_room( + agent: &mut Agent, room_jid: BareJid, nickname: RoomNick, lang: impl Into, diff --git a/xmpp/src/presence/receive.rs b/xmpp/src/presence/receive.rs index 123f4d7..4e6720e 100644 --- a/xmpp/src/presence/receive.rs +++ b/xmpp/src/presence/receive.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::parsers::{ muc::user::{MucUser, Status}, presence::{Presence, Type as PresenceType}, @@ -12,7 +13,10 @@ use tokio_xmpp::parsers::{ use crate::{Agent, Event}; /// Translate a `Presence` stanza into a list of higher-level `Event`s. -pub async fn handle_presence(_agent: &mut Agent, presence: Presence) -> Vec { +pub async fn handle_presence( + _agent: &mut Agent, + presence: Presence, +) -> Vec { // Allocate an empty vector to store the events. let mut events = vec![]; diff --git a/xmpp/src/pubsub/avatar.rs b/xmpp/src/pubsub/avatar.rs index 02e103f..83728ad 100644 --- a/xmpp/src/pubsub/avatar.rs +++ b/xmpp/src/pubsub/avatar.rs @@ -8,6 +8,7 @@ use super::Agent; use crate::Event; use std::fs::{self, File}; use std::io::{self, Write}; +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::parsers::{ avatar::{Data, Metadata}, iq::Iq, @@ -20,9 +21,9 @@ use tokio_xmpp::parsers::{ Jid, }; -pub(crate) async fn handle_metadata_pubsub_event( +pub(crate) async fn handle_metadata_pubsub_event( from: &Jid, - agent: &mut Agent, + agent: &mut Agent, items: Vec, ) -> Vec { let mut events = Vec::new(); diff --git a/xmpp/src/pubsub/mod.rs b/xmpp/src/pubsub/mod.rs index ae6b25d..1c3cea4 100644 --- a/xmpp/src/pubsub/mod.rs +++ b/xmpp/src/pubsub/mod.rs @@ -7,18 +7,25 @@ use super::Agent; use crate::Event; use std::str::FromStr; -use tokio_xmpp::parsers::{ - bookmarks2::{self, Autojoin}, - ns, - pubsub::event::PubSubEvent, - pubsub::pubsub::PubSub, - BareJid, Element, Jid, +use tokio_xmpp::{ + connect::ServerConnector, + parsers::{ + bookmarks2::{self, Autojoin}, + ns, + pubsub::event::PubSubEvent, + pubsub::pubsub::PubSub, + BareJid, Element, Jid, + }, }; #[cfg(feature = "avatars")] pub(crate) mod avatar; -pub(crate) async fn handle_event(from: &Jid, elem: Element, agent: &mut Agent) -> Vec { +pub(crate) async fn handle_event( + from: &Jid, + elem: Element, + agent: &mut Agent, +) -> Vec { let mut events = Vec::new(); let event = PubSubEvent::try_from(elem); trace!("PubSub event: {:#?}", event); diff --git a/xmpp/src/upload/receive.rs b/xmpp/src/upload/receive.rs index f90af99..ff6ef55 100644 --- a/xmpp/src/upload/receive.rs +++ b/xmpp/src/upload/receive.rs @@ -10,6 +10,7 @@ use reqwest::{ use std::path::PathBuf; use tokio::fs::File; use tokio_util::codec::{BytesCodec, FramedRead}; +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::http_upload::{Header as HttpUploadHeader, SlotResult}, Element, Jid, @@ -17,11 +18,11 @@ use tokio_xmpp::{ use crate::{Agent, Event}; -pub async fn handle_upload_result( +pub async fn handle_upload_result( from: &Jid, iqid: String, elem: Element, - agent: &mut Agent, + agent: &mut Agent, ) -> impl IntoIterator { let mut res: Option<(usize, PathBuf)> = None; diff --git a/xmpp/src/upload/send.rs b/xmpp/src/upload/send.rs index 8b9272e..54edc90 100644 --- a/xmpp/src/upload/send.rs +++ b/xmpp/src/upload/send.rs @@ -6,6 +6,7 @@ use std::path::Path; use tokio::fs::File; +use tokio_xmpp::connect::ServerConnector; use tokio_xmpp::{ parsers::{http_upload::SlotRequest, iq::Iq}, Jid, @@ -13,7 +14,11 @@ use tokio_xmpp::{ use crate::Agent; -pub async fn upload_file_with(agent: &mut Agent, service: &str, path: &Path) { +pub async fn upload_file_with( + agent: &mut Agent, + service: &str, + path: &Path, +) { let name = path.file_name().unwrap().to_str().unwrap().to_string(); let file = File::open(path).await.unwrap(); let size = file.metadata().await.unwrap().len();