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;
|
7827
|
192 struct yahoo_data *yd;
|
7651
|
193
|
|
194 xfer_data = xfer->data;
|
|
195 gc = xfer_data->gc;
|
7827
|
196 yd = gc->proto_data;
|
7651
|
197 account = gaim_connection_get_account(gc);
|
|
198
|
|
199 if (gaim_xfer_get_type(xfer) == GAIM_XFER_SEND) {
|
|
200 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST),
|
|
201 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT),
|
|
202 yahoo_sendfile_connected, xfer) == -1)
|
|
203 {
|
|
204 gaim_notify_error(gc, NULL, _("File Transfer Aborted"),
|
7827
|
205 _("Unable to establish file descriptor."));
|
7651
|
206 gaim_xfer_cancel_remote(xfer);
|
|
207 }
|
|
208 } else {
|
|
209 xfer->fd = gaim_proxy_connect(account, xfer_data->host, xfer_data->port,
|
|
210 yahoo_receivefile_connected, xfer);
|
|
211 if (xfer->fd == -1) {
|
|
212 gaim_notify_error(gc, NULL, _("File Transfer Aborted"),
|
|
213 _("Unable to establish file descriptor."));
|
|
214 gaim_xfer_cancel_remote(xfer);
|
|
215 }
|
|
216 }
|
|
217 }
|
|
218
|
|
219 static void yahoo_xfer_start(GaimXfer *xfer)
|
|
220 {
|
|
221 /* We don't need to do anything here, do we? */
|
|
222 }
|
|
223
|
|
224 static void yahoo_xfer_end(GaimXfer *xfer)
|
|
225 {
|
|
226 GaimAccount *account;
|
|
227 struct yahoo_xfer_data *xfer_data;
|
|
228
|
|
229 account = gaim_xfer_get_account(xfer);
|
|
230 xfer_data = xfer->data;
|
|
231
|
|
232
|
|
233 if (xfer_data)
|
|
234 yahoo_xfer_data_free(xfer_data);
|
|
235 xfer->data = NULL;
|
|
236
|
|
237 }
|
|
238
|
|
239 guint calculate_length(const gchar *l, size_t len)
|
|
240 {
|
|
241 int i;
|
|
242
|
|
243 for (i = 0; i < len; i++) {
|
|
244 if (!g_ascii_isdigit(l[i]))
|
|
245 continue;
|
|
246 return strtol(l + i, NULL, 10);
|
|
247 }
|
|
248 return 0;
|
|
249 }
|
|
250
|
|
251
|
|
252 size_t yahoo_xfer_read(char **buffer, GaimXfer *xfer)
|
|
253 {
|
7710
|
254 gchar buf[4096];
|
7682
|
255 ssize_t len;
|
7651
|
256 gchar *start = NULL;
|
|
257 gchar *length;
|
|
258 gchar *end;
|
7710
|
259 int filelen;
|
7651
|
260 struct yahoo_xfer_data *xd = xfer->data;
|
|
261
|
|
262 if (gaim_xfer_get_type(xfer) != GAIM_XFER_RECEIVE) {
|
|
263 return 0;
|
|
264 }
|
|
265
|
|
266 len = read(xfer->fd, buf, sizeof(buf));
|
|
267
|
7682
|
268 if (len <= 0) {
|
7710
|
269 if ((gaim_xfer_get_size(xfer) > 0) &&
|
|
270 (gaim_xfer_get_bytes_sent(xfer) >= gaim_xfer_get_size(xfer)))
|
7682
|
271 gaim_xfer_set_completed(xfer, TRUE);
|
7651
|
272 else
|
|
273 gaim_xfer_cancel_remote(xfer);
|
|
274 return 0;
|
|
275 }
|
|
276
|
|
277
|
|
278 if (!xd->started) {
|
|
279 xd->rxqueue = g_realloc(xd->rxqueue, len + xd->rxlen);
|
|
280 memcpy(xd->rxqueue + xd->rxlen, buf, len);
|
|
281 xd->rxlen += len;
|
|
282
|
|
283 length = g_strstr_len(xd->rxqueue, len, "Content-length:");
|
|
284 if (length) {
|
|
285 end = g_strstr_len(length, length - xd->rxqueue, "\r\n");
|
|
286 if (!end)
|
|
287 return 0;
|
7710
|
288 if ((filelen = calculate_length(length, len - (length - xd->rxqueue))))
|
|
289 gaim_xfer_set_size(xfer, filelen);
|
7651
|
290 }
|
|
291 start = g_strstr_len(xd->rxqueue, len, "\r\n\r\n");
|
|
292 if (start)
|
|
293 start += 4;
|
|
294 if (!start || start > (xd->rxqueue + len))
|
|
295 return 0;
|
|
296 xd->started = TRUE;
|
|
297
|
|
298 len -= (start - xd->rxqueue);
|
|
299
|
|
300 *buffer = g_malloc(len);
|
|
301 memcpy(*buffer, start, len);
|
|
302 g_free(xd->rxqueue);
|
|
303 xd->rxqueue = NULL;
|
|
304 xd->rxlen = 0;
|
|
305 } else {
|
|
306 *buffer = g_malloc(len);
|
|
307 memcpy(*buffer, buf, len);
|
|
308 }
|
|
309
|
|
310 return len;
|
|
311 }
|
|
312
|
|
313 size_t yahoo_xfer_write(const char *buffer, size_t size, GaimXfer *xfer)
|
|
314 {
|
7710
|
315 ssize_t len;
|
7651
|
316 struct yahoo_xfer_data *xd = xfer->data;
|
|
317
|
|
318 if (!xd)
|
|
319 return 0;
|
|
320
|
|
321 if (gaim_xfer_get_type(xfer) != GAIM_XFER_SEND) {
|
|
322 return 0;
|
|
323 }
|
|
324
|
|
325 len = write(xfer->fd, buffer, size);
|
|
326
|
7710
|
327 if (len == -1) {
|
|
328 if (gaim_xfer_get_bytes_sent(xfer) >= gaim_xfer_get_size(xfer))
|
|
329 gaim_xfer_set_completed(xfer, TRUE);
|
|
330 if ((errno != EAGAIN) && (errno != EINTR))
|
|
331 gaim_xfer_cancel_remote(xfer);
|
|
332 return 0;
|
|
333 }
|
|
334
|
|
335 if ((gaim_xfer_get_bytes_sent(xfer) + len) >= gaim_xfer_get_size(xfer))
|
7651
|
336 gaim_xfer_set_completed(xfer, TRUE);
|
|
337
|
|
338 return len;
|
|
339 }
|
|
340
|
|
341 static void yahoo_xfer_cancel_send(GaimXfer *xfer)
|
|
342 {
|
|
343 GaimAccount *account;
|
|
344 struct yahoo_xfer_data *xfer_data;
|
|
345
|
|
346 xfer_data = xfer->data;
|
|
347 account = gaim_xfer_get_account(xfer);
|
|
348
|
|
349 if (xfer_data)
|
|
350 yahoo_xfer_data_free(xfer_data);
|
|
351 xfer->data = NULL;
|
|
352 }
|
|
353
|
|
354 static void yahoo_xfer_cancel_recv(GaimXfer *xfer)
|
|
355 {
|
|
356 GaimAccount *account;
|
|
357 struct yahoo_xfer_data *xfer_data;
|
|
358
|
|
359 account = gaim_xfer_get_account(xfer);
|
|
360 xfer_data = xfer->data;
|
|
361
|
|
362 if (xfer_data)
|
|
363 yahoo_xfer_data_free(xfer_data);
|
|
364 xfer->data = NULL;
|
|
365 }
|
|
366
|
|
367 void yahoo_process_filetransfer(GaimConnection *gc, struct yahoo_packet *pkt)
|
|
368 {
|
|
369 char *from = NULL;
|
|
370 char *to = NULL;
|
|
371 char *msg = NULL;
|
|
372 char *url = NULL;
|
|
373 long expires = 0;
|
|
374 GaimXfer *xfer;
|
|
375 struct yahoo_xfer_data *xfer_data;
|
|
376
|
|
377 char *service = NULL;
|
|
378
|
|
379 char *filename = NULL;
|
|
380 unsigned long filesize = 0L;
|
|
381
|
|
382 GSList *l;
|
|
383
|
|
384 for (l = pkt->hash; l; l = l->next) {
|
|
385 struct yahoo_pair *pair = l->data;
|
|
386
|
|
387 if (pair->key == 4)
|
|
388 from = pair->value;
|
|
389 if (pair->key == 5)
|
|
390 to = pair->value;
|
|
391 if (pair->key == 14)
|
|
392 msg = pair->value;
|
|
393 if (pair->key == 20)
|
|
394 url = pair->value;
|
|
395 if (pair->key == 38)
|
|
396 expires = strtol(pair->value, NULL, 10);
|
|
397
|
|
398 if (pair->key == 27)
|
|
399 filename = pair->value;
|
|
400 if (pair->key == 28)
|
|
401 filesize = atol(pair->value);
|
|
402
|
|
403 if (pair->key == 49)
|
|
404 service = pair->value;
|
|
405 }
|
|
406
|
|
407 if (pkt->service == YAHOO_SERVICE_P2PFILEXFER) {
|
|
408 if (strcmp("FILEXFER", service) != 0) {
|
|
409 gaim_debug_misc("yahoo", "unhandled service 0x%02x", pkt->service);
|
|
410 return;
|
|
411 }
|
|
412 }
|
|
413
|
|
414 if (msg) {
|
|
415 char *tmp;
|
|
416 tmp = strchr(msg, '\006');
|
|
417 if (tmp)
|
|
418 *tmp = '\0';
|
|
419 }
|
|
420
|
|
421 if (!url || !from)
|
|
422 return;
|
|
423
|
|
424
|
|
425 /* Setup the Yahoo-specific file transfer data */
|
|
426 xfer_data = g_new0(struct yahoo_xfer_data, 1);
|
|
427 xfer_data->gc = gc;
|
|
428 if (!gaim_url_parse(url, &(xfer_data->host), &(xfer_data->port), &(xfer_data->path))) {
|
|
429 g_free(xfer_data);
|
|
430 return;
|
|
431 }
|
|
432
|
|
433 gaim_debug_misc("yahoo_filexfer", "Host is %s, port is %d, path is %s, and the full url was %s.\n",
|
|
434 xfer_data->host, xfer_data->port, xfer_data->path, url);
|
|
435
|
|
436 /* Build the file transfer handle. */
|
|
437 xfer = gaim_xfer_new(gc->account, GAIM_XFER_RECEIVE, from);
|
|
438 xfer->data = xfer_data;
|
|
439
|
|
440 /* Set the info about the incoming file. */
|
|
441 if (filename)
|
|
442 gaim_xfer_set_filename(xfer, filename);
|
|
443 else {
|
|
444 gchar *start, *end;
|
|
445 start = g_strrstr(xfer_data->path, "/");
|
|
446 if (start)
|
|
447 start++;
|
|
448 end = g_strrstr(xfer_data->path, "?");
|
|
449 if (start && *start && end) {
|
|
450 filename = g_strndup(start, end - start);
|
|
451 gaim_xfer_set_filename(xfer, filename);
|
|
452 g_free(filename);
|
|
453 filename = NULL;
|
|
454 }
|
|
455 }
|
|
456
|
|
457 gaim_xfer_set_size(xfer, filesize);
|
|
458
|
|
459 /* Setup our I/O op functions */
|
|
460 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init);
|
|
461 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start);
|
|
462 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end);
|
|
463 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send);
|
|
464 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv);
|
|
465 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read);
|
|
466 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write);
|
|
467
|
|
468 /* Now perform the request */
|
|
469 gaim_xfer_request(xfer);
|
|
470 }
|
|
471
|
|
472 void yahoo_ask_send_file(GaimConnection *gc, const char *who)
|
|
473 {
|
|
474 GaimXfer *xfer;
|
|
475 struct yahoo_xfer_data *xfer_data;
|
|
476
|
|
477 xfer_data = g_new0(struct yahoo_xfer_data, 1);
|
|
478 xfer_data->gc = gc;
|
|
479
|
|
480
|
|
481 /* Build the file transfer handle. */
|
|
482 xfer = gaim_xfer_new(gc->account, GAIM_XFER_SEND, who);
|
|
483 xfer->data = xfer_data;
|
|
484
|
|
485 /* Setup our I/O op functions */
|
|
486 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init);
|
|
487 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start);
|
|
488 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end);
|
|
489 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send);
|
|
490 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv);
|
|
491 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read);
|
|
492 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write);
|
|
493
|
|
494 /* Now perform the request */
|
|
495 gaim_xfer_request(xfer);
|
|
496 }
|
|
497
|
|
498 void yahoo_send_file(GaimConnection *gc, const char *who, const char *file)
|
|
499 {
|
|
500 GaimXfer *xfer;
|
|
501 struct yahoo_xfer_data *xfer_data;
|
|
502
|
|
503 if (!who || !file)
|
|
504 return;
|
|
505
|
|
506 xfer_data = g_new0(struct yahoo_xfer_data, 1);
|
|
507 xfer_data->gc = gc;
|
|
508
|
|
509
|
|
510 /* Build the file transfer handle. */
|
|
511 xfer = gaim_xfer_new(gc->account, GAIM_XFER_SEND, who);
|
|
512 xfer->data = xfer_data;
|
|
513
|
|
514 /* Setup our I/O op functions */
|
|
515 gaim_xfer_set_init_fnc(xfer, yahoo_xfer_init);
|
|
516 gaim_xfer_set_start_fnc(xfer, yahoo_xfer_start);
|
|
517 gaim_xfer_set_end_fnc(xfer, yahoo_xfer_end);
|
|
518 gaim_xfer_set_cancel_send_fnc(xfer, yahoo_xfer_cancel_send);
|
|
519 gaim_xfer_set_cancel_recv_fnc(xfer, yahoo_xfer_cancel_recv);
|
|
520 gaim_xfer_set_read_fnc(xfer, yahoo_xfer_read);
|
|
521 gaim_xfer_set_write_fnc(xfer, yahoo_xfer_write);
|
|
522
|
|
523 /* Now perform the request */
|
7805
|
524 gaim_xfer_request_accepted(xfer, file);
|
7651
|
525 }
|