diff --git a/examples/client.rs b/examples/client.rs index 0167793c72dfde379a5137ca054664f25e44d693..9b8f66827311f2303b633425cc9c278bfb36a97d 100644 --- a/examples/client.rs +++ b/examples/client.rs @@ -3,6 +3,7 @@ extern crate xmpp; use xmpp::jid::Jid; use xmpp::client::ClientBuilder; use xmpp::plugins::stanza::StanzaPlugin; +use xmpp::plugins::unhandled_iq::UnhandledIqPlugin; use xmpp::plugins::messaging::{MessagingPlugin, MessageEvent}; use xmpp::plugins::presence::{PresencePlugin, Type}; use xmpp::plugins::ping::{PingPlugin, PingEvent}; @@ -18,6 +19,7 @@ fn main() { .connect() .unwrap(); client.register_plugin(StanzaPlugin::new()); + client.register_plugin(UnhandledIqPlugin::new()); client.register_plugin(MessagingPlugin::new()); client.register_plugin(PresencePlugin::new()); client.register_plugin(PingPlugin::new()); diff --git a/src/plugins/mod.rs b/src/plugins/mod.rs index c15ae5122d5c0e7a9fb1340fde90ca697c9bc630..8bbb3af8f13d027f6ff1b4b81bc9f68f112f64a9 100644 --- a/src/plugins/mod.rs +++ b/src/plugins/mod.rs @@ -3,3 +3,4 @@ pub mod presence; pub mod ping; pub mod stanza; pub mod stanza_debug; +pub mod unhandled_iq; diff --git a/src/plugins/unhandled_iq.rs b/src/plugins/unhandled_iq.rs new file mode 100644 index 0000000000000000000000000000000000000000..6255bf50965286c5853eee1502f47cf1f52cf507 --- /dev/null +++ b/src/plugins/unhandled_iq.rs @@ -0,0 +1,48 @@ +use std::collections::BTreeMap; + +use plugin::PluginProxy; +use event::{Priority, Propagation}; + +use plugins::stanza::Iq; +use xmpp_parsers::iq::IqType; +use xmpp_parsers::stanza_error::{StanzaError, ErrorType, DefinedCondition}; + +pub struct UnhandledIqPlugin { + proxy: PluginProxy, +} + +impl UnhandledIqPlugin { + pub fn new() -> UnhandledIqPlugin { + UnhandledIqPlugin { + proxy: PluginProxy::new(), + } + } + + fn reply_unhandled_iq(&self, iq: &Iq) -> Propagation { + let iq = iq.clone(); + match iq.payload { + IqType::Get(_) + | IqType::Set(_) => { + self.proxy.send(Iq { + from: None, + to: Some(iq.from.unwrap()), + id: Some(iq.id.unwrap()), + payload: IqType::Error(StanzaError { + type_: ErrorType::Cancel, + defined_condition: DefinedCondition::ServiceUnavailable, + texts: BTreeMap::new(), + by: None, + other: None, + }), + }.into()); + Propagation::Stop + }, + IqType::Result(_) + | IqType::Error(_) => Propagation::Continue + } + } +} + +impl_plugin!(UnhandledIqPlugin, proxy, [ + (Iq, Priority::Min) => reply_unhandled_iq, +]);