? .cvsignore
? privatemsg_recipient_types.patch
? privatemsg_roles
? privatemsg_roles.patch
Index: privatemsg.api.php
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.api.php,v
retrieving revision 1.2
diff -u -p -r1.2 privatemsg.api.php
--- privatemsg.api.php	30 Nov 2009 17:37:15 -0000	1.2
+++ privatemsg.api.php	1 Mar 2010 17:02:54 -0000
@@ -8,8 +8,7 @@
 
 /**
  * @mainpage Privatemsg API Documentation
- * This is the (currently inofficial) API documentation of the privatemsg 6.x
- * API.
+ * This is the API documentation of the privatemsg 6.x API.
  *
  * - Topics:
  *  - @link api API functions @endlink
@@ -17,6 +16,7 @@
  *  - @link message_hooks Message hooks @endlink
  *  - @link generic_hooks Generic hooks @endlink
  *  - @link theming Theming @endlink
+ *  - @link types Types of recipients @endlink
  */
 
 /**
@@ -182,16 +182,28 @@ function hook_privatemsg_sql_messages_al
 }
 
 /**
- * Load the participants of a thread.
+ * Alter the query that loads the participants of a thread.
+ *
+ * This hook is usually used to load additional values that belong to a
+ * recipient type. The following two select columns need to be added to the
+ * query.
+ *
+ *  - {type}_name: The name that should be displayed for the recipient.
+ *  - {type}_key: How to access the recipient id for that recipient.
  *
  * @param $fragments
  *   Query fragments
  * @param $thread_id
  *   Thread id, pmi.thread_id is the same as the mid of the first
  *   message of that thread
+ * @ingroup types
  */
 function hook_privatemsg_sql_participants_alter(&$fragment, $thread_id) {
+  $fragments['select'][] = "CONCAT (r.name, ' [role]') AS role_name";
+  $fragments['select'][] = "'rid' AS role_key";
 
+  $fragments['inner_join'][] = "LEFT JOIN {role} r ON (r.rid = pmi.recipient AND pmi.type = 'role')";
+  $fragments['group_by'][]    = 'role_name';
 }
 
 /**
@@ -371,18 +383,30 @@ function hook_privatemsg_message_insert(
  */
 
 /**
- * Check if $author can send $recipient a message.
+ * Check if the author can send a message to the recipients.
  *
- * Return a message if it is not alled, nothing if it is. This hook is executed
- * for each recipient.
+ * This can be used to limit who can write whom based on other modules and/or
+ * settings.
  *
  * @param $author
  *   Author of the message to be sent
  * @param $recipient
  *   Recipient of the message
+ * @ingroup types
  */
 function hook_privatemsg_block_message($author, $recipient) {
-
+  $blocked = array();
+  foreach($recipients as $recipient) {
+    // Deny/block if the recipient type is role and the account does not have
+    // the necessary permission.
+    if (isset($recipient->type) && $recipient->type == 'role' && !privatemsg_user_access('write privatemsg to roles', $author)) {
+      $blocked[] = array(
+        'recipient' => 'role_' . _privatemsg_get_recipient_property($recipient),
+        'message' => t('Not allowed to write private messages to role members'),
+      );
+    }
+  }
+  return $blocked;
 }
 /**
  * Add content to the view thread page.
@@ -429,6 +453,85 @@ function hook_privatemsg_thread_operatio
 }
 
 /**
+ * @}
+ */
+
+/**
+ * @defgroup types Types of recipients
+ *
+ * It is possible to define other types of recipients than the usual single
+ * user. These types are defined through a few hook implementations and are
+ * stored in the {pm_index} table for each recipient entry.
+ *
+ * Because of that, only the combination of type and recipient (was uid in older
+ * versions) defines a unique recipient.
+ *
+ * This feature is usually used to define groups of recipients. Privatemsg
+ * comes with the privatemsg_roles sub-module, which allows to send messages to
+ * all members of a specific group.
+ *
+ * Privatemsg defines the following types:
+ *
+ * - user: This is the default recipient type which is used to denote a single
+ *   user.
+ * - hidden: To be able to mark a thread/message as read/deleted, Privatemsg
+ *   adds an internal hidden entry if a user recieves a message with a type
+ *   other than user. These recipients are not displayed, and only loaded for
+ *   current user to determine the read/unread status flags.
+ * - role: The sub-module privatemsg_roles defines an additional type called
+ *   role. This allows to send messages to all members of a role.
+ *
+ * To implement a new type, the following hooks need to be implemented. Note
+ * that most of these hooks can also be used alone for other functionality than
+ * defining recipient types.
+ *
+ * - hook_privatemsg_recipient_autocomplete() - Define autocomplete
+ *   suggestions
+ * - hook_privatemsg_name_lookup() - Convert a string to an
+ *   recipient object
+ * - hook_privatemsg_recipients_where() - Define the query parts to load the
+ *   messages of your types.
+ * - hook_privatemsg_sql_participants_alter() - Add the name and key for your
+ *   recipient type.
+ * - hook_privatemsg_is_recipient() - Define if account
+ *   is part of the participants.
+ * - hook_privatemsg_block_message() - This can be used to implement permission
+ *   based checks of the account is allowed to write messages to that role type.
+ */
+
+/**
+ * @addtogroup types
+ * @{
+ */
+
+/**
+ * Provide autocomplete suggestions for a the types the module defines.
+ *
+ * @param $fragment
+ *   The fragment of the name the is currently typing.
+ * @param $names
+ *   The list of recipients the user has already entered, they should be
+ *   excluded from the list.
+ * @param $limit
+ *   The maximum nuber of matches to return.
+ * @return
+ *   An indexed array which contains the possible matches.
+ */
+function hook_privatemsg_recipient_autocomplete($fragment, $names, $limit) {
+  $query = _privatemsg_assemble_query(array('privatemsg_roles', 'autocomplete_roles'), $fragment, $names);
+  $result = db_query_range($query['query'], $fragment, 0, $limit);
+  // 3: Build proper suggestions and print.
+  $matches = array();
+  while ($role = db_fetch_object($result)) {
+    // Don't use placeholders to make sure that the [role] is always at the end
+    // and can be used when resolving names again.
+    $matches[] = $role->name . ' ' .  t('[role]');
+  }
+  return $matches;
+}
+
+
+/**
  * Hook which allows to look up a user object.
  *
  * You can try to look up a user object based on the information passed to the
@@ -438,10 +541,56 @@ function hook_privatemsg_thread_operatio
  * up the string.
  */
 function hook_privatemsg_name_lookup($string) {
-  if ((int)$string > 0) {
-    // This is a possible uid, try to load a matching user.
-    if ($recipient = user_load(array('uid' => $string))) {
-      return $recipient;
+  // To make it possible to have users and roles with the same name, roles need
+  // to contain an [role] as part of their "name".
+  // Search and replace this before looking up the role name.
+  if (strpos($string, t('[role]')) !== FALSE) {
+    $role = str_replace(t('[role]'), '', $string);
+    $result = db_query("SELECT * FROM {role} WHERE name = '%s'", trim($role));
+    if ($role = db_fetch_object($result)) {
+      // Define how to access the "key" property (which will then be used as
+      // recipient id), define type and {type}_key property.
+      $role->type = 'role';
+      $role->role_key = 'rid';
+      return $role;
+    }
+  }
+}
+
+/**
+ * Define how to look for messages for the provided account.
+ *
+ * @param $account
+ *   User object for which to return the query definition.
+ * @return
+ *   An array with the following indexes:
+ *     - where: The SQL query part
+ *     - args: The arguments for that query part.
+ */
+function hook_privatemsg_recipients_where($account) {
+  return array(
+    'where' => array("pmi.type = 'role' AND pmi.recipient IN (" . db_placeholders(array_keys($account->roles)) . ")"),
+    'args'  => array_keys($account->roles),
+  );
+}
+
+/**
+ * Denote if the account is a participant.
+ * 
+ * This is used to determine if an account is part of a thread and therefore
+ * allowed to view it.
+ *
+ * @param $account
+ *   The user object to check.
+ * @param $participants
+ *   Array of participants in a thread.
+ * @return
+ *   Return TRUE if the account belongs to a participant entry.
+ */
+function hook_privatemsg_is_recipient($account, $participants) {
+  foreach ($account->roles as $role_id => $role_name) {
+    if (isset($participants['role_' . $role_id])) {
+      return TRUE;
     }
   }
 }
Index: privatemsg.install
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.install,v
retrieving revision 1.15
diff -u -p -r1.15 privatemsg.install
--- privatemsg.install	4 Jan 2010 15:56:56 -0000	1.15
+++ privatemsg.install	1 Mar 2010 17:02:55 -0000
@@ -24,8 +24,8 @@ function privatemsg_schema() {
         'not null'      => TRUE,
         'unsigned'      => TRUE,
       ),
-      'uid'     => array(
-        'description'   => 'UID of either the author or the recipient',
+      'recipient'     => array(
+        'description'   => 'ID of the recipient object, typically user',
         'type'          => 'int',
         'not null'      => TRUE,
         'unsigned'      => TRUE,
@@ -44,13 +44,20 @@ function privatemsg_schema() {
         'not null' => TRUE,
         'default' => 0
       ),
+      'type' => array(
+        'description'   => 'Type of recipient object',
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => 'user'
+      ),
 
     ),
     'indexes'         => array(
       'mid'               => array('mid'),
       'thread_id'         => array('thread_id'),
-      'uid'         => array('uid'),
-      'is_new'              => array('mid', 'uid', 'is_new', ),
+      'recipient'         => array('recipient', 'type'),
+      'is_new'            => array('mid', 'recipient', 'type', 'is_new'),
     ),
   );
 
@@ -528,3 +535,29 @@ function privatemsg_update_6007() {
 
   return $ret;
 }
+
+/**
+ * Change schema to allow other recipients than single users.
+ */
+function privatemsg_update_6008() {
+  $ret = array();
+  db_drop_index($ret, 'pm_index', 'uid');
+  db_drop_index($ret, 'pm_index', 'is_new');
+  db_change_field($ret, 'pm_index', 'uid', 'recipient', array(
+    'description'   => 'ID of the recipient object, typically user',
+    'type'          => 'int',
+    'not null'      => TRUE,
+    'unsigned'      => TRUE,
+  ));
+  db_add_field($ret, 'pm_index', 'type', array(
+        'description'   => 'Type of recipient object',
+        'type' => 'varchar',
+        'not null' => TRUE,
+        'length'  => '255',
+        'default' => 'user'
+      ), array('indexes' => array(
+      'recipient'         => array('recipient', 'type'),
+      'is_new'            => array('mid', 'recipient', 'type', 'is_new')
+  )));
+  return $ret;
+}
Index: privatemsg.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.module,v
retrieving revision 1.120
diff -u -p -r1.120 privatemsg.module
--- privatemsg.module	25 Feb 2010 18:33:50 -0000	1.120
+++ privatemsg.module	1 Mar 2010 17:03:01 -0000
@@ -67,6 +67,24 @@ function _privatemsg_generate_user_array
 }
 
 /**
+ * Format a single participant.
+ *
+ * @param $participant
+ *   The participant object to format
+ */
+function _privatemsg_format_participant($participant) {
+  if (!empty($participant->name) || isset($participant->uid) && $participant->uid === '0') {
+    if (!isset($participant->uid)) {
+      $participant->uid = $participant->recipient;
+    }
+    return theme('username', $participant);
+  }
+  else {
+    return _privatemsg_get_recipient_property($participant, 'name');
+  }
+}
+
+/**
  * Format an array of user objects.
  *
  * @param $part_array
@@ -85,11 +103,15 @@ function _privatemsg_format_participants
     $to = array();
     $limited = FALSE;
     foreach ($part_array as $account) {
+      // Don't display recipients with type hidden.
+      if (isset($account->type) && $account->type == 'hidden') {
+        continue;
+      }
       if (is_int($limit) && count($to) >= $limit) {
         $limited = TRUE;
         break;
       }
-      $to[] = theme('username', $account);
+      $to[] = _privatemsg_format_participant($account);
     }
 
     $limit_string = '';
@@ -279,8 +301,7 @@ function privatemsg_view_access($thread)
  * @return TRUE if user has disabled private messaging, FALSE otherwise
  */
 function privatemsg_is_disabled(&$account) {
-
-  if (!$account || !$account->uid) {
+  if (!$account || !isset($account->uid) || !$account->uid) {
     return FALSE;
   }
 
@@ -338,12 +359,25 @@ function privatemsg_thread_load($thread_
       $query = _privatemsg_assemble_query('participants', $thread_id);
       $participants = db_query($query['query']);
       $thread['participants'] = array();
+      $exists = FALSE;
       while ($participant = db_fetch_object($participants)) {
-        $thread['participants'][$participant->uid] = $participant;
+        if ($participant->type == 'user' && $participant->recipient == $account->uid) {
+          $exists = TRUE;
+        }
+        $thread['participants'][$participant->type . '_' . $participant->recipient] = $participant;
       }
       $thread['read_all'] = FALSE;
-      if (!array_key_exists($account->uid, $thread['participants']) && privatemsg_user_access('read all private messages', $account)) {
-        $thread['read_all'] = TRUE;
+      if (!$exists) {
+        // Maybe the user is part of some other recipient type, call the hook.
+        foreach (module_implements('privatemsg_is_recipient') as $module) {
+          if (!$exists = module_invoke($module, 'privatemsg_is_recipient', $account, $thread['participants'])) {
+            break;
+          }
+        }
+        // Check $exists again.
+        if (!$exists && privatemsg_user_access('read all private messages', $account)) {
+          $thread['read_all'] = TRUE;
+        }
       }
 
       // Load messages returned by the messages query with privatemsg_message_load_multiple().
@@ -549,7 +583,7 @@ function privatemsg_preprocess_privatems
   $vars['mid'] = isset($message['mid']) ? $message['mid'] : NULL;
   $vars['thread_id'] = isset($message['thread_id']) ? $message['thread_id'] : NULL;
   $vars['author_picture'] = theme('user_picture', $message['author']);
-  $vars['author_name_link'] = theme('username', $message['author']);
+  $vars['author_name_link'] = _privatemsg_format_participant($message['author']);
   /**
    * @todo perhaps make this timestamp configurable via admin UI?
    */
@@ -597,8 +631,16 @@ function privatemsg_message_change_statu
     global $user;
     $account = $user;
   }
-  $query = "UPDATE {pm_index} SET is_new = %d WHERE mid = %d AND uid = %d";
-  db_query($query, $status, $pmid, $account->uid);
+  $exists = db_result(db_query("SELECT 1 FROM {pm_index} WHERE mid = %d AND recipient = %d AND type IN ('user', 'hidden')", $pmid, $account->uid));
+  if ($exists) {
+    $query = "UPDATE {pm_index} SET is_new = %d WHERE mid = %d AND recipient = %d AND type IN ('user', 'hidden')";
+    db_query($query, $status, $pmid, $account->uid);
+  }
+  else {
+    $message = privatemsg_message_load($pmid);
+    $index_sql = "INSERT INTO {pm_index} (mid, thread_id, recipient, type, is_new, deleted) VALUES (%d, %d, %d, '%s', %d, %d)";
+    db_query($index_sql, $pmid, $message['thread_id'], $account->uid, 'hidden', 0, 0);
+  }
 }
 
 /**
@@ -617,7 +659,10 @@ function privatemsg_unread_count($accoun
   }
   if ( !isset($counts[$account->uid])) {
     $query = _privatemsg_assemble_query('unread_count', $account);
-    $counts[$account->uid] = db_result(db_query($query['query']));
+    // We need group by in the sub query, sum all results together to the the
+    // total new messages.
+    $query = 'SELECT SUM(is_new) FROM (' . $query['query'] . ') subquery';
+    $counts[$account->uid] = db_result(db_query($query));
   }
   return $counts[$account->uid];
 }
@@ -632,9 +677,18 @@ function _privatemsg_load_thread_partici
   $query = _privatemsg_assemble_query('participants', $thread_id);
   $result = db_query($query['query']);
   $participants = array();
-  while ($uid = db_fetch_object($result)) {
-    if (($recipient = user_load($uid->uid))) {
-      $participants[$recipient->uid] = $recipient;
+  while ($participant = db_fetch_object($result)) {
+    // Ignore hidden recipients.
+    if ($participant->type == 'hidden') {
+      continue;
+    }
+    if ($participant->type == 'user' && ($account = user_load($participant->recipient))) {
+      $participants['user_' . $account->uid] = $account;
+    }
+    elseif ($participant->type != 'user') {
+      // They key is always recipient.
+      $participant->key = 'recipient';
+      $participants[$participant->type . '_' . $participant->recipient] = $participant;
     }
   }
   return $participants;
@@ -669,8 +723,14 @@ function _privatemsg_parse_userstring($i
       foreach (module_implements('privatemsg_name_lookup') as $module) {
         $function = $module . '_privatemsg_name_lookup';
         if (($recipient = $function($string)) && is_object($recipient)) {
+          // Default to user object.
+          if (!isset($recipient->type)) {
+            $recipient->type = 'user';
+            $recipient->user_key = 'uid';
+          }
+          $recipients[_privatemsg_get_recipient_property($recipient)] = $recipient;
+
           // If there is a match, continue with the next input string.
-          $recipients[$recipient->uid] = $recipient;
           continue 2;
         }
       }
@@ -689,6 +749,28 @@ function _privatemsg_parse_userstring($i
   return array($recipients, $invalid);
 }
 
+function _privatemsg_build_where_clause($account) {
+  // Default parts.
+  $where       = "pmi.recipient = %d AND pmi.type IN ('user', 'hidden')";
+  $args        = $account->uid;
+
+  // Fetch parts from other modules and merge defaults in.
+  $query_parts = module_invoke_all('privatemsg_recipients_where', $account);
+  if (!isset($query_parts['where'])) {
+    $query_parts['where'] = array();
+  }
+  if (!isset($query_parts['args'])) {
+    $query_parts['args'] = array();
+  }
+  array_unshift($query_parts['where'], $where);
+  array_unshift($query_parts['args'], $args);
+
+  // Merge the different where parts in a single string, splitted by OR.
+  $query_parts['where'] = '(' . implode(') OR (', $query_parts['where']) . ')';
+
+  return $query_parts;
+}
+
 /**
  * @addtogroup sql
  * @{
@@ -720,8 +802,15 @@ function privatemsg_sql_list(&$fragments
   $fragments['select'][]      = 'MIN(pm.subject) as subject';
   $fragments['select'][]      = 'MAX(pm.timestamp) as last_updated';
   // We use SUM so that we can count the number of unread messages.
-  $fragments['select'][]      = 'SUM(pmi.is_new) as is_new';
-
+  $read_messages = "(SELECT COUNT(DISTINCT pmie.mid) FROM {pm_index} pmie WHERE pmie.thread_id = pmi.thread_id AND pmie.recipient = %d AND pmie.is_new = 0 AND pmie.type IN ('user', 'hidden') LIMIT 1)";
+  $cast = $read_messages;
+  // On PostgreSQL, we need to cast the result to an integer.
+  if ($GLOBALS['db_type'] == 'pgsql') {
+    $cast = "CAST($read_messages AS integer)";
+  }
+  $fragments['select'][]      = "(COUNT(DISTINCT pmi.mid) - IF ($read_messages IS NULL, 0, $cast)) AS is_new";
+  $fragments['query_args']['select'][] = $account->uid;
+  $fragments['query_args']['select'][] = $account->uid;
   // Select number of messages in the thread if the count is
   // set to be displayed.
   if (in_array('count', $fields)) {
@@ -732,14 +821,14 @@ function privatemsg_sql_list(&$fragments
     // @todo: Replace this with a single query similiar to the tag list.
     if ($GLOBALS['db_type'] == 'pgsql') {
       // PostgreSQL does not know GROUP_CONCAT, so a subquery is required.
-      $fragments['select'][]      = "array_to_string(array(SELECT DISTINCT textin(int4out(pmia.uid))
+      $fragments['select'][]      = "array_to_string(array(SELECT DISTINCT textin(int4out(pmia.recipient))
                                                             FROM {pm_index} pmia
-                                                            WHERE pmia.thread_id = pmi.thread_id), ',') AS participants";
+                                                            WHERE pmia.type = 'user' AND pmia.thread_id = pmi.thread_id), ',') AS participants";
     }
     else {
-      $fragments['select'][]      = '(SELECT GROUP_CONCAT(DISTINCT pmia.uid SEPARATOR ",")
+      $fragments['select'][]      = "(SELECT GROUP_CONCAT(DISTINCT pmia.recipient SEPARATOR ',')
                                                             FROM {pm_index} pmia
-                                                            WHERE pmia.thread_id = pmi.thread_id) AS participants';
+                                                            WHERE pmia.type = 'user' AND pmia.thread_id = pmi.thread_id) AS participants";
     }
   }
   if (in_array('thread_started', $fields)) {
@@ -748,11 +837,15 @@ function privatemsg_sql_list(&$fragments
 
   $fragments['inner_join'][]  = 'INNER JOIN {pm_index} pmi ON pm.mid = pmi.mid';
 
-  // Only load undeleted messages of the current user and group by thread.
-  $fragments['where'][]       = 'pmi.uid = %d';
-  $fragments['query_args']['where'][]  = $account->uid;
+  // Build the query to load all messages for this user.
+  $query_parts = _privatemsg_build_where_clause($account);
+  $fragments['where'][]      = $query_parts['where'];
+  $fragments['query_args']['where'] = array_merge($fragments['query_args']['where'], $query_parts['args']);
+
+  // Only load undeleted messages and group by thread.
   $fragments['where'][]       = 'pmi.deleted = 0';
   $fragments['group_by'][]    = 'pmi.thread_id';
+  $fragments['group_by'][]    = 'pmi.recipient';
 
   // tablesort_sql() generates a ORDER BY string. However, the "ORDER BY " part
   // is not needed and added by the query builder. Discard the first 9
@@ -788,8 +881,10 @@ function privatemsg_sql_load(&$fragments
   $fragments['where'][]       = 'pmi.mid IN (' . db_placeholders($pmids) . ')';
   $fragments['query_args']['where']  += $pmids;
   if ($account) {
-    $fragments['where'][]       = 'pmi.uid = %d';
-    $fragments['query_args']['where'][]  = $account->uid;
+    // Build the query to load all messages for this user.
+    $query_parts = _privatemsg_build_where_clause($account);
+    $fragments['where'][]      = $query_parts['where'];
+    $fragments['query_args']['where'] = array_merge($fragments['query_args']['where'], $query_parts['args']);
   }
   $fragments['order_by'][] = 'pm.timestamp ASC';
 }
@@ -814,9 +909,10 @@ function privatemsg_sql_messages(&$fragm
   $fragments['query_args']['where']   += $threads;
   $fragments['inner_join'][]  = 'INNER JOIN {pm_message} pm ON (pm.mid = pmi.mid)';
   if ($account) {
-    // Only load the user's messages.
-    $fragments['where'][]     = 'pmi.uid = %d';
-    $fragments['query_args']['where'][]  = $account->uid;
+    // Build the query to load all messages for this user.
+    $query_parts = _privatemsg_build_where_clause($account);
+    $fragments['where'][]      = $query_parts['where'];
+    $fragments['query_args']['where'] = array_merge($fragments['query_args']['where'], $query_parts['args']);
   }
   if (!$load_all) {
     // Also load deleted messages when requested.
@@ -843,16 +939,25 @@ function privatemsg_sql_messages(&$fragm
  *   Thread id from which the participants should be loaded.
  */
 function privatemsg_sql_participants(&$fragments, $thread_id) {
+  global $user;
   $fragments['primary_table'] = '{pm_index} pmi';
 
   // Only load each participant once since they are listed as recipient for
   // every message of that thread.
-  $fragments['select'][]      = 'DISTINCT(pmi.uid) AS uid';
-  $fragments['select'][]      = 'u.name AS name';
+  $fragments['select'][]      = 'pmi.recipient';
+  $fragments['select'][]      = 'u.name';
+  $fragments['select'][]      = 'pmi.type';
 
-  $fragments['inner_join'][]  = 'INNER JOIN {users} u ON (u.uid = pmi.uid)';
+  $fragments['inner_join'][]  = "LEFT JOIN {users} u ON (u.uid = pmi.recipient AND pmi.type IN ('user', 'hidden'))";
   $fragments['where'][]       = 'pmi.thread_id = %d';
   $fragments['query_args']['where'][]  = $thread_id;
+
+  $fragments['where'][]       = "(pmi.type <> 'hidden') OR (pmi.type = 'hidden' AND pmi.recipient = %d)";
+  $fragments['query_args']['where'][]  = $user->uid;
+
+  $fragments['group_by'][]    = 'pmi.recipient';
+  $fragments['group_by'][]    = 'u.name';
+  $fragments['group_by'][]    = 'pmi.type';
 }
 
 /**
@@ -866,13 +971,30 @@ function privatemsg_sql_participants(&$f
 function privatemsg_sql_unread_count(&$fragments, $account) {
   $fragments['primary_table'] = '{pm_index} pmi';
 
-  $fragments['select'][]      = 'COUNT(DISTINCT thread_id) as unread_count';
+  $query_parts = _privatemsg_build_where_clause($account);
+
+  $read_messages = "(SELECT COUNT(DISTINCT pmi.mid) FROM {pm_index} pmie WHERE pmie.thread_id = pmi.thread_id AND pmie.recipient = %d AND pmie.is_new = 0 AND pmie.type IN ('user', 'hidden') LIMIT 1)";
+  $cast = $read_messages;
+  // On PostgreSQL, we need to cast the result to an integer.
+  if ($GLOBALS['db_type'] == 'pgsql') {
+    $cast = "CAST($read_messages AS integer)";
+  }
+  $fragments['select'][]      = "(COUNT(DISTINCT pmi.mid) - IF ($read_messages IS NULL, 0, $cast)) AS is_new";
+  $fragments['query_args']['select'][] = $account->uid;
+  $fragments['query_args']['select'][] = $account->uid;
 
   // Only count new messages that have not been deleted.
   $fragments['where'][]       = 'pmi.deleted = 0';
   $fragments['where'][]       = 'pmi.is_new = 1';
-  $fragments['where'][]       = 'pmi.uid = %d';
-  $fragments['query_args']['where'][]  = $account->uid;
+  // Build the query to load all messages for this user.
+  $fragments['where'][]      = $query_parts['where'];
+  $fragments['query_args']['where'] = array_merge($fragments['query_args']['where'], $query_parts['args']);
+
+  $fragments['group_by'][] = 'pmi.recipient';
+  $fragments['group_by'][] = 'pmi.type';
+  $fragments['group_by'][] = 'pmi.deleted';
+  $fragments['group_by'][] = 'pmi.is_new';
+  $fragments['group_by'][] = 'pmi.thread_id';
 }
 
 /**
@@ -1019,10 +1141,10 @@ function privatemsg_user($op, &$edit, &$
       }
 
       // Delete recipient entries of that user.
-      db_query('DELETE FROM {pm_index} WHERE uid = %d', $account->uid);
+      db_query("DELETE FROM {pm_index} WHERE recipient = %d and type = 'user'", $account->uid);
 
       // DELETE any disable flag for user.
-      db_query('DELETE from {pm_disable} where uid=%d', $account->uid);
+      db_query("DELETE from {pm_disable} WHERE uid = %d", $account->uid);
       break;
   }
 }
@@ -1122,7 +1244,15 @@ function privatemsg_message_change_delet
   }
 
   if ($account) {
-    db_query('UPDATE {pm_index} SET deleted = %d WHERE mid = %d AND uid = %d', $delete_value, $pmid, $account->uid);
+    $exists = db_result(db_query("SELECT 1 FROM {pm_index} WHERE mid = %d AND recipient = %d AND type IN ('user', 'hidden')", $pmid, $account->uid));
+    if ($exists) {
+      db_query("UPDATE {pm_index} SET deleted = %d WHERE mid = %d AND recipient = %d AND type IN ('user', 'hidden')", $delete_value, $pmid, $account->uid);
+    }
+    else {
+      $message = privatemsg_message_load($pmid);
+      $index_sql = "INSERT INTO {pm_index} (mid, thread_id, recipient, type, is_new, deleted) VALUES (%d, %d, %d, '%s', %d, %d)";
+      db_query($index_sql, $pmid, $message['thread_id'], $account->uid, 'hidden', 0, 1);
+    }
   }
   else {
     // Mark deleted for all users.
@@ -1275,7 +1405,7 @@ function privatemsg_reply($thread_id, $b
   return $validated;
 }
 
-function _privatemsg_validate_message(&$message, $form = FALSE) {
+function _privatemsg_validate_message(&$message, $form = FALSE, $display_block_messages = TRUE) {
   $messages = array('error' => array(), 'warning' => array());
   if (!privatemsg_user_access('write privatemsg', $message['author'])) {
     // no need to do further checks in this case...
@@ -1343,9 +1473,11 @@ function _privatemsg_validate_message(&$
 
   if (!empty($message['recipients']) && is_array($message['recipients'])) {
     foreach (module_invoke_all('privatemsg_block_message', $message['author'], $message['recipients']) as $blocked) {
-      unset($message['recipients'][$blocked['uid']]);
+      unset($message['recipients'][$blocked['recipient']]);
       if ($form) {
-        drupal_set_message($blocked['message'], 'warning');
+        if ($display_block_messages) {
+          drupal_set_message($blocked['message'], 'warning');
+        }
       }
       else {
         $messages['warning'][] = $blocked['message'];
@@ -1387,14 +1519,14 @@ function _privatemsg_send($message) {
 
   drupal_alter('privatemsg_message_presave', $message);
 
-  $index_sql = "INSERT INTO {pm_index} (mid, thread_id, uid, is_new, deleted) VALUES (%d, %d, %d, %d, 0)";
+  $index_sql = "INSERT INTO {pm_index} (mid, thread_id, recipient, type, is_new, deleted) VALUES (%d, %d, %d, '%s', %d, 0)";
   if (isset($message['read_all']) && $message['read_all']) {
     // The message was sent in read all mode, add the author as recipient to all
     // existing messages.
     $query_messages = _privatemsg_assemble_query('messages', array($message['thread_id']), NULL);
     $conversation = db_query($query_messages['query']);
     while ($result = db_fetch_array($conversation)) {
-      if (!db_query($index_sql, $result['mid'], $message['thread_id'], $message['author']->uid, 0)) {
+      if (!db_query($index_sql, $result['mid'], $message['thread_id'], $message['author']->uid, 'user', 0)) {
         return FALSE;
       }
     }
@@ -1420,7 +1552,7 @@ function _privatemsg_send($message) {
   // 2) Save message to recipients.
   // Each recipient gets a record in the pm_index table.
   foreach ($message['recipients'] as $recipient) {
-    if (!db_query($index_sql, $mid, $message['thread_id'], $recipient->uid, 1) ) {
+    if (!db_query($index_sql, $mid, $message['thread_id'], _privatemsg_get_recipient_property($recipient), $recipient->type, 1) ) {
       // We assume if one insert failed then the rest may fail too against the
       // same table.
       return FALSE;
@@ -1432,7 +1564,7 @@ function _privatemsg_send($message) {
   $is_new = isset($message['recipients'][$message['author']->uid]) ? 1 : 0;
 
   // Also add a record for the author to the pm_index table.
-  if (!db_query($index_sql, $mid, $message['thread_id'], $message['author']->uid, $is_new)) {
+  if (!db_query($index_sql, $mid, $message['thread_id'], $message['author']->uid, 'user', $is_new)) {
     return FALSE;
   }
 
@@ -1709,9 +1841,30 @@ function privatemsg_thread_change_status
     global $user;
     $account = drupal_clone($user);
   }
-  // Merge status and uid with the threads list. array_merge() will not overwrite/ignore thread_id 1.
-  $params = array_merge(array($status, $account->uid), $threads);
-  db_query('UPDATE {pm_index} SET is_new = %d WHERE uid = %d AND thread_id IN ('. db_placeholders($threads) .')', $params);
+
+  $result = db_query("SELECT thread_id FROM {pm_index} WHERE recipient = %d and type IN ('hidden', 'user') AND thread_id IN (" . db_placeholders($threads) . ') GROUP BY thread_id', array_merge(array($account->uid), $threads));
+  $exists = array();
+  while ($row = db_fetch_object($result)) {
+    $exists[] = $row->thread_id;
+  }
+
+  // Merge status and uid with the exising thread list. We only update threads
+  // that already have an user or hidden entry.
+  $params = array_merge(array($status, $account->uid), $exists);
+  db_query("UPDATE {pm_index} SET is_new = %d WHERE recipient = %d and type IN ('user', 'hidden') AND thread_id IN (" . db_placeholders($threads) . ')', $params);
+
+  foreach ($threads as $thread_id) {
+    if (!in_array($thread_id, $exists)) {
+      // Add a new hidden entry if the recipient does not exist.
+      // Load all messages of that thread.
+      $query = _privatemsg_assemble_query('messages', array($thread_id), $account);
+      $result = db_query($query['query']);
+      while ($row = db_fetch_array($result)) {
+        $index_sql = "INSERT INTO {pm_index} (mid, thread_id, recipient, type, is_new, deleted) VALUES (%d, %d, %d, '%s', %d, %d)";
+        db_query($index_sql, $row->mid, $thread_id, $account->uid, 'hidden', 0, 0);
+      }
+    }
+  }
 
   if ($status == PRIVATEMSG_UNREAD) {
     drupal_set_message(format_plural(count($threads), 'Marked 1 thread as unread.', 'Marked @count threads as unread.'));
@@ -1814,15 +1967,17 @@ function privatemsg_thread_change_delete
 function privatemsg_privatemsg_block_message($author, $recipients) {
   $blocked = array();
   if (privatemsg_is_disabled($author)) {
-    $blocked[] = array('uid' => $author->uid,
-                       'message' => t('You have disabled private message sending and receiving.'),
-                 );
+    $blocked[] = array(
+      'recipient' => $author->uid,
+      'message' => t('You have disabled private message sending and receiving.'),
+    );
   }
   foreach($recipients as $recipient) {
     if (privatemsg_is_disabled($recipient)) {
-      $blocked[] = array('uid' => $recipient->uid,
-                       'message' => t('%recipient has disabled private message receiving.', array('%recipient' => $recipient->name))
-                 );
+      $blocked[] = array(
+        'recipient' => $recipient->uid,
+        'message' => t('%recipient has disabled private message receiving.', array('%recipient' => $recipient->name)),
+      );
     }
   }
 
@@ -1917,4 +2072,25 @@ function privatemsg_views_api() {
     'api' => 2,
     'path' => drupal_get_path('module', 'privatemsg') . '/views',
   );
-}
\ No newline at end of file
+}
+
+/**
+ * Get the recipient key of a recipient object.
+ */
+function _privatemsg_get_recipient_property($recipient, $property = 'key') {
+  // If set, simply return the recipient key.
+  if (isset($recipient->recipient)) {
+    return $recipient->recipient;
+  }
+  // Set default type if not existing.
+  if (!isset($recipient->type)) {
+    $recipient->type = 'user';
+    $recipient->user_key = 'uid';
+  }
+  // The property which stores the recipient id is stored in {type}_{property}.
+  $property_name = $recipient->type . '_' . $property;
+  // Get the value of that property.
+  $property = $recipient->$property_name;
+  // Return recipient id based on recipient type.
+  return $recipient->$property;
+}
Index: privatemsg.pages.inc
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.pages.inc,v
retrieving revision 1.1
diff -u -p -r1.1 privatemsg.pages.inc
--- privatemsg.pages.inc	2 Dec 2009 20:03:59 -0000	1.1
+++ privatemsg.pages.inc	1 Mar 2010 17:03:03 -0000
@@ -195,20 +195,28 @@ function privatemsg_new(&$form_state, $r
   $to_themed = array();
   $blocked = FALSE;
   foreach ($recipients as $recipient) {
+    if (isset($recipient->type) && $recipient->type == 'hidden') {
+      continue;
+    }
     if (in_array($recipient->name, $to)) {
       // 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) {
+    $user_blocked = module_invoke_all('privatemsg_block_message', $user, array($recipient->recipient => $recipient));
+    if (!count($user_blocked) <> 0 && $recipient->recipient) {
+      if ($recipient->recipient == $user->uid) {
         $usercount++;
         // Skip putting author in the recipients list for now.
         continue;
       }
-      $to[] = $recipient->name;
-      $to_themed[$recipient->uid] = theme('username', $recipient);
+      if (!empty($recipient->name)) {
+        $to[] = $recipient->name;
+      }
+      else {
+        $to[] = _privatemsg_get_recipient_property($recipient, 'name');
+      }
+      $to_themed[$recipient->type . '_' . $recipient->recipient] = _privatemsg_format_participant($recipient);
     }
     else {
       // Recipient list contains blocked users.
@@ -219,7 +227,7 @@ function privatemsg_new(&$form_state, $r
   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->name;
-    $to_themed[$user->uid] = theme('username', $user);
+    $to_themed[$user->uid] = _privatemsg_format_participant($user);
   }
 
   if (!empty($to)) {
@@ -363,12 +371,11 @@ function privatemsg_new_validate($form, 
     // Load participants.
     $message['recipients'] = _privatemsg_load_thread_participants($message['thread_id']);
     // Remove author.
-    if (isset($message['recipients'][$message['author']->uid]) && count($message['recipients']) > 1) {
-      unset($message['recipients'][$message['author']->uid]);
+    if (isset($message['recipients']['user_' . $message['author']->uid]) && count($message['recipients']) > 1) {
+      unset($message['recipients']['user_' . $message['author']->uid]);
     }
   }
-
-  $validated = _privatemsg_validate_message($message, TRUE);
+  $validated = _privatemsg_validate_message($message, TRUE, FALSE);
   foreach ($validated['messages'] as $type => $text) {
     drupal_set_message($text, $type);
   }
@@ -387,7 +394,12 @@ function privatemsg_new_submit($form, &$
   // Load usernames to which the message was sent to.
   $recipient_names = array();
   foreach ($form_state['validate_built_message']['recipients'] as $recipient) {
-    $recipient_names[] = theme('username', $recipient);
+    if (isset($recipient->uid)) {
+      $recipient_names[] = _privatemsg_format_participant($recipient);
+    }
+    else {
+      $recipient_names[] = $recipient->name;
+    }
   }
   if ($status !== FALSE )  {
     drupal_set_message(t('A message has been sent to !recipients.', array('!recipients' => implode(', ', $recipient_names))));
@@ -641,19 +653,35 @@ function privatemsg_user_name_autocomple
       $names[$name] = $name;
     }
   }
-  // By using user_validate_user we can ensure that names included in $names are at least logisticaly possible.
   // 2: Find the next user name suggestion.
   $fragment = array_pop($names);
   $matches = array();
   if (!empty($fragment)) {
-    $query = _privatemsg_assemble_query('autocomplete', $fragment, $names);
-    $result = db_query_range($query['query'], $fragment, 0, 10);
-    $prefix = count($names) ? implode(", ", $names) .", " : '';
-    // 3: Build proper suggestions and print.
-    while ($user = db_fetch_object($result)) {
-      $matches[$prefix . $user->name .", "] = $user->name;
+    $remaining = 10;
+    foreach (module_implements('privatemsg_recipient_autocomplete') as $module) {
+      $matches += module_invoke($module, 'privatemsg_recipient_autocomplete', $fragment, $names, $remaining);
+      $remaining = 10 - count($matches);
+      if ($remaining <= 0) {
+        break;
+      }
+    }
+    if ($remaining > 0) {
+      $query = _privatemsg_assemble_query('autocomplete', $fragment, $names);
+      $result = db_query_range($query['query'], $fragment, 0, $remaining);
+      // 3: Build proper suggestions and print.
+      while ($user = db_fetch_object($result)) {
+        $matches[] = $user->name;
+      }
     }
   }
+  // Prefix the matches and convert them to the correct form for the
+  // autocomplete.
+  $prefix = count($names) ? implode(", ", $names) .", " : '';
+  $suggestions = array();
+  foreach ($matches as $match) {
+    $suggestions[$prefix . $match . ', '] = $match;
+  }
+
   // convert to object to prevent drupal bug, see http://drupal.org/node/175361
-  drupal_json((object)$matches);
+  drupal_json((object)$suggestions);
 }
\ No newline at end of file
Index: privatemsg.test
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.test,v
retrieving revision 1.7
diff -u -p -r1.7 privatemsg.test
--- privatemsg.test	17 Feb 2010 10:05:45 -0000	1.7
+++ privatemsg.test	1 Mar 2010 17:03:05 -0000
@@ -315,7 +315,7 @@ class PrivatemsgTestCase extends DrupalW
     );
 
     $this->drupalPost(NULL, $reply, t('Send message'));
-    $this->assertText(t('A message has been sent to @recipient, @author.', array('@recipient' => $recipient->name, '@author' => $author->name)), 'Reply has been sent.');
+    $this->assertText(t('A message has been sent to @author, @recipient.', array('@recipient' => $recipient->name, '@author' => $author->name)), 'Reply has been sent.');
     $this->assertText($reply['body'], 'New message body displayed.');
 
     $this->drupalPost(NULL, $replyempty, t('Send message'));
@@ -461,7 +461,7 @@ class PrivatemsgTestCase extends DrupalW
     $this->assertText($edit['body'], t('First message body displayed.'));
     $this->assertText($admin_edit['body'], t('New message body displayed.'));
 
-    $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE uid = %d AND thread_id = %d", $admin->uid, 1));
+    $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE recipient = %d AND thread_id = %d", $admin->uid, 1));
     $this->assertEqual($admin_recipient_count, 2, t('Admin is listed as recipient for every message once.'));
 
 
@@ -476,7 +476,7 @@ class PrivatemsgTestCase extends DrupalW
     $this->assertText($admin_edit['body'], t('Second response body displayed.'));
     $this->assertText($admin_edit2['body'], t('Third message body displayed.'));
 
-    $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE uid = %d AND thread_id = %d", $admin->uid, 1));
+    $admin_recipient_count = db_result(db_query("SELECT COUNT(*) FROM {pm_index} WHERE recipient = %d AND thread_id = %d", $admin->uid, 1));
     $this->assertEqual($admin_recipient_count, 3, t('Admin is listed as recipient for every message once.'));
 
   }
Index: pm_block_user/pm_block_user.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/pm_block_user/pm_block_user.module,v
retrieving revision 1.5
diff -u -p -r1.5 pm_block_user.module
--- pm_block_user/pm_block_user.module	18 Feb 2010 18:16:22 -0000	1.5
+++ pm_block_user/pm_block_user.module	1 Mar 2010 17:03:06 -0000
@@ -169,7 +169,7 @@ function pm_block_user_privatemsg_block_
     // parameter). Further below is a check whether the recipient may block it.
     if (_pm_block_user_rule_exists($author, $recipients[$uid], PM_BLOCK_USER_DISALLOW_SENDING)) {
       $blocked[] = array(
-        'uid' => $uid,
+        'recipient' => $uid,
         'message' => t('Sorry, private messaging rules forbid sending messages to !name.', array('!name' => $recipients[$uid]->name)),
       );
     }
@@ -184,7 +184,7 @@ function pm_block_user_privatemsg_block_
       continue;
     }
     $blocked[] = array(
-      'uid' => $row['recipient'],
+      'recipient' => $row['recipient'],
       'message' => t('%name has chosen to not recieve any more messages from you.', array('%name' => $recipients[$row['recipient']]->name))
     );
   }
@@ -194,7 +194,7 @@ function pm_block_user_privatemsg_block_
 function pm_block_user_privatemsg_sql_load_alter(&$fragments, $pmid, $uid) {
   $fragments['select'][] = 'pmbu.recipient AS is_blocked';
 
-  $fragments['inner_join'][] = 'LEFT JOIN {pm_block_user} pmbu ON (pm.author = pmbu.author AND pmi.uid = pmbu.recipient)';
+  $fragments['inner_join'][] = "LEFT JOIN {pm_block_user} pmbu ON (pm.author = pmbu.author AND pmi.recipient = pmbu.recipient AND pmi.type = 'user')";
 }
 
 /**
Index: pm_email_notify/pm_email_notify.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/pm_email_notify/pm_email_notify.module,v
retrieving revision 1.4
diff -u -p -r1.4 pm_email_notify.module
--- pm_email_notify/pm_email_notify.module	12 Feb 2010 08:39:49 -0000	1.4
+++ pm_email_notify/pm_email_notify.module	1 Mar 2010 17:03:06 -0000
@@ -52,7 +52,7 @@ function _pm_email_notify_is_enabled($ui
 function pm_email_notify_privatemsg_message_insert($message) {
   foreach ($message['recipients'] as $recipient) {
     // check if recipient enabled email notifications
-    if (_pm_email_notify_is_enabled($recipient->uid)) {
+    if (isset($recipient->uid) && _pm_email_notify_is_enabled($recipient->uid)) {
       // send them a new pm notification email if they did
       $params['recipient'] = $recipient;
       $params['message'] = $message;
Index: privatemsg_filter/privatemsg_filter.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg_filter/privatemsg_filter.module,v
retrieving revision 1.3
diff -u -p -r1.3 privatemsg_filter.module
--- privatemsg_filter/privatemsg_filter.module	2 Dec 2009 20:04:00 -0000	1.3
+++ privatemsg_filter/privatemsg_filter.module	1 Mar 2010 17:03:09 -0000
@@ -589,7 +589,7 @@ function privatemsg_filter_privatemsg_sq
     $count = 0;
     if (isset($filter['tags']) && !empty($filter['tags'])) {
       foreach ($filter['tags'] as $tag) {
-        $fragments['inner_join'][]  = "INNER JOIN {pm_tags_index} pmti$count ON (pmti$count.thread_id = pmi.thread_id AND pmti$count.uid = pmi.uid)";
+        $fragments['inner_join'][]  = "INNER JOIN {pm_tags_index} pmti$count ON (pmti$count.thread_id = pmi.thread_id AND pmti$count.uid = pmi.recipient AND pmi.type = 'user')";
         $fragments['where'][]       = "pmti$count.tag_id = %d";
         $fragments['query_args']['where'][]   = $tag;
         $count++;
@@ -599,7 +599,7 @@ function privatemsg_filter_privatemsg_sq
     if (isset($filter['author']) && !empty($filter['author'])) {
       foreach ($filter['author'] as $author) {
         $fragments['inner_join'][]  = "INNER JOIN {pm_index} pmi$count ON (pmi$count.mid = pm.mid)";
-        $fragments['where'][]       = "pmi$count.uid = %d";
+        $fragments['where'][]       = "pmi$count.recipient = %d AND type = 'user'";
         $fragments['query_args']['where'][]   = $author->uid;
         $count++;
       }
@@ -752,10 +752,10 @@ function privatemsg_filter_privatemsg_sq
   // @todo: Check if these results can be grouped to avoid unecessary loops.
   if (arg(1) == 'filter') {
     // JOIN on index entries where the to be selected user is a recipient.
-    $fragments['inner_join'][] = 'INNER JOIN {pm_index} pip ON pip.uid = u.uid';
+    $fragments['inner_join'][] = "INNER JOIN {pm_index} pip ON pip.recipient = u.uid AND pip.type = 'user'";
     // JOIN on rows where the current user is the recipient and that have the
     // same mid as those above.
-    $fragments['inner_join'][] = 'INNER JOIN {pm_index} piu ON piu.uid = %d AND pip.mid = piu.mid';
+    $fragments['inner_join'][] = "INNER JOIN {pm_index} piu ON piu.recipient = u.uid AND piu.type = 'user' AND pip.mid = piu.mid";
     $fragments['query_args']['join'][] = $user->uid;
   }
 }
Index: privatemsg_rules/privatemsg_rules.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg_rules/privatemsg_rules.module,v
retrieving revision 1.1
diff -u -p -r1.1 privatemsg_rules.module
--- privatemsg_rules/privatemsg_rules.module	6 Jan 2010 18:41:33 -0000	1.1
+++ privatemsg_rules/privatemsg_rules.module	1 Mar 2010 17:03:09 -0000
@@ -5,7 +5,9 @@
  */
 function privatemsg_rules_privatemsg_message_insert($message) {
   foreach ($message['recipients'] as $recipient) {
-    rules_invoke_event('privatemsg_insert', $message['subject'], $message['author'], $recipient, $message['mid'], $message['thread_id']);
+    if (!isset($recipient->type) || $recipient->type == 'user') {
+      rules_invoke_event('privatemsg_insert', $message['subject'], $message['author'], $recipient, $message['mid'], $message['thread_id']);
+    }
   }
 }
 ?>
