changeset 25772:c4eb9f10ecb5

propagate from branch 'im.pidgin.pidgin' (head 11ea1799b1892059650fa7dcf6e848a020045e50) to branch 'org.darkrain42.pidgin.xmpp' (head 5b18884cb593c048171bfce406c09d70ddf03f02)
author Paul Aurich <paul@darkrain42.org>
date Sat, 29 Nov 2008 18:41:39 +0000
parents 5cd0188892f6 (current diff) e1f363f8fd6b (diff)
children f47429b5ab52
files libpurple/protocols/jabber/buddy.c libpurple/protocols/jabber/jabber.c
diffstat 25 files changed, 1810 insertions(+), 505 deletions(-) [+]
line wrap: on
line diff
--- a/libpurple/dnssrv.c	Sat Nov 29 18:20:58 2008 +0000
+++ b/libpurple/dnssrv.c	Sat Nov 29 18:41:39 2008 +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;
 		}
@@ -175,10 +193,12 @@
 
 end:
 	size = g_list_length(ret);
-	write(out, &size, sizeof(int));
+	write(out, &(query.type), sizeof(query.type));
+	write(out, &size, sizeof(size));
 	while (ret != NULL)
 	{
-		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);
 	}
@@ -193,39 +213,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);
 }
 
@@ -237,34 +284,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;
 
@@ -277,40 +347,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 */
@@ -328,6 +408,7 @@
 {
 	char *query;
 	PurpleSrvQueryData *query_data;
+	PurpleSrvInternalQuery internal_query;
 #ifndef _WIN32
 	int in[2], out[2];
 	int pid;
@@ -369,11 +450,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];
@@ -392,7 +478,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;
 
@@ -416,6 +503,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)
 {
@@ -429,7 +614,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);
@@ -440,3 +625,9 @@
 #endif
 	g_free(query_data);
 }
+
+void
+purple_txt_cancel(PurpleSrvQueryData *query_data)
+{
+	purple_srv_cancel(query_data);
+}
--- a/libpurple/dnssrv.h	Sat Nov 29 18:20:58 2008 +0000
+++ b/libpurple/dnssrv.h	Sat Nov 29 18:41:39 2008 +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/protocols/jabber/Makefile.am	Sat Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/Makefile.am	Sat Nov 29 18:41:39 2008 +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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/Makefile.mingw	Sat Nov 29 18:41:39 2008 +0000
@@ -46,6 +46,7 @@
 			adhoccommands.c \
 			auth.c \
 			buddy.c \
+			bosh.c
 			caps.c \
 			chat.c \
 			data.c \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libpurple/protocols/jabber/bosh.c	Sat Nov 29 18:41:39 2008 +0000
