xmpp/disco/
mod.rs

1// Copyright (c) 2023 xmpp-rs contributors.
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7use tokio_xmpp::{
8    jid::Jid,
9    parsers::{
10        bookmarks,
11        disco::DiscoInfoResult,
12        iq::Iq,
13        ns,
14        private::Query as PrivateXMLQuery,
15        pubsub::pubsub::{Items, PubSub},
16    },
17};
18
19use crate::Agent;
20
21pub async fn handle_disco_info_result(agent: &mut Agent, disco: DiscoInfoResult, from: Jid) {
22    // Safe unwrap because no DISCO is received when we are not online
23    if from == agent.client.bound_jid().unwrap().to_bare() && agent.awaiting_disco_bookmarks_type {
24        info!("Received disco info about bookmarks type");
25        // Trigger bookmarks query
26        // TODO: only send this when the JoinRooms feature is enabled.
27        agent.awaiting_disco_bookmarks_type = false;
28        let perform_bookmarks2 = disco
29            .features
30            .contains(&String::from("urn:xmpp:bookmarks:1#compat"));
31
32        if perform_bookmarks2 {
33            // XEP-0402 bookmarks (modern)
34            let iq = Iq::from_get("bookmarks", PubSub::Items(Items::new(ns::BOOKMARKS2))).into();
35            let _ = agent.client.send_stanza(iq).await;
36        } else {
37            // XEP-0048 v1.0 bookmarks (legacy)
38            let iq = Iq::from_get(
39                "bookmarks-legacy",
40                PrivateXMLQuery {
41                    storage: bookmarks::Storage::new(),
42                },
43            )
44            .into();
45            let _ = agent.client.send_stanza(iq).await;
46        }
47    } else {
48        info!("Ignored disco#info response from {}", from);
49    }
50}