Index: doc/user-guide/commands.xml
===================================================================
--- doc/user-guide/commands.xml	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ doc/user-guide/commands.xml	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -1432,9 +1432,9 @@
 	<bitlbee-command name="blist">
 		<syntax>blist [all|online|offline|away]</syntax>
-		<short-description>List all the buddies in your contact list</short-description>
-
-		<description>
-			<para>
-				You can get a better readable buddy list using the <emphasis>blist</emphasis> command. If you want a complete list (including the offline users) you can use the <emphasis>all</emphasis> argument.
+		<short-description>List all the buddies in the current channel</short-description>
+
+		<description>
+			<para>
+				You can get a more readable buddy list using the <emphasis>blist</emphasis> command. If you want a complete list (including the offline users) you can use the <emphasis>all</emphasis> argument.
 			</para>
 		</description>
Index: irc.h
===================================================================
--- irc.h	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ irc.h	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -258,4 +258,5 @@
 void irc_channel_update_ops( irc_channel_t *ic, char *value );
 char *set_eval_irc_channel_ops( struct set *set, char *value );
+gboolean irc_channel_wants_user( irc_channel_t *ic, irc_user_t *iu );
 
 /* irc_commands.c */
Index: irc_channel.c
===================================================================
--- irc_channel.c	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ irc_channel.c	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -726,4 +726,29 @@
 }
 
