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::connect::ServerConnector;
 8use tokio_xmpp::{
 9    parsers::{
10        bookmarks,
11        disco::DiscoInfoResult,
12        iq::Iq,
13        ns,
14        private::Query as PrivateXMLQuery,
15        pubsub::pubsub::{Items, PubSub},
16    },
17    Jid,
18};
19
20use crate::Agent;
21
22pub async fn handle_disco_info_result<C: ServerConnector>(
23    agent: &mut Agent<C>,
24    disco: DiscoInfoResult,
25    from: Jid,
26) {
27    // Safe unwrap because no DISCO is received when we are not online
28    if from == agent.client.bound_jid().unwrap().to_bare() && agent.awaiting_disco_bookmarks_type {
29        info!("Received disco info about bookmarks type");
30        // Trigger bookmarks query
31        // TODO: only send this when the JoinRooms feature is enabled.
32        agent.awaiting_disco_bookmarks_type = false;
33        let mut perform_bookmarks2 = false;
34        info!("{:#?}", disco.features);
35        for feature in disco.features {
36            if feature.var == "urn:xmpp:bookmarks:1#compat" {
37                perform_bookmarks2 = true;
38            }
39        }
40
41        if perform_bookmarks2 {
42            // XEP-0402 bookmarks (modern)
43            let iq = Iq::from_get("bookmarks", PubSub::Items(Items::new(ns::BOOKMARKS2))).into();
44            let _ = agent.client.send_stanza(iq).await;
45        } else {
46            // XEP-0048 v1.0 bookmarks (legacy)
47            let iq = Iq::from_get(
48                "bookmarks-legacy",
49                PrivateXMLQuery {
50                    storage: bookmarks::Storage::new(),
51                },
52            )
53            .into();
54            let _ = agent.client.send_stanza(iq).await;
55        }
56    } else {
57        unimplemented!("Ignored disco#info response from {}", from);
58    }
59}