Mercurial > pidgin
annotate src/protocols/yahoo/yahoo_filexfer.c @ 10576:6c6772d3ea31
[gaim-migrate @ 11970]
Fix setting a buddy icon when using an HTTP proxy
committer: Tailor Script <tailor@pidgin.im>
author | Stu Tomlinson <stu@nosnilmot.com> |
---|---|
date | Sat, 05 Feb 2005 18:41:30 +0000 |
parents | 96307e1a47c9 |
children | 71478e6a6074 |
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) { | |
69 gaim_xfer_error(GAIM_XFER_RECEIVE, xfer->who, _("Unable to connect.")); | |
7805 | 70 gaim_xfer_cancel_remote(xfer); |
7651 | 71 return; |
72 } | |
73 | |
74 xfer->fd = source; | |
75 gaim_xfer_start(xfer, source, NULL, 0); | |
76 | |
10575 | 77 buf = g_strdup_printf("GET http://%s:%d/%s HTTP/1.0\r\nHost: %s\r\n\r\n", |
78 xd->host, xd->port, xd->path, xd->host); | |
7651 | 79 write(xfer->fd, buf, strlen(buf)); |
80 g_free(buf); | |
81 | |
82 return; | |
83 } | |
84 | |
85 static void yahoo_sendfile_connected(gpointer data, gint source, GaimInputCondition condition) | |
86 { | |
87 GaimXfer *xfer; | |
88 struct yahoo_xfer_data *xd; | |
89 struct yahoo_packet *pkt; | |
90 gchar *size, *post, *buf; | |
10576 | 91 const char *host; |
10575 | 92 int content_length, port; |
7651 | 93 GaimConnection *gc; |
94 GaimAccount *account; | |
95 struct yahoo_data *yd; | |
7805 | 96 char *filename; |
7651 | 97 |
98 gaim_debug(GAIM_DEBUG_INFO, "yahoo", | |
99 "AAA - in yahoo_sendfile_connected\n"); | |
100 if (!(xfer = data)) | |
101 return; | |
102 if (!(xd = xfer->data)) | |
103 return; | |
104 | |
105 gc = xd->gc; | |
106 account = gaim_connection_get_account(gc); | |
107 yd = gc->proto_data; | |
108 | |
109 | |
110 | |
111 if (source < 0) { | |
112 gaim_xfer_error(GAIM_XFER_RECEIVE, xfer->who, _("Unable to connect.")); | |
7805 | 113 gaim_xfer_cancel_remote(xfer); |
7651 | 114 return; |
115 } | |
116 | |
117 xfer->fd = source; | |
118 gaim_xfer_start(xfer, source, NULL, 0); | |
119 | |
120 | |
121 pkt = yahoo_packet_new(YAHOO_SERVICE_FILETRANSFER, YAHOO_STATUS_AVAILABLE, yd->session_id); | |
122 | |
10111 | 123 size = g_strdup_printf("%" G_GSIZE_FORMAT, gaim_xfer_get_size(xfer)); |
7805 | 124 filename = g_path_get_basename(gaim_xfer_get_local_filename(xfer)); |
10394 | 125 |
126 yahoo_packet_hash(pkt, "sssss", 0, gaim_connection_get_display_name(gc), | |
127 5, xfer->who, 14, "", 27, filename, 28, size); | |
7651 | 128 |
129 content_length = YAHOO_PACKET_HDRLEN + yahoo_packet_length(pkt); | |
130 | |
131 buf = g_strdup_printf("Y=%s; T=%s", yd->cookie_y, yd->cookie_t); | |
132 | |
10576 | 133 host = gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST); |
10575 | 134 port = gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT); |
135 post = g_strdup_printf("POST http://%s:%d/notifyft HTTP/1.0\r\n" | |
10111 | 136 "Content-length: %" G_GSIZE_FORMAT "\r\n" |
7651 | 137 "Host: %s:%d\r\n" |
138 "Cookie: %s\r\n" | |
139 "\r\n", | |
10576 | 140 host, port, content_length + 4 + gaim_xfer_get_size(xfer), host, port, buf); |
7651 | 141 write(xfer->fd, post, strlen(post)); |
142 | |
10392 | 143 yahoo_packet_send_special(pkt, xfer->fd, 8); |
7651 | 144 yahoo_packet_free(pkt); |
145 | |
146 write(xfer->fd, "29\xc0\x80", 4); | |
147 | |
148 g_free(size); | |
149 g_free(post); | |
150 g_free(buf); | |
7805 | 151 g_free(filename); |
7651 | 152 } |
153 | |
154 static void yahoo_xfer_init(GaimXfer *xfer) | |
155 { | |
156 struct yahoo_xfer_data *xfer_data; | |
157 GaimConnection *gc; | |
158 GaimAccount *account; | |
7827 | 159 struct yahoo_data *yd; |
7651 | 160 |
161 xfer_data = xfer->data; | |
162 gc = xfer_data->gc; | |
7827 | 163 yd = gc->proto_data; |
7651 | 164 account = gaim_connection_get_account(gc); |
165 | |
166 if (gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) { | |
10361 | 167 if (0 && gaim_xfer_get_size(xfer) >= 1048577) { |
7651 | 168 gaim_notify_error(gc, NULL, _("File Transfer Aborted"), |
8282 | 169 _("Gaim cannot send files over Yahoo! that are bigger than " |
170 "One Megabyte (1,048,576 bytes).")); | |
171 gaim_xfer_cancel_local(xfer); | |
172 } else { | |
9164 | 173 if (yd->jp) { |
174 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xferjp_host", YAHOOJP_XFER_HOST), | |
175 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT), | |
176 yahoo_sendfile_connected, xfer) == -1) | |
177 { | |
178 gaim_notify_error(gc, NULL, _("File Transfer Aborted"), | |
179 _("Unable to establish file descriptor.")); | |
180 gaim_xfer_cancel_remote(xfer); | |
181 } | |
182 } else { | |
183 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST), | |
184 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT), | |
185 yahoo_sendfile_connected, xfer) == -1) | |
186 { | |
187 gaim_notify_error(gc, NULL, _("File Transfer Aborted"), | |
188 _("Unable to establish file descriptor.")); | |
189 gaim_xfer_cancel_remote(xfer); | |
190 } | |
8282 | 191 } |
7651 | 192 } |
193 } else { | |
194 xfer->fd = gaim_proxy_connect(account, xfer_data->host, xfer_data->port, | |
195 yahoo_receivefile_connected, xfer); | |
196 if (xfer->fd == -1) { | |
197 gaim_notify_error(gc, NULL, _("File Transfer Aborted"), | |
198 _("Unable to establish file descriptor.")); | |
199 gaim_xfer_cancel_remote(xfer); | |
200 } | |
201 } | |
202 } | |
203 | |
204 static void yahoo_xfer_start(GaimXfer *xfer) | |
205 { | |
206 /* We don't need to do anything here, do we? */ | |
207 } | |
208 | |
209 static void yahoo_xfer_end(GaimXfer *xfer) | |
210 { | |
211 GaimAccount *account; | |
212 struct yahoo_xfer_data *xfer_data; | |
213 | |
214 account = gaim_xfer_get_account(xfer); | |
215 xfer_data = xfer->data; | |
216 | |
217 | |
218 if (xfer_data) | |
219 yahoo_xfer_data_free(xfer_data); | |
220 xfer->data = NULL; | |
221 | |
222 } | |
223 | |
224 guint calculate_length(const gchar *l, size_t len) | |
225 { | |
226 int i; | |
227 | |
228 for (i = 0; i < len; i++) { | |
229 if (!g_ascii_isdigit(l[i])) | |
230 continue; | |
231 return strtol(l + i, NULL, 10); | |
232 } | |
233 return 0; | |
234 } | |
235 | |
236 | |
8231
f50c059b6384
[gaim-migrate @ 8954]
Christian Hammond <chipx86@chipx86.com>
parents:
8046
diff
changeset
|
237 ssize_t yahoo_xfer_read(char **buffer, GaimXfer *xfer) |
7651 | 238 { |
7710 | 239 gchar buf[4096]; |
7682 | 240 ssize_t len; |
7651 | 241 gchar *start = NULL; |
242 gchar *length; | |
243 gchar *end; | |
7710 | 244 int filelen; |
7651 | 245 struct yahoo_xfer_data *xd = xfer->data; |
246 | |
247 if (gaim_xfer_get_type(xfer) != GAIM_XFER_RECEIVE) { | |
248 return 0; | |
249 } | |
250 | |
251 len = read(xfer->fd, buf, sizeof(buf)); | |
252 | |
7682 | 253 if (len <= 0) { |
7710 | 254 if ((gaim_xfer_get_size(xfer) > 0) && |
9798 | 255 (gaim_xfer_get_bytes_sent(xfer) >= gaim_xfer_get_size(xfer))) { |
7682 | 256 gaim_xfer_set_completed(xfer, TRUE); |
9798 | 257 return 0; |
258 } else | |
259 return -1; | |
7651 | 260 } |
261 | |
262 | |
263 if (!xd->started) { | |
264 xd->rxqueue = g_realloc(xd->rxqueue, len + xd->rxlen); | |
265 memcpy(xd->rxqueue + xd->rxlen, buf, len); | |
266 xd->rxlen += len; | |
267 | |
268 length = g_strstr_len(xd->rxqueue, len, "Content-length:"); | |
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 } |