Created extensible hooks
[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->nshooks = g_hash_table_new (g_str_hash, g_str_equal);
42   g_hash_table_insert (xmpp->nshooks, HC_XMPP_NS_BIND, hc_xmpp_hook_bind);
43   g_hash_table_insert (xmpp->nshooks, HC_XMPP_NS_SESSION, hc_xmpp_hook_session);
44   g_hash_table_insert (xmpp->nshooks, HC_XMPP_NS_DISCO_INFO,
45                        hc_xmpp_hook_disco);
46   return xmpp;
47 }
48
49 int
50 hc_xmpp_is_tls_supported (hc_xmpp_t *xmpp)
51 {
52   return xmpp->tls & SUPPORTED;
53 }
54
55 int
56 hc_xmpp_is_tls_required (hc_xmpp_t *xmpp)
57 {
58   return xmpp->tls & REQUIRED;
59 }
60
61 int
62 hc_xmpp_is_tls_optional (hc_xmpp_t *xmpp)
63 {
64   return xmpp->tls & OPTIONAL;
65 }
66
67 int
68 hc_xmpp_is_tls_enabled (hc_xmpp_t *xmpp)
69 {
70   return xmpp->tls & ENABLED;
71 }
72
73 int
74 hc_xmpp_is_sasl_supported (hc_xmpp_t *xmpp)
75 {
76   return xmpp->sasl & SUPPORTED;
77 }
78
79 int
80 hc_xmpp_is_sasl_required (hc_xmpp_t *xmpp)
81 {
82   return xmpp->sasl & REQUIRED;
83 }
84
85 int
86 hc_xmpp_is_sasl_optional (hc_xmpp_t *xmpp)
87 {
88   return xmpp->sasl & OPTIONAL;
89 }
90
91 int
92 hc_xmpp_is_sasl_enabled (hc_xmpp_t *xmpp)
93 {
94   return xmpp->sasl & ENABLED;
95 }
96
97 int
98 hc_xmpp_is_bind_supported (hc_xmpp_t *xmpp)
99 {
100   return xmpp->bind & SUPPORTED;
101 }
102
103 int
104 hc_xmpp_is_session_supported (hc_xmpp_t *xmpp)
105 {
106   return xmpp->session & SUPPORTED;
107 }
108
109 int
110 hc_xmpp_is_session_required (hc_xmpp_t *xmpp)
111 {
112   return xmpp->session & REQUIRED;
113 }
114
115 char *
116 hc_xmpp_server (hc_xmpp_t *xmpp)
117 {
118   return xmpp->server;
119 }
120
121 int
122 hc_xmpp_fd (hc_xmpp_t *xmpp)
123 {
124   return xmpp->fd;
125 }
126
127 void
128 hc_xmpp_send_buffer (hc_xmpp_t *xmpp, char *buffer, size_t len)
129 {
130   if (len == 0)
131     len = strlen (buffer);
132   write (xmpp->fd, buffer, len);
133 }
134
135 void
136 hc_xmpp_send_iks (hc_xmpp_t *xmpp, iks *x)
137 {
138   char *str;
139   str = iks_string (iks_stack (x), x);
140   write (xmpp->fd, str, strlen (str));
141 }
142
143 void
144 hc_xmpp_read_and_parse (hc_xmpp_t *xmpp)
145 {
146   char buffer[4096];
147   int r;
148   r = read (xmpp->fd, buffer, sizeof (buffer));
149   iks_parse (xmpp->parser, buffer, r, 0);
150 }
151
152 int
153 hc_xmpp_status (hc_xmpp_t *xmpp)
154 {
155   return xmpp->status;
156 }