mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
Implement StanzaPlugin with {Message,Presence,Iq}Event
This commit is contained in:
parent
0e11cea0fa
commit
9c6b42a961
2 changed files with 95 additions and 0 deletions
|
@ -1,3 +1,4 @@
|
|||
pub mod messaging;
|
||||
pub mod presence;
|
||||
pub mod ping;
|
||||
pub mod stanza;
|
||||
|
|
94
src/plugins/stanza.rs
Normal file
94
src/plugins/stanza.rs
Normal file
|
@ -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<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 {}
|
||||
|
||||
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::<Vec<_>>();
|
||||
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue