messaging.rs

 1use plugin::{Plugin, PluginReturn, PluginProxy};
 2use event::Event;
 3use minidom::Element;
 4use jid::Jid;
 5use ns;
 6
 7#[derive(Debug)]
 8pub struct MessageEvent {
 9    from: Jid,
10    to: Jid,
11    body: String,
12}
13
14impl Event for MessageEvent {}
15
16pub struct MessagingPlugin {
17    proxy: PluginProxy,
18}
19
20impl MessagingPlugin {
21    pub fn new() -> MessagingPlugin {
22        MessagingPlugin {
23            proxy: PluginProxy::new(),
24        }
25    }
26}
27
28impl Plugin for MessagingPlugin {
29    fn get_proxy(&mut self) -> &mut PluginProxy {
30        &mut self.proxy
31    }
32
33    fn handle(&mut self, elem: &Element) -> PluginReturn {
34        if elem.is("message", ns::CLIENT) && elem.attr("type") == Some("chat") {
35            if let Some(body) = elem.get_child("body", ns::CLIENT) {
36                self.proxy.dispatch(MessageEvent { // TODO: safety!!!
37                    from: elem.attr("from").unwrap().parse().unwrap(),
38                    to: elem.attr("to").unwrap().parse().unwrap(),
39                    body: body.text(),
40                });
41            }
42        }
43        PluginReturn::Continue
44    }
45}