@@ -0,0 +1,454 @@
+/*
+ * 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 "cipher.h"
+#include "debug.h"
+#include "imgstore.h"
+#include "prpl.h"
+#include "notify.h"
+#include "request.h"
+#include "util.h"
+#include "xmlnode.h"
+
+#include "buddy.h"
+#include "chat.h"
+#include "jabber.h"
+#include "iq.h"
+#include "presence.h"
+#include "xdata.h"
+#include "pep.h"
+#include "adhoccommands.h"
+#include "connection.h"
+
+void jabber_bosh_connection_init(PurpleBOSHConnection *conn, PurpleAccount *account, JabberStream *js, char *url) {
+	conn->pipelining = TRUE;
+	conn->account = account;
+	if (!purple_url_parse(url, &(conn->host), &(conn->port), &(conn->path), &(conn->user), &(conn->passwd))) {
+		purple_debug_info("jabber", "Unable to parse given URL.\n");
+		return;
+	}
+	if (conn->user || conn->passwd) {
+		purple_debug_info("jabber", "Sorry, HTTP Authentication isn't supported yet. Username and password in the BOSH URL will be ignored.\n");
+	}
+	conn->js = js;
+	conn->rid = rand() % 100000 + 1728679472;
+	conn->ready = FALSE;
+	conn->conn_a = g_new0(PurpleHTTPConnection, 1);
+	jabber_bosh_http_connection_init(conn->conn_a, conn->account, conn->host, conn->port);
+	conn->conn_a->userdata = conn;
+}
+
+void jabber_bosh_connection_stream_restart(PurpleBOSHConnection *conn) {
+	xmlnode *restart = xmlnode_new("body");
+	char *tmp = NULL;
+	conn->rid++;
+	xmlnode_set_attrib(restart, "rid", tmp = g_strdup_printf("%d", conn->rid));
+	g_free(tmp);
+	xmlnode_set_attrib(restart, "sid", conn->sid);
+	xmlnode_set_attrib(restart, "to", conn->js->user->domain);
+	xmlnode_set_attrib(restart, "xml:lang", "en");
+	xmlnode_set_attrib(restart, "xmpp:restart", "true");
+	xmlnode_set_attrib(restart, "xmlns", "http://jabber.org/protocol/httpbind");
+	xmlnode_set_attrib(restart, "xmlns:xmpp", "urn:xmpp:xbosh"); 
+	
+	jabber_bosh_connection_send_native(conn, restart);
+}
+
+gboolean jabber_bosh_connection_error_check(PurpleBOSHConnection *conn, xmlnode *node) {
+	char *type;
+	
+	if (!node) return FALSE;
+	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 conncetion manager suggested to terminate your session."));
+		return TRUE;
+	}
+	return FALSE;
+}
+
+void jabber_bosh_connection_received(PurpleBOSHConnection *conn, xmlnode *node) {
+	xmlnode *child;
+	JabberStream *js = conn->js;
+	
+	if (node == NULL) return;
+	
+	if (jabber_bosh_connection_error_check(conn, node) == TRUE) return;
+	
+	child = node->child;
+	while (child != 0) {
+		if (child->type == XMLNODE_TYPE_TAG) {
+			xmlnode *session = NULL;
+			if (!strcmp(child->name, "iq")) session = xmlnode_get_child(child, "session");
+			if (session) {
+				conn->ready = TRUE;
+			}
+			jabber_process_packet(js, &child);
+		}
+		child = child->next;
+	}
+}
+
+void jabber_bosh_connection_auth_response(PurpleBOSHConnection *conn, xmlnode *node) {
+	xmlnode *child = node->child;
+	
+	if (jabber_bosh_connection_error_check(conn, node) == TRUE) return;
+	
+	while(child != NULL && child->type != XMLNODE_TYPE_TAG) {
+		child = child->next;	
+	}
+	
+	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 printf("\n!! no child!!\n");
+}
+
+void jabber_bosh_connection_boot_response(PurpleBOSHConnection *conn, xmlnode *node) {
+	char *version;
+	
+	if (jabber_bosh_connection_error_check(conn, node) == TRUE) return;
+	
+	if (xmlnode_get_attrib(node, "sid")) {
+		conn->sid = g_strdup(xmlnode_get_attrib(node, "sid"));
+	} else {
+		purple_debug_info("jabber", "Connection manager doesn't behave BOSH-like!\n");
+	}
+	
+	if ((version = xmlnode_get_attrib(node, "ver"))) {
+		version[1] = 0;
+		if (!(atoi(version) >= 1 && atoi(&version[2]) >= 6)) purple_debug_info("jabber", 	"Unsupported version of BOSH protocol. The connection manager must at least support version 1.6!\n");
+		else {
+			xmlnode *packet = xmlnode_get_child(node, "features");
+			conn->js->use_bosh = TRUE;
+			conn->receive_cb = jabber_bosh_connection_auth_response;
+			jabber_stream_features_parse(conn->js, packet);
+		}
+		version[1] = '.';
+	} else {
+		purple_debug_info("jabber", "Missing version in session creation response!\n");	
+	}
+}
+
+static void jabber_bosh_connection_boot(PurpleBOSHConnection *conn) {
+	char *tmp;
+	xmlnode *init = xmlnode_new("body");
+	xmlnode_set_attrib(init, "content", "text/xml; charset=utf-8");
+	xmlnode_set_attrib(init, "secure", "true");
+	//xmlnode_set_attrib(init, "route", tmp = g_strdup_printf("xmpp:%s:5222", conn->js->user->domain));
+	//g_free(tmp);
+	xmlnode_set_attrib(init, "to", conn->js->user->domain);
+	xmlnode_set_attrib(init, "xml:lang", "en");
+	xmlnode_set_attrib(init, "xmpp:version", "1.0");
+	xmlnode_set_attrib(init, "ver", "1.6");
+	xmlnode_set_attrib(init, "xmlns:xmpp", "urn:xmpp:xbosh"); 
+	xmlnode_set_attrib(init, "rid", tmp = g_strdup_printf("%d", conn->rid));
+	g_free(tmp);
+	xmlnode_set_attrib(init, "wait", "60"); /* this should be adjusted automatically according to real time network behavior */
+	xmlnode_set_attrib(init, "xmlns", "http://jabber.org/protocol/httpbind");
+	xmlnode_set_attrib(init, "hold", "1");
+	
+	conn->receive_cb = jabber_bosh_connection_boot_response;
+	jabber_bosh_connection_send_native(conn, init);
+}
+
+void jabber_bosh_connection_http_received_cb(PurpleHTTPRequest *req, PurpleHTTPResponse *res, void *userdata) {
+	PurpleBOSHConnection *conn = userdata;
+	if (conn->receive_cb) {
+		xmlnode *node = xmlnode_from_str(res->data, res->data_len);
+		if (node) {
+			char *txt = xmlnode_to_formatted_str(node, NULL);
+			printf("\njabber_bosh_connection_http_received_cb\n%s\n", txt);
+			g_free(txt);
+			conn->receive_cb(conn, node);
+			xmlnode_free(node);
+		} else {
+			printf("\njabber_bosh_connection_http_received_cb: XML ERROR: %s\n", res->data); 
+		}
+	} else purple_debug_info("jabber", "missing receive_cb of PurpleBOSHConnection.\n");
+}
+
+void jabber_bosh_connection_send(PurpleBOSHConnection *conn, xmlnode *node) {
+	xmlnode *packet = xmlnode_new("body");
+	char *tmp;
+	conn->rid++;
+	xmlnode_set_attrib(packet, "xmlns", "http://jabber.org/protocol/httpbind");
+	xmlnode_set_attrib(packet, "sid", conn->sid);
+	tmp = g_strdup_printf("%d", conn->rid);
+	xmlnode_set_attrib(packet, "rid", tmp);
+	g_free(tmp);
+	
+	if (node) {
+		xmlnode_insert_child(packet, node);
+		if (conn->ready == TRUE) xmlnode_set_attrib(node, "xmlns", "jabber:client");
+	}
+	jabber_bosh_connection_send_native(conn, packet);
+}
+
+void jabber_bosh_connection_send_native(PurpleBOSHConnection *conn, xmlnode *node) {
+	PurpleHTTPRequest *request = g_new0(PurpleHTTPRequest, 1);
+	
+	char *txt = xmlnode_to_formatted_str(node, NULL);
+	printf("\njabber_bosh_connection_send\n%s\n", txt);
+	g_free(txt);
+	
+	jabber_bosh_http_request_init(request, "POST", g_strdup_printf("/%s", conn->path), jabber_bosh_connection_http_received_cb, conn);
+	jabber_bosh_http_request_add_to_header(request, "Content-Encoding", "text/xml; charset=utf-8");
+	request->data = xmlnode_to_str(node, &(request->data_len));
+	jabber_bosh_http_request_add_to_header(request, "Content-Length", g_strdup_printf("%d", (int)strlen(request->data)));
+	jabber_bosh_http_connection_send_request(conn->conn_a, request);
+}
+
+static void jabber_bosh_connection_connected(PurpleHTTPConnection *conn) {
+	PurpleBOSHConnection *bosh_conn = conn->userdata;
+	if (bosh_conn->ready == TRUE && bosh_conn->connect_cb) {
+		purple_debug_info("jabber", "BOSH session already exists. Trying to reuse it.\n");
+		bosh_conn->receive_cb = jabber_bosh_connection_received;
+		bosh_conn->connect_cb(bosh_conn);
+	} else jabber_bosh_connection_boot(bosh_conn);
+}
+
+static void jabber_bosh_connection_refresh(PurpleHTTPConnection *conn) {
+	PurpleBOSHConnection *bosh_conn = conn->userdata;
+	jabber_bosh_connection_send(bosh_conn, NULL);
+}
+
+static void jabber_bosh_http_connection_disconnected(PurpleHTTPConnection *conn) {
+	PurpleBOSHConnection *bosh_conn = conn->userdata;
+	bosh_conn->conn_a->connect_cb = jabber_bosh_connection_connected;
+	jabber_bosh_http_connection_connect(bosh_conn->conn_a);
+}
+
+void jabber_bosh_connection_connect(PurpleBOSHConnection *conn) {
+	conn->conn_a->connect_cb = jabber_bosh_connection_connected;
+	jabber_bosh_http_connection_connect(conn->conn_a);
+}
+
+void jabber_bosh_http_connection_receive_parse_header(PurpleHTTPResponse *response, char **data, int *len) {
+	GHashTable *header = response->header;
+	char *beginning = *data;
+	char *found = g_strstr_len(*data, len, "\r\n\r\n");
+	char *field = NULL;
+	char *value = NULL;
+	char *old_data = *data;
+	
+	while (*beginning != 'H') ++beginning;
+	beginning[12] = 0;
+	response->status = atoi(&beginning[9]);
+	beginning = &beginning[13];
+	do {
+		++beginning;
+	} while (*beginning != '\n');
+	++beginning;
+	/* parse HTTP response header */
+	for (;beginning != found; ++beginning) {
+		if (!field) field = beginning;
+		if (*beginning == ':') {
+			*beginning = 0;
+			value = beginning + 1;
+		} else if (*beginning == '\r') {
+			*beginning = 0;
+			g_hash_table_replace(header, g_strdup(field), g_strdup(value));
+			value = field = 0;
+			++beginning;
+		}
+	}
+	++beginning; ++beginning; ++beginning; ++beginning;
+	/* remove the header from data buffer */
+	*data = g_strdup(beginning);
+	*len = *len - (beginning - old_data);
+	g_free(old_data);	
+}
+
+static void jabber_bosh_http_connection_receive(gpointer data, gint source, PurpleInputCondition condition) {
+	char buffer[1025];
+	int len;
+	PurpleHTTPConnection *conn = data;
+	PurpleBOSHConnection *bosh_conn = conn->userdata;
+	PurpleHTTPResponse *response = conn->current_response;
+	
+	purple_debug_info("jabber", "jabber_bosh_http_connection_receive\n");
+	
+	len = read(source, buffer, 1024);
+	if (len > 0) {
+		buffer[len] = 0;
+		if (conn->current_data == NULL) {
+			conn->current_len = len;	
+			conn->current_data = g_strdup_printf("%s", buffer);
+		} else {
+			char *old_data = conn->current_data;
+			conn->current_len += len;	
+			conn->current_data = g_strdup_printf("%s%s", conn->current_data, buffer);
+			g_free(old_data);
+		}
+		
+		if (!response) {
+			/* check for header footer */
+			char *found = NULL;
+			if (found = g_strstr_len(conn->current_data, conn->current_len, "\r\n\r\n")) {
+				
+				// new response
+				response = conn->current_response = g_new0(PurpleHTTPResponse, 1);
+				jabber_bosh_http_response_init(response);
+				jabber_bosh_http_connection_receive_parse_header(response, &(conn->current_data), &(conn->current_len));
+				response->data_len = atoi(g_hash_table_lookup(response->header, "Content-Length"));
+			} else {
+				printf("\nDid not receive HTTP header\n");
+			}
+		} 
+		
+		if (response) {
+			if (conn->current_len >= response->data_len) {
+				PurpleHTTPRequest *request = g_queue_pop_head(conn->requests);
+				
+				#warning for a pure HTTP 1.1 stack this would be needed to be handled elsewhereå
+				if (bosh_conn->ready == TRUE && g_queue_is_empty(conn->requests) == TRUE) {
+					jabber_bosh_connection_send(bosh_conn, NULL); 
+					printf("\n SEND AN EMPTY REQUEST \n");
+				}
+				
+				if (request) {
+					char *old_data = conn->current_data;
+					response->data = g_memdup(conn->current_data, response->data_len);
+					conn->current_data = g_strdup(&conn->current_data[response->data_len]);
+					conn->current_len -= response->data_len;
+					g_free(old_data);
+					
+					if (request->cb) request->cb(request, response, conn->userdata);
+					else purple_debug_info("jabber", "missing request callback!\n");
+					
+					jabber_bosh_http_request_clean(request);
+					jabber_bosh_http_response_clean(response);
+					conn->current_response = NULL;
+					g_free(request);
+					g_free(response);
+				} else {
+					purple_debug_info("jabber", "received HTTP response but haven't requested anything yet.\n");
+				}
+			}
+		}
+	} else if (len == 0) {
+		purple_input_remove(conn->ie_handle);
+		if (conn->disconnect_cb) conn->disconnect_cb(conn);
+	} else {
+		purple_debug_info("jabber", "jabber_bosh_http_connection_receive: problem receiving data (%d)\n", len);
+	}
+}
+
+void jabber_bosh_http_connection_init(PurpleHTTPConnection *conn, PurpleAccount *account, char *host, int port) {
+	conn->account = account;
+	conn->host = host;
+	conn->port = port;
+	conn->connect_cb = NULL;
+	conn->current_response = NULL;
+	conn->current_data = NULL;
+	conn->requests = g_queue_new();
+}
+
+void jabber_bosh_http_connection_clean(PurpleHTTPConnection *conn) {
+	g_queue_free(conn->requests);
+}
+
+static void jabber_bosh_http_connection_callback(gpointer data, gint source, const gchar *error) {
+	PurpleHTTPConnection *conn = data;
+	if (source < 0) {
+		purple_debug_info("jabber", "Couldn't connect becasue of: %s\n", error);
+		return;
+	}
+	conn->fd = source;
+	if (conn->connect_cb) conn->connect_cb(conn);
+	else purple_debug_info("jabber", "No connect callback for HTTP connection.\n");
+	conn->ie_handle = purple_input_add(conn->fd, PURPLE_INPUT_READ, jabber_bosh_http_connection_receive, conn);
+}
+
+void jabber_bosh_http_connection_connect(PurpleHTTPConnection *conn) {
+	if((purple_proxy_connect(&(conn->handle), conn->account, conn->host, conn->port, jabber_bosh_http_connection_callback, conn)) == NULL) {
+		purple_debug_info("jabber", "Unable to connect to %s.\n", conn->host);
+	} 
+}
+
+static void jabber_bosh_http_connection_send_request_add_field_to_string(gpointer key, gpointer value, gpointer user_data) {
+	char **ppacket = user_data;
+	char *tmp = *ppacket;
+	char *field = key;
+	char *val = value;
+	*ppacket = g_strdup_printf("%s%s: %s\r\n", tmp, field, val);
+	g_free(tmp);
+}
+
+void jabber_bosh_http_connection_send_request(PurpleHTTPConnection *conn, PurpleHTTPRequest *req) {
+	char *packet;
+	char *tmp;
+	jabber_bosh_http_request_add_to_header(req, "Host", conn->host);
+	jabber_bosh_http_request_add_to_header(req, "User-Agent", "libpurple");
+	packet = tmp = g_strdup_printf("%s %s HTTP/1.1\r\n", req->method, req->path);
+	g_hash_table_foreach(req->header, jabber_bosh_http_connection_send_request_add_field_to_string, &packet);
+	tmp = packet;
+	packet = g_strdup_printf("%s\r\n%s", tmp, req->data);
+	g_free(tmp);
+	if (write(conn->fd, packet, strlen(packet)) == -1) purple_debug_info("jabber", "send error\n");
+	g_queue_push_tail(conn->requests, req);
+}
+
+void jabber_bosh_http_request_init(PurpleHTTPRequest *req, const char *method, const char *path, PurpleHTTPRequestCallback cb, void *userdata) {
+	req->method = g_strdup(method);
+	req->path = g_strdup(path);
+	req->cb = cb;
+	req->userdata = userdata;
+	req->header = g_hash_table_new(g_str_hash, g_str_equal);
+}
+
+void jabber_bosh_http_request_add_to_header(PurpleHTTPRequest *req, const char *field, const char *value) {
+	g_hash_table_replace(req->header, field, value);
+}
+
+void jabber_bosh_http_request_set_data(PurpleHTTPRequest *req, char *data, int len) {
+	req->data = data;
+	req->data_len = len;
+}
+
+void jabber_bosh_http_request_clean(PurpleHTTPRequest *req) {
+	g_hash_table_destroy(req->header);
+	g_free(req->method);
+	g_free(req->path);
+	g_free(req->data);
+}
+
+void jabber_bosh_http_response_init(PurpleHTTPResponse *res) {
+	res->status = 0;
+	res->header = g_hash_table_new(g_str_hash, g_str_equal);
+}
+
+
+void jabber_bosh_http_response_clean(PurpleHTTPResponse *res) {
+	g_hash_table_destroy(res->header);
+	g_free(res->data);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libpurple/protocols/jabber/bosh.h	Sat Nov 29 18:41:39 2008 +0000
@@ -0,0 +1,116 @@
+/**
+ * @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_
+
+#include <glib.h>
+
+typedef struct _PurpleHTTPRequest PurpleHTTPRequest;
+typedef struct _PurpleHTTPResponse PurpleHTTPResponse;
+typedef struct _PurpleHTTPConnection PurpleHTTPConnection;
+typedef struct _PurpleBOSHConnection PurpleBOSHConnection;
+
+typedef void (*PurpleHTTPConnectionConnectFunction)(PurpleHTTPConnection *conn);
+typedef void (*PurpleHTTPConnectionDisconnectFunction)(PurpleHTTPConnection *conn);
+typedef void (*PurpleHTTPRequestCallback)(PurpleHTTPRequest *req, PurpleHTTPResponse *res, void *userdata);
+typedef void (*PurpleBOSHConnectionConnectFunction)(PurpleBOSHConnection *conn);
+typedef void (*PurpleBOSHConnectionReciveFunction)(PurpleBOSHConnection *conn, xmlnode *node);
+
+struct _PurpleBOSHConnection {
+    /* decoded URL */
+    char *host;
+    int port;
+    char *path; 
+    char *user;
+    char *passwd;
+    
+    int rid;
+    char *sid;
+    int wait;
+        
+    JabberStream *js;
+    void *userdata;
+    PurpleAccount *account;
+    gboolean pipelining;
+    PurpleHTTPConnection *conn_a;
+    PurpleHTTPConnection *conn_b;
+    
+    gboolean ready;
+    PurpleBOSHConnectionConnectFunction connect_cb;
+    PurpleBOSHConnectionReciveFunction receive_cb;
+};
+
+struct _PurpleHTTPConnection {
+    int fd;
+    char *host;
+    int port;
+    int handle;
+    int ie_handle;
+    PurpleConnection *conn;
+    PurpleAccount *account;
+    GQueue *requests;
+    
+    PurpleHTTPResponse *current_response;
+    char *current_data;
+    int current_len;
+    
+    int pih;
+    PurpleHTTPConnectionConnectFunction connect_cb;
+    PurpleHTTPConnectionConnectFunction disconnect_cb;
+    void *userdata;
+};
+
+struct _PurpleHTTPRequest {
+    PurpleHTTPRequestCallback cb;
+    char *method;
+    char *path;
+    GHashTable *header;
+    char *data;
+    int data_len;
+    void *userdata;
+};
+
+struct _PurpleHTTPResponse {
+    int status;
+    GHashTable *header;
+    char *data;
+    int data_len;
+};
+
+void jabber_bosh_connection_init(PurpleBOSHConnection *conn, PurpleAccount *account, JabberStream *js, char *url);
+void jabber_bosh_connection_connect(PurpleBOSHConnection *conn);
+void jabber_bosh_connection_send_native(PurpleBOSHConnection *conn, xmlnode *node);
+void jabber_bosh_connection_send(PurpleBOSHConnection *conn, xmlnode *node);
+
+void jabber_bosh_http_connection_init(PurpleHTTPConnection *conn, PurpleAccount *account, char *host, int port);
+void jabber_bosh_http_connection_connect(PurpleHTTPConnection *conn);
+void jabber_bosh_http_connection_send_request(PurpleHTTPConnection *conn, PurpleHTTPRequest *req);
+void jabber_bosh_http_connection_clean(PurpleHTTPConnection *conn);
+
+void jabber_bosh_http_request_init(PurpleHTTPRequest *req, const char *method, const char *path, PurpleHTTPRequestCallback cb, void *userdata);
+void jabber_bosh_http_request_add_to_header(PurpleHTTPRequest *req, const char *field, const char *value);
+void jabber_bosh_http_request_set_data(PurpleHTTPRequest *req, char *data, int len);
+void jabber_bosh_http_request_clean(PurpleHTTPRequest *req);
+
+void jabber_bosh_http_response_init(PurpleHTTPResponse *res);
+void jabber_bosh_http_response_clean(PurpleHTTPResponse *res);
+#endif /* _PURPLE_JABBER_BOSH_H_ */
--- a/libpurple/protocols/jabber/buddy.c	Sat Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/buddy.c	Sat Nov 29 18:41:39 2008 +0000
@@ -180,9 +180,8 @@
 		g_free(cmd);
 		jbr->commands = g_list_delete_link(jbr->commands, jbr->commands);
 	}
