changeset 9306:04a3e9e869ee

[gaim-migrate @ 10114] Ok, this is yahoo buddy icon uploading support. It's still not quite right, but it mostly works. We don't send out updates yet so changing it or unsetting it may not work. But setting it initally, or changing it and relogging will probably work. I never did figure out what hash function yahoo is using, so I just used g_string_hash. It probably won't matter. I hope to finish this up before release. But people probably won't notice the bugs too much anyway. It shouldn't crash or anything, people just might not always see your newest icon right away. Have fun kids. For the record, Simguy tells me Yahoo likes 96x96 PNGs. committer: Tailor Script <tailor@pidgin.im>
author Tim Ringenbach <marv@pidgin.im>
date Fri, 18 Jun 2004 07:28:25 +0000
parents 0c201a2386c7
children 2138b3a07cb8
files ChangeLog src/protocols/yahoo/Makefile.am src/protocols/yahoo/Makefile.mingw src/protocols/yahoo/yahoo.c src/protocols/yahoo/yahoo.h src/protocols/yahoo/yahoo_filexfer.c src/protocols/yahoo/yahoo_picture.c src/protocols/yahoo/yahoo_picture.h
diffstat 8 files changed, 591 insertions(+), 190 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Jun 18 05:09:01 2004 +0000
+++ b/ChangeLog	Fri Jun 18 07:28:25 2004 +0000
@@ -21,7 +21,7 @@
 	  buddy is currently in. For example, if the buddy is idle, set
 	  "Return from idle" by default. The last action(s) used are the
 	  defaults for the next pounce
-	* Yahoo buddy icon support, can't set your own yet though
+	* Yahoo buddy icon support
 
 	Bug Fixes:
 	* Non-looping animated icons no longer cause Gaim to freeze
--- a/src/protocols/yahoo/Makefile.am	Fri Jun 18 05:09:01 2004 +0000
+++ b/src/protocols/yahoo/Makefile.am	Fri Jun 18 07:28:25 2004 +0000
@@ -16,6 +16,8 @@
 	yahoo_filexfer.c \
 	yahoo_friend.h \
 	yahoo_friend.c \
+	yahoo_picture.c \
+	yahoo_picture.h \
 	yahoo_profile.c
 
 AM_CFLAGS = $(st)
--- a/src/protocols/yahoo/Makefile.mingw	Fri Jun 18 05:09:01 2004 +0000
+++ b/src/protocols/yahoo/Makefile.mingw	Fri Jun 18 07:28:25 2004 +0000
@@ -73,6 +73,7 @@
 			yahoochat.c \
 			yahoo_filexfer.c \
 			yahoo_profile.c \
+			yahoo_picture.c \
 			yahoo_friend.c \
 			crypt.c \
 			util.c
--- a/src/protocols/yahoo/yahoo.c	Fri Jun 18 05:09:01 2004 +0000
+++ b/src/protocols/yahoo/yahoo.c	Fri Jun 18 07:28:25 2004 +0000
@@ -40,15 +40,15 @@
 #include "yahoochat.h"
 #include "yahoo_auth.h"
 #include "yahoo_filexfer.h"
+#include "yahoo_picture.h"
 #include "md5.h"
 
 extern char *yahoo_crypt(const char *, const char *);
 
-#define YAHOO_ICON_CHECKSUM_KEY "icon_checksum"
 /* #define YAHOO_DEBUG */
 
 static void yahoo_add_buddy(GaimConnection *gc, GaimBuddy *, GaimGroup *);
-static void yahoo_send_buddy_icon_request(GaimConnection *gc, const char *who);
+
 
 struct yahoo_packet *yahoo_packet_new(enum yahoo_service service, enum yahoo_status status, int id)
 {
@@ -246,6 +246,36 @@
 	return ret;
 }
 
