Move handling of IqType::Result to iq::result module

This commit is contained in:
xmppftw@kl.netlib.re 2023-12-31 13:55:15 +01:00
parent 159452b39d
commit d821d2ecb2
2 changed files with 55 additions and 38 deletions

View file

@ -6,15 +6,13 @@
use tokio_xmpp::parsers::{ use tokio_xmpp::parsers::{
iq::{Iq, IqType}, iq::{Iq, IqType},
ns,
private::Query as PrivateXMLQuery,
roster::Roster,
stanza_error::{DefinedCondition, ErrorType, StanzaError}, stanza_error::{DefinedCondition, ErrorType, StanzaError},
}; };
use crate::{disco, pubsub, upload, Agent, Event}; use crate::{Agent, Event};
pub mod get; pub mod get;
pub mod result;
pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec<Event> { pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec<Event> {
let mut events = vec![]; let mut events = vec![];
@ -25,46 +23,15 @@ pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec<Event> {
if let IqType::Get(payload) = iq.payload { if let IqType::Get(payload) = iq.payload {
get::handle_iq_get(agent, &mut events, from, iq.to, iq.id, payload).await; get::handle_iq_get(agent, &mut events, from, iq.to, iq.id, payload).await;
} else if let IqType::Result(Some(payload)) = iq.payload { } else if let IqType::Result(Some(payload)) = iq.payload {
// TODO: move private iqs like this one somewhere else, for result::handle_iq_result(agent, &mut events, from, iq.to, iq.id, payload).await;
// security reasons. } else if let IqType::Set(_payload) = iq.payload {
if payload.is("query", ns::ROSTER) && Some(from.clone()) == iq.from {
let roster = Roster::try_from(payload).unwrap();
for item in roster.items.into_iter() {
events.push(Event::ContactAdded(item));
}
} else if payload.is("pubsub", ns::PUBSUB) {
let new_events = pubsub::handle_iq_result(&from, payload);
events.extend(new_events);
} else if payload.is("slot", ns::HTTP_UPLOAD) {
let new_events =
upload::receive::handle_upload_result(&from, iq.id, payload, agent).await;
events.extend(new_events);
} else if payload.is("query", ns::PRIVATE) {
match PrivateXMLQuery::try_from(payload) {
Ok(query) => {
for conf in query.storage.conferences {
let (jid, room) = conf.into_bookmarks2();
events.push(Event::JoinRoom(jid, room));
}
}
Err(e) => {
panic!("Wrong XEP-0048 v1.0 Bookmark format: {}", e);
}
}
} else if payload.is("query", ns::DISCO_INFO) {
disco::handle_disco_info_result_payload(agent, payload, from).await;
}
} else if let IqType::Set(_) = iq.payload {
// We MUST answer unhandled set iqs with a service-unavailable error.
let error = StanzaError::new( let error = StanzaError::new(
ErrorType::Cancel, ErrorType::Cancel,
DefinedCondition::ServiceUnavailable, DefinedCondition::ServiceUnavailable,
"en", "en",
"No handler defined for this kind of iq.", "No handler defined for this kind of iq.",
); );
let iq = Iq::from_error(iq.id, error) let iq = Iq::from_error(iq.id, error).with_to(from).into();
.with_to(iq.from.unwrap())
.into();
let _ = agent.client.send_stanza(iq).await; let _ = agent.client.send_stanza(iq).await;
} }
events events

50
xmpp/src/iq/result.rs Normal file
View file

@ -0,0 +1,50 @@
// 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::{ns, private::Query as PrivateXMLQuery, roster::Roster},
Element, Jid,
};
use crate::{disco, pubsub, upload, Agent, Event};
pub async fn handle_iq_result(
agent: &mut Agent,
events: &mut Vec<Event>,
from: Jid,
_to: Option<Jid>,
id: String,
payload: Element,
) {
// TODO: move private iqs like this one somewhere else, for
// security reasons.
if payload.is("query", ns::ROSTER) && from == agent.client.bound_jid().unwrap().to_bare() {
let roster = Roster::try_from(payload).unwrap();
for item in roster.items.into_iter() {
events.push(Event::ContactAdded(item));
}
} else if payload.is("pubsub", ns::PUBSUB) {
let new_events = pubsub::handle_iq_result(&from, payload);
events.extend(new_events);
} else if payload.is("slot", ns::HTTP_UPLOAD) {
let new_events = upload::receive::handle_upload_result(&from, id, payload, agent).await;
events.extend(new_events);
} else if payload.is("query", ns::PRIVATE) {
match PrivateXMLQuery::try_from(payload) {
Ok(query) => {
for conf in query.storage.conferences {
let (jid, room) = conf.into_bookmarks2();
events.push(Event::JoinRoom(jid, room));
}
}
Err(e) => {
panic!("Wrong XEP-0048 v1.0 Bookmark format: {}", e);
}
}
} else if payload.is("query", ns::DISCO_INFO) {
disco::handle_disco_info_result_payload(agent, payload, from).await;
}
}