2020-03-05 01:25:24 +01:00
|
|
|
use super::Error;
|
2024-07-24 20:39:27 +02:00
|
|
|
use minidom::Element;
|
|
|
|
|
use xmpp_parsers::jid::Jid;
|
2017-07-13 01:47:05 +02:00
|
|
|
|
2018-08-02 19:58:19 +02:00
|
|
|
/// High-level event on the Stream implemented by Client and Component
|
2017-07-13 01:47:05 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum Event {
|
2018-08-02 19:58:19 +02:00
|
|
|
/// Stream is connected and initialized
|
2020-03-17 22:39:23 +01:00
|
|
|
Online {
|
|
|
|
|
/// Server-set Jabber-Id for your session
|
|
|
|
|
///
|
|
|
|
|
/// This may turn out to be a different JID resource than
|
|
|
|
|
/// expected, so use this one instead of the JID with which
|
|
|
|
|
/// the connection was setup.
|
|
|
|
|
bound_jid: Jid,
|
|
|
|
|
/// Was this session resumed?
|
|
|
|
|
///
|
|
|
|
|
/// Not yet implemented for the Client
|
|
|
|
|
resumed: bool,
|
|
|
|
|
},
|
2018-08-02 19:58:19 +02:00
|
|
|
/// Stream end
|
2020-03-05 01:25:24 +01:00
|
|
|
Disconnected(Error),
|
2018-08-02 19:58:19 +02:00
|
|
|
/// Received stanza/nonza
|
2017-07-17 20:53:00 +02:00
|
|
|
Stanza(Element),
|
2017-07-13 01:47:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Event {
|
2018-08-02 19:58:19 +02:00
|
|
|
/// `Online` event?
|
2017-07-13 01:47:05 +02:00
|
|
|
pub fn is_online(&self) -> bool {
|
2017-07-21 00:19:08 +02:00
|
|
|
match *self {
|
2020-03-17 22:39:23 +01:00
|
|
|
Event::Online { .. } => true,
|
2017-07-13 01:47:05 +02:00
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-15 22:02:14 +02:00
|
|
|
/// Get the server-assigned JID for the `Online` event
|
|
|
|
|
pub fn get_jid(&self) -> Option<&Jid> {
|
|
|
|
|
match *self {
|
2020-03-17 22:39:23 +01:00
|
|
|
Event::Online { ref bound_jid, .. } => Some(bound_jid),
|
2019-10-15 22:02:14 +02:00
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 19:58:19 +02:00
|
|
|
/// `Stanza` event?
|
2017-07-13 01:47:05 +02:00
|
|
|
pub fn is_stanza(&self, name: &str) -> bool {
|
2017-07-21 00:19:08 +02:00
|
|
|
match *self {
|
|
|
|
|
Event::Stanza(ref stanza) => stanza.name() == name,
|
2017-07-13 01:47:05 +02:00
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 19:58:19 +02:00
|
|
|
/// If this is a `Stanza` event, get its data
|
2017-07-17 20:53:00 +02:00
|
|
|
pub fn as_stanza(&self) -> Option<&Element> {
|
2017-07-21 00:19:08 +02:00
|
|
|
match *self {
|
|
|
|
|
Event::Stanza(ref stanza) => Some(stanza),
|
2017-07-13 01:47:05 +02:00
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-20 01:07:07 +02:00
|
|
|
|
2018-08-02 19:58:19 +02:00
|
|
|
/// If this is a `Stanza` event, unwrap into its data
|
2017-07-20 01:07:07 +02:00
|
|
|
pub fn into_stanza(self) -> Option<Element> {
|
|
|
|
|
match self {
|
|
|
|
|
Event::Stanza(stanza) => Some(stanza),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-13 01:47:05 +02:00
|
|
|
}
|