stanza.rs

 1use try_from::TryFrom;
 2
 3use plugin::PluginProxy;
 4use event::{Event, 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    fn handle_receive_element(&self, evt: &ReceiveElement) -> Propagation {
27        let elem = &evt.0;
28
29        // TODO: make the handle take an Element instead of a reference.
30        let elem = elem.clone();
31
32        if elem.is("message", ns::CLIENT) {
33            let message = Message::try_from(elem).unwrap();
34            self.proxy.dispatch(message);
35        } else if elem.is("presence", ns::CLIENT) {
36            let presence = Presence::try_from(elem).unwrap();
37            self.proxy.dispatch(presence);
38        } else if elem.is("iq", ns::CLIENT) {
39            let iq = Iq::try_from(elem).unwrap();
40            self.proxy.dispatch(iq);
41        } else {
42            // TODO: handle nonzas too.
43            return Propagation::Continue;
44        }
45
46        Propagation::Stop
47    }
48}
49
50impl_plugin!(StanzaPlugin, proxy, [
51    (ReceiveElement, Priority::Default) => handle_receive_element,
52]);