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	29 Apr 2010 20:09:17 -0000
@@ -0,0 +1,8 @@
+; $Id$
+name = Privatemsg Realname Integration
+description = Provides reverse lookup for realnames
+package = Mail
+core = 6.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	29 Apr 2010 20:09:17 -0000
@@ -0,0 +1,112 @@
+<?php
+// $Id$
+
+/**
+ * Implements hook_privatemsg_name_lookup().
+ */
+function privatemsg_realname_privatemsg_name_lookup($string) {
+  $result = db_query("SELECT r.uid FROM {realname} r WHERE r.realname = '%s'", $string);
+  if ($uid = db_result($result)) {
+    return user_load(array('uid' => $uid));
+  }
+}
+
+/**
+ * Implements hook_privatemsg_sql_autocomplete_alter().
+ */
+function privatemsg_realname_privatemsg_sql_autocomplete_alter(&$fragments, $search, $names) {
+  // Either display profile field or username, depending on which value does match.
+  $fragments['select'][0] = "IF (r.realname LIKE '%s', r.realname, u.name) AS name";
+  $fragments['query_args']['select'][] = $search . '%%';
+
+  // Create necessary LEFT JOINs. LEFT because some users might not have a realname for any reason.
+  $fragments['inner_join'][] = 'LEFT JOIN {realname} r ON (r.uid = u.uid)';
+
+  // Either select users where the profile field name and value matches or the username.
+  // This does replace the default where.
+  $fragments['where'][1] = "(r.realname LIKE '%s' OR u.name LIKE '%s')";
+  $fragments['query_args']['where'][] = $search . '%%';
+
+  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.
+    $fragments['where'][] = "r.realname NOT IN (". db_placeholders($names, 'text') .") OR r.realname IS NULL";
+    $fragments['query_args']['where'] = array_merge($fragments['query_args']['where'], array_values($names));
+  }
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter().
+ */
+function privatemsg_realname_form_privatemsg_new_alter(&$form, &$form_state) {
+  global $user;
+
+  if (isset($form['privatemsg']['thread_id'])) {
+    return;
+  }
+  $recipients_string = '';
+
+  $recipients = arg(2);
+
+  // convert recipients to array of user objects
+  if (!empty($recipients) && is_string($recipients) || is_int($recipients)) {
+    $recipients = _privatemsg_generate_user_array($recipients);
+  }
+  elseif (is_object($recipients)) {
+    $recipients = array($recipients);
+  }
+
+  if (empty($recipients)) {
+    return;
+  }
+
+  $usercount = 0;
+  $to = array();
+  $blocked = FALSE;
+  foreach ($recipients as $recipient) {
+    if (isset($to[$recipient->uid])) {
+      // We already added the recipient to the list, skip him.
+      continue;
+    }
+    // Check if another module is blocking the sending of messages to the
+    // recipient by current user.
+    $user_blocked = module_invoke_all('privatemsg_block_message', $user, array($recipient->uid => $recipient));
+    if (!count($user_blocked) <> 0 && $recipient->uid) {
+      if ($recipient->uid == $user->uid) {
+        $usercount++;
+        // Skip putting author in the recipients list for now.
+        continue;
+      }
+      $to[$recipient->uid] = theme('username', $recipient, array('plain' => TRUE, 'allowed_tags' => array()));
+    }
+    else {
+      // Recipient list contains blocked users.
+      $blocked = TRUE;
+    }
+  }
+
+  if (empty($to) && $usercount >= 1 && !$blocked) {
+    // Assume the user sent message to own account as if the usercount is one
+    // or less, then the user sent a message but not to self.
+    $to[$user->uid] = theme('username', $user, array('plain' => TRUE, 'allowed_tags' => array()));
+  }
+
+  if (!empty($to)) {
+    $recipients_string = implode(', ', $to);
+    drupal_set_title(t('Write new message to %recipient', array('%recipient' => $recipients_string)));
+    $form['privatemsg']['recipient']['#default_value'] = $recipients_string;
+  }
+}
+
+/**
+ * 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)';
+}
\ No newline at end of file