+/* Figure out if a channel is supposed to have the user, assuming s/he is
+   online or otherwise also selected by the show_users setting. Only works
+   for control channels, but does *not* check if this channel is of that
+   type. Beware! */
+gboolean irc_channel_wants_user( irc_channel_t *ic, irc_user_t *iu )
+{
+	struct irc_control_channel *icc = ic->data;
+	
+	if( iu->bu == NULL )
+		return FALSE;
+	
+	switch( icc->type )
+	{
+	case IRC_CC_TYPE_GROUP:
+		return iu->bu->group == icc->group;
+	case IRC_CC_TYPE_ACCOUNT:
+		return iu->bu->ic->acc == icc->account;
+	case IRC_CC_TYPE_PROTOCOL:
+		return iu->bu->ic->acc->prpl == icc->protocol;
+	case IRC_CC_TYPE_DEFAULT:
+	default:
+		return TRUE;
+	}
+}
+
 static gboolean control_channel_free( irc_channel_t *ic )
 {
Index: irc_im.c
===================================================================
--- irc_im.c	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ irc_im.c	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -141,7 +141,5 @@
 void bee_irc_channel_update( irc_t *irc, irc_channel_t *ic, irc_user_t *iu )
 {
-	struct irc_control_channel *icc;
 	GSList *l;
-	gboolean match = FALSE;
 	
 	if( ic == NULL )
@@ -168,16 +166,5 @@
 	}
 	
-	icc = ic->data;
-	
-	if( icc->type == IRC_CC_TYPE_DEFAULT )
-		match = TRUE;
-	else if( icc->type == IRC_CC_TYPE_GROUP )
-		match = iu->bu->group == icc->group;
-	else if( icc->type == IRC_CC_TYPE_ACCOUNT )
-		match = iu->bu->ic->acc == icc->account;
-	else if( icc->type == IRC_CC_TYPE_PROTOCOL )
-		match = iu->bu->ic->acc->prpl == icc->protocol;
-	
-	if( !match )
+	if( !irc_channel_wants_user( ic, iu ) )
 	{
 		irc_channel_del_user( ic, iu, IRC_CDU_PART, NULL );
@@ -185,4 +172,5 @@
 	else
 	{
+		struct irc_control_channel *icc = ic->data;
 		int mode = 0;
 		
Index: irc_send.c
===================================================================
--- irc_send.c	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ irc_send.c	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -44,7 +44,8 @@
 	irc_send_num( irc,   3, ":%s", IRCD_INFO );
 	irc_send_num( irc,   4, "%s %s %s %s", irc->root->host, BITLBEE_VERSION, UMODES UMODES_PRIV, CMODES );
-	irc_send_num( irc,   5, "PREFIX=(ov)@+ CHANTYPES=%s CHANMODES=,,,%s NICKLEN=%d NETWORK=BitlBee "
-	                        "CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 :are supported by this server",
-	                        CTYPES, CMODES, MAX_NICK_LENGTH - 1 );
+	irc_send_num( irc,   5, "PREFIX=(ohv)@%%+ CHANTYPES=%s CHANMODES=,,,%s NICKLEN=%d CHANNELLEN=%d "
+	                        "NETWORK=BitlBee SAFELIST CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 "
+	                        ":are supported by this server",
+	                        CTYPES, CMODES, MAX_NICK_LENGTH - 1, MAX_NICK_LENGTH - 1 );
 	irc_send_motd( irc );
 }
Index: irc_user.c
===================================================================
--- irc_user.c	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ irc_user.c	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -211,5 +211,9 @@
 	
 	for( l = iu->irc->channels; l; l = l->next )
-		send_quit |= irc_channel_del_user( (irc_channel_t*) l->data, iu, IRC_CDU_SILENT, NULL );
+	{
+		irc_channel_t *ic = l->data;
+		send_quit |= irc_channel_del_user( ic, iu, IRC_CDU_SILENT, NULL ) &&
+		             ( ic->flags & IRC_CHANNEL_JOINED );
+	}
 	
 	if( send_quit )
Index: lib/misc.c
===================================================================
--- lib/misc.c	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ lib/misc.c	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -505,8 +505,9 @@
 }
 
-struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain )
+struct ns_srv_reply **srv_lookup( char *service, char *protocol, char *domain )
 {	
+	struct ns_srv_reply **replies = NULL;
+#ifdef HAVE_RESOLV_A
 	struct ns_srv_reply *reply = NULL;
-#ifdef HAVE_RESOLV_A
 	char name[1024];
 	unsigned char querybuf[1024];
@@ -514,5 +515,5 @@
 	ns_msg nsh;
 	ns_rr rr;
-	int i, len, size;
+	int i, n, len, size;
 	
 	g_snprintf( name, sizeof( name ), "_%s._%s.%s", service, protocol, domain );
@@ -524,35 +525,54 @@
 		return NULL;
 	
-	if( ns_parserr( &nsh, ns_s_an, 0, &rr ) != 0 )
-		return NULL;
-	
-	size = ns_rr_rdlen( rr );
-	buf = ns_rr_rdata( rr );
-	
-	len = 0;
-	for( i = 6; i < size && buf[i]; i += buf[i] + 1 )
-		len += buf[i] + 1;
-	
-	if( i > size )
-		return NULL;
-	
-	reply = g_malloc( sizeof( struct ns_srv_reply ) + len );
-	memcpy( reply->name, buf + 7, len );
-	
-	for( i = buf[6]; i < len && buf[7+i]; i += buf[7+i] + 1 )
-		reply->name[i] = '.';
-	
-	if( i > len )
-	{
-		g_free( reply );
-		return NULL;
-	}
-	
-	reply->prio = ( buf[0] << 8 ) | buf[1];
-	reply->weight = ( buf[2] << 8 ) | buf[3];
-	reply->port = ( buf[4] << 8 ) | buf[5];
+	n = 0;
+	while( ns_parserr( &nsh, ns_s_an, n, &rr ) == 0 )
+	{
+		size = ns_rr_rdlen( rr );
+		buf = ns_rr_rdata( rr );
+		
+		len = 0;
+		for( i = 6; i < size && buf[i]; i += buf[i] + 1 )
+			len += buf[i] + 1;
+		
+		if( i > size )
+			break;
+		
+		reply = g_malloc( sizeof( struct ns_srv_reply ) + len );
+		memcpy( reply->name, buf + 7, len );
+		
+		for( i = buf[6]; i < len && buf[7+i]; i += buf[7+i] + 1 )
+			reply->name[i] = '.';
+		
+		if( i > len )
+		{
+			g_free( reply );
+			break;
+		}
+		
+		reply->prio = ( buf[0] << 8 ) | buf[1];
+		reply->weight = ( buf[2] << 8 ) | buf[3];
+		reply->port = ( buf[4] << 8 ) | buf[5];
+		
+		n ++;
+		replies = g_renew( struct ns_srv_reply *, replies, n + 1 );
+		replies[n-1] = reply;
+	}
+	if( replies )
+		replies[n] = NULL;
 #endif
 	
-	return reply;
+	return replies;
+}
+
+void srv_free( struct ns_srv_reply **srv )
+{
+	int i;
+	
+	if( srv == NULL )
+		return;
+	
+	for( i = 0; srv[i]; i ++ )
+		g_free( srv[i] );
+	g_free( srv );
 }
 
Index: lib/misc.h
===================================================================
--- lib/misc.h	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ lib/misc.h	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -61,5 +61,6 @@
 G_MODULE_EXPORT int bool2int( char *value );
 
-G_MODULE_EXPORT struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain );
+G_MODULE_EXPORT struct ns_srv_reply **srv_lookup( char *service, char *protocol, char *domain );
+G_MODULE_EXPORT void srv_free( struct ns_srv_reply **srv );
 
 G_MODULE_EXPORT char *word_wrap( const char *msg, int line_len );
Index: protocols/jabber/jabber.c
===================================================================
--- protocols/jabber/jabber.c	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ protocols/jabber/jabber.c	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -96,5 +96,5 @@
 	struct im_connection *ic = imcb_new( acc );
 	struct jabber_data *jd = g_new0( struct jabber_data, 1 );