-	
-	jabber_caps_free_clientinfo(jbr->caps);
 
+	/* jbr->caps is owned by the caps code */
 	g_free(jbr->name);
 	g_free(jbr->status);
 	g_free(jbr->thread_id);
@@ -497,9 +496,6 @@
 
 void jabber_set_buddy_icon(PurpleConnection *gc, PurpleStoredImage *img)
 {
-	PurplePresence *gpresence;
-	PurpleStatus *status;
-	
 	if(((JabberStream*)gc->proto_data)->pep) {
 		/* XEP-0084: User Avatars */
 		if(img) {
@@ -629,9 +625,7 @@
 	/* publish vCard for those poor older clients */
 	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);
 }
 
 /*
@@ -2505,7 +2499,7 @@
 gboolean
 jabber_resource_has_capability(const JabberBuddyResource *jbr, const gchar *cap)
 {
-	const GList *iter = NULL;
+	const GList *node = NULL;
 
 	if (!jbr->caps) {
 		purple_debug_error("jabber",
@@ -2513,15 +2507,14 @@
 		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->features, cap, (GCompareFunc)strcmp);
+	/* 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); 
 
-	purple_debug_info("jabber", "Cap %s not found\n", cap);
-	return FALSE;
+	return (node != NULL);
 }
 
 gboolean
--- a/libpurple/protocols/jabber/buddy.h	Sat Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/buddy.h	Sat Nov 29 18:41:39 2008 +0000
@@ -81,7 +81,7 @@
 		char *name;
 		char *os;
 	} client;
-	JabberCapsClientInfo *caps;
+	const JabberCapsClientInfo *caps;
 	GList *commands;
 } JabberBuddyResource;
 
--- a/libpurple/protocols/jabber/caps.c	Sat Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/caps.c	Sat Nov 29 18:41:39 2008 +0000
@@ -21,73 +21,100 @@
 
 #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 */
 
+/**
+ *	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);
+
+#if 0
 typedef struct _JabberCapsValue {
 	GList *identities; /* JabberCapsIdentity */
 	GList *features; /* char * */
 	GHashTable *ext; /* char * -> JabberCapsValueExt */
 } JabberCapsValue;
