xmpp.custom_smiley: 14434101: Custom smileys for XMPP according to XEP...

sadrul at pidgin.im sadrul at pidgin.im
Sun Jun 1 17:06:38 EDT 2008


-----------------------------------------------------------------
Revision: 144341017ede7e3be12c467ddec33984a0868928
Ancestor: 45d6ac96faff4505b292cb4df4cdf8104114a04b
Author: ml at update.uu.se
Date: 2008-06-01T20:55:54
Branch: im.pidgin.xmpp.custom_smiley
URL: http://d.pidgin.im/viewmtn/revision/info/144341017ede7e3be12c467ddec33984a0868928

Modified files:
        libpurple/protocols/jabber/Makefile.am
        libpurple/protocols/jabber/Makefile.mingw
        libpurple/protocols/jabber/buddy.c
        libpurple/protocols/jabber/buddy.h
        libpurple/protocols/jabber/chat.c
        libpurple/protocols/jabber/iq.c
        libpurple/protocols/jabber/jabber.c
        libpurple/protocols/jabber/libxmpp.c
        libpurple/protocols/jabber/message.c
        libpurple/protocols/jabber/message.h

ChangeLog: 

Custom smileys for XMPP according to XEP 0231. Refs #5627.

-------------- next part --------------
============================================================
--- libpurple/protocols/jabber/Makefile.am	c3548815c48a2264e4ed44e9023d8a124c3fc7f7
+++ libpurple/protocols/jabber/Makefile.am	e813fba2b70462aa27685581de86e4c0b835a7ad
@@ -11,6 +11,8 @@ JABBERSOURCES = auth.c \
 			  buddy.h \
 			  chat.c \
 			  chat.h \
+			  data.c \
+			  data.h \
 			  disco.c \
 			  disco.h \
 			  google.c \
============================================================
--- libpurple/protocols/jabber/Makefile.mingw	f0ba792560dee01a48fc6252c5fb4b1fd8e71689
+++ libpurple/protocols/jabber/Makefile.mingw	7a0b19832a5c61bd3005ed394621fd36bdb21178
@@ -48,6 +48,7 @@ C_SRC =	\
 			buddy.c \
 			caps.c \
 			chat.c \
+			data.c \
 			disco.c \
 			google.c \
 			iq.c \
============================================================
--- libpurple/protocols/jabber/buddy.c	e0c5c843c37ca7e37813e88635b0d909dc78c60c
+++ libpurple/protocols/jabber/buddy.c	21f6f7d03b8e3985cbce08ebf7ce4e9e22b2cf3f
@@ -2500,5 +2500,38 @@ void jabber_user_search_begin(PurplePlug
 			js);
 }
 
