7651
|
1 /*
|
|
2 * @file yahoo_filexfer.c Yahoo Filetransfer
|
|
3 *
|
|
4 *
|
|
5 * Copyright (C) 2003 Timothy T Ringenbach <omarvo@hotmail.com>
|
|
6 * Some code borrowed from MSN and copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org>
|
|
7 * Some code copyright (C) 2002, Philip S Tellis <philip . tellis AT gmx . net>
|
|
8 *
|
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
13 * (at your option) any later version.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; if not, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
23 */
|
|
24
|
|
25 #include "prpl.h"
|
|
26 #include "internal.h"
|
|
27 #include "util.h"
|
|
28 #include "debug.h"
|
|
29 #include "notify.h"
|
|
30 #include "proxy.h"
|
|
31 #include "ft.h"
|
|
32 #include "yahoo.h"
|
|
33 #include "yahoo_filexfer.h"
|
|
34
|
|
35
|
|
36
|
|
37 struct yahoo_xfer_data {
|
|
38 gchar *host;
|
|
39 gchar *path;
|
|
40 int port;
|
|
41 GaimConnection *gc;
|
|
42 long expires;
|
|
43 gboolean started;
|
|
44 gchar *rxqueue;
|
|
45 guint rxlen;
|
|
46 };
|
|
47
|
|
48 static void yahoo_xfer_data_free(struct yahoo_xfer_data *xd)
|
|
49 {
|
|
50 if (xd->host)
|
|
51 g_free(xd->host);
|
|
52 if (xd->path)
|
|
53 g_free(xd->path);
|
|
54 g_free(xd);
|
|
55 }
|
|
56
|
|
57 static void yahoo_receivefile_connected(gpointer data, gint source, GaimInputCondition condition)
|
|
58 {
|
|
59 GaimXfer *xfer;
|
|
60 struct yahoo_xfer_data *xd;
|
|
61 gchar *buf;
|
|
62
|
|
63 gaim_debug(GAIM_DEBUG_INFO, "yahoo",
|
|
64 "AAA - in yahoo_receivefile_connected\n");
|
|
65 if (!(xfer = data))
|
|
66 return;
|
|
67 if (!(xd = xfer->data))
|
|
68 return;
|
|
69 if (source < 0) {
|
|
70 gaim_xfer_error(GAIM_XFER_RECEIVE, 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
|
|
78 buf = g_strdup_printf("GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n", xd->path, xd->host);
|
|
79 write(xfer->fd, buf, strlen(buf));
|
|
80 g_free(buf);
|
|
81
|
|
82 return;
|
|
83 }
|
|
84
|
|
85 static int yahoo_send_packet_special(int fd, struct yahoo_packet *pkt, int pad)
|
|
86 {
|
|
87 int pktlen = yahoo_packet_length(pkt);
|
|
88 int len = YAHOO_PACKET_HDRLEN + pktlen;
|
|
89 int ret;
|
|
90
|
|
91 guchar *data;
|
|
92 int pos = 0;
|
|
93
|
|
94 if (fd < 0)
|
|
95 return -1;
|
|
96
|
|
97 data = g_malloc0(len + 1);
|
|
98
|
|
99 memcpy(data + pos, "YMSG", 4); pos += 4;
|
|
100 pos += yahoo_put16(data + pos, YAHOO_PROTO_VER);
|
|
101 pos += yahoo_put16(data + pos, 0x0000);
|
|
102 pos += yahoo_put16(data + pos, pktlen + pad);
|
|
103 pos += yahoo_put16(data + pos, pkt->service);
|
|
104 pos += yahoo_put32(data + pos, pkt->status);
|
|
105 pos += yahoo_put32(data + pos, pkt->id);
|
|
106
|
|
107 yahoo_packet_write(pkt, data + pos);
|
|
108
|
|
109 ret = write(fd, data, len);
|
|
110 g_free(data);
|
|
111
|
|
112 return ret;
|
|
113 }
|
|
114
|
|
115 static void yahoo_sendfile_connected(gpointer data, gint source, GaimInputCondition condition)
|
|
116 {
|
|
117 GaimXfer *xfer;
|
|
118 struct yahoo_xfer_data *xd;
|
|
119 struct yahoo_packet *pkt;
|
|
120 gchar *size, *post, *buf;
|
|
121 int content_length;
|
|
122 GaimConnection *gc;
|
|
123 GaimAccount *account;
|
|
124 struct yahoo_data *yd;
|
7805
|
125 char *filename;
|
7651
|
126
|
|
127 gaim_debug(GAIM_DEBUG_INFO, "yahoo",
|
|
128 "AAA - in yahoo_sendfile_connected\n");
|
|
129 if (!(xfer = data))
|
|
130 return;
|
|
131 if (!(xd = xfer->data))
|
|
132 return;
|
|
133
|
|
134 gc = xd->gc;
|
|
135 account = gaim_connection_get_account(gc);
|
|
136 yd = gc->proto_data;
|
|
137
|
|
138
|
|
139
|
|
140 if (source < 0) {
|
|
141 gaim_xfer_error(GAIM_XFER_RECEIVE, xfer->who, _("Unable to connect."));
|
7805
|
142 gaim_xfer_cancel_remote(xfer);
|
7651
|
143 return;
|
|
144 }
|
|
145
|
|
146 xfer->fd = source;
|
|
147 gaim_xfer_start(xfer, source, NULL, 0);
|
|
148
|
|
149
|
|
150 pkt = yahoo_packet_new(YAHOO_SERVICE_FILETRANSFER, YAHOO_STATUS_AVAILABLE, yd->session_id);
|
|
151
|
|
152 size = g_strdup_printf("%d", gaim_xfer_get_size(xfer));
|
|
153
|
|
154 yahoo_packet_hash(pkt, 0, gaim_connection_get_display_name(gc));
|
|
155 yahoo_packet_hash(pkt, 5, xfer->who);
|
|
156 yahoo_packet_hash(pkt, 14, "");
|
7805
|
157 filename = g_path_get_basename(gaim_xfer_get_local_filename(xfer));
|
|
158 yahoo_packet_hash(pkt, 27, filename);
|
7651
|
159 yahoo_packet_hash(pkt, 28, size);
|
|
160
|
|
161 content_length = YAHOO_PACKET_HDRLEN + yahoo_packet_length(pkt);
|
|
162
|
|
163 buf = g_strdup_printf("Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
|
|
164
|
|
165 post = g_strdup_printf("POST /notifyft HTTP/1.0\r\n"
|
|
166 "Content-length: %d\r\n"
|
|
167 "Host: %s:%d\r\n"
|
|
168 "Cookie: %s\r\n"
|
|
169 "\r\n",
|
|
170 content_length + 4 + gaim_xfer_get_size(xfer),
|
|
171 gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST),
|
|
172 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT),
|
|
173 buf);
|
|
174 write(xfer->fd, post, strlen(post));
|
|
175
|
|
176 yahoo_send_packet_special(xfer->fd, pkt, 8);
|
|
177 yahoo_packet_free(pkt);
|
|
178
|
|
179 write(xfer->fd, "29\xc0\x80", 4);
|
|
180
|
|
181 g_free(size);
|
|
182 g_free(post);
|
|
183 g_free(buf);
|
7805
|
184 g_free(filename);
|
7651
|
185 }
|
|
186
|
|
187 static void yahoo_xfer_init(GaimXfer *xfer)
|
|
188 {
|
|
189 struct yahoo_xfer_data *xfer_data;
|
|
190 GaimConnection *gc;
|
|
191 GaimAccount *account;
|
|
192
|
|
193 xfer_data = xfer->data;
|
|
194 gc = xfer_data->gc;
|
|
195 account = gaim_connection_get_account(gc);
|
|
196
|
|
197 if (gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) {
|
|
198 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST),
|
|
199 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT),
|
|
200 yahoo_sendfile_connected, xfer) == -1)
|
|
201 {
|
|
202 gaim_notify_error(gc, NULL, _("File Transfer Aborted"),
|
|
203 _("Unable to establish file descriptor."));
|
|
204 gaim_xfer_cancel_remote(xfer);
|
|
205 }
|
|
206
|
|
207 } else {
|
|
208 xfer->fd = gaim_proxy_connect(account, xfer_data->host, xfer_data->port,
|
|
209 yahoo_receivefile_connected, xfer);
|
|
210 if (xfer->fd == -1) {
|
|
211 gaim_notify_error(gc, NULL, _("File Transfer Aborted"),
|
|
212 _("Unable to establish file descriptor."));
|
|
213 gaim_xfer_cancel_remote(xfer);
|
|
214 }
|
|
215 }
|
|
216 }
|
|
217
|
|
218 static void yahoo_xfer_start(GaimXfer *xfer)
|
|
219 {
|
|
220 /* We don't need to do anything here, do we? */
|
|
221 }
|
|
222
|
|
223 static void yahoo_xfer_end(GaimXfer *xfer)
|
|
224 {
|
|
225 GaimAccount *account;
|
|
226 struct yahoo_xfer_data *xfer_data;
|
|
227
|
|
228 account = gaim_xfer_get_account(xfer);
|
|
229 xfer_data = xfer->data;
|
|
230
|
|
231
|
|
232 if (xfer_data)
|
|
233 yahoo_xfer_data_free(xfer_data);
|
|
234 xfer->data = NULL;
|
|
235
|
|
236 }
|
|
237
|
|
238 guint calculate_length(const gchar *l, size_t len)
|
|
239 {
|
|
240 int i;
|
|
241
|
|
242 for (i = 0; i < len; i++) {
|
|
243 if (!g_ascii_isdigit(l[i]))
|
|
244 continue;
|
|
245 return strtol(l + i, NULL, 10);
|
|
246 }
|
|
247 return 0;
|
|
248 }
|
|
249
|
|
250
|
|
251 size_t yahoo_xfer_read(char **buffer, GaimXfer *xfer)
|
|
252 {
|
7710
|
253 gchar buf[4096];
|
7682
|
254 ssize_t len;
|
7651
|
255 gchar *start = NULL;
|
|
256 gchar *length;
|
|
257 gchar *end;
|
7710
|
258 int filelen;
|
7651
|
259 struct yahoo_xfer_data *xd = xfer->data;
|
|
260
|
|
261 if (gaim_xfer_get_type(xfer) != GAIM_XFER_RECEIVE) {
|
|
262 return 0;
|
|
263 }
|
|
264
|
|
265 len = read(xfer->fd, buf, sizeof(buf));
|
|
266
|
7682
|
267 if (len <= 0) {
|
7710
|
268 if ((gaim_xfer_get_size(xfer) > 0) &&
|
|
269 (gaim_xfer_get_bytes_sent(xfer) >= gaim_xfer_get_size(xfer)))
|
7682
|
270 gaim_xfer_set_completed(xfer, TRUE);
|
7651
|
271 else
|
|
272 gaim_xfer_cancel_remote(xfer);
|
|
273 return 0;
|
|
274 }
|
|
275
|
|
276
|
|
277 if (!xd->started) {
|
|
278 xd->rxqueue = g_realloc(xd->rxqueue, len + xd->rxlen);
|
|
279 memcpy(xd->rxqueue + xd->rxlen, buf, len);
|
|
280 xd->rxlen += len;
|
|
281
|
|
282 length = g_strstr_len(xd->rxqueue, len, "Content-length:");
|
|
283 if (length) {
|
|
284 end = g_strstr_len(length, length - xd->rxqueue, "\r\n");
|
|
285 if (!end)
|
|
286 return 0;
|
7710
|
287 if ((filelen = calculate_length(length, len - (length - xd->rxqueue))))
|
|
288 gaim_xfer_set_size(xfer, filelen);
|
7651
|
289 }
|
|
290 start = g_strstr_len(xd->rxqueue, len, "\r\n\r\n");
|
|
291 if (start)
|
|
292 start += 4;
|
|
293 if (!start || start > (xd->rxqueue + len))
|
|
294 return 0;
|
|
295 xd->started = TRUE;
|
|
296
|
|
297 len -= (start - xd->rxqueue);
|
|
298
|
|
299 *buffer = g_malloc(len);
|
|
300 memcpy(*buffer, start, len);
|
|
301 g_free(xd->rxqueue);
|
|
302 xd->rxqueue = NULL;
|
|
303 xd->rxlen = 0;
|
|
304 } else {
|
|
305 *buffer = g_malloc(len);
|
|
306 memcpy(*buffer, buf, len);
|
|
307 }
|
|
308
|
|
309 return len;
|
|
310 }
|
|
311
|
|
312 size_t yahoo_xfer_write(const char *buffer, size_t size, GaimXfer *xfer)
|
|
313 {
|
7710
|
314 ssize_t len;
|
7651
|
315 struct yahoo_xfer_data *xd = xfer->data;
|
|
316
|
|
317 if (!xd)
|
|
318 return 0;
|
|
319
|
|
320 if (gaim_xfer_get_type(xfer) != GAIM_XFER_SEND) {
|
|
321 return 0;
|
|
322 }
|
|
323
|
|
324 len = write(xfer->fd, buffer, size);
|
|
325
|
7710
|
326 if (len == -1) {
|
|
327 if (gaim_xfer_get_bytes_sent(xfer) >= gaim_xfer_get_size(xfer))
|
|
328 gaim_xfer_set_completed(xfer, TRUE);
|
|
329 if ((errno != EAGAIN) && (errno != EINTR))
|
|
330 gaim_xfer_cancel_remote(xfer);
|
|
331 return 0;
|
|
332 }
|
|
333
|
|
334 if ((gaim_xfer_get_bytes_sent(xfer) + len) >= gaim_xfer_get_size(xfer))
|
7651
|
335 gaim_xfer_set_completed(xfer, TRUE);
|
|
336
|
|
337 return len;
|
|
338 }
|
|
339
|
|
340 static void yahoo_xfer_cancel_send(GaimXfer *xfer)
|
|
341 {
|
|
342 GaimAccount *account;
|
|
343 struct yahoo_xfer_data *xfer_data;
|
|
344
|
|
345 xfer_data = xfer->data;
|
|
346 account = gaim_xfer_get_account(xfer);
|
|
347
|
|
348 if (xfer_data)
|
|
349 yahoo_xfer_data_free(xfer_data);
|
|
350 xfer->data = NULL;
|
|
351 }
|
|
352
|
|
353 static void yahoo_xfer_cancel_recv(GaimXfer *xfer)
|
|
354 {
|
|
355 GaimAccount *account;
|
|
356 struct yahoo_xfer_data *xfer_data;
|
|
357
|
|
358 account = gaim_xfer_get_account(xfer);
|
|
359 xfer_data = xfer->data;
|
|
360
|
|
361 if (xfer_data)
|
|
362 yahoo_xfer_data_free(xfer_data);
|
|
363 xfer->data = NULL;
|
|
364 }
|
|
365
|
|
366 void yahoo_process_filetransfer(GaimConnection *gc, struct yahoo_packet *pkt)
|
|
367 {
|
|
368 char *from = NULL;
|
|
369 char *to = NULL;
|
|
370 char *msg = NULL;
|
|
371 char *url = NULL;
|
|
372 long expires = 0;
|
|
373 GaimXfer *xfer;
|
|
374 struct yahoo_xfer_data *xfer_data;
|
|
375
|
|
376 char *service = NULL;
|
|
377
|
|
378 char *filename = NULL;
|
|
379 unsigned long filesize = 0L;
|
|
380
|
|
381 GSList *l;
|
|
382
|
|
383 for (l = pkt->hash; l; l = l->next) {
|
|
384 struct yahoo_pair *pair = l->data;
|
|
385
|
|
386 if (pair->key == 4)
|
|
387 from = pair->value;
|
|
388 if (pair->key == 5)
|
|
389 to = pair->value;
|
|
390 if (pair->key == 14)
|
|
391 msg = pair->value;
|
|
392 if (pair->key == 20)
|
|
393 url = pair->value;
|
|
394 if (pair->key == 38)
|
|
395 expires = strtol(pair->value, NULL, 10);
|
|
396
|
|
397 if (pair->key == 27)
|
|
398 filename = pair->value;
|
|
399 if (pair->key == 28)
|
|
400 filesize = atol(pair->value);
|
|
401
|
|
402 if (pair->key == 49)
|
|
403 service = pair->value;
|
|
404 }
|
|
405
|
|
406 if (pkt->service == YAHOO_SERVICE_P2PFILEXFER) {
|
|
407 if (strcmp("FILEXFER", service) != 0) {
|
|
408 gaim_debug_misc("yahoo", "unhandled service 0x%02x", pkt->service);
|
|
409 return;
|
|
410 }
|
|
411 }
|
|
412
|
|
413 if (msg) {
|
|
414 char *tmp;
|
|
415 tmp = strchr(msg, '\006');
|
|
416 if (tmp)
|
|
417 *tmp = '\0';
|
|
418 }
|
|
419
|
|
420 if (!url || !from)
|
|
421 return;
|
|
422
|
|
423
|
|
424 /* Setup the Yahoo-specific file transfer data */
|
|
425 xfer_data = g_new0(struct yahoo_xfer_data, 1);
|
|
426 xfer_data->gc = gc;
|
|
427 if (!gaim_url_parse(url, &(xfer_data->host), &(xfer_data->port), &(xfer_data->path))) {
|
|
428 g_free(xfer_data);
|
|
429 return;
|
|
430 }
|
|
431
|
|
432 gaim_debug_misc("yahoo_filexfer", "Host is %s, port is %d, path is %s, and the full url was %s.\n",
|
|
433 xfer_data->host, xfer_data->port, xfer_data->path, url);
|
|
434
|
|
435 /* Build the file transfer handle. */
|
|
436 xfer = gaim_xfer_new(gc->account, GAIM_XFER_RECEIVE, from);
|
|
437 xfer->data = xfer_data;
|
|
438
|
|
439 /* Set the info about the incoming file. */
|
|
440 if (filename)
|
|
441 gaim_xfer_set_filename(xfer, filename);
|
|
442 else {
|
|
443 gchar *start, *end;
|
|
444 start = g_strrstr(xfer_data->path, "/");
|
|
445 if (start)
|
|
446 start++;
|
|
447 end = g_strrstr(xfer_data->path, "?");
|
|
448 if (start && *start && end) {
|
|
449 filename = g_strndup(start, end - start);
|
|
450 gaim_xfer_set_filename(xfer, filename);
|
|
451 g_free(filename);
|
|
452 filename = NULL;
|
|
453 }
|
|
454 }
|
|
455
|
|
456 gaim_xfer_set_size(xfer, filesize);
|
|
457
|
|
458 /* Setup our I/O op functions */
|
|
459 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init);
|
|
460 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start);
|
|
461 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end);
|
|
462 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send);
|
|
463 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv);
|
|
464 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read);
|
|
465 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write);
|
|
466
|
|
467 /* Now perform the request */
|
|
468 gaim_xfer_request(xfer);
|
|
469 }
|
|
470
|
|
471 void yahoo_ask_send_file(GaimConnection *gc, const char *who)
|
|
472 {
|
|
473 GaimXfer *xfer;
|
|
474 struct yahoo_xfer_data *xfer_data;
|
|
475
|
|
476 xfer_data = g_new0(struct yahoo_xfer_data, 1);
|
|
477 xfer_data->gc = gc;
|
|
478
|
|
479
|
|
480 /* Build the file transfer handle. */
|
|
481 xfer = gaim_xfer_new(gc->account, GAIM_XFER_SEND, who);
|
|
482 xfer->data = xfer_data;
|
|
483
|
|
484 /* Setup our I/O op functions */
|
|
485 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init);
|
|
486 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start);
|
|
487 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end);
|
|
488 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send);
|
|
489 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv);
|
|
490 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read);
|
|
491 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write);
|
|
492
|
|
493 /* Now perform the request */
|
|
494 gaim_xfer_request(xfer);
|
|
495 }
|
|
496
|
|
497 void yahoo_send_file(GaimConnection *gc, const char *who, const char *file)
|
|
498 {
|
|
499 GaimXfer *xfer;
|
|
500 struct yahoo_xfer_data *xfer_data;
|
|
501
|
|
502 if (!who || !file)
|
|
503 return;
|
|
504
|
|
505 xfer_data = g_new0(struct yahoo_xfer_data, 1);
|
|
506 xfer_data->gc = gc;
|
|
507
|
|
508
|
|
509 /* Build the file transfer handle. */
|
|
510 xfer = gaim_xfer_new(gc->account, GAIM_XFER_SEND, who);
|
|
511 xfer->data = xfer_data;
|
|
512
|
|
513 /* Setup our I/O op functions */
|
|
514 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init);
|
|
515 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start);
|
|
516 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end);
|
|
517 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send);
|
|
518 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv);
|
|
519 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read);
|
|
520 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write);
|
|
521
|
|
522 /* Now perform the request */
|
7805
|
523 gaim_xfer_request_accepted(xfer, file);
|
7651
|
524 }
|