pidgin: e1bd4479: Add purple_utf8_strip_unprintables and u...

darkrain42 at pidgin.im darkrain42 at pidgin.im
Wed Jun 3 18:15:44 EDT 2009


-----------------------------------------------------------------
Revision: e1bd4479e6211023f43781c3f4864d56fc9bb1b3
Ancestor: aa75a8b95f9bc37e78de2c743bb76c343e3ad515
Author: darkrain42 at pidgin.im
Date: 2009-06-03T19:09:16
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/e1bd4479e6211023f43781c3f4864d56fc9bb1b3

Modified files:
        ChangeLog ChangeLog.API libpurple/protocols/jabber/message.c
        libpurple/util.c libpurple/util.h

ChangeLog: 

Add purple_utf8_strip_unprintables and use it on outgoing XMPP messages.

We will no longer send messages which contain entities considered invalid
in XML 1.0 (i.e.  and other ASCII control characters). Closes #5768.

-------------- next part --------------
============================================================
--- ChangeLog	adcb3aaa97f259bde40bc63d0d4956ef6882fcdb
+++ ChangeLog	f09799c4c50c259002d1eb4b50d8712c1b630a13
@@ -41,6 +41,8 @@ version 2.6.0 (??/??/2009):
 	* /affiliate and /role will now list the room members with the specified
 	  affiliation/role if possible. (Andrei Mozzhuhin)
 	* Put section breaks between resources in "Get Info" to improve readability.
+	* Silently remove invalid XML 1.0 entities (e.g. ASCII control characters)
+	  from sent messages.
 	* XHTML markup is only included in outgoing messages when the message
 	  contains formatting.
 	* Show when the user was last logged in when doing "Get Info" on an offline
============================================================
--- ChangeLog.API	4d73dec8b0061eac0c5c850d9b72aa3a525ca241
+++ ChangeLog.API	6f29af39ec81834a8433f7473785276404c6e347
@@ -49,6 +49,7 @@ version 2.6.0 (??/??/2009):
 		* purple_request_field_get_ui_data
 		* purple_request_field_set_ui_data
 		* purple_strequal
+		* purple_utf8_strip_unprintables
 		* xmlnode_from_file
 		* xmlnode_get_parent
 		* xmlnode_set_attrib_full
============================================================
--- libpurple/protocols/jabber/message.c	c68b642fa86509494449802815971fc28763b904
+++ libpurple/protocols/jabber/message.c	3dad8a124eef4953da0be9f671d8f1dd464ead29
@@ -1190,7 +1190,9 @@ int jabber_message_send_im(PurpleConnect
 			jm->typing_style |= JM_TS_JEP_0022;
 	}
 
-	purple_markup_html_to_xhtml(msg, &xhtml, &jm->body);
+	tmp = purple_utf8_strip_unprintables(msg);
+	purple_markup_html_to_xhtml(tmp, &xhtml, &jm->body);
+	g_free(tmp);
 	tmp = jabber_message_smileyfy_xhtml(jm, xhtml);
 	if (tmp) {
 		g_free(xhtml);
@@ -1231,7 +1233,9 @@ int jabber_message_send_chat(PurpleConne
 	jm->to = g_strdup_printf("%s@%s", chat->room, chat->server);
 	jm->id = jabber_get_next_id(jm->js);
 
+	tmp = purple_utf8_strip_unprintables(msg);
 	purple_markup_html_to_xhtml(msg, &xhtml, &jm->body);
+	g_free(tmp);
 	tmp = jabber_message_smileyfy_xhtml(jm, xhtml);
 	if (tmp) {
 		g_free(xhtml);
============================================================
--- libpurple/util.c	163214ee10eee990bb7736e0902eeb4eada9e92e
+++ libpurple/util.c	6f0104b30052597717ce66df8bdd300bcbca215a
@@ -4424,6 +4424,34 @@ purple_utf8_salvage(const char *str)
 	return g_string_free(workstr, FALSE);
 }
 
+gchar *
+purple_utf8_strip_unprintables(const gchar *str)
+{
+	gchar *workstr, *iter;
+
+	g_return_val_if_fail(str != NULL, NULL);
+	g_return_val_if_fail(g_utf8_validate(str, -1, NULL), NULL);
+
+	workstr = iter = g_new(gchar, strlen(str) + 1);
+	while (*str) {
+		gunichar c = g_utf8_get_char(str);
+		const gchar *next = g_utf8_next_char(str);
+		size_t len = next - str;
+
+		if (g_unichar_isprint(c)) {
+			memcpy(iter, str, len);
+			iter += len;
+		}
+
+		str = next;
+	}
+
+	/* nul-terminate the new string */
+	*iter = '\0';
+
+	return workstr;
+}
+
 /*
  * This function is copied from g_strerror() but changed to use
  * gai_strerror().
============================================================
--- libpurple/util.h	8bb1a61e0826ad7b0cd3dc7dd526620006e8a926
+++ libpurple/util.h	02f703769da5bbbed302846596311003de8ddc4c
@@ -1248,6 +1248,21 @@ gchar *purple_utf8_salvage(const char *s
 gchar *purple_utf8_salvage(const char *str);
 
 /**
+ * Removes unprintable characters from a UTF-8 string. These characters
+ * (in particular low-ASCII characters) are invalid in XML 1.0 and thus
+ * are not allowed in XMPP and are rejected by libxml2 by default. This
+ * function uses g_unichar_isprint to determine what characters should
+ * be stripped. The returned string must be freed by the caller.
+ *
+ * @param str A valid UTF-8 string.
+ *
+ * @return A newly allocated UTF-8 string without the unprintable characters.
+ *
+ * @see g_unichar_isprint
+ */
+gchar *purple_utf8_strip_unprintables(const gchar *str);
+
+/**
  * Return the UTF-8 version of gai_strerror().  It calls gai_strerror()
  * then converts the result to UTF-8.  This function is analogous to
  * g_strerror().


More information about the Commits mailing list