Restructure the code a lot.
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 30 Jan 2009 23:40:39 +0000 (21:40 -0200)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Fri, 30 Jan 2009 23:40:39 +0000 (21:40 -0200)
This seems much more like libtc used to be.

status.c

index 78ccd6f..c93a6b5 100644 (file)
--- a/status.c
+++ b/status.c
 #include <iksemel.h>
 #include <stdio.h>
 
-struct udata
+int
+xmpp_session_hook (iksparser *parser, iks *node)
 {
-  iksparser *parser;
-  int auth;
-};
+  iks *iq;
+  iq = iks_new ("iq");
+  iks_insert_attrib (iq, "type", "set");
+  iks_insert_attrib (iq, "id", "session1");
+  iks_insert_attrib (iks_insert (iq, "session"),
+                     "xmlns", "urn:ietf:params:xml:ns:xmpp-session");
+  iks_send (parser, iq);
+  iks_delete (iq);
+  return 0;
+}
 
-enum
+int
+xmpp_initial_presence_hook (iksparser *parser, iks *node)
 {
-  AUTH_NONE,
-  AUTH_TLS,
-  AUTH_SASL,
-  AUTH_BIND,
-  AUTH_SESSION
-} AUTH_STATE;
+  iks *pres;
+  pres = iks_make_pres (IKS_SHOW_AVAILABLE, "Microblogging here!\n");
+  iks_send (parser, pres);
+  iks_delete (pres);
+  return 0;
+}
 
