comparison libgaim/protocols/yahoo/yahoo_picture.c @ 14192:60b1bc8dbf37

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