/soc/2012/tomkiewicz/gg: b20aed44c357: Gadu-Gadu: setting own bu...

Tomasz Wasilczyk tomkiewicz at cpw.pidgin.im
Mon Aug 6 18:12:48 EDT 2012


Changeset: b20aed44c357a117b698a330071f2d8ec6841868
Author:	 Tomasz Wasilczyk <tomkiewicz at cpw.pidgin.im>
Date:	 2012-08-07 00:12 +0200
Branch:	 soc.2012.gg
URL: http://hg.pidgin.im/soc/2012/tomkiewicz/gg/rev/b20aed44c357

Description:

Gadu-Gadu: setting own buddy icon

diffstat:

 libpurple/protocols/gg/avatar.c |  108 ++++++++++++++++++++++++++++++++++++++++
 libpurple/protocols/gg/avatar.h |    3 +
 libpurple/protocols/gg/gg.c     |    4 +-
 3 files changed, 113 insertions(+), 2 deletions(-)

diffs (182 lines):

diff --git a/libpurple/protocols/gg/avatar.c b/libpurple/protocols/gg/avatar.c
--- a/libpurple/protocols/gg/avatar.c
+++ b/libpurple/protocols/gg/avatar.c
@@ -4,6 +4,7 @@
 
 #include "gg.h"
 #include "utils.h"
+#include "oauth/oauth-purple.h"
 
 // Common
 
@@ -33,6 +34,21 @@ static void ggp_avatar_buddy_update_rece
 
 #define GGP_AVATAR_BUDDY_URL "http://avatars.gg.pl/%u/s,big"
 
+// Own avatar setting
+
+typedef struct
+{
+	PurpleStoredImage *img;
+} ggp_avatar_own_data;
+
+static void ggp_avatar_own_got_token(PurpleConnection *gc, const gchar *token,
+	gpointer img);
+static void ggp_avatar_own_sent(PurpleUtilFetchUrlData *url_data,
+	gpointer user_data, const gchar *url_text, gsize len,
+	const gchar *error_message);
+
+#define GGP_AVATAR_RESPONSE_MAX 10240
+
 /*******************************************************************************
  * Common.
  ******************************************************************************/
@@ -43,6 +59,7 @@ void ggp_avatar_setup(PurpleConnection *
 
 	avdata->pending_updates = NULL;
 	avdata->current_update = NULL;
+	avdata->own_data = g_new0(ggp_avatar_own_data, 1);
 	
 	avdata->timer = purple_timeout_add_seconds(1, ggp_avatar_timer_cb, gc);
 }
@@ -62,6 +79,8 @@ void ggp_avatar_cleanup(PurpleConnection
 		g_free(current_update);
 	}
 	avdata->current_update = NULL;
+	
+	g_free(avdata->own_data);
 
 	g_list_free_full(avdata->pending_updates, &g_free);
 	avdata->pending_updates = NULL;
@@ -253,3 +272,92 @@ static void ggp_avatar_buddy_update_rece
 		pending_update->timestamp);
 	g_free(pending_update);
 }
