Index: realname.module
===================================================================
--- realname.module	(Revision 267)
+++ realname.module	(Arbeitskopie)
@@ -144,6 +144,14 @@
     'access arguments' => array('access content'),
     'type' => MENU_CALLBACK,
     );
+
+  // Privatemsg intercept for recipient autocomplete.
+  $items['realname/privatemsg/autocomplete'] = array(
+    'title' => 'Realname Privatemsg autocomplete',
+    'access callback' => user_access('access private messages'),
+    'page callback' => 'realname_privatemsg_autocomplete',
+    'type' => MENU_CALLBACK,
+  );
  
   return $items;
 }
@@ -226,7 +234,7 @@
   }
   
   if ($account) {
-    realname_check_breadcrumbs($account);    
+    realname_check_breadcrumbs($account);
     drupal_set_title(realname_user_page_title($account));
   }
   return $output;
@@ -245,7 +253,7 @@
     $output = t('You cannot contact more than %number users per hour. Please try again later.', array('%number' => variable_get('contact_hourly_threshold', 3)));
   }
   else {
-    realname_check_breadcrumbs($account);    
+    realname_check_breadcrumbs($account);
     $output = drupal_get_form('contact_mail_user', $account);
   }
 
@@ -266,11 +274,11 @@
 }
 
 /**
- * An implementation of hook_theme_registry_alter()
+ * Implementation of hook_theme_registry_alter().
  *
  * Swap in our own replacement for theme_username(), allowing the
  * site admin to configure the string on a per-nodetype basis.
- **/
+ */
 function realname_theme_registry_alter(&$theme_registry) {
   if (!empty($theme_registry['form_element'])) {
     $path = drupal_get_path('module', 'realname');
@@ -437,7 +445,7 @@
         else {
           $users[$comment->uid] = $account = realname_get_user($comment->uid);
         }
-        
+
         $comment->homepage = isset($account->homepage) ? $account->homepage : NULL;
       }
       break;
@@ -478,7 +486,7 @@
         $user->realname = realname_make_name($user);
       }
       $value = $user->uid ? $user->realname_save : variable_get('anonymous', 'Anonymous');
-      eval($field_name .' = $value;');      
+      eval($field_name .' = $value;');
     }
   }
 
@@ -524,6 +532,11 @@
       $form['delete']['#weight'] = 99;
       $form['#submit'][] = '_realname_role_submit';
       break;
+
+    case 'privatemsg_new':
+      $form['privatemsg']['recipient']['#autocomplete_path'] = 'realname/privatemsg/autocomplete';
+      break;
+
   }
 }
 
@@ -533,7 +546,7 @@
 function _realname_role_submit($form, &$form_state) {
   $rid = $form_state['values']['rid'];
   $pic = $form_state['values']['level'];
-  variable_set('realname_user_level_'. $rid, $pic);  
+  variable_set('realname_user_level_'. $rid, $pic);
 }
 
 /**
@@ -545,7 +558,7 @@
   }
   switch ($op) {
     case 'name':
-      if ($skip_access_check || user_access('access user profiles')) {      
+      if ($skip_access_check || user_access('access user profiles')) {
         return variable_get('realname_user_disable', FALSE) ? t('Users') : t('User names');
       }
 
@@ -560,7 +573,7 @@
       return array('remaining' => $remaining, 'total' => $total);
 
     case 'search':
-      if ($skip_access_check || user_access('access user profiles')) {      
+      if ($skip_access_check || user_access('access user profiles')) {
         $results = array();
         $find = do_search($keys, 'realname');
         foreach ($find as $item) {
@@ -611,6 +624,34 @@
   }
 }
 
+/**
+ * Intercept Privatemsg autocomplete results for usernames.
+ */
+function realname_privatemsg_autocomplete($string) {
+  $names = explode(',', $string);
+  $names = array_map('trim', $names);
+  $search = array_pop($names);
+  if ($search != '') {
+    $sql = 'SELECT u.uid, u.name, r.realname FROM {users} u INNER JOIN {realname} r USING(uid) ';
+    $sql .= ' WHERE u.status <> 0 AND LOWER(r.realname) LIKE LOWER(\'%%%s%%\') AND ';
+    $sql .= variable_get('privatemsg_default_allow', 1) ? '(u.data NOT LIKE \'%%:16:"privatemsg_allow";i:0%%\' OR u.data IS NULL)' : 'u.data LIKE \'%%:16:"privatemsg_allow";i:1%%\'';
+    $sql .= ' ORDER BY r.realname ASC';
+    $result = db_query_range($sql, $search, 0, 10);
+    $prefix = count($names) ? implode(', ', $names) .', ' : '';
+    $matches = array();
+    while ($user = db_fetch_object($result)) {
+      $account = user_load(array('uid' => $user->uid));
+      $matches[$prefix . $user->name] = $user->realname;
+      if ($user->realname != $user->name) {
+        $matches[$prefix . $user->name] .= ' ('. $user->name .')';
+      }
+    }
+    drupal_json($matches);
+  } else {
+    drupal_json(array()); // prevent Drupal autocomplete error message
+  }
+}
+
 //********************************************************************
 //* Module Functions
 //********************************************************************
