Created XMPP context and better features parser
authorThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Sun, 2 Nov 2008 03:58:31 +0000 (01:58 -0200)
committerThadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
Sun, 2 Nov 2008 03:58:31 +0000 (01:58 -0200)
Makefile
features.c [new file with mode: 0644]
tictactoe.c
xmpp.c [new file with mode: 0644]
xmpp.h [new file with mode: 0644]
xmpp_internal.h [new file with mode: 0644]

index d855658..15fe746 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,5 @@
-OBJECTS = sort_udns.o tcp_connect.o iksemel_extra.o tictactoe.o
+OBJECTS = sort_udns.o tcp_connect.o iksemel_extra.o tictactoe.o \
+       xmpp.o features.o
 CC = gcc
 CFLAGS = -g -Wall `pkg-config --cflags iksemel`
 LIBS = -ludns `pkg-config --libs iksemel`
diff --git a/features.c b/features.c
new file mode 100644 (file)
index 0000000..3fd1896
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include "xmpp_internal.h"
+
+void
+sasl (hc_xmpp_t *xmpp, iks *f)
+{
+  iks *c;
+  xmpp->sasl |= SUPPORTED;
+  for (c = iks_child (f); c != NULL; c = iks_next (c))
+    {
+      if (!iks_strcmp (iks_name (c), "optional"))
+        xmpp->sasl |= OPTIONAL;
+      else if (!iks_strcmp (iks_name (c), "required"))
+        xmpp->sasl |= REQUIRED;
+    }
+}
+
+void
+tls (hc_xmpp_t *xmpp, iks *f)
+{
+  iks *c;
+  xmpp->tls |= SUPPORTED;
+  for (c = iks_child (f); c != NULL; c = iks_next (c))
+    {
+      if (!iks_strcmp (iks_name (c), "optional"))
+        xmpp->tls |= OPTIONAL;
+      else if (!iks_strcmp (iks_name (c), "required"))
+        xmpp->tls |= REQUIRED;
+    }
+}
+
+void
+hc_xmpp_features (hc_xmpp_t *xmpp, iks *features)
+{
+  iks *c;
+  for (c = iks_child (features); c != NULL; c = iks_next (c))
+    {
+      if (!iks_strcmp (iks_name (c), "starttls") &&
+          !iks_strcmp (iks_find_attrib (c, "xmlns"), HC_XMPP_NS_TLS))
+        tls (xmpp, c);
+      if (!iks_strcmp (iks_name (c), "mechanisms") &&
+          !iks_strcmp (iks_find_attrib (c, "xmlns"), HC_XMPP_NS_SASL))
+        sasl (xmpp, c);
+    }
+}
index 19b4891..0049385 100644 (file)
 #include <string.h>
 #include "tcp_connect.h"
 #include "iksemel_extra.h"
