add xmpp-parsers dependency, and use it in the stanza plugin

This commit is contained in:
Emmanuel Gil Peyrot 2017-05-23 23:45:05 +01:00
parent 2826f6624c
commit 9488c6fe12
3 changed files with 18 additions and 61 deletions

View file

@ -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"

View file

@ -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;

View file

@ -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<Jid>,
pub to: Option<Jid>,
pub id: Option<String>,
pub type_: Option<String>,
pub payloads: Vec<Element>,
}
#[derive(Debug)]
pub struct IqEvent {
pub from: Option<Jid>,
pub to: Option<Jid>,
pub id: Option<String>,
pub type_: Option<String>,
pub payloads: Vec<Element>,
}
#[derive(Debug)]
pub struct PresenceEvent {
pub from: Option<Jid>,
pub to: Option<Jid>,
pub id: Option<String>,
pub type_: Option<String>,
pub payloads: Vec<Element>,
}
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<ReceiveElement> 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::<Vec<_>>();
// 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