+#endif
 
 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);
-	
-	return nodehash ^ verhash;
+	guint hashhash = g_str_hash(name->hash);
+	return nodehash ^ verhash ^ hashhash;
 }
 
 static gboolean jabber_caps_compare(gconstpointer v1, gconstpointer v2) {
 	const JabberCapsKey *name1 = v1;
 	const JabberCapsKey *name2 = v2;
-
-	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 &&
+	       strcmp(name1->hash, name2->hash) == 0;
 }
 
-static void jabber_caps_destroy_key(gpointer key) {
+void jabber_caps_destroy_key(gpointer key) {
 	JabberCapsKey *keystruct = key;
 	g_free(keystruct->node);
 	g_free(keystruct->ver);
+	g_free(keystruct->hash);
 	g_free(keystruct);
 }
 
-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(gpointer data) {
+	JabberCapsClientInfo *info = data;
+	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(info->features) {
+		g_free(info->features->data);
+		info->features = g_list_delete_link(info->features, info->features);
 	}
-	while(valuestruct->features) {
-		g_free(valuestruct->features->data);
-		valuestruct->features = g_list_delete_link(valuestruct->features,valuestruct->features);
+
+	while(info->forms) {
+		g_free(info->forms->data);
+		info->forms = g_list_delete_link(info->forms, info->forms);
 	}
+
+#if 0
 	g_hash_table_destroy(valuestruct->ext);
-	g_free(valuestruct);
+#endif
+
+	g_free(info);
 }
 
+#if 0
 static void jabber_caps_ext_destroy_value(gpointer value) {
 	JabberCapsValueExt *valuestruct = value;
 	while(valuestruct->identities) {
@@ -105,14 +132,22 @@
 	}
 	g_free(valuestruct);
 }
+#endif
 
 static void jabber_caps_load(void);
 
-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);
+void jabber_caps_init(void)
+{
+	capstable = g_hash_table_new_full(jabber_caps_hash, jabber_caps_compare, jabber_caps_destroy_key, jabber_caps_client_info_destroy);
 	jabber_caps_load();
 }
 
+void jabber_caps_uninit(void)
+{
+	g_hash_table_destroy(capstable);
+	capstable = NULL;
+}
+
 static void jabber_caps_load(void) {
 	xmlnode *capsdata = purple_util_read_xml_from_file(JABBER_CAPS_FILENAME, "XMPP capabilities cache");
 	xmlnode *client;
@@ -130,11 +165,11 @@
 			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;
 			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"));
 			for(child = client->child; child; child = child->next) {
 				if(child->type != XMLNODE_TYPE_TAG)
 					continue;
@@ -147,43 +182,21 @@
 					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);
+					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);
 					
 					value->identities = g_list_append(value->identities,id);
-				} else if(!strcmp(child->name,"ext")) {
-					const char *identifier = xmlnode_get_attrib(child, "identifier");
-					if(identifier) {
-						xmlnode *extchild;
-						
-						JabberCapsValueExt *extvalue = g_new0(JabberCapsValueExt, 1);
-						
-						for(extchild = child->child; extchild; extchild = extchild->next) {
-							if(extchild->type != XMLNODE_TYPE_TAG)
-								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");
-								
-								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);
-					}
+				} else if(!strcmp(child->name,"x")) {
+					value->forms = g_list_append(value->forms, xmlnode_copy(child));
 				}
 			}
 			g_hash_table_replace(capstable, key, value);
