Added support for resouce binding and session establishment
[cascardo/chat.git] / tictactoe.c
index 700a269..b57c1b9 100644 (file)
 #include "iksemel_extra.h"
 #include "xmpp.h"
 #include "sasl.h"
+#include "bind.h"
 
-int
+static void write_stream (hc_xmpp_t *);
+
+static void
+send_message_test (hc_xmpp_t *xmpp)
+{
+  iks *msg;
+  msg = iks_new ("message");
+  iks_insert_attrib (msg, "to", "metal@jabber-br.org");
+  iks_insert_cdata (iks_insert (msg, "body"), "pubsub", 0);
+  hc_xmpp_send_iks (xmpp, msg);
+  iks_delete (msg);
+}
+
+static int
 myhook (void *data, int type, iks *stanza)
 {
-  if (!iks_strcmp (iks_name (stanza), "stream:features"))
+  if (!iks_strcmp (iks_name (stanza), "iq"))
+    {
+      char *ns = iks_find_attrib (iks_child (stanza), "xmlns");
+      if (!iks_strcmp (ns, HC_XMPP_NS_BIND))
+        {
+          hc_xmpp_bind_result (data, stanza);
+          if (hc_xmpp_status (data) == HC_XMPP_BOUND &&
+              hc_xmpp_is_session_supported (data))
+            hc_xmpp_session (data);
+        }
+      else if (!iks_strcmp (ns, HC_XMPP_NS_SESSION))
+        {
+          hc_xmpp_session_result (data, stanza);
+          if (hc_xmpp_status (data) == HC_XMPP_SESSION)
+            send_message_test (data);
+        }
+    }
+  else if (!iks_strcmp (iks_name (stanza), "stream:features"))
     {
       hc_xmpp_features (data, stanza);
-      if (hc_xmpp_is_sasl_supported (data))
+      if (hc_xmpp_is_sasl_supported (data) & !hc_xmpp_is_sasl_enabled (data))
         {
           hc_xmpp_sasl_authenticate (data);
         }
+      if (hc_xmpp_is_bind_supported (data))
+        {
+          hc_xmpp_bind (data);
+        }
     }
   else if (!iks_strcmp (iks_find_attrib (stanza, "xmlns"), HC_XMPP_NS_SASL))
     {
       hc_xmpp_sasl_iterate (data, stanza);
       if (hc_xmpp_status (data) == HC_XMPP_AUTHENTICATED)
-        fprintf (stdout, "Authenticated\n");
+        {
+          write_stream (data);
+          fprintf (stdout, "Authenticated\n");
+        }
     }
   else
     {
@@ -52,7 +90,7 @@ myhook (void *data, int type, iks *stanza)
   return IKS_OK;
 }
 
-void
+static void
 write_stream (hc_xmpp_t *xmpp)
 {
   char *buffer = NULL;
@@ -63,7 +101,7 @@ write_stream (hc_xmpp_t *xmpp)
   free (buffer);
 }
 
-void
+static void
 loop (hc_xmpp_t *xmpp)
 {
   while (1)