author | hallski <hallski> |
Mon, 23 Jun 2003 23:21:40 +0000 | |
changeset 9 | 2e14e3ab411b |
parent 7 | d8eae83450ac |
child 12 | fc5e71b1369c |
permissions | -rw-r--r-- |
1 | 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
2 |
/* |
|
7
d8eae83450ac
2003-06-13 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
4
diff
changeset
|
3 |
* Copyright (C) 2003 Mikael Hallendal <micke@imendio.com> |
1 | 4 |
* Copyright (C) 2003 CodeFactory AB. |
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or |
|
7 |
* modify it under the terms of the GNU Lesser General Public License as |
|
8 |
* published by the Free Software Foundation; either version 2 of the |
|
9 |
* License, or (at your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, |
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* Lesser General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU Lesser General Public |
|
17 |
* License along with this program; if not, write to the |
|
18 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
19 |
* Boston, MA 02111-1307, USA. |
|
20 |
*/ |
|
21 |
||
22 |
#include <config.h> |
|
23 |
||
9
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
24 |
#ifndef __WIN32__ |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
25 |
#include <netdb.h> |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
26 |
#include <sys/socket.h> |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
27 |
#include <netinet/in.h> |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
28 |
#else |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
29 |
#include <winsock2.h> |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
30 |
#endif |
2e14e3ab411b
2003-06-24 Mikael Hallendal <micke@imendio.com>
hallski <hallski>
parents:
7
diff
changeset
|
31 |
|
1 | 32 |
#include <string.h> |
33 |
#include <unistd.h> |
|
34 |
#include <sys/types.h> |
|
35 |
||
36 |
#include "lm-debug.h" |
|
37 |
#include "lm-error.h" |
|
38 |
#include "lm-internals.h" |
|
39 |
#include "lm-parser.h" |
|
40 |
#include "lm-sha.h" |
|
41 |
#include "lm-connection.h" |
|
42 |
||
43 |
#define IN_BUFFER_SIZE 1024 |
|
44 |
||
45 |
typedef struct { |
|
46 |
LmHandlerPriority priority; |
|
47 |
LmMessageHandler *handler; |
|
48 |
} HandlerData; |
|
49 |
||
50 |
typedef struct { |
|
51 |
GSource source; |
|
52 |
||
53 |
LmConnection *connection; |
|
54 |
} LmIncomingSource; |
|
55 |
||
56 |
struct _LmConnection { |
|
57 |
/* Parameters */ |
|
58 |
gchar *server; |
|
59 |
guint port; |
|
60 |
gboolean use_ssl; |
|
61 |
||
62 |
gboolean is_open; |
|
63 |
gboolean is_authenticated; |
|
64 |
||
65 |
LmParser *parser; |
|
66 |
gchar *stream_id; |
|
67 |
||
68 |
GHashTable *id_handlers; |
|
69 |
GSList *handlers[LM_MESSAGE_TYPE_UNKNOWN]; |
|
70 |
||
71 |
/* Communication */ |
|
72 |
GIOChannel *io_channel; |
|
73 |
||
74 |
LmCallback *open_cb; |
|
75 |
LmCallback *close_cb; |
|
76 |
LmCallback *auth_cb; |
|
77 |
LmCallback *register_cb; |
|
78 |
||
79 |
LmCallback *disconnect_cb; |
|
80 |
||
81 |
GQueue *incoming_messages; |
|
82 |
GSource *incoming_source; |
|
83 |
||
84 |
gint ref_count; |
|
85 |
}; |
|
86 |
||
87 |
static void connection_free (LmConnection *connection); |
|
88 |
||
89 |
||
90 |
static void connection_handle_message (LmConnection *connection, |
|
91 |
LmMessage *message); |
|
92 |
||
93 |
static void connection_new_message_cb (LmParser *parser, |
|
94 |
LmMessage *message, |
|
95 |
LmConnection *connection); |
|
96 |
static gboolean connection_do_open (LmConnection *connection, |
|
97 |
GError **error); |
|
98 |
||
99 |
static void connection_do_close (LmConnection *connection); |
|
100 |
static gboolean connection_in_event (GIOChannel *source, |
|
101 |
GIOCondition condition, |
|
102 |
LmConnection *connection); |
|
103 |
static gboolean connection_error_event (GIOChannel *source, |
|
104 |
GIOCondition condition, |
|
105 |
LmConnection *connection); |
|
106 |
static gboolean connection_hup_event (GIOChannel *source, |
|
107 |
GIOCondition condition, |
|
108 |
LmConnection *connection); |
|
109 |
static gboolean connection_send (LmConnection *connection, |
|
110 |
const gchar *str, |
|
111 |
gint len, |
|
112 |
GError **error); |
|
113 |
static LmHandlerResult connection_auth_reply (LmMessageHandler *handler, |
|
114 |
LmConnection *connection, |
|
115 |
LmMessage *m, |
|
116 |
gpointer user_data); |
|
117 |
||
118 |
static void connection_stream_received (LmConnection *connection, |
|
119 |
LmMessage *m); |
|
120 |
||
121 |
static gint connection_handler_compare_func (HandlerData *a, |
|
122 |
HandlerData *b); |
|
123 |
static gboolean connection_incoming_prepare (GSource *source, |
|
124 |
gint *timeout); |
|
125 |
static gboolean connection_incoming_check (GSource *source); |
|
126 |
static gboolean connection_incoming_dispatch (GSource *source, |
|
127 |
GSourceFunc callback, |
|
128 |
gpointer user_data); |
|
129 |
static GSource * connection_create_source (LmConnection *connection); |
|
130 |
static void connection_signal_disconnect (LmConnection *connection, |
|
131 |
LmDisconnectReason reason); |
|
132 |
||
133 |
static GSourceFuncs incoming_funcs = { |
|
134 |
connection_incoming_prepare, |
|
135 |
connection_incoming_check, |
|
136 |
connection_incoming_dispatch, |
|
137 |
NULL |
|
138 |
}; |
|
139 |
||
140 |
static void |
|
141 |
connection_free (LmConnection *connection) |
|
142 |
{ |
|
143 |
g_free (connection->server); |
|
144 |
||
145 |
if (connection->io_channel) { |
|
146 |
g_io_channel_unref (connection->io_channel); |
|
147 |
} |
|
148 |
||
149 |
g_free (connection); |
|
150 |
} |
|
151 |
||
152 |
||
153 |
static void |
|
154 |
connection_handle_message (LmConnection *connection, LmMessage *m) |
|
155 |
{ |
|
156 |
LmMessageHandler *handler; |
|
157 |
GSList *l; |
|
158 |
const gchar *id; |
|
159 |
LmHandlerResult result = LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
160 |
||
161 |
if (lm_message_get_type (m) == LM_MESSAGE_TYPE_STREAM) { |
|
162 |
connection_stream_received (connection, m); |
|
163 |
return; |
|
164 |
} |
|
165 |
||
166 |
id = lm_message_node_get_attribute (m->node, "id"); |
|
167 |
||
168 |
if (id) { |
|
169 |
handler = g_hash_table_lookup (connection->id_handlers, id); |
|
170 |
if (handler) { |
|
171 |
result = _lm_message_handler_handle_message (handler, |
|
172 |
connection, |
|
173 |
m); |
|
174 |
g_hash_table_remove (connection->id_handlers, id); |
|
175 |
} |
|
176 |
} |
|
177 |
||
178 |
if (result == LM_HANDLER_RESULT_REMOVE_MESSAGE) { |
|
179 |
return; |
|
180 |
} |
|
181 |
||
182 |
for (l = connection->handlers[lm_message_get_type (m)]; |
|
183 |
l && result == LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
184 |
l = l->next) { |
|
185 |
HandlerData *hd = (HandlerData *) l->data; |
|
186 |
||
187 |
result = _lm_message_handler_handle_message (hd->handler, |
|
188 |
connection, |
|
189 |
m); |
|
190 |
} |
|
191 |
||
192 |
return; |
|
193 |
} |
|
194 |
||
195 |
static void |
|
196 |
connection_new_message_cb (LmParser *parser, |
|
197 |
LmMessage *m, |
|
198 |
LmConnection *connection) |
|
199 |
{ |
|
200 |
lm_message_ref (m); |
|
201 |
||
202 |
lm_verbose ("New message with type=\"%s\" from: %s\n", |
|
203 |
_lm_message_type_to_string (lm_message_get_type (m)), |
|
204 |
lm_message_node_get_attribute (m->node, "from")); |
|
205 |
||
206 |
g_queue_push_tail (connection->incoming_messages, m); |
|
207 |
} |
|
208 |
||
209 |
static gboolean |
|
210 |
connection_do_open (LmConnection *connection, GError **error) |
|
211 |
{ |
|
212 |
struct hostent *he; |
|
213 |
struct in_addr *haddr; |
|
214 |
struct sockaddr_in saddr; |
|
215 |
gint fd; |
|
216 |
||
217 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
218 |
||
219 |
he = gethostbyname(connection->server); |
|
220 |
if (he == NULL) { |
|
221 |
g_set_error (error, |
|
222 |
LM_ERROR, |
|
223 |
LM_ERROR_CONNECTION_OPEN, |
|
224 |
"gethostbyname() failed"); |
|
225 |
return FALSE; |
|
226 |
} |
|
227 |
||
228 |
haddr = ((struct in_addr *) (he->h_addr_list)[0]); |
|
229 |
||
230 |
fd = socket(AF_INET, SOCK_STREAM, 0); |
|
231 |
memset(&saddr, 0, sizeof(saddr)); |
|
232 |
memcpy(&saddr.sin_addr, haddr, sizeof(struct in_addr)); |
|
233 |
saddr.sin_family = AF_INET; |
|
234 |
saddr.sin_port = htons (connection->port); |
|
235 |
||
236 |
if (connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) { |
|
237 |
g_set_error (error, |
|
238 |
LM_ERROR, |
|
239 |
LM_ERROR_CONNECTION_OPEN, |
|
240 |
"connect() failed"); |
|
241 |
close (fd); |
|
242 |
return FALSE; |
|
243 |
} |
|
244 |
||
245 |
connection->io_channel = g_io_channel_unix_new (fd); |
|
246 |
g_io_channel_set_close_on_unref (connection->io_channel, TRUE); |
|
247 |
g_io_channel_set_encoding (connection->io_channel, NULL, NULL); |
|
248 |
||
249 |
g_io_channel_set_buffered (connection->io_channel, FALSE); |
|
250 |
g_io_channel_set_flags (connection->io_channel, |
|
251 |
G_IO_FLAG_NONBLOCK, NULL); |
|
252 |
g_io_add_watch (connection->io_channel, |
|
253 |
G_IO_IN, |
|
254 |
(GIOFunc) connection_in_event, |
|
255 |
connection); |
|
256 |
g_io_add_watch (connection->io_channel, |
|
257 |
G_IO_ERR, |
|
258 |
(GIOFunc) connection_error_event, |
|
259 |
connection); |
|
260 |
g_io_add_watch (connection->io_channel, |
|
261 |
G_IO_HUP, |
|
262 |
(GIOFunc) connection_hup_event, |
|
263 |
connection); |
|
264 |
||
265 |
connection->is_open = TRUE; |
|
266 |
||
267 |
if (!connection_send (connection, |
|
268 |
"<?xml version='1.0' encoding='UTF-8'?>", -1, |
|
269 |
error)) { |
|
270 |
return FALSE; |
|
271 |
} |
|
272 |
||
273 |
return TRUE; |
|
274 |
} |
|
275 |
||
276 |
static void |
|
277 |
connection_do_close (LmConnection *connection) |
|
278 |
{ |
|
279 |
if (connection->io_channel) { |
|
280 |
g_io_channel_unref (connection->io_channel); |
|
281 |
} |
|
282 |
||
283 |
connection->io_channel = NULL; |
|
284 |
connection->is_open = FALSE; |
|
285 |
} |
|
286 |
||
287 |
||
288 |
static gboolean |
|
289 |
connection_in_event (GIOChannel *source, |
|
290 |
GIOCondition condition, |
|
291 |
LmConnection *connection) |
|
292 |
{ |
|
293 |
gchar buf[IN_BUFFER_SIZE]; |
|
294 |
gsize bytes_read; |
|
295 |
||
296 |
if (!connection->io_channel) { |
|
297 |
return FALSE; |
|
298 |
} |
|
299 |
g_io_channel_read_chars (connection->io_channel, |
|
300 |
buf, IN_BUFFER_SIZE - 1, |
|
301 |
&bytes_read, |
|
302 |
NULL); |
|
303 |
buf[bytes_read] = '\0'; |
|
304 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "\nRECV:\n"); |
|
305 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
306 |
"-----------------------------------\n"); |
|
307 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "%s\n", buf); |
|
308 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
309 |
"-----------------------------------\n"); |
|
310 |
||
311 |
lm_parser_parse (connection->parser, buf); |
|
312 |
||
313 |
return TRUE; |
|
314 |
} |
|
315 |
||
316 |
static gboolean |
|
317 |
connection_error_event (GIOChannel *source, |
|
318 |
GIOCondition condition, |
|
319 |
LmConnection *connection) |
|
320 |
{ |
|
321 |
lm_verbose ("Error event: %d\n", condition); |
|
322 |
||
323 |
connection_do_close (connection); |
|
324 |
connection_signal_disconnect (connection, LM_DISCONNECT_REASON_ERROR); |
|
325 |
||
326 |
return TRUE; |
|
327 |
} |
|
328 |
||
329 |
static gboolean |
|
330 |
connection_hup_event (GIOChannel *source, |
|
331 |
GIOCondition condition, |
|
332 |
LmConnection *connection) |
|
333 |
{ |
|
334 |
lm_verbose ("HUP event\n"); |
|
335 |
||
336 |
connection_do_close (connection); |
|
337 |
connection_signal_disconnect (connection, LM_DISCONNECT_REASON_HUP); |
|
338 |
||
339 |
return TRUE; |
|
340 |
} |
|
341 |
||
342 |
static gboolean |
|
343 |
connection_send (LmConnection *connection, |
|
344 |
const gchar *str, |
|
345 |
gint len, |
|
346 |
GError **error) |
|
347 |
{ |
|
348 |
gsize bytes_written; |
|
349 |
||
350 |
if (!lm_connection_is_open (connection)) { |
|
351 |
g_set_error (error, |
|
352 |
LM_ERROR, |
|
353 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
354 |
"Connection is not open, call lm_connection_open() first"); |
|
355 |
return FALSE; |
|
356 |
} |
|
357 |
||
358 |
if (len == -1) { |
|
359 |
len = strlen (str); |
|
360 |
} |
|
361 |
||
362 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "\nSEND:\n"); |
|
363 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
364 |
"-----------------------------------\n"); |
|
365 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, "%s\n", str); |
|
366 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
367 |
"-----------------------------------\n"); |
|
368 |
||
369 |
g_io_channel_write_chars (connection->io_channel, str, len, |
|
370 |
&bytes_written, NULL); |
|
371 |
||
372 |
return TRUE; |
|
373 |
} |
|
374 |
||
375 |
static LmHandlerResult |
|
376 |
connection_auth_reply (LmMessageHandler *handler, |
|
377 |
LmConnection *connection, |
|
378 |
LmMessage *m, |
|
379 |
gpointer user_data) |
|
380 |
{ |
|
381 |
const gchar *type; |
|
382 |
gboolean result = TRUE; |
|
383 |
||
384 |
g_return_val_if_fail (connection != NULL, |
|
385 |
LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS); |
|
386 |
||
387 |
||
388 |
type = lm_message_node_get_attribute (m->node, "type"); |
|
389 |
if (strcmp (type, "result") == 0) { |
|
390 |
result = TRUE; |
|
391 |
} |
|
392 |
else if (strcmp (type, "error") == 0) { |
|
393 |
result = FALSE; |
|
394 |
} |
|
395 |
||
396 |
lm_verbose ("AUTH reply: %d\n", result); |
|
397 |
||
398 |
if (connection->auth_cb && connection->auth_cb->func) { |
|
399 |
LmCallback *cb = connection->auth_cb; |
|
400 |
||
401 |
(* ((LmResultFunction) cb->func)) (connection, |
|
402 |
result, cb->user_data); |
|
403 |
} |
|
404 |
||
405 |
_lm_utils_free_callback (connection->auth_cb); |
|
406 |
connection->auth_cb = NULL; |
|
407 |
||
408 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
409 |
} |
|
410 |
||
411 |
||
412 |
static void |
|
413 |
connection_stream_received (LmConnection *connection, LmMessage *m) |
|
414 |
{ |
|
415 |
gboolean result; |
|
416 |
||
417 |
g_return_if_fail (connection != NULL); |
|
418 |
g_return_if_fail (m != NULL); |
|
419 |
||
420 |
connection->stream_id = g_strdup (lm_message_node_get_attribute (m->node, |
|
421 |
"id"));; |
|
422 |
||
423 |
lm_verbose ("Stream received: %s\n", connection->stream_id); |
|
424 |
||
425 |
/* Check to see if the stream is correctly set up */ |
|
426 |
result = TRUE; |
|
427 |
||
428 |
if (connection->open_cb && connection->open_cb->func) { |
|
429 |
LmCallback *cb = connection->open_cb; |
|
430 |
||
431 |
(* ((LmResultFunction) cb->func)) (connection, result, |
|
432 |
cb->user_data); |
|
433 |
} |
|
434 |
||
435 |
_lm_utils_free_callback (connection->open_cb); |
|
436 |
connection->open_cb = NULL; |
|
437 |
} |
|
438 |
||
439 |
static gint |
|
440 |
connection_handler_compare_func (HandlerData *a, HandlerData *b) |
|
441 |
{ |
|
442 |
return b->priority - a->priority; |
|
443 |
} |
|
444 |
||
445 |
static gboolean |
|
446 |
connection_incoming_prepare (GSource *source, gint *timeout) |
|
447 |
{ |
|
448 |
LmConnection *connection; |
|
449 |
||
450 |
connection = ((LmIncomingSource *)source)->connection; |
|
451 |
||
452 |
return !g_queue_is_empty (connection->incoming_messages); |
|
453 |
} |
|
454 |
||
455 |
static gboolean |
|
456 |
connection_incoming_check (GSource *source) |
|
457 |
{ |
|
458 |
return FALSE; |
|
459 |
} |
|
460 |
||
461 |
static gboolean |
|
462 |
connection_incoming_dispatch (GSource *source, |
|
463 |
GSourceFunc callback, |
|
464 |
gpointer user_data) |
|
465 |
{ |
|
466 |
LmConnection *connection; |
|
467 |
LmMessage *m; |
|
468 |
||
469 |
connection = ((LmIncomingSource *) source)->connection; |
|
470 |
||
471 |
m = (LmMessage *) g_queue_pop_head (connection->incoming_messages); |
|
472 |
||
473 |
if (m) { |
|
474 |
connection_handle_message (connection, m); |
|
475 |
lm_message_unref (m); |
|
476 |
} |
|
477 |
||
478 |
return TRUE; |
|
479 |
} |
|
480 |
||
481 |
static GSource * |
|
482 |
connection_create_source (LmConnection *connection) |
|
483 |
{ |
|
484 |
GSource *source; |
|
485 |
||
486 |
source = g_source_new (&incoming_funcs, sizeof (LmIncomingSource)); |
|
487 |
((LmIncomingSource *) source)->connection = connection; |
|
488 |
||
489 |
return source; |
|
490 |
} |
|
491 |
||
492 |
static void |
|
493 |
connection_signal_disconnect (LmConnection *connection, |
|
494 |
LmDisconnectReason reason) |
|
495 |
{ |
|
496 |
if (connection->disconnect_cb && connection->disconnect_cb->func) { |
|
497 |
LmCallback *cb = connection->disconnect_cb; |
|
498 |
||
499 |
(* ((LmDisconnectFunction) cb->func)) (connection, |
|
500 |
reason, |
|
501 |
cb->user_data); |
|
502 |
} |
|
503 |
} |
|
504 |
||
505 |
/** |
|
506 |
* lm_connection_new: |
|
507 |
* @server: The hostname to the server for the connection. |
|
508 |
* |
|
509 |
* Creates a new closed connection. To open the connection call |
|
510 |
* lm_connection_open(). @server can be #NULL but must be set before calling lm_connection_open(). |
|
511 |
* |
|
512 |
* Return value: A newly created LmConnection, should be unreffed with lm_connection_unref(). |
|
513 |
**/ |
|
514 |
LmConnection * |
|
515 |
lm_connection_new (const gchar *server) |
|
516 |
{ |
|
517 |
LmConnection *connection; |
|
518 |
gint i; |
|
519 |
||
520 |
lm_debug_init (); |
|
521 |
||
522 |
connection = g_new0 (LmConnection, 1); |
|
523 |
||
524 |
if (server) { |
|
525 |
connection->server = g_strdup (server); |
|
526 |
} else { |
|
527 |
connection->server = NULL; |
|
528 |
} |
|
529 |
||
530 |
connection->port = LM_CONNECTION_DEFAULT_PORT; |
|
531 |
connection->use_ssl = FALSE; |
|
532 |
connection->disconnect_cb = NULL; |
|
533 |
connection->incoming_messages = g_queue_new (); |
|
534 |
connection->incoming_source = connection_create_source (connection); |
|
535 |
||
536 |
connection->id_handlers = g_hash_table_new_full (g_str_hash, |
|
537 |
g_str_equal, |
|
538 |
g_free, |
|
539 |
(GDestroyNotify) lm_message_handler_unref); |
|
540 |
connection->ref_count = 1; |
|
541 |
g_source_attach (connection->incoming_source, NULL); |
|
542 |
||
543 |
for (i = 0; i < LM_MESSAGE_TYPE_UNKNOWN; ++i) { |
|
544 |
connection->handlers[i] = NULL; |
|
545 |
} |
|
546 |
||
547 |
connection->parser = lm_parser_new |
|
548 |
((LmParserMessageFunction) connection_new_message_cb, |
|
549 |
connection, NULL); |
|
550 |
||
551 |
return connection; |
|
552 |
} |
|
553 |
||
554 |
/** |
|
555 |
* lm_connection_open: |
|
556 |
* @connection: #LmConnection to open |
|
557 |
* @function: Callback function that will be called when the connection is open. |
|
558 |
* @user_data: User data that will be passed to @function. |
|
559 |
* @notify: Function for freeing that user_data, can be NULL. |
|
560 |
* @error: location to store error, or %NULL |
|
561 |
* |
|
562 |
* An async call to open @connection. When the connection is open @function will be called. |
|
563 |
* |
|
564 |
* Return value: #TRUE if everything went fine, otherwise #FALSE. |
|
565 |
**/ |
|
566 |
gboolean |
|
567 |
lm_connection_open (LmConnection *connection, |
|
568 |
LmResultFunction function, |
|
569 |
gpointer user_data, |
|
570 |
GDestroyNotify notify, |
|
571 |
GError **error) |
|
572 |
{ |
|
573 |
LmMessage *m; |
|
574 |
gboolean result; |
|
575 |
||
576 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
577 |
||
578 |
if (lm_connection_is_open (connection)) { |
|
579 |
g_set_error (error, |
|
580 |
LM_ERROR, |
|
581 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
582 |
"Connection is already open, call lm_connection_close() first"); |
|
583 |
return FALSE; |
|
584 |
} |
|
585 |
||
586 |
if (!connection->server) { |
|
587 |
g_set_error (error, |
|
588 |
LM_ERROR, |
|
589 |
LM_ERROR_CONNECTION_OPEN, |
|
590 |
"You need to set the server hostname in the call to lm_connection_new()"); |
|
591 |
return FALSE; |
|
592 |
} |
|
593 |
||
594 |
connection->open_cb = _lm_utils_new_callback (function, user_data, notify); |
|
595 |
||
596 |
lm_verbose ("Connecting to: %s:%d\n", |
|
597 |
connection->server, connection->port); |
|
598 |
||
599 |
if (!connection_do_open (connection, error)) { |
|
600 |
return FALSE; |
|
601 |
} |
|
602 |
||
603 |
m = lm_message_new (connection->server, LM_MESSAGE_TYPE_STREAM); |
|
604 |
lm_message_node_set_attributes (m->node, |
|
605 |
"xmlns:stream", "http://etherx.jabber.org/streams", |
|
606 |
"xmlns", "jabber:client", |
|
607 |
NULL); |
|
608 |
||
609 |
lm_verbose ("Opening stream..."); |
|
610 |
||
611 |
result = lm_connection_send (connection, m, error); |
|
612 |
lm_message_unref (m); |
|
613 |
||
614 |
return result; |
|
615 |
} |
|
616 |
||
617 |
/** |
|
618 |
* lm_connection_open_and_block: |
|
619 |
* @connection: an #LmConnection |
|
620 |
* @error: location to store error, or %NULL |
|
621 |
* |
|
622 |
* Opens @connection and waits until the stream is setup. |
|
623 |
* |
|
624 |
* Return value: #TRUE if no errors where encountered during opening and stream setup successfully, #FALSE otherwise. |
|
625 |
**/ |
|
626 |
gboolean |
|
627 |
lm_connection_open_and_block (LmConnection *connection, GError **error) |
|
628 |
{ |
|
629 |
LmMessage *m; |
|
630 |
gboolean result; |
|
631 |
gboolean finished = FALSE; |
|
632 |
gboolean ret_val = FALSE; |
|
633 |
gint last_len = 0; |
|
634 |
||
635 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
636 |
||
637 |
if (lm_connection_is_open (connection)) { |
|
638 |
g_set_error (error, |
|
639 |
LM_ERROR, |
|
640 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
641 |
"Connection is already open, call lm_connection_close() first"); |
|
642 |
return FALSE; |
|
643 |
} |
|
644 |
if (!connection->server) { |
|
645 |
g_set_error (error, |
|
646 |
LM_ERROR, |
|
647 |
LM_ERROR_CONNECTION_OPEN, |
|
648 |
"You need to set the server hostname in the call to lm_connection_new()"); |
|
649 |
return FALSE; |
|
650 |
} |
|
651 |
||
652 |
lm_verbose ("(Block)Connecting to: %s:%d\n", |
|
653 |
connection->server, connection->port); |
|
654 |
||
655 |
if (!connection_do_open (connection, error)) { |
|
656 |
return FALSE; |
|
657 |
} |
|
658 |
||
659 |
m = lm_message_new (connection->server, LM_MESSAGE_TYPE_STREAM); |
|
660 |
lm_message_node_set_attributes (m->node, |
|
661 |
"xmlns:stream", "http://etherx.jabber.org/streams", |
|
662 |
"xmlns", "jabber:client", |
|
663 |
NULL); |
|
664 |
||
665 |
lm_verbose ("Sending stream: \n%s\n", |
|
666 |
lm_message_node_to_string (m->node)); |
|
667 |
||
668 |
result = lm_connection_send (connection, m, error); |
|
669 |
lm_message_unref (m); |
|
670 |
||
671 |
g_source_remove (g_source_get_id (connection->incoming_source)); |
|
672 |
g_source_unref (connection->incoming_source); |
|
673 |
||
674 |
while (!finished) { |
|
675 |
gint len; |
|
676 |
||
677 |
g_main_context_iteration (NULL, TRUE); |
|
678 |
||
679 |
len = connection->incoming_messages->length; |
|
680 |
||
681 |
if (len > last_len) { |
|
682 |
LmMessage *m; |
|
683 |
||
684 |
last_len = len; |
|
685 |
m = (LmMessage *) g_queue_peek_tail (connection->incoming_messages); |
|
686 |
if (lm_message_get_type (m) == LM_MESSAGE_TYPE_STREAM) { |
|
687 |
connection->stream_id = |
|
688 |
g_strdup (lm_message_node_get_attribute (m->node, "id")); |
|
689 |
ret_val = TRUE; |
|
690 |
finished = TRUE; |
|
691 |
g_queue_pop_tail (connection->incoming_messages); |
|
692 |
} |
|
693 |
} |
|
694 |
} |
|
695 |
||
696 |
connection->incoming_source = connection_create_source (connection); |
|
697 |
g_source_attach (connection->incoming_source, NULL); |
|
698 |
||
699 |
return ret_val; |
|
700 |
} |
|
701 |
||
702 |
/** |
|
703 |
* lm_connection_close: |
|
704 |
* @connection: #LmConnection to close |
|
705 |
* @error: location to store error, or %NULL |
|
706 |
* |
|
707 |
* A synchronos call to close the connection. When returning the connection is considered to be closed and can be opened again with lm_connection_open(). |
|
708 |
* |
|
709 |
* Return value: Returns #TRUE if no errors where detected, otherwise #FALSE. |
|
710 |
**/ |
|
711 |
gboolean |
|
712 |
lm_connection_close (LmConnection *connection, |
|
713 |
GError **error) |
|
714 |
{ |
|
715 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
716 |
||
717 |
if (!lm_connection_is_open (connection)) { |
|
718 |
g_set_error (error, |
|
719 |
LM_ERROR, |
|
720 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
721 |
"Connection is not open, call lm_connection_open() first"); |
|
722 |
return FALSE; |
|
723 |
} |
|
724 |
||
725 |
lm_verbose ("Disconnecting from: %s:%d\n", |
|
726 |
connection->server, |
|
727 |
connection->port); |
|
728 |
||
729 |
if (!connection_send (connection, "</stream:stream>", -1, error)) { |
|
730 |
return FALSE; |
|
731 |
} |
|
732 |
||
733 |
g_io_channel_flush (connection->io_channel, NULL); |
|
734 |
||
735 |
connection_do_close (connection); |
|
736 |
connection_signal_disconnect (connection, LM_DISCONNECT_REASON_OK); |
|
737 |
||
738 |
return TRUE; |
|
739 |
} |
|
740 |
||
741 |
/** |
|
742 |
* lm_connection_authenticate: |
|
743 |
* @connection: #LmConnection to authenticate. |
|
744 |
* @username: Username used to authenticate. |
|
745 |
* @password: Password corresponding to @username. |
|
746 |
* @resource: Resource used for this connection. |
|
747 |
* @function: Callback called when authentication is finished. |
|
748 |
* @user_data: Userdata passed to @function when called. |
|
749 |
* @notify: Destroy function to free the memory used by @user_data, can be NULL. |
|
750 |
* @error: location to store error, or %NULL |
|
751 |
* |
|
752 |
* Tries to authenticate a user against the server. The #LmResult in the result callback will tell if it succeeded or not. |
|
753 |
* |
|
754 |
* Return value: #TRUE if no errors where detected while sending the authentication message, #FALSE otherwise. |
|
755 |
**/ |
|
756 |
gboolean |
|
757 |
lm_connection_authenticate (LmConnection *connection, |
|
758 |
const gchar *username, |
|
759 |
const gchar *password, |
|
760 |
const gchar *resource, |
|
761 |
LmResultFunction function, |
|
762 |
gpointer user_data, |
|
763 |
GDestroyNotify notify, |
|
764 |
GError **error) |
|
765 |
{ |
|
766 |
LmMessage *m; |
|
767 |
LmMessageNode *q_node; |
|
768 |
LmMessageHandler *handler; |
|
769 |
gboolean result; |
|
770 |
||
771 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
772 |
g_return_val_if_fail (username != NULL, FALSE); |
|
773 |
g_return_val_if_fail (password != NULL, FALSE); |
|
774 |
g_return_val_if_fail (resource != NULL, FALSE); |
|
775 |
||
776 |
if (!lm_connection_is_open (connection)) { |
|
777 |
g_set_error (error, |
|
778 |
LM_ERROR, |
|
779 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
780 |
"Connection is not open, call lm_connection_open() first"); |
|
781 |
return FALSE; |
|
782 |
} |
|
783 |
||
784 |
connection->auth_cb = _lm_utils_new_callback (function, |
|
785 |
user_data, |
|
786 |
notify); |
|
787 |
||
788 |
m = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, |
|
789 |
LM_MESSAGE_SUB_TYPE_SET); |
|
790 |
q_node = lm_message_node_add_child (m->node, "query", NULL); |
|
791 |
lm_message_node_set_attributes (q_node, |
|
792 |
"xmlns", "jabber:iq:auth", |
|
793 |
NULL); |
|
794 |
lm_message_node_add_child (q_node, "username", username); |
|
795 |
||
796 |
/* Check what the server can handle */ |
|
797 |
if (TRUE) { |
|
798 |
gchar *str; |
|
799 |
const gchar *digest; |
|
800 |
str = g_strconcat (connection->stream_id, password, NULL); |
|
801 |
digest = lm_sha_hash (str); |
|
802 |
g_free (str); |
|
803 |
lm_message_node_add_child (q_node, "digest", digest); |
|
804 |
} else { |
|
805 |
lm_message_node_add_child (q_node, "password", password); |
|
806 |
} |
|
807 |
||
808 |
lm_message_node_add_child (q_node, "resource", resource); |
|
809 |
handler = lm_message_handler_new (connection_auth_reply, NULL, NULL); |
|
810 |
||
811 |
result = lm_connection_send_with_reply (connection, m, handler, error); |
|
812 |
||
813 |
lm_message_handler_unref (handler); |
|
814 |
lm_message_unref (m); |
|
815 |
||
816 |
return result; |
|
817 |
} |
|
818 |
||
819 |
/** |
|
820 |
* lm_connection_authenticate_and_block: |
|
821 |
* @connection: an #LmConnection |
|
822 |
* @username: Username used to authenticate. |
|
823 |
* @password: Password corresponding to @username. |
|
824 |
* @resource: Resource used for this connection. |
|
825 |
* @error: location to store error, or %NULL |
|
826 |
* |
|
827 |
* Tries to authenticate a user against the server. This function blocks until a reply to the authentication attempt is returned and returns whether it was successful or not. |
|
828 |
* |
|
829 |
* Return value: #TRUE if no errors where detected and authentication was successful. #FALSE otherwise. |
|
830 |
**/ |
|
831 |
gboolean |
|
832 |
lm_connection_authenticate_and_block (LmConnection *connection, |
|
833 |
const gchar *username, |
|
834 |
const gchar *password, |
|
835 |
const gchar *resource, |
|
836 |
GError **error) |
|
837 |
{ |
|
838 |
LmMessage *m; |
|
839 |
LmMessageNode *q_node; |
|
840 |
LmMessage *result; |
|
841 |
||
842 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
843 |
g_return_val_if_fail (username != NULL, FALSE); |
|
844 |
g_return_val_if_fail (password != NULL, FALSE); |
|
845 |
g_return_val_if_fail (resource != NULL, FALSE); |
|
846 |
||
847 |
if (!lm_connection_is_open (connection)) { |
|
848 |
g_set_error (error, |
|
849 |
LM_ERROR, |
|
850 |
LM_ERROR_CONNECTION_NOT_OPEN, |
|
851 |
"Connection is not open, call lm_connection_open() first"); |
|
852 |
return FALSE; |
|
853 |
} |
|
854 |
||
855 |
m = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_IQ, |
|
856 |
LM_MESSAGE_SUB_TYPE_SET); |
|
857 |
q_node = lm_message_node_add_child (m->node, "query", NULL); |
|
858 |
lm_message_node_set_attributes (q_node, |
|
859 |
"xmlns", "jabber:iq:auth", |
|
860 |
NULL); |
|
861 |
lm_message_node_add_child (q_node, "username", username); |
|
862 |
||
863 |
/* Check what the server can handle */ |
|
864 |
if (TRUE) { |
|
865 |
gchar *str; |
|
866 |
const gchar *digest; |
|
867 |
str = g_strconcat (connection->stream_id, password, NULL); |
|
868 |
digest = lm_sha_hash (str); |
|
869 |
g_free (str); |
|
870 |
lm_message_node_add_child (q_node, "digest", digest); |
|
871 |
} else { |
|
872 |
lm_message_node_add_child (q_node, "password", password); |
|
873 |
} |
|
874 |
||
875 |
lm_message_node_add_child (q_node, "resource", resource); |
|
876 |
||
877 |
result = lm_connection_send_with_reply_and_block (connection, m, error); |
|
878 |
lm_message_unref (m); |
|
879 |
||
880 |
if (!result) { |
|
881 |
return FALSE; |
|
882 |
} |
|
883 |
||
884 |
switch (lm_message_get_sub_type (result)) { |
|
885 |
case LM_MESSAGE_SUB_TYPE_RESULT: |
|
886 |
return TRUE; |
|
887 |
break; |
|
888 |
case LM_MESSAGE_SUB_TYPE_ERROR: |
|
889 |
return FALSE; |
|
890 |
break; |
|
891 |
default: |
|
892 |
g_assert_not_reached (); |
|
893 |
break; |
|
894 |
} |
|
895 |
||
896 |
return FALSE; |
|
897 |
} |
|
898 |
||
899 |
/** |
|
900 |
* lm_connection_is_open: |
|
901 |
* @connection: #LmConnection to check if it is open. |
|
902 |
* |
|
903 |
* Check if the @connection is currently open. |
|
904 |
* |
|
905 |
* Return value: #TRUE if connection is open and #FALSE if it is closed. |
|
906 |
**/ |
|
907 |
gboolean |
|
908 |
lm_connection_is_open (LmConnection *connection) |
|
909 |
{ |
|
910 |
return connection->is_open; |
|
911 |
} |
|
912 |
||
913 |
/** |
|
914 |
* lm_connection_is_authenticated: |
|
915 |
* @connection: #LmConnection to check if it is authenticated |
|
916 |
* |
|
917 |
* Check if @connection is authenticated. |
|
918 |
* |
|
919 |
* Return value: #TRUE if connection is authenticated, #FALSE otherwise. |
|
920 |
**/ |
|
921 |
gboolean |
|
922 |
lm_connection_is_authenticated (LmConnection *connection) |
|
923 |
{ |
|
924 |
return connection->is_authenticated; |
|
925 |
} |
|
926 |
||
927 |
/** |
|
928 |
* lm_connection_get_server: |
|
929 |
* @connection: an #LmConnection |
|
930 |
* |
|
931 |
* Fetches the server address that @connection is using. |
|
932 |
* |
|
933 |
* Return value: the server address |
|
934 |
**/ |
|
935 |
const gchar * |
|
936 |
lm_connection_get_server (LmConnection *connection) |
|
937 |
{ |
|
938 |
return connection->server; |
|
939 |
} |
|
940 |
||
941 |
/** |
|
942 |
* lm_connection_set_server: |
|
943 |
* @connection: an #LmConnection |
|
944 |
* @server: Address of the server |
|
945 |
* |
|
946 |
* Sets the server address to @connection. Notice that @connection can't be open while doing this. |
|
947 |
**/ |
|
948 |
void |
|
949 |
lm_connection_set_server (LmConnection *connection, const gchar *server) |
|
950 |
{ |
|
951 |
if (lm_connection_is_open (connection)) { |
|
952 |
g_warning ("Can't change server address while connected"); |
|
953 |
return; |
|
954 |
} |
|
955 |
||
956 |
if (connection->server) { |
|
957 |
g_free (connection->server); |
|
958 |
} |
|
959 |
||
960 |
connection->server = g_strdup (server); |
|
961 |
} |
|
962 |
||
963 |
/** |
|
964 |
* lm_connection_get_port: |
|
965 |
* @connection: an #LmConnection |
|
966 |
* |
|
967 |
* Fetches the port tnat @connection is using. |
|
968 |
* |
|
969 |
* Return value: |
|
970 |
**/ |
|
971 |
guint |
|
972 |
lm_connection_get_port (LmConnection *connection) |
|
973 |
{ |
|
974 |
return connection->port; |
|
975 |
} |
|
976 |
||
977 |
/** |
|
978 |
* lm_connection_set_port: |
|
979 |
* @connection: an #LmConnection |
|
980 |
* @port: server port |
|
981 |
* |
|
982 |
* Sets the server port of that @connection will be using. |
|
983 |
**/ |
|
984 |
void |
|
985 |
lm_connection_set_port (LmConnection *connection, guint port) |
|
986 |
{ |
|
987 |
if (lm_connection_is_open (connection)) { |
|
988 |
g_warning ("Can't change server port while connected"); |
|
989 |
return; |
|
990 |
} |
|
991 |
||
992 |
connection->port = port; |
|
993 |
} |
|
994 |
||
995 |
/** |
|
996 |
* lm_connection_get_use_ssl: |
|
997 |
* @connection: an #LmConnection |
|
998 |
* |
|
999 |
* Fetches if @connection is using SSL or not |
|
1000 |
* |
|
1001 |
* Return value: #TRUE if @connection is using SSL, #FALSE otherwise. |
|
1002 |
**/ |
|
1003 |
gboolean |
|
1004 |
lm_connection_get_use_ssl (LmConnection *connection) |
|
1005 |
{ |
|
1006 |
return connection->use_ssl; |
|
1007 |
} |
|
1008 |
||
1009 |
/** |
|
1010 |
* lm_connection_set_use_ssl: |
|
1011 |
* @connection: an #LmConnection |
|
1012 |
* @use_ssl: whether to use SSL or not. |
|
1013 |
* |
|
1014 |
* Sets whether @connection should use SSL for encryping traffic to/from the server. |
|
1015 |
**/ |
|
1016 |
void |
|
1017 |
lm_connection_set_use_ssl (LmConnection *connection, gboolean use_ssl) |
|
1018 |
{ |
|
1019 |
if (lm_connection_is_open (connection)) { |
|
1020 |
g_warning ("use_ssl can't be changed while connected"); |
|
1021 |
return; |
|
1022 |
} |
|
1023 |
||
1024 |
connection->use_ssl = use_ssl; |
|
1025 |
} |
|
1026 |
||
1027 |
/** |
|
1028 |
* lm_connection_send: |
|
1029 |
* @connection: #LmConnection to send connection over. |
|
1030 |
* @message: #LmMessage to send. |
|
1031 |
* @error: location to store error, or %NULL |
|
1032 |
* |
|
1033 |
* Asynchronous call to send a message. |
|
1034 |
* |
|
1035 |
* Return value: Returns #TRUE if no errors where detected while sending, #FALSE otherwise. |
|
1036 |
**/ |
|
1037 |
gboolean |
|
1038 |
lm_connection_send (LmConnection *connection, |
|
1039 |
LmMessage *message, |
|
1040 |
GError **error) |
|
1041 |
{ |
|
1042 |
gchar *xml_str; |
|
1043 |
gchar *ch; |
|
1044 |
gboolean result; |
|
1045 |
||
1046 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1047 |
g_return_val_if_fail (message != NULL, FALSE); |
|
1048 |
||
1049 |
xml_str = lm_message_node_to_string (message->node); |
|
1050 |
if ((ch = strstr (xml_str, "</stream:stream>"))) { |
|
1051 |
*ch = '\0'; |
|
1052 |
} |
|
1053 |
||
1054 |
result = connection_send (connection, xml_str, -1, error); |
|
1055 |
g_free (xml_str); |
|
1056 |
||
1057 |
return result; |
|
1058 |
} |
|
1059 |
||
1060 |
/** |
|
1061 |
* lm_connection_send_with_reply: |
|
1062 |
* @connection: #LmConnection used to send message. |
|
1063 |
* @message: #LmMessage to send. |
|
1064 |
* @handler: #LmMessageHandler that will be used when a reply to @message arrives |
|
1065 |
* @error: location to store error, or %NULL |
|
1066 |
* |
|
1067 |
* Send a #LmMessage which will result in a reply. |
|
1068 |
* |
|
1069 |
* Return value: |
|
1070 |
**/ |
|
1071 |
gboolean |
|
1072 |
lm_connection_send_with_reply (LmConnection *connection, |
|
1073 |
LmMessage *message, |
|
1074 |
LmMessageHandler *handler, |
|
1075 |
GError **error) |
|
1076 |
{ |
|
1077 |
gchar *id; |
|
1078 |
||
1079 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1080 |
g_return_val_if_fail (message != NULL, FALSE); |
|
1081 |
g_return_val_if_fail (handler != NULL, FALSE); |
|
1082 |
||
1083 |
if (lm_message_node_get_attribute (message->node, "id")) { |
|
1084 |
id = g_strdup (lm_message_node_get_attribute (message->node, |
|
1085 |
"id")); |
|
1086 |
} else { |
|
1087 |
id = _lm_utils_generate_id (); |
|
1088 |
lm_message_node_set_attributes (message->node, "id", id, NULL); |
|
1089 |
} |
|
1090 |
||
1091 |
g_hash_table_insert (connection->id_handlers, |
|
1092 |
id, lm_message_handler_ref (handler)); |
|
1093 |
||
1094 |
return lm_connection_send (connection, message, error); |
|
1095 |
} |
|
1096 |
||
1097 |
/** |
|
1098 |
* lm_connection_send_with_reply_and_block: |
|
1099 |
* @connection: an #LmConnection |
|
1100 |
* @message: an #LmMessage |
|
1101 |
* @error: Set if error was detected during sending. |
|
1102 |
* |
|
1103 |
* Send @message and wait for return. |
|
1104 |
* |
|
1105 |
* Return value: The reply |
|
1106 |
**/ |
|
1107 |
LmMessage * |
|
1108 |
lm_connection_send_with_reply_and_block (LmConnection *connection, |
|
1109 |
LmMessage *message, |
|
1110 |
GError **error) |
|
1111 |
{ |
|
1112 |
gchar *id; |
|
1113 |
LmMessage *reply = NULL; |
|
1114 |
gint last_len = 0; |
|
1115 |
||
1116 |
if (lm_message_node_get_attribute (message->node, "id")) { |
|
1117 |
id = g_strdup (lm_message_node_get_attribute (message->node, |
|
1118 |
"id")); |
|
1119 |
} else { |
|
1120 |
id = _lm_utils_generate_id (); |
|
1121 |
lm_message_node_set_attributes (message->node, "id", id, NULL); |
|
1122 |
} |
|
1123 |
||
1124 |
g_source_remove (g_source_get_id (connection->incoming_source)); |
|
1125 |
g_source_unref (connection->incoming_source); |
|
1126 |
||
1127 |
lm_connection_send (connection, message, error); |
|
1128 |
||
1129 |
while (!reply) { |
|
1130 |
gint len; |
|
1131 |
const gchar *m_id; |
|
1132 |
||
1133 |
g_main_context_iteration (NULL, TRUE); |
|
1134 |
||
1135 |
len = connection->incoming_messages->length; |
|
1136 |
||
1137 |
if (len > last_len) { |
|
1138 |
LmMessage *m; |
|
1139 |
last_len = len; |
|
1140 |
m = (LmMessage *) g_queue_peek_tail (connection->incoming_messages); |
|
1141 |
m_id = lm_message_node_get_attribute (m->node, "id"); |
|
1142 |
||
1143 |
if (m_id && strcmp (m_id, id) == 0) { |
|
1144 |
reply = m; |
|
1145 |
} |
|
1146 |
||
1147 |
g_queue_pop_tail (connection->incoming_messages); |
|
1148 |
} |
|
1149 |
} |
|
1150 |
||
1151 |
g_free (id); |
|
1152 |
connection->incoming_source = connection_create_source (connection); |
|
1153 |
g_source_attach (connection->incoming_source, NULL); |
|
1154 |
||
1155 |
return reply; |
|
1156 |
} |
|
1157 |
||
1158 |
/** |
|
1159 |
* lm_connection_register_message_handler: |
|
1160 |
* @connection: Connection to register a handler for. |
|
1161 |
* @handler: Message handler to register. |
|
1162 |
* @type: Message type that @handler will handle. |
|
1163 |
* @priority: The priority in which to call @handler. |
|
1164 |
* |
|
1165 |
* Registers a #LmMessageHandler to handle incoming messages of a certain type. |
|
1166 |
* To unregister the handler call lm_connection_unregister_message_handler(). |
|
1167 |
**/ |
|
1168 |
void |
|
1169 |
lm_connection_register_message_handler (LmConnection *connection, |
|
1170 |
LmMessageHandler *handler, |
|
1171 |
LmMessageType type, |
|
1172 |
LmHandlerPriority priority) |
|
1173 |
{ |
|
1174 |
HandlerData *hd; |
|
1175 |
||
1176 |
g_return_if_fail (connection != NULL); |
|
1177 |
g_return_if_fail (handler != NULL); |
|
1178 |
g_return_if_fail (type != LM_MESSAGE_TYPE_UNKNOWN); |
|
1179 |
||
1180 |
hd = g_new0 (HandlerData, 1); |
|
1181 |
hd->priority = priority; |
|
1182 |
hd->handler = lm_message_handler_ref (handler); |
|
1183 |
||
1184 |
connection->handlers[type] = g_slist_insert_sorted (connection->handlers[type], |
|
1185 |
hd, |
|
1186 |
(GCompareFunc) connection_handler_compare_func); |
|
1187 |
} |
|
1188 |
||
1189 |
/** |
|
1190 |
* lm_connection_unregister_message_handler: |
|
1191 |
* @connection: Connection to unregister a handler for. |
|
1192 |
* @handler: The handler to unregister. |
|
1193 |
* @type: What type of messages to unregister this handler for. |
|
1194 |
* |
|
1195 |
* Unregisters a handler for @connection. @handler will no longer be called |
|
1196 |
* when incoming messages of @type arrive. |
|
1197 |
**/ |
|
1198 |
void |
|
1199 |
lm_connection_unregister_message_handler (LmConnection *connection, |
|
1200 |
LmMessageHandler *handler, |
|
1201 |
LmMessageType type) |
|
1202 |
{ |
|
1203 |
GSList *l, *prev = NULL; |
|
1204 |
||
1205 |
g_return_if_fail (connection != NULL); |
|
1206 |
g_return_if_fail (handler != NULL); |
|
1207 |
g_return_if_fail (type != LM_MESSAGE_TYPE_UNKNOWN); |
|
1208 |
||
1209 |
for (l = connection->handlers[type]; l; l = l->next) { |
|
1210 |
HandlerData *hd = (HandlerData *) l->data; |
|
1211 |
||
1212 |
if (hd->handler == handler) { |
|
1213 |
if (prev) { |
|
1214 |
prev->next = l->next; |
|
1215 |
} else { |
|
1216 |
connection->handlers[type] = l->next; |
|
1217 |
} |
|
1218 |
l->next = NULL; |
|
1219 |
g_slist_free (l); |
|
1220 |
lm_message_handler_unref (hd->handler); |
|
1221 |
g_free (hd); |
|
1222 |
break; |
|
1223 |
} |
|
1224 |
prev = l; |
|
1225 |
} |
|
1226 |
} |
|
1227 |
||
1228 |
/** |
|
1229 |
* lm_connection_set_disconnect_function: |
|
1230 |
* @connection: Connection to register disconnect callback for. |
|
1231 |
* @function: Function to be called when @connection is closed. |
|
1232 |
* @user_data: User data passed to @function. |
|
1233 |
* @notify: Function that will be called with @user_data when @user_data needs to be freed. Pass #NULL if it shouldn't be freed. |
|
1234 |
* |
|
1235 |
* Set the callback that will be called when a connection is closed. |
|
1236 |
**/ |
|
1237 |
void |
|
1238 |
lm_connection_set_disconnect_function (LmConnection *connection, |
|
1239 |
LmDisconnectFunction function, |
|
1240 |
gpointer user_data, |
|
1241 |
GDestroyNotify notify) |
|
1242 |
{ |
|
1243 |
if (connection->disconnect_cb) { |
|
1244 |
_lm_utils_free_callback (connection->disconnect_cb); |
|
1245 |
} |
|
1246 |
||
1247 |
connection->disconnect_cb = _lm_utils_new_callback (function, |
|
1248 |
user_data, |
|
1249 |
notify); |
|
1250 |
} |
|
1251 |
||
1252 |
/** |
|
1253 |
* lm_connection_send_raw: |
|
1254 |
* @connection: Connection used to send |
|
1255 |
* @str: The string to send, the entire string will be sent. |
|
1256 |
* @error: Set if error was detected during sending. |
|
1257 |
* |
|
1258 |
* Asynchronous call to send a raw string. Useful for debugging and testing. |
|
1259 |
* |
|
1260 |
* Return value: Returns #TRUE if no errors was detected during sending, |
|
1261 |
* #FALSE otherwise. |
|
1262 |
**/ |
|
1263 |
gboolean |
|
1264 |
lm_connection_send_raw (LmConnection *connection, |
|
1265 |
const gchar *str, |
|
1266 |
GError **error) |
|
1267 |
{ |
|
1268 |
g_return_val_if_fail (connection != NULL, FALSE); |
|
1269 |
||
1270 |
return connection_send (connection, str, -1, error); |
|
1271 |
} |
|
1272 |
||
1273 |
/** |
|
1274 |
* lm_connection_ref: |
|
1275 |
* @connection: Connection to add a reference to. |
|
1276 |
* |
|
1277 |
* Add a reference on @connection. To remove a reference call |
|
1278 |
* lm_connection_unref(). |
|
1279 |
* |
|
1280 |
* Return value: Returns the same connection. |
|
1281 |
**/ |
|
1282 |
LmConnection* |
|
1283 |
lm_connection_ref (LmConnection *connection) |
|
1284 |
{ |
|
1285 |
g_return_val_if_fail (connection != NULL, NULL); |
|
1286 |
||
1287 |
connection->ref_count++; |
|
1288 |
||
1289 |
return connection; |
|
1290 |
} |
|
1291 |
||
1292 |
/** |
|
1293 |
* lm_connection_unref: |
|
1294 |
* @connection: Connection to remove reference from. |
|
1295 |
* |
|
1296 |
* Removes a reference on @connection. If there are no references to |
|
1297 |
* @connection it will be fried and shouldn't be used again. |
|
1298 |
**/ |
|
1299 |
void |
|
1300 |
lm_connection_unref (LmConnection *connection) |
|
1301 |
{ |
|
1302 |
g_return_if_fail (connection != NULL); |
|
1303 |
||
1304 |
connection->ref_count--; |
|
1305 |
||
1306 |
if (connection->ref_count == 0) { |
|
1307 |
connection_free (connection); |
|
1308 |
} |
|
1309 |
} |
|
1310 |
||
1311 |
#if 0 |
|
1312 |
void |
|
1313 |
lm_connection_register (LmConnection *connection, |
|
1314 |
const gchar *username, |
|
1315 |
const gchar *password, |
|
1316 |
const gchar *resource, |
|
1317 |
LmRegisterCallback callback, |
|
1318 |
gpointer user_data) |
|
1319 |
{ |
|
1320 |
LmElement *element; |
|
1321 |
LmNode *q_node; |
|
1322 |
gchar *id; |
|
1323 |
static gint register_id = 0; |
|
1324 |
|
|
1325 |
g_return_if_fail (connection != NULL); |
|
1326 |
g_return_if_fail (lm_connection_is_open (connection)); |
|
1327 |
|
|
1328 |
/* Use lm:iq:register name space */ |
|
1329 |
||
1330 |
element = lm_iq_new (LM_IQ_TYPE_SET); |
|
1331 |
|
|
1332 |
q_node = lm_node_new ("query"); |
|
1333 |
lm_node_set_attribute (q_node, "xmlns", JABBER_IQ_REGISTER); |
|
1334 |
lm_node_add_child (q_node, "username", username); |
|
1335 |
lm_node_add_child (q_node, "password", password); |
|
1336 |
lm_node_add_child (q_node, "resource", resource); |
|
1337 |
||
1338 |
lm_element_add_child_node (element, q_node); |
|
1339 |
|
|
1340 |
id = g_strdup_printf ("register_%d", ++register_id); |
|
1341 |
lm_element_set_id (element, id); |
|
1342 |
|
|
1343 |
lm_connection_send (connection, element, NULL); |
|
1344 |
connection_add_callback (connection, id, callback, user_data); |
|
1345 |
lm_element_unref (element); |
|
1346 |
||
1347 |
g_free (id); |
|
1348 |
||
1349 |
|
|
1350 |
} |
|
1351 |
||
1352 |
#endif |