1use tokio_xmpp::{
8 jid::Jid,
9 minidom::Element,
10 parsers::{
11 disco::DiscoInfoQuery,
12 iq::Iq,
13 ns,
14 stanza_error::{DefinedCondition, ErrorType, StanzaError},
15 },
16};
17
18use crate::{Agent, Event};
19
20pub async fn handle_iq_get(
21 agent: &mut Agent,
22 _events: &mut Vec<Event>,
23 from: Jid,
24 _to: Option<Jid>,
25 id: String,
26 payload: Element,
27) {
28 if payload.is("query", ns::DISCO_INFO) {
29 let query = DiscoInfoQuery::try_from(payload);
30 match query {
31 Ok(query) => {
32 let mut disco_info = agent.disco.clone();
33 disco_info.node = query.node;
34 let iq = Iq::from_result(id, Some(disco_info)).with_to(from).into();
35 let _ = agent.client.send_stanza(iq).await;
36 }
37 Err(err) => {
38 let error = StanzaError::new(
39 ErrorType::Modify,
40 DefinedCondition::BadRequest,
41 "en",
42 &format!("{}", err),
43 );
44 let iq = Iq::from_error(id, error).with_to(from).into();
45 let _ = agent.client.send_stanza(iq).await;
46 }
47 }
48 } else {
49 let error = StanzaError::new(
51 ErrorType::Cancel,
52 DefinedCondition::ServiceUnavailable,
53 "en",
54 "No handler defined for this kind of iq.",
55 );
56 let iq = Iq::from_error(id, error).with_to(from).into();
57 let _ = agent.client.send_stanza(iq).await;
58 }
59}