2023-12-29 23:13:25 +00:00
|
|
|
// 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 futures::StreamExt;
|
2023-12-31 06:17:29 +00:00
|
|
|
use tokio_xmpp::connect::ServerConnector;
|
2023-12-29 23:13:25 +00:00
|
|
|
use tokio_xmpp::{
|
2024-08-10 13:05:42 +00:00
|
|
|
parsers::{disco::DiscoInfoQuery, iq::Iq, roster::Roster},
|
|
|
|
Event as TokioXmppEvent, Stanza,
|
2023-12-29 23:13:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{iq, message, presence, Agent, Event};
|
|
|
|
|
2024-08-08 13:14:57 +00:00
|
|
|
/// Wait for new events, or Error::Disconnected when stream is closed and will not reconnect.
|
|
|
|
pub async fn wait_for_events<C: ServerConnector>(agent: &mut Agent<C>) -> Vec<Event> {
|
2023-12-29 23:13:25 +00:00
|
|
|
if let Some(event) = agent.client.next().await {
|
|
|
|
let mut events = Vec::new();
|
|
|
|
|
|
|
|
match event {
|
|
|
|
TokioXmppEvent::Online { resumed: false, .. } => {
|
2023-12-29 23:21:32 +00:00
|
|
|
let presence =
|
|
|
|
presence::send::make_initial_presence(&agent.disco, &agent.node).into();
|
2023-12-29 23:13:25 +00:00
|
|
|
let _ = agent.client.send_stanza(presence).await;
|
|
|
|
events.push(Event::Online);
|
|
|
|
// TODO: only send this when the ContactList feature is enabled.
|
|
|
|
let iq = Iq::from_get(
|
|
|
|
"roster",
|
|
|
|
Roster {
|
|
|
|
ver: None,
|
|
|
|
items: vec![],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.into();
|
|
|
|
let _ = agent.client.send_stanza(iq).await;
|
|
|
|
|
|
|
|
// Query account disco to know what bookmarks spec is used
|
|
|
|
let iq = Iq::from_get("disco-account", DiscoInfoQuery { node: None }).into();
|
|
|
|
let _ = agent.client.send_stanza(iq).await;
|
|
|
|
agent.awaiting_disco_bookmarks_type = true;
|
|
|
|
}
|
|
|
|
TokioXmppEvent::Online { resumed: true, .. } => {}
|
|
|
|
TokioXmppEvent::Disconnected(e) => {
|
|
|
|
events.push(Event::Disconnected(e));
|
|
|
|
}
|
2024-08-10 13:05:42 +00:00
|
|
|
TokioXmppEvent::Stanza(Stanza::Iq(iq)) => {
|
|
|
|
let new_events = iq::handle_iq(agent, iq).await;
|
|
|
|
events.extend(new_events);
|
|
|
|
}
|
|
|
|
TokioXmppEvent::Stanza(Stanza::Message(message)) => {
|
|
|
|
let new_events = message::receive::handle_message(agent, message).await;
|
|
|
|
events.extend(new_events);
|
|
|
|
}
|
|
|
|
TokioXmppEvent::Stanza(Stanza::Presence(presence)) => {
|
|
|
|
let new_events = presence::receive::handle_presence(agent, presence).await;
|
|
|
|
events.extend(new_events);
|
2023-12-29 23:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 13:14:57 +00:00
|
|
|
events
|
2023-12-29 23:13:25 +00:00
|
|
|
} else {
|
2024-08-08 13:14:57 +00:00
|
|
|
// Stream was closed and not opening again because TokioXmppClient reconnect is false
|
|
|
|
// However we set reconnect true in agent builder so this should never happen and indicates
|
|
|
|
// logic error in tokio_xmpp::AsyncClient::poll_next
|
|
|
|
panic!("xmpp::Agent should never receive None event (stream closed, no reconnect)");
|
2023-12-29 23:13:25 +00:00
|
|
|
}
|
|
|
|
}
|