make PingPlugin add its feature to DiscoPlugin

Emmanuel Gil Peyrot created

Change summary

examples/client.rs  |  1 +
src/plugins/ping.rs | 20 ++++++++++++++++++++
2 files changed, 21 insertions(+)

Detailed changes

examples/client.rs 🔗

@@ -27,6 +27,7 @@ fn main() {
     client.register_plugin(DiscoPlugin::new("client", "bot", "en", "xmpp-rs"));
     client.register_plugin(IbbPlugin::new());
     client.register_plugin(PingPlugin::new());
+    client.plugin::<PingPlugin>().init();
     client.register_handler(Priority::Max, |e: &MessageEvent| {
         println!("{:?}", e);
         Propagation::Continue

src/plugins/ping.rs 🔗

@@ -6,8 +6,10 @@ use error::Error;
 use jid::Jid;
 
 use plugins::stanza::Iq;
+use plugins::disco::DiscoPlugin;
 use xmpp_parsers::iq::{IqType, IqPayload};
 use xmpp_parsers::ping::Ping;
+use xmpp_parsers::ns;
 
 #[derive(Debug)]
 pub struct PingEvent {
@@ -28,6 +30,24 @@ impl PingPlugin {
         }
     }
 
+    // TODO: make that called automatically after plugins are created.
+    pub fn init(&self) {
+        if let Some(disco) = self.proxy.plugin::<DiscoPlugin>() {
+            disco.add_feature(ns::PING);
+        } else {
+            panic!("Please handle dependencies in the correct order.");
+        }
+    }
+
+    // TODO: make that called automatically before removal.
+    pub fn deinit(&self) {
+        if let Some(disco) = self.proxy.plugin::<DiscoPlugin>() {
+            disco.remove_feature(ns::PING);
+        } else {
+            panic!("Please handle dependencies in the correct order.");
+        }
+    }
+
     pub fn send_ping(&self, to: &Jid) -> Result<(), Error> {
         let to = to.clone();
         self.proxy.send(Iq {