b57c1b9e945f9464dccbdf5a536a00041ed01ac2
[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 #include "sasl.h"
30 #include "bind.h"
31
32 static void write_stream (hc_xmpp_t *);
33
34 static void
35 send_message_test (hc_xmpp_t *xmpp)
36 {
37   iks *msg;
38   msg = iks_new ("message");
39   iks_insert_attrib (msg, "to", "metal@jabber-br.org");
40   iks_insert_cdata (iks_insert (msg, "body"), "pubsub", 0);
41   hc_xmpp_send_iks (xmpp, msg);
42   iks_delete (msg);
43 }
44
45 static int
46 myhook (void *data, int type, iks *stanza)
47 {
48   if (!iks_strcmp (iks_name (stanza), "iq"))
49     {
50       char *ns = iks_find_attrib (iks_child (stanza), "xmlns");
51       if (!iks_strcmp (ns, HC_XMPP_NS_BIND))
52         {
53           hc_xmpp_bind_result (data, stanza);
54           if (hc_xmpp_status (data) == HC_XMPP_BOUND &&
55               hc_xmpp_is_session_supported (data))
56             hc_xmpp_session (data);
57         }
58       else if (!iks_strcmp (ns, HC_XMPP_NS_SESSION))
59         {
60           hc_xmpp_session_result (data, stanza);
61           if (hc_xmpp_status (data) == HC_XMPP_SESSION)
62             send_message_test (data);
63         }
64     }
65   else if (!iks_strcmp (iks_name (stanza), "stream:features"))
66     {
67       hc_xmpp_features (data, stanza);
68       if (hc_xmpp_is_sasl_supported (data) & !hc_xmpp_is_sasl_enabled (data))
69         {
70           hc_xmpp_sasl_authenticate (data);
71         }
72       if (hc_xmpp_is_bind_supported (data))
73         {
74           hc_xmpp_bind (data);
75         }
76     }
77   else if (!iks_strcmp (iks_find_attrib (stanza, "xmlns"), HC_XMPP_NS_SASL))
78     {
79       hc_xmpp_sasl_iterate (data, stanza);
80       if (hc_xmpp_status (data) == HC_XMPP_AUTHENTICATED)
81         {
82           write_stream (data);
83           fprintf (stdout, "Authenticated\n");
84         }
85     }
86   else
87     {
88       fprintf (stderr, "Other: %s\n", iks_string (iks_stack (stanza), stanza));
89     }
90   return IKS_OK;
91 }
92
93 static void
94 write_stream (hc_xmpp_t *xmpp)
95 {
96   char *buffer = NULL;
97   asprintf (&buffer, "<stream:stream xmlns='jabber:client' "
98                      "xmlns:stream='http://etherx.jabber.org/streams' "
99                      "version='1.0' to='%s'>", hc_xmpp_server (xmpp));
100   hc_xmpp_send_buffer (xmpp, buffer, 0);
101   free (buffer);
102 }
103
104 static void
105 loop (hc_xmpp_t *xmpp)
106 {
107   while (1)
108     hc_xmpp_read_and_parse (xmpp);
109 }
110
111 int
112 main (int argc, char **argv)
113 {
114   char *server = "jabber-br.org";
115   char *user = "pubsub";
116   char *password = "pubsub";
117   hc_xmpp_t *xmpp;
118   dns_init (NULL, 1);
119   xmpp = hc_xmpp_new (myhook, server, user, password);
120   write_stream (xmpp);
121   loop (xmpp);
122   return 0;
123 }