xmpp: More nicks are typed as RoomNick

xmppftw created

Change summary

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(-)

Detailed changes

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)

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<RwLock<ResourcePart>>,
+    pub(crate) default_nick: Arc<RwLock<RoomNick>>,
     pub(crate) lang: Arc<Vec<String>>,
     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<BareJid, ResourcePart>,
-    pub(crate) rooms_joining: HashMap<BareJid, ResourcePart>,
-    pub(crate) rooms_leaving: HashMap<BareJid, ResourcePart>,
+    pub(crate) rooms_joined: HashMap<BareJid, RoomNick>,
+    pub(crate) rooms_joining: HashMap<BareJid, RoomNick>,
+    pub(crate) rooms_leaving: HashMap<BareJid, RoomNick>,
 }
 
 impl Agent {

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<String>,
     disco: (ClientType, String),
     features: Vec<ClientFeature>,
@@ -78,7 +77,7 @@ impl<C: ServerConnector> 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<C: ServerConnector> ClientBuilder<'_, C> {
     }
 
     pub fn set_default_nick(mut self, nick: impl AsRef<ResourceRef>) -> Self {
-        self.default_nick = nick.as_ref().to_owned();
+        self.default_nick = RoomNick::from_resource_ref(nick.as_ref());
         self
     }
 

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<Event>,
@@ -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,
                         })

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");
 

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<ResourcePart>,
+    pub nick: Option<RoomNick>,
     pub password: Option<String>,
     pub status: Option<(&'a str, &'a str)>,
 }
@@ -34,7 +34,7 @@ impl<'a> JoinRoomSettings<'a> {
     }
 
     pub fn with_nick(mut self, nick: impl AsRef<ResourceRef>) -> 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.

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,
                                         })