+
+/*******************************************************************************
+ * Own avatar setting.
+ ******************************************************************************/
+
+/**
+ * TODO: use new, GG11 method, when IMToken will be provided by libgadu.
+ *
+ * POST https://avatars.mpa.gg.pl/avatars/user,<uin>/0
+ * Authorization: IMToken 0123456789abcdef0123456789abcdef01234567
+ * photo=<avatar content>
+ */
+
+void ggp_avatar_own_set(PurpleConnection *gc, PurpleStoredImage *img)
+{
+	ggp_avatar_own_data *own_data = ggp_avatar_get_avdata(gc)->own_data;
+	
+	purple_debug_info("gg", "ggp_avatar_own_set(%p, %p)", gc, img);
+	
+	if (img == NULL)
+	{
+		purple_debug_warning("gg", "ggp_avatar_own_set: avatar removing"
+			" is probably not possible within old protocol");
+		return;
+	}
+	
+	own_data->img = img;
+	
+	ggp_oauth_request(gc, ggp_avatar_own_got_token, img);
+}
+
+static void ggp_avatar_own_got_token(PurpleConnection *gc, const gchar *token,
+	gpointer img)
+{
+	ggp_avatar_own_data *own_data = ggp_avatar_get_avdata(gc)->own_data;
+	gchar *img_data, *img_data_e, *request, *request_data;
+	PurpleAccount *account = purple_connection_get_account(gc);
+	uin_t uin = ggp_str_to_uin(purple_account_get_username(account));
+	
+	if (img != own_data->img)
+	{
+		purple_debug_warning("gg", "ggp_avatar_own_got_token: "
+			"avatar was changed in meantime\n");
+		return;
+	}
+	own_data->img = NULL;
+	
+	img_data = purple_base64_encode(purple_imgstore_get_data(img),
+		purple_imgstore_get_size(img));
+	img_data_e = g_uri_escape_string(img_data, NULL, FALSE);
+	g_free(img_data);
+	request_data = g_strdup_printf("uin=%d&photo=%s", uin, img_data_e);
+	g_free(img_data_e);
+	
+	request = g_strdup_printf(
+		"POST /upload HTTP/1.1\r\n"
+		"Host: avatars.nowe.gg\r\n"
+		"Authorization: %s\r\n"
+		"From: avatars to avatars\r\n"
+		"Content-Length: %u\r\n"
+		"Content-Type: application/x-www-form-urlencoded\r\n"
+		"\r\n%s",
+		token, strlen(request_data), request_data);
+	g_free(request_data);
+	
+	purple_debug_misc("gg", "ggp_avatar_own_got_token: "
+		"uploading new avatar...\n");
+	purple_util_fetch_url_request(account, "http://avatars.nowe.gg/upload",
+		FALSE, NULL, TRUE, request, FALSE, GGP_AVATAR_RESPONSE_MAX,
+		ggp_avatar_own_sent, gc);
+	
+	g_free(request);
+}
+
+static void ggp_avatar_own_sent(PurpleUtilFetchUrlData *url_data,
+	gpointer user_data, const gchar *url_text, gsize len,
+	const gchar *error_message)
+{
+	PurpleConnection *gc = user_data;
+	
+	if (!PURPLE_CONNECTION_IS_VALID(gc))
+		return;
+	
+	if (len == 0)
+		purple_debug_error("gg", "ggp_avatar_own_sent: "
+			"avatar not sent. %s\n", error_message);
+	else
+		purple_debug_info("gg", "ggp_avatar_own_sent: %s\n", url_text);
+}
diff --git a/libpurple/protocols/gg/avatar.h b/libpurple/protocols/gg/avatar.h
--- a/libpurple/protocols/gg/avatar.h
+++ b/libpurple/protocols/gg/avatar.h
@@ -10,6 +10,7 @@ typedef struct
 	GList *pending_updates;
 	
 	gpointer current_update;
+	gpointer own_data;
 } ggp_avatar_session_data;
 
 void ggp_avatar_setup(PurpleConnection *gc);
@@ -18,4 +19,6 @@ void ggp_avatar_cleanup(PurpleConnection
 void ggp_avatar_buddy_update(PurpleConnection *gc, uin_t uin, time_t timestamp);
 void ggp_avatar_buddy_remove(PurpleConnection *gc, uin_t uin);
 
+void ggp_avatar_own_set(PurpleConnection *gc, PurpleStoredImage *img);
+
 #endif /* _GGP_AVATAR_H */
diff --git a/libpurple/protocols/gg/gg.c b/libpurple/protocols/gg/gg.c
--- a/libpurple/protocols/gg/gg.c
+++ b/libpurple/protocols/gg/gg.c
@@ -2111,7 +2111,7 @@ static PurplePluginProtocolInfo prpl_inf
 	OPT_PROTO_REGISTER_NOSCREENNAME | OPT_PROTO_IM_IMAGE,
 	NULL,				/* user_splits */
 	NULL,				/* protocol_options */
-	{"png", 32, 32, 96, 96, 0, PURPLE_ICON_SCALE_DISPLAY},	/* icon_spec */
+	{"png", 1, 1, 200, 200, 0, PURPLE_ICON_SCALE_DISPLAY | PURPLE_ICON_SCALE_SEND},	/* icon_spec */
 	ggp_list_icon,			/* list_icon */
 	ggp_list_emblem,		/* list_emblem */
 	ggp_status_text,		/* status_text */
@@ -2154,7 +2154,7 @@ static PurplePluginProtocolInfo prpl_inf
 	ggp_buddy_free,			/* buddy_free */
 	NULL,				/* convo_closed */
 	ggp_normalize,			/* normalize */
-	NULL,				/* set_buddy_icon */
+	ggp_avatar_own_set,		/* set_buddy_icon */
 	NULL,				/* remove_group */
 	NULL,				/* get_cb_real_name */
 	NULL,				/* set_chat_topic */



More information about the Commits mailing list