Index: protocols/jabber/jabber.c
===================================================================
--- protocols/jabber/jabber.c	(revision 4cf80bb798a4159f42a904cb830b99c87a0ca8e0)
+++ protocols/jabber/jabber.c	(revision 76c85b4c79d533ca7a780df381ccda5b9ab2934c)
@@ -58,4 +58,6 @@
 	char str[16];
 	
+	s = set_add( &acc->set, "activity_timeout", "600", set_eval_int, acc );
+	
 	g_snprintf( str, sizeof( str ), "%d", jabber_port_list[0] );
 	s = set_add( &acc->set, "port", str, set_eval_int, acc );
@@ -67,5 +69,5 @@
 	s->flags |= ACC_SET_OFFLINE_ONLY;
 	
-	s = set_add( &acc->set, "resource_select", "priority", NULL, acc );
+	s = set_add( &acc->set, "resource_select", "activity", NULL, acc );
 	
 	s = set_add( &acc->set, "server", NULL, set_eval_account, acc );
@@ -305,5 +307,5 @@
 		bud = jabber_buddy_by_ext_jid( ic, who, 0 );
 	else
-		bud = jabber_buddy_by_jid( ic, who, 0 );
+		bud = jabber_buddy_by_jid( ic, who, GET_BUDDY_BARE_OK );
 	
 	node = xt_new_node( "body", message, NULL );
@@ -350,15 +352,7 @@
 static void jabber_get_info( struct im_connection *ic, char *who )
 {
-	struct jabber_data *jd = ic->proto_data;
 	struct jabber_buddy *bud;
 	
-	if( strchr( who, '/' ) )
-		bud = jabber_buddy_by_jid( ic, who, 0 );
-	else
-	{
-		char *s = jabber_normalize( who );
-		bud = g_hash_table_lookup( jd->buddies, s );
-		g_free( s );
-	}
+	bud = jabber_buddy_by_jid( ic, who, GET_BUDDY_FIRST );
 	
 	while( bud )
Index: protocols/jabber/jabber.h
===================================================================
--- protocols/jabber/jabber.h	(revision 4cf80bb798a4159f42a904cb830b99c87a0ca8e0)
+++ protocols/jabber/jabber.h	(revision 76c85b4c79d533ca7a780df381ccda5b9ab2934c)
@@ -107,4 +107,11 @@
 };
 
+/* Somewhat messy data structure: We have a hash table with the bare JID as
+   the key and the head of a struct jabber_buddy list as the value. The head
+   is always a bare JID. If the JID has other resources (often the case,
+   except for some transports that don't support multiple resources), those
+   follow. In that case, the bare JID at the beginning doesn't actually
+   refer to a real session and should only be used for operations that
+   support incomplete JIDs. */
 struct jabber_buddy
 {
@@ -120,5 +127,5 @@
 	char *away_message;
 	
-	time_t last_act;
+	time_t last_msg;
 	jabber_buddy_flags_t flags;
 	
@@ -208,4 +215,6 @@
 	GET_BUDDY_EXACT = 2,	/* Get an exact match (only makes sense with bare JIDs). */
 	GET_BUDDY_FIRST = 4,	/* No selection, simply get the first resource for this JID. */
+	GET_BUDDY_BARE = 8,	/* Get the bare version of the JID (possibly inexistent). */
+	GET_BUDDY_BARE_OK = 16,	/* Allow returning a bare JID if that seems better. */
 } get_buddy_flags_t;
 
