Index: protocols/jabber/jabber.c
===================================================================
--- protocols/jabber/jabber.c	(revision 5f8ab6a9adf09ea7c07f728227bdb6d3953588f1)
+++ protocols/jabber/jabber.c	(revision a6b2f13e38e75e7bca00a1e6c1963783f244c0b2)
@@ -57,4 +57,6 @@
 	set_t *s;
 	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] );
@@ -307,5 +309,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 );
@@ -352,15 +354,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 5f8ab6a9adf09ea7c07f728227bdb6d3953588f1)
+++ protocols/jabber/jabber.h	(revision a6b2f13e38e75e7bca00a1e6c1963783f244c0b2)
@@ -108,4 +108,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
 {
@@ -121,5 +128,5 @@
 	char *away_message;
 	
-	time_t last_act;
+	time_t last_msg;
 	jabber_buddy_flags_t flags;
 	
@@ -209,4 +216,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 5f8ab6a9adf09ea7c07f728227bdb6d3953588f1)
+++ protocols/jabber/jabber_util.c	(revision a6b2f13e38e75e7bca00a1e6c1963783f244c0b2)
@@ -4,5 +4,5 @@
 *  Jabber module - Misc. stuff                                              *
 *                                                                           *
-*  Copyright 2006 Wilmer van der Gaast <wilmer@gaast.net>                   *
+*  Copyright 2006-2010 Wilmer van der Gaast <wilmer@gaast.net>              
 *                                                                           *
 *  This program is free software; you can redistribute it and/or modify     *
@@ -345,4 +345,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: */
@@ -379,8 +384,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;
+		}
 	}
 	
@@ -408,5 +418,5 @@
 {
 	struct jabber_data *jd = ic->proto_data;
-	struct jabber_buddy *bud;
+	struct jabber_buddy *bud, *head;
 	char *s, *jid;
 	
@@ -420,4 +430,9 @@
 		if( ( bud = g_hash_table_lookup( jd->buddies, jid ) ) )
 		{
+			bare_exists = 1;
+			
+			if( bud->next )
+				bud = bud->next;
+			
 			/* Just return the first one for this bare JID. */
 			if( flags & GET_BUDDY_FIRST )
@@ -441,14 +456,7 @@
 					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 ) ) )
+		    ( bare_exists || imcb_find_buddy( ic, jid ) ) )
 		{
 			*s = '/';
@@ -464,5 +472,6 @@
 		char *set;
 		
-		bud = g_hash_table_lookup( jd->buddies, jid );
+		head = g_hash_table_lookup( jd->buddies, jid );
+		bud = ( head && head->next ) ? head->next : head;
 		
 		g_free( jid );
@@ -481,4 +490,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;
@@ -487,5 +498,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;
 		}
@@ -493,8 +504,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;
 	}
 }
@@ -538,5 +556,5 @@
 {
 	struct jabber_data *jd = ic->proto_data;
-	struct jabber_buddy *bud, *prev, *bi;
+	struct jabber_buddy *bud, *prev = NULL, *bi;
 	char *s, *full_jid;
 	
@@ -548,4 +566,7 @@
 	if( ( bud = g_hash_table_lookup( jd->buddies, full_jid ) ) )
 	{
+		if( bud->next )
+			bud = (prev=bud)->next;
+		
 		/* If there's only one item in the list (and if the resource
 		   matches), removing it is simple. (And the hash reference
@@ -555,14 +576,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 )
@@ -575,5 +587,5 @@
 		else
 		{
-			for( bi = bud, prev = NULL; bi; bi = (prev=bi)->next )
+			for( bi = bud; bi; bi = (prev=bi)->next )
 				if( strcmp( bi->resource, s + 1 ) == 0 )
 					break;
@@ -586,6 +598,5 @@
 					prev->next = bi->next;
 				else
-					/* The hash table should point at the second
-					   item, because we're removing the first. */
+					/* Don't think this should ever happen anymore. */
 					g_hash_table_replace( jd->buddies, bi->bare_jid, bi->next );
 				
@@ -656,8 +667,7 @@
 time_t jabber_get_timestamp( struct xt_node *xt )
 {
-	struct tm tp, utc;
 	struct xt_node *c;
-	time_t res, tres;
 	char *s = NULL;
+	struct tm tp;
 	
 	for( c = xt->children; ( c = xt_find_node( c, "x" ) ); c = c->next )
@@ -677,28 +687,6 @@
 	tp.tm_year -= 1900;
 	tp.tm_mon --;
-	tp.tm_isdst = -1; /* GRRRRRRRRRRR */
-	
-	res = mktime( &tp );
-	/* Problem is, mktime() just gave us the GMT timestamp for the
-	   given local time... While the given time WAS NOT local. So
-	   we should fix this now.
-	
-	   Now I could choose between messing with environment variables
-	   (kludgy) or using timegm() (not portable)... Or doing the
-	   following, which I actually prefer... */
-	gmtime_r( &res, &utc );
-	utc.tm_isdst = -1; /* Once more: GRRRRRRRRRRRRRRRRRR!!! */
-	if( utc.tm_hour == tp.tm_hour && utc.tm_min == tp.tm_min )
-		/* Sweet! We're in UTC right now... */
-		return res;
-	
-	tres = mktime( &utc );
-	res += res - tres;
-	
-	/* Yes, this is a hack. And it will go wrong around DST changes.
-	   BUT this is more likely to be threadsafe than messing with
-	   environment variables, and possibly more portable... */
-	
-	return res;
+	
+	return mktime_utc( &tp );
 }
 
Index: protocols/jabber/message.c
===================================================================
--- protocols/jabber/message.c	(revision 5f8ab6a9adf09ea7c07f728227bdb6d3953588f1)
+++ protocols/jabber/message.c	(revision a6b2f13e38e75e7bca00a1e6c1963783f244c0b2)
@@ -71,5 +71,5 @@
 			if( bud )
 			{
-				bud->last_act = time( NULL );
+				bud->last_msg = time( NULL );
 				from = bud->ext_jid ? : bud->bare_jid;
 			}
@@ -80,6 +80,6 @@
 		if( type && strcmp( type, "headline" ) == 0 )
 		{
-			c = xt_find_node( node->children, "subject" );
-			g_string_append_printf( fullmsg, "Headline: %s\n", c && c->text_len > 0 ? c->text : "" );
+			if( ( c = xt_find_node( node->children, "subject" ) ) && c->text_len > 0 )
+				g_string_append_printf( fullmsg, "Headline: %s\n", c->text );
 			
 			/* <x xmlns="jabber:x:oob"><url>http://....</url></x> can contain a URL, it seems. */
Index: protocols/jabber/presence.c
===================================================================
--- protocols/jabber/presence.c	(revision 5f8ab6a9adf09ea7c07f728227bdb6d3953588f1)
+++ protocols/jabber/presence.c	(revision a6b2f13e38e75e7bca00a1e6c1963783f244c0b2)
@@ -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 );
 		}
 		
