1use std::fmt::Debug;
2use std::any::Any;
3
4use plugin::{Plugin, PluginReturn, PluginProxy};
5use event::Event;
6use minidom::Element;
7use jid::Jid;
8use ns;
9
10pub trait Stanza: Any + Debug {}
11
12#[derive(Debug)]
13pub struct MessageEvent {
14 pub from: Option<Jid>,
15 pub to: Option<Jid>,
16 pub id: Option<String>,
17 pub type_: Option<String>,
18 pub payloads: Vec<Element>,
19}
20
21#[derive(Debug)]
22pub struct IqEvent {
23 pub from: Option<Jid>,
24 pub to: Option<Jid>,
25 pub id: Option<String>,
26 pub type_: Option<String>,
27 pub payloads: Vec<Element>,
28}
29
30#[derive(Debug)]
31pub struct PresenceEvent {
32 pub from: Option<Jid>,
33 pub to: Option<Jid>,
34 pub id: Option<String>,
35 pub type_: Option<String>,
36 pub payloads: Vec<Element>,
37}
38
39impl Event for MessageEvent {}
40impl Event for IqEvent {}
41impl Event for PresenceEvent {}
42
43pub struct StanzaPlugin {
44 proxy: PluginProxy,
45}
46
47impl StanzaPlugin {
48 pub fn new() -> StanzaPlugin {
49 StanzaPlugin {
50 proxy: PluginProxy::new(),
51 }
52 }
53}
54
55impl Plugin for StanzaPlugin {
56 fn get_proxy(&mut self) -> &mut PluginProxy {
57 &mut self.proxy
58 }
59
60 fn handle(&mut self, elem: &Element) -> PluginReturn {
61 let from = match elem.attr("from") { Some(from) => Some(from.parse().unwrap()), None => None };
62 let to = match elem.attr("to") { Some(to) => Some(to.parse().unwrap()), None => None };
63 let id = match elem.attr("id") { Some(id) => Some(id.parse().unwrap()), None => None };
64 let type_ = match elem.attr("type") { Some(type_) => Some(type_.parse().unwrap()), None => None };
65 let payloads = elem.children().cloned().collect::<Vec<_>>();
66
67 if elem.is("message", ns::CLIENT) {
68 self.proxy.dispatch(MessageEvent {
69 from: from,
70 to: to,
71 id: id,
72 type_: type_,
73 payloads: payloads,
74 });
75 } else if elem.is("presence", ns::CLIENT) {
76 self.proxy.dispatch(PresenceEvent {
77 from: from,
78 to: to,
79 id: id,
80 type_: type_,
81 payloads: payloads,
82 });
83 } else if elem.is("iq", ns::CLIENT) {
84 self.proxy.dispatch(IqEvent {
85 from: from,
86 to: to,
87 id: id,
88 type_: type_,
89 payloads: payloads,
90 });
91 }
92 PluginReturn::Continue
93 }
94}