+/*
+ * Copyright (C) 2009 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 <iksemel.h>
+#include <stdio.h>
+
+struct udata
+{
+ iksparser *parser;
+ int auth;
+};
+
+enum
+{
+ AUTH_NONE,
+ AUTH_TLS,
+ AUTH_SASL,
+ AUTH_BIND,
+ AUTH_SESSION
+} AUTH_STATE;
+
+static int
+hook (void *data, int type, iks *node)
+{
+ iks *sub;
+ struct udata *udata;
+ udata = (struct udata *) data;
+ if (type == IKS_NODE_START)
+ {
+ 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>");
+ }
+ }
+ else
+ {
+ if (!iks_strcmp (iks_find_attrib (node, "id"), "boo"))
+ {
+ udata->auth = AUTH_BIND;
+ iks_send_raw (udata->parser, "<iq type='set' id='goo'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>");
+ }
+ else if (!iks_strcmp (iks_find_attrib (node, "id"), "goo"))
+ {
+ 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);
+ }
+ if (!iks_strcmp (iks_name (node), "success"))
+ {
+ iks_send_header (udata->parser, "jabber-br.org");
+ udata->auth = AUTH_SASL;
+ }
+ printf ("%s\n", iks_string (iks_stack (node), node));
+ }
+ return IKS_OK;
+}
+
+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);
+ while (1)
+ iks_recv (udata.parser, -1);
+ return 0;
+}