changeset 26459:81b30f96250e

propagate from branch 'im.pidgin.pidgin' (head 2c36a90a26f2451978aa41b23e9980337514057d) to branch 'im.pidgin.cpw.darkrain42.xmpp.bosh' (head 3962b427a087df544a52cae8dec0fc6d1d2e1635)
author Paul Aurich <paul@darkrain42.org>
date Sat, 04 Apr 2009 20:25:03 +0000
parents 9eb8c8ec0a75 (current diff) f25410b0827c (diff)
children 680b54e417c1
files libpurple/media.h libpurple/protocols/jabber/jabber.c
diffstat 28 files changed, 2577 insertions(+), 783 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/dnssrv.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/dnssrv.c	Sat Apr 04 20:25:03 2009 +0000
@@ -33,12 +33,18 @@
 #ifndef T_SRV
 #define T_SRV	33
 #endif
-#else
+#ifndef T_TXT
+#define T_TXT	16
+#endif
+#else /* WIN32 */
 #include <windns.h>
 /* Missing from the mingw headers */
 #ifndef DNS_TYPE_SRV
 # define DNS_TYPE_SRV 33
 #endif
+#ifndef DNS_TYPE_TXT
+# define DNS_TYPE_TXT 16
+#endif
 #endif
 
 #include "dnssrv.h"
@@ -60,9 +66,14 @@
 #endif
 
 struct _PurpleSrvQueryData {
-	PurpleSrvCallback cb;
+	union {
+		PurpleSrvCallback srv;
+		PurpleTxtCallback txt;
+	} cb;
+
 	gpointer extradata;
 	guint handle;
+	int type;
 #ifdef _WIN32
 	GThread *resolver;
 	char *query;
@@ -74,6 +85,11 @@
 #endif
 };
 
