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