Index: privatemsg.api.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/privatemsg.api.php,v
retrieving revision 1.1.2.5.2.8
diff -u -p -r1.1.2.5.2.8 privatemsg.api.php
--- privatemsg.api.php	31 Oct 2010 13:11:22 -0000	1.1.2.5.2.8
+++ privatemsg.api.php	22 Nov 2010 08:50:28 -0000
@@ -300,7 +300,7 @@ function hook_privatemsg_message_validat
   foreach ($message->recipients as $recipient) {
     if ($recipient->name == 'blocked user') {
       $_privatemsg_invalid_recipients[] = $recipient->uid;
-      $errors[] = t('%name has chosen to not recieve any more messages from you.', array('%name' => $recipient->name));
+      $errors[] = t('%name has chosen to not recieve any more messages from you.', array('%name' => privatemsg_recipient_format($recipient, array('plain' => TRUE))));
     }
   }
 }
Index: privatemsg.author-pane.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/privatemsg.author-pane.inc,v
retrieving revision 1.1.2.3.2.3
diff -u -p -r1.1.2.3.2.3 privatemsg.author-pane.inc
--- privatemsg.author-pane.inc	10 Aug 2010 19:57:09 -0000	1.1.2.3.2.3
+++ privatemsg.author-pane.inc	22 Nov 2010 08:50:28 -0000
@@ -17,13 +17,11 @@ function privatemsg_preprocess_author_pa
 
   global $user;
   $account = $variables['account'];
-  $account_id = $account->uid;
-  $account_name = $account->name;
 
   if ($account_id != $user->uid) {
     $url = privatemsg_get_link($account);
     if (!empty($url)) {
-      $variables['privatemsg'] = l(t('Send private message'), $url, array('attributes' => array('class' => array('author-privatemsg-icon'), 'title' => t('Send @name a private message', array('@name' => $account_name))), 'html' => TRUE));
+      $variables['privatemsg'] = l(t('Send private message'), $url, array('attributes' => array('class' => array('author-privatemsg-icon'), 'title' => t('Send @name a private message', array('@name' => privatemsg_recipient_format($account, array('plain' => TRUE))))), 'html' => TRUE));
     }
   }
 }
Index: privatemsg.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/privatemsg.module,v
retrieving revision 1.70.2.30.2.91.2.64.2.93
diff -u -p -r1.70.2.30.2.91.2.64.2.93 privatemsg.module
--- privatemsg.module	10 Nov 2010 15:49:58 -0000	1.70.2.30.2.91.2.64.2.93
+++ privatemsg.module	22 Nov 2010 08:50:28 -0000
@@ -896,7 +896,15 @@ function _privatemsg_parse_userstring($i
         $function = $module . '_privatemsg_name_lookup';
         $return = $function($string);
         if (isset($return) && is_array($return)) {
-          $matches += $return;
+         foreach ($return as $recipient) {
+           // Save recipients under their key to merge recipients which were
+           // loaded multiple times.
+           if (empty($recipient->type)) {
+             $recipient->type = 'user';
+             $recipient->recipient = $recipient->uid;
+           }
+           $matches[privatemsg_recipient_key($recipient)] = $recipient;
+         }
         }
       }
 
@@ -1335,7 +1343,7 @@ function privatemsg_privatemsg_block_mes
     if (privatemsg_is_disabled($recipient)) {
       $blocked[] = array(
         'recipient' => 'user_' . $recipient->uid,
-        'message' => t('%recipient has disabled private message receiving.', array('%recipient' => $recipient->name)),
+        'message' => t('%recipient has disabled private message receiving.', array('%recipient' => privatemsg_recipient_format($recipient, array('plain' => TRUE)))),
       );
     }
   }
