1use futures::StreamExt;
8use tokio_xmpp::{
9 parsers::{disco::DiscoInfoQuery, iq::Iq, roster::Roster},
10 Event as TokioXmppEvent, Stanza,
11};
12
13use crate::{iq, message, presence, Agent, Event};
14
15pub async fn wait_for_events(agent: &mut Agent) -> Vec<Event> {
17 if let Some(event) = agent.client.next().await {
18 let mut events = Vec::new();
19
20 match event {
21 TokioXmppEvent::Online { resumed: false, .. } => {
22 let presence =
23 presence::send::make_initial_presence(&agent.disco, &agent.node).into();
24 let _ = agent.client.send_stanza(presence).await;
25 events.push(Event::Online);
26 let iq = Iq::from_get(
28 "roster",
29 Roster {
30 ver: None,
31 items: vec![],
32 },
33 )
34 .into();
35 let _ = agent.client.send_stanza(iq).await;
36
37 let iq = Iq::from_get("disco-account", DiscoInfoQuery { node: None }).into();
39 let _ = agent.client.send_stanza(iq).await;
40 agent.awaiting_disco_bookmarks_type = true;
41 }
42 TokioXmppEvent::Online { resumed: true, .. } => {}
43 TokioXmppEvent::Disconnected(e) => {
44 events.push(Event::Disconnected(e));
45 }
46 TokioXmppEvent::Stanza(Stanza::Iq(iq)) => {
47 let new_events = iq::handle_iq(agent, iq).await;
48 events.extend(new_events);
49 }
50 TokioXmppEvent::Stanza(Stanza::Message(message)) => {
51 let new_events = message::receive::handle_message(agent, message).await;
52 events.extend(new_events);
53 }
54 TokioXmppEvent::Stanza(Stanza::Presence(presence)) => {
55 let new_events = presence::receive::handle_presence(agent, presence).await;
56 events.extend(new_events);
57 }
58 }
59
60 events
61 } else {
62 panic!("xmpp::Agent should never receive None event (stream closed, no reconnect)");
66 }
67}