From 6144a4c8b001b483b96b6f84349ff5f6f72a0d7c Mon Sep 17 00:00:00 2001 From: xmppftw Date: Wed, 7 Jun 2023 16:21:17 +0200 Subject: [PATCH] Event::RoomPrivateMessage for MUC PMs (#85) --- xmpp/src/lib.rs | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/xmpp/src/lib.rs b/xmpp/src/lib.rs index 7df2300b..f23b1100 100644 --- a/xmpp/src/lib.rs +++ b/xmpp/src/lib.rs @@ -90,6 +90,9 @@ pub enum Event { RoomJoined(BareJid), RoomLeft(BareJid), RoomMessage(Id, BareJid, RoomNick, Body), + /// A private message received from a room, containing the message ID, the room's BareJid, + /// the sender's FullJid, the corresponding nickname, and the message body. + RoomPrivateMessage(Id, BareJid, FullJid, RoomNick, Body), ServiceMessage(Id, BareJid, Body), HttpUploadedFile(String), } @@ -359,9 +362,38 @@ impl Agent { events.push(event) } MessageType::Chat | MessageType::Normal => { - let event = - Event::ChatMessage(message.id.clone(), from.clone().into(), body.clone()); - events.push(event) + let mut found_special_message = false; + + for payload in &message.payloads { + if payload.is("x", xmpp_parsers::ns::MUC_USER) { + let event = match from.clone() { + Jid::Bare(bare) => { + // TODO: Can a service message be of type Chat/Normal and not Groupchat? + warn!("Received misformed MessageType::Chat in muc#user namespace from a bare JID."); + Event::ServiceMessage(message.id.clone(), bare, body.clone()) + } + Jid::Full(full) => Event::RoomPrivateMessage( + message.id.clone(), + full.clone().into(), + full.clone(), + full.resource, + body.clone(), + ), + }; + + found_special_message = true; + events.push(event); + } + } + + if !found_special_message { + let event = Event::ChatMessage( + message.id.clone(), + from.clone().into(), + body.clone(), + ); + events.push(event) + } } _ => (), },