From 6bba699d87b60d3b09d9ebeab4c7afbaa3cf67e4 Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Mon, 15 Apr 2024 23:08:43 +0200 Subject: [PATCH] xmpp: disco-info iq for rooms --- crates/projection-xmpp/src/iq.rs | 68 ++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/crates/projection-xmpp/src/iq.rs b/crates/projection-xmpp/src/iq.rs index eebbc4d..0031c36 100644 --- a/crates/projection-xmpp/src/iq.rs +++ b/crates/projection-xmpp/src/iq.rs @@ -2,7 +2,7 @@ use quick_xml::events::Event; -use lavina_core::room::RoomRegistry; +use lavina_core::room::{RoomId, RoomRegistry}; use proto_xmpp::bind::{BindResponse, Jid, Name, Server}; use proto_xmpp::client::{Iq, IqError, IqErrorType, IqType}; use proto_xmpp::disco::{Feature, Identity, InfoQuery, Item, ItemQuery}; @@ -52,15 +52,29 @@ impl<'a> XmppConnection<'a> { req.serialize(output); } IqClientBody::DiscoInfo(info) => { - let response = self.disco_info(iq.to.as_ref(), &info); - let req = Iq { - from: iq.to, - id: iq.id, - to: None, - r#type: IqType::Result, - body: response, - }; - req.serialize(output); + let response = self.disco_info(iq.to.as_ref(), &info).await; + match response { + Ok(response) => { + let req = Iq { + from: iq.to, + id: iq.id, + to: None, + r#type: IqType::Result, + body: response, + }; + req.serialize(output); + } + Err(response) => { + let req = Iq { + from: iq.to, + id: iq.id, + to: None, + r#type: IqType::Error, + body: response, + }; + req.serialize(output); + } + } } IqClientBody::DiscoItem(item) => { let response = self.disco_items(iq.to.as_ref(), &item, self.rooms).await; @@ -88,7 +102,7 @@ impl<'a> XmppConnection<'a> { } } - fn disco_info(&self, to: Option<&Jid>, req: &InfoQuery) -> InfoQuery { + async fn disco_info(&self, to: Option<&Jid>, req: &InfoQuery) -> Result { let identity; let feature; @@ -126,16 +140,44 @@ impl<'a> XmppConnection<'a> { Feature::new("http://jabber.org/protocol/muc"), ] } + Some(Jid { + name: Some(room_name), + server, + resource: None, + }) if server.0 == self.hostname_rooms => { + let room_id = RoomId::from(room_name.0.clone()).unwrap(); + let Some(_) = self.rooms.get_room(&room_id).await else { + // TODO should return item-not-found + // example: + // + // + // Conference room does not exist + // + return Err(IqError { + r#type: IqErrorType::Cancel, + }); + }; + identity = vec![Identity { + category: "conference".into(), + name: Some(room_id.into_inner().to_string()), + r#type: "text".into(), + }]; + feature = vec![ + Feature::new("http://jabber.org/protocol/disco#info"), + Feature::new("http://jabber.org/protocol/disco#items"), + Feature::new("http://jabber.org/protocol/muc"), + ] + } _ => { identity = vec![]; feature = vec![]; } }; - InfoQuery { + Ok(InfoQuery { node: None, identity, feature, - } + }) } async fn disco_items(&self, to: Option<&Jid>, req: &ItemQuery, rooms: &RoomRegistry) -> ItemQuery {