diff libpurple/protocols/msn/msg.c @ 32802:fa666e7f747e

propagate from branch 'im.pidgin.pidgin.2.x.y' (head 55372aac7eca74e2a3121931b867702d2fdfe299) to branch 'im.pidgin.pidgin' (head 1f233991b11d30d7bd9d1e058e19fe162a9600f3)
author Elliott Sales de Andrade <qulogic@pidgin.im>
date Fri, 25 May 2012 19:41:04 +0000
parents 4acc5b98f143 aacfb71133cc
children
line wrap: on
line diff
--- a/libpurple/protocols/msn/msg.c	Fri May 25 19:28:41 2012 +0000
+++ b/libpurple/protocols/msn/msg.c	Fri May 25 19:41:04 2012 +0000
@@ -257,13 +257,47 @@
 		msg->body[msg->body_len] = '\0';
 	}
 
-	if ((!content_type || !strcmp(content_type, "text/plain"))
-			&& msg->charset == NULL) {
-		char *body = g_convert(msg->body, msg->body_len, "UTF-8",
-				"ISO-8859-1", NULL, &msg->body_len, NULL);
-		g_free(msg->body);
-		msg->body = body;
-		msg->charset = g_strdup("UTF-8");
+	if (msg->body && content_type && purple_str_has_prefix(content_type, "text/")) {
+		char *body = NULL;
+
+		if (msg->charset == NULL || g_str_equal(msg->charset, "UTF-8")) {
+			/* Charset is UTF-8 */
+			if (!g_utf8_validate(msg->body, msg->body_len, NULL)) {
+				purple_debug_warning("msn", "Message contains invalid "
+						"UTF-8. Attempting to salvage.\n");
+				body = purple_utf8_salvage(msg->body);
+				payload_len = strlen(body);
+			}
+		} else {
+			/* Charset is something other than UTF-8 */
+			GError *err = NULL;
+			body = g_convert(msg->body, msg->body_len, "UTF-8",
+					msg->charset, NULL, &payload_len, &err);
+			if (!body || err) {
+				purple_debug_warning("msn", "Unable to convert message from "
+						"%s to UTF-8: %s\n", msg->charset,
+						err ? err->message : "Unknown error");
+				if (err)
+					g_error_free(err);
+
+				/* Fallback to ISO-8859-1 */
+				g_free(body);
+				body = g_convert(msg->body, msg->body_len, "UTF-8",
+						"ISO-8859-1", NULL, &payload_len, NULL);
+				if (!body) {
+					g_free(msg->body);
+					msg->body = NULL;
+					msg->body_len = 0;
+				}
+			}
+		}
+
+		if (body) {
+			g_free(msg->body);
+			msg->body = body;
+			msg->body_len = payload_len;
+			msn_message_set_charset(msg, "UTF-8");
+		}
 	}
 
 	g_free(tmp_base);