2017-05-27 16:56:44 +02:00
|
|
|
use plugin::PluginProxy;
|
|
|
|
|
use event::{Event, ReceiveElement, Priority, Propagation};
|
2017-02-20 16:28:51 +01:00
|
|
|
use minidom::Element;
|
2017-02-27 15:03:08 +01:00
|
|
|
use error::Error;
|
2017-02-20 16:28:51 +01:00
|
|
|
use jid::Jid;
|
|
|
|
|
use ns;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct MessageEvent {
|
2017-02-27 15:03:08 +01:00
|
|
|
pub from: Jid,
|
|
|
|
|
pub to: Jid,
|
|
|
|
|
pub body: String,
|
2017-02-20 16:28:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Event for MessageEvent {}
|
|
|
|
|
|
|
|
|
|
pub struct MessagingPlugin {
|
|
|
|
|
proxy: PluginProxy,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl MessagingPlugin {
|
|
|
|
|
pub fn new() -> MessagingPlugin {
|
|
|
|
|
MessagingPlugin {
|
|
|
|
|
proxy: PluginProxy::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-27 15:03:08 +01:00
|
|
|
|
|
|
|
|
pub fn send_message(&self, to: &Jid, body: &str) -> Result<(), Error> {
|
|
|
|
|
let mut elem = Element::builder("message")
|
|
|
|
|
.attr("type", "chat")
|
|
|
|
|
.attr("to", to.to_string())
|
|
|
|
|
.build();
|
2017-04-30 17:44:07 +01:00
|
|
|
elem.append_child(Element::builder("body").append(body).build());
|
2017-02-27 15:03:08 +01:00
|
|
|
self.proxy.send(elem);
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2017-02-20 16:28:51 +01:00
|
|
|
|
2017-05-27 16:56:44 +02:00
|
|
|
fn handle_receive_element(&self, evt: &ReceiveElement) -> Propagation {
|
2017-05-10 00:13:54 +02:00
|
|
|
let elem = &evt.0;
|
2017-02-20 16:28:51 +01:00
|
|
|
if elem.is("message", ns::CLIENT) && elem.attr("type") == Some("chat") {
|
|
|
|
|
if let Some(body) = elem.get_child("body", ns::CLIENT) {
|
|
|
|
|
self.proxy.dispatch(MessageEvent { // TODO: safety!!!
|
|
|
|
|
from: elem.attr("from").unwrap().parse().unwrap(),
|
|
|
|
|
to: elem.attr("to").unwrap().parse().unwrap(),
|
|
|
|
|
body: body.text(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-10 00:13:54 +02:00
|
|
|
Propagation::Continue
|
2017-02-20 16:28:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
2017-05-27 16:56:44 +02:00
|
|
|
|
|
|
|
|
impl_plugin!(MessagingPlugin, proxy, [
|
|
|
|
|
(ReceiveElement, Priority::Default) => handle_receive_element,
|
|
|
|
|
]);
|