stanza.rs

 1use std::convert::TryFrom;
 2
 3use plugin::PluginProxy;
 4use event::{Event, EventHandler, ReceiveElement, Propagation, Priority};
 5use ns;
 6
 7pub use xmpp_parsers::message::Message;
 8pub use xmpp_parsers::presence::Presence;
 9pub use xmpp_parsers::iq::Iq;
10
11impl Event for Message {}
12impl Event for Presence {}
13impl Event for Iq {}
14
15pub struct StanzaPlugin {
16    proxy: PluginProxy,
17}
18
19impl StanzaPlugin {
20    pub fn new() -> StanzaPlugin {
21        StanzaPlugin {
22            proxy: PluginProxy::new(),
23        }
24    }
25}
26
27impl_plugin!(StanzaPlugin, proxy, [
28    ReceiveElement => Priority::Default,
29]);
30
31impl EventHandler<ReceiveElement> for StanzaPlugin {
32    fn handle(&self, evt: &ReceiveElement) -> Propagation {
33        let elem = &evt.0;
34
35        // TODO: make the handle take an Element instead of a reference.
36        let elem = elem.clone();
37
38        if elem.is("message", ns::CLIENT) {
39            let message = Message::try_from(elem).unwrap();
40            self.proxy.dispatch(message);
41        } else if elem.is("presence", ns::CLIENT) {
42            let presence = Presence::try_from(elem).unwrap();
43            self.proxy.dispatch(presence);
44        } else if elem.is("iq", ns::CLIENT) {
45            let iq = Iq::try_from(elem).unwrap();
46            self.proxy.dispatch(iq);
47        }
48
49        Propagation::Continue
50    }
51}