pidgin: f23de15c: jabber: Refactor BoB request code to res...

malu at pidgin.im malu at pidgin.im
Sun Mar 21 15:45:49 EDT 2010


-----------------------------------------------------------------
Revision: f23de15cf5cab74ad9b1c2ce72676be0e80e7f23
Ancestor: fbc5141857873eae0512b7cecad373ea0612207e
Author: malu at pidgin.im
Date: 2010-03-21T19:39:40
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/f23de15cf5cab74ad9b1c2ce72676be0e80e7f23

Modified files:
        libpurple/protocols/jabber/data.c
        libpurple/protocols/jabber/data.h
        libpurple/protocols/jabber/message.c

ChangeLog: 

jabber: Refactor BoB request code to reside inside data.c
TODO: add hash validation.

-------------- next part --------------
============================================================
--- libpurple/protocols/jabber/data.c	27f1a28081ef31291d4b0dabfbe9fe1148d502c7
+++ libpurple/protocols/jabber/data.c	bc27279b80a8328f0a50c322cc082a4432aa3f22
@@ -165,7 +165,7 @@ jabber_data_get_xhtml_im(const JabberDat
 	return img;
 }
 
-xmlnode *
+static xmlnode *
 jabber_data_get_xml_request(const gchar *cid)
 {
 	xmlnode *tag = xmlnode_new("data");
@@ -176,6 +176,70 @@ jabber_data_get_xml_request(const gchar 
 	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)
 {
============================================================
--- libpurple/protocols/jabber/data.h	cb00d566b725ad66aa578f242509865f2ea01f8f
+++ libpurple/protocols/jabber/data.h	2eac9027496d99898c4382fa297a495c2e079791
@@ -28,6 +28,7 @@
 
 #define JABBER_DATA_MAX_SIZE 8192
 
+
 typedef struct {
 	char *cid;
 	char *type;
@@ -35,6 +36,10 @@ typedef struct {
 	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 @@ xmlnode *jabber_data_get_xhtml_im(const 
 /* 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);
============================================================
--- libpurple/protocols/jabber/message.c	cd808324542a2c49a23f30d0b8b743e2125a6a71
+++ libpurple/protocols/jabber/message.c	a18b7db3bb903a34a373dd1f05610992e4d9878a
@@ -479,65 +479,22 @@ jabber_message_add_remote_smileys(const 
 	}
 }
 
-/* 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");
+	PurpleConversation *conv = (PurpleConversation *) userdata;
 
-	/* 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) {
-			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 @@ void jabber_message_parse(JabberStream *
 				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 @@ void jabber_message_parse(JabberStream *
 							/* 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);


More information about the Commits mailing list