add an unhandled iq plugin

Emmanuel Gil Peyrot created

Change summary

examples/client.rs          |  2 +
src/plugins/mod.rs          |  1 
src/plugins/unhandled_iq.rs | 48 +++++++++++++++++++++++++++++++++++++++
3 files changed, 51 insertions(+)

Detailed changes

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());

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;

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,
+]);