MSN Patch from Thijs (xnyhps) Alkemade which do verify/convert to UTF-8
authorThijs Alkemade <thijsalkemade@gmail.com>
Fri, 20 Jan 2012 05:15:22 +0000
changeset1b1b97b8e942 pushlog
parent 3165d176fe94
child f9eeb175a5c9
MSN Patch from Thijs (xnyhps) Alkemade which do verify/convert to UTF-8
incoming OIM.

Fixes #14884


committer: Jorge VillaseƱor <masca@cpw.pidgin.im>
libpurple/protocols/msn/oim.c
     1.1 --- a/libpurple/protocols/msn/oim.c
     1.2 +++ b/libpurple/protocols/msn/oim.c
     1.3 @@ -606,11 +606,12 @@
     1.4  	const char *date;
     1.5  	const char *from;
     1.6  	const char *boundary;
     1.7 -	char *decode_msg = NULL;
     1.8 +	char *decode_msg = NULL, *clean_msg = NULL;
     1.9  	gsize body_len;
    1.10  	char **tokens;
    1.11  	char *passport = NULL;
    1.12  	time_t stamp;
    1.13 +	const char *charset = NULL;
    1.14  
    1.15  	message = msn_message_new(MSN_MSG_UNKNOWN);
    1.16  
    1.17 @@ -638,6 +639,8 @@
    1.18  			type = msn_message_get_content_type(multipart);
    1.19  			if (type && !strcmp(type, "text/plain")) {
    1.20  				decode_msg = (char *)purple_base64_decode(multipart->body, &body_len);
    1.21 +				charset = msn_message_get_charset(multipart);
    1.22 +
    1.23  				msn_message_unref(multipart);
    1.24  				break;
    1.25  			}
    1.26 @@ -654,6 +657,45 @@
    1.27  		}
    1.28  	} else {
    1.29  		decode_msg = (char *)purple_base64_decode(message->body, &body_len);
    1.30 +		charset = msn_message_get_charset(message);
    1.31 +	}
    1.32 +
    1.33 +
    1.34 +	if (charset && !(purple_utf8_strcasecmp(charset, "UTF-8") || purple_utf8_strcasecmp(charset, "UTF8"))) {
    1.35 +		clean_msg = g_convert(decode_msg, strlen(decode_msg), "UTF-8", charset, NULL, NULL, NULL);
    1.36 +
    1.37 +		if (!clean_msg) {
    1.38 +			char *clean = purple_utf8_salvage(decode_msg);
    1.39 +
    1.40 +			purple_debug_error("msn", "Failed to convert charset from %s to UTF-8 for OIM message: %s", charset, clean);
    1.41 +
    1.42 +			msn_oim_post_delete_msg(rdata);
    1.43 +			msn_message_unref(message);
    1.44 +
    1.45 +			g_free(decode_msg);
    1.46 +			g_free(clean);
    1.47 +
    1.48 +			return;
    1.49 +		}
    1.50 +
    1.51 +		g_free(decode_msg);
    1.52 +
    1.53 +	} else {
    1.54 +		if (!g_utf8_validate(decode_msg, -1, NULL)) {
    1.55 +			char *clean = purple_utf8_salvage(decode_msg);
    1.56 +
    1.57 +			purple_debug_error("msn", "Received an OIM message that is not UTF-8, and no encoding specified: %s", clean);
    1.58 +
    1.59 +			msn_oim_post_delete_msg(rdata);
    1.60 +			msn_message_unref(message);
    1.61 +
    1.62 +			g_free(decode_msg);
    1.63 +			g_free(clean);
    1.64 +
    1.65 +			return;
    1.66 +		}
    1.67 +
    1.68 +		clean_msg = decode_msg;
    1.69  	}
    1.70  
    1.71  	from = msn_message_get_header_value(message, "X-OIM-originatingSource");
    1.72 @@ -695,7 +737,7 @@
    1.73  	purple_debug_info("msn", "oim Date:{%s},passport{%s}\n",
    1.74  	                  date, passport);
    1.75  
    1.76 -	serv_got_im(purple_account_get_connection(rdata->oim->session->account), passport, decode_msg, 0,
    1.77 +	serv_got_im(purple_account_get_connection(rdata->oim->session->account), passport, clean_msg, 0,
    1.78  	            stamp);
    1.79  
    1.80  	/*Now get the oim message ID from the oim_list.
    1.81 @@ -704,7 +746,7 @@
    1.82  	msn_oim_post_delete_msg(rdata);
    1.83  
    1.84  	g_free(passport);
    1.85 -	g_free(decode_msg);
    1.86 +	g_free(clean_msg);
    1.87  	msn_message_unref(message);
    1.88  }
    1.89