@@ -192,6 +205,7 @@
 	xmlnode_free(capsdata);
 }
 
+#if 0
 static void jabber_caps_store_ext(gpointer key, gpointer value, gpointer user_data) {
 	const char *extname = key;
 	JabberCapsValueExt *props = value;
@@ -216,24 +230,27 @@
 		xmlnode_set_attrib(feature, "var", feat);
 	}
 }
+#endif
 
 static void jabber_caps_store_client(gpointer key, gpointer value, gpointer user_data) {
 	JabberCapsKey *clientinfo = key;
-	JabberCapsValue *props = value;
+	JabberCapsClientInfo *props = value;
 	xmlnode *root = user_data;
-	xmlnode *client = xmlnode_new_child(root,"client");
+	xmlnode *client = xmlnode_new_child(root, "client");
 	GList *iter;
 
-	xmlnode_set_attrib(client,"node",clientinfo->node);
-	xmlnode_set_attrib(client,"ver",clientinfo->ver);
-	
+	xmlnode_set_attrib(client, "node", clientinfo->node);
+	xmlnode_set_attrib(client, "ver", clientinfo->ver);
+	xmlnode_set_attrib(client, "hash", clientinfo->hash);
 	for(iter = props->identities; iter; iter = g_list_next(iter)) {
-		JabberCapsIdentity *id = iter->data;
+		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);
 	}
 
 	for(iter = props->features; iter; iter = g_list_next(iter)) {
@@ -242,19 +259,24 @@
 		xmlnode_set_attrib(feature, "var", feat);
 	}
 	
-	g_hash_table_foreach(props->ext,jabber_caps_store_ext,client);
+	for(iter = props->forms; iter; iter = g_list_next(iter)) {
+		xmlnode *xdata = iter->data;
+		xmlnode_insert_child(client, xmlnode_copy(xdata));
+	}
 }
 
 static void jabber_caps_store(void) {
 	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, NULL);
+	str = xmlnode_to_formatted_str(root, &length);
 	xmlnode_free(root);
-	purple_util_write_data_to_file(JABBER_CAPS_FILENAME, str, -1);
+	purple_util_write_data_to_file(JABBER_CAPS_FILENAME, str, length);
 	g_free(str);
 }
 
+#if 0
 /* 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;
@@ -290,7 +312,7 @@
 		
 		result->features = g_list_append(result->features,newfeat);
 	}
-	
+#if 0	
 	for(iter = ext; iter; iter = g_list_next(iter)) {
 		const char *extname = iter->data;
 		JabberCapsValueExt *extinfo = g_hash_table_lookup(caps->ext,extname);
@@ -314,30 +336,10 @@
 			}
 		}
 	}
+#endif
 	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);
-}
+#endif
 
 typedef struct _jabber_caps_cbplususerdata {
 	jabber_caps_get_info_cb cb;
@@ -346,15 +348,20 @@
 	char *who;
 	char *node;
 	char *ver;
-	GList *ext;
+	char *hash;
+#if 0
 	unsigned extOutstanding;
+#endif
 } jabber_caps_cbplususerdata;
 
+#if 0
 typedef struct jabber_ext_userdata {
 	jabber_caps_cbplususerdata *userdata;
 	char *node;
 } jabber_ext_userdata;
+#endif
 
+#if 0
 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);
@@ -368,7 +375,8 @@
 		g_free(userdata);
 	}
 }
-
+#endif
+#if 0
 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");
@@ -379,7 +387,7 @@
 	--userdata->extOutstanding;
 
 	/* TODO: Better error handling */
