changeset 32476:c4c9ebac3d7f

MSN Patch from Thijs (xnyhps) Alkemade which do verify/convert to UTF-8 incoming OIM. Fixes #14884 committer: masca@cpw.pidgin.im
author thijsalkemade@gmail.com
date Fri, 20 Jan 2012 05:15:22 +0000
parents ec30049bdf5b
children e456bcd69588
files libpurple/protocols/msn/oim.c
diffstat 1 files changed, 45 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/msn/oim.c	Thu Jan 19 03:38:33 2012 +0000
+++ b/libpurple/protocols/msn/oim.c	Fri Jan 20 05:15:22 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,45 @@
 		}
 	} else {
 		decode_msg = (char *)purple_base64_decode(message->body, &body_len);
+		charset = msn_message_get_charset(message);
+	}
+
+
+	if (charset && !(purple_utf8_strcasecmp(charset, "UTF-8") || purple_utf8_strcasecmp(charset, "UTF8"))) {
+		clean_msg = g_convert(decode_msg, strlen(decode_msg), "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", charset, clean);
+
+			msn_oim_post_delete_msg(rdata);
+			msn_message_unref(message);
+
+			g_free(decode_msg);
+			g_free(clean);
+
+			return;
+		}
+
+		g_free(decode_msg);
+
+	} else {
+		if (!g_utf8_validate(decode_msg, -1, 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", clean);
+
+			msn_oim_post_delete_msg(rdata);
+			msn_message_unref(message);
+
+			g_free(decode_msg);
+			g_free(clean);
+
+			return;
+		}
+
+		clean_msg = decode_msg;
 	}
 
 	from = msn_message_get_header_value(message, "X-OIM-originatingSource");
@@ -695,7 +737,7 @@
 	purple_debug_info("msn", "oim Date:{%s},passport{%s}\n",
 	                  date, passport);
 
-	serv_got_im(purple_account_get_connection(rdata->oim->session->account), 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 +746,7 @@
 	msn_oim_post_delete_msg(rdata);
 
 	g_free(passport);
-	g_free(decode_msg);
+	g_free(clean_msg);
 	msn_message_unref(message);
 }