Index: protocols/msn/msn.c
===================================================================
--- protocols/msn/msn.c	(revision bb839e8ae5b6228f9dcd8dda96b4e3ac5c0f63ba)
+++ protocols/msn/msn.c	(revision aa7ce1b3dd961e948e907460bd42bf1592ea1717)
@@ -31,16 +31,12 @@
 GSList *msn_switchboards;
 
-static char *msn_set_display_name( set_t *set, char *value );
+static char *set_eval_display_name( set_t *set, char *value );
 
 static void msn_init( account_t *acc )
 {
-	set_t *s;
-	
-	s = set_add( &acc->set, "display_name", NULL, msn_set_display_name, acc );
-	s->flags |= ACC_SET_NOSAVE | ACC_SET_ONLINE_ONLY;
-
-	s = set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc );
-	
-	s = set_add( &acc->set, "switchboard_keepalives", "false", set_eval_bool, acc );
+	set_add( &acc->set, "display_name", NULL, set_eval_display_name, acc );
+	set_add( &acc->set, "local_display_name", "false", set_eval_bool, acc );
+	set_add( &acc->set, "mail_notifications", "false", set_eval_bool, acc );
+	set_add( &acc->set, "switchboard_keepalives", "false", set_eval_bool, acc );
 }
 
@@ -169,5 +165,5 @@
 static void msn_set_my_name( struct im_connection *ic, char *info )
 {
-	msn_set_display_name( set_find( &ic->acc->set, "display_name" ), info );
+	msn_set_display_name( ic, info );
 }
 
@@ -285,16 +281,12 @@
 }
 
-static char *msn_set_display_name( set_t *set, char *value )
+static char *set_eval_display_name( set_t *set, char *value )
 {
 	account_t *acc = set->data;
 	struct im_connection *ic = acc->ic;
-	struct msn_data *md;
-	char buf[1024], *fn;
-	
-	/* Double-check. */
+	
+	/* Allow any name if we're offline. */
 	if( ic == NULL )
-		return NULL;
-	
-	md = ic->proto_data;
+		return value;
 	
 	if( strlen( value ) > 129 )
@@ -303,15 +295,9 @@
 		return NULL;
 	}
-	
-	fn = msn_http_encode( value );
-	
-	g_snprintf( buf, sizeof( buf ), "REA %d %s %s\r\n", ++md->trId, ic->acc->user, fn );
-	msn_write( ic, buf, strlen( buf ) );
-	g_free( fn );
 	
 	/* Returning NULL would be better, because the server still has to
 	   confirm the name change. However, it looks a bit confusing to the
 	   user. */
-	return value;
+	return msn_set_display_name( ic, value ) ? value : NULL;
 }
 
Index: protocols/msn/msn.h
===================================================================
--- protocols/msn/msn.h	(revision bb839e8ae5b6228f9dcd8dda96b4e3ac5c0f63ba)
+++ protocols/msn/msn.h	(revision aa7ce1b3dd961e948e907460bd42bf1592ea1717)
@@ -167,4 +167,5 @@
 char *msn_http_encode( const char *input );
 void msn_msgq_purge( struct im_connection *ic, GSList **list );
+gboolean msn_set_display_name( struct im_connection *ic, const char *rawname );
 
 /* tables.c */
Index: protocols/msn/msn_util.c
===================================================================
--- protocols/msn/msn_util.c	(revision bb839e8ae5b6228f9dcd8dda96b4e3ac5c0f63ba)
+++ protocols/msn/msn_util.c	(revision aa7ce1b3dd961e948e907460bd42bf1592ea1717)
@@ -38,8 +38,8 @@
 		imcb_error( ic, "Short write() to main server" );
 		imc_logout( ic, TRUE );
-		return( 0 );
-	}
-	
-	return( 1 );
+		return 0;
+	}
+	
+	return 1;
 }
 
@@ -377,2 +377,14 @@
 	g_string_free( ret, TRUE );
 }