-
+	printf("\n\tjabber_caps_ext_iqcb for %s", xmlnode_get_attrib(packet, "from"));
 	if(node && query) {
 		const char *key;
 		JabberCapsValue *client;
@@ -423,8 +431,6 @@
 				value->identities = g_list_append(value->identities,id);
 			}
 		}
-		g_hash_table_replace(client->ext, g_strdup(key), value);
-
 		jabber_caps_store();
 	}
 
@@ -432,105 +438,116 @@
 	g_free(extuserdata);
 	jabber_caps_get_info_check_completion(userdata);
 }
+#endif
 
-static void jabber_caps_client_iqcb(JabberStream *js, xmlnode *packet, gpointer data) {
-	/* collect data and fetch all exts */
+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;
+	JabberCapsClientInfo *info, *value;
+	gchar *hash;
+	const char *type = xmlnode_get_attrib(packet, "type");
+	JabberCapsKey key;
 
-	/* TODO: Better error checking! */
+	if (!query || !strcmp(type, "error")) {
+		userdata->cb(NULL, userdata->user_data);
 
-	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);
+		g_free(userdata->who);
+		g_free(userdata->node);
+		g_free(userdata->ver);
+		g_free(userdata->hash);
+		g_free(userdata);
+		return;
+	}
 
-		key->node = g_strdup(userdata->node);
-		key->ver = g_strdup(userdata->ver);
+	/* check hash */
+	info = jabber_caps_parse_client_info(query);
+
+	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");
+	} else {
+		purple_debug_warning("jabber", "unknown caps hash algorithm: %s\n", userdata->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");
+		userdata->cb(NULL, userdata->user_data);
+
+		jabber_caps_client_info_destroy(info);
+		g_free(userdata->who);
+		g_free(userdata->node);
+		g_free(userdata->ver);
+		g_free(userdata->hash);
+		g_free(userdata);		
+		return;	
+	}
 
-				JabberCapsIdentity *id = g_new0(JabberCapsIdentity, 1);
-				id->category = g_strdup(category);
-				id->type = g_strdup(type);
-				id->name = g_strdup(name);
+	if (!hash || strcmp(hash, userdata->ver)) {
+		purple_debug_warning("jabber", "caps hash from %s did not match\n", xmlnode_get_attrib(packet, "from"));
+		userdata->cb(NULL, userdata->user_data);
 
-				value->identities = g_list_append(value->identities,id);
-			}
-		}
-		g_hash_table_replace(capstable, key, value);
-		jabber_caps_store();
+		jabber_caps_client_info_destroy(info);
+		g_free(userdata->who);
+		g_free(userdata->node);
+		g_free(userdata->ver);
+		g_free(userdata->hash);
+		g_free(userdata);		
+		g_free(hash);
+		return;
+	}
 
+	key.node = userdata->node;
+	key.ver = userdata->ver;
+	key.hash = userdata->hash;
 
-		/* 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;
+	/* check whether it's not in the table */
+	if ((value = g_hash_table_lookup(capstable, &key))) {
+		JabberCapsClientInfo *tmp = info;
+		info = value;
+		jabber_caps_client_info_destroy(tmp);
+	} 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;
 
-			xmlnode_set_attrib(query, "node", node);
-			xmlnode_set_attrib(iq->node, "to", userdata->who);
+		g_hash_table_insert(capstable, n_key, info);
+		jabber_caps_store();
+	}
+
+	userdata->cb(info, userdata->user_data);
 
-			jabber_iq_set_callback(iq, jabber_caps_ext_iqcb, ext_data);
-			jabber_iq_send(iq);
-		}
-
-	} else
-		/* Don't wait for the ext discoveries; they aren't going to happen */
-		userdata->extOutstanding = 0;
-
-	jabber_caps_get_info_check_completion(userdata);
+	/* capstable will free info */
+	g_free(userdata->who);
+	g_free(userdata->node);
+	g_free(userdata->ver);
+	g_free(userdata->hash);	
+	g_free(userdata);
+	g_free(hash);
 }
 
-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;
+void jabber_caps_get_info(JabberStream *js, const char *who, const char *node,
+		const char *ver, const char *hash, jabber_caps_get_info_cb cb,
+		gpointer user_data)
+{
+	JabberCapsClientInfo *client;
 	JabberCapsKey *key = g_new0(JabberCapsKey, 1);
-	char *originalext = g_strdup(ext);
 	jabber_caps_cbplususerdata *userdata = g_new0(jabber_caps_cbplususerdata, 1);
 	userdata->cb = cb;
 	userdata->user_data = user_data;
 	userdata->who = g_strdup(who);
 	userdata->node = g_strdup(node);
 	userdata->ver = g_strdup(ver);
+	userdata->hash = g_strdup(hash);
 
-	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);
-
-	key->node = (char *)node;
-	key->ver = (char *)ver;
-
+	key->node = g_strdup(node);
+	key->ver = g_strdup(ver);
+	key->hash = g_strdup(hash);
+	
 	client = g_hash_table_lookup(capstable, key);
-
-	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");
@@ -541,6 +558,8 @@
 
 		jabber_iq_set_callback(iq,jabber_caps_client_iqcb,userdata);
 		jabber_iq_send(iq);
+	}
+#if 0
 	} else {
 		GList *iter;
 		/* fetch unknown exts only */
@@ -574,5 +593,291 @@
 		/* 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);
 	}
+#endif
+}
+
+static gint jabber_caps_jabber_identity_compare(gconstpointer a, gconstpointer b) {
+	const JabberIdentity *ac;
+	const JabberIdentity *bc;
+	gint cat_cmp;
+	gint typ_cmp;
+	
+	ac = a;
+	bc = b;
+
+	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;
+		}
+	} else {
+		return cat_cmp;
+	}
+}
+
+#if 0
+static gint jabber_caps_jabber_feature_compare(gconstpointer a, gconstpointer b) {
+	const JabberFeature *ac;
+	const JabberFeature *bc;
+	
+	ac = a;
+	bc = b;
+	
+	return strcmp(ac->namespace, bc->namespace);
+}
+#endif
+
+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_caps_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)
+				continue;
+			info->features = g_list_append(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;
+
+	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_caps_jabber_identity_compare);
+	info->features = g_list_sort(info->features, (GCompareFunc)strcmp);
+	info->forms = g_list_sort(info->forms, jabber_caps_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/> element's 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);
+
+	if (!purple_cipher_context_digest(context, verification->len, checksum, &checksum_size)) {
+		/* purple_debug_error("util", "Failed to get digest.\n"); */
+		g_string_free(verification, TRUE);
+		purple_cipher_context_destroy(context);
+		return NULL;
+	}
+
+	g_string_free(verification, TRUE);
+	purple_cipher_context_destroy(context);
+
+	return purple_base64_encode(checksum, checksum_size);
+}
+
+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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/caps.h	Sat Nov 29 18:41:39 2008 +0000
@@ -26,24 +26,54 @@
 
 #include "jabber.h"
 
