xmpp-rs/src/plugins/messaging.rs

56 lines
1.5 KiB
Rust
Raw Normal View History

2017-02-20 16:28:51 +01:00
use plugin::{Plugin, PluginReturn, PluginProxy};
use event::Event;
use minidom::Element;
use error::Error;
2017-02-20 16:28:51 +01:00
use jid::Jid;
use ns;
#[derive(Debug)]
pub struct MessageEvent {
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(),
}
}
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());
self.proxy.send(elem);
Ok(())
}
2017-02-20 16:28:51 +01:00
}
impl Plugin for MessagingPlugin {
fn get_proxy(&mut self) -> &mut PluginProxy {
&mut self.proxy
}
fn handle(&mut self, elem: &Element) -> PluginReturn {
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(),
});
}
}
PluginReturn::Continue
}
}