Support for SASL PLAIN authentication
[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
31 int
32 myhook (void *data, int type, iks *stanza)
33 {
34   if (!iks_strcmp (iks_name (stanza), "stream:features"))
35     {
36       hc_xmpp_features (data, stanza);
37       if (hc_xmpp_is_sasl_supported (data))
38         {
39           hc_xmpp_sasl_authenticate (data);
40         }
41     }
42   else if (!iks_strcmp (iks_find_attrib (stanza, "xmlns"), HC_XMPP_NS_SASL))
43     {
44       hc_xmpp_sasl_iterate (data, stanza);
45       if (hc_xmpp_status (data) == HC_XMPP_AUTHENTICATED)
46         fprintf (stdout, "Authenticated\n");
47     }
48   else
49     {
50       fprintf (stderr, "Other: %s\n", iks_string (iks_stack (stanza), stanza));
51     }
52   return IKS_OK;
53 }
54
55 void
56 write_stream (hc_xmpp_t *xmpp)
57 {
58   char *buffer = NULL;
59   asprintf (&buffer, "<stream:stream xmlns='jabber:client' "
60                      "xmlns:stream='http://etherx.jabber.org/streams' "
61                      "version='1.0' to='%s'>", hc_xmpp_server (xmpp));
62   hc_xmpp_send_buffer (xmpp, buffer, 0);
63   free (buffer);
64 }
65
66 void
67 loop (hc_xmpp_t *xmpp)
68 {
69   while (1)
70     hc_xmpp_read_and_parse (xmpp);
71 }
72
73 int
74 main (int argc, char **argv)
75 {
76   char *server = "jabber-br.org";
77   char *user = "pubsub";
78   char *password = "pubsub";
79   hc_xmpp_t *xmpp;
80   dns_init (NULL, 1);
81   xmpp = hc_xmpp_new (myhook, server, user, password);
82   write_stream (xmpp);
83   loop (xmpp);
84   return 0;
85 }