xmpp: Agent::send_message is only for normal messages

xmppftw created

Change summary

xmpp/ChangeLog                  |  1 
xmpp/src/agent.rs               | 12 ++------
xmpp/src/message/send.rs        | 50 +++++++++++++++++++++++++---------
xmpp/src/muc/private_message.rs |  1 
xmpp/src/muc/room.rs            | 17 ++++++-----
5 files changed, 51 insertions(+), 30 deletions(-)

Detailed changes

xmpp/ChangeLog 🔗

@@ -1,6 +1,7 @@
 Version NEXT
 XXXX-YY-ZZ [ RELEASER <admin@localhost> ]
     * Breaking:
+      - Agent::send_message now only sends normal messages to other users (!487)
       - Event::LeaveRoom, Event::LeaveAllRooms, and Event::JoinRooms have been removed.
         Agent now handles MUC connection states internally. (!481)
       - Agent::leave_room now takes LeaveRoomSettings argument (!483)

xmpp/src/agent.rs 🔗

@@ -9,7 +9,7 @@ use std::path::{Path, PathBuf};
 use std::sync::Arc;
 use tokio::sync::RwLock;
 pub use tokio_xmpp::parsers;
-use tokio_xmpp::parsers::{disco::DiscoInfoResult, message::MessageType};
+use tokio_xmpp::parsers::disco::DiscoInfoResult;
 pub use tokio_xmpp::{
     jid::{BareJid, FullJid, Jid, ResourcePart},
     minidom::Element,
@@ -49,14 +49,8 @@ impl Agent {
         muc::room::leave_room(self, settings).await
     }
 
-    pub async fn send_message(
-        &mut self,
-        recipient: Jid,
-        type_: MessageType,
-        lang: &str,
-        text: &str,
-    ) {
-        message::send::send_message(self, recipient, type_, lang, text).await
+    pub async fn send_message<'a>(&mut self, settings: message::send::MessageSettings<'a>) {
+        message::send::send_message(self, settings).await
     }
 
     pub async fn send_room_message<'a>(&mut self, settings: muc::room::RoomMessageSettings<'a>) {

xmpp/src/message/send.rs 🔗

@@ -5,23 +5,47 @@
 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 use tokio_xmpp::{
-    jid::Jid,
+    jid::BareJid,
     parsers::message::{Body, Message, MessageType},
 };
 
 use crate::Agent;
 
-pub async fn send_message(
-    agent: &mut Agent,
-    recipient: Jid,
-    type_: MessageType,
-    lang: &str,
-    text: &str,
-) {
-    let mut message = Message::new(Some(recipient));
-    message.type_ = type_;
-    message
+#[derive(Clone, Debug)]
+pub struct MessageSettings<'a> {
+    pub recipient: BareJid,
+    pub message: &'a str,
+    pub lang: Option<&'a str>,
+}
+
+impl<'a> MessageSettings<'a> {
+    pub fn new(recipient: BareJid, message: &'a str) -> Self {
+        Self {
+            recipient,
+            message,
+            lang: None,
+        }
+    }
+
+    pub fn with_lang(mut self, lang: &'a str) -> Self {
+        self.lang = Some(lang);
+        self
+    }
+}
+
+pub async fn send_message<'a>(agent: &mut Agent, settings: MessageSettings<'a>) {
+    let MessageSettings {
+        recipient,
+        message,
+        lang,
+    } = settings;
+
+    // TODO: check that recipient may receive normal chat message, eg is not a MUC chatroom
+
+    let mut stanza = Message::new(Some(recipient.into()));
+    stanza.type_ = MessageType::Chat;
+    stanza
         .bodies
-        .insert(String::from(lang), Body(String::from(text)));
-    let _ = agent.client.send_stanza(message.into()).await;
+        .insert(lang.unwrap_or("").to_string(), Body(String::from(message)));
+    agent.client.send_stanza(stanza.into()).await.unwrap();
 }

xmpp/src/muc/private_message.rs 🔗

@@ -48,6 +48,7 @@ pub async fn send_room_private_message<'a>(
         lang,
     } = settings;
 
+    // TODO: check that room is in agent.joined_rooms
     let recipient: Jid = room.with_resource(recipient.as_ref()).into();
     let mut stanza = Message::new(recipient).with_payload(MucUser::new());
     stanza.type_ = MessageType::Chat;

xmpp/src/muc/room.rs 🔗

@@ -8,6 +8,7 @@ use crate::parsers::message::MessageType;
 use tokio_xmpp::{
     jid::{BareJid, ResourcePart, ResourceRef},
     parsers::{
+        message::{Body, Message},
         muc::Muc,
         presence::{Presence, Type as PresenceType},
     },
@@ -188,12 +189,12 @@ pub async fn send_room_message<'a>(agent: &mut Agent, settings: RoomMessageSetti
         lang,
     } = settings;
 
-    agent
-        .send_message(
-            room.into(),
-            MessageType::Groupchat,
-            lang.unwrap_or(""),
-            message,
-        )
-        .await;
+    // TODO: check that room is in agent.joined_rooms
+
+    let mut stanza = Message::new(Some(room.into()));
+    stanza.type_ = MessageType::Groupchat;
+    stanza
+        .bodies
+        .insert(lang.unwrap_or("").to_string(), Body(String::from(message)));
+    agent.client.send_stanza(stanza.into()).await.unwrap();
 }