 .../user_relationships_api.privatemsg.inc          |   43 +++++++++++++-------
 .../user_relationships_ui.admin.inc                |   12 +++++-
 2 files changed, 39 insertions(+), 16 deletions(-)

diff --git user_relationships_api/user_relationships_api.privatemsg.inc user_relationships_api/user_relationships_api.privatemsg.inc
index 9a6e0ed..500b8c2 100644
--- user_relationships_api/user_relationships_api.privatemsg.inc
+++ user_relationships_api/user_relationships_api.privatemsg.inc
@@ -5,26 +5,20 @@
  * @author mansspams http://drupal.org/user/293179
  * @author alex.k http://drupal.org/user/183217
  */
+
 /**
  * Fills new Privatemsg autocomplete To: field with friends only.
- *
  */
 function user_relationships_api_privatemsg_sql_autocomplete_alter(&$fragments, $search, $names) {
-  //#522078 killswitch for autocomplete
-  if (!variable_get('user_relationships_privatemsg_autocomplete_alter', 0)) {
+  global $user;
+  // Check if $author needs to be restricted.
+  if (!user_relationships_api_privatemsg_restrict($user)) {
     return;
   }
-
-  global $user;
-  
-  static $uniqueprefix = 0;
-  
-  $fragments['inner_join'][] = "INNER JOIN {user_relationships} ur_$uniqueprefix ON u.uid = ur_$uniqueprefix.requestee_id";
-  $fragments['where'][] = "ur_$uniqueprefix.approved = 1";
-  $fragments['where'][] = "ur_$uniqueprefix.requester_id = %d";
+  $fragments['inner_join'][] = "INNER JOIN {user_relationships} ur ON u.uid = ur.requestee_id";
+  $fragments['where'][] = "ur.approved = 1";
+  $fragments['where'][] = "ur.requester_id = %d";
   $fragments['query_args']['where'][] = $user->uid;
-  
-  $uniqueprefix++;
 }
 
 
@@ -33,10 +27,11 @@ function user_relationships_api_privatemsg_sql_autocomplete_alter(&$fragments, $
  * @see hook_privatemsg_block_message()
  */
 function user_relationships_api_privatemsg_block_message($author, $recipients) {
-  //#522078 admin killswitch
-  if (variable_get('user_relationships_restrict_privatemsg', 'all') == 'all') {
+  // Check if $author needs to be restricted.
+  if (!user_relationships_api_privatemsg_restrict($author)) {
     return;
   }
+
   $blocked = array();
   foreach ($recipients as $recipient) {
     //block if user is only receiving pm's from his relationships, and author is not one of them
@@ -54,3 +49,21 @@ function user_relationships_api_privatemsg_block_message($author, $recipients) {
   }
   return $blocked;
 }
+
+function user_relationships_api_privatemsg_restrict($author) {
+    //#522078 admin killswitch, always ignore this for user 1.
+  if (variable_get('user_relationships_restrict_privatemsg', 'all') == 'all' || $author->uid == 1) {
+    return FALSE;
+  }
+
+  $exlude_roles = array_filter(variable_get('user_relationships_privatemsg_role_exclusions', array()));
+  // First, make sure that we have a user object with roles, and then check if
+  // the users does have any of the exluded roles.
+  if (!empty($author->roles) || $author = user_load($author->uid)) {
+    $is_excluded = array_intersect(array_keys($author->roles), $exlude_roles);
+    if (!empty($is_excluded)) {
+      return FALSE;
+    }
+  }
+  return TRUE;
+}
diff --git user_relationships_ui/user_relationships_ui.admin.inc user_relationships_ui/user_relationships_ui.admin.inc
index b5114ea..8ca82d0 100644
--- user_relationships_ui/user_relationships_ui.admin.inc
+++ user_relationships_ui/user_relationships_ui.admin.inc
@@ -54,11 +54,11 @@ function user_relationships_ui_settings() {
       $form['privatemsg'] = array(
         '#type'   => 'fieldset',
         '#title'  => t('Private messages'),
+        '#description'    => t('Allow Privatemsg to work only between related users. Note that this setting does not affect user 1.'),
       );
       $form['privatemsg']['user_relationships_restrict_privatemsg'] = array(
         '#type'           => 'radios',
         '#title'          => t('Restrict Private Messaging to only related users'),
-        '#description'    => t('Allow Privatemsg to work only between related users'),
         '#default_value'  => variable_get('user_relationships_restrict_privatemsg', 'all'),
         '#options' => array(
           'all' => t('Allow sending messages to all users'),
@@ -72,6 +72,16 @@ function user_relationships_ui_settings() {
         '#description'    => t("Show only user's relationships when autocompleting the To: field."),
         '#default_value'  => variable_get('user_relationships_privatemsg_autocomplete_alter', 0),
       );
+      $form['privatemsg']['roles'] = array(
+        '#title' => t('Role exclusions'),
+        '#type' => 'fieldset',
+        '#description' => t('You may choose which roles are allowed to send message to all users, even if it is otherwise restricted.'),
+        'user_relationships_privatemsg_role_exclusions' => array(
+          '#type' => 'checkboxes',
+          '#options' => user_roles(TRUE),
+          '#default_value' => variable_get('user_relationships_privatemsg_role_exclusions', array()),
+        ),
+      );
   }
   $form['positioning'] = array(
     '#type'   => 'fieldset',
