Index: privatemsg.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.module,v
retrieving revision 1.70.2.30.2.91.2.58
diff -u -p -r1.70.2.30.2.91.2.58 privatemsg.module
--- privatemsg.module	10 Jul 2009 16:53:18 -0000	1.70.2.30.2.91.2.58
+++ privatemsg.module	16 Jul 2009 23:21:30 -0000
@@ -815,14 +815,22 @@ function _privatemsg_parse_userstring($s
   return array($users, $invalid);
 }
 
+/**
+ * Submit callback for the privatemsg_new form.
+ */
 function pm_send($form, &$form_state) {
-  if (_privatemsg_send($form_state['validate_built_message'])) {    // 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);
-    }
+  $status = _privatemsg_send($form_state['validate_built_message']);
+  // 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 ($status !== FALSE )  {
     drupal_set_message(t('A message has been sent to !recipients.', array('!recipients' => implode(', ', $recipient_names))));
   }
+  else {
+    drupal_set_message(t('An attempt to send message <em>may have failed</em> when sending to !recipients.', array('!recipients' => implode(', ', $recipient_names))), 'error');
+  }
 }
 
 function pm_preview($form, &$form_state) {
@@ -1157,6 +1165,7 @@ function privatemsg_delete_submit($form,
   }
   $form_state['redirect'] = 'messages';
 }
+
 /**
  * Send a new message.
  *
@@ -1178,7 +1187,16 @@ function privatemsg_delete_submit($form,
  *     timestamp => Time when the message was sent
  *
  * @return
- *   Either true or an array with validation errors
+ *   An array with a key success. If TRUE, it also contains a key 'message' with
+ *   the created $message array, the same that is passed to the insert hook.
+ *   If FALSE, it contains a key 'messages'. This key contains an array where
+ *   the key is the error type (error, warning, notice) and an array with
+ *   messages of that type.
+ *
+ *   Example:
+ *   @code
+ *   array('error' => array('A error message'))
+ *   @endcode
  *
  * @ingroup api
  */
@@ -1203,7 +1221,13 @@ function privatemsg_new_thread($recipien
 
   $validated = _privatemsg_validate_message($message);
   if ($validated['success']) {
-    $validated['success'] = _privatemsg_send($message);
+    $return = _privatemsg_send($message);
+    if ($return === FALSE) {
+      $validated['sucess'] = FALSE;
+    }
+    else {
+      $validated['message'] = $return;
+    }
   }
 
   return $validated;
@@ -1223,7 +1247,16 @@ function privatemsg_new_thread($recipien
  *     timestamp => Time when the message was sent
  *
  * @return
- *   Either true or an array with validation errors
+ *   An array with a key success and messages. This key contains an array where
+ *   the key is the error type (error, warning, notice) and an array with
+ *   messages of that type.. If success is TRUE, it also contains a key $message
+ *   with the created $message array, the same that is passed to
+ *   hook_privatemsg_message_insert().
+ *
+ *   Example messages values:
+ *   @code
+ *   array('error' => array('A error message'))
+ *   @endcode
  *
  * @ingroup api
  */
@@ -1264,7 +1297,13 @@ function privatemsg_reply($thread_id, $b
 
   $validated = _privatemsg_validate_message($message);
   if ($validated['success']) {
-    $validated['success'] = _privatemsg_send($message);
+    $return = _privatemsg_send($message);
+    if ($return === FALSE) {
+      $validated['sucess'] = FALSE;
+    }
+    else {
+      $validated['message'] = $return;
+    }
   }
   return $validated;
 }
@@ -1329,13 +1368,25 @@ function _privatemsg_validate_message(&$
   }
 
   $messages += module_invoke_all('privatemsg_message_validate', $message, $form);
-  $success = empty($messages['error']);
+  // Check if there are errors in $messages or if $form is TRUE, there are form errors.
+  $success = empty($messages['error']) || ($form && count((array)form_get_errors()) > 0);
   return array(
     'success'  => $success,
     'messages'   => $messages,
   );
 }
 
+/**
+ * Internal function to save a message.
+ *
+ * @param $message
+ *   A $message array with the data that should be saved. If a thread_id exists
+ *   it will be created as a reply to an existing thread. If not, a new thread
+ *   will be created.
+ *
+ * @return
+ *   The updated $message array.
+ */
 function _privatemsg_send($message) {
 
   drupal_alter('privatemsg_message_presave', $message);
@@ -1358,22 +1409,24 @@ function _privatemsg_send($message) {
 
   // 2) Save message to recipients.
   // Each recipient gets a record in the pm_index table.
-
   $query = "INSERT INTO {pm_index} (mid, thread_id, uid, is_new, deleted) VALUES (%d, %d, %d, %d, 0)";
   foreach ($message['recipients'] as $recipient) {
-
-    db_query($query, $mid, $message['thread_id'], $recipient->uid, 1);
+    if ( !db_query($query, $mid, $message['thread_id'], $recipient->uid, 1) ) {
+      return FALSE;  // We assume if one insert failed then the rest may fail too against the same table
+    }
   }
 
   // When author is also the recipient, we want to set message to UNREAD. all other times his message is set to READ.
   $is_new = isset($message['recipients'][$message['author']->uid]) ? 1 : 0;
 
   // Also add a record for the author to the pm_index table.
-  db_query($query, $mid, $message['thread_id'], $message['author']->uid, $is_new);
+  if ( !db_query($query, $mid, $message['thread_id'], $message['author']->uid, $is_new)) {
+    return FALSE; // same as above
+  }
 
   module_invoke_all('privatemsg_message_insert', $message);
 
-  return TRUE;
+  return $message;  // if we reached here that means we were successful at writing all messages to db
 }
 
 /**
@@ -1718,7 +1771,7 @@ function _privatemsg_list_headers($has_p
 
 /**
  * Table header definition for themes that don't support theme patterns.
- * 
+ *
  * @return
  *   Array with the correct headers.
  */
@@ -1730,7 +1783,7 @@ function _privatemsg_list_headers_fallba
       $header[$key] = $theme_function();
     }
   }
-  
+
   return $header;
 }
 
@@ -1778,7 +1831,7 @@ function _privatemsg_list_thread_fallbac
       $row_data[$key] = $theme_function($thread);
     }
   }
-  
+
   return $row_data;
 }
 
