pidgin: d4b4f486: Only include XHTML payload in XMPP messa...

darkrain42 at pidgin.im darkrain42 at pidgin.im
Thu May 7 01:25:39 EDT 2009


-----------------------------------------------------------------
Revision: d4b4f4860f4f855aaea7809eaf25b9652f74cf56
Ancestor: 20c5e507ef0c05c3b6e0d4bbb890bde450586854
Author: darkrain42 at pidgin.im
Date: 2009-05-07T04:37:30
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/d4b4f4860f4f855aaea7809eaf25b9652f74cf56

Modified files:
        ChangeLog libpurple/protocols/jabber/message.c

ChangeLog: 

Only include XHTML payload in XMPP message when it's needed. Closes #8738.

-------------- next part --------------
============================================================
--- ChangeLog	26dc48ec9f9d328fe45563e96027db3284f50f76
+++ ChangeLog	eca31d69d145410484de05c54bffedb800b8634e
@@ -34,6 +34,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.
+	* XHTML markup is only included in outgoing messages when the message
+	  contains formatting.
 
 	Yahoo:
 	* P2P file transfers. (Sulabh Mahajan)
============================================================
--- libpurple/protocols/jabber/message.c	87433ed1ad50340793e7912223ceea700520140f
+++ libpurple/protocols/jabber/message.c	e854c47f850b8d4220b791dc5da2f826441201c1
@@ -940,6 +940,58 @@ jabber_conv_support_custom_smileys(const
 	}
 }
 