@@ -1641,7 +1649,7 @@ function _privatemsg_validate_message(&$
       );
     }
     else {
-      $messages['error'][] = t('@user is not allowed to write messages.', array('@user' => $message->author->name));
+      $messages['error'][] = t('@user is not allowed to write messages.', array('@user' => privatemsg_recipient_format($message->author, array('plain' => TRUE))));
       return array(
          'success'  => FALSE,
          'messages'   => $messages,
@@ -1674,7 +1682,7 @@ function _privatemsg_validate_message(&$
       form_set_error('format', t('You are not allowed to use the specified format.'));
     }
     else {
-      $messages['error'][] = t('@user is not allowed to use the specified input format.', array('@user' => $message->author->name));
+      $messages['error'][] = t('@user is not allowed to use the specified input format.', array('@user' => privatemsg_recipient_format($message->author, array('plain' => TRUE))));
     }
   }
 
@@ -2763,11 +2771,11 @@ function privatemsg_tokens($type, $token
 
         // Default values for the chained tokens handled below.
         case 'author':
-          $replacements[$original] = $sanitize ? filter_xss($message->author->name) : $message->author->name;
+          $replacements[$original] = $sanitize ? filter_xss(privatemsg_recipient_format($message->author, array('plain' => TRUE))) : privatemsg_recipient_format($message->author, array('plain' => TRUE));
           break;
 
         case 'recipient':
-            $replacements[$original] = $sanitize ? filter_xss($recipient->name) : $recipient->name;
+            $replacements[$original] = $sanitize ? filter_xss(privatemsg_recipient_format($recipient, array('plain' => TRUE))) : privatemsg_recipient_format($recipient, array('plain' => TRUE));
           break;
 
         case 'timestamp':
Index: privatemsg.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/privatemsg.pages.inc,v
retrieving revision 1.1.2.32
diff -u -p -r1.1.2.32 privatemsg.pages.inc
--- privatemsg.pages.inc	21 Oct 2010 09:12:37 -0000	1.1.2.32
+++ privatemsg.pages.inc	22 Nov 2010 08:50:28 -0000
@@ -229,7 +229,9 @@ function privatemsg_new($form, &$form_st
   $body      = '';
 
   // convert recipients to array of user objects
+  $unique = FALSE;
   if (!empty($recipients) && is_string($recipients) || is_int($recipients)) {
+    $unique = TRUE;
     $recipients = _privatemsg_generate_user_array($recipients);
   }
   elseif (is_object($recipients)) {
@@ -270,7 +272,7 @@ function privatemsg_new($form, &$form_st
         continue;
       }
       $to[privatemsg_recipient_key($recipient)] = privatemsg_recipient_format($recipient);
-      $to_plain[privatemsg_recipient_key($recipient)] = privatemsg_recipient_format($recipient, array('plain' => TRUE));
+      $to_plain[privatemsg_recipient_key($recipient)] = privatemsg_recipient_format($recipient, array('plain' => TRUE, 'unique' => $unique));
     }
     else {
       // Recipient list contains blocked users.
Index: privatemsg.theme.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/privatemsg.theme.inc,v
retrieving revision 1.1.2.7.2.13
diff -u -p -r1.1.2.7.2.13 privatemsg.theme.inc
--- privatemsg.theme.inc	6 Oct 2010 19:41:08 -0000	1.1.2.7.2.13
+++ privatemsg.theme.inc	22 Nov 2010 08:50:28 -0000
@@ -263,7 +263,7 @@ function theme_privatemsg_username($vari
     $recipient->uid = $recipient->recipient;
   }
   if (!empty($options['plain'])) {
-    $name = $recipient->name;
+    $name = format_username($recipient);
     if (!empty($options['unique'])) {
       $name .= ' [user]';
     }
Index: pm_block_user/pm_block_user.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/pm_block_user/pm_block_user.module,v
retrieving revision 1.1.2.8.2.22
diff -u -p -r1.1.2.8.2.22 pm_block_user.module
--- pm_block_user/pm_block_user.module	31 Oct 2010 13:11:23 -0000	1.1.2.8.2.22
+++ pm_block_user/pm_block_user.module	22 Nov 2010 08:50:28 -0000
@@ -306,7 +306,7 @@ function pm_block_user_privatemsg_block_
     }
     $blocked[] = array(
       'recipient' => privatemsg_recipient_key($recipient),
-      'message' => t('%name has chosen to block messages from you.', array('%name' => $recipient->name))
+      'message' => t('%name has chosen to block messages from you.', array('%name' => privatemsg_recipient_format($recipient, array('plain' => TRUE))))
     );
   }
   return $blocked;
Index: pm_block_user/pm_block_user.pages.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/pm_block_user/pm_block_user.pages.inc,v
retrieving revision 1.1.2.6
diff -u -p -r1.1.2.6 pm_block_user.pages.inc
--- pm_block_user/pm_block_user.pages.inc	13 Oct 2010 15:30:11 -0000	1.1.2.6
+++ pm_block_user/pm_block_user.pages.inc	22 Nov 2010 08:50:28 -0000
@@ -11,15 +11,11 @@ function pm_block_user_form($form, $form
 
   $form['author'] = array(
     '#type' => 'value',
-    '#value' => $author->uid,
+    '#value' => $author,
   );
     $form['recipient'] = array(
     '#type' => 'value',
-    '#value' => $user->uid,
-  );
-  $form['author_name'] = array(
-    '#type' => 'value',
-    '#value' => $author->name,
+    '#value' => $user,
   );
   $form['destination'] = array(
     '#type' => 'value',
@@ -31,10 +27,10 @@ function pm_block_user_form($form, $form
       '#value' => 'unblock_user',
     );
     return confirm_form($form,
-      t('You have previously blocked "@author" from sending you any more messages. Are you sure you want to unblock this user?', array('@author' => $author->name)),
+      t('You have previously blocked "@author" from sending you any more messages. Are you sure you want to unblock this user?', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
       isset($_GET['destination']) ? $_GET['destination'] : 'messages/',
       '',
-      t('Unblock @author', array('@author' => $author->name)),
+      t('Unblock @author', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
       t('Cancel')
     );
   }
@@ -44,10 +40,10 @@ function pm_block_user_form($form, $form
       '#value' => 'block_user',
     );
     return confirm_form($form,
-      t('Are you sure you want to block "@author" from sending you any more messages?', array('@author' => $author->name)),
+      t('Are you sure you want to block "@author" from sending you any more messages?', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
       isset($_GET['destination']) ? $_GET['destination'] : 'messages/',
       '',
-      t('Block @author', array('@author' => $author->name)),
+      t('Block @author', array('@author' => privatemsg_recipient_format($author, array('plain' => TRUE)))),
       t('Cancel')
     );
   }
@@ -62,18 +58,18 @@ function pm_block_user_form_submit($form
       case 'block_user':
         db_insert('pm_block_user')
           ->fields(array(
-            'author' => $form_state['values']['author'],
-            'recipient' => $form_state['values']['recipient'],
+            'author' => $form_state['values']['author']->uid,
+            'recipient' => $form_state['values']['recipient']->uid,
           ))
           ->execute();
-        drupal_set_message(t('@author has been blocked from sending you any further messages.', array('@author' => $form_state['values']['author_name'])));
+        drupal_set_message(t('@author has been blocked from sending you any further messages.', array('@author' => privatemsg_recipient_format($form_state['values']['author'], array('plain' => TRUE)))));
       break;
       case 'unblock_user':
         db_delete('pm_block_user')
-          ->condition('author', $form_state['values']['author'])
-          ->condition('recipient', $form_state['values']['recipient'])
+          ->condition('author', $form_state['values']['author']->uid)
+          ->condition('recipient', $form_state['values']['recipient']->uid)
           ->execute();
-        drupal_set_message(t('@author is now allowed to send you new messages.', array('@author' => $form_state['values']['author_name'])));
+        drupal_set_message(t('@author is now allowed to send you new messages.', array('@author' => privatemsg_recipient_format($form_state['values']['author'], array('plain' => TRUE)))));
       break;
     }
   }
Index: privatemsg_filter/privatemsg_filter.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/privatemsg/privatemsg_filter/privatemsg_filter.module,v
retrieving revision 1.1.2.17.2.30
diff -u -p -r1.1.2.17.2.30 privatemsg_filter.module
--- privatemsg_filter/privatemsg_filter.module	6 Oct 2010 19:46:35 -0000	1.1.2.17.2.30
+++ privatemsg_filter/privatemsg_filter.module	22 Nov 2010 08:50:29 -0000
@@ -405,7 +405,7 @@ function privatemsg_filter_dropdown_set_
   if (isset($filter['author'])) {
     $string = '';
     foreach ($filter['author'] as $author) {
-      $string .= $author->name . ', ';
+      $string .= privatemsg_recipient_format($author, array('plain' => TRUE)) . ', ';
     }
     $form['filter']['author']['#default_value'] = $string;
   }
@@ -483,10 +483,10 @@ function privatemsg_filter_create_get_qu
   if (isset($filter['author']) && !empty($filter['author'])) {
     foreach ($filter['author'] as $author) {
       if (is_object($author) && isset($author->uid) && isset($author->name)) {
-        $query['author'][] = $author->name;
+        $query['author'][] = privatemsg_recipient_format($author, array('plain' => TRUE));
       }
       elseif (is_int($author) && $author_obj = _privatemsg_user_load($author)) {
-        $query['author'][] = $author_obj->name;
+        $query['author'][] = privatemsg_recipient_format($author, array('plain' => TRUE));
       }
     }
     if (isset($query['author'])) {
Index: privatemsg_realname/privatemsg_realname.info
===================================================================
RCS file: privatemsg_realname/privatemsg_realname.info
diff -N privatemsg_realname/privatemsg_realname.info
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ privatemsg_realname/privatemsg_realname.info	22 Nov 2010 08:50:29 -0000
@@ -0,0 +1,8 @@
+; $Id$
+name = Privatemsg Realname Integration
+description = Provides reverse lookup for realnames
+package = Mail
+core = 7.x
+dependencies[] = privatemsg
+dependencies[] = realname
+
Index: privatemsg_realname/privatemsg_realname.module
===================================================================
RCS file: privatemsg_realname/privatemsg_realname.module
diff -N privatemsg_realname/privatemsg_realname.module
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ privatemsg_realname/privatemsg_realname.module	22 Nov 2010 08:50:29 -0000
@@ -0,0 +1,149 @@
+<?php
+// $Id$
+
+/**
+ * Implements hook_privatemsg_name_lookup().
+ */
+function privatemsg_realname_privatemsg_name_lookup($string) {
+
+  // First, check the unique version.
+  if (preg_match('/\[user:(.+)\]/', $string, $match)) {
+    $account = reset(user_load_multiple(array(variable_get('privatemsg_realname_unique_identifier', 'name') => trim($match[1]))));
+    $account->type = 'user';
+    $account->recipient = $account->uid;
+    return array($account);
+  }
+
+  // Then try to look it up with the real name.
+  $result = db_query('SELECT r.uid FROM {realname} r WHERE r.realname = :realname', array(':realname' => $string));
+  $accounts = array();
+  foreach ($result as $row) {
+    if ($account = user_load($row->uid)) {
+      $account->type = 'user';
+      $account->recipient = $account->uid;
+      $accounts[] = $account;
+    }
+  }
+  return $accounts;
+}
+
+/**
+ * Implemenets hook_theme().
+ */
+function privatemsg_realname_theme() {
+  return array(
+    'privatemsg_realname_username'  => array(
+      'variables' => array('recipient' => NULL, 'options' => array()),
+    ),
+  );
+}
+
+/**
+ * Implements hook_query_privatemsg_autocomplete_alter().
+ */
+function privatemsg_realname_privatemsg_sql_autocomplete_alter(SelectQueryInterface $query) {
+
+  $search = $query->getMetaData('arg_1');
+  $names = $query->getMetaData('arg_2');
+
+  // Create necessary LEFT JOINs. LEFT because some users might not have a realname for any reason.
+  $alias = $query->leftJoin('realname', 'r', '%alias.uid = u.uid');
+
+  // Either select users where the profile field name and value matches or the username.
+  // This does replace the default where.
+
+  $conditions = $query->conditions();
+  dpm($conditions);
+  if (variable_get('privatemsg_realname_search_username', TRUE)) {
+    //$fragments['where'][1] = "(r.realname LIKE '%s' OR u.name LIKE '%s')";
+    //array_unshift($fragments['query_args']['where'], $search . '%%');
+  }
+  else {
+    //$fragments['where'][1] = "r.realname LIKE '%s'";
+  }
+
+  if (!empty($names)) {
+    // Exclude already existing realnames, but explicitly allow NULL.
+    // r.realname is left joined and cann be NULL => NULL NOT IN (...) => NOT (NULL) => NULL.
+    $query->condition(db_or()
+      ->condition($alias . '.realname', $names, 'NOT IN')
+      ->isNull($alias . '.realname')
+    );
+  }
+}
+
+/**
+ * Implements hook_privatemsg_recipient_info_alter().
+ */
+function privatemsg_realname_privatemsg_recipient_type_info_alter(&$recipients) {
+  // Override format callback.
+  $recipients['user']['format'] = 'privatemsg_realname_username';
+}
+
+/**
+ * Used to theme and display user recipients.
+ *
+ * Wrapper for theme_username() with a few additional options.
+ */
+function theme_privatemsg_realname_username($variables) {
+  $recipient = $variables['recipient'];
+  $options = $variables['options'];
+  if (!isset($recipient->uid)) {
+    $recipient->uid = $recipient->recipient;
+  }
+  if (!empty($options['plain'])) {
+    $name = format_username($recipient);
+    if (!empty($options['unique'])) {
+      $identifier = variable_get('privatemsg_realname_unique_identifier', 'name');
+      $name .= ' [user: ' . $recipient->$identifier . ']';
+    }
+    return $name;
+  }
+  else {
+    return theme('username', array('account' => $recipient));
+  }
+}
+
+/**
+ * Implements hook_privatemsg_sql_participants_alter().
+ */
+function privatemsg_realname_privatemsg_sql_participants_alter(&$fragments, $thread_id) {
+  // Select the realname.
+  $fragments['select'][]      = 'r.realname';
+  // Also select homepage to avoid php notices.
+  $fragements['select'][]     = 'u.homepage';
+
+  // Join realname table.
+  $fragments['inner_join'][]  = 'LEFT JOIN {realname} r ON (r.uid = u.uid)';
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function privatemsg_realname_form_privatemsg_admin_settings_alter(&$form, &$form_state) {
+  $form['realname'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Realname integration'),
+    '#collapsed' => TRUE,
+    '#collapsible' => TRUE,
+    '#weight' => 25,
+  );
+
+  $form['realname']['privatemsg_realname_unique_identifier'] = array(
+    '#type' => 'radios',
+    '#title' => t('Field to use as a unique identifier'),
+    '#description' => t('Real names are often not unique. Choose which field should be used as a unique identifier when sending private messages.'),
+    '#default_value' => variable_get('privatemsg_realname_unique_identifier', 'name'),
+    '#options' => array(
+      'name' => t('Username'),
+      'uid' => t('UID'),
+      'mail' => t('E-mail'),
+    ),
+  );
+
+  $form['realname']['privatemsg_realname_search_username'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Search usernames for autocomplete suggestions'),
+    '#default_value' => variable_get('privatemsg_realname_search_username', TRUE),
+  );
+}
\ No newline at end of file
