From 793b76d0d5f4bb1e0734c323c9d82228a3300a17 Mon Sep 17 00:00:00 2001 From: xmppftw Date: Wed, 18 Dec 2024 21:44:57 +0100 Subject: [PATCH] xmpp: More nicks are typed as RoomNick --- xmpp/examples/hello_bot.rs | 11 +++++++---- xmpp/src/agent.rs | 12 ++++++------ xmpp/src/builder.rs | 23 +++++++++++------------ xmpp/src/iq/result.rs | 9 +++++---- xmpp/src/lib.rs | 2 +- xmpp/src/muc/room.rs | 16 +++++++--------- xmpp/src/pubsub/mod.rs | 14 ++++++-------- 7 files changed, 43 insertions(+), 44 deletions(-) diff --git a/xmpp/examples/hello_bot.rs b/xmpp/examples/hello_bot.rs index a31d6249df79d0b31760805573e12d151d399b9e..9b38e48b88e7696baec3484cc5d475e853bfd4d1 100644 --- a/xmpp/examples/hello_bot.rs +++ b/xmpp/examples/hello_bot.rs @@ -4,11 +4,14 @@ // 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 xmpp::{ + jid::BareJid, + muc::room::{JoinRoomSettings, RoomMessageSettings}, + ClientBuilder, ClientFeature, ClientType, Event, RoomNick, +}; + use std::env::args; use std::str::FromStr; -use tokio_xmpp::jid::{BareJid, ResourcePart}; -use xmpp::muc::room::{JoinRoomSettings, RoomMessageSettings}; -use xmpp::{ClientBuilder, ClientFeature, ClientType, Event}; #[tokio::main] async fn main() -> Result<(), Option<()>> { @@ -39,7 +42,7 @@ async fn main() -> Result<(), Option<()>> { } } - let nick = ResourcePart::new("bot").unwrap(); + let nick = RoomNick::from_str("bot").unwrap(); // Client instance let mut client = ClientBuilder::new(jid, password) diff --git a/xmpp/src/agent.rs b/xmpp/src/agent.rs index 67fdfb32de1139d77230f13f98a3d04c991c064a..a15a9d333b27084fea94ed68ab0c04c15af7209c 100644 --- a/xmpp/src/agent.rs +++ b/xmpp/src/agent.rs @@ -11,25 +11,25 @@ use tokio::sync::RwLock; use crate::{ event_loop, - jid::{BareJid, Jid, ResourcePart}, + jid::{BareJid, Jid}, message, muc, parsers::disco::DiscoInfoResult, - upload, Error, Event, + upload, Error, Event, RoomNick, }; use tokio_xmpp::Client as TokioXmppClient; pub struct Agent { pub(crate) client: TokioXmppClient, - pub(crate) default_nick: Arc>, + pub(crate) default_nick: Arc>, pub(crate) lang: Arc>, pub(crate) disco: DiscoInfoResult, pub(crate) node: String, pub(crate) uploads: Vec<(String, Jid, PathBuf)>, pub(crate) awaiting_disco_bookmarks_type: bool, // Mapping of room->nick - pub(crate) rooms_joined: HashMap, - pub(crate) rooms_joining: HashMap, - pub(crate) rooms_leaving: HashMap, + pub(crate) rooms_joined: HashMap, + pub(crate) rooms_joining: HashMap, + pub(crate) rooms_leaving: HashMap, } impl Agent { diff --git a/xmpp/src/builder.rs b/xmpp/src/builder.rs index f015242ee3337d53aa7fd92802f79bf8d7cfd9e8..f60304f0ef6728a53b5ecbd06a21fde8c4756974 100644 --- a/xmpp/src/builder.rs +++ b/xmpp/src/builder.rs @@ -4,24 +4,23 @@ // 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/. +#[cfg(any(feature = "starttls-rust", feature = "starttls-native"))] +use crate::tokio_xmpp::connect::{DnsConfig, StartTlsServerConnector}; use std::collections::HashMap; +use std::str::FromStr; use std::sync::Arc; use tokio::sync::RwLock; -#[cfg(any(feature = "starttls-rust", feature = "starttls-native"))] -use tokio_xmpp::connect::{DnsConfig, StartTlsServerConnector}; -use tokio_xmpp::{ - connect::ServerConnector, - jid::{BareJid, Jid, ResourcePart, ResourceRef}, + +use crate::{ + jid::{BareJid, Jid, ResourceRef}, parsers::{ disco::{DiscoInfoResult, Feature, Identity}, ns, }, - xmlstream::Timeouts, - Client as TokioXmppClient, + tokio_xmpp::{connect::ServerConnector, xmlstream::Timeouts, Client as TokioXmppClient}, + Agent, ClientFeature, RoomNick, }; -use crate::{Agent, ClientFeature}; - #[derive(Debug)] pub enum ClientType { Bot, @@ -48,7 +47,7 @@ pub struct ClientBuilder<'a, C: ServerConnector> { password: &'a str, server_connector: C, website: String, - default_nick: ResourcePart, + default_nick: RoomNick, lang: Vec, disco: (ClientType, String), features: Vec, @@ -78,7 +77,7 @@ impl ClientBuilder<'_, C> { password, server_connector, website: String::from("https://gitlab.com/xmpp-rs/tokio-xmpp"), - default_nick: ResourcePart::new("xmpp-rs").unwrap().into(), + default_nick: RoomNick::from_str("xmpp-rs").unwrap(), lang: vec![String::from("en")], disco: (ClientType::default(), String::from("tokio-xmpp")), features: vec![], @@ -104,7 +103,7 @@ impl ClientBuilder<'_, C> { } pub fn set_default_nick(mut self, nick: impl AsRef) -> Self { - self.default_nick = nick.as_ref().to_owned(); + self.default_nick = RoomNick::from_resource_ref(nick.as_ref()); self } diff --git a/xmpp/src/iq/result.rs b/xmpp/src/iq/result.rs index 0d866d2bd5fbd0f65c0d23ab9ab7b4d9970dd7d9..78c41a4f49c097530d314c6c41395083066e1845 100644 --- a/xmpp/src/iq/result.rs +++ b/xmpp/src/iq/result.rs @@ -4,14 +4,15 @@ // 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::{ +use crate::{ + disco, jid::Jid, minidom::Element, + muc::room::JoinRoomSettings, parsers::{disco::DiscoInfoResult, ns, private::Query as PrivateXMLQuery, roster::Roster}, + pubsub, upload, Agent, Event, RoomNick, }; -use crate::{disco, muc::room::JoinRoomSettings, pubsub, upload, Agent, Event}; - pub async fn handle_iq_result( agent: &mut Agent, events: &mut Vec, @@ -41,7 +42,7 @@ pub async fn handle_iq_result( agent .join_room(JoinRoomSettings { room: jid, - nick: room.nick, + nick: room.nick.map(RoomNick::new), password: room.password, status: None, }) diff --git a/xmpp/src/lib.rs b/xmpp/src/lib.rs index dc205a5f02e8c8d22467a5fa6f8e7e918cdbba2d..e259f48472b8bc4736bfe8573ba0d1a1e1a2f83d 100644 --- a/xmpp/src/lib.rs +++ b/xmpp/src/lib.rs @@ -114,7 +114,7 @@ mod tests { #[tokio::test] async fn test_simple() { let jid = BareJid::from_str("foo@bar").unwrap(); - let nick = ResourcePart::new("bot").unwrap(); + let nick = RoomNick::from_str("bot").unwrap(); let client = TokioXmppClient::new(jid.clone(), "meh"); diff --git a/xmpp/src/muc/room.rs b/xmpp/src/muc/room.rs index 9b79ff706bf84d876ab71f0c02fbd01e3dfddbc6..2037c9ac4eac7a93a267e3217dd7732c7f500b43 100644 --- a/xmpp/src/muc/room.rs +++ b/xmpp/src/muc/room.rs @@ -5,20 +5,20 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. use crate::{ - jid::{BareJid, ResourcePart, ResourceRef}, + jid::{BareJid, ResourceRef}, message::send::RawMessageSettings, parsers::{ message::MessageType, muc::Muc, presence::{Presence, Type as PresenceType}, }, - Agent, + Agent, RoomNick, }; #[derive(Clone, Debug)] pub struct JoinRoomSettings<'a> { pub room: BareJid, - pub nick: Option, + pub nick: Option, pub password: Option, pub status: Option<(&'a str, &'a str)>, } @@ -34,7 +34,7 @@ impl<'a> JoinRoomSettings<'a> { } pub fn with_nick(mut self, nick: impl AsRef) -> Self { - self.nick = Some(nick.as_ref().into()); + self.nick = Some(RoomNick::from_resource_ref(nick.as_ref())); self } @@ -81,7 +81,7 @@ pub async fn join_room<'a>(agent: &mut Agent, settings: JoinRoomSettings<'a>) { agent.default_nick.read().await.clone() }; - let room_jid = room.with_resource(&nick); + let room_jid = room.with_resource(nick.as_ref()); let mut presence = Presence::new(PresenceType::None).with_to(room_jid); presence.add_payload(muc); @@ -138,10 +138,8 @@ pub async fn leave_room<'a>(agent: &mut Agent, settings: LeaveRoomSettings<'a>) // XEP-0045 specifies that, to leave a room, the client must send a presence stanza // with type="unavailable". - let mut presence = Presence::new(PresenceType::Unavailable).with_to( - room.with_resource_str(nickname.as_str()) - .expect("Invalid room JID after adding resource part."), - ); + let mut presence = + Presence::new(PresenceType::Unavailable).with_to(room.with_resource(nickname.as_ref())); // Optionally, the client may include a status message in the presence stanza. // TODO: Should this be optional? The XEP says "MAY", but the method signature requires the arguments. diff --git a/xmpp/src/pubsub/mod.rs b/xmpp/src/pubsub/mod.rs index 148d13d80b95c39eb82184958f4746038ef3f038..a82de045fd047916008ccb1bd8b84495b8dadf4f 100644 --- a/xmpp/src/pubsub/mod.rs +++ b/xmpp/src/pubsub/mod.rs @@ -4,21 +4,19 @@ // 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 super::Agent; use crate::{ - muc::room::{JoinRoomSettings, LeaveRoomSettings}, - Event, -}; -use std::str::FromStr; -use tokio_xmpp::{ jid::{BareJid, Jid}, minidom::Element, + muc::room::{JoinRoomSettings, LeaveRoomSettings}, parsers::{ bookmarks2, ns, pubsub::{event::PubSubEvent, pubsub::PubSub}, }, + Agent, Event, RoomNick, }; +use std::str::FromStr; + #[cfg(feature = "avatars")] pub(crate) mod avatar; @@ -56,7 +54,7 @@ pub(crate) async fn handle_event( agent .join_room(JoinRoomSettings { room: jid, - nick: conference.nick, + nick: conference.nick.map(RoomNick::new), password: conference.password, status: None, }) @@ -139,7 +137,7 @@ pub(crate) async fn handle_iq_result( agent .join_room(JoinRoomSettings { room: jid, - nick: conference.nick, + nick: conference.nick.map(RoomNick::new), password: conference.password, status: None, })