changeset 30027:fce17268c9ec

jabber: Refactor BoB request code to reside inside data.c TODO: add hash validation.
author Marcus Lundblad <ml@update.uu.se>
date Sun, 21 Mar 2010 19:39:40 +0000
parents eb72ae3357dc
children 2b52480439ee 885ceb384678
files libpurple/protocols/jabber/data.c libpurple/protocols/jabber/data.h libpurple/protocols/jabber/message.c
diffstat 3 files changed, 85 insertions(+), 59 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/protocols/jabber/data.c	Sat Mar 20 16:20:35 2010 +0000
+++ b/libpurple/protocols/jabber/data.c	Sun Mar 21 19:39:40 2010 +0000
@@ -165,7 +165,7 @@
 	return img;
 }
 
-xmlnode *
+static xmlnode *
 jabber_data_get_xml_request(const gchar *cid)
 {
 	xmlnode *tag = xmlnode_new("data");
@@ -176,6 +176,70 @@
 	return tag;
 }
 
+
+typedef struct {
+	gpointer userdata;
+	gchar *alt;
+	gboolean ephemeral;
+	JabberDataRequestCallback *cb;
+} JabberDataRequestData;
+
+static void
+jabber_data_request_cb(JabberStream *js, const char *from,
+	JabberIqType type, const char *id, xmlnode *packet, gpointer data)
+{
+	JabberDataRequestData *request_data = (JabberDataRequestData *) data;
+	gpointer userdata = request_data->userdata;
+	gchar *alt = request_data->alt;
+	gboolean ephemeral = request_data->ephemeral;
+	JabberDataRequestCallback *cb = request_data->cb;
+	
+	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 && type == JABBER_IQ_RESULT) {
+		JabberData *data = jabber_data_create_from_xml(data_element);
+
+		if (data) {
+			if (ephemeral) {
+				jabber_data_associate_remote(data);
+			}
+			/* TODO: validate hash */
+			cb(data, alt, userdata);
+		}
+	} else if (item_not_found) {
+		purple_debug_info("jabber",
+			"Responder didn't recognize requested data\n");
+		cb(NULL, alt, userdata);
+	} else {
+		purple_debug_error("jabber", "Unknown response to data request\n");
+		cb(NULL, alt, userdata);
+	}
+
+	g_free(request_data);
+}
+
+void
+jabber_data_request(JabberStream *js, const gchar *cid, const gchar *who, 
+    gchar *alt, gboolean ephemeral, JabberDataRequestCallback cb,
+    gpointer userdata)
+{
+	JabberIq *request = jabber_iq_new(js, JABBER_IQ_GET);
+	xmlnode *data_request = jabber_data_get_xml_request(cid);
+	JabberDataRequestData *data = g_new0(JabberDataRequestData, 1);
+
+	data->userdata = userdata;
+	data->alt = alt;
+	data->ephemeral = ephemeral;
+	data->cb = cb;
+	
+	xmlnode_set_attrib(request->node, "to", who);
+	jabber_iq_set_callback(request, jabber_data_request_cb, data);
+	xmlnode_insert_child(request->node, data_request);
+	jabber_iq_send(request);
+}
+
 const JabberData *
 jabber_data_find_local_by_alt(const gchar *alt)
 {
--- a/libpurple/protocols/jabber/data.h	Sat Mar 20 16:20:35 2010 +0000
+++ b/libpurple/protocols/jabber/data.h	Sun Mar 21 19:39:40 2010 +0000
@@ -28,6 +28,7 @@
 
 #define JABBER_DATA_MAX_SIZE 8192
 
+
 typedef struct {
 	char *cid;
 	char *type;
@@ -35,6 +36,10 @@
 	gpointer data;
 } JabberData;
 
+typedef void (JabberDataRequestCallback)(JabberData *data, gchar *alt,
+    gpointer userdata);
+
+
 /* creates a JabberData instance from raw data */
 JabberData *jabber_data_create_from_data(gconstpointer data, gsize size,
 										 const char *type, JabberStream *js);
@@ -55,9 +60,9 @@
 /* returns an XHTML-IM "img" tag given a data instance */
 xmlnode *jabber_data_get_xhtml_im(const JabberData *data, const gchar *alt);
 
-/* returns a data request element (to be included in an iq stanza) for requesting
-  data */
-xmlnode *jabber_data_get_xml_request(const gchar *cid);
+void jabber_data_request(JabberStream *js, const gchar *cid, const gchar *who, 
+    gchar *alt, gboolean ephemeral, JabberDataRequestCallback cb,
+    gpointer userdata);
 
 /* lookup functions */
 const JabberData *jabber_data_find_local_by_alt(const gchar *alt);
--- a/libpurple/protocols/jabber/message.c	Sat Mar 20 16:20:35 2010 +0000
+++ b/libpurple/protocols/jabber/message.c	Sun Mar 21 19:39:40 2010 +0000
@@ -479,65 +479,22 @@
 	}
 }
 
