changeset 14294:8a6154a52b84

[gaim-migrate @ 16984] get rid of some hardcoding of jabber IQ callbacks this should let plugins add their own callbacks, I think committer: Tailor Script <tailor@pidgin.im>
author Nathan Walp <nwalp@pidgin.im>
date Tue, 22 Aug 2006 18:28:00 +0000
parents 82373bb6a47a
children b76462fea543
files libgaim/protocols/jabber/disco.c libgaim/protocols/jabber/iq.c libgaim/protocols/jabber/iq.h libgaim/protocols/jabber/jabber.c libgaim/protocols/jabber/oob.c libgaim/protocols/jabber/si.c
diffstat 6 files changed, 59 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/libgaim/protocols/jabber/disco.c	Tue Aug 22 18:10:43 2006 +0000
+++ b/libgaim/protocols/jabber/disco.c	Tue Aug 22 18:28:00 2006 +0000
@@ -200,7 +200,7 @@
 	const char *from = xmlnode_get_attrib(packet, "from");
 	const char *type = xmlnode_get_attrib(packet, "type");
 
-	if(!strcmp(type, "get")) {
+	if(type && !strcmp(type, "get")) {
 		JabberIq *iq = jabber_iq_new_query(js, JABBER_IQ_RESULT,
 				"http://jabber.org/protocol/disco#items");
 
--- a/libgaim/protocols/jabber/iq.c	Tue Aug 22 18:10:43 2006 +0000
+++ b/libgaim/protocols/jabber/iq.c	Tue Aug 22 18:28:00 2006 +0000
@@ -34,6 +34,9 @@
 #include "utsname.h"
 #endif
 
+GHashTable *iq_handlers = NULL;
+
+
 JabberIq *jabber_iq_new(JabberStream *js, JabberIqType type)
 {
 	JabberIq *iq;
@@ -250,6 +253,7 @@
 	xmlnode *query, *error, *x;
 	const char *xmlns;
 	const char *type, *id, *from;
+	JabberIqHandler *jih;
 
 	query = xmlnode_get_child(packet, "query");
 	type = xmlnode_get_attrib(packet, "type");
@@ -269,53 +273,18 @@
 	/* Apparently not, so lets see if we have a pre-defined handler */
 
 	if(type && query && (xmlns = xmlnode_get_namespace(query))) {
-		if(!strcmp(type, "set")) {
-			if(!strcmp(xmlns, "jabber:iq:roster")) {
-				jabber_roster_parse(js, packet);
-				return;
-			} else if(!strcmp(xmlns, "jabber:iq:oob")) {
-				jabber_oob_parse(js, packet);
-				return;
-			} else if(!strcmp(xmlns, "http://jabber.org/protocol/bytestreams")) {
-				jabber_bytestreams_parse(js, packet);
-				return;
-			}
-		} else if(!strcmp(type, "get")) {
-			if(!strcmp(xmlns, "jabber:iq:last")) {
-				jabber_iq_last_parse(js, packet);
-				return;
-			} else if(!strcmp(xmlns, "jabber:iq:time")) {
-				jabber_iq_time_parse(js, packet);
-				return;
-			} else if(!strcmp(xmlns, "jabber:iq:version")) {
-				jabber_iq_version_parse(js, packet);
-				return;
-			} else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#info")) {
-				jabber_disco_info_parse(js, packet);
-				return;
-			} else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#items")) {
-				jabber_disco_items_parse(js, packet);
-				return;
-			}
-		} else if(!strcmp(type, "result")) {
-			if(!strcmp(xmlns, "jabber:iq:roster")) {
-				jabber_roster_parse(js, packet);
-				return;
-			} else if(!strcmp(xmlns, "jabber:iq:register")) {
-				jabber_register_parse(js, packet);
-				return;
-			} else if(!strcmp(xmlns, "http://jabber.org/protocol/disco#info")) {
-				jabber_disco_info_parse(js, packet);
-				return;
-			}
-		}
-	} else {
-		if(xmlnode_get_child_with_namespace(packet, "si", "http://jabber.org/protocol/si")) {
-			jabber_si_parse(js, packet);
+		if((jih = g_hash_table_lookup(iq_handlers, xmlns))) {
+			jih(js, packet);
 			return;
 		}
 	}
 
+
+	if(xmlnode_get_child_with_namespace(packet, "si", "http://jabber.org/protocol/si")) {
+		jabber_si_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);
@@ -335,3 +304,28 @@
 	}
 }
 
