add a Component2S connection type

Emmanuel Gil Peyrot created

Change summary

src/connection.rs | 23 +++++++++++++++++++++++
src/ns.rs         |  1 +
2 files changed, 24 insertions(+)

Detailed changes

src/connection.rs 🔗

@@ -36,3 +36,26 @@ impl Connection for C2S {
         Ok(())
     }
 }
+
+pub struct Component2S;
+
+impl Connection for Component2S {
+    type InitError = Error;
+    type CloseError = Error;
+
+    fn namespace() -> &'static str { ns::COMPONENT_ACCEPT }
+
+    fn init<T: Transport>(transport: &mut T, domain: &str, id: &str) -> Result<(), Error> {
+        transport.write_event(WriterEvent::start_element("stream:stream")
+                                          .attr("to", domain)
+                                          .attr("id", id)
+                                          .default_ns(ns::COMPONENT_ACCEPT)
+                                          .ns("stream", ns::STREAM))?;
+        Ok(())
+    }
+
+    fn close<T: Transport>(transport: &mut T) -> Result<(), Error> {
+        transport.write_event(WriterEvent::end_element())?;
+        Ok(())
+    }
+}

src/ns.rs 🔗

@@ -1,6 +1,7 @@
 //! Provides constants for namespaces.
 
 pub const CLIENT: &'static str = "jabber:client";
+pub const COMPONENT_ACCEPT: &'static str = "jabber:component:accept";
 pub const STREAM: &'static str = "http://etherx.jabber.org/streams";
 pub const TLS: &'static str = "urn:ietf:params:xml:ns:xmpp-tls";
 pub const SASL: &'static str = "urn:ietf:params:xml:ns:xmpp-sasl";