Created XMPP context and better features parser
[cascardo/chat.git] / tictactoe.c
1 /*
2  *  Copyright (C) 2008  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 #define _GNU_SOURCE
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <udns.h>
24 #include <unistd.h>
25 #include <string.h>
26 #include "tcp_connect.h"
27 #include "iksemel_extra.h"
28 #include "xmpp.h"
29
30 int
31 myhook (void *data, int type, iks *stanza)
32 {
33   if (!iks_strcmp (iks_name (stanza), "stream:features"))
34     {
35       hc_xmpp_features (data, stanza);
36       if (hc_xmpp_is_tls_supported (data))
37         fprintf (stderr, "TLS is supported\n");
38       if (hc_xmpp_is_sasl_supported (data))
39         fprintf (stderr, "SASL is supported\n");
40     }
41   else
42     {
43       fprintf (stderr, "Other: %s\n", iks_string (iks_stack (stanza), stanza));
44     }
45   return IKS_OK;
46 }
47
48 void
49 write_stream (int fd, char *server)
50 {
51   char *buffer = NULL;
52   asprintf (&buffer, "<stream:stream xmlns='jabber:client' "
53                      "xmlns:stream='http://etherx.jabber.org/streams' "
54                      "version='1.0' to='%s'>", server);
55   write (fd, buffer, strlen (buffer));
56   free (buffer);
57 }
58
59 void
60 loop (iksparser *parser, int fd)
61 {
62   char buffer[4096];
63   int r;
64   while ((r = read (fd, buffer, sizeof (buffer))) > 0)
65     iks_parse (parser, buffer, r, 0);
66 }
67
68 int
69 main (int argc, char **argv)
70 {
71   char *server = "jabber-br.org";
72   int fd;
73   iksparser *parser;
74   hc_xmpp_t *xmpp;
75   dns_init (NULL, 1);
76   xmpp = hc_xmpp_new ();
77   fd = hc_tcp_connect (server, "xmpp-client");
78   parser = iks_extra_stream_new (xmpp, myhook);
79   write_stream (fd, server);
80   loop (parser, fd);
81   return 0;
82 }