Mercurial > pidgin.yaz
annotate src/protocols/yahoo/yahoo_filexfer.c @ 10976:be22eb8fa671
[gaim-migrate @ 12802]
Fix encoding of filename
committer: Tailor Script <tailor@pidgin.im>
author | Daniel Atallah <daniel.atallah@gmail.com> |
---|---|
date | Mon, 06 Jun 2005 22:38:11 +0000 |
parents | ca3e0882d377 |
children | bd8ac1d4b2f2 |
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; | |
10976
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
97 char *filename, *encoded_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)); |
10976
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
127 encoded_filename = yahoo_string_encode(gc, filename, NULL); |
10394 | 128 |
129 yahoo_packet_hash(pkt, "sssss", 0, gaim_connection_get_display_name(gc), | |
10976
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
130 5, xfer->who, 14, "", 27, encoded_filename, 28, size); |
7651 | 131 |
132 content_length = YAHOO_PACKET_HDRLEN + yahoo_packet_length(pkt); | |
133 | |
134 buf = g_strdup_printf("Y=%s; T=%s", yd->cookie_y, yd->cookie_t); | |
135 | |
10576 | 136 host = gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST); |
10575 | 137 port = gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT); |
138 post = g_strdup_printf("POST http://%s:%d/notifyft HTTP/1.0\r\n" | |
10111 | 139 "Content-length: %" G_GSIZE_FORMAT "\r\n" |
7651 | 140 "Host: %s:%d\r\n" |
141 "Cookie: %s\r\n" | |
142 "\r\n", | |
10576 | 143 host, port, content_length + 4 + gaim_xfer_get_size(xfer), host, port, buf); |
7651 | 144 write(xfer->fd, post, strlen(post)); |
145 | |
10392 | 146 yahoo_packet_send_special(pkt, xfer->fd, 8); |
7651 | 147 yahoo_packet_free(pkt); |
148 | |
149 write(xfer->fd, "29\xc0\x80", 4); | |
150 | |
151 g_free(size); | |
152 g_free(post); | |
153 g_free(buf); | |
10976
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
154 g_free(encoded_filename); |
7805 | 155 g_free(filename); |
7651 | 156 } |
157 | |
158 static void yahoo_xfer_init(GaimXfer *xfer) | |
159 { | |
160 struct yahoo_xfer_data *xfer_data; | |
161 GaimConnection *gc; | |
162 GaimAccount *account; | |
7827 | 163 struct yahoo_data *yd; |
7651 | 164 |
165 xfer_data = xfer->data; | |
166 gc = xfer_data->gc; | |
7827 | 167 yd = gc->proto_data; |
7651 | 168 account = gaim_connection_get_account(gc); |
169 | |
170 if (gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) { | |
10937 | 171 if (yd->jp) { |
172 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xferjp_host", YAHOOJP_XFER_HOST), | |
173 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT), | |
174 yahoo_sendfile_connected, xfer) == -1) | |
175 { | |
176 gaim_notify_error(gc, NULL, _("File Transfer Failed"), | |
177 _("Unable to establish file descriptor.")); | |
178 gaim_xfer_cancel_remote(xfer); | |
179 } | |
8282 | 180 } else { |
10937 | 181 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST), |
182 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT), | |
183 yahoo_sendfile_connected, xfer) == -1) | |
184 { | |
185 gaim_notify_error(gc, NULL, _("File Transfer Failed"), | |
186 _("Unable to establish file descriptor.")); | |
187 gaim_xfer_cancel_remote(xfer); | |
8282 | 188 } |
7651 | 189 } |
190 } else { | |
191 xfer->fd = gaim_proxy_connect(account, xfer_data->host, xfer_data->port, | |
192 yahoo_receivefile_connected, xfer); | |
193 if (xfer->fd == -1) { | |
10654 | 194 gaim_notify_error(gc, NULL, _("File Transfer Failed"), |
7651 | 195 _("Unable to establish file descriptor.")); |
196 gaim_xfer_cancel_remote(xfer); | |
197 } | |
198 } | |
199 } | |
200 | |
201 static void yahoo_xfer_start(GaimXfer *xfer) | |
202 { | |
203 /* We don't need to do anything here, do we? */ | |
204 } | |
205 | |
206 static void yahoo_xfer_end(GaimXfer *xfer) | |
207 { | |
208 GaimAccount *account; | |
209 struct yahoo_xfer_data *xfer_data; | |
210 | |
211 account = gaim_xfer_get_account(xfer); | |
212 xfer_data = xfer->data; | |
213 | |
214 | |
215 if (xfer_data) | |
216 yahoo_xfer_data_free(xfer_data); | |
217 xfer->data = NULL; | |
218 | |
219 } | |
220 | |
221 guint calculate_length(const gchar *l, size_t len) | |
222 { | |
223 int i; | |
224 | |
225 for (i = 0; i < len; i++) { | |
226 if (!g_ascii_isdigit(l[i])) | |
227 continue; | |
228 return strtol(l + i, NULL, 10); | |
229 } | |
230 return 0; | |
231 } | |
232 | |
233 | |
8231
f50c059b6384
[gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
8046
diff
changeset
|
234 ssize_t yahoo_xfer_read(char **buffer, GaimXfer *xfer) |
7651 | 235 { |
7710 | 236 gchar buf[4096]; |
7682 | 237 ssize_t len; |
7651 | 238 gchar *start = NULL; |
239 gchar *length; | |
240 gchar *end; | |
7710 | 241 int filelen; |
7651 | 242 struct yahoo_xfer_data *xd = xfer->data; |
243 | |
244 if (gaim_xfer_get_type(xfer) != GAIM_XFER_RECEIVE) { | |
245 return 0; | |
246 } | |
247 | |
248 len = read(xfer->fd, buf, sizeof(buf)); | |
249 | |
7682 | 250 if (len <= 0) { |
7710 | 251 if ((gaim_xfer_get_size(xfer) > 0) && |
9798 | 252 (gaim_xfer_get_bytes_sent(xfer) >= gaim_xfer_get_size(xfer))) { |
7682 | 253 gaim_xfer_set_completed(xfer, TRUE); |
9798 | 254 return 0; |
255 } else | |
256 return -1; | |
7651 | 257 } |
258 | |
259 | |
260 if (!xd->started) { | |
261 xd->rxqueue = g_realloc(xd->rxqueue, len + xd->rxlen); | |
262 memcpy(xd->rxqueue + xd->rxlen, buf, len); | |
263 xd->rxlen += len; | |
264 | |
265 length = g_strstr_len(xd->rxqueue, len, "Content-length:"); | |
10579 | 266 /* some proxies re-write this header, changing the capitalization :( |
267 * technically that's allowed since headers are case-insensitive | |
268 * [RFC 2616, section 4.2] */ | |
269 if (length == NULL) | |
270 length = g_strstr_len(xd->rxqueue, len, "Content-Length:"); | |
7651 | 271 if (length) { |
272 end = g_strstr_len(length, length - xd->rxqueue, "\r\n"); | |
273 if (!end) | |
274 return 0; | |
7710 | 275 if ((filelen = calculate_length(length, len - (length - xd->rxqueue)))) |
276 gaim_xfer_set_size(xfer, filelen); | |
7651 | 277 } |
278 start = g_strstr_len(xd->rxqueue, len, "\r\n\r\n"); | |
279 if (start) | |
280 start += 4; | |
281 if (!start || start > (xd->rxqueue + len)) | |
282 return 0; | |
283 xd->started = TRUE; | |
284 | |
285 len -= (start - xd->rxqueue); | |
286 | |
287 *buffer = g_malloc(len); | |
288 memcpy(*buffer, start, len); | |
289 g_free(xd->rxqueue); | |
290 xd->rxqueue = NULL; | |
291 xd->rxlen = 0; | |
292 } else { | |
293 *buffer = g_malloc(len); | |
294 memcpy(*buffer, buf, len); | |
295 } | |
296 | |
297 return len; | |
298 } | |
299 | |
8231
f50c059b6384
[gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
8046
diff
changeset
|
300 ssize_t yahoo_xfer_write(const char *buffer, size_t size, GaimXfer *xfer) |
7651 | 301 { |
7710 | 302 ssize_t len; |
7651 | 303 struct yahoo_xfer_data *xd = xfer->data; |
304 | |
305 if (!xd) | |
9798 | 306 return -1; |
7651 | 307 |
308 if (gaim_xfer_get_type(xfer) != GAIM_XFER_SEND) { | |
9798 | 309 return -1; |
7651 | 310 } |
311 | |
312 len = write(xfer->fd, buffer, size); | |
313 | |
7710 | 314 if (len == -1) { |
315 if (gaim_xfer_get_bytes_sent(xfer) >= gaim_xfer_get_size(xfer)) | |
316 gaim_xfer_set_completed(xfer, TRUE); | |
317 if ((errno != EAGAIN) && (errno != EINTR)) | |
9798 | 318 return -1; |
7710 | 319 return 0; |
320 } | |
321 | |
322 if ((gaim_xfer_get_bytes_sent(xfer) + len) >= gaim_xfer_get_size(xfer)) | |
7651 | 323 gaim_xfer_set_completed(xfer, TRUE); |
324 | |
325 return len; | |
326 } | |
327 | |
328 static void yahoo_xfer_cancel_send(GaimXfer *xfer) | |
329 { | |
330 GaimAccount *account; | |
331 struct yahoo_xfer_data *xfer_data; | |
332 | |
333 xfer_data = xfer->data; | |
334 account = gaim_xfer_get_account(xfer); | |
335 | |
336 if (xfer_data) | |
337 yahoo_xfer_data_free(xfer_data); | |
338 xfer->data = NULL; | |
339 } | |
340 | |
341 static void yahoo_xfer_cancel_recv(GaimXfer *xfer) | |
342 { | |
343 GaimAccount *account; | |
344 struct yahoo_xfer_data *xfer_data; | |
345 | |
346 account = gaim_xfer_get_account(xfer); | |
347 xfer_data = xfer->data; | |
348 | |
349 if (xfer_data) | |
350 yahoo_xfer_data_free(xfer_data); | |
351 xfer->data = NULL; | |
352 } | |
353 | |
354 void yahoo_process_filetransfer(GaimConnection *gc, struct yahoo_packet *pkt) | |
355 { | |
356 char *from = NULL; | |
357 char *to = NULL; | |
358 char *msg = NULL; | |
359 char *url = NULL; | |
360 long expires = 0; | |
361 GaimXfer *xfer; | |
362 struct yahoo_xfer_data *xfer_data; | |
363 | |
364 char *service = NULL; | |
365 | |
366 char *filename = NULL; | |
367 unsigned long filesize = 0L; | |
368 | |
369 GSList *l; | |
370 | |
371 for (l = pkt->hash; l; l = l->next) { | |
372 struct yahoo_pair *pair = l->data; | |
373 | |
374 if (pair->key == 4) | |
375 from = pair->value; | |
376 if (pair->key == 5) | |
377 to = pair->value; | |
378 if (pair->key == 14) | |
379 msg = pair->value; | |
380 if (pair->key == 20) | |
381 url = pair->value; | |
382 if (pair->key == 38) | |
383 expires = strtol(pair->value, NULL, 10); | |
384 | |
385 if (pair->key == 27) | |
386 filename = pair->value; | |
387 if (pair->key == 28) | |
388 filesize = atol(pair->value); | |
389 | |
390 if (pair->key == 49) | |
391 service = pair->value; | |
392 } | |
393 | |
394 if (pkt->service == YAHOO_SERVICE_P2PFILEXFER) { | |
10261 | 395 if (service && (strcmp("FILEXFER", service) != 0)) { |
7651 | 396 gaim_debug_misc("yahoo", "unhandled service 0x%02x", pkt->service); |
397 return; | |
398 } | |
399 } | |
400 | |
401 if (msg) { | |
402 char *tmp; | |
403 tmp = strchr(msg, '\006'); | |
404 if (tmp) | |
405 *tmp = '\0'; | |
406 } | |
407 | |
408 if (!url || !from) | |
409 return; | |
410 | |
411 | |
412 /* Setup the Yahoo-specific file transfer data */ | |
413 xfer_data = g_new0(struct yahoo_xfer_data, 1); | |
414 xfer_data->gc = gc; | |
9227
9171e528d7e5
[gaim-migrate @ 10023]
Christian Hammond <chipx86@chipx86.com>
parents:
9164
diff
changeset
|
415 if (!gaim_url_parse(url, &(xfer_data->host), &(xfer_data->port), &(xfer_data->path), NULL, NULL)) { |
7651 | 416 g_free(xfer_data); |
417 return; | |
418 } | |
419 | |
420 gaim_debug_misc("yahoo_filexfer", "Host is %s, port is %d, path is %s, and the full url was %s.\n", | |
421 xfer_data->host, xfer_data->port, xfer_data->path, url); | |
422 | |
423 /* Build the file transfer handle. */ | |
424 xfer = gaim_xfer_new(gc->account, GAIM_XFER_RECEIVE, from); | |
425 xfer->data = xfer_data; | |
426 | |
427 /* Set the info about the incoming file. */ | |
10976
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
428 if (filename) { |
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
429 char *utf8_filename = yahoo_string_decode(gc, filename, TRUE); |
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
430 gaim_xfer_set_filename(xfer, utf8_filename); |
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
431 g_free(utf8_filename); |
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
432 } else { |
7651 | 433 gchar *start, *end; |
434 start = g_strrstr(xfer_data->path, "/"); | |
435 if (start) | |
436 start++; | |
437 end = g_strrstr(xfer_data->path, "?"); | |
438 if (start && *start && end) { | |
10976
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
439 char *utf8_filename; |
7651 | 440 filename = g_strndup(start, end - start); |
10976
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
441 utf8_filename = yahoo_string_decode(gc, filename, TRUE); |
7651 | 442 g_free(filename); |
10976
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
443 gaim_xfer_set_filename(xfer, utf8_filename); |
be22eb8fa671
[gaim-migrate @ 12802]
Daniel Atallah <daniel.atallah@gmail.com>
parents:
10937
diff
changeset
|
444 g_free(utf8_filename); |
7651 | 445 filename = NULL; |
446 } | |
447 } | |
448 | |
449 gaim_xfer_set_size(xfer, filesize); | |
450 | |
451 /* Setup our I/O op functions */ | |
452 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init); | |
453 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start); | |
454 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end); | |
455 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send); | |
456 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv); | |
457 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read); | |
458 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write); | |
459 | |
460 /* Now perform the request */ | |
461 gaim_xfer_request(xfer); | |
462 } | |
463 | |
464 void yahoo_send_file(GaimConnection *gc, const char *who, const char *file) | |
465 { | |
466 GaimXfer *xfer; | |
467 struct yahoo_xfer_data *xfer_data; | |
468 | |
9466 | 469 if (!who) |
7651 | 470 return; |
471 | |
472 xfer_data = g_new0(struct yahoo_xfer_data, 1); | |
473 xfer_data->gc = gc; | |
474 | |
475 | |
476 /* Build the file transfer handle. */ | |
477 xfer = gaim_xfer_new(gc->account, GAIM_XFER_SEND, who); | |
478 xfer->data = xfer_data; | |
479 | |
480 /* Setup our I/O op functions */ | |
481 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init); | |
482 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start); | |
483 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end); | |
484 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send); | |
485 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv); | |
486 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read); | |
487 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write); | |
488 | |
489 /* Now perform the request */ | |
9466 | 490 if (file) |
491 gaim_xfer_request_accepted(xfer, file); | |
492 else | |
493 gaim_xfer_request(xfer); | |
7651 | 494 } |