src/protocols/yahoo/yahoo_picture.c

branch
cpw.khc.msnp14
changeset 20472
6a6d2ef151e6
parent 13912
463b4fa9f067
parent 20469
b2836a24d81e
child 20473
91e1b3a49d10
equal deleted inserted replaced
13912:463b4fa9f067 20472:6a6d2ef151e6
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_send_picture_info(GaimConnection *gc, const char *who)
229 {
230 struct yahoo_data *yd = gc->proto_data;
231 struct yahoo_packet *pkt;
232
233 if (!yd->picture_url) {
234 gaim_debug_warning("yahoo", "Attempted to send picture info without a picture\n");
235 return;
236 }
237
238 pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE, YAHOO_STATUS_AVAILABLE, 0);
239 yahoo_packet_hash(pkt, "sssssi", 1, gaim_connection_get_display_name(gc),
240 4, gaim_connection_get_display_name(gc), 5, who,
241 13, "2", 20, yd->picture_url, 192, yd->picture_checksum);
242 yahoo_packet_send_and_free(pkt, yd);
243 }
244
245 void yahoo_send_picture_request(GaimConnection *gc, const char *who)
246 {
247 struct yahoo_data *yd = gc->proto_data;
248 struct yahoo_packet *pkt;
249
250 pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE, YAHOO_STATUS_AVAILABLE, 0);
251 yahoo_packet_hash_str(pkt, 4, gaim_connection_get_display_name(gc)); /* me */
252 yahoo_packet_hash_str(pkt, 5, who); /* the other guy */
253 yahoo_packet_hash_str(pkt, 13, "1"); /* 1 = request, 2 = reply */
254 yahoo_packet_send_and_free(pkt, yd);
255 }
256
257 void yahoo_send_picture_checksum(GaimConnection *gc)
258 {
259 struct yahoo_data *yd = gc->proto_data;
260 struct yahoo_packet *pkt;
261
262 pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE_CHECKSUM, YAHOO_STATUS_AVAILABLE, 0);
263 yahoo_packet_hash(pkt, "ssi", 1, gaim_connection_get_display_name(gc),
264 212, "1", 192, yd->picture_checksum);
265 yahoo_packet_send_and_free(pkt, yd);
266 }
267
268 void yahoo_send_picture_update_to_user(GaimConnection *gc, const char *who, int type)
269 {
270 struct yahoo_data *yd = gc->proto_data;
271 struct yahoo_packet *pkt;
272
273 pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE_UPDATE, YAHOO_STATUS_AVAILABLE, 0);
274 yahoo_packet_hash(pkt, "ssi", 1, gaim_connection_get_display_name(gc), 5, who, 206, type);
275 yahoo_packet_send_and_free(pkt, yd);
276 }
277
278 struct yspufe {
279 GaimConnection *gc;
280 int type;
281 };
282
283 static void yahoo_send_picture_update_foreach(gpointer key, gpointer value, gpointer data)
284 {
285 char *who = key;
286 YahooFriend *f = value;
287 struct yspufe *d = data;
288
289 if (f->status != YAHOO_STATUS_OFFLINE)
290 yahoo_send_picture_update_to_user(d->gc, who, d->type);
291 }
292
293 void yahoo_send_picture_update(GaimConnection *gc, int type)
294 {
295 struct yahoo_data *yd = gc->proto_data;
296 struct yspufe data;
297
298 data.gc = gc;
299 data.type = type;
300
301 g_hash_table_foreach(yd->friends, yahoo_send_picture_update_foreach, &data);
302 }
303
304 void yahoo_buddy_icon_upload_data_free(struct yahoo_buddy_icon_upload_data *d)
305 {
306 gaim_debug_misc("yahoo", "In yahoo_buddy_icon_upload_data_free()\n");
307
308 if (d->str)
309 g_string_free(d->str, TRUE);
310 g_free(d->filename);
311 if (d->watcher)
312 gaim_input_remove(d->watcher);
313 if (d->fd != -1)
314 close(d->fd);
315 g_free(d);
316 }
317
318 /* we couldn't care less about the server's response, but yahoo gets grumpy if we close before it sends it */
319 static void yahoo_buddy_icon_upload_reading(gpointer data, gint source, GaimInputCondition condition)
320 {
321 struct yahoo_buddy_icon_upload_data *d = data;
322 GaimConnection *gc = d->gc;
323 char buf[1024];
324 int ret;
325
326 if (!GAIM_CONNECTION_IS_VALID(gc)) {
327 yahoo_buddy_icon_upload_data_free(d);
328 return;
329 }
330
331 ret = read(d->fd, buf, sizeof(buf));
332
333 if (ret < 0 && errno == EAGAIN)
334 return;
335 else if (ret <= 0)
336 yahoo_buddy_icon_upload_data_free(d);
337 }
338
339 static void yahoo_buddy_icon_upload_pending(gpointer data, gint source, GaimInputCondition condition)
340 {
341 struct yahoo_buddy_icon_upload_data *d = data;
342 GaimConnection *gc = d->gc;
343 ssize_t wrote;
344
345 if (!GAIM_CONNECTION_IS_VALID(gc)) {
346 yahoo_buddy_icon_upload_data_free(d);
347 return;
348 }
349
350 wrote = write(d->fd, d->str->str + d->pos, d->str->len - d->pos);
351 if (wrote < 0 && errno == EAGAIN)
352 return;
353 if (wrote <= 0) {
354 yahoo_buddy_icon_upload_data_free(d);
355 return;
356 }
357 d->pos += wrote;
358 if (d->pos >= d->str->len) {
359 gaim_debug_misc("yahoo", "Finished uploading buddy icon.\n");
360 gaim_input_remove(d->watcher);
361 d->watcher = gaim_input_add(d->fd, GAIM_INPUT_READ, yahoo_buddy_icon_upload_reading, d);
362 }
363 }
364
365 static void yahoo_buddy_icon_upload_connected(gpointer data, gint source, GaimInputCondition condition)
366 {
367 struct yahoo_buddy_icon_upload_data *d = data;
368 struct yahoo_packet *pkt;
369 gchar *size, *header;
370 guchar *pkt_buf;
371 const char *host;
372 int port;
373 size_t content_length, pkt_buf_len;
374 GaimConnection *gc;
375 GaimAccount *account;
376 struct yahoo_data *yd;
377
378 if (!d)
379 return;
380
381 gc = d->gc;
382 account = gaim_connection_get_account(gc);
383 yd = gc->proto_data;
384
385 if (source < 0) {
386 gaim_debug_error("yahoo", "Buddy icon upload failed, no file desc.\n");
387 yahoo_buddy_icon_upload_data_free(d);
388 return;
389 }
390
391
392 pkt = yahoo_packet_new(0xc2, YAHOO_STATUS_AVAILABLE, yd->session_id);
393
394 size = g_strdup_printf("%" G_GSIZE_FORMAT, d->str->len);
395 /* 1 = me, 38 = expire time(?), 0 = me, 28 = size, 27 = filename, 14 = NULL, 29 = data */
396 yahoo_packet_hash_str(pkt, 1, gaim_connection_get_display_name(gc));
397 yahoo_packet_hash_str(pkt, 38, "604800"); /* time til expire */
398 gaim_account_set_int(account, YAHOO_PICEXPIRE_SETTING, time(NULL) + 604800);
399 yahoo_packet_hash_str(pkt, 0, gaim_connection_get_display_name(gc));
400 yahoo_packet_hash_str(pkt, 28, size);
401 g_free(size);
402 yahoo_packet_hash_str(pkt, 27, d->filename);
403 yahoo_packet_hash_str(pkt, 14, "");
404
405 content_length = YAHOO_PACKET_HDRLEN + yahoo_packet_length(pkt);
406
407 host = gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST);
408 port = gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT);
409 header = g_strdup_printf(
410 "POST http://%s:%d/notifyft HTTP/1.0\r\n"
411 "Content-length: %" G_GSIZE_FORMAT "\r\n"
412 "Host: %s:%d\r\n"
413 "Cookie: Y=%s; T=%s\r\n"
414 "\r\n",
415 host, port, content_length + 4 + d->str->len,
416 host, port, yd->cookie_y, yd->cookie_t);
417
418 /* There's no magic here, we just need to prepend in reverse order */
419 g_string_prepend(d->str, "29\xc0\x80");
420
421 pkt_buf_len = yahoo_packet_build(pkt, 8, FALSE, &pkt_buf);
422 yahoo_packet_free(pkt);
423 g_string_prepend_len(d->str, pkt_buf, pkt_buf_len);
424 g_free(pkt_buf);
425
426 g_string_prepend(d->str, header);
427 g_free(header);
428
429 d->fd = source;
430 d->watcher = gaim_input_add(d->fd, GAIM_INPUT_WRITE, yahoo_buddy_icon_upload_pending, d);
431
432 yahoo_buddy_icon_upload_pending(d, d->fd, GAIM_INPUT_WRITE);
433 }
434
435 void yahoo_buddy_icon_upload(GaimConnection *gc, struct yahoo_buddy_icon_upload_data *d)
436 {
437 GaimAccount *account = gaim_connection_get_account(gc);
438 struct yahoo_data *yd = gc->proto_data;
439
440 if (yd->jp) {
441 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xferjp_host", YAHOOJP_XFER_HOST),
442 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT),
443 yahoo_buddy_icon_upload_connected, d) == -1)
444 {
445 gaim_debug_error("yahoo", "Uploading our buddy icon failed to connect.\n");
446 yahoo_buddy_icon_upload_data_free(d);
447 }
448 } else {
449 if (gaim_proxy_connect(account, gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST),
450 gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT),
451 yahoo_buddy_icon_upload_connected, d) == -1)
452 {
453 gaim_debug_error("yahoo", "Uploading our buddy icon failed to connect.\n");
454 yahoo_buddy_icon_upload_data_free(d);
455 }
456 }
457 }
458
459 void yahoo_set_buddy_icon(GaimConnection *gc, const char *iconfile)
460 {
461 struct yahoo_data *yd = gc->proto_data;
462 GaimAccount *account = gc->account;
463 FILE *file;
464 struct stat st;
465
466 if (iconfile == NULL) {
467 if (yd->picture_url)
468 g_free(yd->picture_url);
469 yd->picture_url = NULL;
470
471 gaim_account_set_string(account, YAHOO_PICURL_SETTING, NULL);
472 gaim_account_set_int(account, YAHOO_PICCKSUM_SETTING, 0);
473 gaim_account_set_int(account, YAHOO_PICEXPIRE_SETTING, 0);
474 if (yd->logged_in)
475 yahoo_send_picture_update(gc, 0);
476 /* TODO: check if we're connected and tell everyone we ain't not one no more */
477 } else if (!g_stat(iconfile, &st)) {
478 file = g_fopen(iconfile, "rb");
479 if (file) {
480 GString *s = g_string_sized_new(st.st_size);
481 size_t len;
482 struct yahoo_buddy_icon_upload_data *d;
483 int oldcksum = gaim_account_get_int(account, YAHOO_PICCKSUM_SETTING, 0);
484 int expire = gaim_account_get_int(account, YAHOO_PICEXPIRE_SETTING, 0);
485 const char *oldurl = gaim_account_get_string(account, YAHOO_PICURL_SETTING, NULL);
486
487 g_string_set_size(s, st.st_size);
488 len = fread(s->str, 1, st.st_size, file);
489 fclose(file);
490 g_string_set_size(s, len);
491 yd->picture_checksum = g_string_hash(s);
492
493 if ((yd->picture_checksum == oldcksum) && (expire > (time(NULL) + 60*60*24)) &&
494 oldcksum && expire && oldurl) {
495 gaim_debug_misc("yahoo", "buddy icon is up to date. Not reuploading.\n");
496 g_string_free(s, TRUE);
497 if (yd->picture_url)
498 g_free(yd->picture_url);
499 yd->picture_url = g_strdup(oldurl);
500 return;
501 }
502
503 d = g_new0(struct yahoo_buddy_icon_upload_data, 1);
504 d->gc = gc;
505 d->str = s;
506 d->fd = -1;
507 d->filename = g_strdup(iconfile);
508
509 if (!yd->logged_in) {
510 yd->picture_upload_todo = d;
511 return;
512 }
513
514 yahoo_buddy_icon_upload(gc, d);
515 } else
516 gaim_debug_error("yahoo",
517 "Can't open buddy icon file!\n");
518 } else
519 gaim_debug_error("yahoo",
520 "Can't stat buddy icon file!\n");
521 }

mercurial