1
0
mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git synced 2024-06-26 08:58:27 +02:00

Move handling of IqType::Get to iq::get module

This commit is contained in:
xmppftw@kl.netlib.re 2023-12-30 01:08:52 +01:00
parent dbf053f576
commit 159452b39d
2 changed files with 61 additions and 38 deletions

58
xmpp/src/iq/get.rs Normal file
View File

@ -0,0 +1,58 @@
// Copyright (c) 2023 xmpp-rs contributors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::{
parsers::{
disco::DiscoInfoQuery,
iq::Iq,
ns,
stanza_error::{DefinedCondition, ErrorType, StanzaError},
},
Element, Jid,
};
use crate::{Agent, Event};
pub async fn handle_iq_get(
agent: &mut Agent,
_events: &mut Vec<Event>,
from: Jid,
_to: Option<Jid>,
id: String,
payload: Element,
) {
if payload.is("query", ns::DISCO_INFO) {
let query = DiscoInfoQuery::try_from(payload);
match query {
Ok(query) => {
let mut disco_info = agent.disco.clone();
disco_info.node = query.node;
let iq = Iq::from_result(id, Some(disco_info)).with_to(from).into();
let _ = agent.client.send_stanza(iq).await;
}
Err(err) => {
let error = StanzaError::new(
ErrorType::Modify,
DefinedCondition::BadRequest,
"en",
&format!("{}", err),
);
let iq = Iq::from_error(id, error).with_to(from).into();
let _ = agent.client.send_stanza(iq).await;
}
}
} else {
// We MUST answer unhandled get iqs with a service-unavailable error.
let error = StanzaError::new(
ErrorType::Cancel,
DefinedCondition::ServiceUnavailable,
"en",
"No handler defined for this kind of iq.",
);
let iq = Iq::from_error(id, error).with_to(from).into();
let _ = agent.client.send_stanza(iq).await;
}
}

View File

@ -5,7 +5,6 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
use tokio_xmpp::parsers::{
disco::DiscoInfoQuery,
iq::{Iq, IqType},
ns,
private::Query as PrivateXMLQuery,
@ -15,6 +14,8 @@ use tokio_xmpp::parsers::{
use crate::{disco, pubsub, upload, Agent, Event};
pub mod get;
pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec<Event> {
let mut events = vec![];
let from = iq
@ -22,43 +23,7 @@ pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec<Event> {
.clone()
.unwrap_or_else(|| agent.client.bound_jid().unwrap().clone());
if let IqType::Get(payload) = iq.payload {
if payload.is("query", ns::DISCO_INFO) {
let query = DiscoInfoQuery::try_from(payload);
match query {
Ok(query) => {
let mut disco_info = agent.disco.clone();
disco_info.node = query.node;
let iq = Iq::from_result(iq.id, Some(disco_info))
.with_to(iq.from.unwrap())
.into();
let _ = agent.client.send_stanza(iq).await;
}
Err(err) => {
let error = StanzaError::new(
ErrorType::Modify,
DefinedCondition::BadRequest,
"en",
&format!("{}", err),
);
let iq = Iq::from_error(iq.id, error)
.with_to(iq.from.unwrap())
.into();
let _ = agent.client.send_stanza(iq).await;
}
}
} else {
// We MUST answer unhandled get iqs with a service-unavailable error.
let error = StanzaError::new(
ErrorType::Cancel,
DefinedCondition::ServiceUnavailable,
"en",
"No handler defined for this kind of iq.",
);
let iq = Iq::from_error(iq.id, error)
.with_to(iq.from.unwrap())
.into();
let _ = agent.client.send_stanza(iq).await;
}
get::handle_iq_get(agent, &mut events, from, iq.to, iq.id, payload).await;
} else if let IqType::Result(Some(payload)) = iq.payload {
// TODO: move private iqs like this one somewhere else, for
// security reasons.