Mercurial > pidgin
annotate src/protocols/jabber/si.c @ 8622:ffed55cbdd67
[gaim-migrate @ 9373]
Any objections to this?
committer: Tailor Script <tailor@pidgin.im>
| author | Mark Doliner <mark@kingant.net> |
|---|---|
| date | Fri, 09 Apr 2004 02:49:30 +0000 |
| parents | 46df38515dc7 |
| children | 518455386538 |
| rev | line source |
|---|---|
| 7395 | 1 /* |
| 2 * gaim - Jabber Protocol Plugin | |
| 3 * | |
| 4 * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> | |
| 5 * | |
| 6 * This program is free software; you can redistribute it and/or modify | |
| 7 * it under the terms of the GNU General Public License as published by | |
| 8 * the Free Software Foundation; either version 2 of the License, or | |
| 9 * (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 | |
| 14 * GNU General Public License for more details. | |
| 15 * | |
| 16 * You should have received a copy of the GNU General Public License | |
| 17 * along with this program; if not, write to the Free Software | |
| 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 * | |
| 20 */ | |
| 21 #include "internal.h" | |
| 22 #include "debug.h" | |
| 23 #include "ft.h" | |
|
8231
f50c059b6384
[gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
8135
diff
changeset
|
24 #include "network.h" |
| 7395 | 25 #include "notify.h" |
| 8262 | 26 #include "sha.h" |
| 7395 | 27 #include "util.h" |
| 28 | |
| 29 #include "buddy.h" | |
| 8312 | 30 #include "disco.h" |
| 7395 | 31 #include "jabber.h" |
| 32 #include "iq.h" | |
| 33 #include "si.h" | |
| 34 | |
| 35 #include "si.h" | |
| 36 | |
| 8262 | 37 struct bytestreams_streamhost { |
| 38 char *jid; | |
| 39 char *host; | |
| 40 int port; | |
| 41 }; | |
| 42 | |
| 43 typedef struct _JabberSIXfer { | |
| 44 JabberStream *js; | |
| 45 | |
| 46 char *stream_id; | |
| 47 char *iq_id; | |
| 48 | |
| 49 enum { | |
| 50 STREAM_METHOD_UNKNOWN = 0, | |
| 51 STREAM_METHOD_BYTESTREAMS = 2 << 1, | |
| 52 STREAM_METHOD_IBB = 2 << 2, | |
| 53 STREAM_METHOD_UNSUPPORTED = 2 << 31 | |
| 54 } stream_method; | |
| 55 | |
| 56 GList *streamhosts; | |
| 57 GaimProxyInfo *gpi; | |
| 8316 | 58 |
| 59 char *rxqueue; | |
| 60 size_t rxlen; | |
| 8262 | 61 } JabberSIXfer; |
| 62 | |
| 63 static GaimXfer* | |
| 64 jabber_si_xfer_find(JabberStream *js, const char *sid, const char *from) | |
| 7395 | 65 { |
| 66 GList *xfers; | |
| 67 | |
| 8262 | 68 if(!sid || !from) |
| 7395 | 69 return NULL; |
| 70 | |
| 71 for(xfers = js->file_transfers; xfers; xfers = xfers->next) { | |
| 72 GaimXfer *xfer = xfers->data; | |
| 73 JabberSIXfer *jsx = xfer->data; | |
| 8316 | 74 if(jsx->stream_id && xfer->who && |
| 75 !strcmp(jsx->stream_id, sid) && !strcmp(xfer->who, from)) | |
| 7395 | 76 return xfer; |
| 77 } | |
| 78 | |
| 79 return NULL; | |
| 80 } | |
| 81 | |
| 8262 | 82 |
| 83 static void jabber_si_bytestreams_attempt_connect(GaimXfer *xfer); | |
| 84 | |
| 85 static void jabber_si_bytestreams_connect_cb(gpointer data, gint source, GaimInputCondition cond) | |
| 86 { | |
| 7395 | 87 GaimXfer *xfer = data; |
| 88 JabberSIXfer *jsx = xfer->data; | |
| 8262 | 89 JabberIq *iq; |
| 90 xmlnode *query, *su; | |
| 91 struct bytestreams_streamhost *streamhost = jsx->streamhosts->data; | |
| 7395 | 92 |
| 8262 | 93 gaim_proxy_info_destroy(jsx->gpi); |
| 94 | |
| 95 if(source < 0) { | |
| 96 jsx->streamhosts = g_list_remove(jsx->streamhosts, streamhost); | |
| 97 g_free(streamhost->jid); | |
| 98 g_free(streamhost->host); | |
| 99 g_free(streamhost); | |
| 100 jabber_si_bytestreams_attempt_connect(xfer); | |
| 101 return; | |
| 102 } | |
| 103 | |
| 104 iq = jabber_iq_new_query(jsx->js, JABBER_IQ_RESULT, "http://jabber.org/protocol/bytestreams"); | |
| 105 xmlnode_set_attrib(iq->node, "to", xfer->who); | |
| 106 jabber_iq_set_id(iq, jsx->iq_id); | |
| 107 query = xmlnode_get_child(iq->node, "query"); | |
| 108 su = xmlnode_new_child(query, "streamhost-used"); | |
| 109 xmlnode_set_attrib(su, "jid", streamhost->jid); | |
| 110 | |
| 111 jabber_iq_send(iq); | |
| 112 | |
| 113 gaim_xfer_start(xfer, source, NULL, -1); | |
| 114 } | |
| 115 | |
| 116 static void jabber_si_bytestreams_attempt_connect(GaimXfer *xfer) | |
| 117 { | |
| 118 JabberSIXfer *jsx = xfer->data; | |
| 119 struct bytestreams_streamhost *streamhost; | |
| 120 char *dstaddr, *p; | |
| 121 int i; | |
| 122 unsigned char hashval[20]; | |
| 123 | |
| 124 if(!jsx->streamhosts) { | |
| 125 JabberIq *iq = jabber_iq_new(jsx->js, JABBER_IQ_ERROR); | |
| 126 xmlnode *error, *condition; | |
| 127 | |
| 128 if(jsx->iq_id) | |
| 129 jabber_iq_set_id(iq, jsx->iq_id); | |
| 130 | |
| 131 xmlnode_set_attrib(iq->node, "to", xfer->who); | |
| 132 error = xmlnode_new_child(iq->node, "error"); | |
| 133 xmlnode_set_attrib(error, "code", "404"); | |
| 134 xmlnode_set_attrib(error, "type", "cancel"); | |
| 135 condition = xmlnode_new_child(error, "condition"); | |
| 136 xmlnode_set_attrib(condition, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas"); | |
| 137 xmlnode_new_child(condition, "item-not-found"); | |
| 138 | |
| 139 jabber_iq_send(iq); | |
| 140 | |
| 141 gaim_xfer_cancel_local(xfer); | |
| 142 | |
| 143 return; | |
| 144 } | |
| 145 | |
| 146 streamhost = jsx->streamhosts->data; | |
| 7395 | 147 |
| 8262 | 148 jsx->gpi = gaim_proxy_info_new(); |
| 149 gaim_proxy_info_set_type(jsx->gpi, GAIM_PROXY_SOCKS5); | |
| 150 gaim_proxy_info_set_host(jsx->gpi, streamhost->host); | |
| 151 gaim_proxy_info_set_port(jsx->gpi, streamhost->port); | |
| 152 | |
| 153 dstaddr = g_strdup_printf("%s%s%s@%s/%s", jsx->stream_id, xfer->who, jsx->js->user->node, | |
| 154 jsx->js->user->domain, jsx->js->user->resource); | |
| 155 shaBlock((unsigned char *)dstaddr, strlen(dstaddr), hashval); | |
| 156 g_free(dstaddr); | |
| 157 dstaddr = g_malloc(41); | |
| 158 p = dstaddr; | |
| 159 for(i=0; i<20; i++, p+=2) | |
| 160 snprintf(p, 3, "%02x", hashval[i]); | |
| 161 | |
| 162 gaim_proxy_connect_socks5(jsx->gpi, dstaddr, 0, jabber_si_bytestreams_connect_cb, xfer); | |
| 163 g_free(dstaddr); | |
| 164 } | |
| 165 | |
| 166 void jabber_bytestreams_parse(JabberStream *js, xmlnode *packet) | |
| 167 { | |
| 168 GaimXfer *xfer; | |
| 169 JabberSIXfer *jsx; | |
| 170 xmlnode *query, *streamhost; | |
| 171 const char *sid, *from; | |
| 172 | |
| 173 if(!(from = xmlnode_get_attrib(packet, "from"))) | |
| 174 return; | |
| 175 | |
| 176 if(!(query = xmlnode_get_child(packet, "query"))) | |
| 177 return; | |
| 178 | |
| 179 if(!(sid = xmlnode_get_attrib(query, "sid"))) | |
| 180 return; | |
| 181 | |
| 182 if(!(xfer = jabber_si_xfer_find(js, sid, from))) | |
| 183 return; | |
| 184 | |
| 185 jsx = xfer->data; | |
| 186 if(jsx->iq_id) | |
| 187 g_free(jsx->iq_id); | |
| 188 jsx->iq_id = g_strdup(xmlnode_get_attrib(packet, "id")); | |
| 189 | |
| 190 for(streamhost = xmlnode_get_child(query, "streamhost"); streamhost; | |
| 191 streamhost = xmlnode_get_next_twin(streamhost)) { | |
| 192 const char *jid, *host, *port; | |
| 193 int portnum; | |
| 194 | |
| 195 if((jid = xmlnode_get_attrib(streamhost, "jid")) && | |
| 196 (host = xmlnode_get_attrib(streamhost, "host")) && | |
| 197 (port = xmlnode_get_attrib(streamhost, "port")) && | |
| 198 (portnum = atoi(port))) { | |
| 199 struct bytestreams_streamhost *sh = g_new0(struct bytestreams_streamhost, 1); | |
| 200 sh->jid = g_strdup(jid); | |
| 201 sh->host = g_strdup(host); | |
| 202 sh->port = portnum; | |
| 203 jsx->streamhosts = g_list_append(jsx->streamhosts, sh); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 jabber_si_bytestreams_attempt_connect(xfer); | |
| 7395 | 208 } |
| 209 | |
| 8312 | 210 static void |
| 8316 | 211 jabber_si_xfer_bytestreams_send_read_again_cb(gpointer data, gint source, |
| 8312 | 212 GaimInputCondition cond) |
| 213 { | |
| 214 GaimXfer *xfer = data; | |
| 8316 | 215 JabberSIXfer *jsx = xfer->data; |
| 8312 | 216 int i; |
| 8316 | 217 char buffer[256]; |
| 218 int len; | |
| 219 char *dstaddr, *p; | |
| 220 unsigned char hashval[20]; | |
| 221 const char *host; | |
| 222 | |
| 223 gaim_debug_info("jabber", "in jabber_si_xfer_bytestreams_send_read_again_cb\n"); | |
| 8312 | 224 |
| 8316 | 225 if(jsx->rxlen < 5) { |
| 226 gaim_debug_info("jabber", "reading the first 5 bytes\n"); | |
| 227 if((len = read(source, buffer, 5 - jsx->rxlen)) <= 0) { | |
| 228 gaim_input_remove(xfer->watcher); | |
| 229 xfer->watcher = 0; | |
| 230 close(source); | |
| 231 gaim_xfer_cancel_remote(xfer); | |
| 232 return; | |
| 233 } | |
| 234 jsx->rxqueue = g_realloc(jsx->rxqueue, len + jsx->rxlen); | |
| 235 memcpy(jsx->rxqueue + jsx->rxlen, buffer, len); | |
| 236 jsx->rxlen += len; | |
| 237 return; | |
| 238 } else if(jsx->rxqueue[0] != 0x05 || jsx->rxqueue[1] != 0x01 || | |
| 239 jsx->rxqueue[3] != 0x03) { | |
| 240 gaim_debug_info("jabber", "invalid socks5 stuff\n"); | |
| 241 gaim_input_remove(xfer->watcher); | |
| 242 xfer->watcher = 0; | |
| 243 close(source); | |
| 244 gaim_xfer_cancel_remote(xfer); | |
| 245 return; | |
| 246 } else if(jsx->rxlen - 5 < jsx->rxqueue[4] + 2) { | |
| 247 gaim_debug_info("jabber", "reading umpteen more bytes\n"); | |
| 248 if((len = read(source, buffer, jsx->rxqueue[4] + 5 + 2 - jsx->rxlen)) <= 0) { | |
| 249 gaim_input_remove(xfer->watcher); | |
| 250 xfer->watcher = 0; | |
| 251 close(source); | |
| 252 gaim_xfer_cancel_remote(xfer); | |
| 253 return; | |
| 254 } | |
| 255 jsx->rxqueue = g_realloc(jsx->rxqueue, len + jsx->rxlen); | |
| 256 memcpy(jsx->rxqueue + jsx->rxlen, buffer, len); | |
| 257 jsx->rxlen += len; | |
| 258 } | |
| 8312 | 259 |
| 8316 | 260 if(jsx->rxlen - 5 < jsx->rxqueue[4] + 2) |
| 261 return; | |
| 262 | |
| 263 gaim_input_remove(xfer->watcher); | |
| 264 xfer->watcher = 0; | |
| 8312 | 265 |
| 8316 | 266 dstaddr = g_strdup_printf("%s%s@%s/%s%s", jsx->stream_id, |
| 267 jsx->js->user->node, jsx->js->user->domain, | |
| 268 jsx->js->user->resource, xfer->who); | |
| 269 shaBlock((unsigned char *)dstaddr, strlen(dstaddr), hashval); | |
| 270 g_free(dstaddr); | |
| 271 dstaddr = g_malloc(41); | |
| 272 p = dstaddr; | |
| 273 for(i=0; i<20; i++, p+=2) | |
| 274 snprintf(p, 3, "%02x", hashval[i]); | |
| 275 | |
| 276 if(jsx->rxqueue[4] != 40 || strncmp(dstaddr, jsx->rxqueue+5, 40) || | |
| 277 jsx->rxqueue[45] != 0x00 || jsx->rxqueue[46] != 0x00) { | |
| 278 gaim_debug_error("jabber", "someone connected with the wrong info!\n"); | |
| 8312 | 279 close(source); |
| 280 gaim_xfer_cancel_remote(xfer); | |
| 281 return; | |
| 282 } | |
| 283 | |
| 8316 | 284 host = gaim_network_get_ip_for_account(jsx->js->gc->account, jsx->js->fd); |
| 285 | |
| 286 buffer[0] = 0x05; | |
| 287 buffer[1] = 0x00; | |
| 288 buffer[2] = 0x00; | |
| 289 buffer[3] = 0x03; | |
| 290 buffer[4] = strlen(host); | |
| 291 memcpy(buffer + 5, host, strlen(host)); | |
| 292 buffer[5+strlen(host)] = 0x00; | |
| 293 buffer[6+strlen(host)] = 0x00; | |
| 294 | |
| 295 write(source, buffer, strlen(host)+7); | |
| 296 | |
| 297 gaim_xfer_start(xfer, source, NULL, -1); | |
| 298 } | |
| 299 | |
| 300 static void | |
| 301 jabber_si_xfer_bytestreams_send_read_cb(gpointer data, gint source, | |
| 302 GaimInputCondition cond) | |
| 303 { | |
| 304 GaimXfer *xfer = data; | |
| 305 JabberSIXfer *jsx = xfer->data; | |
| 306 int i; | |
| 307 int len; | |
| 308 char buffer[256]; | |
| 309 | |
| 310 gaim_debug_info("jabber", "in jabber_si_xfer_bytestreams_send_read_cb\n"); | |
| 311 | |
| 312 xfer->fd = source; | |
| 313 | |
| 314 if(jsx->rxlen < 2) { | |
| 315 gaim_debug_info("jabber", "reading those first two bytes\n"); | |
| 316 if((len = read(source, buffer, 2 - jsx->rxlen)) <= 0) { | |
| 317 gaim_input_remove(xfer->watcher); | |
| 318 xfer->watcher = 0; | |
| 319 close(source); | |
| 320 gaim_xfer_cancel_remote(xfer); | |
| 321 return; | |
| 322 } | |
| 323 jsx->rxqueue = g_realloc(jsx->rxqueue, len + jsx->rxlen); | |
| 324 memcpy(jsx->rxqueue + jsx->rxlen, buffer, len); | |
| 325 jsx->rxlen += len; | |
| 326 return; | |
| 327 } else if(jsx->rxlen - 2 < jsx->rxqueue[1]) { | |
| 328 gaim_debug_info("jabber", "reading the next umpteen bytes\n"); | |
| 329 if((len = read(source, buffer, jsx->rxqueue[1] + 2 - jsx->rxlen)) <= 0) { | |
| 330 gaim_input_remove(xfer->watcher); | |
| 331 xfer->watcher = 0; | |
| 332 close(source); | |
| 333 gaim_xfer_cancel_remote(xfer); | |
| 334 return; | |
| 335 } | |
| 336 jsx->rxqueue = g_realloc(jsx->rxqueue, len + jsx->rxlen); | |
| 337 memcpy(jsx->rxqueue + jsx->rxlen, buffer, len); | |
| 338 jsx->rxlen += len; | |
| 339 } | |
| 340 | |
| 341 if(jsx->rxlen -2 < jsx->rxqueue[1]) | |
| 342 return; | |
| 343 | |
| 344 gaim_input_remove(xfer->watcher); | |
| 345 xfer->watcher = 0; | |
| 346 | |
| 347 | |
| 348 gaim_debug_info("jabber", "checking to make sure we're socks FIVE\n"); | |
| 349 | |
| 350 if(jsx->rxqueue[0] != 0x05) { | |
| 351 close(source); | |
| 352 gaim_xfer_cancel_remote(xfer); | |
| 353 return; | |
| 354 } | |
| 355 | |
| 356 gaim_debug_info("jabber", "going to test %hhu different methods\n", jsx->rxqueue[1]); | |
| 357 | |
| 358 for(i=0; i<jsx->rxqueue[1]; i++) { | |
| 359 | |
| 360 gaim_debug_info("jabber", "testing %hhu\n", jsx->rxqueue[i+2]); | |
| 361 if(jsx->rxqueue[i+2] == 0x00) { | |
| 362 buffer[0] = 0x05; | |
| 363 buffer[1] = 0x00; | |
| 364 write(source, buffer, 2); | |
| 365 xfer->watcher = gaim_input_add(source, GAIM_INPUT_READ, | |
| 366 jabber_si_xfer_bytestreams_send_read_again_cb, xfer); | |
| 367 g_free(jsx->rxqueue); | |
| 368 jsx->rxqueue = NULL; | |
| 369 jsx->rxlen = 0; | |
| 8312 | 370 return; |
| 371 } | |
| 372 } | |
| 373 | |
| 8316 | 374 buffer[0] = 0x05; |
| 375 buffer[1] = 0xFF; | |
| 376 write(source, buffer, 2); | |
| 377 close(source); | |
| 378 g_free(jsx->rxqueue); | |
| 379 jsx->rxqueue = NULL; | |
| 380 jsx->rxlen = 0; | |
| 8312 | 381 gaim_xfer_cancel_remote(xfer); |
| 382 } | |
| 383 | |
| 384 static void | |
| 8316 | 385 jabber_si_xfer_bytestreams_send_connected_cb(gpointer data, gint source, |
| 386 GaimInputCondition cond) | |
| 387 { | |
| 388 GaimXfer *xfer = data; | |
| 389 int acceptfd; | |
| 390 | |
| 391 gaim_debug_info("jabber", "in jabber_si_xfer_bytestreams_send_connected_cb\n"); | |
| 392 | |
| 393 if((acceptfd = accept(source, NULL, 0)) == -1) { | |
| 394 gaim_debug_warning("jabber", "accept: %s\n", strerror(errno)); | |
| 395 return; | |
| 396 } | |
| 397 | |
| 398 gaim_input_remove(xfer->watcher); | |
| 399 close(source); | |
| 400 | |
| 401 xfer->watcher = gaim_input_add(acceptfd, GAIM_INPUT_READ, | |
| 402 jabber_si_xfer_bytestreams_send_read_cb, xfer); | |
| 403 } | |
| 404 | |
| 405 | |
| 406 static void | |
| 8312 | 407 jabber_si_xfer_bytestreams_send_init(GaimXfer *xfer) |
| 408 { | |
| 409 JabberSIXfer *jsx = xfer->data; | |
| 410 JabberIq *iq; | |
| 411 xmlnode *query, *streamhost; | |
| 412 char *jid, *port; | |
| 413 int fd; | |
| 414 | |
| 415 iq = jabber_iq_new_query(jsx->js, JABBER_IQ_SET, | |
| 416 "http://jabber.org/protocol/bytestreams"); | |
| 417 xmlnode_set_attrib(iq->node, "to", xfer->who); | |
| 418 query = xmlnode_get_child(iq->node, "query"); | |
| 419 | |
| 420 xmlnode_set_attrib(query, "sid", jsx->stream_id); | |
| 421 | |
| 422 streamhost = xmlnode_new_child(query, "streamhost"); | |
| 423 jid = g_strdup_printf("%s@%s/%s", jsx->js->user->node, jsx->js->user->domain, jsx->js->user->resource); | |
| 424 xmlnode_set_attrib(streamhost, "jid", jid); | |
| 425 g_free(jid); | |
| 426 | |
| 427 if((fd = gaim_network_listen_range(0, 0)) < 0) { | |
| 428 /* XXX: couldn't open a port, we're fscked */ | |
| 429 return; | |
| 430 } | |
| 431 | |
| 8316 | 432 xmlnode_set_attrib(streamhost, "host", gaim_network_get_ip_for_account(jsx->js->gc->account, jsx->js->fd)); |
| 8312 | 433 xfer->local_port = gaim_network_get_port_from_fd(fd); |
| 8316 | 434 port = g_strdup_printf("%hu", xfer->local_port); |
| 8312 | 435 xmlnode_set_attrib(streamhost, "port", port); |
| 436 g_free(port); | |
| 437 | |
| 438 xfer->watcher = gaim_input_add(fd, GAIM_INPUT_READ, | |
| 439 jabber_si_xfer_bytestreams_send_connected_cb, xfer); | |
| 440 | |
| 441 /* XXX: insert proxies here */ | |
| 442 | |
| 443 /* XXX: callback to find out which streamhost they used, or see if they | |
| 444 * screwed it up */ | |
| 445 jabber_iq_send(iq); | |
| 446 } | |
| 447 | |
| 448 static void jabber_si_xfer_send_method_cb(JabberStream *js, xmlnode *packet, | |
| 449 gpointer data) | |
| 450 { | |
| 451 GaimXfer *xfer = data; | |
| 452 xmlnode *si, *feature, *x, *field, *value; | |
| 453 | |
| 454 if(!(si = xmlnode_get_child_with_namespace(packet, "si", "http://jabber.org/protocol/si"))) { | |
| 455 gaim_xfer_cancel_remote(xfer); | |
| 456 return; | |
| 457 } | |
| 458 | |
| 459 if(!(feature = xmlnode_get_child_with_namespace(si, "feature", "http://jabber.org/protocol/feature-neg"))) { | |
| 460 gaim_xfer_cancel_remote(xfer); | |
| 461 return; | |
| 462 } | |
| 463 | |
| 464 if(!(x = xmlnode_get_child_with_namespace(feature, "x", "jabber:x:data"))) { | |
| 465 gaim_xfer_cancel_remote(xfer); | |
| 466 return; | |
| 467 } | |
| 468 | |
| 469 for(field = xmlnode_get_child(x, "field"); field; field = xmlnode_get_next_twin(field)) { | |
| 470 const char *var = xmlnode_get_attrib(field, "var"); | |
| 471 | |
| 472 if(var && !strcmp(var, "stream-method")) { | |
| 473 if((value = xmlnode_get_child(field, "value"))) { | |
| 474 char *val = xmlnode_get_data(value); | |
| 475 if(val && !strcmp(val, "http://jabber.org/protocol/bytestreams")) { | |
| 476 jabber_si_xfer_bytestreams_send_init(xfer); | |
| 477 g_free(val); | |
| 478 return; | |
| 479 } | |
| 480 g_free(val); | |
| 481 } | |
| 482 } | |
| 483 } | |
| 484 gaim_xfer_cancel_remote(xfer); | |
| 485 } | |
| 486 | |
| 487 static void jabber_si_xfer_send_request(GaimXfer *xfer) | |
| 488 { | |
| 489 JabberSIXfer *jsx = xfer->data; | |
| 490 JabberIq *iq; | |
| 491 xmlnode *si, *file, *feature, *x, *field, *option, *value; | |
| 492 char buf[32]; | |
| 493 | |
| 494 xfer->filename = g_path_get_basename(xfer->local_filename); | |
| 495 | |
| 496 iq = jabber_iq_new(jsx->js, JABBER_IQ_SET); | |
| 497 xmlnode_set_attrib(iq->node, "to", xfer->who); | |
| 498 si = xmlnode_new_child(iq->node, "si"); | |
| 499 xmlnode_set_attrib(si, "xmlns", "http://jabber.org/protocol/si"); | |
| 500 jsx->stream_id = jabber_get_next_id(jsx->js); | |
| 501 xmlnode_set_attrib(si, "id", jsx->stream_id); | |
| 502 xmlnode_set_attrib(si, "profile", | |
| 503 "http://jabber.org/protocol/si/profile/file-transfer"); | |
| 504 | |
| 505 file = xmlnode_new_child(si, "file"); | |
| 506 xmlnode_set_attrib(file, "xmlns", | |
| 507 "http://jabber.org/protocol/si/profile/file-transfer"); | |
| 508 xmlnode_set_attrib(file, "name", xfer->filename); | |
| 509 g_snprintf(buf, sizeof(buf), "%d", xfer->size); | |
| 510 xmlnode_set_attrib(file, "size", buf); | |
| 511 /* maybe later we'll do hash and date attribs */ | |
| 512 | |
| 513 feature = xmlnode_new_child(si, "feature"); | |
| 514 xmlnode_set_attrib(feature, "xmlns", | |
| 515 "http://jabber.org/protocol/feature-neg"); | |
| 516 x = xmlnode_new_child(feature, "x"); | |
| 517 xmlnode_set_attrib(x, "xmlns", "jabber:x:data"); | |
| 518 xmlnode_set_attrib(x, "type", "form"); | |
| 519 field = xmlnode_new_child(x, "field"); | |
| 520 xmlnode_set_attrib(field, "var", "stream-method"); | |
| 521 xmlnode_set_attrib(field, "type", "list-single"); | |
| 522 option = xmlnode_new_child(field, "option"); | |
| 523 value = xmlnode_new_child(option, "value"); | |
| 524 xmlnode_insert_data(value, "http://jabber.org/protocol/bytestreams", | |
| 525 -1); | |
| 526 /* | |
| 527 option = xmlnode_new_child(field, "option"); | |
| 528 value = xmlnode_new_child(option, "value"); | |
| 529 xmlnode_insert_data(value, "http://jabber.org/protocol/ibb", -1); | |
| 530 */ | |
| 531 | |
| 532 jabber_iq_set_callback(iq, jabber_si_xfer_send_method_cb, xfer); | |
| 533 | |
| 534 jabber_iq_send(iq); | |
| 535 } | |
| 536 | |
| 8316 | 537 static void jabber_si_xfer_free(GaimXfer *xfer) |
| 8312 | 538 { |
| 8316 | 539 JabberSIXfer *jsx = xfer->data; |
| 540 JabberStream *js = jsx->js; | |
| 541 | |
| 542 js->file_transfers = g_list_remove(js->file_transfers, xfer); | |
| 543 | |
| 544 g_free(jsx->stream_id); | |
| 545 g_free(jsx->iq_id); | |
| 546 /* XXX: free other stuff */ | |
| 547 g_free(jsx); | |
| 548 xfer->data = NULL; | |
| 549 } | |
| 550 | |
| 551 static void jabber_si_xfer_cancel_send(GaimXfer *xfer) | |
| 552 { | |
| 553 jabber_si_xfer_free(xfer); | |
| 8312 | 554 gaim_debug(GAIM_DEBUG_INFO, "jabber", "in jabber_si_xfer_cancel_send\n"); |
| 555 } | |
| 556 | |
| 557 | |
| 8316 | 558 static void jabber_si_xfer_cancel_recv(GaimXfer *xfer) |
| 8312 | 559 { |
| 8316 | 560 jabber_si_xfer_free(xfer); |
| 8312 | 561 gaim_debug(GAIM_DEBUG_INFO, "jabber", "in jabber_si_xfer_cancel_recv\n"); |
| 562 } | |
| 563 | |
| 564 | |
| 8316 | 565 static void jabber_si_xfer_end(GaimXfer *xfer) |
| 566 { | |
| 567 jabber_si_xfer_free(xfer); | |
| 568 } | |
| 569 | |
| 570 | |
| 8312 | 571 static void jabber_si_xfer_send_disco_cb(JabberStream *js, const char *who, |
| 572 JabberCapabilities capabilities, gpointer data) | |
| 573 { | |
| 574 GaimXfer *xfer = data; | |
| 575 | |
| 576 if(capabilities & JABBER_CAP_SI_FILE_XFER) { | |
| 577 jabber_si_xfer_send_request(xfer); | |
| 578 } else { | |
| 579 char *msg = g_strdup_printf(_("Unable to send file to %s, user does not support file transfers"), who); | |
| 580 gaim_notify_error(js->gc, _("File Send Failed"), | |
| 581 _("File Send Failed"), msg); | |
| 582 g_free(msg); | |
| 583 } | |
| 584 } | |
| 585 | |
| 8262 | 586 static void jabber_si_xfer_init(GaimXfer *xfer) |
| 587 { | |
| 588 JabberSIXfer *jsx = xfer->data; | |
| 589 JabberIq *iq; | |
| 8312 | 590 if(gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) { |
| 591 JabberBuddy *jb; | |
| 592 JabberBuddyResource *jbr = NULL; | |
| 593 | |
| 594 jb = jabber_buddy_find(jsx->js, xfer->who, TRUE); | |
| 595 /* XXX */ | |
| 596 if(!jb) | |
| 597 return; | |
| 8262 | 598 |
| 8312 | 599 /* XXX: for now, send to the first resource available */ |
| 600 if(g_list_length(jb->resources) >= 1) { | |
| 601 char *who; | |
| 8316 | 602 jbr = jabber_buddy_find_resource(jb, NULL); |
| 8312 | 603 who = g_strdup_printf("%s/%s", xfer->who, jbr->name); |
| 604 g_free(xfer->who); | |
| 605 xfer->who = who; | |
| 606 jabber_disco_info_do(jsx->js, who, | |
| 607 jabber_si_xfer_send_disco_cb, xfer); | |
| 608 } else { | |
| 609 return; /* XXX: ick */ | |
| 610 } | |
| 611 } else { | |
| 612 xmlnode *si, *feature, *x, *field, *value; | |
| 8262 | 613 |
| 8312 | 614 iq = jabber_iq_new(jsx->js, JABBER_IQ_RESULT); |
| 615 xmlnode_set_attrib(iq->node, "to", xfer->who); | |
| 616 if(jsx->iq_id) | |
| 617 jabber_iq_set_id(iq, jsx->iq_id); | |
| 618 | |
| 619 si = xmlnode_new_child(iq->node, "si"); | |
| 620 xmlnode_set_attrib(si, "xmlns", "http://jabber.org/protocol/si"); | |
| 621 | |
| 622 feature = xmlnode_new_child(si, "feature"); | |
| 623 xmlnode_set_attrib(feature, "xmlns", "http://jabber.org/protocol/feature-neg"); | |
| 8262 | 624 |
| 8312 | 625 x = xmlnode_new_child(feature, "x"); |
| 626 xmlnode_set_attrib(x, "xmlns", "jabber:x:data"); | |
| 8343 | 627 xmlnode_set_attrib(x, "type", "submit"); |
| 8262 | 628 |
| 8312 | 629 field = xmlnode_new_child(x, "field"); |
| 630 xmlnode_set_attrib(field, "var", "stream-method"); | |
| 631 | |
| 632 value = xmlnode_new_child(field, "value"); | |
| 633 if(jsx->stream_method & STREAM_METHOD_BYTESTREAMS) | |
| 634 xmlnode_insert_data(value, "http://jabber.org/protocol/bytestreams", -1); | |
| 635 /* | |
| 636 else if(jsx->stream_method & STREAM_METHOD_IBB) | |
| 8262 | 637 xmlnode_insert_data(value, "http://jabber.org/protocol/ibb", -1); |
| 638 */ | |
| 639 | |
| 8312 | 640 jabber_iq_send(iq); |
| 641 } | |
| 8262 | 642 } |
| 643 | |
| 8312 | 644 void jabber_si_xfer_ask_send(GaimConnection *gc, const char *name) |
| 645 { | |
| 646 JabberStream *js = gc->proto_data; | |
| 647 GaimXfer *xfer; | |
| 648 JabberSIXfer *jsx; | |
| 649 | |
| 650 if(!gaim_find_buddy(gc->account, name) || !jabber_buddy_find(js, name, FALSE)) | |
| 651 return; | |
| 652 | |
| 653 xfer = gaim_xfer_new(gaim_connection_get_account(gc), GAIM_XFER_SEND, name); | |
| 8262 | 654 |
| 8312 | 655 xfer->data = jsx = g_new0(JabberSIXfer, 1); |
| 656 jsx->js = js; | |
| 8262 | 657 |
| 8312 | 658 gaim_xfer_set_init_fnc(xfer, jabber_si_xfer_init); |
| 659 gaim_xfer_set_cancel_send_fnc(xfer, jabber_si_xfer_cancel_send); | |
| 8316 | 660 gaim_xfer_set_end_fnc(xfer, jabber_si_xfer_end); |
| 8312 | 661 |
| 662 js->file_transfers = g_list_append(js->file_transfers, xfer); | |
| 663 | |
| 664 gaim_xfer_request(xfer); | |
| 8262 | 665 } |
| 666 | |
| 667 void jabber_si_parse(JabberStream *js, xmlnode *packet) | |
| 668 { | |
| 669 JabberSIXfer *jsx; | |
| 670 GaimXfer *xfer; | |
| 671 xmlnode *si, *file, *feature, *x, *field, *option, *value; | |
| 672 const char *stream_id, *filename, *filesize_c, *profile; | |
| 673 size_t filesize = 0; | |
| 674 | |
| 675 if(!(si = xmlnode_get_child(packet, "si"))) | |
| 676 return; | |
| 677 | |
| 678 if(!(profile = xmlnode_get_attrib(si, "profile")) || | |
| 679 strcmp(profile, "http://jabber.org/protocol/si/profile/file-transfer")) | |
| 680 return; | |
| 681 | |
| 682 if(!(stream_id = xmlnode_get_attrib(si, "id"))) | |
| 683 return; | |
| 684 | |
| 685 if(!(file = xmlnode_get_child(si, "file"))) | |
| 686 return; | |
| 687 | |
| 688 if(!(filename = xmlnode_get_attrib(file, "name"))) | |
| 689 return; | |
| 690 | |
| 691 if((filesize_c = xmlnode_get_attrib(file, "size"))) | |
| 692 filesize = atoi(filesize_c); | |
| 693 | |
| 694 if(!(feature = xmlnode_get_child(si, "feature"))) | |
| 695 return; | |
| 696 | |
| 697 if(!(x = xmlnode_get_child_with_namespace(feature, "x", "jabber:x:data"))) | |
| 698 return; | |
| 699 | |
| 700 jsx = g_new0(JabberSIXfer, 1); | |
| 701 | |
| 702 for(field = xmlnode_get_child(x, "field"); field; field = xmlnode_get_next_twin(field)) { | |
| 703 const char *var = xmlnode_get_attrib(field, "var"); | |
| 704 if(var && !strcmp(var, "stream-method")) { | |
| 705 for(option = xmlnode_get_child(field, "option"); option; | |
| 706 option = xmlnode_get_next_twin(option)) { | |
| 707 if((value = xmlnode_get_child(option, "value"))) { | |
| 708 char *val; | |
| 709 if((val = xmlnode_get_data(value))) { | |
| 710 if(!strcmp(val, "http://jabber.org/protocol/bytestreams")) { | |
| 711 jsx->stream_method |= STREAM_METHOD_BYTESTREAMS; | |
| 712 /* | |
| 713 } else if(!strcmp(val, "http://jabber.org/protocol/ibb")) { | |
| 714 jsx->stream_method |= STREAM_METHOD_IBB; | |
| 715 */ | |
| 716 } | |
| 717 g_free(val); | |
| 718 } | |
| 719 } | |
| 720 } | |
| 721 } | |
| 722 } | |
| 723 | |
| 724 if(jsx->stream_method == STREAM_METHOD_UNKNOWN) { | |
| 725 g_free(jsx); | |
| 726 return; | |
| 727 } | |
| 728 | |
| 729 jsx->js = js; | |
| 730 jsx->stream_id = g_strdup(stream_id); | |
| 731 jsx->iq_id = g_strdup(xmlnode_get_attrib(packet, "id")); | |
| 732 | |
| 733 xfer = gaim_xfer_new(js->gc->account, GAIM_XFER_RECEIVE, | |
| 734 xmlnode_get_attrib(packet, "from")); | |
| 735 xfer->data = jsx; | |
| 736 | |
| 737 gaim_xfer_set_filename(xfer, filename); | |
| 738 if(filesize > 0) | |
| 739 gaim_xfer_set_size(xfer, filesize); | |
| 740 | |
| 741 gaim_xfer_set_init_fnc(xfer, jabber_si_xfer_init); | |
| 8316 | 742 gaim_xfer_set_cancel_recv_fnc(xfer, jabber_si_xfer_cancel_recv); |
| 743 gaim_xfer_set_end_fnc(xfer, jabber_si_xfer_end); | |
| 8262 | 744 |
| 745 js->file_transfers = g_list_append(js->file_transfers, xfer); | |
| 746 | |
| 747 gaim_xfer_request(xfer); | |
| 748 } | |
| 749 | |
| 7395 | 750 |
