Index: privatemsg.module
===================================================================
RCS file: /cvs/drupal/contributions/modules/privatemsg/privatemsg.module,v
retrieving revision 1.70.2.30.2.91.2.25
diff -u -p -r1.70.2.30.2.91.2.25 privatemsg.module
--- privatemsg.module	22 Feb 2009 21:21:08 -0000	1.70.2.30.2.91.2.25
+++ privatemsg.module	24 Feb 2009 12:05:49 -0000
@@ -654,7 +654,6 @@ function privatemsg_new(&$form_state, $a
 }
 
 function pm_send_validate($form, &$form_state) {
-//  drupal_set_message('<pre>'. print_r($form_state['values'], 1) . '</pre>');
   // The actual message that is being sent, we create this during validation and pass to submit to send out.
   $message = array();
 
@@ -698,25 +697,13 @@ function pm_send_validate($form, &$form_
       $invalid[$name] = $name;
     }
   }
+  if (!empty($invalid)) {
+    drupal_set_message(t('The following recipients are invalid: @invalid', array('@invalid' => implode(", ", $invalid))), 'error');
+  }
 
-  // Verify that our recipients are valid.
-  /**
-   * VALIDATE NAMES
-   * 1) Make sure the name exists.
-   * 2) Make sure he accepts private messages.
-   * 3) Make sure the sender is not on block list of the recipient.
-   */
-  /**
-   * BUILD VALID RECIPIENT LIST
-   * 1) Names that were not valid from previous step will be stripped out.
-   * 2) Names that remain will be put into a recipients array.
-   */
-
-  $errors = _privatemsg_validate_message($message, $message['author'], TRUE);
-  if (!empty($errors)) {
-      foreach ($errors as $error) {
-        form_set_error('body', $error);
-      }
+  $validated = _privatemsg_validate_message($message, TRUE);
+  foreach ($validated['messages'] as $type => $message) {
+    drupal_set_message($message, $type);
   }
 
   $form_state['validate_built_message'] = $message;
@@ -1157,13 +1144,11 @@ function privatemsg_new_thread($subject,
   $message['timestamp'] = time();
   $message['recipients'] = $recipients;
 
-  $errors = _privatemsg_validate_message($message, $author);
-  if (!empty($errors)) {
-    return $errors;
-  }
-  else {
-    return _privatemsg_send($message);
+  $validated = _privatemsg_validate_message($message);
+  if ($validated['success']) {
+    $validated['success'] = _privatemsg_send($message);
   }
+  return $validated;
 }
 /**
  * Send a reply message
@@ -1205,51 +1190,67 @@ function privatemsg_reply($thread_id, $b
 
   $message['subject'] = $first_message['subject'];
 
-  $errors = _privatemsg_validate_message($message, $author);
-  if (!empty($errors)) {
-    return $errors;
-  }
-  else {
-    return _privatemsg_send($message);
+  $validated = _privatemsg_validate_message($message);
+  if ($validated['success']) {
+    $validated['success'] = _privatemsg_send($message);
   }
+  return $validated;
 }
 
-function _privatemsg_validate_message(&$message, $author, $show_warnings = FALSE) {
-
-  $errors = array();
-  if (!privatemsg_user_access('write privatemsg', $author)) {
+function _privatemsg_validate_message(&$message, $form = FALSE) {
+  if (!privatemsg_user_access('write privatemsg', $message['author'])) {
     // no need to do further checks in this case...
-    return array(t('User @user is not allowed to write messages', array('@user' => $author->name)));
+    if ($form) {
+      form_set_error('author', t('User @user is not allowed to write messages', array('@user' => $author->name)));
+    }
+    else {
+     return array('messages' => array ('error' => array(t('User @user is not allowed to write messages', array('@user' => $author->name)))));
+    }
   }
 
+  $messages = array();
   if (empty($message['subject'])) {
-    $errors[] = t('Disallowed to send a message without subject');
+    if ($form) {
+      form_set_error('subject', t('Disallowed to send a message without subject'));
+    }
+    else {
+      $messages['error'][] = t('Disallowed to send a message without subject');
+    }
   }
 
   if (empty($message['recipients']) || !is_array($message['recipients'])) {
-    $errors[] = t('Disallowed to send a message without atleast one recipient');
+    if ($form) {
+      form_set_error('to', t('Disallowed to send a message without atleast one valid recipient'));
+    }
+    else {
+      $messages['error'][] = t('Disallowed to send a message without atleast one valid recipient');
+    }
   }
 
   if (!empty($message['recipients']) && is_array($message['recipients'])) {
     foreach ($message['recipients'] as $uid => $recipient) {
-      $block_results = module_invoke_all('privatemsg_block_message', $author, $recipient);
+      $block_results = module_invoke_all('privatemsg_block_message', $message['author'], $recipient);
       if (count($block_results) > 0) {
         unset($message['recipients'][$uid]);
-        if ($show_warnings) {
-          foreach ($block_results as $block_result) {
-            drupal_set_message($block_result);
-          }
-        }
+        $messages['warning'] += $block_results;
       }
     }
   }
 
   // Check again, give another error message if all recipients are blocked
   if (empty($message['recipients'])) {
-    $errors[] = t('Disallowed to send message because all recipients are blocked');
+    if ($form) {
+      form_set_error('to', t('Disallowed to send message because all recipients are blocked'));
+    }
+    else {
+      $messages['error'][] = t('Disallowed to send message because all recipients are blocked');
+    }
   }
 
-  return $errors += module_invoke_all('privatemsg_message_validate', $message);
+  $messages += module_invoke_all('privatemsg_message_validate', $message, $form);
+  return array('success'  => !(bool) (empty($messages['error']) || count($messages['error'])),
+               'messages'   => $messages,
+  );
 }
 
 function _privatemsg_send($message) {
