changeset 23017:c1b8ee4df034

applied changes from b92fdea98a3ea454f3e9debd7f6ac17e5ae36ef6 through e9fa06c5d654248927dd0838d27cfb6b712a153a
author Daniel Atallah <daniel.atallah@gmail.com>
date Thu, 19 Jun 2008 02:54:57 +0000
parents 49264628f72d
children 1c7896012802
files libpurple/protocols/yahoo/yahoo_aliases.c
diffstat 1 files changed, 73 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/yahoo/yahoo_aliases.c	Thu Jun 19 02:54:25 2008 +0000
+++ b/libpurple/protocols/yahoo/yahoo_aliases.c	Thu Jun 19 02:54:57 2008 +0000
@@ -48,7 +48,7 @@
  */
 struct callback_data {
 	PurpleConnection *gc;
-	char *id;
+	gchar *id;
 };
 
 
@@ -57,7 +57,7 @@
  **************************************************************************/
 
 static void
-yahoo_fetch_aliases_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,const gchar *url_text, size_t len, const gchar *error_message)
+yahoo_fetch_aliases_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, size_t len, const gchar *error_message)
 {
 	struct callback_data *cb = user_data;
 	PurpleConnection *gc = cb->gc;
@@ -69,8 +69,8 @@
 		purple_debug_info("yahoo", "No Aliases to process.%s%s\n",
 						  error_message ? " Error:" : "", error_message ? error_message : "");
 	} else {
-		gchar *full_name, *nick_name, *alias;
-		const char *yid, *id, *fn, *ln, *nn;
+		gchar *full_name, *nick_name;
+		const char *yid, *id, *fn, *ln, *nn, *alias;
 		YahooFriend *f;
 		PurpleBuddy *b;
 		xmlnode *item, *contacts;
@@ -79,7 +79,9 @@
 		contacts = xmlnode_from_str(url_text, -1);
 
 		if (contacts == NULL) {
-			purple_debug_error("yahoo_aliases","Badly formed XML\n");
+			purple_debug_error("yahoo", "Badly formed Alias XML\n");
+			g_free(cb->id);
+			g_free(cb);
 			return;
 		}
 		purple_debug_info("yahoo", "Fetched %" G_GSIZE_FORMAT
@@ -89,20 +91,21 @@
 		for(item = xmlnode_get_child(contacts, "ct"); item; item = xmlnode_get_next_twin(item)) {
 			/* Yahoo replies with two types of contact (ct) record, we are only interested in the alias ones */
 			if ((yid = xmlnode_get_attrib(item, "yi"))) {
-		                /* Grab all the bits of information we can */
-				fn = xmlnode_get_attrib(item,"fn");
-				ln = xmlnode_get_attrib(item,"ln");
-				nn = xmlnode_get_attrib(item,"nn");
-				id = xmlnode_get_attrib(item,"id");
+				/* Grab all the bits of information we can */
+				fn = xmlnode_get_attrib(item, "fn");
+				ln = xmlnode_get_attrib(item, "ln");
+				nn = xmlnode_get_attrib(item, "nn");
+				id = xmlnode_get_attrib(item, "id");
 
-				full_name = nick_name = alias = NULL;
+				full_name = nick_name = NULL;
+				alias = NULL;
 
 				/* Yahoo stores first and last names separately, lets put them together into a full name */
 				if (yd->jp)
 					full_name = g_strstrip(g_strdup_printf("%s %s", (ln != NULL ? ln : "") , (fn != NULL ? fn : "")));
 				else
 					full_name = g_strstrip(g_strdup_printf("%s %s", (fn != NULL ? fn : "") , (ln != NULL ? ln : "")));
-				nick_name = (nn != NULL ? g_strstrip(g_strdup_printf("%s", nn)) : NULL);
+				nick_name = (nn != NULL ? g_strstrip(g_strdup(nn)) : NULL);
 
 				if (nick_name != NULL)
 					alias = nick_name;   /* If we have a nickname from Yahoo, let's use it */
@@ -115,16 +118,17 @@
 
 				/*  If we don't find a matching buddy, ignore the alias !!  */
 				if (f != NULL && b != NULL) {
+					const char *buddy_alias = purple_buddy_get_alias(b);
 					yahoo_friend_set_alias_id(f, id);
 
 					/* Finally, if we received an alias, we better update the buddy list */
 					if (alias != NULL) {
 						serv_got_alias(cb->gc, yid, alias);
-						purple_debug_info("yahoo","Fetched alias '%s' (%s)\n",alias,id);
-					} else if (b->alias != alias && strcmp(b->alias, "") != 0) {
+						purple_debug_info("yahoo", "Fetched alias '%s' (%s)\n", alias, id);
+					} else if (buddy_alias != NULL && strcmp(buddy_alias, "") != 0) {
 					/* Or if we have an alias that Yahoo doesn't, send it up */
-						yahoo_update_alias(cb->gc, yid, b->alias);
-						purple_debug_info("yahoo","Sent alias '%s'\n", b->alias);
+						yahoo_update_alias(cb->gc, yid, buddy_alias);
+						purple_debug_info("yahoo", "Sent updated alias '%s'\n", buddy_alias);
 					}
 				} else {
 					purple_debug_info("yahoo", "Bizarre, received alias for %s, but they are not on your list...\n", yid);
@@ -136,6 +140,7 @@
 		}
 		xmlnode_free(contacts);
 	}
+
 	g_free(cb->id);
 	g_free(cb);
 }
@@ -146,14 +151,15 @@
 	struct yahoo_data *yd = gc->proto_data;
 	struct callback_data *cb;
 	const char *url;
-	char *request, *webpage, *webaddress;
+	gchar *request, *webpage, *webaddress;
 	PurpleUtilFetchUrlData *url_data;
 
 	gboolean use_whole_url = FALSE;
 
 	/* use whole URL if using HTTP Proxy */
-	if ((gc->account->proxy_info) && (gc->account->proxy_info->type == PURPLE_PROXY_HTTP))
-	    use_whole_url = TRUE;
+	if ((gc->account->proxy_info)
+	    && (gc->account->proxy_info->type == PURPLE_PROXY_HTTP))
+		use_whole_url = TRUE;
 
 	/* Using callback_data so I have access to gc in the callback function */
 	cb = g_new0(struct callback_data, 1);
@@ -167,13 +173,16 @@
 				 "Cookie: T=%s; Y=%s\r\n"
 				 "Host: %s\r\n"
 				 "Cache-Control: no-cache\r\n\r\n",
-				  use_whole_url ? "http://" : "", use_whole_url ? webaddress : "", webpage, yd->cookie_t,yd->cookie_y, webaddress);
+				  use_whole_url ? "http://" : "", use_whole_url ? webaddress : "", webpage,
+				  yd->cookie_t, yd->cookie_y,
+				  webaddress);
 
 	/* We have a URL and some header information, let's connect and get some aliases  */
