xmpp: disco-info iq for rooms

This commit is contained in:
Nikita Vilunov 2024-04-15 23:08:43 +02:00
parent 6d493d83a3
commit 6bba699d87
1 changed files with 55 additions and 13 deletions

View File

@ -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,7 +52,9 @@ impl<'a> XmppConnection<'a> {
req.serialize(output);
}
IqClientBody::DiscoInfo(info) => {
let response = self.disco_info(iq.to.as_ref(), &info);
let response = self.disco_info(iq.to.as_ref(), &info).await;
match response {
Ok(response) => {
let req = Iq {
from: iq.to,
id: iq.id,
@ -62,6 +64,18 @@ impl<'a> XmppConnection<'a> {
};
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;
let req = Iq {
@ -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<InfoQuery, IqError> {
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:
// <error type="cancel">
// <item-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"/>
// <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="en">Conference room does not exist</text>
// </error>
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 {