This code snippet removes the ‘send private message button’ on a Buddypress members profile, unless the logged in users are friends.

To clarify; a Buddypress user can only see the button on another members profile by being friends with them first.

This could be a step towards eliminating potential spam and focus dialogues out in the open for easier moderation.

bp-custom.php

add_filter('bp_get_send_message_button',
function ($array)
	{
	if (friends_check_friendship(bp_loggedin_user_id() , bp_displayed_user_id()))
		{
		return $array;
		}
	  else
		{
		return '';
		}
	});
add_filter('bp_get_send_public_message_button',
function ($r)
	{
	if (friends_check_friendship(bp_loggedin_user_id() , bp_displayed_user_id()))
		{
		return $r;
		}
	  else
		{
		$r['component'] = '';
		return $r;
		}
	});