-	struct ns_srv_reply *srv = NULL;
+	struct ns_srv_reply **srvl = NULL, *srv = NULL;
 	char *connect_to, *s;
 	int i;
@@ -196,7 +196,17 @@
 	if( acc->server && *acc->server )
 		connect_to = acc->server;
-	else if( ( srv = srv_lookup( "xmpp-client", "tcp", jd->server ) ) ||
-		 ( srv = srv_lookup( "jabber-client", "tcp", jd->server ) ) )
+	else if( ( srvl = srv_lookup( "xmpp-client", "tcp", jd->server ) ) ||
+	         ( srvl = srv_lookup( "jabber-client", "tcp", jd->server ) ) )
+	{
+		/* Find the lowest-priority one. These usually come
+		   back in random/shuffled order. Not looking at
+		   weights etc for now. */
+		srv = *srvl;
+		for( i = 1; srvl[i]; i ++ )
+			if( srvl[i]->prio < srv->prio )
+				srv = srvl[i];
+		
 		connect_to = srv->name;
+	}
 	else
 		connect_to = jd->server;
@@ -227,5 +237,5 @@
 		jd->fd = proxy_connect( connect_to, srv ? srv->port : set_getint( &acc->set, "port" ), jabber_connected_plain, ic );
 	}
-	g_free( srv );
+	srv_free( srvl );
 	
 	if( jd->fd == -1 )
Index: protocols/twitter/twitter.c
===================================================================
--- protocols/twitter/twitter.c	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ protocols/twitter/twitter.c	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -257,5 +257,5 @@
 	
 	// Set the status to logged out.
-	ic->flags = 0;
+	ic->flags &= ~ OPT_LOGGED_IN;
 
 	// Remove the main_loop function from the function queue.
Index: protocols/twitter/twitter_lib.c
===================================================================
--- protocols/twitter/twitter_lib.c	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ protocols/twitter/twitter_lib.c	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -357,4 +357,9 @@
 }
 
+#ifdef __GLIBC__
+#define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S %z %Y"
+#else
+#define TWITTER_TIME_FORMAT "%a %b %d %H:%M:%S +0000 %Y"
+#endif
 
 /**
@@ -393,5 +398,5 @@
 			   this field. :-( Also assumes the timezone used
 			   is UTC since C time handling functions suck. */
-			if( strptime( child->text, "%a %b %d %H:%M:%S %z %Y", &parsed ) != NULL )
+			if( strptime( child->text, TWITTER_TIME_FORMAT, &parsed ) != NULL )
 				txs->created_at = mktime_utc( &parsed );
 		}
Index: root_commands.c
===================================================================
--- root_commands.c	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ root_commands.c	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -1008,4 +1008,7 @@
 	irc_usermsg( irc, format, "Nick", "Handle/Account", "Status" );
 	
+	if( strcmp( set_getstr( &irc->root->last_channel->set, "type" ), "control" ) != 0 )
+		irc->root->last_channel = NULL;
+	
 	for( l = irc->users; l; l = l->next )
 	{
@@ -1013,5 +1016,6 @@
 		bee_user_t *bu = iu->bu;
 		
-		if( !bu || ( bu->flags & ( BEE_USER_ONLINE | BEE_USER_AWAY ) ) != BEE_USER_ONLINE )
+		if( !bu || ( irc->root->last_channel && !irc_channel_wants_user( irc->root->last_channel, iu ) ) ||
+		    ( bu->flags & ( BEE_USER_ONLINE | BEE_USER_AWAY ) ) != BEE_USER_ONLINE )
 			continue;
 		
@@ -1035,5 +1039,6 @@
 		bee_user_t *bu = iu->bu;
 		
-		if( !bu || !( bu->flags & BEE_USER_ONLINE ) || !( bu->flags & BEE_USER_AWAY ) )
+		if( !bu || ( irc->root->last_channel && !irc_channel_wants_user( irc->root->last_channel, iu ) ) ||
+		    !( bu->flags & BEE_USER_ONLINE ) || !( bu->flags & BEE_USER_AWAY ) )
 			continue;
 		
@@ -1051,5 +1056,6 @@
 		bee_user_t *bu = iu->bu;
 		
-		if( !bu || bu->flags & BEE_USER_ONLINE )
+		if( !bu || ( irc->root->last_channel && !irc_channel_wants_user( irc->root->last_channel, iu ) ) ||
+		    bu->flags & BEE_USER_ONLINE )
 			continue;
 		
Index: sock.h
===================================================================
--- sock.h	(revision 584867592546f43f857645e02169d135f0df25e8)
+++ sock.h	(revision 07874bef9e9c4e6ace44e4d0605ce1aec89cad74)
@@ -13,4 +13,6 @@
 #ifndef EVENTS_LIBEVENT
 #define closesocket(a) close(a)
+#else
+void closesocket( int fd );
 #endif
 #else