-/* used in the function below to supply a conversation and shortcut for a
- smiley */
-typedef struct {
-	PurpleConversation *conv;
-	gchar *alt;
-} JabberDataRef;
-
 static void
-jabber_message_get_data_cb(JabberStream *js, const char *from,
-                           JabberIqType type, const char *id,
-                           xmlnode *packet, gpointer data)
+jabber_message_request_data_cb(JabberData *data, gchar *alt,
+    gpointer userdata)
 {
-	JabberDataRef *ref = (JabberDataRef *) data;
-	PurpleConversation *conv = ref->conv;
-	const gchar *alt = ref->alt;
-	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 && type == JABBER_IQ_RESULT) {
-		JabberData *data = jabber_data_create_from_xml(data_element);
+	PurpleConversation *conv = (PurpleConversation *) userdata;
 
-		if (data) {
-			jabber_data_associate_remote(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 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");
+	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);
 	}
-	g_free(ref->alt);
-	g_free(ref);
-}
 
-static void
-jabber_message_send_data_request(JabberStream *js, PurpleConversation *conv,
-								 const gchar *cid, const gchar *who,
-								 const gchar *alt)
-{
-	JabberIq *request = jabber_iq_new(js, JABBER_IQ_GET);
-	JabberDataRef *ref = g_new0(JabberDataRef, 1);
-	xmlnode *data_request = jabber_data_get_xml_request(cid);
-
-	xmlnode_set_attrib(request->node, "to", who);
-	ref->conv = conv;
-	ref->alt = g_strdup(alt);
-	jabber_iq_set_callback(request, jabber_message_get_data_cb, ref);
-	xmlnode_insert_child(request->node, data_request);
-
-	jabber_iq_send(request);
+	g_free(alt);
 }
 
-
 void jabber_message_parse(JabberStream *js, xmlnode *packet)
 {
 	JabberMessage *jm;
@@ -693,7 +650,7 @@
 				for (; conv && smiley_refs ; smiley_refs = g_list_delete_link(smiley_refs, smiley_refs)) {
 					JabberSmileyRef *ref = (JabberSmileyRef *) smiley_refs->data;
 					const gchar *cid = ref->cid;
-					const gchar *alt = ref->alt;
+					gchar *alt = g_strdup(ref->alt);
 
 					purple_debug_info("jabber",
 						"about to add custom smiley %s to the conv\n", alt);
@@ -713,8 +670,8 @@
 							/* we need to request the smiley (data) */
 							purple_debug_info("jabber",
 								"data is unknown, need to request it\n");
-							jabber_message_send_data_request(js, conv, cid, from,
-								alt);
+							jabber_data_request(js, cid, from, alt, FALSE,
+							    jabber_message_request_data_cb, conv);
 						}
 					}
 					g_free(ref->cid);