-	url_data = purple_util_fetch_url_request(url, use_whole_url, NULL, TRUE, request, FALSE, yahoo_fetch_aliases_cb, cb);
-	if (url_data != NULL) {
+	url_data = purple_util_fetch_url_request(url, use_whole_url, NULL, TRUE,
+						 request, FALSE,
+						 yahoo_fetch_aliases_cb, cb);
+	if (url_data != NULL)
 		yd->url_datas = g_slist_prepend(yd->url_datas, url_data);
-	}
 
 	g_free(webaddress);
 	g_free(webpage);
@@ -185,7 +194,7 @@
  **************************************************************************/
 
 static void
-yahoo_update_alias_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,const gchar *url_text, size_t len, const gchar *error_message)
+yahoo_update_alias_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, size_t len, const gchar *error_message)
 {
 	xmlnode *node, *result;
 	struct callback_data *cb = user_data;
@@ -205,17 +214,18 @@
 
 	result = xmlnode_from_str(url_text, -1);
 
-	purple_debug_info("yahoo", "ID: %s, Return data: %s\n",cb->id, url_text);
+	purple_debug_info("yahoo", "ID: %s, Return data: %s\n", cb->id, url_text);
 
 	if (result == NULL) {
-		purple_debug_error("yahoo","Alias update failed: Badly formed response\n");
+		purple_debug_error("yahoo", "Alias update failed: Badly formed response\n");
 		g_free(cb->id);
 		g_free(cb);
 		return;
 	}
 
 	if ((node = xmlnode_get_child(result, "ct"))) {
-		if (g_ascii_strncasecmp(xmlnode_get_attrib(node, "id"), cb->id, strlen(cb->id))==0)
+		const char *update_id = xmlnode_get_attrib(node, "id");
+		if (g_ascii_strncasecmp(update_id, cb->id, strlen(cb->id)) == 0)
 			purple_debug_info("yahoo", "Alias update succeeded\n");
 		else
 			purple_debug_error("yahoo", "Alias update failed (Contact record return mismatch)\n");
@@ -232,27 +242,35 @@
 yahoo_update_alias(PurpleConnection *gc, const char *who, const char *alias)
 {
 	struct yahoo_data *yd;
-	char *content, *url, *request, *webpage, *webaddress, *strtmp;
-	char *escaped_alias, *alias_jp, *converted_alias_jp;
-	int inttmp;
+	const char *url, *alias_id;
+	gchar *content, *request, *webpage, *webaddress;
 	struct callback_data *cb;
 	PurpleUtilFetchUrlData *url_data;
 	gboolean use_whole_url = FALSE;
 	YahooFriend *f;
 
 	/* use whole URL if using HTTP Proxy */
-	if ((gc->account->proxy_info) && (gc->account->proxy_info->type == PURPLE_PROXY_HTTP))
-	    use_whole_url = TRUE;
+	if ((gc->account->proxy_info)
+	    && (gc->account->proxy_info->type == PURPLE_PROXY_HTTP))
+		use_whole_url = TRUE;
 
-	g_return_if_fail(alias != NULL);
 	g_return_if_fail(who != NULL);
 	g_return_if_fail(gc != NULL);
 
+	if (alias == NULL)
+		alias = "";
+
 	purple_debug_info("yahoo", "Sending '%s' as new alias for user '%s'.\n", alias, who);
 
 	f = yahoo_friend_find(gc, who);
 	if (f == NULL) {
-		purple_debug_info("yahoo", "Missing proto_data (get_yahoo_aliases must have failed), bailing out\n");
+		purple_debug_error("yahoo", "Missing YahooFriend. Unable to set server alias.\n");
+		return;
+	}
+
+	alias_id = yahoo_friend_get_alias_id(f);
+	if (alias_id == NULL) {
+		purple_debug_error("yahoo", "Missing Yahoo Alias ID (get_yahoo_aliases must have failed), bailing out\n");
 		return;
 	}
 
@@ -260,27 +278,28 @@
 
 	/* Using callback_data so I have access to gc in the callback function */
 	cb = g_new0(struct callback_data, 1);
-	cb->id = g_strdup(yahoo_friend_get_alias_id(f));
+	cb->id = g_strdup(alias_id);
 	cb->gc = gc;
 
 	/*  Build all the info to make the web request */
-	url = yd->jp? YAHOOJP_ALIAS_UPDATE_URL: YAHOO_ALIAS_UPDATE_URL;
-	purple_url_parse(url, &webaddress, &inttmp, &webpage, &strtmp, &strtmp);
+	url = yd->jp ? YAHOOJP_ALIAS_UPDATE_URL: YAHOO_ALIAS_UPDATE_URL;
+	purple_url_parse(url, &webaddress, NULL, &webpage, NULL, NULL);
 
 	if (yd->jp) {
-		alias_jp = g_convert(alias, strlen(alias), "EUC-JP", "UTF-8", NULL, NULL, NULL);
-		converted_alias_jp = yahoo_convert_to_numeric(alias_jp);
+		gchar *alias_jp = g_convert(alias, -1, "EUC-JP", "UTF-8", NULL, NULL, NULL);
+		gchar *converted_alias_jp = yahoo_convert_to_numeric(alias_jp);
 		content = g_strdup_printf("<ab k=\"%s\" cc=\"1\">\n"
-		                          "<ct e=\"1\"  yi='%s' id='%s' nn='%s' pr='0' />\n</ab>\r\n",
-		                          gc->account->username, who, yahoo_friend_get_alias_id(f), converted_alias_jp);
+					  "<ct e=\"1\"  yi='%s' id='%s' nn='%s' pr='0' />\n</ab>\r\n",
+					   purple_account_get_username(gc->account),
+					   who, alias_id, converted_alias_jp);
 		free(converted_alias_jp);
 		g_free(alias_jp);
-	}
-	else {
-		escaped_alias = g_markup_escape_text(alias, strlen(alias));
+	} else {
+		gchar *escaped_alias = g_markup_escape_text(alias, -1);
 		content = g_strdup_printf("<?xml version=\"1.0\" encoding=\"utf-8\"?><ab k=\"%s\" cc=\"1\">\n"
-		                          "<ct e=\"1\"  yi='%s' id='%s' nn='%s' pr='0' />\n</ab>\r\n",
-		                          gc->account->username, who, yahoo_friend_get_alias_id(f), escaped_alias);
+					  "<ct e=\"1\"  yi='%s' id='%s' nn='%s' pr='0' />\n</ab>\r\n",
+					  purple_account_get_username(gc->account),
+					  who, alias_id, escaped_alias);
 		g_free(escaped_alias);
 	}
 
@@ -291,15 +310,19 @@
 				  "Content-Length: %" G_GSIZE_FORMAT "\r\n"
 				  "Cache-Control: no-cache\r\n\r\n"
 				  "%s",
-				  use_whole_url ? "http://" : "", use_whole_url ? webaddress : "", webpage, yd->cookie_t,yd->cookie_y, webaddress,
-			 	  strlen(content), content);
+				  use_whole_url ? "http://" : "", use_whole_url ? webaddress : "", webpage,
+				  yd->cookie_t, yd->cookie_y,
+				  webaddress,
+				  strlen(content),
+				  content);
 
 	/* We have a URL and some header information, let's connect and update the alias  */
 	url_data = purple_util_fetch_url_request(url, use_whole_url, NULL, TRUE, request, FALSE, yahoo_update_alias_cb, cb);
-	if (url_data != NULL) {
+	if (url_data != NULL)
 		yd->url_datas = g_slist_prepend(yd->url_datas, url_data);
-	}
 
+	g_free(webpage);
+	g_free(webaddress);
 	g_free(content);
 	g_free(request);
 }