-static int
-hook (void *data, int type, iks *node)
+int
+xmpp_id_hook (iksparser *parser, iks *node, char *id)
 {
-  iks *sub;
-  struct udata *udata;
-  udata = (struct udata *) data;
-  if (type == IKS_NODE_START)
+  if (!iks_strcmp (id, "bind1"))
     {
-      if (!iks_is_secure (udata->parser) || udata->auth == AUTH_NONE)
-        {
-          iks_start_tls (udata->parser);
-          udata->auth = AUTH_TLS;
-        }
-      else if (udata->auth == AUTH_TLS)
-        {
-          iks_start_sasl (udata->parser, IKS_SASL_PLAIN, "pubsub", "pubsub");
-        }
-      else if (udata->auth == AUTH_SASL)
-        {
-          iks_send_raw (udata->parser, "<iq type='set' id='boo'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>");
-        }
+      xmpp_session_hook (parser, node);
+      return 0;
+    }
+  else if (!iks_strcmp (id, "session1"))
+    {
+      xmpp_initial_presence_hook (parser, node);
+      return 0;
     }
+  return 1;
+}
+
+int
+xmpp_ns_hook (iksparser *parser, iks *node, char *ns)
+{
+  return 1;
+}
+
+int
+xmpp_iq_error (iksparser *parser, iks *node)
+{
+  iks *enode;
+  char *to;
+  char *from;
+  if (!iks_strcmp (iks_find_attrib (node, "type"), "error"))
+    return 1;
+  to = iks_find_attrib (node, "to");
+  from = iks_find_attrib (node, "from");
+  if (to)
+    iks_insert_attrib (node, "from", to);
   else
+    iks_insert_attrib (node, "from", NULL);
+  if (from)
+    iks_insert_attrib (node, "to", from);
+  else
+    iks_insert_attrib (node, "to", NULL);
+  iks_insert_attrib (node, "type", "error");
+  enode = iks_insert (node, "error");
+  iks_insert_attrib (enode, "type", "cancel");
+  iks_insert_attrib (iks_insert (enode, "feature-not-implemented"),
+                     "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas");
+  iks_send (parser, node);
+  return 0;
+}
+
+int
+xmpp_tls_hook (iksparser *parser, iks *node)
+{
+  iks_start_tls (parser);
+  return 0;
+}
+
+int
+xmpp_sasl_hook (iksparser *parser, iks* node)
+{
+  iks_start_sasl (parser, IKS_SASL_DIGEST_MD5, "pubsub", "pubsub");
+  return 0;
+}
+
+int
+xmpp_bind_hook (iksparser *parser, iks *node)
+{
+  iks *iq;
+  iq = iks_new ("iq");
+  iks_insert_attrib (iq, "type", "set");
+  iks_insert_attrib (iq, "id", "bind1");
+  iks_insert_attrib (iks_insert (iq, "bind"),
+                     "xmlns", "urn:ietf:params:xml:ns:xmpp-bind");
+  iks_send (parser, iq);
+  iks_delete (iq);
+  return 0;
+}
+
+int
+xmpp_features_hook (iksparser *parser, iks *node)
+{
+  iks *feat;
+  char *ns;
+  for (feat = iks_child (node); feat != NULL; feat = iks_next (feat))
     {
-      if (!iks_strcmp (iks_find_attrib (node, "id"), "boo"))
+      ns = iks_find_attrib (feat, "xmlns");
+      if (!iks_strcmp (ns, "urn:ietf:params:xml:ns:xmpp-tls"))
         {
-          udata->auth = AUTH_BIND;
-          iks_send_raw (udata->parser, "<iq type='set' id='goo'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>");
+          xmpp_tls_hook (parser, node);
+          return 0;
         }
-      else if (!iks_strcmp (iks_find_attrib (node, "id"), "goo"))
+      else if (!iks_strcmp (ns, "urn:ietf:params:xml:ns:xmpp-sasl"))
         {
-          printf ("goooooo\n");
-          sub = iks_make_s10n (IKS_TYPE_SUBSCRIBED, "cascardo@jabber-br.org", "");
-          iks_send (udata->parser, sub);
-          iks_delete (sub);
-          sub = iks_make_pres (IKS_SHOW_AVAILABLE, "here");
-          iks_insert_attrib (sub, "to", "cascardo@jabber-br.org");
-          iks_send (udata->parser, sub);
-          iks_delete (sub);
+          xmpp_sasl_hook (parser, node);
+          return 0;
         }
-      if (!iks_strcmp (iks_name (node), "success"))
+      else if (!iks_strcmp (ns, "urn:ietf:params:xml:ns:xmpp-bind"))
         {
-          iks_send_header (udata->parser, "jabber-br.org");
-          udata->auth = AUTH_SASL;
+          xmpp_bind_hook (parser, node);
+          return 0;
         }
-      printf ("%s\n", iks_string (iks_stack (node), node));
+    }
+  return 1;
+}
+
+int
+xmpp_other_hook (iksparser *parser, iks *node, char *ns)
+{
+  if (!iks_strcmp (ns, "urn:ietf:params:xml:ns:xmpp-sasl"))
+    {
+      if (!iks_strcmp (iks_name (node), "success"))
+        iks_send_header (parser, "jabber-br.org");
+      return 0;
+    }
+  return 1;
+}
+
+static int
+hook (void *data, int type, iks *node)
+{
+  char *name;
+  char *id;
+  char *ns;
+  char *iqns;
+  iksparser *parser;
+  parser = *(iksparser **) data;
+  name = iks_name (node);
+  id = iks_find_attrib (node, "id");
+  ns = iks_find_attrib (node, "xmlns"); 
+  iqns = iks_find_attrib (iks_child (node), "xmlns"); 
+  if (!iks_strcmp (name, "message"))
+    {
+      printf ("Got milk?\n");
+      return IKS_OK;
+    }
+  else if (!iks_strcmp (name, "presence"))
+    {
+      printf ("I sense a disturbance in the force!\n");
+    }
+  else if (!iks_strcmp (name, "iq"))
+    {
+      if (xmpp_id_hook (parser, node, id) == 0)
+        return IKS_OK;
+      if (xmpp_ns_hook (parser, node, iqns) == 0)
+        return IKS_OK;
+      xmpp_iq_error (parser, node);
+    }
+  else if (!iks_strcmp (name, "stream:features"))
+    {
+      if (xmpp_features_hook (parser, node) == 0)
+        return IKS_OK;
+    }
+  else
+    {
+      if (xmpp_other_hook (parser, node, ns) == 0)
+        return IKS_OK;
+      printf ("No no: %s\n", name);
     }
   return IKS_OK;
 }
@@ -88,11 +207,10 @@ hook (void *data, int type, iks *node)
 int
 main (int argc, char **argv)
 {
-  struct udata udata;
-  udata.auth = 0;
-  udata.parser = iks_stream_new ("jabber:client", &udata, hook);
-  iks_connect_tcp (udata.parser, "jabber-br.org", 5222);
+  iksparser *parser;
+  parser = iks_stream_new ("jabber:client", &parser, hook);
+  iks_connect_tcp (parser, "jabber-br.org", 5222);
   while (1)
-    iks_recv (udata.parser, -1);
+    iks_recv (parser, -1);
   return 0;
 }