Query the server for its info
[cascardo/chat.git] / tictactoe.c
index 19b4891..ed74e49 100644 (file)
 #include <string.h>
 #include "tcp_connect.h"
 #include "iksemel_extra.h"
+#include "xmpp.h"
+#include "sasl.h"
+#include "bind.h"
+#include "disco.h"
 
-void
-plain (void *data)
+static void write_stream (hc_xmpp_t *);
+
+static void
+send_message_test (hc_xmpp_t *xmpp)
 {
-  iks *mplain;
-  char *str;
-  mplain = iks_new ("auth");
-  iks_insert_attrib (mplain, "xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
-  iks_insert_attrib (mplain, "mechanism", "PLAIN");
-  str = iks_string (iks_stack (mplain), mplain);
-  write ((int) data, str, strlen (str));
-  iks_delete (mplain);
+  hc_xmpp_send_disco_info (xmpp, hc_xmpp_server (xmpp));
 }
 
-void
-sasl (void *data, iks *s)
+static int
+myhook (void *data, int type, iks *stanza)
 {
-  iks *m;
-  for (m = iks_child (s); m != NULL; m = iks_next (m))
+  if (!iks_strcmp (iks_name (stanza), "iq"))
     {
-      if (!iks_strcmp (iks_name (m), "mechanism") &&
-          !iks_strcmp (iks_cdata (iks_child (m)), "PLAIN"))
-        plain (data);
+      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 (ns, HC_XMPP_NS_DISCO_INFO))
+        {
+          hc_xmpp_recv_disco (data, stanza);
+        }
     }
-}
-
-void
-features (void *data, iks *s)
-{
-  iks *f;
-  for (f = iks_child (s); f != NULL; f = iks_next (f))
+  else if (!iks_strcmp (iks_name (stanza), "stream:features"))
     {
-      if (!iks_strcmp (iks_name (f), "mechanisms") &&
-          !iks_strcmp (iks_find_attrib (f, "xmlns"),
-                       "urn:ietf:params:xml:ns:xmpp-sasl"))
-        sasl (data, f);
+      hc_xmpp_features (data, stanza);
+      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);
+        }
     }
-}
-
-int
-myhook (void *data, int type, iks *stanza)
-{
-  if (!iks_strcmp (iks_name (stanza), "stream:features"))
+  else if (!iks_strcmp (iks_find_attrib (stanza, "xmlns"), HC_XMPP_NS_SASL))
     {
-      features (data, stanza);
+      hc_xmpp_sasl_iterate (data, stanza);
+      if (hc_xmpp_status (data) == HC_XMPP_AUTHENTICATED)
+        {
+          write_stream (data);
+          fprintf (stdout, "Authenticated\n");
+        }
     }
   else
     {
@@ -78,36 +90,34 @@ myhook (void *data, int type, iks *stanza)
   return IKS_OK;
 }
 
-void
-write_stream (int fd, char *server)
+static void
+write_stream (hc_xmpp_t *xmpp)
 {
   char *buffer = NULL;
   asprintf (&buffer, "<stream:stream xmlns='jabber:client' "
                      "xmlns:stream='http://etherx.jabber.org/streams' "
-                     "version='1.0' to='%s'>", server);
-  write (fd, buffer, strlen (buffer));
+                     "version='1.0' to='%s'>", hc_xmpp_server (xmpp));
+  hc_xmpp_send_buffer (xmpp, buffer, 0);
   free (buffer);
 }
 
-void
-loop (iksparser *parser, int fd)
+static void
+loop (hc_xmpp_t *xmpp)
 {
-  char buffer[4096];
-  int r;
-  while ((r = read (fd, buffer, sizeof (buffer))) > 0)
-    iks_parse (parser, buffer, r, 0);
+  while (1)
+    hc_xmpp_read_and_parse (xmpp);
 }
 
 int
 main (int argc, char **argv)
 {
   char *server = "jabber-br.org";
-  int fd;
-  iksparser *parser;
+  char *user = "pubsub";
+  char *password = "pubsub";
+  hc_xmpp_t *xmpp;
   dns_init (NULL, 1);
-  fd = hc_tcp_connect (server, "xmpp-client");
-  parser = iks_extra_stream_new ((void *) fd, myhook);
-  write_stream (fd, server);
-  loop (parser, fd);
+  xmpp = hc_xmpp_new (myhook, server, user, password);
+  write_stream (xmpp);
+  loop (xmpp);
   return 0;
 }