78ccd6f3454589d5c08da0c6dcc1acfe74189f1b
[cascardo/pubsub-bot.git] / status.c
1 /*
2  *  Copyright (C) 2009  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <iksemel.h>
21 #include <stdio.h>
22
23 struct udata
24 {
25   iksparser *parser;
26   int auth;
27 };
28
29 enum
30 {
31   AUTH_NONE,
32   AUTH_TLS,
33   AUTH_SASL,
34   AUTH_BIND,
35   AUTH_SESSION
36 } AUTH_STATE;
37
38 static int
39 hook (void *data, int type, iks *node)
40 {
41   iks *sub;
42   struct udata *udata;
43   udata = (struct udata *) data;
44   if (type == IKS_NODE_START)
45     {
46       if (!iks_is_secure (udata->parser) || udata->auth == AUTH_NONE)
47         {
48           iks_start_tls (udata->parser);
49           udata->auth = AUTH_TLS;
50         }
51       else if (udata->auth == AUTH_TLS)
52         {
53           iks_start_sasl (udata->parser, IKS_SASL_PLAIN, "pubsub", "pubsub");
54         }
55       else if (udata->auth == AUTH_SASL)
56         {
57           iks_send_raw (udata->parser, "<iq type='set' id='boo'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq>");
58         }
59     }
60   else
61     {
62       if (!iks_strcmp (iks_find_attrib (node, "id"), "boo"))
63         {
64           udata->auth = AUTH_BIND;
65           iks_send_raw (udata->parser, "<iq type='set' id='goo'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq>");
66         }
67       else if (!iks_strcmp (iks_find_attrib (node, "id"), "goo"))
68         {
69           printf ("goooooo\n");
70           sub = iks_make_s10n (IKS_TYPE_SUBSCRIBED, "cascardo@jabber-br.org", "");
71           iks_send (udata->parser, sub);
72           iks_delete (sub);
73           sub = iks_make_pres (IKS_SHOW_AVAILABLE, "here");
74           iks_insert_attrib (sub, "to", "cascardo@jabber-br.org");
75           iks_send (udata->parser, sub);
76           iks_delete (sub);
77         }
78       if (!iks_strcmp (iks_name (node), "success"))
79         {
80           iks_send_header (udata->parser, "jabber-br.org");
81           udata->auth = AUTH_SASL;
82         }
83       printf ("%s\n", iks_string (iks_stack (node), node));
84     }
85   return IKS_OK;
86 }
87
88 int
89 main (int argc, char **argv)
90 {
91   struct udata udata;
92   udata.auth = 0;
93   udata.parser = iks_stream_new ("jabber:client", &udata, hook);
94   iks_connect_tcp (udata.parser, "jabber-br.org", 5222);
95   while (1)
96     iks_recv (udata.parser, -1);
97   return 0;
98 }