+
+gboolean msn_set_display_name( struct im_connection *ic, const char *rawname )
+{
+	char *fn = msn_http_encode( rawname );
+	struct msn_data *md = ic->proto_data;
+	char buf[1024];
+	
+	g_snprintf( buf, sizeof( buf ), "REA %d %s %s\r\n", ++md->trId, ic->acc->user, fn );
+	g_free( fn );
+	
+	return msn_write( ic, buf, strlen( buf ) ) != 0;
+}
Index: protocols/msn/ns.c
===================================================================
--- protocols/msn/ns.c	(revision bb839e8ae5b6228f9dcd8dda96b4e3ac5c0f63ba)
+++ protocols/msn/ns.c	(revision aa7ce1b3dd961e948e907460bd42bf1592ea1717)
@@ -35,4 +35,5 @@
 
 static void msn_auth_got_passport_token( struct msn_auth_data *mad );
+static gboolean msn_ns_got_display_name( struct im_connection *ic, char *name );
 
 gboolean msn_ns_connected( gpointer data, gint source, b_input_condition cond )
@@ -231,23 +232,8 @@
 		else if( num_parts >= 7 && strcmp( cmd[2], "OK" ) == 0 )
 		{
-			set_t *s;
-			
 			if( num_parts == 7 )
-			{
-				http_decode( cmd[4] );
-				
-				strncpy( ic->displayname, cmd[4], sizeof( ic->displayname ) );
-				ic->displayname[sizeof(ic->displayname)-1] = 0;
-				
-				if( ( s = set_find( &ic->acc->set, "display_name" ) ) )
-				{
-					g_free( s->value );
-					s->value = g_strdup( cmd[4] );
-				}
-			}
+				msn_ns_got_display_name( ic, cmd[4] );
 			else
-			{
 				imcb_log( ic, "Warning: Friendly name in server response was corrupted" );
-			}
 			
 			imcb_log( ic, "Authenticated, getting buddy list" );
@@ -573,4 +559,7 @@
 		return( 0 );
 	}
+#if 0
+	/* Discard this one completely for now since I don't care about the ack
+	   and since MSN servers can apparently screw up the formatting. */
 	else if( strcmp( cmd[0], "REA" ) == 0 )
 	{
@@ -603,4 +592,5 @@
 		}
 	}
+#endif
 	else if( strcmp( cmd[0], "IPG" ) == 0 )
 	{
@@ -752,2 +742,47 @@
 	}
 }
+
+static gboolean msn_ns_got_display_name( struct im_connection *ic, char *name )
+{
+	set_t *s;
+	
+	if( ( s = set_find( &ic->acc->set, "display_name" ) ) == NULL )
+		return FALSE; /* Shouldn't happen.. */
+	
+	http_decode( name );
+	
+	if( s->value && strcmp( s->value, name ) == 0 )
+	{
+		return TRUE;
+		/* The names match, nothing to worry about. */
+	}
+	else if( s->value != NULL &&
+	         ( strcmp( name, ic->acc->user ) == 0 ||
+	           set_getbool( &ic->acc->set, "local_display_name" ) ) )
+	{
+		/* The server thinks our display name is our e-mail address
+		   which is probably wrong, or the user *wants* us to do this:
+		   Always use the locally set display_name. */
+		return msn_set_display_name( ic, s->value );
+	}
+	else
+	{
+		if( s->value && *s->value )
+			imcb_log( ic, "BitlBee thinks your display name is `%s' but "
+			              "the MSN server says it's `%s'. Using the MSN "
+			              "server's name. Set local_display_name to true "
+			              "to use the local name.", s->value, name );
+		
+		if( g_utf8_validate( name, -1, NULL ) )
+		{
+			g_free( s->value );
+			s->value = g_strdup( name );
+		}
+		else
+		{
+			imcb_log( ic, "Warning: Friendly name in server response was corrupted" );
+		}
+		
+		return TRUE;
+	}
+}
