From d3603d1e68e46f9d85d9961e57e91cc2731d6abc Mon Sep 17 00:00:00 2001 From: "xmppftw@kl.netlib.re" Date: Sun, 31 Dec 2023 13:57:39 +0100 Subject: [PATCH] Move handling of IqType::Set to iq::set module --- xmpp/src/iq/mod.rs | 17 ++++------------- xmpp/src/iq/set.rs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 xmpp/src/iq/set.rs diff --git a/xmpp/src/iq/mod.rs b/xmpp/src/iq/mod.rs index c0fb6a9..1709867 100644 --- a/xmpp/src/iq/mod.rs +++ b/xmpp/src/iq/mod.rs @@ -4,15 +4,13 @@ // 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::{ - iq::{Iq, IqType}, - stanza_error::{DefinedCondition, ErrorType, StanzaError}, -}; +use tokio_xmpp::parsers::iq::{Iq, IqType}; use crate::{Agent, Event}; pub mod get; pub mod result; +pub mod set; pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec { let mut events = vec![]; @@ -24,15 +22,8 @@ pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec { get::handle_iq_get(agent, &mut events, from, iq.to, iq.id, payload).await; } else if let IqType::Result(Some(payload)) = iq.payload { result::handle_iq_result(agent, &mut events, from, iq.to, iq.id, payload).await; - } else if let IqType::Set(_payload) = iq.payload { - 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(from).into(); - let _ = agent.client.send_stanza(iq).await; + } else if let IqType::Set(payload) = iq.payload { + set::handle_iq_set(agent, &mut events, from, iq.to, iq.id, payload).await; } events } diff --git a/xmpp/src/iq/set.rs b/xmpp/src/iq/set.rs new file mode 100644 index 0000000..763f888 --- /dev/null +++ b/xmpp/src/iq/set.rs @@ -0,0 +1,34 @@ +// 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::{ + iq::Iq, + stanza_error::{DefinedCondition, ErrorType, StanzaError}, + }, + Element, Jid, +}; + +use crate::{Agent, Event}; + +pub async fn handle_iq_set( + agent: &mut Agent, + _events: &mut Vec, + from: Jid, + _to: Option, + id: String, + _payload: Element, +) { + // We MUST answer unhandled set 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; +}