+typedef struct _PurpleSrvInternalQuery {
+	int type;
+	char query[256];
+} PurpleSrvInternalQuery;
+
 static gint
 responsecompare(gconstpointer ar, gconstpointer br)
 {
@@ -99,6 +115,7 @@
 {
 	GList *ret = NULL;
 	PurpleSrvResponse *srvres;
+	PurpleTxtResponse *txtres;
 	queryans answer;
 	int size;
 	int qdcount;
@@ -107,23 +124,22 @@
 	guchar *cp;
 	gchar name[256];
 	guint16 type, dlen, pref, weight, port;
-	gchar query[256];
+	PurpleSrvInternalQuery query;
 
 #ifdef HAVE_SIGNAL_H
 	purple_restore_default_signal_handlers();
 #endif
 
-	if (read(in, query, 256) <= 0) {
+	if (read(in, &query, sizeof(query)) <= 0) {
 		close(out);
 		close(in);
 		_exit(0);
 	}
 
-	size = res_query( query, C_IN, T_SRV, (u_char*)&answer, sizeof( answer));
-
+	size = res_query( query.query, C_IN, query.type, (u_char*)&answer, sizeof( answer));
+	
 	qdcount = ntohs(answer.hdr.qdcount);
 	ancount = ntohs(answer.hdr.ancount);
-
 	cp = (guchar*)&answer + sizeof(HEADER);
 	end = (guchar*)&answer + size;
 
@@ -138,17 +154,14 @@
 		size = dn_expand((unsigned char*)&answer, end, cp, name, 256);
 		if(size < 0)
 			goto end;
-
 		cp += size;
-
 		GETSHORT(type,cp);
 
 		/* skip ttl and class since we already know it */
 		cp += 6;
 
 		GETSHORT(dlen,cp);
-
-		if (type == T_SRV) {
+		if (query.type == T_SRV) {
 			GETSHORT(pref,cp);
 
 			GETSHORT(weight,cp);
@@ -168,6 +181,11 @@
 			srvres->weight = weight;
 
 			ret = g_list_insert_sorted(ret, srvres, responsecompare);
+		} else if (query.type == T_TXT) {
+			txtres = g_new0(PurpleTxtResponse, 1);
+			strncpy(txtres->content, (gchar*)(++cp), dlen-1);
+			ret = g_list_append(ret, txtres);
+			cp += dlen - 1;
 		} else {
 			cp += dlen;
 		}
@@ -176,11 +194,13 @@
 end:
 	size = g_list_length(ret);
 	/* TODO: Check return value */
-	write(out, &size, sizeof(int));
+	write(out, &(query.type), sizeof(query.type));
+	write(out, &size, sizeof(size));
 	while (ret != NULL)
 	{
 		/* TODO: Check return value */
-		write(out, ret->data, sizeof(PurpleSrvResponse));
+		if (query.type == T_SRV) write(out, ret->data, sizeof(PurpleSrvResponse));
+		if (query.type == T_TXT) write(out, ret->data, sizeof(PurpleTxtResponse));
 		g_free(ret->data);
 		ret = g_list_remove(ret, ret->data);
 	}
@@ -195,39 +215,66 @@
 resolved(gpointer data, gint source, PurpleInputCondition cond)
 {
 	int size;
+	int type;
 	PurpleSrvQueryData *query_data = (PurpleSrvQueryData*)data;
-	PurpleSrvResponse *res;
-	PurpleSrvResponse *tmp;
 	int i;
-	PurpleSrvCallback cb = query_data->cb;
 	int status;
-
-	if (read(source, &size, sizeof(int)) == sizeof(int))
-	{
-		ssize_t red;
-		purple_debug_info("dnssrv","found %d SRV entries\n", size);
-		tmp = res = g_new0(PurpleSrvResponse, size);
-		for (i = 0; i < size; i++) {
-			red = read(source, tmp++, sizeof(PurpleSrvResponse));
-			if (red != sizeof(PurpleSrvResponse)) {
-				purple_debug_error("dnssrv","unable to read srv "
-						"response: %s\n", g_strerror(errno));
+	
+	if (read(source, &type, sizeof(type)) == sizeof(type)) {
+		if (type == T_SRV) {
+			PurpleSrvResponse *res;
+			PurpleSrvResponse *tmp;
+			PurpleSrvCallback cb = query_data->cb.srv;
+			if (read(source, &size, sizeof(int)) == sizeof(int)) {
+				ssize_t red;
+				purple_debug_info("dnssrv","found %d SRV entries\n", size);
+				tmp = res = g_new0(PurpleSrvResponse, size);
+				for (i = 0; i < size; i++) {
+					red = read(source, tmp++, sizeof(PurpleSrvResponse));
+					if (red != sizeof(PurpleSrvResponse)) {
+						purple_debug_error("dnssrv","unable to read srv "
+								"response: %s\n", g_strerror(errno));
+						size = 0;
+						g_free(res);
+						res = NULL;
+					}
+				}
+			} else {
+				purple_debug_info("dnssrv","found 0 SRV entries; errno is %i\n", errno);
 				size = 0;
-				g_free(res);
 				res = NULL;
 			}
+			cb(res, size, query_data->extradata);
+		} else if (type == T_TXT) {
+			PurpleTxtResponse *res;
+			PurpleTxtResponse *tmp;
+			PurpleTxtCallback cb = query_data->cb.txt;
+			if (read(source, &size, sizeof(int)) == sizeof(int)) {
+				ssize_t red;
+				purple_debug_info("dnssrv","found %d TXT entries\n", size);
+				tmp = res = g_new0(PurpleTxtResponse, size);
+				for (i = 0; i < size; i++) {
+					red = read(source, tmp++, sizeof(PurpleTxtResponse));
+					if (red != sizeof(PurpleTxtResponse)) {
+						purple_debug_error("dnssrv","unable to read txt "
+								"response: %s\n", g_strerror(errno));
+						size = 0;
+						g_free(res);
+						res = NULL;
+					}
+				}
+			} else {
+				purple_debug_info("dnssrv","found 0 TXT entries; errno is %i\n", errno);
+				size = 0;
+				res = NULL;
+			}
+			cb(res, size, query_data->extradata);			
+		} else {
+			purple_debug_info("dnssrv","type unknown of DNS result entry; errno is %i\n", errno);
 		}
 	}
-	else
-	{
-		purple_debug_info("dnssrv","found 0 SRV entries; errno is %i\n", errno);
-		size = 0;
-		res = NULL;
-	}
 
-	cb(res, size, query_data->extradata);
 	waitpid(query_data->pid, &status, 0);
-
 	purple_srv_cancel(query_data);
 }
 
@@ -239,34 +286,57 @@
 res_main_thread_cb(gpointer data)
 {
 	PurpleSrvResponse *srvres = NULL;
+	PurpleTxtResponse *txtres = NULL;
 	int size = 0;
 	PurpleSrvQueryData *query_data = data;
-
 	if(query_data->error_message != NULL)
 		purple_debug_error("dnssrv", query_data->error_message);
 	else {
-		PurpleSrvResponse *srvres_tmp = NULL;
-		GSList *lst = query_data->results;
+		if (query_data->type == T_SRV) {
+			PurpleSrvResponse *srvres_tmp = NULL;
+			GSList *lst = query_data->results;
+
+			size = g_slist_length(lst);
 
-		size = g_slist_length(lst);
+			if(query_data->cb.srv && size > 0)
+				srvres_tmp = srvres = g_new0(PurpleSrvResponse, size);
+			while (lst) {
+				if(query_data->cb.srv)
+					memcpy(srvres_tmp++, lst->data, sizeof(PurpleSrvResponse));
+				g_free(lst->data);
+				lst = g_slist_remove(lst, lst->data);
+			}
+
+			query_data->results = NULL;
 
-		if(query_data->cb && size > 0)
-			srvres_tmp = srvres = g_new0(PurpleSrvResponse, size);
-		while (lst) {
-			if(query_data->cb)
-				memcpy(srvres_tmp++, lst->data, sizeof(PurpleSrvResponse));
-			g_free(lst->data);
-			lst = g_slist_remove(lst, lst->data);
+			purple_debug_info("dnssrv", "found %d SRV entries\n", size);
+			
+			if(query_data->cb.srv) query_data->cb.srv(srvres, size, query_data->extradata);
+		} else if (query_data->type == T_TXT) {
+			PurpleTxtResponse *txtres_tmp = NULL;
+			GSList *lst = query_data->results;
+
+			size = g_slist_length(lst);
+
+			if(query_data->cb.txt && size > 0)
+				txtres_tmp = txtres = g_new0(PurpleTxtResponse, size);
+			while (lst) {
+				if(query_data->cb.txt)
+					memcpy(txtres_tmp++, lst->data, sizeof(PurpleTxtResponse));
+				g_free(lst->data);
+				lst = g_slist_remove(lst, lst->data);
+			}
+
+			query_data->results = NULL;
+
+			purple_debug_info("dnssrv", "found %d TXT entries\n", size);
+			
+			if(query_data->cb.txt) query_data->cb.txt(txtres, size, query_data->extradata);
+		} else {
+			purple_debug_error("dnssrv", "unknown query type");
 		}
-
-		query_data->results = NULL;
-
-	purple_debug_info("dnssrv", "found %d SRV entries\n", size);
 	}
 
-	if(query_data->cb)
-		query_data->cb(srvres, size, query_data->extradata);
-
 	query_data->resolver = NULL;
 	query_data->handle = 0;
 
@@ -279,40 +349,50 @@
 res_thread(gpointer data)
 {
 	PDNS_RECORD dr = NULL;
-	int type = DNS_TYPE_SRV;
+	int type;
 	DNS_STATUS ds;
 	PurpleSrvQueryData *query_data = data;
-
+	type = query_data->type;
 	ds = MyDnsQuery_UTF8(query_data->query, type, DNS_QUERY_STANDARD, NULL, &dr, NULL);
 	if (ds != ERROR_SUCCESS) {
 		gchar *msg = g_win32_error_message(ds);
-		query_data->error_message = g_strdup_printf("Couldn't look up SRV record. %s (%lu).\n", msg, ds);
+		if (type == DNS_TYPE_SRV) {
+			query_data->error_message = g_strdup_printf("Couldn't look up SRV record. %s (%lu).\n", msg, ds);
+		} else if (type == DNS_TYPE_TXT) {
+			query_data->error_message = g_strdup_printf("Couldn't look up TXT record. %s (%lu).\n", msg, ds);
+		}
 		g_free(msg);
 	} else {
-		PDNS_RECORD dr_tmp;
-		GSList *lst = NULL;
-		DNS_SRV_DATA *srv_data;
-		PurpleSrvResponse *srvres;
+		if (type == DNS_TYPE_SRV) {
+			PDNS_RECORD dr_tmp;
+			GSList *lst = NULL;
+			DNS_SRV_DATA *srv_data;
+			PurpleSrvResponse *srvres;
+			
+			for (dr_tmp = dr; dr_tmp != NULL; dr_tmp = dr_tmp->pNext) {
+				/* Discard any incorrect entries. I'm not sure if this is necessary */
+				if (dr_tmp->wType != type || strcmp(dr_tmp->pName, query_data->query) != 0) {
+					continue;
+				}
 
-		for (dr_tmp = dr; dr_tmp != NULL; dr_tmp = dr_tmp->pNext) {
-			/* Discard any incorrect entries. I'm not sure if this is necessary */
-			if (dr_tmp->wType != type || strcmp(dr_tmp->pName, query_data->query) != 0) {
-				continue;
+				srv_data = &dr_tmp->Data.SRV;
+				srvres = g_new0(PurpleSrvResponse, 1);
+				strncpy(srvres->hostname, srv_data->pNameTarget, 255);
+				srvres->hostname[255] = '\0';
+				srvres->pref = srv_data->wPriority;
+				srvres->port = srv_data->wPort;
+				srvres->weight = srv_data->wWeight;
+				
+				lst = g_slist_insert_sorted(lst, srvres, responsecompare);
 			}
 
-			srv_data = &dr_tmp->Data.SRV;
-			srvres = g_new0(PurpleSrvResponse, 1);
-			strncpy(srvres->hostname, srv_data->pNameTarget, 255);
-			srvres->hostname[255] = '\0';
-			srvres->pref = srv_data->wPriority;
-			srvres->port = srv_data->wPort;
-			srvres->weight = srv_data->wWeight;
-
-			lst = g_slist_insert_sorted(lst, srvres, responsecompare);
+			MyDnsRecordListFree(dr, DnsFreeRecordList);
+			query_data->results = lst;
+		} else if (type == T_TXT) {
+			#error IMPLEMENTATION MISSING		
+		} else {
+			
 		}
-
-		MyDnsRecordListFree(dr, DnsFreeRecordList);
-		query_data->results = lst;
 	}
 
 	/* back to main thread */
@@ -330,6 +410,7 @@
 {
 	char *query;
 	PurpleSrvQueryData *query_data;
+	PurpleSrvInternalQuery internal_query;
 #ifndef _WIN32
 	int in[2], out[2];
 	int pid;
@@ -377,11 +458,16 @@
 	close(out[1]);
 	close(in[0]);
 
-	if (write(in[1], query, strlen(query)+1) < 0)
+	internal_query.type = T_SRV;
+	strncpy(internal_query.query, query, 255);
+	
+	if (write(in[1], &internal_query, sizeof(internal_query)) < 0)
 		purple_debug_error("dnssrv", "Could not write to SRV resolver\n");
+	
 
 	query_data = g_new0(PurpleSrvQueryData, 1);
-	query_data->cb = cb;
+	query_data->type = T_SRV;
+	query_data->cb.srv = cb;
 	query_data->extradata = extradata;
 	query_data->pid = pid;
 	query_data->fd_out = out[0];
@@ -400,7 +486,8 @@
 	}
 
 	query_data = g_new0(PurpleSrvQueryData, 1);
-	query_data->cb = cb;
+	query_data->type = DNS_TYPE_SRV;
+	query_data->cb.srv = cb;
 	query_data->query = query;
 	query_data->extradata = extradata;
 
@@ -424,6 +511,104 @@
 #endif
 }
 
+PurpleSrvQueryData *purple_txt_resolve(const char *owner, const char *domain, PurpleTxtCallback cb, gpointer extradata)
+{
+	char *query;
+	PurpleSrvQueryData *query_data;
+	PurpleSrvInternalQuery internal_query;
+#ifndef _WIN32
+	int in[2], out[2];
+	int pid;
+#else
+	GError* err = NULL;
+	static gboolean initialized = FALSE;
+#endif
+
+	query = g_strdup_printf("%s.%s", owner, domain);
+	purple_debug_info("dnssrv","querying TXT record for %s\n", query);
+
+#ifndef _WIN32
+	if(pipe(in) || pipe(out)) {
+		purple_debug_error("dnssrv", "Could not create pipe\n");
+		g_free(query);
+		cb(NULL, 0, extradata);
+		return NULL;
+	}
+
+	pid = fork();
+	if (pid == -1) {
+		purple_debug_error("dnssrv", "Could not create process!\n");
+		cb(NULL, 0, extradata);
+		g_free(query);
+		return NULL;
+	}
+
+	/* Child */
+	if (pid == 0)
+	{
+		g_free(query);
+
+		close(out[0]);
+		close(in[1]);
+		resolve(in[0], out[1]);
+		/* resolve() does not return */
+	}
+
+	close(out[1]);
+	close(in[0]);
+	
+	internal_query.type = T_TXT;
+	strncpy(internal_query.query, query, 255);
+	
+	if (write(in[1], &internal_query, sizeof(internal_query)) < 0)
+		purple_debug_error("dnssrv", "Could not write to TXT resolver\n");
+
+	query_data = g_new0(PurpleSrvQueryData, 1);
+	query_data->type = T_TXT;
+	query_data->cb.txt = cb;
+	query_data->extradata = extradata;
+	query_data->pid = pid;
+	query_data->fd_out = out[0];
+	query_data->fd_in = in[1];
+	query_data->handle = purple_input_add(out[0], PURPLE_INPUT_READ, resolved, query_data);
+
+	g_free(query);
+
+	return query_data;
+#else
+	if (!initialized) {
+		MyDnsQuery_UTF8 = (void*) wpurple_find_and_loadproc("dnsapi.dll", "DnsQuery_UTF8");
+		MyDnsRecordListFree = (void*) wpurple_find_and_loadproc(
+			"dnsapi.dll", "DnsRecordListFree");
+		initialized = TRUE;
+	}
+
+	query_data = g_new0(PurpleSrvQueryData, 1);
+	query_data->type = DNS_TYPE_TXT;
+	query_data->cb.txt = cb;
+	query_data->query = query;
+	query_data->extradata = extradata;
+
+	if (!MyDnsQuery_UTF8 || !MyDnsRecordListFree)
+		query_data->error_message = g_strdup("System missing DNS API (Requires W2K+)\n");
+	else {
+		query_data->resolver = g_thread_create(res_thread, query_data, FALSE, &err);
+		if (query_data->resolver == NULL) {
+			query_data->error_message = g_strdup_printf("TXT thread create failure: %s\n", (err && err->message) ? err->message : "");
+			g_error_free(err);
+		}
+	}
+
+	/* The query isn't going to happen, so finish the SRV lookup now.
+	 * Asynchronously call the callback since stuff may not expect
+	 * the callback to be called before this returns */
+	if (query_data->error_message != NULL)
+		query_data->handle = purple_timeout_add(0, res_main_thread_cb, query_data);
+
+	return query_data;
+#endif
+}
+
 void
 purple_srv_cancel(PurpleSrvQueryData *query_data)
 {
@@ -437,7 +622,7 @@
 		 * just set the callback to NULL and let the DNS lookup
 		 * finish.
 		 */
-		query_data->cb = NULL;
+		query_data->cb.srv = NULL;
 		return;
 	}
 	g_free(query_data->query);
@@ -448,3 +633,9 @@
 #endif
 	g_free(query_data);
 }
+
+void
+purple_txt_cancel(PurpleSrvQueryData *query_data)
+{
+	purple_srv_cancel(query_data);
+}
--- a/libpurple/dnssrv.h	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/dnssrv.h	Sat Apr 04 20:25:03 2009 +0000
@@ -28,8 +28,9 @@
 extern "C" {
 #endif
 
+typedef struct _PurpleSrvQueryData PurpleSrvQueryData;
 typedef struct _PurpleSrvResponse PurpleSrvResponse;
-typedef struct _PurpleSrvQueryData PurpleSrvQueryData;
+typedef struct _PurpleTxtResponse PurpleTxtResponse;
 
 struct _PurpleSrvResponse {
 	char hostname[256];
@@ -38,7 +39,12 @@
 	int pref;
 };
 
+struct _PurpleTxtResponse {
+    char content[256];
+};
+
 typedef void (*PurpleSrvCallback)(PurpleSrvResponse *resp, int results, gpointer data);
+typedef void (*PurpleTxtCallback)(PurpleTxtResponse *resp, int results, gpointer data);
 
 /**
  * Queries an SRV record.
@@ -58,6 +64,23 @@
  */
 void purple_srv_cancel(PurpleSrvQueryData *query_data);
 
+/**
+ * Queries an TXT record.
+ *
+ * @param owner Name of the protocol (e.g. "_xmppconnect")
+ * @param domain Domain name to query (e.g. "blubb.com")
+ * @param cb A callback which will be called with the results
+ * @param extradata Extra data to be passed to the callback
+ */
+PurpleSrvQueryData *purple_txt_resolve(const char *owner, const char *domain, PurpleTxtCallback cb, gpointer extradata);
+
+/**
+ * Cancel an TXT DNS query.
+ *
+ * @param query_data The request to cancel.
+ */
+void purple_txt_cancel(PurpleSrvQueryData *query_data);
+
 #ifdef __cplusplus
 }
 #endif
--- a/libpurple/media.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/media.c	Sat Apr 04 20:25:03 2009 +0000
@@ -40,6 +40,7 @@
 #ifdef USE_VV
 
 #include <gst/farsight/fs-conference-iface.h>
+#include <gst/farsight/fs-transmitter.h>
 
 /** @copydoc _PurpleMediaSession */
 typedef struct _PurpleMediaSession PurpleMediaSession;
@@ -3057,3 +3058,26 @@
 #endif
 }
 
+gboolean
+purple_media_transmitter_exists(const gchar *transmitter)
+{
+#ifdef USE_VV
+	char **transmitters;
+	int i;
+
+	g_return_val_if_fail(transmitter != NULL, FALSE);
+
+	transmitters = fs_transmitter_list_available();
+	for (i = 0; transmitters[i]; ++i) {
+		if (g_str_equal(transmitter, transmitters[i])) {
+			g_strfreev(transmitters);
+			return TRUE;
+		}
+	}
+
+	g_strfreev(transmitters);
+	return FALSE;
+#else
+	return FALSE;
+#endif
+}
--- a/libpurple/media.h	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/media.h	Sat Apr 04 20:25:03 2009 +0000
@@ -679,6 +679,15 @@
  */
 void purple_media_remove_output_windows(PurpleMedia *media);
 
+/**
+ * Returns whether or not a specific transmitter type is present.
+ *
+ * @param transmitter The name of the transmitter.
+ *
+ * @return TRUE if it is present, FALSE otherwise.
+ */
+gboolean purple_media_transmitter_exists(const gchar *transmitter);
+
 #ifdef __cplusplus
 }
 #endif
--- a/libpurple/protocols/jabber/Makefile.am	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/Makefile.am	Sat Apr 04 20:25:03 2009 +0000
@@ -9,6 +9,8 @@
 			  auth.h \
 			  buddy.c \
 			  buddy.h \
+			  bosh.c \
+			  bosh.h \
 			  chat.c \
 			  chat.h \
 			  data.c \
--- a/libpurple/protocols/jabber/Makefile.mingw	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/Makefile.mingw	Sat Apr 04 20:25:03 2009 +0000
@@ -46,6 +46,7 @@
 			adhoccommands.c \
 			auth.c \
 			buddy.c \
+			bosh.c
 			caps.c \
 			chat.c \
 			data.c \
--- a/libpurple/protocols/jabber/auth.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/auth.c	Sat Apr 04 20:25:03 2009 +0000
@@ -281,7 +281,7 @@
 	secprops.min_ssf = 0;
 	secprops.security_flags = SASL_SEC_NOANONYMOUS;
 
-	if (!js->gsc) {
+	if (!jabber_stream_is_ssl(js)) {
 		secprops.max_ssf = -1;
 		secprops.maxbufsize = 4096;
 		plaintext = purple_account_get_bool(js->gc->account, "auth_plain_in_clear", FALSE);
@@ -544,7 +544,7 @@
 	} else if(plain) {
 		js->auth_type = JABBER_AUTH_PLAIN;
 
-		if(js->gsc == NULL && !purple_account_get_bool(js->gc->account, "auth_plain_in_clear", FALSE)) {
+		if(!jabber_stream_is_ssl(js) && !purple_account_get_bool(js->gc->account, "auth_plain_in_clear", FALSE)) {
 			char *msg = g_strdup_printf(_("%s requires plaintext authentication over an unencrypted connection.  Allow this and continue authentication?"),
 					js->gc->account->username);
 			purple_request_yes_no(js->gc, _("Plaintext Authentication"),
@@ -571,7 +571,7 @@
 	const char *type = xmlnode_get_attrib(packet, "type");
 
 	if(type && !strcmp(type, "result")) {
-		jabber_stream_set_state(js, JABBER_STREAM_CONNECTED);
+		jabber_disco_items_server(js);
 	} else {
 		PurpleConnectionError reason = PURPLE_CONNECTION_ERROR_NETWORK_ERROR;
 		char *msg = jabber_parse_error(js, packet, &reason);
@@ -662,7 +662,7 @@
 			jabber_iq_send(iq);
 
 		} else if(xmlnode_get_child(query, "password")) {
-			if(js->gsc == NULL && !purple_account_get_bool(js->gc->account,
+			if(!jabber_stream_is_ssl(js) && !purple_account_get_bool(js->gc->account,
 						"auth_plain_in_clear", FALSE)) {
 				char *msg = g_strdup_printf(_("%s requires plaintext authentication over an unencrypted connection.  Allow this and continue authentication?"),
 											js->gc->account->username);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libpurple/protocols/jabber/bosh.c	Sat Apr 04 20:25:03 2009 +0000
@@ -0,0 +1,829 @@
+/*
+ * purple - Jabber Protocol Plugin
+ *
+ * Copyright (C) 2008, Tobias Markmann <tmarkmann@googlemail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
+ *
+ */
+#include "internal.h"
+#include "circbuffer.h"
+#include "core.h"
+#include "cipher.h"
+#include "debug.h"
+#include "prpl.h"
+#include "util.h"
+#include "xmlnode.h"
+
+#include "bosh.h"
+
+#define MAX_HTTP_CONNECTIONS      2
+#define MAX_FAILED_CONNECTIONS    3
+
+typedef struct _PurpleHTTPConnection PurpleHTTPConnection;
+
+typedef void (*PurpleBOSHConnectionConnectFunction)(PurpleBOSHConnection *conn);
+typedef void (*PurpleBOSHConnectionReceiveFunction)(PurpleBOSHConnection *conn, xmlnode *node);
+
+static char *bosh_useragent = NULL;
+
+typedef enum {
+	PACKET_TERMINATE,
+	PACKET_STREAM_RESTART,
+	PACKET_NORMAL,
+} PurpleBOSHPacketType;
+
+struct _PurpleBOSHConnection {
+	JabberStream *js;
+	gboolean pipelining;
+	PurpleHTTPConnection *connections[MAX_HTTP_CONNECTIONS];
+	unsigned short failed_connections;
+
+	gboolean ready;
+	gboolean ssl;
+
+	/* decoded URL */
+	char *host;
+	int port;
+	char *path; 
+
+	/* Must be big enough to hold 2^53 - 1 */
+	guint64 rid;
+	char *sid;
+
+	unsigned int inactivity_timer;
+	int max_inactivity;
+	int wait;
+
+	PurpleCircBuffer *pending;
+	int max_requests;
+	int requests;
+
+	PurpleBOSHConnectionConnectFunction connect_cb;
+	PurpleBOSHConnectionReceiveFunction receive_cb;
+};
+
+struct _PurpleHTTPConnection {
+	PurpleBOSHConnection *bosh;
+	PurpleSslConnection *psc;
+	int fd;
+	int ie_handle;
+
+	gboolean ready;
+	int requests; /* number of outstanding HTTP requests */
+
+	GString *buf;
+	gboolean headers_done;
+	gsize handled_len;
+	gsize body_len;
+
+};
+
+static void http_connection_connect(PurpleHTTPConnection *conn);
+static void http_connection_send_request(PurpleHTTPConnection *conn, const GString *req);
+
+void jabber_bosh_init(void)
+{
+	GHashTable *ui_info = purple_core_get_ui_info();
+	const char *ui_name = NULL;
+	const char *ui_version = NULL;
+
+	if (ui_info) {
+		ui_name = g_hash_table_lookup(ui_info, "name");
+		ui_version = g_hash_table_lookup(ui_info, "version");
+	}
+
+	if (ui_name)
+		bosh_useragent = g_strdup_printf("%s%s%s (libpurple " VERSION ")",
+		                                 ui_name, ui_version ? " " : "",
+		                                 ui_version ? ui_version : "");
+	else
+		bosh_useragent = g_strdup("libpurple " VERSION);
+}
+
+void jabber_bosh_uninit(void)
+{
+	g_free(bosh_useragent);
+	bosh_useragent = NULL;
+}
+
+static PurpleHTTPConnection*
+jabber_bosh_http_connection_init(PurpleBOSHConnection *bosh)
+{
+	PurpleHTTPConnection *conn = g_new0(PurpleHTTPConnection, 1);
+	conn->bosh = bosh;
+	conn->fd = -1;
+	conn->ready = FALSE;
+
+	return conn;
+}
+
+static void
+jabber_bosh_http_connection_destroy(PurpleHTTPConnection *conn)
+{
+	if (conn->buf)
+		g_string_free(conn->buf, TRUE);
+
+	if (conn->ie_handle)
+		purple_input_remove(conn->ie_handle);
+	if (conn->psc)
+		purple_ssl_close(conn->psc);
+	if (conn->fd >= 0)
+		close(conn->fd);
+
+	purple_proxy_connect_cancel_with_handle(conn);
+
+	g_free(conn);
+}
+
+PurpleBOSHConnection*
+jabber_bosh_connection_init(JabberStream *js, const char *url)
+{
+	PurpleBOSHConnection *conn;
+	char *host, *path, *user, *passwd;
+	int port;
+
+	if (!purple_url_parse(url, &host, &port, &path, &user, &passwd)) {
+		purple_debug_info("jabber", "Unable to parse given URL.\n");
+		return NULL;
+	}
+
+	conn = g_new0(PurpleBOSHConnection, 1);
+	conn->host = host;
+	conn->port = port;
+	conn->path = g_strdup_printf("/%s", path);
+	g_free(path);
+	conn->pipelining = TRUE;
+
+	if ((user && user[0] != '\0') || (passwd && passwd[0] != '\0')) {
+		purple_debug_info("jabber", "Ignoring unexpected username and password "
+		                            "in BOSH URL.\n");
+	}
+
+	g_free(user);
+	g_free(passwd);
+
+	conn->js = js;
+	/* FIXME: This doesn't seem very random */
+	conn->rid = rand() % 100000 + 1728679472;
+
+	conn->pending = purple_circ_buffer_new(0 /* default grow size */);
+
+	conn->ready = FALSE;
+	if (purple_strcasestr(url, "https://") != NULL)
+		conn->ssl = TRUE;
+	else
+		conn->ssl = FALSE;
+
+	conn->connections[0] = jabber_bosh_http_connection_init(conn);
+
+	return conn;
+}
+
+void
+jabber_bosh_connection_destroy(PurpleBOSHConnection *conn)
+{
+	int i;
+
+	g_free(conn->host);
+	g_free(conn->path);
+
+	if (conn->inactivity_timer)
+		purple_timeout_remove(conn->inactivity_timer);
+
+	purple_circ_buffer_destroy(conn->pending);
+
+	for (i = 0; i < MAX_HTTP_CONNECTIONS; ++i) {
+		if (conn->connections[i])
+			jabber_bosh_http_connection_destroy(conn->connections[i]);
+	}
+
+	g_free(conn);
+}
+
+gboolean jabber_bosh_connection_is_ssl(PurpleBOSHConnection *conn)
+{
+	return conn->ssl;
+}
+
+static PurpleHTTPConnection *
+find_available_http_connection(PurpleBOSHConnection *conn)
+{
+	int i;
+
+	/* Easy solution: Does everyone involved support pipelining? Hooray! Just use
+	 * one TCP connection! */
+	if (conn->pipelining)
+		return conn->connections[0];
+
+	/* First loop, look for a connection that's ready */
+	for (i = 0; i < MAX_HTTP_CONNECTIONS; ++i) {
+		if (conn->connections[i] && conn->connections[i]->ready &&
+		                            conn->connections[i]->requests == 0)
+			return conn->connections[i];
+	}
+
+	/* Second loop, look for one that's NULL and create a new connection */
+	for (i = 0; i < MAX_HTTP_CONNECTIONS; ++i) {
+		if (!conn->connections[i]) {
+			conn->connections[i] = jabber_bosh_http_connection_init(conn);
+
+			http_connection_connect(conn->connections[i]);
+			return NULL;
+		}
+	}
+
+	/* None available. */
+	return NULL;
+}
+
+static void
+jabber_bosh_connection_send(PurpleBOSHConnection *conn, PurpleBOSHPacketType type,
+                            const char *data)
+{
+	PurpleHTTPConnection *chosen;
+	GString *packet = NULL;
+
+	chosen = find_available_http_connection(conn);
+
+	if (type != PACKET_NORMAL && !chosen) {
+		/*
+		 * For non-ordinary traffic, we don't want to 'buffer' it, so use the first
+		 * connection.
+		 */
+		chosen = conn->connections[0];
+	
+		if (!chosen->ready)
+			purple_debug_warning("jabber", "First BOSH connection wasn't ready. Bad "
+					"things may happen.\n");
+	}
+
+	if (type == PACKET_NORMAL && (!chosen ||
+	        (conn->max_requests > 0 && conn->requests == conn->max_requests))) {
+		/*
+		 * For normal data, send up to max_requests requests at a time or there is no
+		 * connection ready (likely, we're currently opening a second connection and
+		 * will send these packets when connected).
+		 */
+		if (data) {
+			int len = data ? strlen(data) : 0;
+			purple_circ_buffer_append(conn->pending, data, len); 
+		}
+		return;
+	}
+
+	packet = g_string_new("");
+
+	g_string_printf(packet, "<body "
+	                "rid='%" G_GUINT64_FORMAT "' "
+	                "sid='%s' "
+	                "to='%s' "
+	                "xml:lang='en' "
+	                "xmlns='http://jabber.org/protocol/httpbind' "
+	                "xmlns:xmpp='urn:xmpp:xbosh'",
+	                ++conn->rid,
+	                conn->sid,
+	                conn->js->user->domain);
+
+	if (type == PACKET_STREAM_RESTART)
+		packet = g_string_append(packet, " xmpp:restart='true'/>");
+	else {
+		gsize read_amt;
+		if (type == PACKET_TERMINATE)
+			packet = g_string_append(packet, " type='terminate'");
+
+		packet = g_string_append_c(packet, '>');
+
+		while ((read_amt = purple_circ_buffer_get_max_read(conn->pending)) > 0) {
+			packet = g_string_append_len(packet, conn->pending->outptr, read_amt);
+			purple_circ_buffer_mark_read(conn->pending, read_amt);
+		}
+
+		if (data)
+			packet = g_string_append(packet, data);
+		packet = g_string_append(packet, "</body>");
+	}
+
+	http_connection_send_request(chosen, packet);
+}
+
+void jabber_bosh_connection_close(PurpleBOSHConnection *conn)
+{
+	jabber_bosh_connection_send(conn, PACKET_TERMINATE, NULL);
+}
+
+static void jabber_bosh_connection_stream_restart(PurpleBOSHConnection *conn) {
+	jabber_bosh_connection_send(conn, PACKET_STREAM_RESTART, NULL);
+}
+
+static gboolean jabber_bosh_connection_error_check(PurpleBOSHConnection *conn, xmlnode *node) {
+	const char *type;
+
+	type = xmlnode_get_attrib(node, "type");
+	
+	if (type != NULL && !strcmp(type, "terminate")) {
+		conn->ready = FALSE;
+		purple_connection_error_reason (conn->js->gc,
+			PURPLE_CONNECTION_ERROR_OTHER_ERROR,
+			_("The BOSH connection manager terminated your session."));
+		return TRUE;
+	}
+	return FALSE;
+}
+
+static gboolean
+bosh_inactivity_cb(gpointer data)
+{
+	PurpleBOSHConnection *bosh = data;
+
+	jabber_bosh_connection_send(bosh, PACKET_NORMAL, NULL);
+	return TRUE;
+}
+
+static void jabber_bosh_connection_received(PurpleBOSHConnection *conn, xmlnode *node) {
+	xmlnode *child;
+	JabberStream *js = conn->js;
+
+	g_return_if_fail(node != NULL);
+	if (jabber_bosh_connection_error_check(conn, node))
+		return;
+
+	child = node->child;
+	while (child != NULL) {
+		/* jabber_process_packet might free child */
+		xmlnode *next = child->next;
+		if (child->type == XMLNODE_TYPE_TAG) {
+			if (!strcmp(child->name, "iq")) {
+				if (xmlnode_get_child(child, "session"))
+					conn->ready = TRUE;
+			}
+
+			jabber_process_packet(js, &child);
+		}
+
+		child = next;
+	}
+}
+
+static void auth_response_cb(PurpleBOSHConnection *conn, xmlnode *node) {
+	xmlnode *child;
+
+	g_return_if_fail(node != NULL);
+	if (jabber_bosh_connection_error_check(conn, node))
+		return;
+
+	child = node->child;
+	while(child != NULL && child->type != XMLNODE_TYPE_TAG) {
+		child = child->next;
+	}
+
+	/* We're only expecting one XML node here, so only process the first one */
+	if (child != NULL && child->type == XMLNODE_TYPE_TAG) {
+		JabberStream *js = conn->js;
+		if (!strcmp(child->name, "success")) {
+			jabber_bosh_connection_stream_restart(conn);
+			jabber_process_packet(js, &child);
+			conn->receive_cb = jabber_bosh_connection_received;
+		} else {
+			js->state = JABBER_STREAM_AUTHENTICATING;
+			jabber_process_packet(js, &child);
+		}
+	} else {
+		purple_debug_warning("jabber", "Received unexepcted empty BOSH packet.\n");	
+	}
+}
+
+static void boot_response_cb(PurpleBOSHConnection *conn, xmlnode *node) {
+	const char *sid, *version;
+	const char *inactivity, *requests;
+	xmlnode *packet;
+
+	g_return_if_fail(node != NULL);
+	if (jabber_bosh_connection_error_check(conn, node))
+		return;
+
+	sid = xmlnode_get_attrib(node, "sid");
+	version = xmlnode_get_attrib(node, "ver");
+
+	inactivity = xmlnode_get_attrib(node, "inactivity");
+	requests = xmlnode_get_attrib(node, "requests");
+
+	if (sid) {
+		conn->sid = g_strdup(sid);
+	} else {
+		purple_connection_error_reason(conn->js->gc,
+		        PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
+		        _("No session ID given"));
+		return;
+	}
+
+	if (version) {
+		const char *dot = strstr(version, ".");
+		int major = atoi(version);
+		int minor = atoi(dot + 1);
+
+		purple_debug_info("jabber", "BOSH connection manager version %s\n", version);
+
+		if (major != 1 || minor < 6) {
+			purple_connection_error_reason(conn->js->gc,
+			        PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
+			        _("Unsupported version of BOSH protocol"));
+			return;
+		}
+	} else {
+		purple_debug_info("jabber", "Missing version in BOSH initiation\n");
+	}
+
+	if (inactivity) {
+		conn->max_inactivity = atoi(inactivity);
+		if (conn->max_inactivity <= 2) {
+			purple_debug_warning("jabber", "Ignoring bogusly small inactivity: %s\n",
+			                     inactivity);
+			conn->max_inactivity = 0;
+		} else {
+			/* TODO: Integrate this with jabber.c keepalive checks... */
+			conn->inactivity_timer = purple_timeout_add_seconds(
+					conn->max_inactivity - 2 /* rounding */, bosh_inactivity_cb,
+					conn);
+		}
+	}
+
+	if (requests)
+		conn->max_requests = atoi(requests);
+
+	/* FIXME: Depending on receiving features might break with some hosts */
+	packet = xmlnode_get_child(node, "features");
+	conn->js->use_bosh = TRUE;
+	conn->receive_cb = auth_response_cb;
+	jabber_stream_features_parse(conn->js, packet);		
+}
+
+static void jabber_bosh_connection_boot(PurpleBOSHConnection *conn) {
+	GString *buf = g_string_new("");
+
+	g_string_printf(buf, "<body content='text/xml; charset=utf-8' "
+	                "secure='true' "
+	                "to='%s' "
+	                "xml:lang='en' "
+	                "xmpp:version='1.0' "
+	                "ver='1.6' "
+	                "xmlns:xmpp='urn:xmpp:bosh' "
+	                "rid='%" G_GUINT64_FORMAT "' "
+/* TODO: This should be adjusted/adjustable automatically according to
+ * realtime network behavior */
+	                "wait='60' "
+	                "hold='1' "
+	                "xmlns='http://jabber.org/protocol/httpbind'/>",
+	                conn->js->user->domain,
+	                ++conn->rid);
+
+	conn->receive_cb = boot_response_cb;
+	http_connection_send_request(conn->connections[0], buf);
+	g_string_free(buf, TRUE);
+}
+
+static void
+http_received_cb(const char *data, int len, PurpleBOSHConnection *conn)
+{
+	if (conn->failed_connections)
+		/* We've got some data, so reset the number of failed connections */
+		conn->failed_connections = 0;
+
+	if (conn->receive_cb) {
+		xmlnode *node = xmlnode_from_str(data, len);
+
+		purple_debug_info("jabber", "RecvBOSH %s(%d): %s\n",
+		                  conn->ssl ? "(ssl)" : "", len, data);
+
+		if (node) {
+			conn->receive_cb(conn, node);
+			xmlnode_free(node);
+		} else {
+			purple_debug_warning("jabber", "BOSH: Received invalid XML\n");
+		}
+	} else {
+		g_return_if_reached();
+	}
+}
+
+void jabber_bosh_connection_send_raw(PurpleBOSHConnection *conn,
+                                     const char *data)
+{
+	jabber_bosh_connection_send(conn, PACKET_NORMAL, data);
+}
+
+static void
+connection_common_established_cb(PurpleHTTPConnection *conn)
+{
+	/* Indicate we're ready and reset some variables */
+	conn->ready = TRUE;
+	conn->requests = 0;
+	if (conn->buf) {
+		g_string_free(conn->buf, TRUE);
+		conn->buf = NULL;
+	}
+	conn->headers_done = FALSE;
+	conn->handled_len = conn->body_len = 0;
+
+	if (conn->bosh->ready) {
+		purple_debug_info("jabber", "BOSH session already exists. Trying to reuse it.\n");
+		if (conn->bosh->pending->bufused > 0) {
+			/* Send the pending data */
+			jabber_bosh_connection_send(conn->bosh, PACKET_NORMAL, NULL);
+		}
+#if 0
+		conn->bosh->receive_cb = jabber_bosh_connection_received;
+		if (conn->bosh->connect_cb)
+			conn->bosh->connect_cb(conn->bosh);
+#endif
+	} else
+		jabber_bosh_connection_boot(conn->bosh);
+}
+
+void jabber_bosh_connection_refresh(PurpleBOSHConnection *conn)
+{
+	jabber_bosh_connection_send(conn, PACKET_NORMAL, NULL);
+}
+
+static void http_connection_disconnected(PurpleHTTPConnection *conn)
+{
+	/*
+	 * Well, then. Fine! I never liked you anyway, server! I was cheating on you
+	 * with AIM!
+	 */
+	conn->ready = FALSE;
+	if (conn->psc) {
+		purple_ssl_close(conn->psc);
+		conn->psc = NULL;
+	} else if (conn->fd >= 0) {
+		close(conn->fd);
+		conn->fd = -1;
+	}
+
+	if (conn->ie_handle) {
+		purple_input_remove(conn->ie_handle);
+		conn->ie_handle = 0;
+	}
+
+	if (conn->bosh->pipelining)
+		/* Hmmmm, fall back to multiple connections */
+		conn->bosh->pipelining = FALSE;
+
+	if (++conn->bosh->failed_connections == MAX_FAILED_CONNECTIONS) {
+		purple_connection_error_reason(conn->bosh->js->gc,
+				PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
+				_("Unable to establish a connection with the server"));
+	} else {
+		/* No! Please! Take me back. It was me, not you! I was weak! */
+		http_connection_connect(conn);
+	}
+}
+
+void jabber_bosh_connection_connect(PurpleBOSHConnection *bosh) {
+	PurpleHTTPConnection *conn = bosh->connections[0];
+	http_connection_connect(conn);
+}
+
+static void
+jabber_bosh_http_connection_process(PurpleHTTPConnection *conn)
+{
+	const char *cursor;
+
+	cursor = conn->buf->str + conn->handled_len;
+
+	if (!conn->headers_done) {
+		const char *content_length = purple_strcasestr(cursor, "\r\nContent-Length");
+		const char *end_of_headers = purple_strcasestr(cursor, "\r\n\r\n");
+
+		/* Make sure Content-Length is in headers, not body */
+		if (content_length && content_length < end_of_headers) {
+			char *sep = strstr(content_length, ": ");
+			int len = atoi(sep + 2);
+			if (len == 0) 
+				purple_debug_warning("jabber", "Found mangled Content-Length header.\n");
+
+			conn->body_len = len;
+		}
+
+		if (end_of_headers) {
+			conn->headers_done = TRUE;
+			conn->handled_len = end_of_headers - conn->buf->str + 4;
+			cursor = end_of_headers + 4;
+		} else {
+			conn->handled_len = conn->buf->len;
+			return;
+		}
+	}
+
+	/* Have we handled everything in the buffer? */
+	if (conn->handled_len >= conn->buf->len)
+		return;
+
+	/* Have we read all that the Content-Length promised us? */
+	if (conn->buf->len - conn->handled_len < conn->body_len)
+		return;
+
+	--conn->requests;
+	--conn->bosh->requests;
+
+	http_received_cb(conn->buf->str + conn->handled_len, conn->body_len,
+	                 conn->bosh);
+
+	if (conn->bosh->ready &&
+			(conn->bosh->requests == 0 || conn->bosh->pending->bufused > 0)) {
+		jabber_bosh_connection_send(conn->bosh, PACKET_NORMAL, NULL);
+		purple_debug_misc("jabber", "BOSH: Sending an empty request\n");
+	}
+
+	g_string_free(conn->buf, TRUE);
+	conn->buf = NULL;
+	conn->headers_done = FALSE;
+	conn->handled_len = conn->body_len = 0;
+}
+
+/*
+ * Common code for reading, called from http_connection_read_cb_ssl and
+ * http_connection_read_cb.
+ */
+static void
+http_connection_read(PurpleHTTPConnection *conn)
+{
+	char buffer[1025];
+	int cnt, count = 0;
+
+	if (!conn->buf)
+		conn->buf = g_string_new("");
+
+	/* Read once to prime cnt before the loop */
+	if (conn->psc)
+		cnt = purple_ssl_read(conn->psc, buffer, sizeof(buffer));
+	else
+		cnt = read(conn->fd, buffer, sizeof(buffer));
+	while (cnt > 0) {
+		count += cnt;
+		g_string_append_len(conn->buf, buffer, cnt);
+
+		if (conn->psc)
+			cnt = purple_ssl_read(conn->psc, buffer, sizeof(buffer));
+		else
+			cnt = read(conn->fd, buffer, sizeof(buffer));
+	}
+
+	if (cnt == 0 || (cnt < 0 && errno != EAGAIN)) {
+		if (cnt < 0)
+			purple_debug_info("jabber", "bosh read=%d, errno=%d\n", cnt, errno);
+		else
+			purple_debug_info("jabber", "bosh server closed the connection\n");
+
+		/*
+		 * If the socket is closed, the processing really needs to know about
+		 * it. Handle that now (it will be handled again post-processing).
+		 */
+		http_connection_disconnected(conn);
+
+		/* Process what we do have */
+	}
+
+
+	jabber_bosh_http_connection_process(conn);
+}
+
+static void
+http_connection_read_cb(gpointer data, gint fd, PurpleInputCondition condition)
+{
+	PurpleHTTPConnection *conn = data;
+
+	http_connection_read(conn);
+}
+
+static void
+http_connection_read_cb_ssl(gpointer data, PurpleSslConnection *psc,
+                            PurpleInputCondition cond)
+{
+	PurpleHTTPConnection *conn = data;
+
+	http_connection_read(conn);
+}
+
+static void
+ssl_connection_established_cb(gpointer data, PurpleSslConnection *psc,
+                              PurpleInputCondition cond)
+{
+	PurpleHTTPConnection *conn = data;
+
+	purple_ssl_input_add(psc, http_connection_read_cb_ssl, conn);
+	connection_common_established_cb(conn);
+}
+
+static void
+ssl_connection_error_cb(PurpleSslConnection *gsc, PurpleSslErrorType error,
+                        gpointer data)
+{
+	PurpleHTTPConnection *conn = data;
+
+	/* sslconn frees the connection on error */
+	conn->psc = NULL;
+
+	purple_connection_ssl_error(conn->bosh->js->gc, error);
+}
+
+static void
+connection_established_cb(gpointer data, gint source, const gchar *error)
+{
+	PurpleHTTPConnection *conn = data;
+	PurpleConnection *gc = conn->bosh->js->gc;
+
+	if (source < 0) {
+		gchar *tmp;
+		tmp = g_strdup_printf(_("Could not establish a connection with the server:\n%s"),
+		        error);
+		purple_connection_error_reason(gc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, tmp);
+		g_free(tmp);
+		return;
+	}
+
+	conn->fd = source;
+	conn->ie_handle = purple_input_add(conn->fd, PURPLE_INPUT_READ,
+	        http_connection_read_cb, conn);
+	connection_common_established_cb(conn);
+}
+
+static void http_connection_connect(PurpleHTTPConnection *conn)
+{
+	PurpleBOSHConnection *bosh = conn->bosh;
+	PurpleConnection *gc = bosh->js->gc;
+	PurpleAccount *account = purple_connection_get_account(gc);
+
+	if (bosh->ssl) {
+		if (purple_ssl_is_supported()) {
+			conn->psc = purple_ssl_connect(account, bosh->host, bosh->port,
+			                               ssl_connection_established_cb,
+			                               ssl_connection_error_cb,
+			                               conn);
+			if (!conn->psc) {
+				purple_connection_error_reason(gc,
+					PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT,
+					_("Unable to establish SSL connection"));
+			}
+		} else {
+			purple_connection_error_reason(gc,
+			    PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT,
+			    _("SSL support unavailable"));
+		}
+	} else if (purple_proxy_connect(conn, account, bosh->host, bosh->port,
+	                                 connection_established_cb, conn) == NULL) {
+		purple_connection_error_reason(gc,
+		    PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
+		    _("Unable to create socket"));
+	}
+}
+
+static void
+http_connection_send_request(PurpleHTTPConnection *conn, const GString *req)
+{
+	char *packet;
+	int ret;
+
+	packet = g_strdup_printf("POST %s HTTP/1.1\r\n"
+	                       "Host: %s\r\n"
+	                       "User-Agent: %s\r\n"
+	                       "Content-Encoding: text/xml; charset=utf-8\r\n"
+	                       "Content-Length: %" G_GSIZE_FORMAT "\r\n\r\n"
+	                       "%s",
+	                       conn->bosh->path, conn->bosh->host, bosh_useragent,
+	                       req->len, req->str);
+
+	/* TODO: Better error handling, circbuffer or possible integration with
+	 * low-level code in jabber.c */
+	if (conn->psc)
+		ret = purple_ssl_write(conn->psc, packet, strlen(packet));
+	else
+		ret = write(conn->fd, packet, strlen(packet));
+
+	++conn->requests;
+	++conn->bosh->requests;
+	g_free(packet);
+
+	if (ret < 0 && errno == EAGAIN)
+		purple_debug_error("jabber", "BOSH write would have blocked\n");
+
+	if (ret <= 0) {
+		purple_connection_error_reason(conn->bosh->js->gc,
+				PURPLE_CONNECTION_ERROR_NETWORK_ERROR,
+				_("Write error"));
+		return;
+	}
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libpurple/protocols/jabber/bosh.h	Sat Apr 04 20:25:03 2009 +0000
@@ -0,0 +1,41 @@
+/**
+ * @file bosh.h Buddy handlers
+ *
+ * purple
+ *
+ * Copyright (C) 2008, Tobias Markmann <tmarkmann@googlemail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
+ */
+#ifndef _PURPLE_JABBER_BOSH_H_
+#define _PURPLE_JABBER_BOSH_H_
+
+typedef struct _PurpleBOSHConnection PurpleBOSHConnection;
+
+#include "jabber.h"
+
+void jabber_bosh_init(void);
+void jabber_bosh_uninit(void);
+
+PurpleBOSHConnection* jabber_bosh_connection_init(JabberStream *js, const char *url);
+void jabber_bosh_connection_destroy(PurpleBOSHConnection *conn);
+
+gboolean jabber_bosh_connection_is_ssl(PurpleBOSHConnection *conn);
+
+void jabber_bosh_connection_connect(PurpleBOSHConnection *conn);
+void jabber_bosh_connection_close(PurpleBOSHConnection *conn);
+void jabber_bosh_connection_send_raw(PurpleBOSHConnection *conn, const char *data);
+void jabber_bosh_connection_refresh(PurpleBOSHConnection *conn);
+#endif /* _PURPLE_JABBER_BOSH_H_ */
--- a/libpurple/protocols/jabber/buddy.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/buddy.c	Sat Apr 04 20:25:03 2009 +0000
@@ -180,8 +180,10 @@
 		jbr->commands = g_list_delete_link(jbr->commands, jbr->commands);
 	}
 
-	jabber_caps_free_clientinfo(jbr->caps);
-
+	if (jbr->caps.exts) {
+		g_list_foreach(jbr->caps.exts, (GFunc)g_free, NULL);
+		g_list_free(jbr->caps.exts);
+	}
 	g_free(jbr->name);
 	g_free(jbr->status);
 	g_free(jbr->thread_id);
@@ -489,9 +491,6 @@
 
 void jabber_set_buddy_icon(PurpleConnection *gc, PurpleStoredImage *img)
 {
-	PurplePresence *gpresence;
-	PurpleStatus *status;
-
 	if(((JabberStream*)purple_connection_get_protocol_data(gc))->pep) {
 		/* XEP-0084: User Avatars */
 		if(img) {
@@ -609,9 +608,7 @@
 	 */
 	jabber_set_info(gc, purple_account_get_user_info(gc->account));
 
-	gpresence = purple_account_get_presence(gc->account);
-	status = purple_presence_get_active_status(gpresence);
-	jabber_presence_send(gc->account, status);
+	jabber_presence_send(gc->proto_data, FALSE);
 }
 
 /*
@@ -2491,23 +2488,35 @@
 gboolean
 jabber_resource_has_capability(const JabberBuddyResource *jbr, const gchar *cap)
 {
-	const GList *iter = NULL;
+	const GList *node = NULL;
+	const JabberCapsNodeExts *exts;
 
-	if (!jbr->caps) {
+	if (!jbr->caps.info) {
 		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)) {
-		if (strcmp(iter->data, cap) == 0) {
-			purple_debug_info("jabber", "Found cap: %s\n", (char *)iter->data);
-			return TRUE;
+	node = g_list_find_custom(jbr->caps.info->features, cap, (GCompareFunc)strcmp);
+	if (!node && jbr->caps.exts && jbr->caps.info->exts) {
+		const GList *ext;
+		exts = jbr->caps.info->exts;
+		/* Walk through all the enabled caps, checking each list for the cap.
+		 * Don't check it twice, though. */
+		for (ext = jbr->caps.exts; ext && !node; ext = ext->next) {
+			GList *features = g_hash_table_lookup(exts->exts, ext->data);
+			if (features)
+				node = g_list_find_custom(features, cap, (GCompareFunc)strcmp);
 		}
 	}
 
-	purple_debug_info("jabber", "Cap %s not found\n", cap);
-	return FALSE;
+	/* TODO: Are these messages actually useful? */
+	if (node)
+		purple_debug_info("jabber", "Found cap: %s\n", cap);
+	else
+		purple_debug_info("jabber", "Cap %s not found\n", cap); 
+
+	return (node != NULL);
 }
 
 gboolean
--- a/libpurple/protocols/jabber/buddy.h	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/buddy.h	Sat Apr 04 20:25:03 2009 +0000
@@ -81,7 +81,10 @@
 		char *name;
 		char *os;
 	} client;
-	JabberCapsClientInfo *caps;
+	struct {
+		JabberCapsClientInfo *info;
+		GList *exts;
+	} caps;
 	GList *commands;
 } JabberBuddyResource;
 
--- a/libpurple/protocols/jabber/caps.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/caps.c	Sat Apr 04 20:25:03 2009 +0000
@@ -21,99 +21,222 @@
 
 #include "internal.h"
 
+#include "debug.h"
 #include "caps.h"
-#include <string.h>
-#include "internal.h"
+#include "cipher.h"
+#include "iq.h"
+#include "presence.h"
 #include "util.h"
-#include "iq.h"
 
 #define JABBER_CAPS_FILENAME "xmpp-caps.xml"
 
-static GHashTable *capstable = NULL; /* JabberCapsKey -> JabberCapsValue */
+typedef struct _JabberDataFormField {
+	gchar *var;
+	GList *values;
+} JabberDataFormField;
 
 typedef struct _JabberCapsKey {
 	char *node;
 	char *ver;
+	char *hash;
 } JabberCapsKey;
 
-typedef struct _JabberCapsValueExt {
-	GList *identities; /* JabberCapsIdentity */
-	GList *features; /* char * */
-} JabberCapsValueExt;
+static GHashTable *capstable = NULL; /* JabberCapsKey -> JabberCapsClientInfo */
+static GHashTable *nodetable = NULL; /* char *node -> JabberCapsNodeExts */
+static guint       save_timer = 0;
+
+/**
+ *	Processes a query-node and returns a JabberCapsClientInfo object with all relevant info.
+ *	
+ *	@param 	query 	A query object.
+ *	@return 		A JabberCapsClientInfo object.
+ */
+static JabberCapsClientInfo *jabber_caps_parse_client_info(xmlnode *query);
+
+/* Free a GList of allocated char* */
+static void
+free_string_glist(GList *list)
+{
+	g_list_foreach(list, (GFunc)g_free, NULL);
+	g_list_free(list);
+}
+
+static JabberCapsNodeExts*
+jabber_caps_node_exts_ref(JabberCapsNodeExts *exts)
+{
+	g_return_val_if_fail(exts != NULL, NULL);
 
-typedef struct _JabberCapsValue {
-	GList *identities; /* JabberCapsIdentity */
-	GList *features; /* char * */
-	GHashTable *ext; /* char * -> JabberCapsValueExt */
-} JabberCapsValue;
+	++exts->ref;
+	return exts;
+}
+
+static void
+jabber_caps_node_exts_unref(JabberCapsNodeExts *exts)
+{
+	if (exts == NULL)
+		return;
+
+	g_return_if_fail(exts->ref != 0);
 
-static guint jabber_caps_hash(gconstpointer key) {
-	const JabberCapsKey *name = key;
-	guint nodehash = g_str_hash(name->node);
-	guint verhash = g_str_hash(name->ver);
+	if (--exts->ref != 0)
+		return;
+
+	g_hash_table_destroy(exts->exts);
+	g_free(exts);
+}
 
-	return nodehash ^ verhash;
+static guint jabber_caps_hash(gconstpointer data) {
+	const JabberCapsKey *key = data;
+	guint nodehash = g_str_hash(key->node);
+	guint verhash  = g_str_hash(key->ver);
+	/* 'hash' was optional in XEP-0115 v1.4 and I think g_str_hash crashes on
+	 * NULL >:O. Okay, maybe I've played too much Zelda, but that looks like
+	 * a Deku Shrub... */
+	guint hashhash = (key->hash ? g_str_hash(key->hash) : 0);
+	return nodehash ^ verhash ^ hashhash;
 }
 
 static gboolean jabber_caps_compare(gconstpointer v1, gconstpointer v2) {
 	const JabberCapsKey *name1 = v1;
 	const JabberCapsKey *name2 = v2;
+	/* Again, hash might be NULL and I *know* strcmp will crash on NULL. */
+	gboolean hasheq = ((!name1->hash && !name2->hash) ||
+	        (name1->hash && name2->hash && !strcmp(name1->hash, name2->hash)));
 
-	return strcmp(name1->node,name2->node) == 0 && strcmp(name1->ver,name2->ver) == 0;
+	return strcmp(name1->node, name2->node) == 0 &&
+	       strcmp(name1->ver, name2->ver) == 0 &&
+	       hasheq;
 }
 
-static void jabber_caps_destroy_key(gpointer key) {
-	JabberCapsKey *keystruct = key;
-	g_free(keystruct->node);
-	g_free(keystruct->ver);
-	g_free(keystruct);
+void jabber_caps_destroy_key(gpointer data) {
+	JabberCapsKey *key = data;
+	g_free(key->node);
+	g_free(key->ver);
+	g_free(key->hash);
+	g_free(key);
 }
 
-static void jabber_caps_destroy_value(gpointer value) {
-	JabberCapsValue *valuestruct = value;
-	while(valuestruct->identities) {
-		JabberCapsIdentity *id = valuestruct->identities->data;
+static void
+jabber_caps_client_info_destroy(JabberCapsClientInfo *info)
+{
+	if (info == NULL)
+		return;
+
+	while(info->identities) {
+		JabberIdentity *id = info->identities->data;
 		g_free(id->category);
 		g_free(id->type);
 		g_free(id->name);
+		g_free(id->lang);
 		g_free(id);
-
-		valuestruct->identities = g_list_delete_link(valuestruct->identities,valuestruct->identities);
+		info->identities = g_list_delete_link(info->identities, info->identities);
 	}
-	while(valuestruct->features) {
-		g_free(valuestruct->features->data);
-		valuestruct->features = g_list_delete_link(valuestruct->features,valuestruct->features);
+
+	free_string_glist(info->features);
+	free_string_glist(info->forms);
+
+	jabber_caps_node_exts_unref(info->exts);
+
+	g_free(info);
+}
+
+/* NOTE: Takes a reference to the exts, unref it if you don't really want to
+ * keep it around. */
+static JabberCapsNodeExts*
+jabber_caps_find_exts_by_node(const char *node)
+{
+	JabberCapsNodeExts *exts;
+	if (NULL == (exts = g_hash_table_lookup(nodetable, node))) {
+		exts = g_new0(JabberCapsNodeExts, 1);
+		exts->exts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
+		                                   (GDestroyNotify)free_string_glist);
+		g_hash_table_insert(nodetable, g_strdup(node), jabber_caps_node_exts_ref(exts));
 	}
-	g_hash_table_destroy(valuestruct->ext);
-	g_free(valuestruct);
+
+	return jabber_caps_node_exts_ref(exts);
+}
+
+static void
+exts_to_xmlnode(gconstpointer key, gconstpointer value, gpointer user_data)
+{
+	const char *identifier = key;
+	const GList *features = value, *node;
+	xmlnode *client = user_data, *ext, *feature;
+
+	ext = xmlnode_new_child(client, "ext");
+	xmlnode_set_attrib(ext, "identifier", identifier);
+
+	for (node = features; node; node = node->next) {
+		feature = xmlnode_new_child(ext, "feature");
+		xmlnode_set_attrib(feature, "var", (const gchar *)node->data);
+	}
 }
 
-static void jabber_caps_ext_destroy_value(gpointer value) {
-	JabberCapsValueExt *valuestruct = value;
-	while(valuestruct->identities) {
-		JabberCapsIdentity *id = valuestruct->identities->data;
-		g_free(id->category);
-		g_free(id->type);
-		g_free(id->name);
-		g_free(id);
+static void jabber_caps_store_client(gpointer key, gpointer value, gpointer user_data) {
+	JabberCapsKey *clientinfo = key;
+	JabberCapsClientInfo *props = value;
+	xmlnode *root = user_data;
+	xmlnode *client = xmlnode_new_child(root, "client");
+	GList *iter;
 
-		valuestruct->identities = g_list_delete_link(valuestruct->identities,valuestruct->identities);
+	xmlnode_set_attrib(client, "node", clientinfo->node);
+	xmlnode_set_attrib(client, "ver", clientinfo->ver);
+	if (clientinfo->hash)
+		xmlnode_set_attrib(client, "hash", clientinfo->hash);
+	for(iter = props->identities; iter; iter = g_list_next(iter)) {
+		JabberIdentity *id = iter->data;
+		xmlnode *identity = xmlnode_new_child(client, "identity");
+		xmlnode_set_attrib(identity, "category", id->category);
+		xmlnode_set_attrib(identity, "type", id->type);
+		if (id->name)
+			xmlnode_set_attrib(identity, "name", id->name);
+		if (id->lang)
+			xmlnode_set_attrib(identity, "lang", id->lang);
 	}
-	while(valuestruct->features) {
-		g_free(valuestruct->features->data);
-		valuestruct->features = g_list_delete_link(valuestruct->features,valuestruct->features);
+
+	for(iter = props->features; iter; iter = g_list_next(iter)) {
+		const char *feat = iter->data;
+		xmlnode *feature = xmlnode_new_child(client, "feature");
+		xmlnode_set_attrib(feature, "var", feat);
 	}
-	g_free(valuestruct);
+	
+	for(iter = props->forms; iter; iter = g_list_next(iter)) {
+		/* FIXME: See #7814 */
+		xmlnode *xdata = iter->data;
+		xmlnode_insert_child(client, xmlnode_copy(xdata));
+	}
+
+	/* TODO: Ideally, only save this once-per-node... */
+	if (props->exts)
+		g_hash_table_foreach(props->exts->exts, (GHFunc)exts_to_xmlnode, client);
 }
 
-static void jabber_caps_load(void);
+static gboolean
+do_jabber_caps_store(gpointer data)
+{
+	char *str;
+	int length = 0;
+	xmlnode *root = xmlnode_new("capabilities");
+	g_hash_table_foreach(capstable, jabber_caps_store_client, root);
+	str = xmlnode_to_formatted_str(root, &length);
+	xmlnode_free(root);
+	purple_util_write_data_to_file(JABBER_CAPS_FILENAME, str, length);
+	g_free(str);
 
-void jabber_caps_init(void) {
-	capstable = g_hash_table_new_full(jabber_caps_hash, jabber_caps_compare, jabber_caps_destroy_key, jabber_caps_destroy_value);
-	jabber_caps_load();
+	save_timer = 0;
+	return FALSE;
 }
 
-static void jabber_caps_load(void) {
+static void
+schedule_caps_save(void)
+{
+	if (save_timer == 0)
+		save_timer = purple_timeout_add_seconds(5, do_jabber_caps_store, NULL);
+}
+
+static void
+jabber_caps_load(void)
+{
 	xmlnode *capsdata = purple_util_read_xml_from_file(JABBER_CAPS_FILENAME, "XMPP capabilities cache");
 	xmlnode *client;
 
@@ -124,17 +247,23 @@
 		xmlnode_free(capsdata);
 		return;
 	}
-
+	
 	for(client = capsdata->child; client; client = client->next) {
 		if(client->type != XMLNODE_TYPE_TAG)
 			continue;
 		if(!strcmp(client->name, "client")) {
 			JabberCapsKey *key = g_new0(JabberCapsKey, 1);
-			JabberCapsValue *value = g_new0(JabberCapsValue, 1);
+			JabberCapsClientInfo *value = g_new0(JabberCapsClientInfo, 1);
 			xmlnode *child;
+			JabberCapsNodeExts *exts = NULL;
 			key->node = g_strdup(xmlnode_get_attrib(client,"node"));
 			key->ver  = g_strdup(xmlnode_get_attrib(client,"ver"));
-			value->ext = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, jabber_caps_ext_destroy_value);
+			key->hash = g_strdup(xmlnode_get_attrib(client,"hash"));
+
+			/* v1.3 capabilities */
+			if (key->hash == NULL)
+				exts = jabber_caps_find_exts_by_node(key->node);
+
 			for(child = client->child; child; child = child->next) {
 				if(child->type != XMLNODE_TYPE_TAG)
 					continue;
@@ -147,432 +276,654 @@
 					const char *category = xmlnode_get_attrib(child, "category");
 					const char *type = xmlnode_get_attrib(child, "type");
 					const char *name = xmlnode_get_attrib(child, "name");
+					const char *lang = xmlnode_get_attrib(child, "lang");
+					JabberIdentity *id;
 
-					JabberCapsIdentity *id = g_new0(JabberCapsIdentity, 1);
+					if (!category || !type)
+						continue;
+
+					id = g_new0(JabberIdentity, 1);
 					id->category = g_strdup(category);
 					id->type = g_strdup(type);
 					id->name = g_strdup(name);
-
+					id->lang = g_strdup(lang);
+					
 					value->identities = g_list_append(value->identities,id);
-				} else if(!strcmp(child->name,"ext")) {
+				} else if(!strcmp(child->name,"x")) {
+					/* FIXME: See #7814 -- this will cause problems if anyone
+					 * ever actually specifies forms. In fact, for this to
+					 * work properly, that bug needs to be fixed in
+					 * xmlnode_from_str, not the output version... */
+					value->forms = g_list_append(value->forms, xmlnode_copy(child));
+				} else if (!strcmp(child->name, "ext") && key->hash != NULL) {
+					purple_debug_warning("jabber", "Ignoring exts when reading new-style caps\n");
+				} else if (!strcmp(child->name, "ext")) {
+					/* TODO: Do we care about reading in the identities listed here? */
 					const char *identifier = xmlnode_get_attrib(child, "identifier");
-					if(identifier) {
-						xmlnode *extchild;
+					xmlnode *node;
+					GList *features = NULL;
+
+					if (!identifier)
+						continue;
 
-						JabberCapsValueExt *extvalue = g_new0(JabberCapsValueExt, 1);
-
-						for(extchild = child->child; extchild; extchild = extchild->next) {
-							if(extchild->type != XMLNODE_TYPE_TAG)
+					for (node = child->child; node; node = node->next) {
+						if (node->type != XMLNODE_TYPE_TAG)
+							continue;
+						if (!strcmp(node->name, "feature")) {
+							const char *var = xmlnode_get_attrib(node, "var");
+							if (!var)
 								continue;
-							if(!strcmp(extchild->name,"feature")) {
-								const char *var = xmlnode_get_attrib(extchild, "var");
-								if(!var)
-									continue;
-								extvalue->features = g_list_append(extvalue->features,g_strdup(var));
-							} else if(!strcmp(extchild->name,"identity")) {
-								const char *category = xmlnode_get_attrib(extchild, "category");
-								const char *type = xmlnode_get_attrib(extchild, "type");
-								const char *name = xmlnode_get_attrib(extchild, "name");
+							features = g_list_prepend(features, g_strdup(var));
+						}
+					}
 
-								JabberCapsIdentity *id = g_new0(JabberCapsIdentity, 1);
-								id->category = g_strdup(category);
-								id->type = g_strdup(type);
-								id->name = g_strdup(name);
-
-								extvalue->identities = g_list_append(extvalue->identities,id);
-							}
-						}
-						g_hash_table_replace(value->ext, g_strdup(identifier), extvalue);
-					}
+					if (features) {
+						g_hash_table_insert(exts->exts, g_strdup(identifier),
+						                    features);
+					} else
+						purple_debug_warning("jabber", "Caps ext %s had no features.\n",
+						                     identifier);
 				}
 			}
+
+			value->exts = exts;
 			g_hash_table_replace(capstable, key, value);
+
 		}
 	}
 	xmlnode_free(capsdata);
 }
 
-static void jabber_caps_store_ext(gpointer key, gpointer value, gpointer user_data) {
-	const char *extname = key;
-	JabberCapsValueExt *props = value;
-	xmlnode *root = user_data;
-	xmlnode *ext = xmlnode_new_child(root,"ext");
-	GList *iter;
-
-	xmlnode_set_attrib(ext,"identifier",extname);
-
-	for(iter = props->identities; iter; iter = g_list_next(iter)) {
-		JabberCapsIdentity *id = iter->data;
-		xmlnode *identity = xmlnode_new_child(ext, "identity");
-		xmlnode_set_attrib(identity, "category", id->category);
-		xmlnode_set_attrib(identity, "type", id->type);
-		if (id->name)
-			xmlnode_set_attrib(identity, "name", id->name);
-	}
-
-	for(iter = props->features; iter; iter = g_list_next(iter)) {
-		const char *feat = iter->data;
-		xmlnode *feature = xmlnode_new_child(ext, "feature");
-		xmlnode_set_attrib(feature, "var", feat);
-	}
-}
-
-static void jabber_caps_store_client(gpointer key, gpointer value, gpointer user_data) {
-	JabberCapsKey *clientinfo = key;
-	JabberCapsValue *props = value;
-	xmlnode *root = user_data;
-	xmlnode *client = xmlnode_new_child(root,"client");
-	GList *iter;
-
-	xmlnode_set_attrib(client,"node",clientinfo->node);
-	xmlnode_set_attrib(client,"ver",clientinfo->ver);
-
-	for(iter = props->identities; iter; iter = g_list_next(iter)) {
-		JabberCapsIdentity *id = iter->data;
-		xmlnode *identity = xmlnode_new_child(client, "identity");
-		xmlnode_set_attrib(identity, "category", id->category);
-		xmlnode_set_attrib(identity, "type", id->type);
-		if (id->name)
-			xmlnode_set_attrib(identity, "name", id->name);
-	}
-
-	for(iter = props->features; iter; iter = g_list_next(iter)) {
-		const char *feat = iter->data;
-		xmlnode *feature = xmlnode_new_child(client, "feature");
-		xmlnode_set_attrib(feature, "var", feat);
-	}
-
-	g_hash_table_foreach(props->ext,jabber_caps_store_ext,client);
-}
-
-static void jabber_caps_store(void) {
-	char *str;
-	xmlnode *root = xmlnode_new("capabilities");
-	g_hash_table_foreach(capstable, jabber_caps_store_client, root);
-	str = xmlnode_to_formatted_str(root, NULL);
-	xmlnode_free(root);
-	purple_util_write_data_to_file(JABBER_CAPS_FILENAME, str, -1);
-	g_free(str);
+void jabber_caps_init(void)
+{
+	nodetable = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify)jabber_caps_node_exts_unref); 
+	capstable = g_hash_table_new_full(jabber_caps_hash, jabber_caps_compare, jabber_caps_destroy_key, (GDestroyNotify)jabber_caps_client_info_destroy);
+	jabber_caps_load();
 }
 
-/* this function assumes that all information is available locally */
-static JabberCapsClientInfo *jabber_caps_collect_info(const char *node, const char *ver, GList *ext) {
-	JabberCapsClientInfo *result;
-	JabberCapsKey *key = g_new0(JabberCapsKey, 1);
-	JabberCapsValue *caps;
-	GList *iter;
-
-	key->node = (char *)node;
-	key->ver = (char *)ver;
-
-	caps = g_hash_table_lookup(capstable,key);
-
-	g_free(key);
-
-	if (caps == NULL)
-		return NULL;
-
-	result = g_new0(JabberCapsClientInfo, 1);
-
-	/* join all information */
-	for(iter = caps->identities; iter; iter = g_list_next(iter)) {
-		JabberCapsIdentity *id = iter->data;
-		JabberCapsIdentity *newid = g_new0(JabberCapsIdentity, 1);
-		newid->category = g_strdup(id->category);
-		newid->type = g_strdup(id->type);
-		newid->name = g_strdup(id->name);
-
-		result->identities = g_list_append(result->identities,newid);
-	}
-	for(iter = caps->features; iter; iter = g_list_next(iter)) {
-		const char *feat = iter->data;
-		char *newfeat = g_strdup(feat);
-
-		result->features = g_list_append(result->features,newfeat);
+void jabber_caps_uninit(void)
+{
+	if (save_timer != 0) {
+		purple_timeout_remove(save_timer);
+		save_timer = 0;
+		do_jabber_caps_store(NULL);
 	}
-
-	for(iter = ext; iter; iter = g_list_next(iter)) {
-		const char *extname = iter->data;
-		JabberCapsValueExt *extinfo = g_hash_table_lookup(caps->ext,extname);
-
-		if(extinfo) {
-			GList *iter2;
-			for(iter2 = extinfo->identities; iter2; iter2 = g_list_next(iter2)) {
-				JabberCapsIdentity *id = iter2->data;
-				JabberCapsIdentity *newid = g_new0(JabberCapsIdentity, 1);
-				newid->category = g_strdup(id->category);
-				newid->type = g_strdup(id->type);
-				newid->name = g_strdup(id->name);
-
-				result->identities = g_list_append(result->identities,newid);
-			}
-			for(iter2 = extinfo->features; iter2; iter2 = g_list_next(iter2)) {
-				const char *feat = iter2->data;
-				char *newfeat = g_strdup(feat);
-
-				result->features = g_list_append(result->features,newfeat);
-			}
-		}
-	}
-	return result;
-}
-
-void jabber_caps_free_clientinfo(JabberCapsClientInfo *clientinfo) {
-	if(!clientinfo)
-		return;
-	while(clientinfo->identities) {
-		JabberCapsIdentity *id = clientinfo->identities->data;
-		g_free(id->category);
-		g_free(id->type);
-		g_free(id->name);
-		g_free(id);
-
-		clientinfo->identities = g_list_delete_link(clientinfo->identities,clientinfo->identities);
-	}
-	while(clientinfo->features) {
-		char *feat = clientinfo->features->data;
-		g_free(feat);
-
-		clientinfo->features = g_list_delete_link(clientinfo->features,clientinfo->features);
-	}
-
-	g_free(clientinfo);
+	g_hash_table_destroy(capstable);
+	g_hash_table_destroy(nodetable);
+	capstable = NULL;
 }
 
 typedef struct _jabber_caps_cbplususerdata {
+	guint ref;
+
 	jabber_caps_get_info_cb cb;
-	gpointer user_data;
+	gpointer cb_data;
 
 	char *who;
 	char *node;
 	char *ver;
-	GList *ext;
-	unsigned extOutstanding;
+	char *hash;
+
+	JabberCapsClientInfo *info;
+
+	GList *exts;
+	guint extOutstanding;
+	JabberCapsNodeExts *node_exts;
 } jabber_caps_cbplususerdata;
 
-typedef struct jabber_ext_userdata {
-	jabber_caps_cbplususerdata *userdata;
-	char *node;
-} jabber_ext_userdata;
+static jabber_caps_cbplususerdata*
+cbplususerdata_ref(jabber_caps_cbplususerdata *data)
+{
+	g_return_val_if_fail(data != NULL, NULL);
 
-static void jabber_caps_get_info_check_completion(jabber_caps_cbplususerdata *userdata) {
-	if(userdata->extOutstanding == 0) {
-		userdata->cb(jabber_caps_collect_info(userdata->node, userdata->ver, userdata->ext), userdata->user_data);
-		g_free(userdata->who);
-		g_free(userdata->node);
-		g_free(userdata->ver);
-		while(userdata->ext) {
-			g_free(userdata->ext->data);
-			userdata->ext = g_list_delete_link(userdata->ext,userdata->ext);
-		}
-		g_free(userdata);
-	}
+	++data->ref;
+	return data;
 }
 
-static void jabber_caps_ext_iqcb(JabberStream *js, xmlnode *packet, gpointer data) {
-	/* collect data and fetch all exts */
-	xmlnode *query = xmlnode_get_child_with_namespace(packet, "query", "http://jabber.org/protocol/disco#info");
-	jabber_ext_userdata *extuserdata = data;
-	jabber_caps_cbplususerdata *userdata = extuserdata->userdata;
-	const char *node = extuserdata->node;
-
-	--userdata->extOutstanding;
-
-	/* TODO: Better error handling */
+static void
+cbplususerdata_unref(jabber_caps_cbplususerdata *data)
+{
+	if (data == NULL)
+		return;
 
-	if(node && query) {
-		const char *key;
-		JabberCapsValue *client;
-		xmlnode *child;
-		JabberCapsValueExt *value = g_new0(JabberCapsValueExt, 1);
-		JabberCapsKey *clientkey = g_new0(JabberCapsKey, 1);
+	g_return_if_fail(data->ref != 0);
 
-		clientkey->node = userdata->node;
-		clientkey->ver = userdata->ver;
-
-		client = g_hash_table_lookup(capstable, clientkey);
-
-		g_free(clientkey);
+	if (--data->ref > 0)
+		return;
 
-		/* split node by #, key either points to \0 or the correct ext afterwards */
-		for(key = node; key[0] != '\0'; ++key) {
-			if(key[0] == '#') {
-				++key;
-				break;
-			}
-		}
+	g_free(data->who);
+	g_free(data->node);
+	g_free(data->ver);
+	g_free(data->hash);
 
-		for(child = query->child; child; child = child->next) {
-			if(child->type != XMLNODE_TYPE_TAG)
-				continue;
-			if(!strcmp(child->name,"feature")) {
-				const char *var = xmlnode_get_attrib(child, "var");
-				if(!var)
-					continue;
-				value->features = g_list_append(value->features,g_strdup(var));
-			} else if(!strcmp(child->name,"identity")) {
-				const char *category = xmlnode_get_attrib(child, "category");
-				const char *type = xmlnode_get_attrib(child, "type");
-				const char *name = xmlnode_get_attrib(child, "name");
-
-				JabberCapsIdentity *id = g_new0(JabberCapsIdentity, 1);
-				id->category = g_strdup(category);
-				id->type = g_strdup(type);
-				id->name = g_strdup(name);
-
-				value->identities = g_list_append(value->identities,id);
-			}
-		}
-		g_hash_table_replace(client->ext, g_strdup(key), value);
-
-		jabber_caps_store();
-	}
-
-	g_free(extuserdata->node);
-	g_free(extuserdata);
-	jabber_caps_get_info_check_completion(userdata);
+	/* If we have info here, it's already in the capstable, so don't free it */
+	if (data->exts)
+		free_string_glist(data->exts);
+	if (data->node_exts)
+		jabber_caps_node_exts_unref(data->node_exts);
+	g_free(data);
 }
 
-static void jabber_caps_client_iqcb(JabberStream *js, xmlnode *packet, gpointer data) {
-	/* collect data and fetch all exts */
+static void
+jabber_caps_get_info_complete(jabber_caps_cbplususerdata *userdata)
+{
+	userdata->cb(userdata->info, userdata->exts, userdata->cb_data);
+	userdata->info = NULL;
+	userdata->exts = NULL;
+
+	if (userdata->ref != 1)
+		purple_debug_warning("jabber", "Lost a reference to caps cbdata: %d\n",
+		                     userdata->ref);
+}
+
+static void
+jabber_caps_client_iqcb(JabberStream *js, xmlnode *packet, gpointer data)
+{
 	xmlnode *query = xmlnode_get_child_with_namespace(packet, "query",
 		"http://jabber.org/protocol/disco#info");
 	jabber_caps_cbplususerdata *userdata = data;
-
-	/* TODO: Better error checking! */
-
-	if (query) {
-		JabberCapsValue *value = g_new0(JabberCapsValue, 1);
-		JabberCapsKey *key = g_new0(JabberCapsKey, 1);
-		xmlnode *child;
-		GList *iter;
-
-		value->ext = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, jabber_caps_ext_destroy_value);
+	JabberCapsClientInfo *info = NULL, *value;
+	const char *type = xmlnode_get_attrib(packet, "type");
+	JabberCapsKey key;
 
-		key->node = g_strdup(userdata->node);
-		key->ver = g_strdup(userdata->ver);
+	if (!query || !strcmp(type, "error")) {
+		/* Any outstanding exts will be dealt with via ref-counting */
+		userdata->cb(NULL, NULL, userdata->cb_data);
+		cbplususerdata_unref(userdata);
+		return;
+	}
 
-		for(child = query->child; child; child = child->next) {
-			if(child->type != XMLNODE_TYPE_TAG)
-				continue;
-			if(!strcmp(child->name,"feature")) {
-				const char *var = xmlnode_get_attrib(child, "var");
-				if(!var)
-					continue;
-				value->features = g_list_append(value->features, g_strdup(var));
-			} else if(!strcmp(child->name,"identity")) {
-				const char *category = xmlnode_get_attrib(child, "category");
-				const char *type = xmlnode_get_attrib(child, "type");
-				const char *name = xmlnode_get_attrib(child, "name");
+	/* check hash */
+	info = jabber_caps_parse_client_info(query);
 
-				JabberCapsIdentity *id = g_new0(JabberCapsIdentity, 1);
-				id->category = g_strdup(category);
-				id->type = g_strdup(type);
-				id->name = g_strdup(name);
-
-				value->identities = g_list_append(value->identities,id);
-			}
+	/* Only validate if these are v1.5 capabilities */
+	if (userdata->hash) {
+		gchar *hash = NULL;
+		if (!strcmp(userdata->hash, "sha-1")) {
+			hash = jabber_caps_calculate_hash(info, "sha1");
+		} else if (!strcmp(userdata->hash, "md5")) {
+			hash = jabber_caps_calculate_hash(info, "md5");
 		}
-		g_hash_table_replace(capstable, key, value);
-		jabber_caps_store();
-
 
-		/* fetch all exts */
-		for(iter = userdata->ext; iter; iter = g_list_next(iter)) {
-			JabberIq *iq = jabber_iq_new_query(js, JABBER_IQ_GET,
-				"http://jabber.org/protocol/disco#info");
-			xmlnode *query = xmlnode_get_child_with_namespace(iq->node,
-				"query", "http://jabber.org/protocol/disco#info");
-			char *node = g_strdup_printf("%s#%s", userdata->node, (const char*)iter->data);
-			jabber_ext_userdata *ext_data = g_new0(jabber_ext_userdata, 1);
-			ext_data->node = node;
-			ext_data->userdata = userdata;
+		if (!hash || strcmp(hash, userdata->ver)) {
+			purple_debug_warning("jabber", "Could not validate caps info from %s\n",
+			                     xmlnode_get_attrib(packet, "from"));
 
-			xmlnode_set_attrib(query, "node", node);
-			xmlnode_set_attrib(iq->node, "to", userdata->who);
-
-			jabber_iq_set_callback(iq, jabber_caps_ext_iqcb, ext_data);
-			jabber_iq_send(iq);
+			userdata->cb(NULL, NULL, userdata->cb_data);
+			jabber_caps_client_info_destroy(info);
+			cbplususerdata_unref(userdata);
+			g_free(hash);
+			return;
 		}
 
-	} else
-		/* Don't wait for the ext discoveries; they aren't going to happen */
-		userdata->extOutstanding = 0;
+		g_free(hash);
+	}
+
+	if (!userdata->hash && userdata->node_exts) {
+		/* If the ClientInfo doesn't have information about the exts, give them
+		 * ours (along with our ref) */
+		info->exts = userdata->node_exts;
+		userdata->node_exts = NULL;
+	}
+
+	key.node = userdata->node;
+	key.ver  = userdata->ver;
+	key.hash = userdata->hash;
 
-	jabber_caps_get_info_check_completion(userdata);
+	/* Use the copy of this data already in the table if it exists or insert
+	 * a new one if we need to */
+	if ((value = g_hash_table_lookup(capstable, &key))) {
+		jabber_caps_client_info_destroy(info);
+		info = value;
+	} else {
+		JabberCapsKey *n_key = g_new(JabberCapsKey, 1);
+		n_key->node = userdata->node;
+		n_key->ver  = userdata->ver;
+		n_key->hash = userdata->hash;
+		userdata->node = userdata->ver = userdata->hash = NULL;
+
+		/* The capstable gets a reference */
+		g_hash_table_insert(capstable, n_key, info);
+		schedule_caps_save();
+	}
+
+	userdata->info = info;
+
+	if (userdata->extOutstanding == 0)
+		jabber_caps_get_info_complete(userdata);
+
+	cbplususerdata_unref(userdata);
 }
 
-void jabber_caps_get_info(JabberStream *js, const char *who, const char *node, const char *ver, const char *ext, jabber_caps_get_info_cb cb, gpointer user_data) {
-	JabberCapsValue *client;
-	JabberCapsKey *key = g_new0(JabberCapsKey, 1);
-	char *originalext = g_strdup(ext);
-	jabber_caps_cbplususerdata *userdata = g_new0(jabber_caps_cbplususerdata, 1);
+typedef struct {
+	const char *name;
+	jabber_caps_cbplususerdata *data;
+} ext_iq_data;
+
+static void
+jabber_caps_ext_iqcb(JabberStream *js, xmlnode *packet, gpointer data)
+{
+	xmlnode *query = xmlnode_get_child_with_namespace(packet, "query",
+		"http://jabber.org/protocol/disco#info");
+	xmlnode *child;
+	ext_iq_data *userdata = data;
+	const char *type = xmlnode_get_attrib(packet, "type");
+	GList *features = NULL;
+	JabberCapsNodeExts *node_exts;
+
+	if (!query || !strcmp(type, "error")) {
+		cbplususerdata_unref(userdata->data);
+		g_free(userdata);
+		return;
+	}
+
+	/* So, we decrement this after checking for an error, which means that
+	 * if there *is* an error, we'll never call the callback passed to
+	 * jabber_caps_get_info. We will still free all of our data, though.
+	 */
+	--userdata->data->extOutstanding;
+
+	for (child = xmlnode_get_child(query, "feature"); child;
+	        child = xmlnode_get_next_twin(child)) {
+		const char *var = xmlnode_get_attrib(child, "var");
+		if (var)
+			features = g_list_prepend(features, g_strdup(var));
+	}
+
+	node_exts = (userdata->data->info ? userdata->data->info->exts :
+	                                    userdata->data->node_exts);
+	g_hash_table_insert(node_exts->exts, g_strdup(userdata->name), features);
+	schedule_caps_save();
+
+	/* Are we done? */
+	if (userdata->data->info && userdata->data->extOutstanding == 0)
+		jabber_caps_get_info_complete(userdata->data);
+
+	cbplususerdata_unref(userdata->data);
+	g_free(userdata);
+}
+
+void jabber_caps_get_info(JabberStream *js, const char *who, const char *node,
+        const char *ver, const char *hash, const char *ext,
+        jabber_caps_get_info_cb cb, gpointer user_data)
+{
+	JabberCapsClientInfo *info;
+	JabberCapsKey key;
+	jabber_caps_cbplususerdata *userdata;
+
+	if (ext && *ext && hash)
+		purple_debug_info("jabber", "Ignoring exts in new-style caps from %s\n",
+		                     who);
+
+	/* Using this in a read-only fashion, so the cast is OK */
+	key.node = (char *)node;
+	key.ver = (char *)ver;
+	key.hash = (char *)hash;
+
+	info = g_hash_table_lookup(capstable, &key);
+	if (info && hash) {
+		/* v1.5 - We already have all the information we care about */
+		cb(info, NULL, user_data);
+		return;
+	}
+
+	userdata = g_new0(jabber_caps_cbplususerdata, 1);
+	/* This ref is given to fetching the basic node#ver info if we need it 
+	 * or unrefed at the bottom of this function */
+	cbplususerdata_ref(userdata);
 	userdata->cb = cb;
-	userdata->user_data = user_data;
+	userdata->cb_data = user_data;
 	userdata->who = g_strdup(who);
 	userdata->node = g_strdup(node);
 	userdata->ver = g_strdup(ver);
-
-	if(originalext) {
-		int i;
-		gchar **splat = g_strsplit(originalext, " ", 0);
-		for(i =0; splat[i]; i++) {
-			userdata->ext = g_list_append(userdata->ext, splat[i]);
-			++userdata->extOutstanding;
-		}
-		g_free(splat);
-	}
-	g_free(originalext);
+	userdata->hash = g_strdup(hash);
 
-	key->node = (char *)node;
-	key->ver = (char *)ver;
-
-	client = g_hash_table_lookup(capstable, key);
+	if (info) {
+		userdata->info = info;
+	} else {
+		/* If we don't have the basic information about the client, we need
+		 * to fetch it. */
+		JabberIq *iq;
+		xmlnode *query;
+		char *nodever;
 
-	g_free(key);
-
-	if(!client) {
-		JabberIq *iq = jabber_iq_new_query(js,JABBER_IQ_GET,"http://jabber.org/protocol/disco#info");
-		xmlnode *query = xmlnode_get_child_with_namespace(iq->node,"query","http://jabber.org/protocol/disco#info");
-		char *nodever = g_strdup_printf("%s#%s", node, ver);
+		iq = jabber_iq_new_query(js, JABBER_IQ_GET,
+					"http://jabber.org/protocol/disco#info");
+		query = xmlnode_get_child_with_namespace(iq->node, "query",
+					"http://jabber.org/protocol/disco#info");
+		nodever = g_strdup_printf("%s#%s", node, ver);
 		xmlnode_set_attrib(query, "node", nodever);
 		g_free(nodever);
 		xmlnode_set_attrib(iq->node, "to", who);
 
-		jabber_iq_set_callback(iq,jabber_caps_client_iqcb,userdata);
+		jabber_iq_set_callback(iq, jabber_caps_client_iqcb, userdata);
 		jabber_iq_send(iq);
-	} else {
-		GList *iter;
-		/* fetch unknown exts only */
-		for(iter = userdata->ext; iter; iter = g_list_next(iter)) {
-			JabberCapsValueExt *extvalue = g_hash_table_lookup(client->ext, (const char*)iter->data);
-			JabberIq *iq;
-			xmlnode *query;
-			char *nodever;
-			jabber_ext_userdata *ext_data;
+	}
+
+	/* Are there any exts that we don't recognize? */
+	if (ext && *ext && !hash) {
+		JabberCapsNodeExts *node_exts;
+		gchar **splat = g_strsplit(ext, " ", 0);
+		int i;
+
+		if (info) {
+			if (info->exts)
+				node_exts = info->exts;
+			else
+				node_exts = info->exts = jabber_caps_find_exts_by_node(node);
+		} else
+			/* We'll put it in later once we have the client info */
+			node_exts = userdata->node_exts = jabber_caps_find_exts_by_node(node);
 
-			if(extvalue) {
-				/* we already have this ext, don't bother with it */
-				--userdata->extOutstanding;
-				continue;
+		for (i = 0; splat[i]; ++i) {
+			userdata->exts = g_list_prepend(userdata->exts, splat[i]);
+			/* Look it up if we don't already know what it means */
+			if (!g_hash_table_lookup(node_exts->exts, splat[i])) {
+				JabberIq *iq;
+				xmlnode *query;
+				char *nodeext;
+				ext_iq_data *cbdata = g_new(ext_iq_data, 1);
+
+				cbdata->name = splat[i];
+				cbdata->data = cbplususerdata_ref(userdata);
+
+				iq = jabber_iq_new_query(js, JABBER_IQ_GET,
+				            "http://jabber.org/protocol/disco#info");
+				query = xmlnode_get_child_with_namespace(iq->node, "query",
+				            "http://jabber.org/protocol/disco#info");
+				nodeext = g_strdup_printf("%s#%s", node, splat[i]);
+				xmlnode_set_attrib(query, "node", nodeext);
+				g_free(nodeext);
+				xmlnode_set_attrib(iq->node, "to", who);
+
+				jabber_iq_set_callback(iq, jabber_caps_ext_iqcb, cbdata);
+				jabber_iq_send(iq);
+
+				++userdata->extOutstanding;	
 			}
-
-			ext_data = g_new0(jabber_ext_userdata, 1);
+			splat[i] = NULL;
+		}
+		/* All the strings are now part of the GList, so don't need
+		 * g_strfreev. */
+		g_free(splat);
+	}
 
-			iq = jabber_iq_new_query(js,JABBER_IQ_GET,"http://jabber.org/protocol/disco#info");
-			query = xmlnode_get_child_with_namespace(iq->node,"query","http://jabber.org/protocol/disco#info");
-			nodever = g_strdup_printf("%s#%s", node, (const char*)iter->data);
-			xmlnode_set_attrib(query, "node", nodever);
-			xmlnode_set_attrib(iq->node, "to", who);
+	if (userdata->info && userdata->extOutstanding == 0) {
+		jabber_caps_get_info_complete(userdata);
+		cbplususerdata_unref(userdata);
+	}
+}
 
-			ext_data->node = nodever;
-			ext_data->userdata = userdata;
+static gint
+jabber_identity_compare(gconstpointer a, gconstpointer b)
+{
+	const JabberIdentity *ac;
+	const JabberIdentity *bc;
+	gint cat_cmp;
+	gint typ_cmp;
+	
+	ac = a;
+	bc = b;
 
-			jabber_iq_set_callback(iq, jabber_caps_ext_iqcb, ext_data);
-			jabber_iq_send(iq);
+	if ((cat_cmp = strcmp(ac->category, bc->category)) == 0) {
+		if ((typ_cmp = strcmp(ac->type, bc->type)) == 0) {
+			if (!ac->lang && !bc->lang) {
+				return 0;
+			} else if (ac->lang && !bc->lang) {
+				return 1;
+			} else if (!ac->lang && bc->lang) {
+				return -1;
+			} else {
+				return strcmp(ac->lang, bc->lang);
+			}
+		} else {
+			return typ_cmp;
 		}
-		/* maybe we have all data available anyways? This is the ideal case where no network traffic is necessary */
-		jabber_caps_get_info_check_completion(userdata);
+	} else {
+		return cat_cmp;
 	}
 }
 
+static gchar *jabber_caps_get_formtype(const xmlnode *x) {
+	xmlnode *formtypefield;
+	formtypefield = xmlnode_get_child(x, "field");
+	while (formtypefield && strcmp(xmlnode_get_attrib(formtypefield, "var"), "FORM_TYPE")) formtypefield = xmlnode_get_next_twin(formtypefield);
+	formtypefield = xmlnode_get_child(formtypefield, "value");
+	return xmlnode_get_data(formtypefield);;
+}
+
+static gint
+jabber_xdata_compare(gconstpointer a, gconstpointer b)
+{
+	const xmlnode *aformtypefield = a;
+	const xmlnode *bformtypefield = b;
+	char *aformtype;
+	char *bformtype;
+	int result;
+
+	aformtype = jabber_caps_get_formtype(aformtypefield);
+	bformtype = jabber_caps_get_formtype(bformtypefield);
+	
+	result = strcmp(aformtype, bformtype);
+	g_free(aformtype);
+	g_free(bformtype);
+	return result;
+}
+
+static JabberCapsClientInfo *jabber_caps_parse_client_info(xmlnode *query)
+{
+	xmlnode *child;
+	JabberCapsClientInfo *info;
+
+	if (!query || strcmp(query->xmlns, "http://jabber.org/protocol/disco#info"))
+		return 0;
+	
+	info = g_new0(JabberCapsClientInfo, 1);
+
+	for(child = query->child; child; child = child->next) {
+		if (!strcmp(child->name,"identity")) {
+			/* parse identity */
+			const char *category = xmlnode_get_attrib(child, "category");
+			const char *type = xmlnode_get_attrib(child, "type");
+			const char *name = xmlnode_get_attrib(child, "name");
+			const char *lang = xmlnode_get_attrib(child, "lang");
+			JabberIdentity *id;
+
+			if (!category || !type)
+				continue;
+
+			id = g_new0(JabberIdentity, 1);
+			id->category = g_strdup(category);
+			id->type = g_strdup(type);
+			id->name = g_strdup(name);
+			id->lang = g_strdup(lang);
+
+			info->identities = g_list_append(info->identities, id);
+		} else if (!strcmp(child->name, "feature")) {
+			/* parse feature */
+			const char *var = xmlnode_get_attrib(child, "var");
+			if (var)
+				info->features = g_list_prepend(info->features, g_strdup(var));
+		} else if (!strcmp(child->name, "x")) {
+			if (child->xmlns && !strcmp(child->xmlns, "jabber:x:data")) {
+				/* x-data form */
+				xmlnode *dataform = xmlnode_copy(child);
+				info->forms = g_list_append(info->forms, dataform);
+			}
+		}
+	}
+	return info;
+}
+
+static gint jabber_caps_xdata_field_compare(gconstpointer a, gconstpointer b)
+{
+	const JabberDataFormField *ac = a;
+	const JabberDataFormField *bc = b;
+
+	return strcmp(ac->var, bc->var);
+}
+
+static GList* jabber_caps_xdata_get_fields(const xmlnode *x)
+{
+	GList *fields = NULL;
+	xmlnode *field;
+
+	if (!x)
+		return NULL;
+
+	for (field = xmlnode_get_child(x, "field"); field; field = xmlnode_get_next_twin(field)) {
+		xmlnode *value;
+		JabberDataFormField *xdatafield = g_new0(JabberDataFormField, 1);
+		xdatafield->var = g_strdup(xmlnode_get_attrib(field, "var"));
+
+		for (value = xmlnode_get_child(field, "value"); value; value = xmlnode_get_next_twin(value)) {
+			gchar *val = xmlnode_get_data(value);
+			xdatafield->values = g_list_append(xdatafield->values, val);
+		}
+
+		xdatafield->values = g_list_sort(xdatafield->values, (GCompareFunc)strcmp);
+		fields = g_list_append(fields, xdatafield);
+	}
+
+	fields = g_list_sort(fields, jabber_caps_xdata_field_compare);
+	return fields;
+}
+
+static GString*
+jabber_caps_verification_append(GString *verification, const gchar *string)
+{
+	verification = g_string_append(verification, string);
+	return g_string_append_c(verification, '<');
+}
+
+gchar *jabber_caps_calculate_hash(JabberCapsClientInfo *info, const char *hash)
+{
+	GList *node;
+	GString *verification;
+	PurpleCipherContext *context;
+	guint8 checksum[20];
+	gsize checksum_size = 20;
+	gboolean success;
+
+	if (!info || !(context = purple_cipher_context_new_by_name(hash, NULL)))
+		return NULL;
+
+	/* sort identities, features and x-data forms */
+	info->identities = g_list_sort(info->identities, jabber_identity_compare);
+	info->features = g_list_sort(info->features, (GCompareFunc)strcmp);
+	info->forms = g_list_sort(info->forms, jabber_xdata_compare);
+
+	verification = g_string_new("");
+
+	/* concat identities to the verification string */
+	for (node = info->identities; node; node = node->next) {
+		JabberIdentity *id = (JabberIdentity*)node->data;
+
+		g_string_append_printf(verification, "%s/%s/%s/%s<", id->category,
+		        id->type, id->lang ? id->lang : "", id->name);
+	}
+
+	/* concat features to the verification string */
+	for (node = info->features; node; node = node->next) {
+		verification = jabber_caps_verification_append(verification, node->data);
+	}
+
+	/* concat x-data forms to the verification string */
+	for(node = info->forms; node; node = node->next) {
+		xmlnode *data = (xmlnode *)node->data;
+		gchar *formtype = jabber_caps_get_formtype(data);
+		GList *fields = jabber_caps_xdata_get_fields(data);
+
+		/* append FORM_TYPE's field value to the verification string */
+		verification = jabber_caps_verification_append(verification, formtype);
+		g_free(formtype);
+
+		while (fields) {
+			GList *value;
+			JabberDataFormField *field = (JabberDataFormField*)fields->data; 
+
+			if (strcmp(field->var, "FORM_TYPE")) {
+				/* Append the "var" attribute */
+				verification = jabber_caps_verification_append(verification, field->var);
+				/* Append <value/> elements' cdata */
+				for(value = field->values; value; value = value->next) {
+					verification = jabber_caps_verification_append(verification, value->data);
+					g_free(value->data);
+				}
+			}
+
+			g_free(field->var);
+			g_list_free(field->values);
+
+			fields = g_list_delete_link(fields, fields);
+		}
+	}
+
+	/* generate hash */
+	purple_cipher_context_append(context, (guchar*)verification->str, verification->len);
+
+	success = purple_cipher_context_digest(context, verification->len,
+	                                       checksum, &checksum_size);
+
+	g_string_free(verification, TRUE);
+	purple_cipher_context_destroy(context);
+
+	return (success ? purple_base64_encode(checksum, checksum_size) : NULL);
+}
+
+void jabber_caps_calculate_own_hash(JabberStream *js) {
+	JabberCapsClientInfo info;
+	GList *iter = 0;
+	GList *features = 0;
+
+	if (!jabber_identities && !jabber_features) {
+		/* This really shouldn't ever happen */
+		purple_debug_warning("jabber", "No features or identities, cannot calculate own caps hash.\n");
+		g_free(js->caps_hash);
+		js->caps_hash = NULL;
+		return;
+	}
+
+	/* build the currently-supported list of features */
+	if (jabber_features) {
+		for (iter = jabber_features; iter; iter = iter->next) {
+			JabberFeature *feat = iter->data;
+			if(!feat->is_enabled || feat->is_enabled(js, feat->namespace)) {
+				features = g_list_append(features, feat->namespace);
+			}
+		}
+	}
+
+	info.features = features;
+	info.identities = jabber_identities;
+	info.forms = NULL;
+
+	g_free(js->caps_hash);
+	js->caps_hash = jabber_caps_calculate_hash(&info, "sha1");
+	g_list_free(features);
+}
+
+const gchar* jabber_caps_get_own_hash(JabberStream *js)
+{
+	if (!js->caps_hash)
+		jabber_caps_calculate_own_hash(js);
+
+	return js->caps_hash;
+}
+
+void jabber_caps_broadcast_change()
+{
+	GList *node, *accounts = purple_accounts_get_all_active();
+
+	for (node = accounts; node; node = node->next) {
+		PurpleAccount *account = node->data;
+		const char *prpl_id = purple_account_get_protocol_id(account);
+		if (!strcmp("prpl-jabber", prpl_id) && purple_account_is_connected(account)) {
+			PurpleConnection *gc = purple_account_get_connection(account);
+			jabber_presence_send(gc->proto_data, TRUE);
+		}
+	}
+
+	g_list_free(accounts);
+}
+
--- a/libpurple/protocols/jabber/caps.h	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/caps.h	Sat Apr 04 20:25:03 2009 +0000
@@ -26,24 +26,77 @@
 
 #include "jabber.h"
 
-/* Implementation of XEP-0115 */
+/* Implementation of XEP-0115 - Entity Capabilities */
 
-typedef struct _JabberCapsIdentity {
-	char *category;
-	char *type;
-	char *name;
-} JabberCapsIdentity;
+typedef struct _JabberCapsNodeExts JabberCapsNodeExts;
 
 struct _JabberCapsClientInfo {
-	GList *identities; /* JabberCapsIdentity */
+	GList *identities; /* JabberIdentity */
 	GList *features; /* char * */
+	GList *forms; /* xmlnode * */
+	JabberCapsNodeExts *exts;
+};
+
+/*
+ * This stores a set of exts "known" for a specific node (which indicates
+ * a specific client -- for reference, Pidgin, Finch, Meebo, et al share one
+ * node.) In XEP-0115 v1.3, exts are used for features that may or may not be
+ * present at a given time (PEP things, buzz might be disabled, etc).
+ *
+ * This structure is shared among all JabberCapsClientInfo instances matching
+ * a specific node (if the capstable key->hash == NULL, which indicates that
+ * the ClientInfo is using v1.3 caps as opposed to v1.5 caps).
+ *
+ * It's only exposed so that jabber_resource_has_capability can use it.
+ * Everyone else, STAY AWAY!
+ */
+struct _JabberCapsNodeExts {
+	guint ref;
+	GHashTable *exts; /* char *ext_name -> GList *features */
 };
 
-typedef void (*jabber_caps_get_info_cb)(JabberCapsClientInfo *info, gpointer user_data);
+typedef void (*jabber_caps_get_info_cb)(JabberCapsClientInfo *info, GList *exts, gpointer user_data);
 
 void jabber_caps_init(void);
+void jabber_caps_uninit(void);
 
-void jabber_caps_get_info(JabberStream *js, const char *who, const char *node, const char *ver, const char *ext, jabber_caps_get_info_cb cb, gpointer user_data);
-void jabber_caps_free_clientinfo(JabberCapsClientInfo *clientinfo);
+void jabber_caps_destroy_key(gpointer value);
+
+/**
+ * Main entity capabilites function to get the capabilities of a contact.
+ *
+ * The callback will be called synchronously if we already have the
+ * capabilities for the specified (node,ver,hash) (and, if exts are specified,
+ * if we know what each means)
+ */
+void jabber_caps_get_info(JabberStream *js, const char *who, const char *node,
+                          const char *ver, const char *hash,
+                          const char *ext, jabber_caps_get_info_cb cb,
+                          gpointer user_data);
+
+/**
+ *	Takes a JabberCapsClientInfo pointer and returns the caps hash according to
+ *	XEP-0115 Version 1.5.
+ *
+ *	@param info A JabberCapsClientInfo pointer.
+ *	@param hash Hash cipher to be used. Either sha-1 or md5.
+ *	@return		The base64 encoded SHA-1 hash; must be freed by caller
+ */
+gchar *jabber_caps_calculate_hash(JabberCapsClientInfo *info, const char *hash);
+
+/**
+ *  Calculate SHA1 hash for own featureset.
+ */
+void jabber_caps_calculate_own_hash(JabberStream *js);
+
+/** Get the current caps hash.
+ * 	@ret hash
+**/
+const gchar* jabber_caps_get_own_hash(JabberStream *js);
+
+/**
+ *  Broadcast a new calculated hash using a <presence> stanza.
+ */
+void jabber_caps_broadcast_change(void);
 
 #endif /* _PURPLE_JABBER_CAPS_H_ */
--- a/libpurple/protocols/jabber/data.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/data.c	Sat Apr 04 20:25:03 2009 +0000
@@ -244,4 +244,5 @@
 	g_hash_table_destroy(local_data_by_alt);
 	g_hash_table_destroy(local_data_by_cid);
 	g_hash_table_destroy(remote_data_by_cid);
+	local_data_by_alt = local_data_by_cid = remote_data_by_cid = NULL;
 }
--- a/libpurple/protocols/jabber/disco.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/disco.c	Sat Apr 04 20:25:03 2009 +0000
@@ -99,6 +99,10 @@
 
 		xmlnode *in_query;
 		const char *node = NULL;
+		char *node_uri = NULL;
+		
+		/* create custom caps node URI */
+		node_uri = g_strconcat(CAPS0115_NODE, "#", jabber_caps_get_own_hash(js), NULL);
 
 		if((in_query = xmlnode_get_child(packet, "query"))) {
 			node = xmlnode_get_attrib(in_query, "node");
@@ -123,91 +127,55 @@
 														 * handheld, pc, phone,
 														 * web */
 			xmlnode_set_attrib(identity, "name", PACKAGE);
+		}
 
-			SUPPORT_FEATURE("jabber:iq:last")
-			SUPPORT_FEATURE("jabber:iq:oob")
-			SUPPORT_FEATURE("jabber:iq:time")
-			SUPPORT_FEATURE("xmpp:urn:time")
-			SUPPORT_FEATURE("jabber:iq:version")
-			SUPPORT_FEATURE("jabber:x:conference")
-			SUPPORT_FEATURE("http://jabber.org/protocol/bytestreams")
-			SUPPORT_FEATURE("http://jabber.org/protocol/disco#info")
-			SUPPORT_FEATURE("http://jabber.org/protocol/disco#items")
-			SUPPORT_FEATURE("http://jabber.org/protocol/ibb");
-			SUPPORT_FEATURE("http://jabber.org/protocol/muc")
-			SUPPORT_FEATURE("http://jabber.org/protocol/muc#user")
-			SUPPORT_FEATURE("http://jabber.org/protocol/si")
-			SUPPORT_FEATURE("http://jabber.org/protocol/si/profile/file-transfer")
-			SUPPORT_FEATURE("http://jabber.org/protocol/xhtml-im")
-			SUPPORT_FEATURE("urn:xmpp:ping")
-			SUPPORT_FEATURE("http://www.xmpp.org/extensions/xep-0199.html#ns")
-
-			if(!node) { /* non-caps disco#info, add all enabled extensions */
-				GList *features;
-				for(features = jabber_features; features; features = features->next) {
-					JabberFeature *feat = (JabberFeature*)features->data;
-					if(feat->is_enabled == NULL || feat->is_enabled(js, feat->shortname, feat->namespace) == TRUE)
-						SUPPORT_FEATURE(feat->namespace);
-				}
+		if(!node || !strcmp(node, node_uri)) {
+			GList *features, *identities;
+			for(identities = jabber_identities; identities; identities = identities->next) {
+				JabberIdentity *ident = (JabberIdentity*)identities->data;
+				identity = xmlnode_new_child(query, "identity");
+				xmlnode_set_attrib(identity, "category", ident->category);
+				xmlnode_set_attrib(identity, "type", ident->type);
+				if (ident->lang)
+					xmlnode_set_attrib(identity, "xml:lang", ident->lang);
+				if (ident->name)
+					xmlnode_set_attrib(identity, "name", ident->name);
+			}
+			for(features = jabber_features; features; features = features->next) {
+				JabberFeature *feat = (JabberFeature*)features->data;
+				if (!feat->is_enabled || feat->is_enabled(js, feat->namespace)) {
+					feature = xmlnode_new_child(query, "feature");
+					xmlnode_set_attrib(feature, "var", feat->namespace);
+				}	
 			}
 #ifdef USE_VV
-		} else if (node && !strcmp(node, CAPS0115_NODE "#voice-v1")) {
-			SUPPORT_FEATURE("http://www.google.com/xmpp/protocol/session");
-			SUPPORT_FEATURE("http://www.google.com/xmpp/protocol/voice/v1");
-			SUPPORT_FEATURE(JINGLE);
-			SUPPORT_FEATURE(JINGLE_APP_RTP_SUPPORT_AUDIO);
-			SUPPORT_FEATURE(JINGLE_APP_RTP_SUPPORT_VIDEO);
-			SUPPORT_FEATURE(JINGLE_TRANSPORT_RAWUDP);
-			SUPPORT_FEATURE(JINGLE_TRANSPORT_ICEUDP);
+		} else if (g_str_equal(node, CAPS0115_NODE "#" "voice-v1")) {
+			/*
+			 * HUGE HACK! We advertise this ext (see jabber_presence_create_js
+			 * where we add <c/> to the <presence/>) for the Google Talk
+			 * clients that don't actually check disco#info features.
+			 *
+			 * This specific feature is redundant but is what
+			 * node='http://mail.google.com/xmpp/client/caps', ver='1.1'
+			 * advertises as 'voice-v1'.
+			 */
+			xmlnode *feature = xmlnode_new_child(query, "feature");
+			xmlnode_set_attrib(feature, "var", "http://www.google.com/xmpp/protocol/voice/v1");
 #endif
 		} else {
-			const char *ext = NULL;
-			unsigned pos;
-			unsigned nodelen = strlen(node);
-			unsigned capslen = strlen(CAPS0115_NODE);
-			/* do a basic plausability check */
-			if(nodelen > capslen+1) {
-				/* verify that the string is CAPS0115#<ext> and get the pointer to the ext part */
-				for(pos = 0; pos < capslen+1; ++pos) {
-					if(pos == capslen) {
-						if(node[pos] == '#')
-							ext = &node[pos+1];
-						else
-							break;
-					} else if(node[pos] != CAPS0115_NODE[pos])
-					break;
-				}
-
-				if(ext != NULL) {
-					/* look for that ext */
-					GList *features;
-					for(features = jabber_features; features; features = features->next) {
-						JabberFeature *feat = (JabberFeature*)features->data;
-						if(!strcmp(feat->shortname, ext)) {
-							SUPPORT_FEATURE(feat->namespace);
-							break;
-						}
-					}
-					if(features == NULL)
-						ext = NULL;
-				}
-			}
-
-			if(ext == NULL) {
-				xmlnode *error, *inf;
-
-				/* XXX: gross hack, implement jabber_iq_set_type or something */
-				xmlnode_set_attrib(iq->node, "type", "error");
-				iq->type = JABBER_IQ_ERROR;
-
-				error = xmlnode_new_child(query, "error");
-				xmlnode_set_attrib(error, "code", "404");
-				xmlnode_set_attrib(error, "type", "cancel");
-				inf = xmlnode_new_child(error, "item-not-found");
-				xmlnode_set_namespace(inf, "urn:ietf:params:xml:ns:xmpp-stanzas");
-			}
+			xmlnode *error, *inf;
+				
+			/* XXX: gross hack, implement jabber_iq_set_type or something */
+			xmlnode_set_attrib(iq->node, "type", "error");
+			iq->type = JABBER_IQ_ERROR;
+			
+			error = xmlnode_new_child(query, "error");
+			xmlnode_set_attrib(error, "code", "404");
+			xmlnode_set_attrib(error, "type", "cancel");
+			inf = xmlnode_new_child(error, "item-not-found");
+			xmlnode_set_namespace(inf, "urn:ietf:params:xml:ns:xmpp-stanzas");
 		}
-
+		g_free(node_uri);
 		jabber_iq_send(iq);
 	} else if(!strcmp(type, "result")) {
 		xmlnode *query = xmlnode_get_child(packet, "query");
@@ -360,9 +328,6 @@
 		jabber_roster_request(js);
 	}
 
-	/* Send initial presence; this will trigger receipt of presence for contacts on the roster */
-	jabber_presence_send(js->gc->account, NULL);
-
 	if (js->server_caps & JABBER_CAP_ADHOC) {
 		/* The server supports ad-hoc commands, so let's request the list */
 		jabber_adhoc_server_get_list(js);
--- a/libpurple/protocols/jabber/jabber.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/jabber.c	Sat Apr 04 20:25:03 2009 +0000
@@ -42,6 +42,7 @@
 
 #include "auth.h"
 #include "buddy.h"
+#include "caps.h"
 #include "chat.h"
 #include "data.h"
 #include "disco.h"
@@ -66,6 +67,7 @@
 
 static PurplePlugin *my_protocol = NULL;
 GList *jabber_features = NULL;
+GList *jabber_identities = NULL;
 
 static void jabber_unregister_account_cb(JabberStream *js);
 static void try_srv_connect(JabberStream *js);
@@ -91,7 +93,7 @@
 {
 	const char *type = xmlnode_get_attrib(packet, "type");
 	if(type && !strcmp(type, "result")) {
-		jabber_stream_set_state(js, JABBER_STREAM_CONNECTED);
+		jabber_disco_items_server(js);
 		if(js->unregistration)
 			jabber_unregister_account_cb(js);
 	} else {
@@ -182,13 +184,13 @@
 	return purple_strreplace(input, "__HOSTNAME__", hostname);
 }
 
-static void jabber_stream_features_parse(JabberStream *js, xmlnode *packet)
+void jabber_stream_features_parse(JabberStream *js, xmlnode *packet)
 {
 	if(xmlnode_get_child(packet, "starttls")) {
 		if(jabber_process_starttls(js, packet))
 
 			return;
-	} else if(purple_account_get_bool(js->gc->account, "require_tls", FALSE) && !js->gsc) {
+	} else if(purple_account_get_bool(js->gc->account, "require_tls", FALSE) && !jabber_stream_is_ssl(js)) {
 		purple_connection_error_reason (js->gc,
 			 PURPLE_CONNECTION_ERROR_ENCRYPTION_ERROR,
 			_("You require encryption, but it is not available on this server."));
@@ -381,7 +383,7 @@
 		}
 
 		purple_debug(PURPLE_DEBUG_MISC, "jabber", "Sending%s: %s%s%s\n",
-				js->gsc ? " (ssl)" : "", text ? text : data,
+				jabber_stream_is_ssl(js) ? " (ssl)" : "", text ? text : data,
 				last_part ? "password removed" : "",
 				last_part ? last_part : "");
 
@@ -422,7 +424,13 @@
 	}
 #endif
 
-	do_jabber_send_raw(js, data, len);
+	if (len == -1)
+		len = strlen(data);
+
+	if (js->use_bosh)
+		jabber_bosh_connection_send_raw(js->bosh, data);
+	else
+		do_jabber_send_raw(js, data, len);
 }
 
 int jabber_prpl_send_raw(PurpleConnection *gc, const char *buf, int len)
@@ -585,6 +593,41 @@
 	jabber_stream_set_state(js, JABBER_STREAM_INITIALIZING_ENCRYPTION);
 }
 
+static void 
+txt_resolved_cb(PurpleTxtResponse *resp, int results, gpointer data)
+{
+	JabberStream *js = data;
+	int n;
+	
+	js->srv_query_data = NULL;
+
+	if (results == 0) {
+		gchar *tmp;
+		tmp = g_strdup_printf(_("Could not find alternative XMPP connection methods after failing to connect directly.\n"));
+		purple_connection_error_reason (js->gc,
+				PURPLE_CONNECTION_ERROR_NETWORK_ERROR, tmp);
+		g_free(tmp);
+		return;	
+	}
+	
+	for (n = 0; n < results; n++) {
+		gchar **token;
+		token = g_strsplit(resp[n].content, "=", 2);
+		if (!strcmp(token[0], "_xmpp-client-xbosh")) {
+			purple_debug_info("jabber","Found alternative connection method using %s at %s.\n", token[0], token[1]);
+			js->bosh = jabber_bosh_connection_init(js, token[1]);
+			js->use_bosh = TRUE;
+			g_strfreev(token);
+			break;
+		}
+		g_strfreev(token);
+	}
+	if (js->bosh) {
+		jabber_bosh_connection_connect(js->bosh);
+	} else {
+		purple_debug_info("jabber","Didn't find an alternative connection method.\n");
+	}
+}
 
 static void
 jabber_login_callback(gpointer data, gint source, const gchar *error)
@@ -597,12 +640,8 @@
 			purple_debug_error("jabber", "Unable to connect to server: %s.  Trying next SRV record.\n", error);
 			try_srv_connect(js);
 		} else {
-			gchar *tmp;
-			tmp = g_strdup_printf(_("Could not establish a connection with the server:\n%s"),
-					      error);
-			purple_connection_error_reason(gc,
-					PURPLE_CONNECTION_ERROR_NETWORK_ERROR, tmp);
-			g_free(tmp);
+			purple_debug_info("jabber","Couldn't connect directly to %s. Trying to find alternative connection methods, like BOSH.\n", js->user->domain);
+			js->srv_query_data = purple_txt_resolve("_xmppconnect", js->user->domain, txt_resolved_cb, js);
 		}
 		return;
 	}
@@ -730,7 +769,8 @@
 	js->write_buffer = purple_circ_buffer_new(512);
 	js->old_length = 0;
 	js->keepalive_timeout = -1;
-	js->certificate_CN = g_strdup(connect_server[0] ? connect_server : js->user ? js->user->domain : NULL);
+	/* Set the default protocol version to 1.0. Overridden in parser.c. */
+	js->protocol_version = JABBER_PROTO_1_0;
 	js->sessions = NULL;
 	js->stun_ip = NULL;
 	js->stun_port = 0;
@@ -755,6 +795,22 @@
 
 	jabber_stream_set_state(js, JABBER_STREAM_CONNECTING);
 
+	/* TODO: Just use purple_url_parse? */
+	if (!g_ascii_strncasecmp(connect_server, "http://", 7) || !g_ascii_strncasecmp(connect_server, "https://", 8)) {
+		js->use_bosh = TRUE;
+		js->bosh = jabber_bosh_connection_init(js, connect_server);
+		if (!js->bosh) {
+			purple_connection_error_reason (js->gc,
+				PURPLE_CONNECTION_ERROR_INVALID_SETTINGS,
+				_("Malformed BOSH Connect Server"));
+			return;
+		}
+		jabber_bosh_connection_connect(js->bosh);
+		return;
+	} else {
+		js->certificate_CN = g_strdup(connect_server[0] ? connect_server : js->user->domain);
+	}
+
 	/* if they've got old-ssl mode going, we probably want to ignore SRV lookups */
 	if(purple_account_get_bool(js->gc->account, "old_ssl", FALSE)) {
 		if(purple_ssl_is_supported()) {
@@ -762,22 +818,27 @@
 					js->certificate_CN,
 					purple_account_get_int(account, "port", 5223), jabber_login_callback_ssl,
 					jabber_ssl_connect_failure, js->gc);
+			if (!js->gsc) {
+				purple_connection_error_reason (js->gc,
+					PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT,
+					_("Unable to establish SSL connection"));
+			}
 		} else {
 			purple_connection_error_reason (js->gc,
 				PURPLE_CONNECTION_ERROR_NO_SSL_SUPPORT,
 				_("SSL support unavailable"));
 		}
+
+		return;
 	}
 
 	/* no old-ssl, so if they've specified a connect server, we'll use that, otherwise we'll
 	 * invoke the magic of SRV lookups, to figure out host and port */
-	if(!js->gsc) {
-		if(connect_server[0]) {
-			jabber_login_connect(js, js->user->domain, connect_server, purple_account_get_int(account, "port", 5222), TRUE);
-		} else {
-			js->srv_query_data = purple_srv_resolve("xmpp-client",
-					"tcp", js->user->domain, srv_resolved_cb, js);
-		}
+	if(connect_server[0]) {
+		jabber_login_connect(js, js->user->domain, connect_server, purple_account_get_int(account, "port", 5222), TRUE);
+	} else {
+		js->srv_query_data = purple_srv_resolve("xmpp-client",
+				"tcp", js->user->domain, srv_resolved_cb, js);
 	}
 }
 
@@ -1337,8 +1398,12 @@
 	 * if we were forcibly disconnected because it will crash
 	 * on some SSL backends.
 	 */
-	if (!gc->disconnect_timeout)
-		jabber_send_raw(js, "</stream:stream>", -1);
+	if (!gc->disconnect_timeout) {
+		if (js->use_bosh)
+			jabber_bosh_connection_close(js->bosh);
+		else
+			jabber_send_raw(js, "</stream:stream>", -1);
+	}
 
 	if (js->srv_query_data)
 		purple_srv_cancel(js->srv_query_data);
@@ -1354,6 +1419,9 @@
 		close(js->fd);
 	}
 
+	if (js->bosh)
+		jabber_bosh_connection_destroy(js->bosh);
+
 	jabber_buddy_remove_all_pending_buddy_info_requests(js);
 
 	jabber_parser_free(js);
@@ -1395,6 +1463,7 @@
 	if(js->user)
 		jabber_id_free(js->user);
 	g_free(js->avatar_hash);
+	g_free(js->caps_hash);
 
 	purple_circ_buffer_destroy(js->write_buffer);
 	if(js->writeh)
@@ -1496,7 +1565,6 @@
 		case JABBER_STREAM_CONNECTED:
 			/* now we can alert the core that we're ready to send status */
 			purple_connection_set_state(js->gc, PURPLE_CONNECTED);
-			jabber_disco_items_server(js);
 			break;
 	}
 }
@@ -1614,31 +1682,27 @@
 	jabber_iq_send(iq);
 }
 
-void jabber_add_feature(const char *shortname, const char *namespace, JabberFeatureEnabled cb) {
+void jabber_add_feature(const char *namespace, JabberFeatureEnabled cb) {
 	JabberFeature *feat;
 
-	g_return_if_fail(shortname != NULL);
 	g_return_if_fail(namespace != NULL);
 
 	feat = g_new0(JabberFeature,1);
-	feat->shortname = g_strdup(shortname);
 	feat->namespace = g_strdup(namespace);
 	feat->is_enabled = cb;
 
 	/* try to remove just in case it already exists in the list */
-	jabber_remove_feature(shortname);
+	jabber_remove_feature(namespace);
 
 	jabber_features = g_list_append(jabber_features, feat);
 }
 
-void jabber_remove_feature(const char *shortname) {
+void jabber_remove_feature(const char *namespace) {
 	GList *feature;
 	for(feature = jabber_features; feature; feature = feature->next) {
 		JabberFeature *feat = (JabberFeature*)feature->data;
-		if(!strcmp(feat->shortname, shortname)) {
-			g_free(feat->shortname);
+		if(!strcmp(feat->namespace, namespace)) {
 			g_free(feat->namespace);
-
 			g_free(feature->data);
 			jabber_features = g_list_delete_link(jabber_features, feature);
 			break;
@@ -1646,6 +1710,59 @@
 	}
 }
 
+static void jabber_features_destroy(void)
+{
+	while (jabber_features) {
+		JabberFeature *feature = jabber_features->data;
+		g_free(feature->namespace);
+		g_free(feature);
+		jabber_features = g_list_remove_link(jabber_features, jabber_features);
+	}
+}
+
+void jabber_add_identity(const gchar *category, const gchar *type, const gchar *lang, const gchar *name) {
+	GList *identity;
+	JabberIdentity *ident;
+	/* both required according to XEP-0030 */
+	g_return_if_fail(category != NULL);
+	g_return_if_fail(type != NULL);
+	
+	for(identity = jabber_identities; identity; identity = identity->next) {
+		JabberIdentity *ident = (JabberIdentity*)identity->data;
+		if (!strcmp(ident->category, category) &&
+		    !strcmp(ident->type, type) &&
+		    ((!ident->lang && !lang) || (ident->lang && lang && !strcmp(ident->lang, lang)))) {
+			return;
+		}	
+	}
+
+	ident = g_new0(JabberIdentity, 1);
+	ident->category = g_strdup(category);
+	ident->type = g_strdup(type);
+	ident->lang = g_strdup(lang);
+	ident->name = g_strdup(name);
+	jabber_identities = g_list_append(jabber_identities, ident);
+}
+
+static void jabber_identities_destroy(void)
+{
+	while (jabber_identities) {
+		JabberIdentity *id = jabber_identities->data;
+		g_free(id->category);
+		g_free(id->type);
+		g_free(id->lang);
+		g_free(id->name);
+		g_free(id);
+		jabber_identities = g_list_remove_link(jabber_identities, jabber_identities);
+	}
+}
+
+gboolean jabber_stream_is_ssl(JabberStream *js)
+{
+	return (js->bosh && jabber_bosh_connection_is_ssl(js->bosh)) ||
+	       (!js->bosh && js->gsc);
+}
+
 const char *jabber_list_icon(PurpleAccount *a, PurpleBuddy *b)
 {
 	return "jabber";
@@ -2613,6 +2730,30 @@
 }
 
 #ifdef USE_VV
+gboolean
+jabber_audio_enabled(JabberStream *js, const char *namespace)
+{
+	PurpleMediaManager *manager = purple_media_manager_get();
+	PurpleMediaCaps caps = purple_media_manager_get_ui_caps(manager);
+
+	return (caps & (PURPLE_MEDIA_CAPS_AUDIO | PURPLE_MEDIA_CAPS_AUDIO_SINGLE_DIRECTION));
+}
+
+static gboolean
+jabber_video_enabled(JabberStream *js, const char *namespace)
+{
+	PurpleMediaManager *manager = purple_media_manager_get();
+	PurpleMediaCaps caps = purple_media_manager_get_ui_caps(manager);
+
+	return (caps & (PURPLE_MEDIA_CAPS_VIDEO | PURPLE_MEDIA_CAPS_VIDEO_SINGLE_DIRECTION));
+}
+
+static gboolean
+jabber_ice_transmitter_present(JabberStream *js, const char *namespace)
+{
+	return purple_media_transmitter_exists("nice");
+}
+
 typedef struct {
 	PurpleConnection *pc;
 	gchar *who;
@@ -2968,8 +3109,109 @@
 					  _("buzz: Buzz a user to get their attention"), NULL);
 }
 
+/* IPC functions */
+
+/**
+ * IPC function for determining if a contact supports a certain feature.
+ *
+ * @param account   The PurpleAccount
+ * @param jid       The full JID of the contact.
+ * @param feature   The feature's namespace.
+ *
+ * @return TRUE if supports feature; else FALSE.
+ */
+static gboolean
+jabber_ipc_contact_has_feature(PurpleAccount *account, const gchar *jid,
+                               const gchar *feature)
+{
+	PurpleConnection *gc = purple_account_get_connection(account);
+	JabberStream *js;
+	JabberBuddy *jb;
+	JabberBuddyResource *jbr;
+	gchar *resource;
+
+	if (!purple_account_is_connected(account))
+		return FALSE;
+	js = gc->proto_data;
+
+	if (!(resource = jabber_get_resource(jid)) || 
+	    !(jb = jabber_buddy_find(js, jid, FALSE)) ||
+	    !(jbr = jabber_buddy_find_resource(jb, resource))) {
+		g_free(resource);
+		return FALSE;
+	}
+
+	g_free(resource);
+
+	return jabber_resource_has_capability(jbr, feature);
+}
+
+static void
+jabber_ipc_add_feature(const gchar *feature)
+{
+	if (!feature)
+		return;
+	jabber_add_feature(feature, 0);
+
+	/* send presence with new caps info for all connected accounts */
+	jabber_caps_broadcast_change();
+}
+
 void
 jabber_init_plugin(PurplePlugin *plugin)
 {
-        my_protocol = plugin;
+	my_protocol = plugin;
+
+	jabber_add_identity("client", "pc", NULL, PACKAGE);
+
+	/* initialize jabber_features list */
+	jabber_add_feature("jabber:iq:last", 0);
+	jabber_add_feature("jabber:iq:oob", 0);
+	jabber_add_feature("jabber:iq:time", 0);
+	jabber_add_feature("urn:xmpp:time", 0);
+	jabber_add_feature("jabber:iq:version", 0);
+	jabber_add_feature("jabber:x:conference", 0);
+	jabber_add_feature("http://jabber.org/protocol/bytestreams", 0);
+	jabber_add_feature("http://jabber.org/protocol/caps", 0);
+	jabber_add_feature("http://jabber.org/protocol/chatstates", 0);
+	jabber_add_feature("http://jabber.org/protocol/disco#info", 0);
+	jabber_add_feature("http://jabber.org/protocol/disco#items", 0);
+	jabber_add_feature("http://jabber.org/protocol/ibb", 0);
+	jabber_add_feature("http://jabber.org/protocol/muc", 0);
+	jabber_add_feature("http://jabber.org/protocol/muc#user", 0);
+	jabber_add_feature("http://jabber.org/protocol/si", 0);
+	jabber_add_feature("http://jabber.org/protocol/si/profile/file-transfer", 0);
+	jabber_add_feature("http://jabber.org/protocol/xhtml-im", 0);
+	jabber_add_feature("urn:xmpp:ping", 0);
+
+	/* Jingle features! */
+	jabber_add_feature(JINGLE, 0);
+	jabber_add_feature(JINGLE_TRANSPORT_RAWUDP, 0);
+
+#ifdef USE_VV
+	jabber_add_feature("http://www.google.com/xmpp/protocol/session", jabber_audio_enabled);
+	jabber_add_feature("http://www.google.com/xmpp/protocol/voice/v1", jabber_audio_enabled);
+	jabber_add_feature(JINGLE_APP_RTP_SUPPORT_AUDIO, jabber_audio_enabled);
+	jabber_add_feature(JINGLE_APP_RTP_SUPPORT_VIDEO, jabber_video_enabled);
+	jabber_add_feature(JINGLE_TRANSPORT_ICEUDP, jabber_ice_transmitter_present);
+#endif
+
+	/* IPC functions */
+	purple_plugin_ipc_register(plugin, "contact_has_feature", PURPLE_CALLBACK(jabber_ipc_contact_has_feature),
+							 purple_marshal_BOOLEAN__POINTER_POINTER_POINTER,
+							 purple_value_new(PURPLE_TYPE_BOOLEAN), 3,
+							 purple_value_new(PURPLE_TYPE_SUBTYPE, PURPLE_SUBTYPE_ACCOUNT),
+							 purple_value_new(PURPLE_TYPE_STRING),
+							 purple_value_new(PURPLE_TYPE_STRING));
+	purple_plugin_ipc_register(plugin, "add_feature", PURPLE_CALLBACK(jabber_ipc_add_feature),
+							 purple_marshal_VOID__POINTER,
+							 NULL, 1,
+							 purple_value_new(PURPLE_TYPE_STRING));
 }
+
+void
+jabber_uninit_plugin(void)
+{
+	jabber_features_destroy();
+	jabber_identities_destroy();
+}
--- a/libpurple/protocols/jabber/jabber.h	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/jabber.h	Sat Apr 04 20:25:03 2009 +0000
@@ -63,12 +63,13 @@
 #include "jutil.h"
 #include "xmlnode.h"
 #include "buddy.h"
+#include "bosh.h"
 
 #ifdef HAVE_CYRUS_SASL
 #include <sasl/sasl.h>
 #endif
 
-#define CAPS0115_NODE "http://pidgin.im/caps"
+#define CAPS0115_NODE "http://pidgin.im/"
 
 /* Index into attention_types list */
 #define JABBER_BUZZ 0
@@ -209,6 +210,9 @@
 
 	gboolean vcard_fetched;
 
+	/* Entity Capabilities hash */
+	char *caps_hash;
+
 	/* does the local server support PEP? */
 	gboolean pep;
 
@@ -236,10 +240,15 @@
 
 	/* A purple timeout tag for the keepalive */
 	int keepalive_timeout;
-
+	
 	PurpleSrvResponse *srv_rec;
 	guint srv_rec_idx;
 	guint max_srv_rec_idx;
+
+	/* BOSH stuff */
+	gboolean use_bosh;
+	PurpleBOSHConnection *bosh;
+
 	/**
 	 * This linked list contains PurpleUtilFetchUrlData structs
 	 * for when we lookup buddy icons from a url
@@ -256,15 +265,22 @@
 	/* later add stuff to handle TURN relays... */
 };
 
-typedef gboolean (JabberFeatureEnabled)(JabberStream *js, const gchar *shortname, const gchar *namespace);
+typedef gboolean (JabberFeatureEnabled)(JabberStream *js, const gchar *namespace);
 
 typedef struct _JabberFeature
 {
-	gchar *shortname;
 	gchar *namespace;
 	JabberFeatureEnabled *is_enabled;
 } JabberFeature;
 
+typedef struct _JabberIdentity
+{
+	gchar *category;
+	gchar *type;
+	gchar *name;
+	gchar *lang;
+} JabberIdentity;
+
 typedef struct _JabberBytestreamsStreamhost {
 	char *jid;
 	char *host;
@@ -274,7 +290,9 @@
 
 /* what kind of additional features as returned from disco#info are supported? */
 extern GList *jabber_features;
+extern GList *jabber_identities;
 
+void jabber_stream_features_parse(JabberStream *js, xmlnode *packet);
 void jabber_process_packet(JabberStream *js, xmlnode **packet);
 void jabber_send(JabberStream *js, xmlnode *data);
 void jabber_send_raw(JabberStream *js, const char *data, int len);
@@ -295,8 +313,24 @@
  */
 char *jabber_parse_error(JabberStream *js, xmlnode *packet, PurpleConnectionError *reason);
 
-void jabber_add_feature(const gchar *shortname, const gchar *namespace, JabberFeatureEnabled cb); /* cb may be NULL */
-void jabber_remove_feature(const gchar *shortname);
+void jabber_add_feature(const gchar *namespace, JabberFeatureEnabled cb); /* cb may be NULL */
+void jabber_remove_feature(const gchar *namespace);
+
+/** Adds an identitiy to this jabber library instance. For list of valid values vistit the 
+ *	webiste of the XMPP Registrar ( http://www.xmpp.org/registrar/disco-categories.html#client ).
+ *  @param category the category of the identity.
+ *  @param type the type of the identity.
+ *  @param language the language localization of the name. Can be NULL.
+ *  @param name the name of the identity.
+ */
+void jabber_add_identity(const gchar *category, const gchar *type, const gchar *lang, const gchar *name);
+
+/**
+ * Returns true if this connection is over a secure (SSL) stream. Use this
+ * instead of checking js->gsc because BOSH stores its PurpleSslConnection
+ * members in its own data structure.
+ */
+gboolean jabber_stream_is_ssl(JabberStream *js);
 
 /** PRPL functions */
 const char *jabber_list_icon(PurpleAccount *a, PurpleBuddy *b);
@@ -321,10 +355,15 @@
 gboolean jabber_offline_message(const PurpleBuddy *buddy);
 int jabber_prpl_send_raw(PurpleConnection *gc, const char *buf, int len);
 GList *jabber_actions(PurplePlugin *plugin, gpointer context);
+
+gboolean jabber_audio_enabled(JabberStream *js, const char *unused);
 gboolean jabber_initiate_media(PurpleConnection *gc, const char *who,
 		PurpleMediaSessionType type);
 PurpleMediaCaps jabber_get_media_caps(PurpleConnection *gc, const char *who);
+
 void jabber_register_commands(void);
+
 void jabber_init_plugin(PurplePlugin *plugin);
+void jabber_uninit_plugin(void);
 
 #endif /* _PURPLE_JABBER_H_ */
--- a/libpurple/protocols/jabber/libxmpp.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/libxmpp.c	Sat Apr 04 20:25:03 2009 +0000
@@ -70,7 +70,7 @@
 	jabber_set_info,				/* set_info */
 	jabber_send_typing,				/* send_typing */
 	jabber_buddy_get_info,			/* get_info */
-	jabber_presence_send,			/* set_status */
+	jabber_set_status,				/* set_status */
 	jabber_idle_set,				/* set_idle */
 	NULL,							/* change_passwd */
 	jabber_roster_add_buddy,		/* add_buddy */
@@ -151,9 +151,18 @@
 
 	purple_signal_unregister(plugin, "jabber-sending-text");
 
+	/* reverse order of init_plugin */
+	jabber_bosh_uninit();
 	jabber_data_uninit();
 	jabber_si_uninit();
 	jabber_ibb_uninit();
+	/* PEP things should be uninit via jabber_pep_uninit, not here */
+	jabber_pep_uninit();
+	jabber_caps_uninit();
+	jabber_iq_uninit();
+
+	/* Stay on target...stay on target... Almost there... */
+	jabber_uninit_plugin();
 
 	return TRUE;
 }
@@ -207,24 +216,19 @@
 #endif
 	PurpleAccountUserSplit *split;
 	PurpleAccountOption *option;
-
 	/* Translators: 'domain' is used here in the context of Internet domains, e.g. pidgin.im */
 	split = purple_account_user_split_new(_("Domain"), NULL, '@');
 	purple_account_user_split_set_reverse(split, FALSE);
 	prpl_info.user_splits = g_list_append(prpl_info.user_splits, split);
-
 	split = purple_account_user_split_new(_("Resource"), NULL, '/');
 	purple_account_user_split_set_reverse(split, FALSE);
 	prpl_info.user_splits = g_list_append(prpl_info.user_splits, split);
-
 	option = purple_account_option_bool_new(_("Require SSL/TLS"), "require_tls", FALSE);
 	prpl_info.protocol_options = g_list_append(prpl_info.protocol_options,
 											   option);
-
 	option = purple_account_option_bool_new(_("Force old (port 5223) SSL"), "old_ssl", FALSE);
 	prpl_info.protocol_options = g_list_append(prpl_info.protocol_options,
 											   option);
-
 	option = purple_account_option_bool_new(
 						_("Allow plaintext auth over unencrypted streams"),
 						"auth_plain_in_clear", FALSE);
@@ -277,30 +281,27 @@
 #endif
 	jabber_register_commands();
 
+	/* reverse order of unload_plugin */
 	jabber_iq_init();
+	jabber_caps_init();
+	/* PEP things should be init via jabber_pep_init, not here */
 	jabber_pep_init();
+	jabber_data_init();
+	jabber_bosh_init();
 
-	jabber_tune_init();
-	jabber_caps_init();
-
-	jabber_data_init();
-
+	#warning implement adding and retrieving own features via IPC API
 
 	jabber_ibb_init();
 	jabber_si_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", XEP_0224_NAMESPACE,
+	jabber_add_feature(AVATARNAMESPACEMETA, jabber_pep_namespace_only_when_pep_enabled_cb);
+	jabber_add_feature(AVATARNAMESPACEDATA, jabber_pep_namespace_only_when_pep_enabled_cb);
+	jabber_add_feature("http://www.xmpp.org/extensions/xep-0224.html#ns",
 					   jabber_buzz_isenabled);
-	jabber_add_feature("bob", XEP_0231_NAMESPACE,
-					   jabber_custom_smileys_isenabled);
-	jabber_add_feature("ibb", XEP_0047_NAMESPACE, NULL);
+	jabber_add_feature(XEP_0231_NAMESPACE, jabber_custom_smileys_isenabled);
+	jabber_add_feature(XEP_0047_NAMESPACE, NULL);
 
-	jabber_pep_register_handler("avatar", AVATARNAMESPACEMETA, jabber_buddy_avatar_update_metadata);
-#ifdef USE_VV
-	jabber_add_feature("voice-v1", "http://www.xmpp.org/extensions/xep-0167.html#ns", NULL);
-#endif
+	jabber_pep_register_handler(AVATARNAMESPACEMETA, jabber_buddy_avatar_update_metadata);
 }
 
 
--- a/libpurple/protocols/jabber/message.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/message.c	Sat Apr 04 20:25:03 2009 +0000
@@ -1227,12 +1227,11 @@
 	jabber_message_free(jm);
 }
 
-gboolean jabber_buzz_isenabled(JabberStream *js, const gchar *shortname, const gchar *namespace) {
+gboolean jabber_buzz_isenabled(JabberStream *js, const gchar *namespace) {
 	return js->allowBuzz;
 }
 
-gboolean jabber_custom_smileys_isenabled(JabberStream *js, const gchar *shortname,
-										 const gchar *namespace)
+gboolean jabber_custom_smileys_isenabled(JabberStream *js, const gchar *namespace)
 {
 	const PurpleConnection *gc = js->gc;
 	PurpleAccount *account = purple_connection_get_account(gc);
--- a/libpurple/protocols/jabber/message.h	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/message.h	Sat Apr 04 20:25:03 2009 +0000
@@ -80,9 +80,8 @@
 unsigned int jabber_send_typing(PurpleConnection *gc, const char *who, PurpleTypingState state);
 void jabber_message_conv_closed(JabberStream *js, const char *who);
 
-gboolean jabber_buzz_isenabled(JabberStream *js, const gchar *shortname, const gchar *namespace);
+gboolean jabber_buzz_isenabled(JabberStream *js, const gchar *namespace);
 
-gboolean jabber_custom_smileys_isenabled(JabberStream *js, const gchar *shortname,
-										 const gchar *namespace);
+gboolean jabber_custom_smileys_isenabled(JabberStream *js, const const gchar *namespace);
 
 #endif /* _PURPLE_JABBER_MESSAGE_H_ */
--- a/libpurple/protocols/jabber/pep.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/pep.c	Sat Apr 04 20:25:03 2009 +0000
@@ -26,6 +26,7 @@
 #include <string.h>
 #include "usermood.h"
 #include "usernick.h"
+#include "usertune.h"
 
 static GHashTable *pep_handlers = NULL;
 
@@ -35,19 +36,26 @@
 
 		/* register PEP handlers */
 		jabber_mood_init();
+		jabber_tune_init();
 		jabber_nick_init();
 	}
 }
 
+void jabber_pep_uninit(void) {
+	/* any PEP handlers that need to clean things up go here */
+	g_hash_table_destroy(pep_handlers);
+	pep_handlers = NULL;
+}
+
 void jabber_pep_init_actions(GList **m) {
 	/* register the PEP-specific actions */
 	jabber_mood_init_action(m);
 	jabber_nick_init_action(m);
 }
 
-void jabber_pep_register_handler(const char *shortname, const char *xmlns, JabberPEPHandler handlerfunc) {
+void jabber_pep_register_handler(const char *xmlns, JabberPEPHandler handlerfunc) {
 	gchar *notifyns = g_strdup_printf("%s+notify", xmlns);
-	jabber_add_feature(shortname, notifyns, NULL); /* receiving PEPs is always supported */
+	jabber_add_feature(notifyns, NULL); /* receiving PEPs is always supported */
 	g_free(notifyns);
 	g_hash_table_replace(pep_handlers, g_strdup(xmlns), handlerfunc);
 }
@@ -85,7 +93,7 @@
 	jabber_iq_send(iq);
 }
 
-gboolean jabber_pep_namespace_only_when_pep_enabled_cb(JabberStream *js, const gchar *shortname, const gchar *namespace) {
+gboolean jabber_pep_namespace_only_when_pep_enabled_cb(JabberStream *js, const gchar *namespace) {
 	return js->pep;
 }
 
--- a/libpurple/protocols/jabber/pep.h	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/pep.h	Sat Apr 04 20:25:03 2009 +0000
@@ -27,6 +27,7 @@
 #include "buddy.h"
 
 void jabber_pep_init(void);
+void jabber_pep_uninit(void);
 
 void jabber_pep_init_actions(GList **m);
 
@@ -42,11 +43,10 @@
  * Registers a callback for PEP events. Also automatically announces this receiving capability via disco#info.
  * Don't forget to use jabber_add_feature when supporting the sending of PEP events of this type.
  *
- * @parameter shortname		A short name for this feature for XEP-0115. It has no semantic meaning, it just has to be unique.
- * @parameter xmlns		The namespace for this event
+ * @parameter xmlns			The namespace for this event
  * @parameter handlerfunc	The callback to be used when receiving an event with this namespace
  */
-void jabber_pep_register_handler(const char *shortname, const char *xmlns, JabberPEPHandler handlerfunc);
+void jabber_pep_register_handler(const char *xmlns, JabberPEPHandler handlerfunc);
 
 /*
  * Request a specific item from another PEP node.
@@ -64,13 +64,12 @@
 /*
  * Default callback that can be used for namespaces which should only be enabled when PEP is supported
  *
- * @parameter js	The JabberStream struct for this connection
- * @parameter shortname	The namespace's shortname (for caps), ignored.
+ * @parameter js		The JabberStream struct for this connection
  * @parameter namespace The namespace that's queried, ignored.
  *
  * @returns TRUE when PEP is enabled, FALSE otherwise
  */
-gboolean jabber_pep_namespace_only_when_pep_enabled_cb(JabberStream *js, const gchar *shortname, const gchar *namespace);
+gboolean jabber_pep_namespace_only_when_pep_enabled_cb(JabberStream *js, const gchar *namespace);
 
 void jabber_handle_event(JabberMessage *jm);
 
--- a/libpurple/protocols/jabber/presence.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/presence.c	Sat Apr 04 20:25:03 2009 +0000
@@ -93,11 +93,31 @@
 	g_free(my_base_jid);
 }
 
-
-void jabber_presence_send(PurpleAccount *account, PurpleStatus *status)
+void jabber_set_status(PurpleAccount *account, PurpleStatus *status)
 {
-	PurpleConnection *gc = NULL;
-	JabberStream *js = NULL;
+	PurpleConnection *gc;
+	JabberStream *js;
+
+	if (!purple_account_is_connected(account))
+		return;
+	
+	if (!purple_status_is_active(status))
+		return;
+
+	if (purple_status_is_exclusive(status) && !purple_status_is_active(status)) {
+		/* An exclusive status can't be deactivated. You should just
+		 * activate some other exclusive status. */
+		return;
+	}
+
+	gc = purple_account_get_connection(account);
+	js = purple_connection_get_protocol_data(gc);
+	jabber_presence_send(js, FALSE);
+}
+
+void jabber_presence_send(JabberStream *js, gboolean force)
+{
+	PurpleAccount *account;
 	xmlnode *presence, *x, *photo;
 	char *stripped = NULL;
 	JabberBuddyState state;
@@ -106,28 +126,11 @@
 	int length = -1;
 	gboolean allowBuzz;
 	PurplePresence *p;
-	PurpleStatus *tune;
-
-	if (purple_account_is_disconnected(account))
-		return;
-
-	p = purple_account_get_presence(account);
-	if (NULL == status) {
-		status = purple_presence_get_active_status(p);
-	}
+	PurpleStatus *status, *tune;
 
-	if (purple_status_is_exclusive(status)) {
-		/* An exclusive status can't be deactivated. You should just
-		 * activate some other exclusive status. */
-		if (!purple_status_is_active(status))
-			return;
-	} else {
-		/* Work with the exclusive status. */
-		status = purple_presence_get_active_status(p);
-	}
-
-	gc = purple_account_get_connection(account);
-	js = gc->proto_data;
+	account = purple_connection_get_account(js->gc);
+	p = purple_account_get_presence(account);
+	status = purple_presence_get_active_status(p);
 
 	/* we don't want to send presence before we've gotten our roster */
 	if(!js->roster_parsed) {
@@ -141,16 +144,18 @@
 	allowBuzz = purple_status_get_attr_boolean(status,"buzz");
 	/* changing the buzz state has to trigger a re-broadcasting of the presence for caps */
 
-	if (js->googletalk && stripped == NULL && purple_presence_is_status_primitive_active(p, PURPLE_STATUS_TUNE)) {
-		tune = purple_presence_get_status(p, "tune");
+	tune = purple_presence_get_status(p, "tune");
+	if (js->googletalk && !stripped && purple_status_is_active(tune)) {
 		stripped = jabber_google_presence_outgoing(tune);
 	}
 
 #define CHANGED(a,b) ((!a && b) || (a && a[0] == '\0' && b && b[0] != '\0') || \
 					  (a && !b) || (a && a[0] != '\0' && b && b[0] == '\0') || (a && b && strcmp(a,b)))
 	/* check if there are any differences to the <presence> and send them in that case */
-	if (allowBuzz != js->allowBuzz || js->old_state != state || CHANGED(js->old_msg, stripped) ||
-		js->old_priority != priority || CHANGED(js->old_avatarhash, js->avatar_hash)) {
+	if (force || allowBuzz != js->allowBuzz || js->old_state != state ||
+		CHANGED(js->old_msg, stripped) || js->old_priority != priority ||
+		CHANGED(js->old_avatarhash, js->avatar_hash)) {
+		/* Need to update allowBuzz before creating the presence (with caps) */
 		js->allowBuzz = allowBuzz;
 
 		presence = jabber_presence_create_js(js, state, stripped, priority);
@@ -181,8 +186,7 @@
 	g_free(stripped);
 
 	/* next, check if there are any changes to the tune values */
-	tune = purple_presence_get_status(p, "tune");
-	if (tune && purple_status_is_active(tune)) {
+	if (purple_status_is_active(tune)) {
 		artist = purple_status_get_attr_string(tune, PURPLE_TUNE_ARTIST);
 		title = purple_status_get_attr_string(tune, PURPLE_TUNE_TITLE);
 		source = purple_status_get_attr_string(tune, PURPLE_TUNE_ALBUM);
@@ -260,46 +264,28 @@
 	}
 
 	/* JEP-0115 */
+	/* calculate hash */
+	jabber_caps_calculate_own_hash(js);
+	/* create xml */
 	c = xmlnode_new_child(presence, "c");
 	xmlnode_set_namespace(c, "http://jabber.org/protocol/caps");
 	xmlnode_set_attrib(c, "node", CAPS0115_NODE);
-	xmlnode_set_attrib(c, "ver", VERSION);
-#ifdef USE_VV
-	/* Make sure this is 'voice-v1', or you won't be able to talk to Google Talk */
-	xmlnode_set_attrib(c, "ext", "voice-v1");
-#endif
-
-	if(js != NULL) {
-		/* add the extensions */
-		char extlist[1024];
-		unsigned remaining = 1023; /* one less for the \0 */
-		GList *feature;
-
-		extlist[0] = '\0';
-		for(feature = jabber_features; feature && remaining > 0; feature = feature->next) {
-			JabberFeature *feat = (JabberFeature*)feature->data;
-			unsigned featlen;
+	xmlnode_set_attrib(c, "hash", "sha-1");
+	xmlnode_set_attrib(c, "ver", jabber_caps_get_own_hash(js));
 
-			if(feat->is_enabled != NULL && feat->is_enabled(js, feat->shortname, feat->namespace) == FALSE)
-				continue; /* skip this feature */
-
-			featlen = strlen(feat->shortname);
-
-			/* cut off when we don't have any more space left in our buffer (too bad) */
-			if(featlen > remaining)
-				break;
-
-			strncat(extlist,feat->shortname,remaining);
-			remaining -= featlen;
-			if(feature->next) { /* no space at the end */
-				strncat(extlist," ",remaining);
-				--remaining;
-			}
-		}
-		/* did we add anything? */
-		if(remaining < 1023)
-			xmlnode_set_attrib(c, "ext", extlist);
-	}
+#ifdef USE_VV
+	/*
+	 * MASSIVE HUGE DISGUSTING HACK
+	 * This is a huge hack. As far as I can tell, Google Talk's gmail client
+	 * doesn't bother to check the actual features we advertise; they
+	 * just assume that if we advertise a 'voice-v1' ext (ignoring that
+	 * these are to be assigned no semantic value). We need to advertise
+	 * this for the gmail web interface chat client to allow the mail user
+	 * to start a voice chat with us. Boo.
+	 */
+	if (jabber_audio_enabled(js, NULL /* unused */))
+		xmlnode_set_attrib(c, "ext", "voice-v1");
+#endif
 
 	return presence;
 }
@@ -369,39 +355,41 @@
 	char *from;
 } JabberPresenceCapabilities;
 
-static void jabber_presence_set_capabilities(JabberCapsClientInfo *info, gpointer user_data) {
-	JabberPresenceCapabilities *userdata = user_data;
-	JabberID *jid;
+static void
+jabber_presence_set_capabilities(JabberCapsClientInfo *info, GList *exts,
+                                 JabberPresenceCapabilities *userdata)
+{
 	JabberBuddyResource *jbr;
-	GList *iter;
+	char *resource = g_utf8_strrchr(userdata->from, -1, '/');
+	resource += 1;
 
-	jid = jabber_id_new(userdata->from);
-	jbr = jabber_buddy_find_resource(userdata->jb, jid->resource);
-	jabber_id_free(jid);
-
-	if(!jbr) {
+	jbr = jabber_buddy_find_resource(userdata->jb, resource);
+	if (!jbr) {
 		g_free(userdata->from);
 		g_free(userdata);
+		if (exts) {
+			g_list_foreach(exts, (GFunc)g_free, NULL);
+			g_list_free(exts);
+		}
 		return;
 	}
 
-	if(jbr->caps)
-		jabber_caps_free_clientinfo(jbr->caps);
-	jbr->caps = info;
+	/* Any old jbr->caps.info is owned by the caps code */
+	if (jbr->caps.exts) {
+		g_list_foreach(jbr->caps.exts, (GFunc)g_free, NULL);
+		g_list_free(jbr->caps.exts);
+	}
 
-	if (info) {
-		for(iter = info->features; iter; iter = g_list_next(iter)) {
-			if(!strcmp((const char*)iter->data, "http://jabber.org/protocol/commands")) {
-				JabberIq *iq = jabber_iq_new_query(userdata->js, JABBER_IQ_GET, "http://jabber.org/protocol/disco#items");
-				xmlnode *query = xmlnode_get_child_with_namespace(iq->node,"query","http://jabber.org/protocol/disco#items");
-				xmlnode_set_attrib(iq->node, "to", userdata->from);
-				xmlnode_set_attrib(query, "node", "http://jabber.org/protocol/commands");
+	jbr->caps.info = info;
+	jbr->caps.exts = exts;
 
-				jabber_iq_set_callback(iq, jabber_adhoc_disco_result_cb, NULL);
-				jabber_iq_send(iq);
-				break;
-			}
-		}
+	if (jabber_resource_has_capability(jbr, "http://jabber.org/protocol/commands")) {
+		JabberIq *iq = jabber_iq_new_query(userdata->js, JABBER_IQ_GET, "http://jabber.org/protocol/disco#items");
+		xmlnode *query = xmlnode_get_child_with_namespace(iq->node, "query", "http://jabber.org/protocol/disco#items");
+		xmlnode_set_attrib(iq->node, "to", userdata->from);
+		xmlnode_set_attrib(query, "node", "http://jabber.org/protocol/commands");
+		jabber_iq_set_callback(iq, jabber_adhoc_disco_result_cb, NULL);
+		jabber_iq_send(iq);
 	}
 
 	g_free(userdata->from);
@@ -746,16 +734,22 @@
 			jbr = jabber_buddy_track_resource(jb, jid->resource, priority,
 					state, status);
 			if(caps) {
+				/* handle XEP-0115 */
 				const char *node = xmlnode_get_attrib(caps,"node");
 				const char *ver = xmlnode_get_attrib(caps,"ver");
+				const char *hash = xmlnode_get_attrib(caps,"hash");
 				const char *ext = xmlnode_get_attrib(caps,"ext");
 
-				if(node && ver) {
+				/* v1.3 uses: node, ver, and optionally ext.
+				 * v1.5 uses: node, ver, and hash. */
+				if (node && ver) {
 					JabberPresenceCapabilities *userdata = g_new0(JabberPresenceCapabilities, 1);
 					userdata->js = js;
 					userdata->jb = jb;
 					userdata->from = g_strdup(from);
-					jabber_caps_get_info(js, from, node, ver, ext, jabber_presence_set_capabilities, userdata);
+					jabber_caps_get_info(js, from, node, ver, hash, ext,
+					    (jabber_caps_get_info_cb)jabber_presence_set_capabilities,
+					    userdata);
 				}
 			}
 		}
--- a/libpurple/protocols/jabber/presence.h	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/presence.h	Sat Apr 04 20:25:03 2009 +0000
@@ -26,7 +26,17 @@
 #include "jabber.h"
 #include "xmlnode.h"
 
-void jabber_presence_send(PurpleAccount *account, PurpleStatus *status);
+void jabber_set_status(PurpleAccount *account, PurpleStatus *status);
+
+/**
+ *	Send a full presence stanza.
+ *
+ *	@param js       A JabberStream object.
+ *	@param force    Force sending the presence stanza, irrespective of whether
+ *	                the contents seem to have changed.
+ */
+void jabber_presence_send(JabberStream *js, gboolean force);
+
 xmlnode *jabber_presence_create(JabberBuddyState state, const char *msg, int priority); /* DEPRECATED */
 xmlnode *jabber_presence_create_js(JabberStream *js, JabberBuddyState state, const char *msg, int priority);
 void jabber_presence_parse(JabberStream *js, xmlnode *packet);
--- a/libpurple/protocols/jabber/roster.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/roster.c	Sat Apr 04 20:25:03 2009 +0000
@@ -260,11 +260,12 @@
 	js->currently_parsing_roster_push = FALSE;
 
 	/* if we're just now parsing the roster for the first time,
-	 * then now would be the time to send our initial presence */
+	 * then now would be the time to declare ourselves connected and
+	 * send our initial presence */
 	if(!js->roster_parsed) {
 		js->roster_parsed = TRUE;
-
-		jabber_presence_send(js->gc->account, NULL);
+		jabber_presence_send(js, TRUE);
+		jabber_stream_set_state(js, JABBER_STREAM_CONNECTED);
 	}
 }
 
--- a/libpurple/protocols/jabber/usermood.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/usermood.c	Sat Apr 04 20:25:03 2009 +0000
@@ -141,8 +141,8 @@
 }
 
 void jabber_mood_init(void) {
-	jabber_add_feature("mood", "http://jabber.org/protocol/mood", jabber_pep_namespace_only_when_pep_enabled_cb);
-	jabber_pep_register_handler("moodn", "http://jabber.org/protocol/mood", jabber_mood_cb);
+	jabber_add_feature("http://jabber.org/protocol/mood", jabber_pep_namespace_only_when_pep_enabled_cb);
+	jabber_pep_register_handler("http://jabber.org/protocol/mood", jabber_mood_cb);
 }
 
 static void do_mood_set_from_fields(PurpleConnection *gc, PurpleRequestFields *fields) {
--- a/libpurple/protocols/jabber/usernick.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/usernick.c	Sat Apr 04 20:25:03 2009 +0000
@@ -92,8 +92,8 @@
 }
 
 void jabber_nick_init(void) {
-	jabber_add_feature("nick", "http://jabber.org/protocol/nick", jabber_pep_namespace_only_when_pep_enabled_cb);
-	jabber_pep_register_handler("nickn", "http://jabber.org/protocol/nick", jabber_nick_cb);
+	jabber_add_feature("http://jabber.org/protocol/nick", jabber_pep_namespace_only_when_pep_enabled_cb);
+	jabber_pep_register_handler("http://jabber.org/protocol/nick", jabber_nick_cb);
 }
 
 void jabber_nick_init_action(GList **m) {
--- a/libpurple/protocols/jabber/usertune.c	Sat Apr 04 07:39:33 2009 +0000
+++ b/libpurple/protocols/jabber/usertune.c	Sat Apr 04 20:25:03 2009 +0000
@@ -109,8 +109,8 @@
 }
 
 void jabber_tune_init(void) {
-	jabber_add_feature("tune", "http://jabber.org/protocol/tune", jabber_pep_namespace_only_when_pep_enabled_cb);
-	jabber_pep_register_handler("tunen", "http://jabber.org/protocol/tune", jabber_tune_cb);
+	jabber_add_feature("http://jabber.org/protocol/tune", jabber_pep_namespace_only_when_pep_enabled_cb);
+	jabber_pep_register_handler("http://jabber.org/protocol/tune", jabber_tune_cb);
 }
 
 void jabber_tune_set(PurpleConnection *gc, const PurpleJabberTuneInfo *tuneinfo) {