comparison libpurple/protocols/yahoo/yahoo_picture.c @ 15374:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children 32c366eeeb99
comparison
equal deleted inserted replaced
15373:f79e0f4df793 15374:5fe8042783c1
1 /*
2 * gaim
3 *
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.
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
24 #include "internal.h"
25
26 #include "account.h"
27 #include "accountopt.h"
28 #include "blist.h"
29 #include "debug.h"
30 #include "prpl.h"
31 #include "proxy.h"
32 #include "util.h"
33
34 #include "yahoo.h"
35 #include "yahoo_packet.h"
36 #include "yahoo_friend.h"
37 #include "yahoo_picture.h"
38
39
40 struct yahoo_fetch_picture_data {
41 GaimConnection *gc;
42 char *who;
43 int checksum;
44 };
45
46 static void
47 yahoo_fetch_picture_cb(GaimUtilFetchUrlData *url_data, gpointer user_data,
48 const gchar *pic_data, size_t len, const gchar *error_message)
49 {
50 struct yahoo_fetch_picture_data *d;
51 struct yahoo_data *yd;
52 GaimBuddy *b;
53
54 d = user_data;
55 yd = d->gc->proto_data;
56 yd->url_datas = g_slist_remove(yd->url_datas, url_data);
57
58 if (error_message != NULL) {
59 gaim_debug_error("yahoo", "Fetching buddy icon failed: %s\n", error_message);
60 } else if (len == 0) {
61 gaim_debug_error("yahoo", "Fetched an icon with length 0. Strange.\n");
62 } else {
63 gaim_buddy_icons_set_for_user(gaim_connection_get_account(d->gc), d->who, (void *)pic_data, len);
64 b = gaim_find_buddy(gaim_connection_get_account(d->gc), d->who);
65 if (b)
66 gaim_blist_node_set_int((GaimBlistNode*)b, YAHOO_ICON_CHECKSUM_KEY, d->checksum);
67 }
68
69 g_free(d->who);
70 g_free(d);
71 }
72
73 void yahoo_process_picture(GaimConnection *gc, struct yahoo_packet *pkt)
74 {
75 struct yahoo_data *yd;
76 GSList *l = pkt->hash;
77 char *who = NULL, *us = NULL;
78 gboolean got_icon_info = FALSE, send_icon_info = FALSE;
79 char *url = NULL;
80 int checksum = 0;
81
82 while (l) {
83 struct yahoo_pair *pair = l->data;
84
85 switch (pair->key) {
86 case 1:
87 case 4:
88 who = pair->value;
89 break;
90 case 5:
91 us = pair->value;
92 break;
93 case 13: {
94 int tmp;
95 tmp = strtol(pair->value, NULL, 10);
96 if (tmp == 1) {
97 send_icon_info = TRUE;
98 } else if (tmp == 2) {
99 got_icon_info = TRUE;
100 }
101 break;
102 }
103 case 20:
104 url = pair->value;
105 break;
106 case 192:
107 checksum = strtol(pair->value, NULL, 10);
108 break;
109 }
110
111 l = l->next;
112 }
113
114 /* Yahoo IM 6 spits out 0.png as the URL if the buddy icon is not set */
115 if (who && got_icon_info && url && !strncasecmp(url, "http://", 7)) {
116 /* TODO: make this work p2p, try p2p before the url */
117 GaimUtilFetchUrlData *url_data;
118 struct yahoo_fetch_picture_data *data;
119 GaimBuddy *b = gaim_find_buddy(gc->account, who);
120 if (b && (checksum == gaim_blist_node_get_int((GaimBlistNode*)b, YAHOO_ICON_CHECKSUM_KEY)))
121 return;
122
123 data = g_new0(struct yahoo_fetch_picture_data, 1);
124 data->gc = gc;
125 data->who = g_strdup(who);
126 data->checksum = checksum;
127 url_data = gaim_util_fetch_url(url, FALSE,
128 "Mozilla/4.0 (compatible; MSIE 5.0)", FALSE,
129 yahoo_fetch_picture_cb, data);
130 if (url_data != NULL) {
131 yd = gc->proto_data;
132 yd->url_datas = g_slist_prepend(yd->url_datas, url_data);
133 } else {
134 g_free(data->who);
135 g_free(data);
136 }
137 } else if (who && send_icon_info) {
138 yahoo_send_picture_info(gc, who);
139 }
140 }
141
142 void yahoo_process_picture_update(GaimConnection *gc, struct yahoo_packet *pkt)
143 {
144 GSList *l = pkt->hash;
145 char *who = NULL;
146 int icon = 0;
147
148 while (l) {
149 struct yahoo_pair *pair = l->data;
150
151 switch (pair->key) {
152 case 4:
153 who = pair->value;
154 break;
155 case 5:
156 /* us */
157 break;
158 case 206:
159 icon = strtol(pair->value, NULL, 10);
160 break;
161 }
162 l = l->next;
163 }
164
165 if (who) {
166 if (icon == 2)
167 yahoo_send_picture_request(gc, who);
168 else if ((icon == 0) || (icon == 1)) {
169 GaimBuddy *b = gaim_find_buddy(gc->account, who);
170 YahooFriend *f;
171 gaim_buddy_icons_set_for_user(gc->account, who, NULL, 0);
172 if (b)
173 gaim_blist_node_remove_setting((GaimBlistNode *)b, YAHOO_ICON_CHECKSUM_KEY);
174 if ((f = yahoo_friend_find(gc, who)))
175 yahoo_friend_set_buddy_icon_need_request(f, TRUE);
176 gaim_debug_misc("yahoo", "Setting user %s's icon to NULL.\n", who);
177 }
178 }
179 }
180
181 void yahoo_process_picture_checksum(GaimConnection *gc, struct yahoo_packet *pkt)
182 {
183 GSList *l = pkt->hash;
184 char *who = NULL;
185 int checksum = 0;
186
187 while (l) {
188 struct yahoo_pair *pair = l->data;
189
190 switch (pair->key) {
191 case 4:
192 who = pair->value;
193 break;
194 case 5:
195 /* us */
196 break;
197 case 192:
198 checksum = strtol(pair->value, NULL, 10);
199 break;
200 }
201 l = l->next;
202 }
203
204 if (who) {
205 GaimBuddy *b = gaim_find_buddy(gc->account, who);
206 if (b && (checksum != gaim_blist_node_get_int((GaimBlistNode*)b, YAHOO_ICON_CHECKSUM_KEY)))
207 yahoo_send_picture_request(gc, who);
208 }
209 }
210
211 void yahoo_process_picture_upload(GaimConnection *gc, struct yahoo_packet *pkt)
212 {
213 GaimAccount *account = gaim_connection_get_account(gc);
214 struct yahoo_data *yd = gc->proto_data;
215 GSList *l = pkt->hash;
216 char *url = NULL;
217
218 while (l) {
219 struct yahoo_pair *pair = l->data;
220
221 switch (pair->key) {
222 case 5:
223 /* us */
224 break;
225 case 27:
226 /* filename on our computer. */
227 break;
228 case 20: /* url at yahoo */
229 url = pair->value;
230 case 38: /* timestamp */
231 break;
232 }
233 l = l->next;
234 }
235
236 if (url) {
237 if (yd->picture_url)
238 g_free(yd->picture_url);
239 yd->picture_url = g_strdup(url);
240 gaim_account_set_string(account, YAHOO_PICURL_SETTING, url);
241 gaim_account_set_int(account, YAHOO_PICCKSUM_SETTING, yd->picture_checksum);
242 yahoo_send_picture_update(gc, 2);
243 yahoo_send_picture_checksum(gc);
244 }
245 }
246
247 void yahoo_process_avatar_update(GaimConnection *gc, struct yahoo_packet *pkt)
248 {
249 GSList *l = pkt->hash;
250 char *who = NULL;
251 int avatar = 0;
252
253 while (l) {
254 struct yahoo_pair *pair = l->data;
255
256 switch (pair->key) {
257 case 4:
258 who = pair->value;
259 break;
260 case 5:
261 /* us */
262 break;
263 case 206:
264 /*
265 * 0 - No icon or avatar
266 * 1 - Using an avatar
267 * 2 - Using an icon
268 */
269 avatar = strtol(pair->value, NULL, 10);
270 break;
271 }
272 l = l->next;
273 }
274
275 if (who) {
276 if (avatar == 2)
277 yahoo_send_picture_request(gc, who);
278 else if ((avatar == 0) || (avatar == 1)) {
279 GaimBuddy *b = gaim_find_buddy(gc->account, who);
280 YahooFriend *f;
281 gaim_buddy_icons_set_for_user(gc->account, who, NULL, 0);
282 if (b)
283 gaim_blist_node_remove_setting((GaimBlistNode *)b, YAHOO_ICON_CHECKSUM_KEY);
284 if ((f = yahoo_friend_find(gc, who)))
285 yahoo_friend_set_buddy_icon_need_request(f, TRUE);
286 gaim_debug_misc("yahoo", "Setting user %s's icon to NULL.\n", who);
287 }
288 }
289 }
290
291 void yahoo_send_picture_info(GaimConnection *gc, const char *who)
292 {
293 struct yahoo_data *yd = gc->proto_data;
294 struct yahoo_packet *pkt;
295
296 if (!yd->picture_url) {
297 gaim_debug_warning("yahoo", "Attempted to send picture info without a picture\n");
298 return;
299 }
300
301 pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE, YAHOO_STATUS_AVAILABLE, 0);
302 yahoo_packet_hash(pkt, "sssssi", 1, gaim_connection_get_display_name(gc),
303 4, gaim_connection_get_display_name(gc), 5, who,
304 13, "2", 20, yd->picture_url, 192, yd->picture_checksum);
305 yahoo_packet_send_and_free(pkt, yd);
306 }
307
308 void yahoo_send_picture_request(GaimConnection *gc, const char *who)
309 {
310 struct yahoo_data *yd = gc->proto_data;
311 struct yahoo_packet *pkt;
312
313 pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE, YAHOO_STATUS_AVAILABLE, 0);
314 yahoo_packet_hash_str(pkt, 4, gaim_connection_get_display_name(gc)); /* me */
315 yahoo_packet_hash_str(pkt, 5, who); /* the other guy */
316 yahoo_packet_hash_str(pkt, 13, "1"); /* 1 = request, 2 = reply */
317 yahoo_packet_send_and_free(pkt, yd);
318 }
319
320 void yahoo_send_picture_checksum(GaimConnection *gc)
321 {
322 struct yahoo_data *yd = gc->proto_data;
323 struct yahoo_packet *pkt;
324
325 pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE_CHECKSUM, YAHOO_STATUS_AVAILABLE, 0);
326 yahoo_packet_hash(pkt, "ssi", 1, gaim_connection_get_display_name(gc),
327 212, "1", 192, yd->picture_checksum);
328 yahoo_packet_send_and_free(pkt, yd);
329 }
330
331 void yahoo_send_picture_update_to_user(GaimConnection *gc, const char *who, int type)
332 {
333 struct yahoo_data *yd = gc->proto_data;
334 struct yahoo_packet *pkt;
335
336 pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE_UPDATE, YAHOO_STATUS_AVAILABLE, 0);
337 yahoo_packet_hash(pkt, "ssi", 1, gaim_connection_get_display_name(gc), 5, who, 206, type);
338 yahoo_packet_send_and_free(pkt, yd);
339 }
340
341 struct yspufe {
342 GaimConnection *gc;
343 int type;
344 };
345
346 static void yahoo_send_picture_update_foreach(gpointer key, gpointer value, gpointer data)
347 {
348 const char *who = key;
349 YahooFriend *f = value;
350 struct yspufe *d = data;
351
352 if (f->status != YAHOO_STATUS_OFFLINE)
353 yahoo_send_picture_update_to_user(d->gc, who, d->type);
354 }
355
356 void yahoo_send_picture_update(GaimConnection *gc, int type)
357 {
358 struct yahoo_data *yd = gc->proto_data;
359 struct yspufe data;
360
361 data.gc = gc;
362 data.type = type;
363
364 g_hash_table_foreach(yd->friends, yahoo_send_picture_update_foreach, &data);
365 }
366
367 void yahoo_buddy_icon_upload_data_free(struct yahoo_buddy_icon_upload_data *d)
368 {
369 gaim_debug_misc("yahoo", "In yahoo_buddy_icon_upload_data_free()\n");
370
371 if (d->str)
372 g_string_free(d->str, TRUE);
373 g_free(d->filename);
374 if (d->watcher)
375 gaim_input_remove(d->watcher);
376 if (d->fd != -1)
377 close(d->fd);
378 g_free(d);
379 }
380
381 /* we couldn't care less about the server's response, but yahoo gets grumpy if we close before it sends it */
382 static void yahoo_buddy_icon_upload_reading(gpointer data, gint source, GaimInputCondition condition)
383 {
384 struct yahoo_buddy_icon_upload_data *d = data;
385 GaimConnection *gc = d->gc;
386 char buf[1024];
387 int ret;
388
389 if (!GAIM_CONNECTION_IS_VALID(gc)) {
390 yahoo_buddy_icon_upload_data_free(d);
391 return;
392 }
393
394 ret = read(d->fd, buf, sizeof(buf));
395
396 if (ret < 0 && errno == EAGAIN)
397 return;
398 else if (ret <= 0)
399 yahoo_buddy_icon_upload_data_free(d);
400 }
401
402 static void yahoo_buddy_icon_upload_pending(gpointer data, gint source, GaimInputCondition condition)
403 {
404 struct yahoo_buddy_icon_upload_data *d = data;
405 GaimConnection *gc = d->gc;
406 ssize_t wrote;
407
408 if (!GAIM_CONNECTION_IS_VALID(gc)) {
409 yahoo_buddy_icon_upload_data_free(d);
410 return;
411 }
412
413 wrote = write(d->fd, d->str->str + d->pos, d->str->len - d->pos);
414 if (wrote < 0 && errno == EAGAIN)
415 return;
416 if (wrote <= 0) {
417 yahoo_buddy_icon_upload_data_free(d);
418 return;
419 }
420 d->pos += wrote;
421 if (d->pos >= d->str->len) {
422 gaim_debug_misc("yahoo", "Finished uploading buddy icon.\n");
423 gaim_input_remove(d->watcher);
424 d->watcher = gaim_input_add(d->fd, GAIM_INPUT_READ, yahoo_buddy_icon_upload_reading, d);
425 }
426 }
427
428 static void yahoo_buddy_icon_upload_connected(gpointer data, gint source, const gchar *error_message)
429 {
430 struct yahoo_buddy_icon_upload_data *d = data;
431 struct yahoo_packet *pkt;
432 gchar *size, *header;
433 guchar *pkt_buf;
434 const char *host;
435 int port;
436 size_t content_length, pkt_buf_len;
437 GaimConnection *gc;
438 GaimAccount *account;
439 struct yahoo_data *yd;
440
441 gc = d->gc;
442 account = gaim_connection_get_account(gc);
443 yd = gc->proto_data;
444
445 /* Buddy icon connect is now complete; clear the GaimProxyConnectData */
446 yd->buddy_icon_connect_data = NULL;
447
448 if (source < 0) {
449 gaim_debug_error("yahoo", "Buddy icon upload failed: %s\n", error_message);
450 yahoo_buddy_icon_upload_data_free(d);
451 return;
452 }
453
454 pkt = yahoo_packet_new(0xc2, YAHOO_STATUS_AVAILABLE, yd->session_id);
455
456 size = g_strdup_printf("%" G_GSIZE_FORMAT, d->str->len);
457 /* 1 = me, 38 = expire time(?), 0 = me, 28 = size, 27 = filename, 14 = NULL, 29 = data */
458 yahoo_packet_hash_str(pkt, 1, gaim_connection_get_display_name(gc));
459 yahoo_packet_hash_str(pkt, 38, "604800"); /* time til expire */
460 gaim_account_set_int(account, YAHOO_PICEXPIRE_SETTING, time(NULL) + 604800);
461 yahoo_packet_hash_str(pkt, 0, gaim_connection_get_display_name(gc));
462 yahoo_packet_hash_str(pkt, 28, size);
463 g_free(size);
464 yahoo_packet_hash_str(pkt, 27, d->filename);
465 yahoo_packet_hash_str(pkt, 14, "");
466
467 content_length = YAHOO_PACKET_HDRLEN + yahoo_packet_length(pkt);
468
469 host = gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST);
470 port = gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT);
471 header = g_strdup_printf(
472 "POST http://%s:%d/notifyft HTTP/1.0\r\n"
473 "Content-length: %" G_GSIZE_FORMAT "\r\n"
474 "Host: %s:%d\r\n"
475 "Cookie: Y=%s; T=%s\r\n"
476 "\r\n",
477 host, port, content_length + 4 + d->str->len,
478 host, port, yd->cookie_y, yd->cookie_t);
479
480 /* There's no magic here, we just need to prepend in reverse order */
481 g_string_prepend(d->str, "29\xc0\x80");
482
483 pkt_buf_len = yahoo_packet_build(pkt, 8, FALSE, yd->jp, &pkt_buf);
484 yahoo_packet_free(pkt);
485 g_string_prepend_len(d->str, (char *)pkt_buf, pkt_buf_len);
486 g_free(pkt_buf);
487
488 g_string_prepend(d->str, header);
489 g_free(header);
490
491 d->fd = source;
492 d->watcher = gaim_input_add(d->fd, GAIM_INPUT_WRITE, yahoo_buddy_icon_upload_pending, d);
493
494 yahoo_buddy_icon_upload_pending(d, d->fd, GAIM_INPUT_WRITE);
495 }
496
497 void yahoo_buddy_icon_upload(GaimConnection *gc, struct yahoo_buddy_icon_upload_data *d)
498 {
499 GaimAccount *account = gaim_connection_get_account(gc);
500 struct yahoo_data *yd = gc->proto_data;
501
502 if (yd->buddy_icon_connect_data != NULL) {
503 /* Cancel any in-progress buddy icon upload */
504 gaim_proxy_connect_cancel(yd->buddy_icon_connect_data);
505 yd->buddy_icon_connect_data = NULL;
506 }
507
508 yd->buddy_icon_connect_data = gaim_proxy_connect(NULL, account,
509 yd->jp ? gaim_account_get_string(account, "xferjp_host", YAHOOJP_XFER_HOST)
510 : gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST),
511 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT),
512 yahoo_buddy_icon_upload_connected, d);
513
514 if (yd->buddy_icon_connect_data == NULL)
515 {
516 gaim_debug_error("yahoo", "Uploading our buddy icon failed to connect.\n");
517 yahoo_buddy_icon_upload_data_free(d);
518 }
519 }
520
521 void yahoo_set_buddy_icon(GaimConnection *gc, const char *iconfile)
522 {
523 struct yahoo_data *yd = gc->proto_data;
524 GaimAccount *account = gc->account;
525 gchar *icondata;
526 gsize len;
527 GError *error = NULL;
528
529 if (iconfile == NULL) {
530 g_free(yd->picture_url);
531 yd->picture_url = NULL;
532
533 gaim_account_set_string(account, YAHOO_PICURL_SETTING, NULL);
534 gaim_account_set_int(account, YAHOO_PICCKSUM_SETTING, 0);
535 gaim_account_set_int(account, YAHOO_PICEXPIRE_SETTING, 0);
536 if (yd->logged_in)
537 /* Tell everyone we ain't got one no more */
538 yahoo_send_picture_update(gc, 0);
539
540 } else if (g_file_get_contents(iconfile, &icondata, &len, &error)) {
541 GString *s = g_string_new_len(icondata, len);
542 struct yahoo_buddy_icon_upload_data *d;
543 int oldcksum = gaim_account_get_int(account, YAHOO_PICCKSUM_SETTING, 0);
544 int expire = gaim_account_get_int(account, YAHOO_PICEXPIRE_SETTING, 0);
545 const char *oldurl = gaim_account_get_string(account, YAHOO_PICURL_SETTING, NULL);
546
547 yd->picture_checksum = g_string_hash(s);
548
549 if ((yd->picture_checksum == oldcksum) &&
550 (expire > (time(NULL) + 60*60*24)) && oldurl)
551 {
552 gaim_debug_misc("yahoo", "buddy icon is up to date. Not reuploading.\n");
553 g_string_free(s, TRUE);
554 g_free(yd->picture_url);
555 yd->picture_url = g_strdup(oldurl);
556 return;
557 }
558
559 d = g_new0(struct yahoo_buddy_icon_upload_data, 1);
560 d->gc = gc;
561 d->str = s;
562 d->fd = -1;
563 d->filename = g_strdup(iconfile);
564
565 if (!yd->logged_in) {
566 yd->picture_upload_todo = d;
567 return;
568 }
569
570 yahoo_buddy_icon_upload(gc, d);
571
572 } else {
573 gaim_debug_error("yahoo",
574 "Could not read buddy icon file '%s': %s\n",
575 iconfile, error->message);
576 g_error_free(error);
577 }
578 }