Mercurial > pidgin
annotate src/protocols/bonjour/jabber.c @ 11693:b91a84e7cbcb
[gaim-migrate @ 13979]
Don't crash on bonjour when IMing a non-existant user. Also better
error reporting, I think.
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Tue, 18 Oct 2005 04:16:44 +0000 |
parents | 1fd2a974379f |
children | 27ed05facc71 |
rev | line source |
---|---|
11477 | 1 /* |
2 * gaim - Bonjour Protocol Plugin | |
3 * | |
4 * Gaim is the legal property of its developers, whose names are too numerous | |
5 * to list here. Please refer to the COPYRIGHT file distributed with this | |
6 * source distribution. | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 */ | |
11688
1fd2a974379f
[gaim-migrate @ 13974]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11539
diff
changeset
|
22 #ifndef _WIN32 |
11477 | 23 #include <sys/socket.h> |
24 #include <netinet/in.h> | |
25 #include <arpa/inet.h> | |
11688
1fd2a974379f
[gaim-migrate @ 13974]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11539
diff
changeset
|
26 #else |
1fd2a974379f
[gaim-migrate @ 13974]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11539
diff
changeset
|
27 #include "libc_interface.h" |
1fd2a974379f
[gaim-migrate @ 13974]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11539
diff
changeset
|
28 #endif |
1fd2a974379f
[gaim-migrate @ 13974]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
11539
diff
changeset
|
29 #include <sys/types.h> |
11477 | 30 #include <glib.h> |
31 #include <glib/gprintf.h> | |
32 #include <unistd.h> | |
33 #include <fcntl.h> | |
34 | |
35 #include "network.h" | |
36 #include "eventloop.h" | |
37 #include "connection.h" | |
38 #include "blist.h" | |
39 #include "xmlnode.h" | |
40 #include "debug.h" | |
41 #include "notify.h" | |
42 #include "util.h" | |
43 | |
44 #include "jabber.h" | |
45 #include "bonjour.h" | |
46 #include "buddy.h" | |
47 | |
11539 | 48 gint |
49 _connect_to_buddy(GaimBuddy *gb) | |
11477 | 50 { |
51 gint socket_fd; | |
52 gint retorno = 0; | |
53 struct sockaddr_in buddy_address; | |
11539 | 54 |
11477 | 55 // Create a socket and make it non-blocking |
56 socket_fd = socket(PF_INET, SOCK_STREAM, 0); | |
11539 | 57 |
11477 | 58 buddy_address.sin_family = PF_INET; |
59 buddy_address.sin_port = htons(((BonjourBuddy*)(gb->proto_data))->port_p2pj); | |
60 inet_aton(((BonjourBuddy*)(gb->proto_data))->ip, &(buddy_address.sin_addr)); | |
61 memset(&(buddy_address.sin_zero), '\0', 8); | |
11539 | 62 |
11477 | 63 retorno = connect(socket_fd, (struct sockaddr*)&buddy_address, sizeof(struct sockaddr)); |
64 if (retorno == -1) { | |
11539 | 65 perror("connect"); |
11477 | 66 } |
67 fcntl(socket_fd, F_SETFL, O_NONBLOCK); | |
68 | |
69 return socket_fd; | |
70 } | |
71 | |
11539 | 72 char * |
73 _font_size_gaim_to_ichat(int size) | |
11477 | 74 { |
11539 | 75 GString *result = NULL; |
76 | |
77 switch(size) { | |
11477 | 78 case 1 : |
79 result = g_string_new("8"); | |
80 break; | |
81 case 2 : | |
82 result = g_string_new("10"); | |
83 break; | |
84 case 3 : | |
85 result = g_string_new("12"); | |
86 break; | |
87 case 4 : | |
88 result = g_string_new("14"); | |
89 break; | |
90 case 5 : | |
91 result = g_string_new("17"); | |
92 break; | |
93 case 6 : | |
94 result = g_string_new("21"); | |
95 break; | |
96 case 7 : | |
97 result = g_string_new("24"); | |
98 break; | |
99 default: | |
100 result = g_string_new("12"); | |
101 } | |
11539 | 102 |
11477 | 103 return g_string_free(result, FALSE); |
104 } | |
105 | |
11539 | 106 char * |
107 _font_size_ichat_to_gaim(int size) | |
11477 | 108 { |
11539 | 109 GString *result = NULL; |
110 | |
11477 | 111 if (size > 24) { |
112 result = g_string_new("7"); | |
11539 | 113 } else if (size >= 21) { |
11477 | 114 result = g_string_new("6"); |
11539 | 115 } else if (size >= 17) { |
11477 | 116 result = g_string_new("5"); |
11539 | 117 } else if (size >= 14) { |
11477 | 118 result = g_string_new("4"); |
11539 | 119 } else if (size >= 12) { |
11477 | 120 result = g_string_new("3"); |
11539 | 121 } else if (size >= 10) { |
11477 | 122 result = g_string_new("2"); |
123 } else { | |
124 result = g_string_new("1"); | |
125 } | |
126 | |
127 return g_string_free(result, FALSE); | |
128 } | |
11539 | 129 void |
130 _jabber_parse_and_write_message_to_ui(char *message, GaimConnection *connection, GaimBuddy *gb) | |
11477 | 131 { |
11539 | 132 xmlnode *body_node = NULL; |
133 char *body = NULL; | |
134 xmlnode *html_node = NULL; | |
11477 | 135 gboolean isHTML = FALSE; |
11539 | 136 xmlnode *html_body_node = NULL; |
137 const char *ichat_balloon_color = NULL; | |
138 const char *ichat_text_color = NULL; | |
139 xmlnode *html_body_font_node = NULL; | |
140 const char *font_face = NULL; | |
141 const char *font_size = NULL; | |
142 const char *font_color = NULL; | |
143 char *html_body = NULL; | |
144 xmlnode *events_node = NULL; | |
11477 | 145 gboolean composing_event = FALSE; |
146 gint garbage = -1; | |
11539 | 147 xmlnode *message_node = NULL; |
11477 | 148 |
149 // Parsing of the message | |
150 message_node = xmlnode_from_str(message, strlen(message)); | |
151 if (message_node == NULL) { | |
152 return; | |
153 } | |
11539 | 154 |
11477 | 155 body_node = xmlnode_get_child(message_node, "body"); |
156 if (body_node != NULL) { | |
157 body = xmlnode_get_data(body_node); | |
158 } else { | |
159 return; | |
160 } | |
11539 | 161 |
11477 | 162 html_node = xmlnode_get_child(message_node, "html"); |
11539 | 163 if (html_node != NULL) |
164 { | |
11477 | 165 isHTML = TRUE; |
166 html_body_node = xmlnode_get_child(html_node, "body"); | |
11539 | 167 if (html_body_node != NULL) |
168 { | |
11477 | 169 ichat_balloon_color = xmlnode_get_attrib(html_body_node, "ichatballooncolor"); |
170 ichat_text_color = xmlnode_get_attrib(html_body_node, "ichattextcolor"); | |
171 html_body_font_node = xmlnode_get_child(html_body_node, "font"); | |
11539 | 172 if (html_body_font_node != NULL) |
173 { // Types of messages sent by iChat | |
11477 | 174 font_face = xmlnode_get_attrib(html_body_font_node, "face"); |
175 // The absolute iChat font sizes should be converted to 1..7 range | |
176 font_size = xmlnode_get_attrib(html_body_font_node, "ABSZ"); | |
11539 | 177 if (font_size != NULL) |
178 { | |
11477 | 179 font_size = _font_size_ichat_to_gaim(atoi(font_size)); //<-- This call will probably leak memory |
180 } | |
181 font_color = xmlnode_get_attrib(html_body_font_node, "color"); | |
182 html_body = xmlnode_get_data(html_body_font_node); | |
11539 | 183 if (html_body == NULL) |
184 { | |
185 // This is the kind of formated messages that Gaim creates | |
11477 | 186 html_body = xmlnode_to_str(html_body_font_node, &garbage); |
187 } | |
188 } else { | |
189 isHTML = FALSE; | |
190 } | |
191 } else { | |
192 isHTML = FALSE; | |
193 } | |
11539 | 194 |
11477 | 195 } |
11539 | 196 |
11477 | 197 events_node = xmlnode_get_child_with_namespace(message_node, "x", "jabber:x:event"); |
11539 | 198 if (events_node != NULL) |
199 { | |
200 if (xmlnode_get_child(events_node, "composing") != NULL) | |
201 { | |
11477 | 202 composing_event = TRUE; |
203 } | |
11539 | 204 if (xmlnode_get_child(events_node, "id") != NULL) |
205 { | |
206 // The user is just typing | |
11477 | 207 xmlnode_free(message_node); |
208 g_free(body); | |
209 g_free(html_body); | |
210 return; | |
211 } | |
212 } | |
11539 | 213 |
11477 | 214 // Compose the message |
11539 | 215 if (isHTML) |
216 { | |
11477 | 217 if (font_face == NULL) font_face = "Helvetica"; |
218 if (font_size == NULL) font_size = "3"; | |
219 if (ichat_text_color == NULL) ichat_text_color = "#000000"; | |
220 if (ichat_balloon_color == NULL) ichat_balloon_color = "#FFFFFF"; | |
11539 | 221 body = g_strconcat("<font face='", font_face, "' size='", font_size, "' color='", ichat_text_color, |
11477 | 222 "' back='", ichat_balloon_color, "'>", html_body, "</font>", NULL); |
223 } | |
11539 | 224 |
11477 | 225 // Send the message to the UI |
226 serv_got_im(connection, gb->name, body, 0, time(NULL)); | |
11539 | 227 |
11477 | 228 // Free all the strings and nodes (the attributes are freed with their nodes) |
229 xmlnode_free(message_node); | |
230 g_free(body); | |
231 g_free(html_body); | |
232 } | |
233 | |
11539 | 234 gboolean |
235 _check_buddy_by_address(gpointer key, gpointer value, gpointer address) | |
11477 | 236 { |
11539 | 237 GaimBuddy *gb = (GaimBuddy*)value; |
238 BonjourBuddy *bb = (BonjourBuddy*)gb->proto_data; | |
239 | |
240 if (bb != NULL) | |
241 { | |
11477 | 242 if (g_strcasecmp(bb->ip, (char*)address) == 0) { |
243 return TRUE; | |
244 } else { | |
245 return FALSE; | |
246 } | |
247 } else { | |
11539 | 248 return FALSE; |
11477 | 249 } |
250 } | |
251 | |
11539 | 252 gint |
253 _read_data(gint socket, char **message) | |
11477 | 254 { |
11539 | 255 GString *data = g_string_new(""); |
11477 | 256 char parcial_data[512]; |
257 gint total_message_length = 0; | |
258 gint parcial_message_length = 0; | |
259 | |
260 // Read chunks of 512 bytes till the end of the data | |
11539 | 261 while ((parcial_message_length = recv(socket, parcial_data, 512, 0)) > 0) |
262 { | |
11477 | 263 g_string_append_len(data, parcial_data, parcial_message_length); |
264 total_message_length += parcial_message_length; | |
265 } | |
11539 | 266 |
267 if (parcial_message_length == -1) | |
268 { | |
11477 | 269 perror("recv"); |
270 if (total_message_length == 0) { | |
271 return -1; | |
272 } | |
273 } | |
274 | |
275 *message = data->str; | |
276 g_string_free(data, FALSE); | |
11539 | 277 if (total_message_length != 0) |
278 gaim_debug_info("bonjour", "Receive: -%s- %d bytes\n", *message, total_message_length); | |
279 | |
11477 | 280 return total_message_length; |
281 } | |
282 | |
11539 | 283 gint |
284 _send_data(gint socket, char *message) | |
11477 | 285 { |
286 gint message_len = strlen(message); | |
287 gint parcial_sent = 0; | |
11539 | 288 gchar *parcial_message = message; |
289 | |
290 while ((parcial_sent = send(socket, parcial_message, message_len, 0)) < message_len) | |
291 { | |
11477 | 292 if (parcial_sent != -1) { |
293 parcial_message += parcial_sent; | |
294 message_len -= parcial_sent; | |
295 } else { | |
296 return -1; | |
297 } | |
298 } | |
11539 | 299 |
11477 | 300 return strlen(message); |
301 } | |
302 | |
11539 | 303 void |
304 _client_socket_handler(gpointer data, gint socket, GaimInputCondition condition) | |
11477 | 305 { |
11539 | 306 char *message = NULL; |
11477 | 307 gint message_length; |
11539 | 308 GaimBuddy *gb = (GaimBuddy*)data; |
309 GaimAccount *account = gb->account; | |
310 GaimConversation *conversation; | |
311 char *closed_conv_message; | |
312 BonjourBuddy *bb = (BonjourBuddy*)gb->proto_data; | |
11477 | 313 gboolean closed_conversation = FALSE; |
314 | |
315 // Read the data from the socket | |
316 if ((message_length = _read_data(socket, &message)) == -1) { | |
317 // There have been an error reading from the socket | |
318 return; | |
319 } else if (message_length == 0) { // The other end has closed the socket | |
320 closed_conversation = TRUE; | |
321 } else { | |
322 message[message_length] = '\0'; | |
11539 | 323 |
11477 | 324 while (g_ascii_iscntrl(message[message_length - 1])) { |
325 message[message_length - 1] = '\0'; | |
326 message_length--; | |
327 } | |
328 } | |
11539 | 329 |
11477 | 330 // Check if the start of the doctype has been received, if not check that the current |
331 // data is the doctype | |
11539 | 332 if (!(bb->conversation->start_step_one)) |
333 { | |
334 if (g_str_has_prefix(message, DOCTYPE_DECLARATION)) | |
335 { | |
11477 | 336 bb->conversation->start_step_one = TRUE; |
337 } | |
338 } | |
11539 | 339 |
340 // Check if the start of the stream has been received, if not check that the current | |
11477 | 341 // data is the start of the stream |
11539 | 342 if (!(bb->conversation->start_step_two)) |
343 { | |
11477 | 344 if (g_str_has_suffix(message, STREAM_START)) { |
345 bb->conversation->start_step_two = TRUE; | |
11539 | 346 |
11477 | 347 // If we haven't done it yet, we have to sent the start of the stream to the other buddy |
348 if (!(bb->conversation->stream_started)) { | |
349 if (send(bb->conversation->socket, DOCTYPE, strlen(DOCTYPE), 0) == -1) { | |
350 gaim_debug_error("bonjour", "Unable to start a conversation with %s\n", bb->name); | |
351 } | |
352 } | |
353 } | |
354 return; | |
355 } | |
11539 | 356 |
11477 | 357 // Check that this is not the end of the conversation |
358 if (g_str_has_prefix(message, STREAM_END) || (closed_conversation == TRUE)) { | |
359 // Close the socket, clear the watcher and free memory | |
360 if (bb->conversation != NULL) { | |
361 close(bb->conversation->socket); | |
362 gaim_input_remove(bb->conversation->watcher_id); | |
363 g_free(bb->conversation->buddy_name); | |
364 g_free(bb->conversation); | |
365 bb->conversation = NULL; | |
366 } | |
11539 | 367 |
11477 | 368 // Inform the user that the conversation has been closed |
11498 | 369 conversation = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, gb->name, account); |
11477 | 370 closed_conv_message = g_strconcat(gb->name, " has closed the conversation.", NULL); |
371 gaim_conversation_write(conversation, NULL, closed_conv_message, GAIM_MESSAGE_SYSTEM, time(NULL)); | |
372 } else { | |
373 // Parse the message to get the data and send to the ui | |
374 _jabber_parse_and_write_message_to_ui(message, account->gc, gb); | |
375 } | |
376 } | |
377 | |
11539 | 378 void |
379 _server_socket_handler(gpointer data, int server_socket, GaimInputCondition condition) | |
11477 | 380 { |
11539 | 381 GaimBuddy *gb = NULL; |
11477 | 382 struct sockaddr_in their_addr; // connector's address information |
11515 | 383 socklen_t sin_size = sizeof(struct sockaddr); |
11477 | 384 int client_socket; |
11539 | 385 BonjourBuddy *bb = NULL; |
386 char *address_text = NULL; | |
387 GaimBuddyList *bl = gaim_get_blist(); | |
11477 | 388 |
389 //Check that it is a read condition | |
390 if (condition != GAIM_INPUT_READ) { | |
391 return; | |
392 } | |
393 | |
11539 | 394 if ((client_socket = accept(server_socket, (struct sockaddr *)&their_addr, &sin_size)) == -1) |
395 { | |
11477 | 396 return; |
397 } | |
398 fcntl(client_socket, F_SETFL, O_NONBLOCK); | |
399 | |
400 // Look for the buddy that has open the conversation and fill information | |
401 address_text = inet_ntoa(their_addr.sin_addr); | |
402 gb = (GaimBuddy*)g_hash_table_find(bl->buddies, _check_buddy_by_address, address_text); | |
11539 | 403 if (gb == NULL) |
404 { | |
11477 | 405 gaim_debug_info("bonjour", "We don't like invisible buddies, this is not a superheros comic\n"); |
406 close(client_socket); | |
407 return; | |
408 } | |
409 bb = (BonjourBuddy*)gb->proto_data; | |
410 | |
411 // Check if the conversation has been previously started | |
11539 | 412 if (bb->conversation == NULL) |
413 { | |
11477 | 414 bb->conversation = g_new(BonjourJabberConversation, 1); |
415 bb->conversation->socket = client_socket; | |
416 bb->conversation->start_step_one = FALSE; | |
417 bb->conversation->start_step_two = FALSE; | |
418 bb->conversation->stream_started = FALSE; | |
419 bb->conversation->buddy_name = g_strdup(gb->name); | |
420 bb->conversation->message_id = 1; | |
11539 | 421 |
11477 | 422 if (bb->conversation->stream_started == FALSE) { |
423 // Start the stream | |
424 send(bb->conversation->socket, DOCTYPE, strlen(DOCTYPE), 0); | |
425 bb->conversation->stream_started = TRUE; | |
426 } | |
427 | |
428 // Open a watcher for the client socket | |
11539 | 429 bb->conversation->watcher_id = gaim_input_add(client_socket, GAIM_INPUT_READ, |
11477 | 430 _client_socket_handler, gb); |
431 } else { | |
432 close(client_socket); | |
433 } | |
434 } | |
435 | |
11539 | 436 gint |
437 bonjour_jabber_start(BonjourJabber *data) | |
11477 | 438 { |
439 struct sockaddr_in my_addr; | |
440 int yes = 1; | |
11539 | 441 char *error_message = NULL; |
11477 | 442 |
443 // Open a listening socket for incoming conversations | |
11539 | 444 if ((data->socket = socket(PF_INET, SOCK_STREAM, 0)) < 0) |
445 { | |
11477 | 446 gaim_debug_error("bonjour", "Cannot get socket\n"); |
447 error_message = strerror(errno); | |
448 gaim_debug_error("bonjour", "%s\n", error_message); | |
449 gaim_connection_error(data->account->gc, "Cannot open socket"); | |
450 return -1; | |
451 } | |
11539 | 452 |
11477 | 453 // Make the socket reusable |
11539 | 454 if (setsockopt(data->socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) != 0) |
455 { | |
11477 | 456 gaim_debug_error("bonjour", "Cannot make socket reusable\n"); |
457 error_message = strerror(errno); | |
458 gaim_debug_error("bonjour", "%s\n", error_message); | |
459 gaim_connection_error(data->account->gc, "Error setting socket options"); | |
460 return -1; | |
461 } | |
462 | |
463 memset(&my_addr, 0, sizeof(struct sockaddr_in)); | |
464 my_addr.sin_family = PF_INET; | |
465 my_addr.sin_port = htons(data->port); | |
11539 | 466 |
467 if (bind(data->socket, (struct sockaddr*)&my_addr, sizeof(struct sockaddr)) != 0) | |
468 { | |
11477 | 469 gaim_debug_error("bonjour", "Cannot bind socket\n"); |
470 error_message = strerror(errno); | |
471 gaim_debug_error("bonjour", "%s\n", error_message); | |
472 gaim_connection_error(data->account->gc, "Cannot bind socket to port"); | |
473 return -1; | |
474 } | |
11539 | 475 |
476 if (listen(data->socket, 10) != 0) | |
477 { | |
11477 | 478 gaim_debug_error("bonjour", "Cannot listen to socket\n"); |
479 error_message = strerror(errno); | |
480 gaim_debug_error("bonjour", "%s\n", error_message); | |
481 gaim_connection_error(data->account->gc, "Cannot listen to socket"); | |
482 return -1; | |
483 } | |
484 | |
485 //data->socket = gaim_network_listen(data->port); | |
486 | |
11539 | 487 //if (data->socket == -1) |
488 //{ | |
11477 | 489 // gaim_debug_error("bonjour", "No se ha podido crear el socket\n"); |
490 //} | |
491 | |
492 // Open a watcher in the socket we have just opened | |
493 data->watcher_id = gaim_input_add(data->socket, GAIM_INPUT_READ, _server_socket_handler, data); | |
11539 | 494 |
11477 | 495 return 0; |
496 } | |
497 | |
11693 | 498 int |
11539 | 499 bonjour_jabber_send_message(BonjourJabber *data, const gchar *to, const gchar *body) |
11477 | 500 { |
11539 | 501 xmlnode *message_node = NULL; |
502 gchar *message = NULL; | |
11477 | 503 gint message_length = -1; |
11539 | 504 xmlnode *message_body_node = NULL; |
505 xmlnode *message_html_node = NULL; | |
506 xmlnode *message_html_body_node = NULL; | |
507 xmlnode *message_html_body_font_node = NULL; | |
508 xmlnode *message_x_node = NULL; | |
11693 | 509 GaimBuddy *gb = NULL; |
510 BonjourBuddy *bb = NULL; | |
11539 | 511 char *conv_message = NULL; |
512 GaimConversation *conversation = NULL; | |
513 char *message_from_ui = NULL; | |
514 char *stripped_message = NULL; | |
515 | |
11693 | 516 gb = gaim_find_buddy(data->account, to); |
517 if (gb == NULL) | |
518 /* You can not send a message to an offline buddy */ | |
519 return -10000; | |
520 | |
521 bb = (BonjourBuddy *)gb->proto_data; | |
522 | |
11477 | 523 // Enclose the message from the UI within a "font" node |
524 message_body_node = xmlnode_new("body"); | |
525 stripped_message = gaim_markup_strip_html(body); | |
526 xmlnode_insert_data(message_body_node, stripped_message, strlen(stripped_message)); | |
11539 | 527 |
11477 | 528 message_from_ui = g_strconcat("<font>", body, "</font>", NULL); |
529 message_html_body_font_node = xmlnode_from_str(message_from_ui, strlen(message_from_ui)); | |
11539 | 530 |
11477 | 531 message_html_body_node = xmlnode_new("body"); |
532 xmlnode_insert_child(message_html_body_node, message_html_body_font_node); | |
11539 | 533 |
11477 | 534 message_html_node = xmlnode_new("html"); |
535 xmlnode_set_attrib(message_html_node, "xmlns", "http://www.w3.org/1999/xhtml"); | |
536 xmlnode_insert_child(message_html_node, message_html_body_node); | |
537 | |
538 message_x_node = xmlnode_new("x"); | |
539 xmlnode_set_attrib(message_x_node, "xmlns", "jabber:x:event"); | |
540 xmlnode_insert_child(message_x_node, xmlnode_new("composing")); | |
11539 | 541 |
11477 | 542 message_node = xmlnode_new("message"); |
543 xmlnode_set_attrib(message_node, "to", ((BonjourBuddy*)(gb->proto_data))->name); | |
544 xmlnode_set_attrib(message_node, "type", "chat"); | |
545 xmlnode_insert_child(message_node, message_body_node); | |
546 xmlnode_insert_child(message_node, message_html_node); | |
547 xmlnode_insert_child(message_node, message_x_node); | |
11539 | 548 |
11477 | 549 message = xmlnode_to_str(message_node, &message_length); |
550 | |
551 // Check if there is a previously open conversation | |
11539 | 552 if (bb->conversation == NULL) |
553 { | |
11477 | 554 bb->conversation = g_new(BonjourJabberConversation, 1); |
555 bb->conversation->socket = _connect_to_buddy(gb);; | |
556 bb->conversation->start_step_one = FALSE; | |
557 bb->conversation->start_step_two = FALSE; | |
558 bb->conversation->stream_started = FALSE; | |
559 bb->conversation->buddy_name = g_strdup(gb->name); | |
11539 | 560 bb->conversation->watcher_id = gaim_input_add(bb->conversation->socket, |
11477 | 561 GAIM_INPUT_READ, _client_socket_handler, gb); |
562 } | |
563 | |
564 // Check if the stream for the conversation has been started | |
11539 | 565 if (bb->conversation->stream_started == FALSE) |
566 { | |
11477 | 567 // Start the stream |
11539 | 568 if (send(bb->conversation->socket, DOCTYPE, strlen(DOCTYPE), 0) == -1) |
569 { | |
11477 | 570 gaim_debug_error("bonjour", "Unable to start a conversation\n"); |
571 perror("send"); | |
572 conv_message = g_strdup("Unable to send the message, the conversation couldn't be started."); | |
11498 | 573 conversation = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, bb->name, data->account); |
11477 | 574 gaim_conversation_write(conversation, NULL, conv_message, GAIM_MESSAGE_SYSTEM, time(NULL)); |
575 close(bb->conversation->socket); | |
576 gaim_input_remove(bb->conversation->watcher_id); | |
11539 | 577 |
11477 | 578 // Free all the data related to the conversation |
579 g_free(bb->conversation->buddy_name); | |
580 g_free(bb->conversation); | |
581 bb->conversation = NULL; | |
11693 | 582 return 0; |
11477 | 583 } |
11539 | 584 |
11477 | 585 bb->conversation->stream_started = TRUE; |
586 } | |
11539 | 587 |
11477 | 588 // Send the message |
11539 | 589 if (_send_data(bb->conversation->socket, message) == -1) |
11693 | 590 return -10000; |
591 | |
592 return 1; | |
11477 | 593 } |
594 | |
11539 | 595 void |
596 bonjour_jabber_close_conversation(BonjourJabber *data, GaimBuddy *gb) | |
11477 | 597 { |
11539 | 598 BonjourBuddy *bb = (BonjourBuddy*)gb->proto_data; |
599 | |
600 if (bb->conversation != NULL) | |
601 { | |
11477 | 602 // Send the end of the stream to the other end of the conversation |
603 send(bb->conversation->socket, STREAM_END, strlen(STREAM_END), 0); | |
11539 | 604 |
11477 | 605 // Close the socket and remove the watcher |
606 close(bb->conversation->socket); | |
607 gaim_input_remove(bb->conversation->watcher_id); | |
11539 | 608 |
11477 | 609 // Free all the data related to the conversation |
610 g_free(bb->conversation->buddy_name); | |
611 g_free(bb->conversation); | |
612 bb->conversation = NULL; | |
613 } | |
614 } | |
615 | |
11539 | 616 void |
617 bonjour_jabber_stop(BonjourJabber *data) | |
11477 | 618 { |
11539 | 619 GaimBuddy *gb = NULL; |
620 BonjourBuddy *bb = NULL; | |
621 GSList *buddies; | |
622 GSList *l; | |
623 | |
11477 | 624 // Close the server socket and remove all the watcher |
625 close(data->socket); | |
626 gaim_input_remove(data->watcher_id); | |
11539 | 627 |
11477 | 628 // Close all the sockets and remove all the watchers after sending end streams |
11539 | 629 if (data->account->gc != NULL) |
630 { | |
11477 | 631 buddies = gaim_find_buddies(data->account, data->account->username); |
11539 | 632 for (l = buddies; l; l = l->next) |
633 { | |
11477 | 634 gb = (GaimBuddy*)l->data; |
635 bb = (BonjourBuddy*)gb->proto_data; | |
11539 | 636 if (bb->conversation != NULL) |
637 { | |
11477 | 638 send(bb->conversation->socket, STREAM_END, strlen(STREAM_END), 0); |
639 close(bb->conversation->socket); | |
640 gaim_input_remove(bb->conversation->watcher_id); | |
641 } | |
642 } | |
643 g_slist_free(buddies); | |
644 } | |
645 } |