+void jabber_iq_register_handler(const char *xmlns, JabberIqHandler handlerfunc)
+{
+	g_hash_table_replace(iq_handlers, g_strdup(xmlns), handlerfunc);
+}
+
+void jabber_iq_init(void)
+{
+	iq_handlers = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
+
+	jabber_iq_register_handler("jabber:iq:roster", jabber_roster_parse);
+	jabber_iq_register_handler("jabber:iq:oob", jabber_oob_parse);
+	jabber_iq_register_handler("http://jabber.org/protocol/bytestreams", jabber_bytestreams_parse);
+	jabber_iq_register_handler("jabber:iq:last", jabber_iq_last_parse);
+	jabber_iq_register_handler("jabber:iq:time", jabber_iq_time_parse);
+	jabber_iq_register_handler("jabber:iq:version", jabber_iq_version_parse);
+	jabber_iq_register_handler("http://jabber.org/protocol/disco#info", jabber_disco_info_parse);
+	jabber_iq_register_handler("http://jabber.org/protocol/disco#items", jabber_disco_items_parse);
+	jabber_iq_register_handler("jabber:iq:register", jabber_register_parse);
+}
+
+void jabber_iq_uninit(void)
+{
+	g_hash_table_destroy(iq_handlers);
+}
+
--- a/libgaim/protocols/jabber/iq.h	Tue Aug 22 18:10:43 2006 +0000
+++ b/libgaim/protocols/jabber/iq.h	Tue Aug 22 18:28:00 2006 +0000
@@ -34,6 +34,8 @@
 	JABBER_IQ_NONE
 } JabberIqType;
 
+typedef void (JabberIqHandler)(JabberStream *js, xmlnode *packet);
+
 typedef void (JabberIqCallback)(JabberStream *js, xmlnode *packet, gpointer data);
 
 struct _JabberIq {
@@ -60,4 +62,9 @@
 void jabber_iq_send(JabberIq *iq);
 void jabber_iq_free(JabberIq *iq);
 
+void jabber_iq_init(void);
+void jabber_iq_uninit(void);
+
+void jabber_iq_register_handler(const char *xmlns, JabberIqHandler *func);
+
 #endif /* _GAIM_JABBER_IQ_H_ */
--- a/libgaim/protocols/jabber/jabber.c	Tue Aug 22 18:10:43 2006 +0000
+++ b/libgaim/protocols/jabber/jabber.c	Tue Aug 22 18:28:00 2006 +0000
@@ -705,6 +705,10 @@
 
 void jabber_register_parse(JabberStream *js, xmlnode *packet)
 {
+	const char *type;
+	if(!(type = xmlnode_get_attrib(packet, "type")) || strcmp(type, "result"))
+		return;
+
 	if(js->registration) {
 		GaimRequestFields *fields;
 		GaimRequestFieldGroup *group;
@@ -1957,6 +1961,8 @@
 	sasl_client_init(NULL);
 #endif
 	jabber_register_commands();
+
+	jabber_iq_init();
 }
 
 GAIM_INIT_PLUGIN(jabber, init_plugin, info);
--- a/libgaim/protocols/jabber/oob.c	Tue Aug 22 18:10:43 2006 +0000
+++ b/libgaim/protocols/jabber/oob.c	Tue Aug 22 18:28:00 2006 +0000
@@ -192,8 +192,12 @@
 	GaimXfer *xfer;
 	char *filename;
 	char *url;
+	const char *type;
 	xmlnode *querynode, *urlnode;
 
+	if(!(type = xmlnode_get_attrib(packet, "type")) || strcmp(type, "set"))
+		return;
+
 	if(!(querynode = xmlnode_get_child(packet, "query")))
 		return;
 
--- a/libgaim/protocols/jabber/si.c	Tue Aug 22 18:10:43 2006 +0000
+++ b/libgaim/protocols/jabber/si.c	Tue Aug 22 18:28:00 2006 +0000
@@ -191,7 +191,10 @@
 	GaimXfer *xfer;
 	JabberSIXfer *jsx;
 	xmlnode *query, *streamhost;
-	const char *sid, *from;
+	const char *sid, *from, *type;
+
+	if(!(type = xmlnode_get_attrib(packet, "type")) || strcmp(type, "set"))
+		return;
 
 	if(!(from = xmlnode_get_attrib(packet, "from")))
 		return;