+gboolean
+jabber_resource_has_capability(const JabberBuddyResource *jbr, const gchar *cap)
+{
+	const GList *iter = NULL;
 
+	if (!jbr->caps) {
+		purple_debug_error("jabber",
+			"Unable to find caps: nothing known about buddy\n");
+		return FALSE;
+	}
 
+	for (iter = jbr->caps->features ; iter ; iter = g_list_next(iter)) {
+		purple_debug_info("jabber", "Found cap: %s\n", (char *)iter->data);
+		if (strcmp(iter->data, cap) == 0) {
+			return TRUE;
+		}
+	}
+
+	return FALSE;
+}
+
+gboolean
+jabber_buddy_has_capability(const JabberBuddy *jb, const gchar *cap)
+{
+	JabberBuddyResource *jbr = jabber_buddy_find_resource((JabberBuddy*)jb, NULL);
+
+	if (!jbr) {
+		purple_debug_error("jabber",
+			"Unable to find caps: buddy might be offline\n");
+		return FALSE;
+	}
+
+	return jabber_resource_has_capability(jbr, cap);
+}
+
============================================================
--- libpurple/protocols/jabber/buddy.h	76c21a001293a2011e6b482cc9b847e941c7fde6
+++ libpurple/protocols/jabber/buddy.h	d9ee4ab54c3b59a076673d92dc1770e82ff547ee
@@ -119,4 +119,8 @@ void jabber_vcard_fetch_mine(JabberStrea
 
 void jabber_vcard_fetch_mine(JabberStream *js);
 
+gboolean jabber_resource_has_capability(const JabberBuddyResource *jbr,
+										const gchar *cap);
+gboolean jabber_buddy_has_capability(const JabberBuddy *jb, const gchar *cap);
+
 #endif /* _PURPLE_JABBER_BUDDY_H_ */
============================================================
--- libpurple/protocols/jabber/chat.c	cac367aff66c4693fc91eccb7f95f0169a0a7a6e
+++ libpurple/protocols/jabber/chat.c	f5baeb29968954ea304230763d427d0943a89a08
@@ -31,6 +31,7 @@
 #include "message.h"
 #include "presence.h"
 #include "xdata.h"
+#include "data.h"
 
 GList *jabber_chat_info(PurpleConnection *gc)
 {
@@ -678,6 +679,9 @@ void jabber_chat_part(JabberChat *chat, 
 		xmlnode_insert_data(status, msg, -1);
 	}
 	jabber_send(chat->js, presence);
+
+	jabber_data_delete_associated_with_conv(chat->conv);
+
 	xmlnode_free(presence);
 	g_free(room_jid);
 }
============================================================
--- libpurple/protocols/jabber/iq.c	d9db8d4efa9a018e942073b30fae4846223d9cab
+++ libpurple/protocols/jabber/iq.c	d806fe4817c107edc9d45810b776e538b7a19f75
@@ -33,6 +33,7 @@
 #include "si.h"
 #include "ping.h"
 #include "adhoccommands.h"
+#include "data.h"
 
 #ifdef _WIN32
 #include "utsname.h"
@@ -355,6 +356,12 @@ void jabber_iq_parse(JabberStream *js, x
 		return;
 	}
 
+	if (xmlnode_get_child_with_namespace(packet, "data",
+		    "urn:xmpp:tmp:data-element")) {
+		jabber_data_parse(js, packet);
+		return;
+	}
+
 	/* If we get here, send the default error reply mandated by XMPP-CORE */
 	if(type && (!strcmp(type, "set") || !strcmp(type, "get"))) {
 		JabberIq *iq = jabber_iq_new(js, JABBER_IQ_ERROR);
============================================================
--- libpurple/protocols/jabber/jabber.c	2cdf7bfc2030cf7f88da320550f46a9cbf1c45bd
+++ libpurple/protocols/jabber/jabber.c	558f62c48a4fd53e6a2da5e541a55ab41eef22d6
@@ -42,6 +42,7 @@
 #include "auth.h"
 #include "buddy.h"
 #include "chat.h"
+#include "data.h"
 #include "disco.h"
 #include "google.h"
 #include "iq.h"
@@ -57,6 +58,7 @@
 #include "pep.h"
 #include "adhoccommands.h"
 
+
 #define JABBER_CONNECT_STEPS (js->gsc ? 9 : 5)
 
 static PurplePlugin *my_protocol = NULL;
@@ -610,7 +612,8 @@ jabber_login(PurpleAccount *account)
 	JabberStream *js;
 	JabberBuddy *my_jb = NULL;
 
-	gc->flags |= PURPLE_CONNECTION_HTML;
+	gc->flags |= PURPLE_CONNECTION_HTML |
+		PURPLE_CONNECTION_ALLOW_CUSTOM_SMILEY;
 	js = gc->proto_data = g_new0(JabberStream, 1);
 	js->gc = gc;
 	js->fd = -1;
@@ -1862,6 +1865,10 @@ void jabber_convo_closed(PurpleConnectio
 	JabberID *jid;
 	JabberBuddy *jb;
 	JabberBuddyResource *jbr;
+	PurpleAccount *account = purple_connection_get_account(gc);
+	PurpleConversation *conv =
+		purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY,
+			who, account);
 
 	if(!(jid = jabber_id_new(who)))
 		return;
@@ -1876,6 +1883,8 @@ void jabber_convo_closed(PurpleConnectio
 			jabber_message_conv_closed(js, who);
 	}
 
+	jabber_data_delete_associated_with_conv(conv);
+
 	jabber_id_free(jid);
 }
 
============================================================
--- libpurple/protocols/jabber/libxmpp.c	b56db1a878a32e93e80e36ccaa343dc7422f18cf
+++ libpurple/protocols/jabber/libxmpp.c	1a44963d61e80bfab97ca7095d98843de7cee619
@@ -43,6 +43,7 @@
 #include "pep.h"
 #include "usertune.h"
 #include "caps.h"
+#include "data.h"
 
 static PurplePluginProtocolInfo prpl_info =
 {
@@ -240,6 +241,13 @@ init_plugin(PurplePlugin *plugin)
 	prpl_info.protocol_options = g_list_append(prpl_info.protocol_options,
 						  option);
 
+	/* this should probably be part of global smiley theme settings later on,
+	  shared with MSN */
+	option = purple_account_option_bool_new(_("Show Custom Smileys"),
+		"custom_smileys", TRUE);
+	prpl_info.protocol_options = g_list_append(prpl_info.protocol_options,
+		option);
+
 	jabber_init_plugin(plugin);
 
 	purple_prefs_remove("/plugins/prpl/jabber");
@@ -262,11 +270,16 @@ init_plugin(PurplePlugin *plugin)
 	
 	jabber_tune_init();
 	jabber_caps_init();
+	jabber_data_init();
 
 	jabber_add_feature("avatarmeta", AVATARNAMESPACEMETA, jabber_pep_namespace_only_when_pep_enabled_cb);
 	jabber_add_feature("avatardata", AVATARNAMESPACEDATA, jabber_pep_namespace_only_when_pep_enabled_cb);
-	jabber_add_feature("buzz", "http://www.xmpp.org/extensions/xep-0224.html#ns", jabber_buzz_isenabled);
-	
+	jabber_add_feature("buzz", "http://www.xmpp.org/extensions/xep-0224.html#ns",
+					   jabber_buzz_isenabled);
+	/* this string will need to be updated when XEP-0231 turns "draft" */
+	jabber_add_feature("smileys", XEP_0231_IB_IMAGE_NAMESPACE,
+					   jabber_custom_smileys_isenabled);
+
 	jabber_pep_register_handler("avatar", AVATARNAMESPACEMETA, jabber_buddy_avatar_update_metadata);
 }
 
============================================================
--- libpurple/protocols/jabber/message.c	1cf8a8c3a5b3799261f1efa1d3888e3732063fb0
+++ libpurple/protocols/jabber/message.c	0299d0c533f2d900d347e18da3edae27fce9c0e5
@@ -27,11 +27,16 @@
 
 #include "buddy.h"
 #include "chat.h"
+#include "data.h"
 #include "google.h"
 #include "message.h"
 #include "xmlnode.h"
 #include "pep.h"
+#include "smiley.h"
+#include "iq.h"
 
+#include <string.h>
+
 void jabber_message_free(JabberMessage *jm)
 {
 	g_free(jm->from);
@@ -312,6 +317,186 @@ static void handle_buzz(JabberMessage *j
 	g_free(str);
 }
 
+/* used internally by the functions below */
+typedef struct {
+	gchar *cid;
+	gchar *alt;
+} JabberSmileyRef;
+
+static GList *
+_jabber_message_get_refs_from_xmlnode(const xmlnode *message)
+{
+	xmlnode *child;
+	GList *refs = NULL;
+
+	for (child = xmlnode_get_child(message, "img") ; child ;
+		 child = xmlnode_get_next_twin(child)) {
+		const gchar *src = xmlnode_get_attrib(child, "src");
+		const gssize len = strlen(src);
+
+		if (len > 4 && g_str_has_prefix(src, "cid:")) {
+			JabberSmileyRef *ref = g_new0(JabberSmileyRef, 1);
+			ref->cid = g_strdup(&(src[4]));
+			ref->alt = g_strdup(xmlnode_get_attrib(child, "alt"));
+			refs = g_list_append(refs, ref);
+		}
+	}
+
+	for (child = message->child ; child ; child = child->next) {
+		refs = g_list_concat(refs,
+			_jabber_message_get_refs_from_xmlnode(child));
+	}
+
+	return refs;
+}
+
+static GList *
+jabber_message_get_refs_from_xmlnode(const xmlnode *message)
+{
+	GList *refs = _jabber_message_get_refs_from_xmlnode(message);
+	GHashTable *unique_refs = g_hash_table_new(g_str_hash, g_str_equal);
+	GList *result = NULL;
+	GList *iterator = NULL;
+
+	for (iterator = refs ; iterator ; iterator = g_list_next(iterator)) {
+		JabberSmileyRef *ref = (JabberSmileyRef *) iterator->data;
+		if (!g_hash_table_lookup(unique_refs, ref->cid)) {
+			JabberSmileyRef *new_ref = g_new0(JabberSmileyRef, 1);
+			new_ref->cid = g_strdup(ref->cid);
+			new_ref->alt = g_strdup(ref->alt);
+			g_hash_table_insert(unique_refs, ref->cid, ref);
+			result = g_list_append(result, new_ref);
+		}
+	}
+
+	for (iterator = refs ; iterator ; iterator = g_list_next(iterator)) {
+		JabberSmileyRef *ref = (JabberSmileyRef *) iterator->data;
+		g_free(ref->cid);
+		g_free(ref->alt);
+		g_free(ref);
+	}
+
+	g_hash_table_destroy(unique_refs);
+
+	return result;
+}
+
+
+
+static gchar *
+jabber_message_xml_to_string_strip_img_smileys(xmlnode *xhtml)
+{
+	const gchar *markup = xmlnode_to_str(xhtml, NULL);
+	int len = strlen(markup);
+	int pos = 0;
+	GString *out = g_string_new(NULL);
+
+	while (pos < len) {
+		/* this is a bit cludgy, maybe there is a better way to do this...
+		  we need to find all <img> tags within the XHTML and replace those
+			tags with the value of their "alt" attributes */
+		if (g_str_has_prefix(&(markup[pos]), "<img")) {
+			xmlnode *img = NULL;
+			int pos2 = pos;
+			const gchar *src;
+
+			for (; pos2 < len ; pos2++) {
+				if (g_str_has_prefix(&(markup[pos2]), "/>")) {
+					pos2 += 2;
+					break;
+				}
+			}
+
+			/* note, if the above loop didn't find the end of the <img> tag,
+			  it the parsed string will be until the end of the input string,
+			  in which case xmlnode_from_str will bail out and return NULL,
+			  in this case the "if" statement below doesn't trigger and the
+			  text is copied unchanged */
+			img = xmlnode_from_str(&(markup[pos]), pos2 - pos);
+			src = xmlnode_get_attrib(img, "src");
+
+			if (g_str_has_prefix(src, "cid:")) {
+				const gchar *alt = xmlnode_get_attrib(img, "alt");
+				gchar *escaped = g_markup_escape_text(alt, -1);
+				out = g_string_append(out, escaped);
+				pos += pos2 - pos;
+				g_free(escaped);
+			} else {
+				out = g_string_append_c(out, markup[pos]);
+				pos++;
+			}
+
+			xmlnode_free(img);
+
+		} else {
+			out = g_string_append_c(out, markup[pos]);
+			pos++;
+		}
+	}
+
+	return g_string_free(out, FALSE);
+}
+
+static void
+jabber_message_add_remote_smileys_to_conv(PurpleConversation *conv,
+	const xmlnode *message)
+{
+	xmlnode *data_tag;
+	for (data_tag = xmlnode_get_child(message, "data") ; data_tag ;
+		 data_tag = xmlnode_get_next_twin(data_tag)) {
+		const gchar *cid = xmlnode_get_attrib(data_tag, "cid");
+		const JabberData *data = jabber_data_find_remote_by_cid(conv, cid);
+
+		if (!data) {
+			/* we haven't cached this already, let's add it */
+			JabberData *new_data = jabber_data_create_from_xml(data_tag);
+			jabber_data_associate_remote_with_conv(new_data, conv);
+		}
+	}
+}
+
+static void
+jabber_message_get_data_cb(JabberStream *js, xmlnode *packet, gpointer data)
+{
+	PurpleConversation *conv = (PurpleConversation *) data;
+	xmlnode *data_element = xmlnode_get_child(packet, "data");
+	xmlnode *item_not_found = xmlnode_get_child(packet, "item-not-found");
+
+	/* did we get a data element as result? */
+	if (data_element) {
+		JabberData *data = jabber_data_create_from_xml(data_element);
+
+		if (data) {
+			jabber_data_associate_remote_with_conv(data, conv);
+			purple_conv_custom_smiley_write(conv, jabber_data_get_alt(data),
+											jabber_data_get_data(data),
+											jabber_data_get_size(data));
+			purple_conv_custom_smiley_close(conv, jabber_data_get_alt(data));
+		}
+
+	} else if (item_not_found) {
+		purple_debug_info("jabber",
+			"Responder didn't recognize requested data\n");
+	} else {
+		purple_debug_error("jabber", "Unknown response to data request\n");
+	}
+}
+
+static void
+jabber_message_send_data_request(JabberStream *js, PurpleConversation *conv,
+								 const gchar *cid, const gchar *who)
+{
+	JabberIq *request = jabber_iq_new(js, JABBER_IQ_GET);
+	xmlnode *data_request = jabber_data_get_xml_request(cid);
+
+	xmlnode_set_attrib(request->node, "to", who);
+	jabber_iq_set_callback(request, jabber_message_get_data_cb, conv);
+	xmlnode_insert_child(request->node, data_request);
+
+	jabber_iq_send(request);
+}
+
+
 void jabber_message_parse(JabberStream *js, xmlnode *packet)
 {
 	JabberMessage *jm;
@@ -368,14 +553,99 @@ void jabber_message_parse(JabberStream *
 		} else if(!strcmp(child->name, "html") && !strcmp(xmlns,"http://jabber.org/protocol/xhtml-im")) {
 			if(!jm->xhtml && xmlnode_get_child(child, "body")) {
 				char *c;
-				jm->xhtml = xmlnode_to_str(child, NULL);
-			        /* Convert all newlines to whitespace. Technically, even regular, non-XML HTML is supposed to ignore newlines, but Pidgin has, as convention
-			 	 * treated \n as a newline for compatibility with other protocols
+
+				const PurpleConnection *gc = js->gc;
+				const gchar *who = xmlnode_get_attrib(packet, "from");
+				PurpleAccount *account = purple_connection_get_account(gc);
+				PurpleConversation *conv = NULL;
+				const GList *smiley_refs = NULL;
+				gchar *reformatted_xhtml;
+
+				if (purple_account_get_bool(account, "custom_smileys", TRUE)) {
+					/* find a list of smileys ("cid" and "alt" text pairs)
+					  occuring in the message */
+					smiley_refs = jabber_message_get_refs_from_xmlnode(child);
+
+					if (jm->type == JABBER_MESSAGE_GROUPCHAT) {
+						JabberID *jid = jabber_id_new(jm->from);
+						JabberChat *chat = NULL;
+
+						if (jid) {
+							chat = jabber_chat_find(js, jid->node, jid->domain);
+							conv = chat->conv;
+						}
+
+						jabber_id_free(jid);
+					} else {
+						conv =
+							purple_find_conversation_with_account(PURPLE_CONV_TYPE_ANY,
+								who, account);
+					}
+
+					/* if the conversation doesn't exist yet we need to create it
+				  	now */
+					if (!conv) {
+						/* if a message of this type is initiating a conversation,
+					  	that must be an IM */
+						conv = purple_conversation_new(PURPLE_CONV_TYPE_IM,
+							account, who);
+					}
+
+					/* process any newly provided smileys */
+					jabber_message_add_remote_smileys_to_conv(conv, packet);
+				}
+
+				/* reformat xhtml so that img tags with a "cid:" src gets
+				  translated to the bare text of the emoticon (the "alt" attrib) */
+				/* this is done also when custom smiley retrieval is turned off,
+				  this way the receiver always sees the shortcut instead */
+				reformatted_xhtml =
+					jabber_message_xml_to_string_strip_img_smileys(child);
+
+				jm->xhtml = reformatted_xhtml;
+
+				/* add known custom emoticons to the conversation */
+				/* note: if there were no smileys in the incoming message, or
+				  	if receiving custom smileys is turned off, smiley_refs will
+					be NULL */
+				for (; smiley_refs ; smiley_refs = g_list_next(smiley_refs)) {
+					const JabberSmileyRef *ref =
+						(JabberSmileyRef *) smiley_refs->data;
+					const gchar *cid = ref->cid;
+					const gchar *alt = ref->alt;
+
+					if (purple_conv_custom_smiley_add(conv, alt, "cid", cid,
+						    TRUE)) {
+						const JabberData *data =
+								jabber_data_find_remote_by_cid(conv, cid);
+						/* if data is already known, we add write it immediatly */
+						if (data) {
+							purple_conv_custom_smiley_write(conv, alt,
+								jabber_data_get_data(data),
+								jabber_data_get_size(data));
+							purple_conv_custom_smiley_close(conv, alt);
+						} else {
+							/* we need to request the smiley (data) */
+							jabber_message_send_data_request(js, conv, cid, who);
+						}
+					}
+				}
+
+			    /* Convert all newlines to whitespace. Technically, even regular, non-XML HTML is supposed to ignore newlines, but Pidgin has, as convention
+				 * treated \n as a newline for compatibility with other protocols
 				 */
 				for (c = jm->xhtml; *c != '\0'; c++) {
-					if (*c == '\n') 
+					if (*c == '\n')
 						*c = ' ';
 				}
+
+				/* we don't need the list of CIDs anymore */
+				for (; smiley_refs ; smiley_refs = g_list_next(smiley_refs)) {
+					JabberSmileyRef *ref = (JabberSmileyRef *) smiley_refs->data;
+					g_free(ref->cid);
+					g_free(ref->alt);
+					g_free(ref);
+				}
 			}
 		} else if(!strcmp(child->name, "active") && !strcmp(xmlns,"http://jabber.org/protocol/chatstates")) {
 			jm->chat_state = JM_STATE_ACTIVE;
@@ -503,6 +773,120 @@ void jabber_message_parse(JabberStream *
 	jabber_message_free(jm);
 }
 
+static const gchar *
+jabber_message_get_mimetype_from_ext(const gchar *ext)
+{
+	if (strcmp(ext, "png") == 0) {
+		return "image/png";
+	} else if (strcmp(ext, "gif") == 0) {
+		return "image/gif";
+	} else if (strcmp(ext, "jpg") == 0) {
+		return "image/jpeg";
+	} else if (strcmp(ext, "tif") == 0) {
+		return "image/tif";
+	} else {
+		return "image/x-icon"; /* or something... */
+	}
+}
+
+static GList *
+jabber_message_xhtml_find_smileys(const char *xhtml)
+{
+	GList *smileys = purple_smileys_get_all();
+	GList *found_smileys = NULL;
+
+	for (; smileys ; smileys = g_list_next(smileys)) {
+		PurpleSmiley *smiley = (PurpleSmiley *) smileys->data;
+		const gchar *shortcut = purple_smiley_get_shortcut(smiley);
+		const gssize len = strlen(shortcut);
+
+		gchar *escaped = g_markup_escape_text(shortcut, len);
+		const char *pos = strstr(xhtml, escaped);
+
+		if (pos) {
+			found_smileys = g_list_append(found_smileys, smiley);
+		}
+
+		g_free(escaped);
+	}
+
+	return found_smileys;
+}
+
+static gchar *
+jabber_message_get_smileyfied_xhtml(const PurpleConversation *conv,
+									const gchar *xhtml, const GList *smileys)
+{
+	/* create XML element for all smileys (img tags) */
+	GString *result = g_string_new(NULL);
+	int pos = 0;
+	int length = strlen(xhtml);
+
+	while (pos < length) {
+		const GList *iterator;
+		gboolean found_smiley = FALSE;
+
+		for (iterator = smileys ; iterator ;
+			iterator = g_list_next(iterator)) {
+			const PurpleSmiley *smiley = (PurpleSmiley *) iterator->data;
+			const gchar *shortcut = purple_smiley_get_shortcut(smiley);
+			const gssize len = strlen(shortcut);
+			gchar *escaped = g_markup_escape_text(shortcut, len);
+
+			if (g_str_has_prefix(&(xhtml[pos]), escaped)) {
+				/* we found the current smiley at this position */
+				const JabberData *data =
+					jabber_data_find_local_by_alt(conv, shortcut);
+				xmlnode *img = jabber_data_get_xhtml_im(data);
+				int len;
+				gchar *img_text = xmlnode_to_str(img, &len);
+
+				found_smiley = TRUE;
+				result = g_string_append(result, img_text);
+				g_free(img_text);
+				pos += strlen(escaped);
+				g_free(escaped);
+				break;
+			} else {
+				/* cleanup from the before the next round... */
+				g_free(escaped);
+			}
+		}
+		if (!found_smiley) {
+			/* there was no smiley here, just copy one byte */
+			result = g_string_append_c(result, xhtml[pos]);
+			pos++;
+		}
+	}
+
+	return g_string_free(result, FALSE);
+}
+
+static gboolean
+jabber_conv_support_custom_smileys(const PurpleConnection *gc,
+								   const PurpleConversation *conv,
+								   const gchar *who)
+{
+	JabberStream *js = (JabberStream *) gc->proto_data;
+	JabberBuddy *jb = jabber_buddy_find(js, who, FALSE);
+
+	if (!jb) {
+		purple_debug_error("jabber",
+			"jabber_conv_support_custom smileys: could not find buddy\n");
+		return FALSE;
+	}
+
+	switch (purple_conversation_get_type(conv)) {
+		/* for the time being, we will not support custom smileys in MUCs */
+		case PURPLE_CONV_TYPE_IM:
+			return jabber_buddy_has_capability(jb, XEP_0231_IB_IMAGE_NAMESPACE);
+			break;
+		default:
+			return FALSE;
+			break;
+	}
+}
+
 void jabber_message_send(JabberMessage *jm)
 {
 	xmlnode *message, *child;
@@ -588,7 +972,55 @@ void jabber_message_send(JabberMessage *
 	}
 
 	if(jm->xhtml) {
-		child = xmlnode_from_str(jm->xhtml, -1);
+		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)) {
+			const 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(conv, shortcut);
+					
+					/* if data has not been sent before, include data */
+					if (!data) {
+						PurpleStoredImage *image = 
+								purple_smiley_get_stored_image(smiley);
+						const gchar *ext = purple_imgstore_get_extension(image);
+					
+						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),
+														shortcut);
+						jabber_data_associate_local_with_conv(new_data, conv);
+						xmlnode_insert_child(message, 
+											jabber_data_get_xml_definition(new_data));
+					}
+				}			
+				
+				smileyfied_xhtml = 
+					jabber_message_get_smileyfied_xhtml(conv, jm->xhtml, 
+														found_smileys);
+				child = xmlnode_from_str(smileyfied_xhtml, -1);
+				g_free(smileyfied_xhtml);
+			} else {
+				child = xmlnode_from_str(jm->xhtml, -1);
+			}
+		} else {
+			child = xmlnode_from_str(jm->xhtml, -1);
+		}
 		if(child) {
 			xmlnode_insert_child(message, child);
 		} else {
@@ -762,3 +1194,11 @@ gboolean jabber_buzz_isenabled(JabberStr
 	return js->allowBuzz;
 }
 
+gboolean jabber_custom_smileys_isenabled(JabberStream *js, const gchar *shortname,
+										 const gchar *namespace)
+{
+	const PurpleConnection *gc = js->gc;
+	PurpleAccount *account = purple_connection_get_account(gc);
+	
+	return purple_account_get_bool(account, "custom_smileys", TRUE);
+}
============================================================
--- libpurple/protocols/jabber/message.h	8de79d751c731eb68f6282872ae73f790de60789
+++ libpurple/protocols/jabber/message.h	7f0a4f0c9f2914ff427924356b8303ac82256c5e
@@ -80,4 +80,7 @@ gboolean jabber_buzz_isenabled(JabberStr
 
 gboolean jabber_buzz_isenabled(JabberStream *js, const gchar *shortname, const gchar *namespace);
 
+gboolean jabber_custom_smileys_isenabled(JabberStream *js, const gchar *shortname,
+										 const gchar *namespace);
+
 #endif /* _PURPLE_JABBER_MESSAGE_H_ */


More information about the Commits mailing list