diff --git a/Cargo.toml b/Cargo.toml index 3f7be42a..0475a1b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,12 +16,13 @@ gitlab = { repository = "lumi/xmpp-rs" } [dependencies] xml-rs = "0.4.1" -openssl = "0.9.7" +xmpp-parsers = "0.3.0" +openssl = "0.9.12" base64 = "0.5.2" -minidom = "0.3.0" +minidom = "0.4.1" jid = "0.2.0" sasl = "0.4.0" -sha-1 = "0.3.2" +sha-1 = "0.3.3" [features] insecure = [] diff --git a/src/lib.rs b/src/lib.rs index c3e933fc..ebe5e84a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ #![feature(raw)] +#![feature(try_from)] extern crate xml; +extern crate xmpp_parsers; extern crate openssl; extern crate minidom; extern crate base64; diff --git a/src/plugins/stanza.rs b/src/plugins/stanza.rs index d30f0ad4..60d39f0b 100644 --- a/src/plugins/stanza.rs +++ b/src/plugins/stanza.rs @@ -1,44 +1,16 @@ -use std::fmt::Debug; -use std::any::Any; +use std::convert::TryFrom; use plugin::PluginProxy; use event::{Event, EventHandler, ReceiveElement, Propagation, Priority}; -use minidom::Element; -use jid::Jid; use ns; -pub trait Stanza: Any + Debug {} +pub use xmpp_parsers::message::Message; +pub use xmpp_parsers::presence::Presence; +pub use xmpp_parsers::iq::Iq; -#[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 {} +impl Event for Message {} +impl Event for Presence {} +impl Event for Iq {} pub struct StanzaPlugin { proxy: PluginProxy, @@ -60,36 +32,18 @@ impl EventHandler for StanzaPlugin { fn handle(&self, evt: &ReceiveElement) -> Propagation { let elem = &evt.0; - 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::>(); + // TODO: make the handle take an Element instead of a reference. + let elem = elem.clone(); if elem.is("message", ns::CLIENT) { - self.proxy.dispatch(MessageEvent { - from: from, - to: to, - id: id, - type_: type_, - payloads: payloads, - }); + let message = Message::try_from(elem).unwrap(); + self.proxy.dispatch(message); } else if elem.is("presence", ns::CLIENT) { - self.proxy.dispatch(PresenceEvent { - from: from, - to: to, - id: id, - type_: type_, - payloads: payloads, - }); + let presence = Presence::try_from(elem).unwrap(); + self.proxy.dispatch(presence); } else if elem.is("iq", ns::CLIENT) { - self.proxy.dispatch(IqEvent { - from: from, - to: to, - id: id, - type_: type_, - payloads: payloads, - }); + let iq = Iq::try_from(elem).unwrap(); + self.proxy.dispatch(iq); } Propagation::Continue