Index: protocols/jabber/jabber_util.c
===================================================================
--- protocols/jabber/jabber_util.c	(revision 4cf80bb798a4159f42a904cb830b99c87a0ca8e0)
+++ protocols/jabber/jabber_util.c	(revision 76c85b4c79d533ca7a780df381ccda5b9ab2934c)
@@ -340,4 +340,9 @@
 	if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
 	{
+		/* The first entry is always a bare JID. If there are more, we
+		   should ignore the first one here. */
+		if( bud->next )
+			bud = bud->next;
+		
 		/* If this is a transport buddy or whatever, it can't have more
 		   than one instance, so this is always wrong: */
@@ -374,8 +379,13 @@
 	else
 	{
-		/* Keep in mind that full_jid currently isn't really
-		   a full JID... */
-		new->bare_jid = g_strdup( full_jid );
+		new->full_jid = new->bare_jid = g_strdup( full_jid );
 		g_hash_table_insert( jd->buddies, new->bare_jid, new );
+		
+		if( s )
+		{
+			new->next = g_new0( struct jabber_buddy, 1 );
+			new->next->bare_jid = new->bare_jid;
+			new = new->next;
+		}
 	}
 	
@@ -403,5 +413,5 @@
 {
 	struct jabber_data *jd = ic->proto_data;
-	struct jabber_buddy *bud;
+	struct jabber_buddy *bud, *head;
 	char *s, *jid;
 	
@@ -410,9 +420,10 @@
 	if( ( s = strchr( jid, '/' ) ) )
 	{
-		int bare_exists = 0;
-		
 		*s = 0;
 		if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) )
 		{
+			if( bud->next )
+				bud = bud->next;
+			
 			/* Just return the first one for this bare JID. */
 			if( flags & GET_BUDDY_FIRST )
@@ -436,14 +447,6 @@
 					break;
 		}
-		else
-		{
-			/* This variable tells the if down here that the bare
-			   JID already exists and we should feel free to add
-			   more resources, if the caller asked for that. */
-			bare_exists = 1;
-		}
-		
-		if( bud == NULL && ( flags & GET_BUDDY_CREAT ) &&
-		    ( !bare_exists || imcb_find_buddy( ic, jid ) ) )
+		
+		if( bud == NULL && ( flags & GET_BUDDY_CREAT ) && imcb_find_buddy( ic, jid ) )
 		{
 			*s = '/';
@@ -459,5 +462,6 @@
 		char *set;
 		
-		bud = g_hash_table_lookup( jd->buddies, jid );
+		head = g_hash_table_lookup( jd->buddies, jid );
+		bud = head->next ? head->next : head;
 		
 		g_free( jid );
@@ -476,4 +480,6 @@
 			/* Looks like the caller doesn't care about details. */
 			return bud;
+		else if( flags & GET_BUDDY_BARE )
+			return head;
 		
 		best_prio = best_time = bud;
@@ -482,5 +488,5 @@
 			if( bud->priority > best_prio->priority )
 				best_prio = bud;
-			if( bud->last_act > best_time->last_act )
+			if( bud->last_msg > best_time->last_msg )
 				best_time = bud;
 		}
@@ -488,8 +494,15 @@
 		if( ( set = set_getstr( &ic->acc->set, "resource_select" ) ) == NULL )
 			return NULL;
-		else if( strcmp( set, "activity" ) == 0 )
+		else if( strcmp( set, "priority" ) == 0 )
+			return best_prio;
+		else if( flags & GET_BUDDY_BARE_OK ) /* && strcmp( set, "activity" ) == 0 */
+		{
+			if( best_time->last_msg + set_getint( &ic->acc->set, "activity_timeout" ) >= time( NULL ) )
+				return best_time;
+			else
+				return head;
+		}
+		else
 			return best_time;
-		else /* if( strcmp( set, "priority" ) == 0 ) */
-			return best_prio;
 	}
 }
@@ -533,5 +546,5 @@
 {
 	struct jabber_data *jd = ic->proto_data;
-	struct jabber_buddy *bud, *prev, *bi;
+	struct jabber_buddy *head, *bud, *prev, *bi;
 	char *s, *full_jid;
 	
@@ -541,6 +554,8 @@
 		*s = 0;
 	
-	if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
-	{
+	if( ( head = g_hash_table_lookup( jd->buddies, full_jid ) ) )
+	{
+		bud = head->next ? head->next : head;
+		
 		/* If there's only one item in the list (and if the resource
 		   matches), removing it is simple. (And the hash reference
@@ -550,14 +565,5 @@
 		      ( bud->resource && s && strcmp( bud->resource, s + 1 ) == 0 ) ) )
 		{
-			g_hash_table_remove( jd->buddies, bud->bare_jid );
-			g_free( bud->bare_jid );
-			g_free( bud->ext_jid );
-			g_free( bud->full_jid );
-			g_free( bud->away_message );
-			g_free( bud );
-			
-			g_free( full_jid );
-			
-			return 1;
+			return jabber_buddy_remove_bare( ic, full_jid );
 		}
 		else if( s == NULL || bud->resource == NULL )
Index: protocols/jabber/message.c
===================================================================
--- protocols/jabber/message.c	(revision 4cf80bb798a4159f42a904cb830b99c87a0ca8e0)
+++ protocols/jabber/message.c	(revision 76c85b4c79d533ca7a780df381ccda5b9ab2934c)
@@ -71,5 +71,5 @@
 			if( bud )
 			{
-				bud->last_act = time( NULL );
+				bud->last_msg = time( NULL );
 				from = bud->ext_jid ? : bud->bare_jid;
 			}
Index: protocols/jabber/presence.c
===================================================================
--- protocols/jabber/presence.c	(revision 4cf80bb798a4159f42a904cb830b99c87a0ca8e0)
+++ protocols/jabber/presence.c	(revision 76c85b4c79d533ca7a780df381ccda5b9ab2934c)
@@ -68,7 +68,4 @@
 		{
 			bud->away_state = NULL;
-			/* Let's only set last_act if there's *no* away state,
-			   since it could be some auto-away thingy. */
-			bud->last_act = time( NULL );
 		}
 		
