From 2826f6624c2983cf81f3f0fbf6d54ca3a38f2f85 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 23 May 2017 23:40:28 +0100 Subject: [PATCH 1/2] update dependencies --- Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3f7be42a..d57c901f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,12 +16,12 @@ gitlab = { repository = "lumi/xmpp-rs" } [dependencies] xml-rs = "0.4.1" -openssl = "0.9.7" +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 = [] From 9488c6fe1261d0ebc8fb5ee0d009ef49f818225c Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Tue, 23 May 2017 23:45:05 +0100 Subject: [PATCH 2/2] add xmpp-parsers dependency, and use it in the stanza plugin --- Cargo.toml | 1 + src/lib.rs | 2 ++ src/plugins/stanza.rs | 76 +++++++++---------------------------------- 3 files changed, 18 insertions(+), 61 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d57c901f..0475a1b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ gitlab = { repository = "lumi/xmpp-rs" } [dependencies] xml-rs = "0.4.1" +xmpp-parsers = "0.3.0" openssl = "0.9.12" base64 = "0.5.2" minidom = "0.4.1" 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