changeset 28506:2e3678cd33a0

jabber: Properly handle adding buddies that contain a resource. Closes #10151. Need to strip the resource when adding a buddy, so that the core doesn't think the buddy is named "juliet@capulet.lit/chamber", which apparently causes very strange issues.
author Paul Aurich <paul@darkrain42.org>
date Mon, 31 Aug 2009 03:39:34 +0000
parents cb8937a8a17f
children 08a3da3bc2d7
files ChangeLog libpurple/protocols/jabber/jutil.c libpurple/protocols/jabber/jutil.h libpurple/protocols/jabber/roster.c
diffstat 4 files changed, 52 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Aug 31 03:29:48 2009 +0000
+++ b/ChangeLog	Mon Aug 31 03:39:34 2009 +0000
@@ -14,6 +14,10 @@
 	  Windows.
 	* Fix typing notifications with Pidgin 2.5.9 or earlier.
 	* Fix connecting using BOSH and legacy authentication (XEP-0078).
+	* Adding buddies of the form "romeo@montague.net/Resource" are handled
+	  properly.  In addition, it is no longer possible to add buddies of
+	  the form "room@conference.example.net/User", where
+	  room@conference.example.net is a MUC.
 
 	Finch:
 	* Properly detect libpanel on OpenBSD.  (Brad Smith)
--- a/libpurple/protocols/jabber/jutil.c	Mon Aug 31 03:29:48 2009 +0000
+++ b/libpurple/protocols/jabber/jutil.c	Mon Aug 31 03:39:34 2009 +0000
@@ -489,21 +489,31 @@
 	return out;
 }
 
-char *jabber_get_bare_jid(const char *in)
+char *
+jabber_get_bare_jid(const char *in)
 {
 	JabberID *jid = jabber_id_new(in);
 	char *out;
 
-	if(!jid)
+	if (!jid)
 		return NULL;
-
-	out = g_strdup_printf("%s%s%s", jid->node ? jid->node : "",
-			jid->node ? "@" : "", jid->domain);
+	out = jabber_id_get_bare_jid(jid);
 	jabber_id_free(jid);
 
 	return out;
 }
 
+char *
+jabber_id_get_bare_jid(const JabberID *jid)
+{
+	g_return_val_if_fail(jid != NULL, NULL);
+
+	return g_strconcat(jid->node ? jid->node : "",
+	                   jid->node ? "@" : "",
+	                   jid->domain,
+	                   NULL);
+}
+
 JabberID *
 jabber_id_new(const char *str)
 {
--- a/libpurple/protocols/jabber/jutil.h	Mon Aug 31 03:29:48 2009 +0000
+++ b/libpurple/protocols/jabber/jutil.h	Mon Aug 31 03:39:34 2009 +0000
@@ -37,6 +37,7 @@
 
 char *jabber_get_resource(const char *jid);
 char *jabber_get_bare_jid(const char *jid);
+char *jabber_id_get_bare_jid(const JabberID *jid);
 
 const char *jabber_normalize(const PurpleAccount *account, const char *in);
 
--- a/libpurple/protocols/jabber/roster.c	Mon Aug 31 03:29:48 2009 +0000
+++ b/libpurple/protocols/jabber/roster.c	Mon Aug 31 03:39:34 2009 +0000
@@ -26,6 +26,7 @@
 #include "util.h"
 
 #include "buddy.h"
+#include "chat.h"
 #include "google.h"
 #include "presence.h"
 #include "roster.h"
@@ -336,6 +337,7 @@
 {
 	JabberStream *js = gc->proto_data;
 	char *who;
+	JabberID *jid;
 	JabberBuddy *jb;
 	JabberBuddyResource *jbr;
 	const char *name;
@@ -345,13 +347,39 @@
 		return;
 
 	name = purple_buddy_get_name(buddy);
-	if(!(who = jabber_get_bare_jid(name)))
+	jid = jabber_id_new(name);
+	if (jid == NULL) {
+		/* TODO: Remove the buddy from the list? */
 		return;
+	}
 
-	jb = jabber_buddy_find(js, name, FALSE);
+	/* Adding a chat room or a chat buddy to the roster is *not* supported. */
+	if (jabber_chat_find(js, jid->node, jid->domain) != NULL) {
+		/*
+		 * This is the same thing Bonjour does. If it causes problems, move
+		 * it to an idle callback.
+		 */
+		purple_debug_warning("jabber", "Cowardly refusing to add a MUC user "
+		                     "to your buddy list and removing the buddy. "
+		                     "Buddies can only be added by real (non-MUC) "
+		                     "JID\n");
+		purple_blist_remove_buddy(buddy);
+		jabber_id_free(jid);
+		return;
+	}
 
-	purple_debug_info("jabber", "jabber_roster_add_buddy(): Adding %s\n",
-	                  name);
+	who = jabber_id_get_bare_jid(jid);
+	if (jid->resource != NULL) {
+		/*
+		 * If the buddy name added contains a resource, strip that off and
+		 * rename the buddy.
+		 */
+		purple_blist_rename_buddy(buddy, who);
+	}
+
+	jb = jabber_buddy_find(js, who, FALSE);
+
+	purple_debug_info("jabber", "jabber_roster_add_buddy(): Adding %s\n", who);
 
 	jabber_roster_update(js, who, NULL);