diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index 35e0f3b..355942e 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -1,3 +1,4 @@ pub mod messaging; pub mod presence; pub mod ping; +pub mod stanza; diff --git a/src/plugins/stanza.rs b/src/plugins/stanza.rs new file mode 100644 index 0000000..d0d7f8b --- /dev/null +++ b/src/plugins/stanza.rs @@ -0,0 +1,94 @@ +use std::fmt::Debug; +use std::any::Any; + +use plugin::{Plugin, PluginReturn, PluginProxy}; +use event::Event; +use minidom::Element; +use jid::Jid; +use ns; + +pub trait Stanza: Any + Debug {} + +#[derive(Debug)] +pub struct MessageEvent { + pub from: Option, + pub to: Option, + pub id: Option, + pub type_: Option, + pub payloads: Vec, +} + +#[derive(Debug)] +pub struct IqEvent { + pub from: Option, + pub to: Option, + pub id: Option, + pub type_: Option, + pub payloads: Vec, +} + +#[derive(Debug)] +pub struct PresenceEvent { + pub from: Option, + pub to: Option, + pub id: Option, + pub type_: Option, + pub payloads: Vec, +} + +impl Event for MessageEvent {} +impl Event for IqEvent {} +impl Event for PresenceEvent {} + +pub struct StanzaPlugin { + proxy: PluginProxy, +} + +impl StanzaPlugin { + pub fn new() -> StanzaPlugin { + StanzaPlugin { + proxy: PluginProxy::new(), + } + } +} + +impl Plugin for StanzaPlugin { + fn get_proxy(&mut self) -> &mut PluginProxy { + &mut self.proxy + } + + fn handle(&mut self, elem: &Element) -> PluginReturn { + let from = match elem.attr("from") { Some(from) => Some(from.parse().unwrap()), None => None }; + let to = match elem.attr("to") { Some(to) => Some(to.parse().unwrap()), None => None }; + let id = match elem.attr("id") { Some(id) => Some(id.parse().unwrap()), None => None }; + let type_ = match elem.attr("type") { Some(type_) => Some(type_.parse().unwrap()), None => None }; + let payloads = elem.children().cloned().collect::>(); + + if elem.is("message", ns::CLIENT) { + self.proxy.dispatch(MessageEvent { + from: from, + to: to, + id: id, + type_: type_, + payloads: payloads, + }); + } else if elem.is("presence", ns::CLIENT) { + self.proxy.dispatch(PresenceEvent { + from: from, + to: to, + id: id, + type_: type_, + payloads: payloads, + }); + } else if elem.is("iq", ns::CLIENT) { + self.proxy.dispatch(IqEvent { + from: from, + to: to, + id: id, + type_: type_, + payloads: payloads, + }); + } + PluginReturn::Continue + } +}