-
-void
-plain (void *data)
-{
-  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);
-}
-
-void
-sasl (void *data, iks *s)
-{
-  iks *m;
-  for (m = iks_child (s); m != NULL; m = iks_next (m))
-    {
-      if (!iks_strcmp (iks_name (m), "mechanism") &&
-          !iks_strcmp (iks_cdata (iks_child (m)), "PLAIN"))
-        plain (data);
-    }
-}
-
-void
-features (void *data, iks *s)
-{
-  iks *f;
-  for (f = iks_child (s); f != NULL; f = iks_next (f))
-    {
-      if (!iks_strcmp (iks_name (f), "mechanisms") &&
-          !iks_strcmp (iks_find_attrib (f, "xmlns"),
-                       "urn:ietf:params:xml:ns:xmpp-sasl"))
-        sasl (data, f);
-    }
-}
+#include "xmpp.h"
 
 int
 myhook (void *data, int type, iks *stanza)
 {
   if (!iks_strcmp (iks_name (stanza), "stream:features"))
     {
-      features (data, stanza);
+      hc_xmpp_features (data, stanza);
+      if (hc_xmpp_is_tls_supported (data))
+        fprintf (stderr, "TLS is supported\n");
+      if (hc_xmpp_is_sasl_supported (data))
+        fprintf (stderr, "SASL is supported\n");
     }
   else
     {
@@ -104,9 +71,11 @@ main (int argc, char **argv)
   char *server = "jabber-br.org";
   int fd;
   iksparser *parser;
+  hc_xmpp_t *xmpp;
   dns_init (NULL, 1);
+  xmpp = hc_xmpp_new ();
   fd = hc_tcp_connect (server, "xmpp-client");
-  parser = iks_extra_stream_new ((void *) fd, myhook);
+  parser = iks_extra_stream_new (xmpp, myhook);
   write_stream (fd, server);
   loop (parser, fd);
   return 0;
diff --git a/xmpp.c b/xmpp.c
new file mode 100644 (file)
index 0000000..f42c55c
--- /dev/null
+++ b/xmpp.c
@@ -0,0 +1,80 @@
+/*
+ *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#include <stdlib.h>
+#include "xmpp.h"
+#include "xmpp_internal.h"
+
+hc_xmpp_t *
+hc_xmpp_new (void)
+{
+  hc_xmpp_t *xmpp = malloc (sizeof (hc_xmpp_t));
+  xmpp->tls = NONE;
+  xmpp->sasl = NONE;
+  return xmpp;
+}
+
+int
+hc_xmpp_is_tls_supported (hc_xmpp_t *xmpp)
+{
+  return xmpp->tls & SUPPORTED;
+}
+
+int
+hc_xmpp_is_tls_required (hc_xmpp_t *xmpp)
+{
+  return xmpp->tls & REQUIRED;
+}
+
+int
+hc_xmpp_is_tls_optional (hc_xmpp_t *xmpp)
+{
+  return xmpp->tls & OPTIONAL;
+}
+
+int
+hc_xmpp_is_tls_enabled (hc_xmpp_t *xmpp)
+{
+  return xmpp->tls & ENABLED;
+}
+
+int
+hc_xmpp_is_sasl_supported (hc_xmpp_t *xmpp)
+{
+  return xmpp->sasl & SUPPORTED;
+}
+
+int
+hc_xmpp_is_sasl_required (hc_xmpp_t *xmpp)
+{
+  return xmpp->sasl & REQUIRED;
+}
+
+int
+hc_xmpp_is_sasl_optional (hc_xmpp_t *xmpp)
+{
+  return xmpp->sasl & OPTIONAL;
+}
+
+int
+hc_xmpp_is_sasl_enabled (hc_xmpp_t *xmpp)
+{
+  return xmpp->sasl & ENABLED;
+}
+
diff --git a/xmpp.h b/xmpp.h
new file mode 100644 (file)
index 0000000..74cee4a
--- /dev/null
+++ b/xmpp.h
@@ -0,0 +1,41 @@
+/*
+ *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#ifndef HC_XMPP_H
+#define HC_XMPP_H
+
+#include <iksemel.h>
+
+typedef struct _hc_xmpp_t hc_xmpp_t;
+
+#define HC_XMPP_NS_TLS "urn:ietf:params:xml:ns:xmpp-tls"
+#define HC_XMPP_NS_SASL "urn:ietf:params:xml:ns:xmpp-sasl"
+
+hc_xmpp_t * hc_xmpp_new (void);
+int hc_xmpp_is_tls_supported (hc_xmpp_t *);
+int hc_xmpp_is_tls_required (hc_xmpp_t *);
+int hc_xmpp_is_tls_optional (hc_xmpp_t *);
+int hc_xmpp_is_tls_enabled (hc_xmpp_t *);
+int hc_xmpp_is_sasl_supported (hc_xmpp_t *);
+int hc_xmpp_is_sasl_required (hc_xmpp_t *);
+int hc_xmpp_is_sasl_optional (hc_xmpp_t *);
+int hc_xmpp_is_sasl_enabled (hc_xmpp_t *);
+void hc_xmpp_features (hc_xmpp_t *, iks *);
+
+#endif
diff --git a/xmpp_internal.h b/xmpp_internal.h
new file mode 100644 (file)
index 0000000..a1431e9
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ *  Copyright (C) 2008  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+
+#ifndef HC_XMPP_INTERNAL_H
+#define HC_XMPP_INTERNAL_H
+
+#include "xmpp.h"
+
+enum
+{
+  NONE = 0x0,
+  SUPPORTED = 0x1,
+  OPTIONAL = 0x2,
+  REQUIRED = 0x4,
+  ENABLED = 0x8
+};
+
+struct _hc_xmpp_t
+{
+  int tls;
+  int sasl;
+};
+
+#endif