2 * Copyright (C) 2008 Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
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.
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.
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.
24 #include "xmpp_internal.h"
25 #include "iksemel_extra.h"
26 #include "tcp_connect.h"
29 hc_xmpp_new (iksStreamHook *hook, char *server, char *user, char *pass)
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");
40 xmpp->status = HC_XMPP_NONE;
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,
52 hc_xmpp_is_tls_supported (hc_xmpp_t *xmpp)
54 return xmpp->tls & SUPPORTED;
58 hc_xmpp_is_tls_required (hc_xmpp_t *xmpp)
60 return xmpp->tls & REQUIRED;
64 hc_xmpp_is_tls_optional (hc_xmpp_t *xmpp)
66 return xmpp->tls & OPTIONAL;
70 hc_xmpp_is_tls_enabled (hc_xmpp_t *xmpp)
72 return xmpp->tls & ENABLED;
76 hc_xmpp_is_sasl_supported (hc_xmpp_t *xmpp)
78 return xmpp->sasl & SUPPORTED;
82 hc_xmpp_is_sasl_required (hc_xmpp_t *xmpp)
84 return xmpp->sasl & REQUIRED;
88 hc_xmpp_is_sasl_optional (hc_xmpp_t *xmpp)
90 return xmpp->sasl & OPTIONAL;
94 hc_xmpp_is_sasl_enabled (hc_xmpp_t *xmpp)
96 return xmpp->sasl & ENABLED;
100 hc_xmpp_is_bind_supported (hc_xmpp_t *xmpp)
102 return xmpp->bind & SUPPORTED;
106 hc_xmpp_is_session_supported (hc_xmpp_t *xmpp)
108 return xmpp->session & SUPPORTED;
112 hc_xmpp_is_session_required (hc_xmpp_t *xmpp)
114 return xmpp->session & REQUIRED;
118 hc_xmpp_server (hc_xmpp_t *xmpp)
124 hc_xmpp_fd (hc_xmpp_t *xmpp)
130 hc_xmpp_send_buffer (hc_xmpp_t *xmpp, char *buffer, size_t len)
133 len = strlen (buffer);
134 write (xmpp->fd, buffer, len);
138 hc_xmpp_send_iks (hc_xmpp_t *xmpp, iks *x)
141 str = iks_string (iks_stack (x), x);
142 write (xmpp->fd, str, strlen (str));
146 hc_xmpp_read_and_parse (hc_xmpp_t *xmpp)
150 r = read (xmpp->fd, buffer, sizeof (buffer));
151 iks_parse (xmpp->parser, buffer, r, 0);
155 hc_xmpp_status (hc_xmpp_t *xmpp)
161 hc_xmpp_register_ns_hook (hc_xmpp_t *xmpp, char *ns, hc_xmpp_hook_t hook)
163 g_hash_table_insert (xmpp->nshooks, ns, hook);
167 hc_xmpp_set_msg_hook (hc_xmpp_t *xmpp, hc_xmpp_hook_t hook)
169 xmpp->msghook = hook;
173 hc_xmpp_set_sent_msg_hook (hc_xmpp_t *xmpp, hc_xmpp_hook_t hook)
175 xmpp->sentmsghook = hook;
179 hc_xmpp_set_pres_hook (hc_xmpp_t *xmpp, hc_xmpp_hook_t hook)
181 xmpp->preshook = hook;
185 hc_xmpp_set_sent_pres_hook (hc_xmpp_t *xmpp, hc_xmpp_hook_t hook)
187 xmpp->sentpreshook = hook;
191 hc_xmpp_recv_message (hc_xmpp_t *xmpp, iks *stanza)
194 xmpp->msghook (xmpp, stanza);
198 hc_xmpp_sent_message (hc_xmpp_t *xmpp, iks *stanza)
200 if (xmpp->sentmsghook)
201 xmpp->sentmsghook (xmpp, stanza);
205 hc_xmpp_recv_presence (hc_xmpp_t *xmpp, iks *stanza)
208 xmpp->preshook (xmpp, stanza);
212 hc_xmpp_sent_presence (hc_xmpp_t *xmpp, iks *stanza)
214 if (xmpp->sentpreshook)
215 xmpp->sentpreshook (xmpp, stanza);