Mercurial > pidgin
annotate src/protocols/yahoo/yahoo_filexfer.c @ 10937:ca3e0882d377
[gaim-migrate @ 12721]
sf patch #1202156, from Bleeter Yaluser
. Forward port privacy
. Tidy up whitespace
. Remove file TX send limit
committer: Tailor Script <tailor@pidgin.im>
author | Mark Doliner <mark@kingant.net> |
---|---|
date | Tue, 24 May 2005 04:54:27 +0000 |
parents | f2e86683cafc |
children | be22eb8fa671 |
rev | line source |
---|---|
7651 | 1 /* |
2 * @file yahoo_filexfer.c Yahoo Filetransfer | |
3 * | |
8046 | 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. | |
7651 | 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 */ | |
22 | |
23 #include "prpl.h" | |
24 #include "internal.h" | |
25 #include "util.h" | |
26 #include "debug.h" | |
27 #include "notify.h" | |
28 #include "proxy.h" | |
29 #include "ft.h" | |
30 #include "yahoo.h" | |
10392 | 31 #include "yahoo_packet.h" |
7651 | 32 #include "yahoo_filexfer.h" |
33 | |
34 | |
35 | |
36 struct yahoo_xfer_data { | |
37 gchar *host; | |
38 gchar *path; | |
39 int port; | |
40 GaimConnection *gc; | |
41 long expires; | |
42 gboolean started; | |
43 gchar *rxqueue; | |
44 guint rxlen; | |
45 }; | |
46 | |
47 static void yahoo_xfer_data_free(struct yahoo_xfer_data *xd) | |
48 { | |
49 if (xd->host) | |
50 g_free(xd->host); | |
51 if (xd->path) | |
52 g_free(xd->path); | |
53 g_free(xd); | |
54 } | |
55 | |
56 static void yahoo_receivefile_connected(gpointer data, gint source, GaimInputCondition condition) | |
57 { | |
58 GaimXfer *xfer; | |
59 struct yahoo_xfer_data *xd; | |
60 gchar *buf; | |
61 | |
62 gaim_debug(GAIM_DEBUG_INFO, "yahoo", | |
63 "AAA - in yahoo_receivefile_connected\n"); | |
64 if (!(xfer = data)) | |
65 return; | |
66 if (!(xd = xfer->data)) | |
67 return; | |
68 if (source < 0) { | |
10654 | 69 gaim_xfer_error(GAIM_XFER_RECEIVE, gaim_xfer_get_account(xfer), |
70 xfer->who, _("Unable to connect.")); | |
7805 | 71 gaim_xfer_cancel_remote(xfer); |
7651 | 72 return; |
73 } | |
74 | |
75 xfer->fd = source; | |
76 gaim_xfer_start(xfer, source, NULL, 0); | |
77 | |
10651 | 78 buf = g_strdup_printf("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n", |
79 xd->path, xd->host); | |
7651 | 80 write(xfer->fd, buf, strlen(buf)); |
81 g_free(buf); | |
82 | |
83 return; | |
84 } | |
85 | |
86 static void yahoo_sendfile_connected(gpointer data, gint source, GaimInputCondition condition) | |
87 { | |
88 GaimXfer *xfer; | |
89 struct yahoo_xfer_data *xd; | |
90 struct yahoo_packet *pkt; | |
91 gchar *size, *post, *buf; | |
10576 | 92 const char *host; |
10575 | 93 int content_length, port; |
7651 | 94 GaimConnection *gc; |
95 GaimAccount *account; | |
96 struct yahoo_data *yd; | |
7805 | 97 char *filename; |
7651 | 98 |
99 gaim_debug(GAIM_DEBUG_INFO, "yahoo", | |
100 "AAA - in yahoo_sendfile_connected\n"); | |
101 if (!(xfer = data)) | |
102 return; | |
103 if (!(xd = xfer->data)) | |
104 return; | |
105 | |
106 gc = xd->gc; | |
107 account = gaim_connection_get_account(gc); | |
108 yd = gc->proto_data; | |
109 | |
110 | |
111 | |
112 if (source < 0) { | |
10654 | 113 gaim_xfer_error(GAIM_XFER_RECEIVE, gaim_xfer_get_account(xfer), |
114 xfer->who, _("Unable to connect.")); | |
7805 | 115 gaim_xfer_cancel_remote(xfer); |
7651 | 116 return; |
117 } | |
118 | |
119 xfer->fd = source; | |
120 gaim_xfer_start(xfer, source, NULL, 0); | |
121 | |
122 | |
123 pkt = yahoo_packet_new(YAHOO_SERVICE_FILETRANSFER, YAHOO_STATUS_AVAILABLE, yd->session_id); | |
124 | |
10111 | 125 size = g_strdup_printf("%" G_GSIZE_FORMAT, gaim_xfer_get_size(xfer)); |
7805 | 126 filename = g_path_get_basename(gaim_xfer_get_local_filename(xfer)); |
10394 | 127 |
128 yahoo_packet_hash(pkt, "sssss", 0, gaim_connection_get_display_name(gc), | |
129 5, xfer->who, 14, "", 27, filename, 28, size); | |
7651 | 130 |
131 content_length = YAHOO_PACKET_HDRLEN + yahoo_packet_length(pkt); | |
132 | |
133 buf = g_strdup_printf("Y=%s; T=%s", yd->cookie_y, yd->cookie_t); | |
134 | |
10576 | 135 host = gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST); |
10575 | 136 port = gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT); |
137 post = g_strdup_printf("POST http://%s:%d/notifyft HTTP/1.0\r\n" | |
10111 | 138 "Content-length: %" G_GSIZE_FORMAT "\r\n" |
7651 | 139 "Host: %s:%d\r\n" |
140 "Cookie: %s\r\n" | |
141 "\r\n", | |
10576 | 142 host, port, content_length + 4 + gaim_xfer_get_size(xfer), host, port, buf); |
7651 | 143 write(xfer->fd, post, strlen(post)); |
144 | |
10392 | 145 yahoo_packet_send_special(pkt, xfer->fd, 8); |
7651 | 146 yahoo_packet_free(pkt); |
147 | |
148 write(xfer->fd, "29\xc0\x80", 4); | |
149 | |
150 g_free(size); | |
151 g_free(post); | |
152 g_free(buf); | |
7805 | 153 g_free(filename); |
7651 | 154 } |
155 | |
156 static void yahoo_xfer_init(GaimXfer *xfer) | |
157 { | |
158 struct yahoo_xfer_data *xfer_data; | |
159 GaimConnection *gc; | |
160 GaimAccount *account; | |
7827 | 161 struct yahoo_data *yd; |
7651 | 162 |
163 xfer_data = xfer->data; | |
164 gc = xfer_data->gc; | |
7827 | 165 yd = gc->proto_data; |
7651 | 166 account = gaim_connection_get_account(gc); |
167 | |
168 if (gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) { | |
10937 | 169 if (yd->jp) { |
170 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xferjp_host", YAHOOJP_XFER_HOST), | |
171 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT), | |
172 yahoo_sendfile_connected, xfer) == -1) | |
173 { | |
174 gaim_notify_error(gc, NULL, _("File Transfer Failed"), | |
175 _("Unable to establish file descriptor.")); | |
176 gaim_xfer_cancel_remote(xfer); | |
177 } | |
8282 | 178 } else { |
10937 | 179 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST), |
180 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT), | |
181 yahoo_sendfile_connected, xfer) == -1) | |
182 { | |
183 gaim_notify_error(gc, NULL, _("File Transfer Failed"), | |
184 _("Unable to establish file descriptor.")); | |
185 gaim_xfer_cancel_remote(xfer); | |
8282 | 186 } |
7651 | 187 } |
188 } else { | |
189 xfer->fd = gaim_proxy_connect(account, xfer_data->host, xfer_data->port, | |
190 yahoo_receivefile_connected, xfer); | |
191 if (xfer->fd == -1) { | |
10654 | 192 gaim_notify_error(gc, NULL, _("File Transfer Failed"), |
7651 | 193 _("Unable to establish file descriptor.")); |
194 gaim_xfer_cancel_remote(xfer); | |
195 } | |
196 } | |
197 } | |
198 | |
199 static void yahoo_xfer_start(GaimXfer *xfer) | |
200 { | |
201 /* We don't need to do anything here, do we? */ | |
202 } | |
203 | |
204 static void yahoo_xfer_end(GaimXfer *xfer) | |
205 { | |
206 GaimAccount *account; | |
207 struct yahoo_xfer_data *xfer_data; | |
208 | |
209 account = gaim_xfer_get_account(xfer); | |
210 xfer_data = xfer->data; | |
211 | |
212 | |
213 if (xfer_data) | |
214 yahoo_xfer_data_free(xfer_data); | |
215 xfer->data = NULL; | |
216 | |
217 } | |
218 | |
219 guint calculate_length(const gchar *l, size_t len) | |
220 { | |
221 int i; | |
222 | |
223 for (i = 0; i < len; i++) { | |
224 if (!g_ascii_isdigit(l[i])) | |
225 continue; | |
226 return strtol(l + i, NULL, 10); | |
227 } | |
228 return 0; | |
229 } | |
230 | |
231 | |
8231
f50c059b6384
[gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
8046
diff
changeset
|
232 ssize_t yahoo_xfer_read(char **buffer, GaimXfer *xfer) |
7651 | 233 { |
7710 | 234 gchar buf[4096]; |
7682 | 235 ssize_t len; |
7651 | 236 gchar *start = NULL; |
237 gchar *length; | |
238 gchar *end; | |
7710 | 239 int filelen; |
7651 | 240 struct yahoo_xfer_data *xd = xfer->data; |
241 | |
242 if (gaim_xfer_get_type(xfer) != GAIM_XFER_RECEIVE) { | |
243 return 0; | |
244 } | |
245 | |
246 len = read(xfer->fd, buf, sizeof(buf)); | |
247 | |
7682 | 248 if (len <= 0) { |
7710 | 249 if ((gaim_xfer_get_size(xfer) > 0) && |
9798 | 250 (gaim_xfer_get_bytes_sent(xfer) >= gaim_xfer_get_size(xfer))) { |
7682 | 251 gaim_xfer_set_completed(xfer, TRUE); |
9798 | 252 return 0; |
253 } else | |
254 return -1; | |
7651 | 255 } |
256 | |
257 | |
258 if (!xd->started) { | |
259 xd->rxqueue = g_realloc(xd->rxqueue, len + xd->rxlen); | |
260 memcpy(xd->rxqueue + xd->rxlen, buf, len); | |
261 xd->rxlen += len; | |
262 | |
263 length = g_strstr_len(xd->rxqueue, len, "Content-length:"); | |
10579 | 264 /* some proxies re-write this header, changing the capitalization :( |
265 * technically that's allowed since headers are case-insensitive | |
266 * [RFC 2616, section 4.2] */ | |
267 if (length == NULL) | |
268 length = g_strstr_len(xd->rxqueue, len, "Content-Length:"); | |
7651 | 269 if (length) { |
270 end = g_strstr_len(length, length - xd->rxqueue, "\r\n"); | |
271 if (!end) | |
272 return 0; | |
7710 | 273 if ((filelen = calculate_length(length, len - (length - xd->rxqueue)))) |
274 gaim_xfer_set_size(xfer, filelen); | |
7651 | 275 } |
276 start = g_strstr_len(xd->rxqueue, len, "\r\n\r\n"); | |
277 if (start) | |
278 start += 4; | |
279 if (!start || start > (xd->rxqueue + len)) | |
280 return 0; | |
281 xd->started = TRUE; | |
282 | |
283 len -= (start - xd->rxqueue); | |
284 | |
285 *buffer = g_malloc(len); | |
286 memcpy(*buffer, start, len); | |
287 g_free(xd->rxqueue); | |
288 xd->rxqueue = NULL; | |
289 xd->rxlen = 0; | |
290 } else { | |
291 *buffer = g_malloc(len); | |
292 memcpy(*buffer, buf, len); | |
293 } | |
294 | |
295 return len; | |
296 } | |
297 | |
8231
f50c059b6384
[gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
8046
diff
changeset
|
298 ssize_t yahoo_xfer_write(const char *buffer, size_t size, GaimXfer *xfer) |
7651 | 299 { |
7710 | 300 ssize_t len; |
7651 | 301 struct yahoo_xfer_data *xd = xfer->data; |
302 | |
303 if (!xd) | |
9798 | 304 return -1; |
7651 | 305 |
306 if (gaim_xfer_get_type(xfer) != GAIM_XFER_SEND) { | |
9798 | 307 return -1; |
7651 | 308 } |
309 | |
310 len = write(xfer->fd, buffer, size); | |
311 | |
7710 | 312 if (len == -1) { |
313 if (gaim_xfer_get_bytes_sent(xfer) >= gaim_xfer_get_size(xfer)) | |
314 gaim_xfer_set_completed(xfer, TRUE); | |
315 if ((errno != EAGAIN) && (errno != EINTR)) | |
9798 | 316 return -1; |
7710 | 317 return 0; |
318 } | |
319 | |
320 if ((gaim_xfer_get_bytes_sent(xfer) + len) >= gaim_xfer_get_size(xfer)) | |
7651 | 321 gaim_xfer_set_completed(xfer, TRUE); |
322 | |
323 return len; | |
324 } | |
325 | |
326 static void yahoo_xfer_cancel_send(GaimXfer *xfer) | |
327 { | |
328 GaimAccount *account; | |
329 struct yahoo_xfer_data *xfer_data; | |
330 | |
331 xfer_data = xfer->data; | |
332 account = gaim_xfer_get_account(xfer); | |
333 | |
334 if (xfer_data) | |
335 yahoo_xfer_data_free(xfer_data); | |
336 xfer->data = NULL; | |
337 } | |
338 | |
339 static void yahoo_xfer_cancel_recv(GaimXfer *xfer) | |
340 { | |
341 GaimAccount *account; | |
342 struct yahoo_xfer_data *xfer_data; | |
343 | |
344 account = gaim_xfer_get_account(xfer); | |
345 xfer_data = xfer->data; | |
346 | |
347 if (xfer_data) | |
348 yahoo_xfer_data_free(xfer_data); | |
349 xfer->data = NULL; | |
350 } | |
351 | |
352 void yahoo_process_filetransfer(GaimConnection *gc, struct yahoo_packet *pkt) | |
353 { | |
354 char *from = NULL; | |
355 char *to = NULL; | |
356 char *msg = NULL; | |
357 char *url = NULL; | |
358 long expires = 0; | |
359 GaimXfer *xfer; | |
360 struct yahoo_xfer_data *xfer_data; | |
361 | |
362 char *service = NULL; | |
363 | |
364 char *filename = NULL; | |
365 unsigned long filesize = 0L; | |
366 | |
367 GSList *l; | |
368 | |
369 for (l = pkt->hash; l; l = l->next) { | |
370 struct yahoo_pair *pair = l->data; | |
371 | |
372 if (pair->key == 4) | |
373 from = pair->value; | |
374 if (pair->key == 5) | |
375 to = pair->value; | |
376 if (pair->key == 14) | |
377 msg = pair->value; | |
378 if (pair->key == 20) | |
379 url = pair->value; | |
380 if (pair->key == 38) | |
381 expires = strtol(pair->value, NULL, 10); | |
382 | |
383 if (pair->key == 27) | |
384 filename = pair->value; | |
385 if (pair->key == 28) | |
386 filesize = atol(pair->value); | |
387 | |
388 if (pair->key == 49) | |
389 service = pair->value; | |
390 } | |
391 | |
392 if (pkt->service == YAHOO_SERVICE_P2PFILEXFER) { | |
10261 | 393 if (service && (strcmp("FILEXFER", service) != 0)) { |
7651 | 394 gaim_debug_misc("yahoo", "unhandled service 0x%02x", pkt->service); |
395 return; | |
396 } | |
397 } | |
398 | |
399 if (msg) { | |
400 char *tmp; | |
401 tmp = strchr(msg, '\006'); | |
402 if (tmp) | |
403 *tmp = '\0'; | |
404 } | |
405 | |
406 if (!url || !from) | |
407 return; | |
408 | |
409 | |
410 /* Setup the Yahoo-specific file transfer data */ | |
411 xfer_data = g_new0(struct yahoo_xfer_data, 1); | |
412 xfer_data->gc = gc; | |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9164
diff
changeset
|
413 if (!gaim_url_parse(url, &(xfer_data->host), &(xfer_data->port), &(xfer_data->path), NULL, NULL)) { |
7651 | 414 g_free(xfer_data); |
415 return; | |
416 } | |
417 | |
418 gaim_debug_misc("yahoo_filexfer", "Host is %s, port is %d, path is %s, and the full url was %s.\n", | |
419 xfer_data->host, xfer_data->port, xfer_data->path, url); | |
420 | |
421 /* Build the file transfer handle. */ | |
422 xfer = gaim_xfer_new(gc->account, GAIM_XFER_RECEIVE, from); | |
423 xfer->data = xfer_data; | |
424 | |
425 /* Set the info about the incoming file. */ | |
426 if (filename) | |
427 gaim_xfer_set_filename(xfer, filename); | |
428 else { | |
429 gchar *start, *end; | |
430 start = g_strrstr(xfer_data->path, "/"); | |
431 if (start) | |
432 start++; | |
433 end = g_strrstr(xfer_data->path, "?"); | |
434 if (start && *start && end) { | |
435 filename = g_strndup(start, end - start); | |
436 gaim_xfer_set_filename(xfer, filename); | |
437 g_free(filename); | |
438 filename = NULL; | |
439 } | |
440 } | |
441 | |
442 gaim_xfer_set_size(xfer, filesize); | |
443 | |
444 /* Setup our I/O op functions */ | |
445 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init); | |
446 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start); | |
447 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end); | |
448 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send); | |
449 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv); | |
450 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read); | |
451 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write); | |
452 | |
453 /* Now perform the request */ | |
454 gaim_xfer_request(xfer); | |
455 } | |
456 | |
457 void yahoo_send_file(GaimConnection *gc, const char *who, const char *file) | |
458 { | |
459 GaimXfer *xfer; | |
460 struct yahoo_xfer_data *xfer_data; | |
461 | |
9466 | 462 if (!who) |
7651 | 463 return; |
464 | |
465 xfer_data = g_new0(struct yahoo_xfer_data, 1); | |
466 xfer_data->gc = gc; | |
467 | |
468 | |
469 /* Build the file transfer handle. */ | |
470 xfer = gaim_xfer_new(gc->account, GAIM_XFER_SEND, who); | |
471 xfer->data = xfer_data; | |
472 | |
473 /* Setup our I/O op functions */ | |
474 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init); | |
475 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start); | |
476 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end); | |
477 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send); | |
478 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv); | |
479 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read); | |
480 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write); | |
481 | |
482 /* Now perform the request */ | |
9466 | 483 if (file) |
484 gaim_xfer_request_accepted(xfer, file); | |
485 else | |
486 gaim_xfer_request(xfer); | |
7651 | 487 } |