Fix bug with uninitialized variable.
[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->sentmsghook = NULL;
43   xmpp->nshooks = g_hash_table_new (g_str_hash, g_str_equal);
44   g_hash_table_insert (xmpp->nshooks, HC_XMPP_NS_BIND, hc_xmpp_hook_bind);
45   g_hash_table_insert (xmpp->nshooks, HC_XMPP_NS_SESSION, hc_xmpp_hook_session);
46   g_hash_table_insert (xmpp->nshooks, HC_XMPP_NS_DISCO_INFO,
47                        hc_xmpp_hook_disco);
48   return xmpp;
49 }
50
51 int
52 hc_xmpp_is_tls_supported (hc_xmpp_t *xmpp)
53 {
54   return xmpp->tls & SUPPORTED;
55 }
56
57 int
58 hc_xmpp_is_tls_required (hc_xmpp_t *xmpp)
59 {
60   return xmpp->tls & REQUIRED;
61 }
62
63 int
64 hc_xmpp_is_tls_optional (hc_xmpp_t *xmpp)
65 {
66   return xmpp->tls & OPTIONAL;
67 }
68
69 int
70 hc_xmpp_is_tls_enabled (hc_xmpp_t *xmpp)
71 {
72   return xmpp->tls & ENABLED;
73 }
74
75 int
76 hc_xmpp_is_sasl_supported (hc_xmpp_t *xmpp)
77 {
78   return xmpp->sasl & SUPPORTED;
79 }
80
81 int
82 hc_xmpp_is_sasl_required (hc_xmpp_t *xmpp)
83 {
84   return xmpp->sasl & REQUIRED;
85 }
86
87 int
88 hc_xmpp_is_sasl_optional (hc_xmpp_t *xmpp)
89 {
90   return xmpp->sasl & OPTIONAL;
91 }
92
93 int
94 hc_xmpp_is_sasl_enabled (hc_xmpp_t *xmpp)
95 {
96   return xmpp->sasl & ENABLED;
97 }
98
99 int
100 hc_xmpp_is_bind_supported (hc_xmpp_t *xmpp)
101 {
102   return xmpp->bind & SUPPORTED;
103 }
104
105 int
106 hc_xmpp_is_session_supported (hc_xmpp_t *xmpp)
107 {
108   return xmpp->session & SUPPORTED;
109 }
110
111 int
112 hc_xmpp_is_session_required (hc_xmpp_t *xmpp)
113 {
114   return xmpp->session & REQUIRED;
115 }
116
117 char *
118 hc_xmpp_server (hc_xmpp_t *xmpp)
119 {
120   return xmpp->server;
121 }
122
123 int
124 hc_xmpp_fd (hc_xmpp_t *xmpp)
125 {
126   return xmpp->fd;
127 }
128
129 void
130 hc_xmpp_send_buffer (hc_xmpp_t *xmpp, char *buffer, size_t len)
131 {
132   if (len == 0)
133     len = strlen (buffer);
134   write (xmpp->fd, buffer, len);
135 }
136
137 void
138 hc_xmpp_send_iks (hc_xmpp_t *xmpp, iks *x)
139 {
140   char *str;
141   str = iks_string (iks_stack (x), x);
142   write (xmpp->fd, str, strlen (str));
143 }
144
145 void
146 hc_xmpp_read_and_parse (hc_xmpp_t *xmpp)
147 {
148   char buffer[4096];
149   int r;
150   r = read (xmpp->fd, buffer, sizeof (buffer));
151   iks_parse (xmpp->parser, buffer, r, 0);
152 }
153
154 int
155 hc_xmpp_status (hc_xmpp_t *xmpp)
156 {
157   return xmpp->status;
158 }
159
160 void
161 hc_xmpp_register_ns_hook (hc_xmpp_t *xmpp, char *ns, hc_xmpp_hook_t hook)
162 {
163   g_hash_table_insert (xmpp->nshooks, ns, hook);
164 }
165
166 void
167 hc_xmpp_set_msg_hook (hc_xmpp_t *xmpp, hc_xmpp_hook_t hook)
168 {
169   xmpp->msghook = hook;
170 }
171
172 void
173 hc_xmpp_set_sent_msg_hook (hc_xmpp_t *xmpp, hc_xmpp_hook_t hook)
174 {
175   xmpp->sentmsghook = hook;
176 }
177
178 void
179 hc_xmpp_set_pres_hook (hc_xmpp_t *xmpp, hc_xmpp_hook_t hook)
180 {
181   xmpp->preshook = hook;
182 }
183
184 void
185 hc_xmpp_set_sent_pres_hook (hc_xmpp_t *xmpp, hc_xmpp_hook_t hook)
186 {
187   xmpp->sentpreshook = hook;
188 }
189
190 void
191 hc_xmpp_recv_message (hc_xmpp_t *xmpp, iks *stanza)
192 {
193   if (xmpp->msghook)
194     xmpp->msghook (xmpp, stanza);
195 }
196
197 void
198 hc_xmpp_sent_message (hc_xmpp_t *xmpp, iks *stanza)
199 {
200   if (xmpp->sentmsghook)
201     xmpp->sentmsghook (xmpp, stanza);
202 }
203
204 void
205 hc_xmpp_recv_presence (hc_xmpp_t *xmpp, iks *stanza)
206 {
207   if (xmpp->preshook)
208     xmpp->preshook (xmpp, stanza);
209 }
210
211 void
212 hc_xmpp_sent_presence (hc_xmpp_t *xmpp, iks *stanza)
213 {
214   if (xmpp->sentpreshook)
215     xmpp->sentpreshook (xmpp, stanza);
216 }