+static char *
+jabber_message_smileyfy_xhtml(JabberMessage *jm, const char *xhtml)
+{
+	PurpleAccount *account = purple_connection_get_account(jm->js->gc);
+	PurpleConversation *conv =
+		purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY, jm->to,
+			account);
+
+	if (jabber_conv_support_custom_smileys(jm->js->gc, conv, jm->to)) {
+		GList *found_smileys = jabber_message_xhtml_find_smileys(xhtml);
+
+		if (found_smileys) {
+			gchar *smileyfied_xhtml = NULL;
+			const GList *iterator;
+
+			for (iterator = found_smileys; iterator ;
+				iterator = g_list_next(iterator)) {
+				const PurpleSmiley *smiley =
+						(PurpleSmiley *) iterator->data;
+				const gchar *shortcut = purple_smiley_get_shortcut(smiley);
+				const JabberData *data =
+					jabber_data_find_local_by_alt(shortcut);
+
+				/* the object has not been sent before */
+				if (!data) {
+					PurpleStoredImage *image =
+							purple_smiley_get_stored_image(smiley);
+					const gchar *ext = purple_imgstore_get_extension(image);
+					JabberStream *js = jm->js;
+
+					JabberData *new_data =
+						jabber_data_create_from_data(purple_imgstore_get_data(image),
+							purple_imgstore_get_size(image),
+							jabber_message_get_mimetype_from_ext(ext), js);
+					purple_debug_info("jabber",
+						"cache local smiley alt = %s, cid = %s\n",
+						shortcut, jabber_data_get_cid(new_data));
+					jabber_data_associate_local(new_data, shortcut);
+				}
+			}
+
+			smileyfied_xhtml =
+				jabber_message_get_smileyfied_xhtml(xhtml, found_smileys);
+			g_list_free(found_smileys);
+
+			return smileyfied_xhtml;
+		}
+	}
+
+	return NULL;
+}
+
 void jabber_message_send(JabberMessage *jm)
 {
 	xmlnode *message, *child;
@@ -1025,56 +1077,7 @@ void jabber_message_send(JabberMessage *
 	}
 
 	if(jm->xhtml) {
-		PurpleAccount *account = purple_connection_get_account(jm->js->gc);
-		PurpleConversation *conv =
-			purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY, jm->to,
-				account);
-
-		if (jabber_conv_support_custom_smileys(jm->js->gc, conv, jm->to)) {
-			GList *found_smileys = jabber_message_xhtml_find_smileys(jm->xhtml);
-
-			if (found_smileys) {
-				gchar *smileyfied_xhtml = NULL;
-				const GList *iterator;
-
-				for (iterator = found_smileys; iterator ;
-					iterator = g_list_next(iterator)) {
-					const PurpleSmiley *smiley =
-							(PurpleSmiley *) iterator->data;
-					const gchar *shortcut = purple_smiley_get_shortcut(smiley);
-					const JabberData *data =
-						jabber_data_find_local_by_alt(shortcut);
-
-					/* the object has not been sent before */
-					if (!data) {
-						PurpleStoredImage *image =
-								purple_smiley_get_stored_image(smiley);
-						const gchar *ext = purple_imgstore_get_extension(image);
-						JabberStream *js = jm->js;
-
-						JabberData *new_data =
-							jabber_data_create_from_data(purple_imgstore_get_data(image),
-								purple_imgstore_get_size(image),
-								jabber_message_get_mimetype_from_ext(ext), js);
-						purple_debug_info("jabber",
-							"cache local smiley alt = %s, cid = %s\n",
-							shortcut, jabber_data_get_cid(new_data));
-						jabber_data_associate_local(new_data, shortcut);
-					}
-				}
-
-				smileyfied_xhtml =
-					jabber_message_get_smileyfied_xhtml(jm->xhtml, found_smileys);
-				child = xmlnode_from_str(smileyfied_xhtml, -1);
-				g_free(smileyfied_xhtml);
-				g_list_free(found_smileys);
-			} else {
-				child = xmlnode_from_str(jm->xhtml, -1);
-			}
-		} else {
-			child = xmlnode_from_str(jm->xhtml, -1);
-		}
-		if(child) {
+		if ((child = xmlnode_from_str(jm->xhtml, -1))) {
 			xmlnode_insert_child(message, child);
 		} else {
 			purple_debug(PURPLE_DEBUG_ERROR, "jabber",
@@ -1088,14 +1091,51 @@ void jabber_message_send(JabberMessage *
 	xmlnode_free(message);
 }
 
+/*
+ * Compare the XHTML and plain strings passed in for "equality". Any HTML markup
+ * other than <br/> (matches a newline) in the XHTML will cause this to return
+ * FALSE.
+ */
+static gboolean
+jabber_xhtml_plain_equal(const char *xhtml_escaped,
+                         const char *plain)
+{
+	int i = 0;
+	int j = 0;
+	gboolean ret;
+	char *xhtml = purple_unescape_html(xhtml_escaped);
+
+	while (xhtml[i] && plain[j]) {
+		if (xhtml[i] == plain[j]) {
+			i += 1;
+			j += 1;
+			continue;
+		}
+
+		if (plain[j] == '\n' && !strncmp(xhtml+i, "<br/>", 5)) {
+			i += 5;
+			j += 1;
+			continue;
+		}
+
+		g_free(xhtml);
+		return FALSE;
+	}
+
+	/* Are we at the end of both strings? */
+	ret = (xhtml[i] == plain[j]) && (xhtml[i] == '\0');
+	g_free(xhtml);
+	return ret;
+}
+
 int jabber_message_send_im(PurpleConnection *gc, const char *who, const char *msg,
 		PurpleMessageFlags flags)
 {
 	JabberMessage *jm;
 	JabberBuddy *jb;
 	JabberBuddyResource *jbr;
-	char *buf;
 	char *xhtml;
+	char *tmp;
 	char *resource;
 
 	if(!who || !msg)
@@ -1130,15 +1170,18 @@ int jabber_message_send_im(PurpleConnect
 			jm->typing_style |= JM_TS_JEP_0022;
 	}
 
-	buf = g_strdup_printf("<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'>%s</body></html>", msg);
+	purple_markup_html_to_xhtml(msg, &xhtml, &jm->body);
+	tmp = jabber_message_smileyfy_xhtml(jm, xhtml);
+	if (tmp) {
+		g_free(xhtml);
+		xhtml = tmp;
+	}
 
-	purple_markup_html_to_xhtml(buf, &xhtml, &jm->body);
-	g_free(buf);
+	if ((!jbr || jbr->capabilities & JABBER_CAP_XHTML) &&
+			!jabber_xhtml_plain_equal(xhtml, jm->body))
+		jm->xhtml = g_strdup_printf("<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'>%s</body></html>", xhtml);
 
-	if(!jbr || jbr->capabilities & JABBER_CAP_XHTML)
-		jm->xhtml = xhtml;
-	else
-		g_free(xhtml);
+	g_free(xhtml);
 
 	jabber_message_send(jm);
 	jabber_message_free(jm);
@@ -1150,7 +1193,7 @@ int jabber_message_send_chat(PurpleConne
 	JabberChat *chat;
 	JabberMessage *jm;
 	JabberStream *js;
-	char *buf;
+	char *xhtml;
 
 	if(!msg || !gc)
 		return 0;
@@ -1167,15 +1210,13 @@ 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);
 
-	buf = g_strdup_printf("<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'>%s</body></html>", msg);
-	purple_markup_html_to_xhtml(buf, &jm->xhtml, &jm->body);
-	g_free(buf);
+	purple_markup_html_to_xhtml(msg, &xhtml, &jm->body);
 
-	if(!chat->xhtml) {
-		g_free(jm->xhtml);
-		jm->xhtml = NULL;
-	}
+	if (chat->xhtml && !jabber_xhtml_plain_equal(xhtml, jm->body))
+		jm->xhtml = g_strdup_printf("<html xmlns='http://jabber.org/protocol/xhtml-im'><body xmlns='http://www.w3.org/1999/xhtml'>%s</body></html>", xhtml);
 
+	g_free(xhtml);
+
 	jabber_message_send(jm);
 	jabber_message_free(jm);
 


More information about the Commits mailing list