+int yahoo_send_packet_special(int fd, struct yahoo_packet *pkt, int pad)
+{
+	int pktlen = yahoo_packet_length(pkt);
+	int len = YAHOO_PACKET_HDRLEN + pktlen;
+	int ret;
+
+	guchar *data;
+	int pos = 0;
+
+	if (fd < 0)
+		return -1;
+
+	data = g_malloc0(len + 1);
+
+	memcpy(data + pos, "YMSG", 4); pos += 4;
+	pos += yahoo_put16(data + pos, YAHOO_PROTO_VER);
+	pos += yahoo_put16(data + pos, 0x0000);
+	pos += yahoo_put16(data + pos, pktlen + pad);
+	pos += yahoo_put16(data + pos, pkt->service);
+	pos += yahoo_put32(data + pos, pkt->status);
+	pos += yahoo_put32(data + pos, pkt->id);
+
+	yahoo_packet_write(pkt, data + pos);
+
+	ret = write(fd, data, len);
+	g_free(data);
+
+	return ret;
+}
+
 void yahoo_packet_free(struct yahoo_packet *pkt)
 {
 	while (pkt->hash) {
@@ -295,6 +325,10 @@
 				gaim_connection_set_state(gc, GAIM_CONNECTED);
 				serv_finish_login(gc);
 				yd->logged_in = TRUE;
+				if (yd->picture_upload_todo) {
+					yahoo_buddy_icon_upload(gc, yd->picture_upload_todo);
+					yd->picture_upload_todo = NULL;
+				}
 
 				/* this requests the list. i have a feeling that this is very evil
 				 *
@@ -743,9 +777,9 @@
 	}
 
 	for (l = list; l; l = l->next) {
+		YahooFriend *f;
 		char *m, *m2;
 		im = l->data;
-		YahooFriend *f;
 
 		if (!im->from || !im->msg) {
 			g_free(im);
@@ -1588,6 +1622,11 @@
 	yahoo_packet_hash(pack, 6, resp_6);
 	yahoo_packet_hash(pack, 96, resp_96);
 	yahoo_packet_hash(pack, 1, name);
+	if (yd->picture_checksum) {
+		char *cksum = g_strdup_printf("%d", yd->picture_checksum);
+		yahoo_packet_hash(pack, 192, cksum);
+		g_free(cksum);
+	}
 	yahoo_send_packet(yd, pack);
 	yahoo_packet_free(pack);
 
@@ -1871,146 +1910,6 @@
 	}
 }
 
-struct yahoo_fetch_picture_data {
-	GaimConnection *gc;
-	char *who;
-	int checksum;
-};
-
-void yahoo_fetch_picture_cb(void *user_data, const char *pic_data, size_t len)
-{
-	struct yahoo_fetch_picture_data *d = user_data;
-	GaimBuddy *b;
-
-	if (GAIM_CONNECTION_IS_VALID(d->gc) && len) {
-		gaim_buddy_icons_set_for_user(gaim_connection_get_account(d->gc), d->who, (void *)pic_data, len);
-		b = gaim_find_buddy(gaim_connection_get_account(d->gc), d->who);
-		if (b)
-			gaim_blist_node_set_int((GaimBlistNode*)b, YAHOO_ICON_CHECKSUM_KEY, d->checksum);
-	}
-
-	g_free(d->who);
-	g_free(d);
-}
-
-static void yahoo_process_picture(GaimConnection *gc, struct yahoo_packet *pkt)
-{
-	GSList *l = pkt->hash;
-	char *who = NULL, *us = NULL;
-	gboolean got_icon_info = FALSE;
-	char *url = NULL;
-	int checksum = 0;
-
-	while (l) {
-		struct yahoo_pair *pair = l->data;
-
-		switch (pair->key) {
-		case 1:
-		case 4:
-			who = pair->value;
-			break;
-		case 5:
-			us = pair->value;
-			break;
-		case 13: {
-				int tmp;
-				tmp = strtol(pair->value, NULL, 10);
-				if (tmp == 1) {
-					/* send them info about our buddy icon */
-				} else if (tmp == 2) {
-					got_icon_info = TRUE;
-				}
-				break;
-			}
-		case 20:
-			url = pair->value;
-			break;
-		case 192:
-			checksum = strtol(pair->value, NULL, 10); /* just a guess for now */
-			break;
-		}
-
-		l = l->next;
-	}
-
-	if (who && got_icon_info && url) {
-		/* TODO: make this work p2p, try p2p before the url */
-		struct yahoo_fetch_picture_data *data;
-		GaimBuddy *b = gaim_find_buddy(gc->account, who);
-		if (b && (checksum == gaim_blist_node_get_int((GaimBlistNode*)b, YAHOO_ICON_CHECKSUM_KEY)))
-			return;
-
-		data = g_new0(struct yahoo_fetch_picture_data, 1);
-		data->gc = gc;
-		data->who = g_strdup(who);
-		data->checksum = checksum;
-		gaim_url_fetch(url, FALSE, "Mozilla/4.0 (compatible; MSIE 5.0)", FALSE,
-		               yahoo_fetch_picture_cb, data);
-	}
-
-}
-
-static void yahoo_process_picture_update(GaimConnection *gc, struct yahoo_packet *pkt)
-{
-	GSList *l = pkt->hash;
-	char *who = NULL;
-	int icon = 0;
-
-	while (l) {
-		struct yahoo_pair *pair = l->data;
-
-		switch (pair->key) {
-		case 4:
-			who = pair->value;
-			break;
-		case 5:
-			/* us */
-			break;
-		case 206:
-			icon = strtol(pair->value, NULL, 10);
-			break;
-		}
-		l = l->next;
-	}
-
-	if (who) {
-		if (icon == 2)
-			yahoo_send_buddy_icon_request(gc, who);
-		else if (icon == 0)
-			gaim_buddy_icons_set_for_user(gc->account, who, NULL, 0);
-	}
-}
-
-static void yahoo_process_picture_checksum(GaimConnection *gc, struct yahoo_packet *pkt)
-{
-	GSList *l = pkt->hash;
-	char *who = NULL;
-	int checksum = 0;
-
-	while (l) {
-		struct yahoo_pair *pair = l->data;
-
-		switch (pair->key) {
-		case 4:
-			who = pair->value;
-			break;
-		case 5:
-			/* us */
-			break;
-		case 192:
-			checksum = strtol(pair->value, NULL, 10);
-			break;
-		}
-		l = l->next;
-	}
-
-	if (who) {
-		GaimBuddy *b = gaim_find_buddy(gc->account, who);
-		if (b && (checksum != gaim_blist_node_get_int((GaimBlistNode*)b, YAHOO_ICON_CHECKSUM_KEY)))
-			yahoo_send_buddy_icon_request(gc, who);
-	}
-}
-
 static void yahoo_packet_process(GaimConnection *gc, struct yahoo_packet *pkt)
 {
 	switch (pkt->service) {
@@ -2111,6 +2010,9 @@
 	case YAHOO_SERVICE_PICTURE_CHECKSUM:
 		yahoo_process_picture_checksum(gc, pkt);
 		break;
+	case YAHOO_SERVICE_PICTURE_UPLOAD:
+		yahoo_process_picture_upload(gc, pkt);
+		break;
 	default:
 		gaim_debug(GAIM_DEBUG_ERROR, "yahoo",
 				   "Unhandled service 0x%02x\n", pkt->service);
@@ -2408,6 +2310,16 @@
 	if (strcmp(server, "scs.yahoo.com") == 0)
 		gaim_account_set_string(account, "server", YAHOO_PAGER_HOST);
 }
+
+static void yahoo_picture_check(GaimAccount *account)
+{
+	GaimConnection *gc = gaim_account_get_connection(account);
+	const char *buddyicon;
+
+	buddyicon = gaim_account_get_buddy_icon(account);
+	yahoo_set_buddy_icon(gc, buddyicon);
+}
+
 #endif
 
 static void yahoo_login(GaimAccount *account) {
@@ -2428,6 +2340,7 @@
 #ifndef YAHOO_WEBMESSENGER
 
 	yahoo_server_check(account);
+	yahoo_picture_check(account);
 
 	if (gaim_account_get_bool(account, "yahoojp", FALSE)) {
 		yd->jp = TRUE;
@@ -2476,6 +2389,10 @@
 	if (yd->rxqueue)
 		g_free(yd->rxqueue);
 	yd->rxlen = 0;
+	if (yd->picture_url)
+		g_free(yd->picture_url);
+	if (yd->picture_upload_todo)
+		yahoo_buddy_icon_upload_data_free(yd->picture_upload_todo);
 	if (gc->inpa)
 		gaim_input_remove(gc->inpa);
 	g_free(yd);
@@ -2850,7 +2767,10 @@
 	yahoo_packet_hash(pkt,   63, ";0"); /* IMvironment */
 	yahoo_packet_hash(pkt,   64, "0"); /* no idea */
 	yahoo_packet_hash(pkt, 1002, "1"); /* no idea, Yahoo 6 or later only it seems */
-	yahoo_packet_hash(pkt,  206, "0"); /* 0 = no picture, 2 = picture, maybe 1 = avatar? */
+	if (!yd->picture_url)
+		yahoo_packet_hash(pkt, 206, "0"); /* 0 = no picture, 2 = picture, maybe 1 = avatar? */
+	else
+		yahoo_packet_hash(pkt, 206, "2");
 
 	yahoo_send_packet(yd, pkt);
 
@@ -3255,19 +3175,6 @@
 	g_free(gpo);
 }
 
-static void yahoo_send_buddy_icon_request(GaimConnection *gc, const char *who)
-{
-	struct yahoo_data *yd = gc->proto_data;
-	struct yahoo_packet *pkt;
-
-	pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE, YAHOO_STATUS_AVAILABLE, 0);
-	yahoo_packet_hash(pkt, 4, gaim_connection_get_display_name(gc)); /* me */
-	yahoo_packet_hash(pkt, 5, who); /* the other guy */
-	yahoo_packet_hash(pkt, 13, "1"); /* 1 = request, 2 = reply */
-	yahoo_send_packet(yd, pkt);
-	yahoo_packet_free(pkt);
-}
-
 #if 0
 static gboolean yahoo_has_send_file(GaimConnection *gc, const char *who)
 {
@@ -3280,7 +3187,7 @@
 static GaimPluginProtocolInfo prpl_info =
 {
 	GAIM_PRPL_API_VERSION,
-	OPT_PROTO_MAIL_CHECK | OPT_PROTO_CHAT_TOPIC,
+	OPT_PROTO_MAIL_CHECK | OPT_PROTO_CHAT_TOPIC | OPT_PROTO_BUDDY_ICON,
 	NULL, /* user_splits */
 	NULL, /* protocol_options */
 	yahoo_list_icon,
@@ -3325,7 +3232,7 @@
 	NULL, /* buddy_free */
 	NULL, /* convo_closed */
 	NULL, /* normalize */
-	NULL, /* set_buddy_icon */
+	yahoo_set_buddy_icon,
 	NULL, /* void (*remove_group)(GaimConnection *gc, const char *group);*/
 	NULL, /* char *(*get_cb_real_name)(GaimConnection *gc, int id, const char *who); */
 #if 0
--- a/src/protocols/yahoo/yahoo.h	Fri Jun 18 05:09:01 2004 +0000
+++ b/src/protocols/yahoo/yahoo.h	Fri Jun 18 07:28:25 2004 +0000
@@ -45,6 +45,11 @@
 
 #define WEBMESSENGER_URL "http://login.yahoo.com/config/login?.src=pg"
 
+#define YAHOO_ICON_CHECKSUM_KEY "icon_checksum"
+#define YAHOO_PICURL_SETTING "picture_url"
+#define YAHOO_PICCKSUM_SETTING "picture_checksum"
+#define YAHOO_PICEXPIRE_SETTING "picture_expire"
+
 enum yahoo_service { /* these are easier to see in hex */
 	YAHOO_SERVICE_LOGON = 1,
 	YAHOO_SERVICE_LOGOFF,
@@ -66,6 +71,7 @@
 	YAHOO_SERVICE_PING,
 	YAHOO_SERVICE_GOTGROUPRENAME,
 	YAHOO_SERVICE_SYSMESSAGE = 0x14,
+	YAHOO_SERVICE_SKINNAME = 0x15,
 	YAHOO_SERVICE_PASSTHROUGH2 = 0x16,
 	YAHOO_SERVICE_CONFINVITE = 0x18,
 	YAHOO_SERVICE_CONFLOGON,
@@ -108,6 +114,7 @@
 	YAHOO_SERVICE_PICTURE_CHECKSUM = 0xbd,
 	YAHOO_SERVICE_PICTURE = 0xbe,
 	YAHOO_SERVICE_PICTURE_UPDATE = 0xc1,
+	YAHOO_SERVICE_PICTURE_UPLOAD = 0xc2,
 	YAHOO_SERVICE_AVATAR_UPDATE = 0xc7,
 	YAHOO_SERVICE_WEBLOGIN = 0x0226
 };
@@ -131,6 +138,15 @@
 	YAHOO_STATUS_TYPING = 0x16
 };
 
+struct yahoo_buddy_icon_upload_data {
+	GaimConnection *gc;
+	GString *str;
+	char *filename;
+	int pos;
+	int fd;
+	guint watcher;
+};
+
 struct yahoo_data {
 	int fd;
 	guchar *rxqueue;
@@ -149,6 +165,13 @@
 	char *cookie_t;
 	int session_id;
 	gboolean jp;
+	/* picture aka buddy icon stuff */
+	char *picture_url;
+	int picture_checksum;
+
+	/* ew. we have to check the icon before we connect,
+	 * but can't upload it til we're connected. */
+	struct yahoo_buddy_icon_upload_data *picture_upload_todo;
 };
 
 struct yahoo_pair {
@@ -196,6 +219,7 @@
 struct yahoo_packet *yahoo_packet_new(enum yahoo_service service, enum yahoo_status status, int id);
 void yahoo_packet_hash(struct yahoo_packet *pkt, int key, const char *value);
 int yahoo_send_packet(struct yahoo_data *yd, struct yahoo_packet *pkt);
+int yahoo_send_packet_special(int fd, struct yahoo_packet *pkt, int pad);
 void yahoo_packet_write(struct yahoo_packet *pkt, guchar *data);
 int yahoo_packet_length(struct yahoo_packet *pkt);
 void yahoo_packet_free(struct yahoo_packet *pkt);
--- a/src/protocols/yahoo/yahoo_filexfer.c	Fri Jun 18 05:09:01 2004 +0000
+++ b/src/protocols/yahoo/yahoo_filexfer.c	Fri Jun 18 07:28:25 2004 +0000
@@ -80,36 +80,6 @@
 	return;
 }
 
-static int yahoo_send_packet_special(int fd, struct yahoo_packet *pkt, int pad)
-{
-	int pktlen = yahoo_packet_length(pkt);
-	int len = YAHOO_PACKET_HDRLEN + pktlen;
-	int ret;
-
-	guchar *data;
-	int pos = 0;
-
-	if (fd < 0)
-		return -1;
-
-	data = g_malloc0(len + 1);
-
-	memcpy(data + pos, "YMSG", 4); pos += 4;
-	pos += yahoo_put16(data + pos, YAHOO_PROTO_VER);
-	pos += yahoo_put16(data + pos, 0x0000);
-	pos += yahoo_put16(data + pos, pktlen + pad);
-	pos += yahoo_put16(data + pos, pkt->service);
-	pos += yahoo_put32(data + pos, pkt->status);
-	pos += yahoo_put32(data + pos, pkt->id);
-
-	yahoo_packet_write(pkt, data + pos);
-
-	ret = write(fd, data, len);
-	g_free(data);
-
-	return ret;
-}
-
 static void yahoo_sendfile_connected(gpointer data, gint source, GaimInputCondition condition)
 {
 	GaimXfer *xfer;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/protocols/yahoo/yahoo_picture.c	Fri Jun 18 07:28:25 2004 +0000
@@ -0,0 +1,458 @@
+/*
+ * gaim
+ *
+ * Gaim is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#include "internal.h"
+
+#include "account.h"
+#include "accountopt.h"
+#include "blist.h"
+#include "debug.h"
+#include "prpl.h"
+#include "proxy.h"
+#include "util.h"
+
+#include "yahoo.h"
+#include "yahoo_friend.h"
+#include "yahoo_picture.h"
+
+
+struct yahoo_fetch_picture_data {
+	GaimConnection *gc;
+	char *who;
+	int checksum;
+};
+
+void yahoo_fetch_picture_cb(void *user_data, const char *pic_data, size_t len)
+{
+	struct yahoo_fetch_picture_data *d = user_data;
+	GaimBuddy *b;
+
+	if (GAIM_CONNECTION_IS_VALID(d->gc) && len) {
+		gaim_buddy_icons_set_for_user(gaim_connection_get_account(d->gc), d->who, (void *)pic_data, len);
+		b = gaim_find_buddy(gaim_connection_get_account(d->gc), d->who);
+		if (b)
+			gaim_blist_node_set_int((GaimBlistNode*)b, YAHOO_ICON_CHECKSUM_KEY, d->checksum);
+	} else {
+		gaim_debug_error("yahoo", "Fetching buddy icon failed.\n");
+	}
+
+	g_free(d->who);
+	g_free(d);
+}
+
+void yahoo_send_picture_info(GaimConnection *gc, const char *who)
+{
+	struct yahoo_data *yd = gc->proto_data;
+	struct yahoo_packet *pkt;
+	char *buf;
+
+	if (!yd->picture_url)
+		return;
+
+	pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE, YAHOO_STATUS_AVAILABLE, 0);
+	yahoo_packet_hash(pkt, 1, gaim_connection_get_display_name(gc));
+	yahoo_packet_hash(pkt, 4, gaim_connection_get_display_name(gc));
+	yahoo_packet_hash(pkt, 5, who);
+	yahoo_packet_hash(pkt, 13, "2");
+	yahoo_packet_hash(pkt, 20, yd->picture_url);
+	buf = g_strdup_printf("%d", yd->picture_checksum);
+	yahoo_packet_hash(pkt, 192, buf);
+
+	yahoo_send_packet(yd, pkt);
+	yahoo_packet_free(pkt);
+	g_free(buf);
+}
+
+void yahoo_process_picture(GaimConnection *gc, struct yahoo_packet *pkt)
+{
+	GSList *l = pkt->hash;
+	char *who = NULL, *us = NULL;
+	gboolean got_icon_info = FALSE, send_icon_info = FALSE;
+	char *url = NULL;
+	int checksum = 0;
+
+	while (l) {
+		struct yahoo_pair *pair = l->data;
+
+		switch (pair->key) {
+		case 1:
+		case 4:
+			who = pair->value;
+			break;
+		case 5:
+			us = pair->value;
+			break;
+		case 13: {
+				int tmp;
+				tmp = strtol(pair->value, NULL, 10);
+				if (tmp == 1) {
+					send_icon_info = TRUE;
+				} else if (tmp == 2) {
+					got_icon_info = TRUE;
+				}
+				break;
+			}
+		case 20:
+			url = pair->value;
+			break;
+		case 192:
+			checksum = strtol(pair->value, NULL, 10);
+			break;
+		}
+
+		l = l->next;
+	}
+
+	if (who && got_icon_info && url) {
+		/* TODO: make this work p2p, try p2p before the url */
+		struct yahoo_fetch_picture_data *data;
+		GaimBuddy *b = gaim_find_buddy(gc->account, who);
+		if (b && (checksum == gaim_blist_node_get_int((GaimBlistNode*)b, YAHOO_ICON_CHECKSUM_KEY)))
+			return;
+
+		data = g_new0(struct yahoo_fetch_picture_data, 1);
+		data->gc = gc;
+		data->who = g_strdup(who);
+		data->checksum = checksum;
+		gaim_url_fetch(url, FALSE, "Mozilla/4.0 (compatible; MSIE 5.0)", FALSE,
+		               yahoo_fetch_picture_cb, data);
+	} else if (who && send_icon_info) {
+		yahoo_send_picture_info(gc, who);
+	}
+
+}
+
+void yahoo_process_picture_update(GaimConnection *gc, struct yahoo_packet *pkt)
+{
+	GSList *l = pkt->hash;
+	char *who = NULL;
+	int icon = 0;
+
+	while (l) {
+		struct yahoo_pair *pair = l->data;
+
+		switch (pair->key) {
+		case 4:
+			who = pair->value;
+			break;
+		case 5:
+			/* us */
+			break;
+		case 206:
+			icon = strtol(pair->value, NULL, 10);
+			break;
+		}
+		l = l->next;
+	}
+
+	if (who) {
+		if (icon == 2)
+			yahoo_send_buddy_icon_request(gc, who);
+		else if (icon == 0)
+			gaim_buddy_icons_set_for_user(gc->account, who, NULL, 0);
+	}
+}
+
+void yahoo_process_picture_checksum(GaimConnection *gc, struct yahoo_packet *pkt)
+{
+	GSList *l = pkt->hash;
+	char *who = NULL;
+	int checksum = 0;
+
+	while (l) {
+		struct yahoo_pair *pair = l->data;
+
+		switch (pair->key) {
+		case 4:
+			who = pair->value;
+			break;
+		case 5:
+			/* us */
+			break;
+		case 192:
+			checksum = strtol(pair->value, NULL, 10);
+			break;
+		}
+		l = l->next;
+	}
+
+	if (who) {
+		GaimBuddy *b = gaim_find_buddy(gc->account, who);
+		if (b && (checksum != gaim_blist_node_get_int((GaimBlistNode*)b, YAHOO_ICON_CHECKSUM_KEY)))
+			yahoo_send_buddy_icon_request(gc, who);
+	}
+}
+
+void yahoo_process_picture_upload(GaimConnection *gc, struct yahoo_packet *pkt)
+{
+	GaimAccount *account = gaim_connection_get_account(gc);
+	struct yahoo_data *yd = gc->proto_data;
+	GSList *l = pkt->hash;
+	char *url = NULL;
+
+	while (l) {
+		struct yahoo_pair *pair = l->data;
+
+		switch (pair->key) {
+		case 5:
+			/* us */
+			break;
+		case 27:
+			/* filename on our computer. */
+			break;
+		case 20: /* url at yahoo */
+			url = pair->value;
+		case 38: /* timestamp */
+			break;
+		}
+		l = l->next;
+	}
+
+	if (url) {
+		if (yd->picture_url)
+			g_free(yd->picture_url);
+		yd->picture_url = g_strdup(url);
+		gaim_account_set_string(account, YAHOO_PICURL_SETTING, url);
+		gaim_account_set_int(account, YAHOO_PICCKSUM_SETTING, yd->picture_checksum);
+	}
+}
+
+
+void yahoo_send_buddy_icon_request(GaimConnection *gc, const char *who)
+{
+	struct yahoo_data *yd = gc->proto_data;
+	struct yahoo_packet *pkt;
+
+	pkt = yahoo_packet_new(YAHOO_SERVICE_PICTURE, YAHOO_STATUS_AVAILABLE, 0);
+	yahoo_packet_hash(pkt, 4, gaim_connection_get_display_name(gc)); /* me */
+	yahoo_packet_hash(pkt, 5, who); /* the other guy */
+	yahoo_packet_hash(pkt, 13, "1"); /* 1 = request, 2 = reply */
+	yahoo_send_packet(yd, pkt);
+	yahoo_packet_free(pkt);
+}
+
+void yahoo_buddy_icon_upload_data_free(struct yahoo_buddy_icon_upload_data *d)
+{
+	gaim_debug_misc("yahoo", "In yahoo_buddy_icon_upload_data_free()\n");
+
+	if (d->str)
+		g_string_free(d->str, TRUE);
+	if (d->filename)
+		g_free(d->filename);
+	if (d->watcher)
+		gaim_input_remove(d->watcher);
+	if (d->fd != -1)
+		close(d->fd);
+	g_free(d);
+}
+
+/* we could care less about the server's responce, but yahoo gets grumpy if we close before it sends it */
+static void yahoo_buddy_icon_upload_reading(gpointer data, gint source, GaimInputCondition condition)
+{
+	struct yahoo_buddy_icon_upload_data *d = data;
+	GaimConnection *gc = d->gc;
+	char buf[1024];
+
+	if (!GAIM_CONNECTION_IS_VALID(gc)) {
+		yahoo_buddy_icon_upload_data_free(d);
+		return;
+	}
+
+	if (read(d->fd, buf, sizeof(buf)) <= 0)
+		yahoo_buddy_icon_upload_data_free(d);
+}
+
+static void yahoo_buddy_icon_upload_pending(gpointer data, gint source, GaimInputCondition condition)
+{
+	struct yahoo_buddy_icon_upload_data *d = data;
+	GaimConnection *gc = d->gc;
+	ssize_t wrote;
+
+	if (!GAIM_CONNECTION_IS_VALID(gc)) {
+		yahoo_buddy_icon_upload_data_free(d);
+		return;
+	}
+
+	wrote = write(d->fd, d->str->str + d->pos, d->str->len - d->pos);
+	if (wrote <= 0) {
+		yahoo_buddy_icon_upload_data_free(d);
+		return;
+	}
+	d->pos += wrote;
+	if (d->pos >= d->str->len) {
+		gaim_debug_misc("yahoo", "Finished uploading buddy icon.\n");
+		gaim_input_remove(d->watcher);
+		d->watcher = gaim_input_add(d->fd, GAIM_INPUT_READ, yahoo_buddy_icon_upload_reading, d);
+	}
+}
+
+static void yahoo_buddy_icon_upload_connected(gpointer data, gint source, GaimInputCondition condition)
+{
+	struct yahoo_buddy_icon_upload_data *d = data;
+	struct yahoo_packet *pkt;
+	gchar *size, *post, *buf;
+	int content_length;
+	GaimConnection *gc;
+	GaimAccount *account;
+	struct yahoo_data *yd;
+
+	if (!d)
+		return;
+
+
+	gc = d->gc;
+	account = gaim_connection_get_account(gc);
+	yd = gc->proto_data;
+
+
+	if (source < 0) {
+		gaim_debug_error("yahoo", "Buddy icon upload failed, no file desc.\n");
+		yahoo_buddy_icon_upload_data_free(d);
+		return;
+	}
+
+	d->fd = source;
+	d->watcher = gaim_input_add(d->fd, GAIM_INPUT_WRITE, yahoo_buddy_icon_upload_pending, d);
+
+	pkt = yahoo_packet_new(0xc2, YAHOO_STATUS_AVAILABLE, yd->session_id);
+
+	size = g_strdup_printf("%d", d->str->len);
+	/* 1 = me, 38 = expire time(?), 0 = me, 28 = size, 27 = filename, 14 = NULL, 29 = data */
+	yahoo_packet_hash(pkt, 1, gaim_connection_get_display_name(gc));
+	yahoo_packet_hash(pkt, 38, "604800"); /* time til expire */
+	gaim_account_set_int(account, YAHOO_PICEXPIRE_SETTING, time(NULL) + 604800);
+	yahoo_packet_hash(pkt, 0, gaim_connection_get_display_name(gc));
+	yahoo_packet_hash(pkt, 28, size);
+	yahoo_packet_hash(pkt, 27, d->filename);
+	yahoo_packet_hash(pkt, 14, "");
+
+	content_length = YAHOO_PACKET_HDRLEN + yahoo_packet_length(pkt);
+
+	buf = g_strdup_printf("Y=%s; T=%s", yd->cookie_y, yd->cookie_t);
+
+	post = g_strdup_printf("POST /notifyft HTTP/1.0\r\n"
+	                       "Content-length: %d\r\n"
+	                       "Host: %s:%d\r\n"
+	                       "Cookie: %s\r\n"
+	                       "\r\n",
+	                       content_length + 4 + d->str->len,
+			       gaim_account_get_string(account, "xfer_host", YAHOO_XFER_HOST),
+			       gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT),
+			       buf);
+	write(d->fd, post, strlen(post));
+
+	yahoo_send_packet_special(d->fd, pkt, 8);
+	yahoo_packet_free(pkt);
+
+	write(d->fd, "29\xc0\x80", 4);
+
+	g_free(size);
+	g_free(post);
+	g_free(buf);
+}
+
+void yahoo_buddy_icon_upload(GaimConnection *gc, struct yahoo_buddy_icon_upload_data *d)
+{
+	GaimAccount *account = gaim_connection_get_account(gc);
+	struct yahoo_data *yd = gc->proto_data;
+
+	if (yd->jp) {
+		if (gaim_proxy_connect(account, gaim_account_get_string(account, "xferjp_host",  YAHOOJP_XFER_HOST),
+		                       gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT),
+		                       yahoo_buddy_icon_upload_connected, d) == -1)
+		{
+			gaim_debug_error("yahoo", "Uploading our buddy icon failed to connect.\n");
+			yahoo_buddy_icon_upload_data_free(d);
+		}
+	} else {
+		if (gaim_proxy_connect(account, gaim_account_get_string(account, "xfer_host",  YAHOO_XFER_HOST),
+		                       gaim_account_get_int(account, "xfer_port", YAHOO_XFER_PORT),
+		                       yahoo_buddy_icon_upload_connected, d) == -1)
+		{
+			gaim_debug_error("yahoo", "Uploading our buddy icon failed to connect.\n");
+			yahoo_buddy_icon_upload_data_free(d);
+		}
+	}
+}
+
+void yahoo_set_buddy_icon(GaimConnection *gc, const char *iconfile)
+{
+	struct yahoo_data *yd = gc->proto_data;
+	GaimAccount *account = gc->account;
+	FILE *file;
+	struct stat st;
+
+	if (iconfile == NULL) {
+		if (yd->picture_url)
+			g_free(yd->picture_url);
+		yd->picture_url = NULL;
+
+		gaim_account_set_string(account, YAHOO_PICURL_SETTING, NULL);
+		gaim_account_set_int(account, YAHOO_PICCKSUM_SETTING, 0);
+		gaim_account_set_int(account, YAHOO_PICEXPIRE_SETTING, 0);
+		/* TODO: check if we're connected and tell everyone we ain't not one no more */
+	} else if (!stat(iconfile, &st)) {
+		file = fopen(iconfile, "rb");
+		if (file) {
+			GString *s = g_string_sized_new(st.st_size);
+			size_t len;
+			struct yahoo_buddy_icon_upload_data *d;
+			int oldcksum = gaim_account_get_int(account, YAHOO_PICCKSUM_SETTING, 0);
+			int expire = gaim_account_get_int(account, YAHOO_PICEXPIRE_SETTING, 0);
+			const char *oldurl = gaim_account_get_string(account, YAHOO_PICURL_SETTING, NULL);
+
+			g_string_set_size(s, st.st_size);
+			len = fread(s->str, 1, st.st_size, file);
+			fclose(file);
+			g_string_set_size(s, len);
+			yd->picture_checksum = g_string_hash(s);
+
+			if ((yd->picture_checksum == oldcksum) && (expire > (time(NULL) + 60*60*24)) &&
+			    oldcksum && expire && oldurl) {
+				gaim_debug_misc("yahoo", "buddy icon is up to date. Not reuploading.\n");
+				g_string_free(s, TRUE);
+				if (yd->picture_url)
+					g_free(yd->picture_url);
+				yd->picture_url = g_strdup(oldurl);
+				return;
+			}
+
+			d = g_new0(struct yahoo_buddy_icon_upload_data, 1);
+			d->gc = gc;
+			d->str = s;
+			d->fd = -1;
+			d->filename = g_strdup(iconfile);
+
+			if (!yd->logged_in) {
+				yd->picture_upload_todo = d;
+				return;
+			}
+
+			yahoo_buddy_icon_upload(gc, d);
+		} else
+			gaim_debug_error("yahoo",
+				   "Can't open buddy icon file!\n");
+	} else
+		gaim_debug_error("yahooo",
+			   "Can't stat buddy icon file!\n");
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/protocols/yahoo/yahoo_picture.h	Fri Jun 18 07:28:25 2004 +0000
@@ -0,0 +1,39 @@
+/*
+ * gaim
+ *
+ * Gaim is the legal property of its developers, whose names are too numerous
+ * to list here.  Please refer to the COPYRIGHT file distributed with this
+ * source distribution.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#ifndef _YAHOO_PICTURE_H_
+#define _YAHOO_PICTURE_H_
+
+void yahoo_send_buddy_icon_request(GaimConnection *gc, const char *who);
+void yahoo_send_picture_info(GaimConnection *gc, const char *who);
+
+void yahoo_process_picture(GaimConnection *gc, struct yahoo_packet *pkt);
+void yahoo_process_picture_update(GaimConnection *gc, struct yahoo_packet *pkt);
+void yahoo_process_picture_checksum(GaimConnection *gc, struct yahoo_packet *pkt);
+void yahoo_process_picture_upload(GaimConnection *gc, struct yahoo_packet *pkt);
+
+void yahoo_set_buddy_icon(GaimConnection *gc, const char *iconfile);
+void yahoo_buddy_icon_upload(GaimConnection *gc, struct yahoo_buddy_icon_upload_data *d);
+void yahoo_buddy_icon_upload_data_free(struct yahoo_buddy_icon_upload_data *d);
+
+#endif /* _YAHOO_PICTURE_H_ */