19b4891530407b52c70fd8e79cf1a8b6bf630b3e
[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
29 void
30 plain (void *data)
31 {
32   iks *mplain;
33   char *str;
34   mplain = iks_new ("auth");
35   iks_insert_attrib (mplain, "xmlns", "urn:ietf:params:xml:ns:xmpp-sasl");
36   iks_insert_attrib (mplain, "mechanism", "PLAIN");
37   str = iks_string (iks_stack (mplain), mplain);
38   write ((int) data, str, strlen (str));
39   iks_delete (mplain);
40 }
41
42 void
43 sasl (void *data, iks *s)
44 {
45   iks *m;
46   for (m = iks_child (s); m != NULL; m = iks_next (m))
47     {
48       if (!iks_strcmp (iks_name (m), "mechanism") &&
49           !iks_strcmp (iks_cdata (iks_child (m)), "PLAIN"))
50         plain (data);
51     }
52 }
53
54 void
55 features (void *data, iks *s)
56 {
57   iks *f;
58   for (f = iks_child (s); f != NULL; f = iks_next (f))
59     {
60       if (!iks_strcmp (iks_name (f), "mechanisms") &&
61           !iks_strcmp (iks_find_attrib (f, "xmlns"),
62                        "urn:ietf:params:xml:ns:xmpp-sasl"))
63         sasl (data, f);
64     }
65 }
66
67 int
68 myhook (void *data, int type, iks *stanza)
69 {
70   if (!iks_strcmp (iks_name (stanza), "stream:features"))
71     {
72       features (data, stanza);
73     }
74   else
75     {
76       fprintf (stderr, "Other: %s\n", iks_string (iks_stack (stanza), stanza));
77     }
78   return IKS_OK;
79 }
80
81 void
82 write_stream (int fd, char *server)
83 {
84   char *buffer = NULL;
85   asprintf (&buffer, "<stream:stream xmlns='jabber:client' "
86                      "xmlns:stream='http://etherx.jabber.org/streams' "
87                      "version='1.0' to='%s'>", server);
88   write (fd, buffer, strlen (buffer));
89   free (buffer);
90 }
91
92 void
93 loop (iksparser *parser, int fd)
94 {
95   char buffer[4096];
96   int r;
97   while ((r = read (fd, buffer, sizeof (buffer))) > 0)
98     iks_parse (parser, buffer, r, 0);
99 }
100
101 int
102 main (int argc, char **argv)
103 {
104   char *server = "jabber-br.org";
105   int fd;
106   iksparser *parser;
107   dns_init (NULL, 1);
108   fd = hc_tcp_connect (server, "xmpp-client");
109   parser = iks_extra_stream_new ((void *) fd, myhook);
110   write_stream (fd, server);
111   loop (parser, fd);
112   return 0;
113 }