-/* Implementation of XEP-0115 */
-
-typedef struct _JabberCapsIdentity {
-	char *category;
-	char *type;
-	char *name;
-} JabberCapsIdentity;
+/* Implementation of XEP-0115 - Entity Capabilities */
 
 struct _JabberCapsClientInfo {
-	GList *identities; /* JabberCapsIdentity */
+	GList *identities; /* JabberIdentity */
 	GList *features; /* char * */
+	GList *forms; /* xmlnode * */
 };
 
+#if 0
+typedef struct _JabberCapsClientInfo JabberCapsValueExt;
+#endif
+
 typedef void (*jabber_caps_get_info_cb)(JabberCapsClientInfo *info, 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.
+ */
+void jabber_caps_get_info(JabberStream *js, const char *who, const char *node, const char *ver, const char *hash, 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; needs to be freed if not needed 
+ *				any furthermore. 
+ */
+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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/data.c	Sat Nov 29 18:41:39 2008 +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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/disco.c	Sat Nov 29 18:41:39 2008 +0000
@@ -88,7 +88,6 @@
 void jabber_disco_info_parse(JabberStream *js, xmlnode *packet) {
 	const char *from = xmlnode_get_attrib(packet, "from");
 	const char *type = xmlnode_get_attrib(packet, "type");
-
 	if(!from || !type)
 		return;
 
@@ -98,6 +97,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");
@@ -115,90 +118,40 @@
 		if(node)
 			xmlnode_set_attrib(query, "node", node);
 
-		if(!node || !strcmp(node, CAPS0115_NODE "#" VERSION)) {
-			identity = xmlnode_new_child(query, "identity");
-			xmlnode_set_attrib(identity, "category", "client");
-			xmlnode_set_attrib(identity, "type", "pc"); /* XXX: bot, console,
-														 * 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")
-#if 0
-				SUPPORT_FEATURE("http://jabber.org/protocol/ibb")
-#endif
-			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);
+				}	
 			}
 		} 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;
-				}
+			xmlnode *error, *inf;
 				
-				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;
-				}
-			}
+			/* XXX: gross hack, implement jabber_iq_set_type or something */
+			xmlnode_set_attrib(iq->node, "type", "error");
+			iq->type = JABBER_IQ_ERROR;
 			
-			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");
-			}
+			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");
@@ -348,7 +301,7 @@
 	}
 
 	/* Send initial presence; this will trigger receipt of presence for contacts on the roster */
-	jabber_presence_send(js->gc->account, NULL);
+	jabber_presence_send(js, TRUE);
 
 	if (js->server_caps & JABBER_CAP_ADHOC) {
 		/* The server supports ad-hoc commands, so let's request the list */
--- a/libpurple/protocols/jabber/jabber.c	Sat Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/jabber.c	Sat Nov 29 18:41:39 2008 +0000
@@ -39,6 +39,7 @@
 #include "version.h"
 #include "xmlnode.h"
 
+#include "caps.h"
 #include "auth.h"
 #include "buddy.h"
 #include "chat.h"
@@ -62,7 +63,9 @@
 #define JABBER_CONNECT_STEPS (js->gsc ? 9 : 5)
 
 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);
@@ -169,7 +172,7 @@
 	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))
@@ -385,7 +388,19 @@
 	}
 #endif
 
-	do_jabber_send_raw(js, data, len);
+	if (len == -1)
+		len = strlen(data);
+
+	if (js->use_bosh) {
+		xmlnode *xnode = xmlnode_from_str(data, len);
+		if (xnode) jabber_bosh_connection_send(&(js->bosh), xnode);
+		else {
+			purple_connection_error_reason(js->gc, PURPLE_CONNECTION_ERROR_OTHER_ERROR,
+							_("Someone tried to send non-XML in a Jabber world."));
+		}
+	} else {
+		do_jabber_send_raw(js, data, len);
+	}
 }
 
 int jabber_prpl_send_raw(PurpleConnection *gc, const char *buf, int len)
@@ -548,6 +563,46 @@
 	jabber_stream_set_state(js, JABBER_STREAM_INITIALIZING_ENCRYPTION);
 }
 
+static void
+jabber_bosh_login_callback(PurpleBOSHConnection *conn) 
+{
+	purple_debug_info("jabber","YAY...BOSH connection established.\n");
+}
+
+static void 
+txt_resolved_cb(PurpleTxtResponse *resp, int results, gpointer data)
+{
+	PurpleConnection *gc = data;
+	JabberStream *js = gc->proto_data;
+	int n;
+	
+	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 (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]);
+			jabber_bosh_connection_init(&(js->bosh), gc->account, js, token[1]);
+			g_strfreev(token);
+			break;
+		}
+		g_strfreev(token);
+	}
+	if (js->bosh.host) {
+		js->bosh.userdata = gc;
+		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)
@@ -560,12 +615,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);
+			purple_txt_resolve("_xmppconnect", js->user->domain, txt_resolved_cb, gc);
 		}
 		return;
 	}
@@ -731,7 +782,7 @@
 	/* 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]) {
+		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",
@@ -1341,6 +1392,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)
@@ -1442,31 +1494,27 @@
 	js->idle = idle ? time(NULL) - idle : idle;
 }
 
-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;
@@ -1474,6 +1522,53 @@
 	}
 }
 
+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);
+	}
+}
+
 const char *jabber_list_icon(PurpleAccount *a, PurpleBuddy *b)
 {
 	return "jabber";
@@ -2299,7 +2394,6 @@
 
 	JabberBuddy *jb;
 	JabberBuddyResource *jbr;
-	GList *iter;
 
 	if(!username)
 		return FALSE;
@@ -2316,31 +2410,30 @@
 		return FALSE;
 	}
 
+	/* Is this message sufficiently useful to not just fold it in with the tail error condition below? */
 	if(!jbr->caps) {
 		*error = g_strdup_printf(_("Unable to buzz, because there is nothing known about user %s."), username);
 		return FALSE;
 	}
 
