2023-10-13 20:09:08 +00:00
|
|
|
//! Handling of all client2server iq stanzas
|
|
|
|
|
|
|
|
use quick_xml::events::Event;
|
|
|
|
|
2024-04-15 21:08:43 +00:00
|
|
|
use lavina_core::room::{RoomId, RoomRegistry};
|
2024-05-10 13:35:34 +00:00
|
|
|
use proto_xmpp::bind::{BindRequest, BindResponse, Jid, Name, Server};
|
|
|
|
use proto_xmpp::client::{Iq, IqError, IqErrorType, IqType};
|
2023-10-13 20:09:08 +00:00
|
|
|
use proto_xmpp::disco::{Feature, Identity, InfoQuery, Item, ItemQuery};
|
2024-05-05 15:12:58 +00:00
|
|
|
use proto_xmpp::mam::{Fin, Set};
|
2023-10-13 20:09:08 +00:00
|
|
|
use proto_xmpp::roster::RosterQuery;
|
|
|
|
use proto_xmpp::session::Session;
|
2024-05-05 15:12:58 +00:00
|
|
|
use proto_xmpp::xml::ToXml;
|
2023-10-13 20:09:08 +00:00
|
|
|
|
|
|
|
use crate::proto::IqClientBody;
|
|
|
|
use crate::XmppConnection;
|
|
|
|
|
|
|
|
impl<'a> XmppConnection<'a> {
|
|
|
|
pub async fn handle_iq(&self, output: &mut Vec<Event<'static>>, iq: Iq<IqClientBody>) {
|
|
|
|
match iq.body {
|
2024-05-10 13:35:34 +00:00
|
|
|
IqClientBody::Bind(req) => {
|
2023-10-13 20:09:08 +00:00
|
|
|
let req = Iq {
|
|
|
|
from: None,
|
|
|
|
id: iq.id,
|
|
|
|
to: None,
|
|
|
|
r#type: IqType::Result,
|
2024-05-10 13:35:34 +00:00
|
|
|
body: self.bind(&req).await,
|
2023-10-13 20:09:08 +00:00
|
|
|
};
|
|
|
|
req.serialize(output);
|
|
|
|
}
|
|
|
|
IqClientBody::Session(_) => {
|
|
|
|
let req = Iq {
|
|
|
|
from: None,
|
|
|
|
id: iq.id,
|
|
|
|
to: None,
|
|
|
|
r#type: IqType::Result,
|
|
|
|
body: Session,
|
|
|
|
};
|
|
|
|
req.serialize(output);
|
|
|
|
}
|
|
|
|
IqClientBody::Roster(_) => {
|
|
|
|
let req = Iq {
|
|
|
|
from: None,
|
|
|
|
id: iq.id,
|
|
|
|
to: None,
|
|
|
|
r#type: IqType::Result,
|
|
|
|
body: RosterQuery,
|
|
|
|
};
|
|
|
|
req.serialize(output);
|
|
|
|
}
|
|
|
|
IqClientBody::DiscoInfo(info) => {
|
2024-04-15 21:08:43 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2023-10-13 20:09:08 +00:00
|
|
|
}
|
|
|
|
IqClientBody::DiscoItem(item) => {
|
2024-04-15 16:18:51 +00:00
|
|
|
let response = self.disco_items(iq.to.as_ref(), &item, self.rooms).await;
|
2023-10-13 20:09:08 +00:00
|
|
|
let req = Iq {
|
|
|
|
from: iq.to,
|
|
|
|
id: iq.id,
|
|
|
|
to: None,
|
|
|
|
r#type: IqType::Result,
|
|
|
|
body: response,
|
|
|
|
};
|
|
|
|
req.serialize(output);
|
|
|
|
}
|
2024-05-05 15:12:58 +00:00
|
|
|
IqClientBody::MessageArchiveRequest(_) => {
|
|
|
|
let response = Iq {
|
|
|
|
from: iq.to,
|
|
|
|
id: iq.id,
|
|
|
|
to: None,
|
|
|
|
r#type: IqType::Result,
|
|
|
|
body: Fin {
|
|
|
|
set: Set { count: Some(0) },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
response.serialize(output);
|
|
|
|
}
|
2023-10-13 20:09:08 +00:00
|
|
|
_ => {
|
|
|
|
let req = Iq {
|
|
|
|
from: None,
|
|
|
|
id: iq.id,
|
|
|
|
to: None,
|
|
|
|
r#type: IqType::Error,
|
2024-04-06 15:28:46 +00:00
|
|
|
body: IqError {
|
|
|
|
r#type: IqErrorType::Cancel,
|
|
|
|
},
|
2023-10-13 20:09:08 +00:00
|
|
|
};
|
|
|
|
req.serialize(output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-10 13:35:34 +00:00
|
|
|
pub(crate) async fn bind(&self, req: &BindRequest) -> BindResponse {
|
|
|
|
BindResponse(Jid {
|
|
|
|
name: Some(self.user.xmpp_name.clone()),
|
|
|
|
server: Server(self.hostname.clone()),
|
|
|
|
resource: Some(self.user.xmpp_resource.clone()),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-04-15 21:08:43 +00:00
|
|
|
async fn disco_info(&self, to: Option<&Jid>, req: &InfoQuery) -> Result<InfoQuery, IqError> {
|
2024-04-15 00:33:26 +00:00
|
|
|
let identity;
|
|
|
|
let feature;
|
2023-10-13 20:09:08 +00:00
|
|
|
|
2024-04-15 00:33:26 +00:00
|
|
|
match to {
|
2024-04-15 16:18:51 +00:00
|
|
|
Some(Jid {
|
|
|
|
name: None,
|
|
|
|
server,
|
|
|
|
resource: None,
|
|
|
|
}) if server.0 == self.hostname => {
|
2024-04-15 00:33:26 +00:00
|
|
|
identity = vec![Identity {
|
|
|
|
category: "server".into(),
|
2023-10-13 20:09:08 +00:00
|
|
|
name: None,
|
2024-04-15 00:33:26 +00:00
|
|
|
r#type: "im".into(),
|
|
|
|
}];
|
|
|
|
feature = vec![
|
|
|
|
Feature::new("http://jabber.org/protocol/disco#info"),
|
|
|
|
Feature::new("http://jabber.org/protocol/disco#items"),
|
|
|
|
Feature::new("iq"),
|
|
|
|
Feature::new("presence"),
|
|
|
|
]
|
|
|
|
}
|
2024-04-15 16:18:51 +00:00
|
|
|
Some(Jid {
|
|
|
|
name: None,
|
|
|
|
server,
|
|
|
|
resource: None,
|
|
|
|
}) if server.0 == self.hostname_rooms => {
|
2024-04-15 00:33:26 +00:00
|
|
|
identity = vec![Identity {
|
|
|
|
category: "conference".into(),
|
|
|
|
name: Some("Chat rooms".into()),
|
|
|
|
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"),
|
|
|
|
]
|
|
|
|
}
|
2024-04-15 21:08:43 +00:00
|
|
|
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"),
|
|
|
|
]
|
|
|
|
}
|
2024-04-15 00:33:26 +00:00
|
|
|
_ => {
|
|
|
|
identity = vec![];
|
|
|
|
feature = vec![];
|
|
|
|
}
|
|
|
|
};
|
2024-04-15 21:08:43 +00:00
|
|
|
Ok(InfoQuery {
|
2024-04-15 00:33:26 +00:00
|
|
|
node: None,
|
|
|
|
identity,
|
|
|
|
feature,
|
2024-04-15 21:08:43 +00:00
|
|
|
})
|
2024-04-15 00:33:26 +00:00
|
|
|
}
|
|
|
|
|
2024-04-15 16:18:51 +00:00
|
|
|
async fn disco_items(&self, to: Option<&Jid>, req: &ItemQuery, rooms: &RoomRegistry) -> ItemQuery {
|
2024-04-15 00:33:26 +00:00
|
|
|
let item = match to {
|
2024-04-15 16:18:51 +00:00
|
|
|
Some(Jid {
|
|
|
|
name: None,
|
|
|
|
server,
|
|
|
|
resource: None,
|
|
|
|
}) if server.0 == self.hostname => {
|
2024-04-15 00:33:26 +00:00
|
|
|
vec![Item {
|
2023-10-13 20:09:08 +00:00
|
|
|
jid: Jid {
|
2024-04-15 00:33:26 +00:00
|
|
|
name: None,
|
|
|
|
server: Server(self.hostname_rooms.clone()),
|
2023-10-13 20:09:08 +00:00
|
|
|
resource: None,
|
|
|
|
},
|
|
|
|
name: None,
|
|
|
|
node: None,
|
2024-04-15 00:33:26 +00:00
|
|
|
}]
|
|
|
|
}
|
2024-04-15 16:18:51 +00:00
|
|
|
Some(Jid {
|
|
|
|
name: None,
|
|
|
|
server,
|
|
|
|
resource: None,
|
|
|
|
}) if server.0 == self.hostname_rooms => {
|
2024-04-15 00:33:26 +00:00
|
|
|
let room_list = rooms.get_all_rooms().await;
|
|
|
|
room_list
|
|
|
|
.into_iter()
|
|
|
|
.map(|room_info| Item {
|
|
|
|
jid: Jid {
|
|
|
|
name: Some(Name(room_info.id.into_inner())),
|
|
|
|
server: Server(self.hostname_rooms.clone()),
|
|
|
|
resource: None,
|
|
|
|
},
|
|
|
|
name: None,
|
|
|
|
node: None,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
_ => vec![],
|
|
|
|
};
|
|
|
|
ItemQuery { item }
|
|
|
|
}
|
2023-10-13 20:09:08 +00:00
|
|
|
}
|