changeset 32507:29e9fe5b9264

Pluck fixes for OIM charset conversion. *** Plucked rev 3053d6a37cc6d8774aba7607b992a4408216adcd (thijsalkemade@gmail.com): MSN Patch from Thijs (xnyhps) Alkemade which do verify/convert to UTF-8 incoming OIM. Fixes #14884 *** Plucked rev ecabfaee8a1ca02e18ebadbb41cdcce19e78bc2e (masca@cpw.pidgin.im): Apply second patch from xnyhps this time to show the message salvaged to user. Refs #14884 *** Plucked rev b1b8c222ab921963f43e83502b6c6e2e4489a8c4 (qulogic@pidgin.im): Add newlines to debug messages, and word-wrap lines. *** Plucked rev fdb56683f2b5f88f7b388aaef6c53c810d19e374 (qulogic@pidgin.im): We know the length of decode_msg here. *** Plucked rev f12c9f6a6c31bcd3512f162209285a88a86595ff (qulogic@pidgin.im): This extra if-level can be dropped.
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Mon, 06 Feb 2012 22:42:32 +0000
parents 9f26efb70c67
children 704e168fd9ae
files libpurple/protocols/msn/oim.c
diffstat 1 files changed, 46 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/msn/oim.c	Mon Jan 23 16:22:33 2012 +0000
+++ b/libpurple/protocols/msn/oim.c	Mon Feb 06 22:42:32 2012 +0000
@@ -606,11 +606,12 @@
 	const char *date;
 	const char *from;
 	const char *boundary;
-	char *decode_msg = NULL;
+	char *decode_msg = NULL, *clean_msg = NULL;
 	gsize body_len;
 	char **tokens;
 	char *passport = NULL;
 	time_t stamp;
+	const char *charset = NULL;
 
 	message = msn_message_new(MSN_MSG_UNKNOWN);
 
@@ -638,6 +639,8 @@
 			type = msn_message_get_content_type(multipart);
 			if (type && !strcmp(type, "text/plain")) {
 				decode_msg = (char *)purple_base64_decode(multipart->body, &body_len);
+				charset = msn_message_get_charset(multipart);
+
 				msn_message_unref(multipart);
 				break;
 			}
@@ -654,6 +657,46 @@
 		}
 	} else {
 		decode_msg = (char *)purple_base64_decode(message->body, &body_len);
+		charset = msn_message_get_charset(message);
+	}
+
+	if (charset && !((strncasecmp(charset, "UTF-8", 5) == 0) || (strncasecmp(charset, "UTF8", 4) == 0))) {
+		clean_msg = g_convert(decode_msg, body_len, "UTF-8", charset, NULL, NULL, NULL);
+
+		if (!clean_msg) {
+			char *clean = purple_utf8_salvage(decode_msg);
+
+			purple_debug_error("msn", "Failed to convert charset from %s to UTF-8 for OIM message: %s\n", charset, clean);
+
+			clean_msg = g_strdup_printf(_("%s (There was an error receiving this message. "
+			                              "Converting the encoding from %s to UTF-8 failed.)"),
+			                            clean, charset);
+			g_free(clean);
+		}
+
+		g_free(decode_msg);
+
+	} else if (!g_utf8_validate(decode_msg, body_len, NULL)) {
+		char *clean = purple_utf8_salvage(decode_msg);
+
+		purple_debug_error("msn", "Received an OIM message that is not UTF-8,"
+		                          " and no encoding specified: %s\n", clean);
+
+		if (charset) {
+			clean_msg = g_strdup_printf(_("%s (There was an error receiving this message."
+			                              " The charset was %s, but it was not valid UTF-8.)"),
+			                            clean, charset);
+		} else {
+			clean_msg = g_strdup_printf(_("%s (There was an error receiving this message."
+			                              " The charset was missing, but it was not valid UTF-8.)"),
+			                            clean);
+		}
+
+		g_free(clean);
+		g_free(decode_msg);
+
+	} else {
+		clean_msg = decode_msg;
 	}
 
 	from = msn_message_get_header_value(message, "X-OIM-originatingSource");
@@ -695,7 +738,7 @@
 	purple_debug_info("msn", "oim Date:{%s},passport{%s}\n",
 	                  date, passport);
 
-	serv_got_im(rdata->oim->session->account->gc, passport, decode_msg, 0,
+	serv_got_im(purple_account_get_connection(rdata->oim->session->account), passport, clean_msg, 0,
 	            stamp);
 
 	/*Now get the oim message ID from the oim_list.
@@ -704,7 +747,7 @@
 	msn_oim_post_delete_msg(rdata);
 
 	g_free(passport);
-	g_free(decode_msg);
+	g_free(clean_msg);
 	msn_message_unref(message);
 }