-	for(iter = jbr->caps->features; iter; iter = g_list_next(iter)) {
-		if(!strcmp(iter->data, "http://www.xmpp.org/extensions/xep-0224.html#ns")) {
-			xmlnode *buzz, *msg = xmlnode_new("message");
-			gchar *to;
-
-			to = g_strdup_printf("%s/%s", username, jbr->name);
-			xmlnode_set_attrib(msg, "to", to);
-			g_free(to);
-
-			/* avoid offline storage */
-			xmlnode_set_attrib(msg, "type", "headline");
-
-			buzz = xmlnode_new_child(msg, "attention");
-			xmlnode_set_namespace(buzz, "http://www.xmpp.org/extensions/xep-0224.html#ns");
-
-			jabber_send(js, msg);
-			xmlnode_free(msg);
-
-			return TRUE;
-		}
+	if (jabber_resource_has_capability(jbr, "http://www.xmpp.org/extensions/xep-0224.html#ns")) {
+		xmlnode *buzz, *msg = xmlnode_new("message");
+		gchar *to;
+
+		to = g_strdup_printf("%s/%s", username, jbr->name);
+		xmlnode_set_attrib(msg, "to", to);
+		g_free(to);
+
+		/* avoid offline storage */
+		xmlnode_set_attrib(msg, "type", "headline");
+
+		buzz = xmlnode_new_child(msg, "attention");
+		xmlnode_set_namespace(buzz, "http://www.xmpp.org/extensions/xep-0224.html#ns");
+
+		jabber_send(js, msg);
+		xmlnode_free(msg);
+
+		return TRUE;
 	}
 
 	*error = g_strdup_printf(_("Unable to buzz, because the user %s does not support it."), username);
@@ -2474,8 +2567,97 @@
 					  _("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("xmpp:urn: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/disco#info", 0);
+	jabber_add_feature("http://jabber.org/protocol/disco#items", 0);
+#if 0
+	jabber_add_feature("http://jabber.org/protocol/ibb", 0);
+#endif
+	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);
+	
+	/* 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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/jabber.h	Sat Nov 29 18:41:39 2008 +0000
@@ -59,12 +59,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
@@ -205,6 +206,9 @@
 	
 	gboolean vcard_fetched;
 
+	/* Entity Capabilities hash */
+	char *caps_hash;
+
 	/* does the local server support PEP? */
 	gboolean pep;
 
@@ -232,10 +236,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
@@ -243,15 +252,22 @@
 	GSList *url_datas;
 };
 
-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;
@@ -261,7 +277,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);
@@ -282,8 +300,17 @@
  */
 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);
 
 /** PRPL functions */
 const char *jabber_list_icon(PurpleAccount *a, PurpleBuddy *b);
@@ -306,6 +333,8 @@
 int jabber_prpl_send_raw(PurpleConnection *gc, const char *buf, int len);
 GList *jabber_actions(PurplePlugin *plugin, gpointer context);
 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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/libxmpp.c	Sat Nov 29 18:41:39 2008 +0000
@@ -69,7 +69,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 */
@@ -148,9 +148,17 @@
 	purple_signal_unregister(plugin, "jabber-sending-xmlnode");
 	
 	purple_signal_unregister(plugin, "jabber-sending-text");
-	
+
+	/* reverse order of init_plugin */
 	jabber_data_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;
 }
 
@@ -272,23 +280,23 @@
 #endif
 #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_tune_init();
-	jabber_caps_init();
-	
 	jabber_data_init();
-	
-	jabber_add_feature("avatarmeta", AVATARNAMESPACEMETA, jabber_pep_namespace_only_when_pep_enabled_cb);
-	jabber_add_feature("avatardata", AVATARNAMESPACEDATA, jabber_pep_namespace_only_when_pep_enabled_cb);
-	jabber_add_feature("buzz", "http://www.xmpp.org/extensions/xep-0224.html#ns",
+
+	#warning implement adding and retrieving own features via IPC API
+
+	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(XEP_0231_NAMESPACE, jabber_custom_smileys_isenabled);
 
-	jabber_pep_register_handler("avatar", AVATARNAMESPACEMETA, jabber_buddy_avatar_update_metadata);
+	jabber_pep_register_handler(AVATARNAMESPACEMETA, jabber_buddy_avatar_update_metadata);
 }
 
 
--- a/libpurple/protocols/jabber/message.c	Sat Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/message.c	Sat Nov 29 18:41:39 2008 +0000
@@ -1232,12 +1232,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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/message.h	Sat Nov 29 18:41:39 2008 +0000
@@ -78,9 +78,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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/pep.c	Sat Nov 29 18:41:39 2008 +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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/pep.h	Sat Nov 29 18:41:39 2008 +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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/presence.c	Sat Nov 29 18:41:39 2008 +0000
@@ -94,11 +94,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 = gc->proto_data;
+	jabber_presence_send(js, FALSE);
+}
+
+void jabber_presence_send(JabberStream *js, gboolean force)
+{
+	PurpleAccount *account;
 	xmlnode *presence, *x, *photo;
 	char *stripped = NULL;
 	JabberBuddyState state;
@@ -107,28 +127,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) {
@@ -142,16 +145,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);
@@ -182,8 +187,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);
@@ -261,11 +265,16 @@
 	}
 
 	/* 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);
-	
+	xmlnode_set_attrib(c, "hash", "sha-1");
+	xmlnode_set_attrib(c, "ver", jabber_caps_get_own_hash(js));
+
+#if 0
 	if(js != NULL) {
 		/* add the extensions */
 		char extlist[1024];
@@ -277,7 +286,7 @@
 			JabberFeature *feat = (JabberFeature*)feature->data;
 			unsigned featlen;
 			
-			if(feat->is_enabled != NULL && feat->is_enabled(js, feat->shortname, feat->namespace) == FALSE)
+			if(feat->is_enabled != NULL && feat->is_enabled(js, feat->namespace) == FALSE)
 				continue; /* skip this feature */
 			
 			featlen = strlen(feat->shortname);
@@ -297,7 +306,7 @@
 		if(remaining < 1023)
 			xmlnode_set_attrib(c, "ext", extlist);
 	}
-	
+#endif
 	return presence;
 }
 
@@ -373,39 +382,29 @@
 	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, 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);
 		return;
 	}
 
-	if(jbr->caps)
-		jabber_caps_free_clientinfo(jbr->caps);
+	/* old value in jbr->caps is owned by caps code */
 	jbr->caps = info;
 
-	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");
-
-				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);
@@ -748,16 +747,19 @@
 			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 *ext = xmlnode_get_attrib(caps,"ext");
+				const char *hash = xmlnode_get_attrib(caps,"hash");
 				
-				if(node && ver) {
+				if(node && ver && hash) {
 					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,
+					    (jabber_caps_get_info_cb)jabber_presence_set_capabilities,
+					    userdata);
 				}
 			}
 		}
--- a/libpurple/protocols/jabber/presence.h	Sat Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/presence.h	Sat Nov 29 18:41:39 2008 +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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/roster.c	Sat Nov 29 18:41:39 2008 +0000
@@ -261,7 +261,7 @@
 	if(!js->roster_parsed) {
 		js->roster_parsed = TRUE;
 
-		jabber_presence_send(js->gc->account, NULL);
+		jabber_presence_send(js, TRUE);
 	}
 }
 
--- a/libpurple/protocols/jabber/usermood.c	Sat Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/usermood.c	Sat Nov 29 18:41:39 2008 +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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/usernick.c	Sat Nov 29 18:41:39 2008 +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 Nov 29 18:20:58 2008 +0000
+++ b/libpurple/protocols/jabber/usertune.c	Sat Nov 29 18:41:39 2008 +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) {