rewrite the ping plugin to make it use the stanza plugin
This commit is contained in:
parent
8e7b7ae31c
commit
c48086bc9c
1 changed files with 34 additions and 27 deletions
|
@ -1,14 +1,17 @@
|
||||||
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
use plugin::PluginProxy;
|
use plugin::PluginProxy;
|
||||||
use event::{Event, Priority, Propagation, ReceiveElement};
|
use event::{Event, Priority, Propagation};
|
||||||
use minidom::Element;
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use jid::Jid;
|
use jid::Jid;
|
||||||
use ns;
|
|
||||||
|
use plugins::stanza::Iq;
|
||||||
|
use xmpp_parsers::iq::{IqType, IqPayload};
|
||||||
|
use xmpp_parsers::ping::Ping;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct PingEvent {
|
pub struct PingEvent {
|
||||||
pub from: Jid,
|
pub from: Jid,
|
||||||
pub to: Jid,
|
|
||||||
pub id: String,
|
pub id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -26,39 +29,43 @@ impl PingPlugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_ping(&self, to: &Jid) -> Result<(), Error> {
|
pub fn send_ping(&self, to: &Jid) -> Result<(), Error> {
|
||||||
let mut elem = Element::builder("iq")
|
let to = to.clone();
|
||||||
.attr("type", "get")
|
self.proxy.send(Iq {
|
||||||
.attr("to", to.to_string())
|
from: None,
|
||||||
.build();
|
to: Some(to),
|
||||||
elem.append_child(Element::builder("ping").ns(ns::PING).build());
|
// TODO: use a generic way to generate ids.
|
||||||
self.proxy.send(elem);
|
id: Some(String::from("id")),
|
||||||
|
payload: IqType::Get(IqPayload::Ping(Ping).into()),
|
||||||
|
}.into());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reply_ping(&self, event: &PingEvent) {
|
fn handle_iq(&self, iq: &Iq) -> Propagation {
|
||||||
let reply = Element::builder("iq")
|
let iq = iq.clone();
|
||||||
.attr("type", "result")
|
if let IqType::Get(payload) = iq.payload {
|
||||||
.attr("to", event.from.to_string())
|
// TODO: use an intermediate plugin to parse this payload.
|
||||||
.attr("id", event.id.to_string())
|
if let Ok(IqPayload::Ping(_)) = IqPayload::try_from(payload) {
|
||||||
.build();
|
|
||||||
self.proxy.send(reply);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn handle_receive_element(&self, evt: &ReceiveElement) -> Propagation {
|
|
||||||
let elem = &evt.0;
|
|
||||||
if elem.is("iq", ns::CLIENT) && elem.attr("type") == Some("get") {
|
|
||||||
if elem.has_child("ping", ns::PING) {
|
|
||||||
self.proxy.dispatch(PingEvent { // TODO: safety!!!
|
self.proxy.dispatch(PingEvent { // TODO: safety!!!
|
||||||
from: elem.attr("from").unwrap().parse().unwrap(),
|
from: iq.from.unwrap(),
|
||||||
to: elem.attr("to").unwrap().parse().unwrap(),
|
id: iq.id.unwrap(),
|
||||||
id: elem.attr("id").unwrap().parse().unwrap(),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Propagation::Continue
|
Propagation::Continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn reply_ping(&self, ping: &PingEvent) -> Propagation {
|
||||||
|
self.proxy.send(Iq {
|
||||||
|
from: None,
|
||||||
|
to: Some(ping.from.to_owned()),
|
||||||
|
id: Some(ping.id.to_owned()),
|
||||||
|
payload: IqType::Result(None),
|
||||||
|
}.into());
|
||||||
|
Propagation::Continue
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_plugin!(PingPlugin, proxy, [
|
impl_plugin!(PingPlugin, proxy, [
|
||||||
(ReceiveElement, Priority::Default) => handle_receive_element,
|
(Iq, Priority::Default) => handle_iq,
|
||||||
|
(PingEvent, Priority::Default) => reply_ping,
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Reference in a new issue