Added GTK+ interface to connect to server
[cascardo/chat.git] / xmpp.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 #include <stdlib.h>
21 #include <string.h>
22 #include <gsasl.h>
23 #include "xmpp.h"
24 #include "xmpp_internal.h"
25 #include "iksemel_extra.h"
26 #include "tcp_connect.h"
27
28 hc_xmpp_t *
29 hc_xmpp_new (iksStreamHook *hook, char *server, char *user, char *pass)
30 {
31   hc_xmpp_t *xmpp = malloc (sizeof (hc_xmpp_t));
32   xmpp->server = strdup (server);
33   xmpp->user = strdup (user);
34   xmpp->password = strdup (pass);
35   xmpp->parser = iks_extra_stream_new (xmpp, hook);
36   gsasl_init (&xmpp->sasl_ctx);
37   xmpp->fd = hc_tcp_connect (server, "xmpp-client");
38   xmpp->tls = NONE;
39   xmpp->sasl = NONE;
40   xmpp->status = HC_XMPP_NONE;
41   xmpp->msghook = NULL;
42   xmpp->nshooks = g_hash_table_new (g_str_hash, g_str_equal);
43   g_hash_table_insert (xmpp->nshooks, HC_XMPP_NS_BIND, hc_xmpp_hook_bind);
44   g_hash_table_insert (xmpp->nshooks, HC_XMPP_NS_SESSION, hc_xmpp_hook_session);
45   g_hash_table_insert (xmpp->nshooks, HC_XMPP_NS_DISCO_INFO,
46                        hc_xmpp_hook_disco);
47   return xmpp;
48 }
49
50 int
51 hc_xmpp_is_tls_supported (hc_xmpp_t *xmpp)
52 {
53   return xmpp->tls & SUPPORTED;
54 }
55
56 int
57 hc_xmpp_is_tls_required (hc_xmpp_t *xmpp)
58 {
59   return xmpp->tls & REQUIRED;
60 }
61
62 int
63 hc_xmpp_is_tls_optional (hc_xmpp_t *xmpp)
64 {
65   return xmpp->tls & OPTIONAL;
66 }
67
68 int
69 hc_xmpp_is_tls_enabled (hc_xmpp_t *xmpp)
70 {
71   return xmpp->tls & ENABLED;
72 }
73
74 int
75 hc_xmpp_is_sasl_supported (hc_xmpp_t *xmpp)
76 {
77   return xmpp->sasl & SUPPORTED;
78 }
79
80 int
81 hc_xmpp_is_sasl_required (hc_xmpp_t *xmpp)
82 {
83   return xmpp->sasl & REQUIRED;
84 }
85
86 int
87 hc_xmpp_is_sasl_optional (hc_xmpp_t *xmpp)
88 {
89   return xmpp->sasl & OPTIONAL;
90 }
91
92 int
93 hc_xmpp_is_sasl_enabled (hc_xmpp_t *xmpp)
94 {
95   return xmpp->sasl & ENABLED;
96 }
97
98 int
99 hc_xmpp_is_bind_supported (hc_xmpp_t *xmpp)
100 {
101   return xmpp->bind & SUPPORTED;
102 }
103
104 int
105 hc_xmpp_is_session_supported (hc_xmpp_t *xmpp)
106 {
107   return xmpp->session & SUPPORTED;
108 }
109
110 int
111 hc_xmpp_is_session_required (hc_xmpp_t *xmpp)
112 {
113   return xmpp->session & REQUIRED;
114 }
115
116 char *
117 hc_xmpp_server (hc_xmpp_t *xmpp)
118 {
119   return xmpp->server;
120 }
121
122 int
123 hc_xmpp_fd (hc_xmpp_t *xmpp)
124 {
125   return xmpp->fd;
126 }
127
128 void
129 hc_xmpp_send_buffer (hc_xmpp_t *xmpp, char *buffer, size_t len)
130 {
131   if (len == 0)
132     len = strlen (buffer);
133   write (xmpp->fd, buffer, len);
134 }
135
136 void
137 hc_xmpp_send_iks (hc_xmpp_t *xmpp, iks *x)
138 {
139   char *str;
140   str = iks_string (iks_stack (x), x);
141   write (xmpp->fd, str, strlen (str));
142 }
143
144 void
145 hc_xmpp_read_and_parse (hc_xmpp_t *xmpp)
146 {
147   char buffer[4096];
148   int r;
149   r = read (xmpp->fd, buffer, sizeof (buffer));
150   iks_parse (xmpp->parser, buffer, r, 0);
151 }
152
153 int
154 hc_xmpp_status (hc_xmpp_t *xmpp)
155 {
156   return xmpp->status;
157 }
158
159 void
160 hc_xmpp_register_ns_hook (hc_xmpp_t *xmpp, char *ns, hc_xmpp_hook_t hook)
161 {
162   g_hash_table_insert (xmpp->nshooks, ns, hook);
163 }
164
165 void
166 hc_xmpp_set_msg_hook (hc_xmpp_t *xmpp, hc_xmpp_hook_t hook)
167 {
168   xmpp->msghook = hook;
169 }
170
171 void
172 hc_xmpp_recv_message (hc_xmpp_t *xmpp, iks *stanza)
173 {
174   if (xmpp->msghook)
175     xmpp->msghook (xmpp, stanza);
176 }