Index: modules/contact/contact.js
===================================================================
RCS file: modules/contact/contact.js
diff -N modules/contact/contact.js
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/contact/contact.js	15 Sep 2008 02:02:01 -0000
@@ -0,0 +1,36 @@
+// $Id$
+
+Drupal.behaviors.contact = function(context) {
+  var cookie_parts = new Array("name", "mail");
+  var form_parts = new Array("from", "mail");
+  var cookie = '';
+  for (i=0;i<2;i++) {
+    cookie = Drupal.contact.getCookie('comment_info_' + cookie_parts[i]);
+    if (cookie != '') {
+      $("#contact-mail-user input[name=" + form_parts[i] + "]:not(.comment-processed)", context)
+        .val(cookie)
+        .addClass('comment-processed');
+    }
+  }
+};
+
+Drupal.contact = {};
+
+Drupal.contact.getCookie = function(name) {
+  var search = name + '=';
+  var returnValue = '';
+
+  if (document.cookie.length > 0) {
+    offset = document.cookie.indexOf(search);
+    if (offset != -1) {
+      offset += search.length;
+      var end = document.cookie.indexOf(';', offset);
+      if (end == -1) {
+        end = document.cookie.length;
+      }
+      returnValue = decodeURIComponent(document.cookie.substring(offset, end).replace(/\+/g, '%20'));
+    }
+  }
+
+  return returnValue;
+};
Index: modules/contact/contact.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/contact/contact.module,v
retrieving revision 1.108
diff -u -p -r1.108 contact.module
--- modules/contact/contact.module	24 Jul 2008 16:25:17 -0000	1.108
+++ modules/contact/contact.module	15 Sep 2008 02:02:01 -0000
@@ -38,6 +38,7 @@ function contact_help($path, $arg) {
 function contact_perm() {
   return array(
     'access site-wide contact form' => t('Send feedback to administrators via e-mail using the site-wide contact form.'),
+    'access personal contact form' => t('Send feedback to users via e-mail using their personal contact form.'),
     'administer site-wide contact form' => t('Configure site-wide contact form administration settings.'),
   );
 }
@@ -113,12 +114,7 @@ function _contact_user_tab_access($accou
   if (!isset($account->contact)) {
     $account->contact = FALSE;
   }
-  return
-    $account && $user->uid &&
-    (
-      ($user->uid != $account->uid && $account->contact) ||
-      user_access('administer users')
-    );
+  return ($user->uid != $account->uid && $account->contact && user_access('access personal contact form')) || user_access('administer users');
 }
 
 /**
@@ -180,7 +176,7 @@ function contact_mail($key, &$message, $
       $account = $params['account'];
       $message['subject'] .= '[' . variable_get('site_name', 'Drupal') . '] ' . $params['subject'];
       $message['body'][] = "$account->name,";
-      $message['body'][] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array('!name' => $user->name, '!name-url' => url("user/$user->uid", array('absolute' => TRUE, 'language' => $language)), '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)), '!site' => variable_get('site_name', 'Drupal')), $language->language);
+      $message['body'][] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array('!name' => $user->name, '!name-url' => $user->uid ? url("user/$user->uid", array('absolute' => TRUE, 'language' => $language)) : $user->mail, '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)), '!site' => variable_get('site_name', 'Drupal')), $language->language);
       $message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", array('absolute' => TRUE, 'language' => $language))), $language->language);
       $message['body'][] = t('Message:', NULL, $language->language);
       $message['body'][] = $params['message'];
Index: modules/contact/contact.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/contact/contact.pages.inc,v
retrieving revision 1.11
diff -u -p -r1.11 contact.pages.inc
--- modules/contact/contact.pages.inc	16 Jul 2008 21:59:26 -0000	1.11
+++ modules/contact/contact.pages.inc	15 Sep 2008 02:02:01 -0000
@@ -23,6 +23,9 @@ function contact_site_page() {
   return $output;
 }
 
+/**
+ * Provides the form definition for the site-wide contact page.
+ */
 function contact_mail_page() {
   global $user;
 
@@ -157,10 +160,7 @@ function contact_mail_page_submit($form,
 function contact_user_page($account) {
   global $user;
 
-  if (!valid_email_address($user->mail)) {
-    $output = t('You need to provide a valid e-mail address to contact other users. Please update your <a href="@url">user information</a> and try again.', array('@url' => url("user/$user->uid/edit", array('query' => 'destination=' . drupal_get_destination()))));
-  }
-  else if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3)) && !user_access('administer site-wide contact form')) {
+  if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3)) && !user_access('administer site-wide contact form')) {
     $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
   }
   else {
@@ -171,59 +171,109 @@ function contact_user_page($account) {
   return $output;
 }
 
+/**
+ * Provides the form definition for the user contact page.
+ */
 function contact_mail_user(&$form_state, $recipient) {
   global $user;
-  $form['#token'] = $user->name . $user->mail;
-  $form['recipient'] = array('#type' => 'value', '#value' => $recipient);
-  $form['from'] = array('#type' => 'item',
-    '#title' => t('From'),
-    '#markup' => check_plain($user->name) . ' &lt;' . check_plain($user->mail) . '&gt;',
+  $form['#token'] = $user->uid ? $user->name . $user->mail : '';
+  $form['recipient'] = array(
+    '#type' => 'value',
+    '#value' => $recipient
+  );
+  $form['from'] = array('#type' => 'textfield',
+    '#title' => t('Your name'),
+    '#maxlength' => 255,
+    '#default_value' => $user->uid ? $user->name : '',
+    '#required' => TRUE,
+  );
+  $form['mail'] = array('#type' => 'textfield',
+    '#title' => t('Your e-mail address'),
+    '#maxlength' => 255,
+    '#default_value' => $user->uid ? $user->mail : '',
+    '#required' => TRUE,
   );
-  $form['to'] = array('#type' => 'item',
+  $form['to'] = array(
+    '#type' => 'item',
     '#title' => t('To'),
     '#markup' => check_plain($recipient->name),
   );
-  $form['subject'] = array('#type' => 'textfield',
+  $form['subject'] = array(
+    '#type' => 'textfield',
     '#title' => t('Subject'),
     '#maxlength' => 50,
     '#required' => TRUE,
   );
-  $form['message'] = array('#type' => 'textarea',
+  $form['message'] = array(
+    '#type' => 'textarea',
     '#title' => t('Message'),
     '#rows' => 15,
     '#required' => TRUE,
   );
-  $form['copy'] = array('#type' => 'checkbox',
-    '#title' => t('Send yourself a copy.'),
-  );
-  $form['submit'] = array('#type' => 'submit',
+  if ($user->uid) {
+    $form['copy'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Send yourself a copy.'),
+    );
+  }
+  else {
+    drupal_add_js(drupal_get_path('module', 'contact') . '/contact.js');
+    $form['copy'] = array(
+      '#type' => 'value',
+      '#value' => FALSE,
+    );
+  }
+  $form['submit'] = array(
+    '#type' => 'submit',
     '#value' => t('Send e-mail'),
   );
   return $form;
 }
 
 /**
+ * Validate the user contact page form submission.
+ */
+function contact_mail_user_validate($form, &$form_state) {
+  global $user;
+  if (!valid_email_address($form_state['values']['mail'])) {
+    form_set_error('mail', t('You must enter a valid e-mail address.'));
+  }
+  if (!$user->uid) {
+    foreach (array('from' => 'name', 'mail' => 'mail') as $form_field => $cookie_field) {
+      // Set cookie for 365 days.
+      if (isset($form_state['values'][$form_field])) {
+        setcookie('comment_info_' . $cookie_field, $form_state['values'][$form_field], $_SERVER['REQUEST_TIME'] + 31536000, '/');
+      }
+    }
+  }
+}
+
+/**
  * Process the personal contact page form submission.
  */
 function contact_mail_user_submit($form, &$form_state) {
   global $user, $language;
 
-  $account = $form_state['values']['recipient'];
+  $values = $form_state['values'];
+  $account = $values['recipient'];
 
   // Send from the current user to the requested user.
   $to = $account->mail;
-  $from = $user->mail;
+  $from = $values['mail'];
 
   // Save both users and all form values for email composition.
-  $values = $form_state['values'];
   $values['account'] = $account;
+  if (!$user->uid) {
+    $user->mail = $values['mail'];
+    $user->name = $values['from'];
+  }
   $values['user'] = $user;
 
   // Send the e-mail in the requested user language.
   drupal_mail('contact', 'user_mail', $to, user_preferred_language($account), $values, $from);
 
   // Send a copy if requested, using current page language.
-  if ($form_state['values']['copy']) {
+  if ($values['copy']) {
     drupal_mail('contact', 'user_copy', $from, $language, $values, $from);
   }
 
@@ -231,6 +281,7 @@ function contact_mail_user_submit($form,
   watchdog('mail', '%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name));
   drupal_set_message(t('The message has been sent.'));
 
-  // Back to the requested users profile page.
-  $form_state['redirect'] = "user/$account->uid";
+  // Back to the requested users profile page or the homepage if the
+  // user does not have access to user profiles.
+  $form_state['redirect'] = user_access('access user profiles') ? "user/$account->uid" : '';
 }
Index: modules/contact/contact.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/contact/contact.test,v
retrieving revision 1.8
diff -u -p -r1.8 contact.test
--- modules/contact/contact.test	2 Aug 2008 05:16:47 -0000	1.8
+++ modules/contact/contact.test	15 Sep 2008 02:02:01 -0000
@@ -60,7 +60,7 @@ class ContactSitewideTestCase extends Dr
     $recipients = array('simpletest@example.com', 'simpletest2@example.com', 'simpletest3@example.com');
     $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0])), '', TRUE);
     $this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
-    
+
     // Test update contact form category
     $categories = $this->getCategories();
     $category_id = $this->updateCategory($categories, $category = $this->randomName(16), $recipients_str = implode(',', array($recipients[0], $recipients[1])), $reply = $this->randomName(30), FALSE);
@@ -70,7 +70,7 @@ class ContactSitewideTestCase extends Dr
     $this->assertEqual($category_array['reply'], $reply);
     $this->assertFalse($category_array['selected']);
     $this->assertRaw(t('Category %category has been updated.', array('%category' => $category)), t('Category successfully updated.'));
-		
+
     $this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1])), '', FALSE);
     $this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
 
@@ -260,53 +260,103 @@ class ContactPersonalTestCase extends Dr
    * Test personal contact form.
    */
   function testPersonalContact() {
-    $admin_user = $this->drupalCreateUser(array('administer site-wide contact form'));
+    $admin_user = $this->drupalCreateUser(array('administer site-wide contact form', 'administer permissions'));
     $this->drupalLogin($admin_user);
 
-    // Enable the personal contact form.
+    // Set settings and make sure permissions to view user contact pages are disabled.
     $edit = array();
     $edit['contact_default_status'] = TRUE;
     $this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
     $this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));
-
-    // Reload variables.
+    $this->setPermission('anonymous user', array('access personal contact form' => FALSE));
+    $this->setPermission('authenticated user', array('access personal contact form' => FALSE));
     $this->drupalLogout();
 
-    // Create web users and attempt to use personal contact forms with default set to true.
+    // Create web users and attempt to use personal contact forms with
+    // default set to true but permissions disabled.
     $web_user1 = $this->drupalCreateUser(array());
     $web_user2 = $this->drupalCreateUser(array());
 
-    $this->drupalLogin($web_user1);
-
+    // Test denied access by anonymous user
     $this->drupalGet('user/' . $web_user2->uid . '/contact');
-    $this->assertResponse(200, t('Access to personal contact form granted.'));
-
-    $edit = array();
-    $edit['subject'] = $this->randomName(16);
-    $edit['message'] = $this->randomName(64);
-    $this->drupalPost(NULL, $edit, t('Send e-mail'));
-    $this->assertText(t('The message has been sent.'), t('Message sent.'));
+    $this->assertResponse(403, t('Access to personal contact form user denied to anonymous user.'));
 
+    // Test denied acces by registered user
+    $this->drupalLogin($web_user1);
+    $this->drupalGet('user/' . $web_user2->uid . '/contact');
+    $this->assertResponse(403, t('Access to personal contact form user denied to authenticated user.'));
     $this->drupalLogout();
 
+    // Give anonymous and regiseterd users permission to see personal
+    // contact pages and disable contact page for new users by default.
     $this->drupalLogin($admin_user);
-
-    // Disable the personal contact form.
-    $edit = array();
     $edit['contact_default_status'] = FALSE;
     $this->drupalPost('admin/build/contact/settings', $edit, t('Save configuration'));
     $this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.'));
-
-    // Reload variables.
+    $this->setPermission('anonymous user', array('access personal contact form' => TRUE));
+    $this->setPermission('authenticated user', array('access personal contact form' => TRUE));
     $this->drupalLogout();
 
-    // Create web users and attempt to use personal contact forms with default set to false.
+    // Create web users and attempt to use personal contact forms with
+    // default set to false but permissions enabled.
     $web_user3 = $this->drupalCreateUser(array());
     $web_user4 = $this->drupalCreateUser(array());
 
-    $this->drupalLogin($web_user3);
+    // Enable the contact form for one of the new users.
+    $this->drupalLogin($web_user4);
+    $edit_user = array();
+    $edit_user['contact'] = TRUE;
+    $this->drupalPost('user/' . $web_user4->uid . '/edit', $edit_user, t('Save'));
+    $this->assertText(t('The changes have been saved.'), t('User setting successfully saved.'));
+    $this->drupalLogout();
 
+    // Test allowed access and contact form sending by anonymous user.
+    $this->drupalGet('user/' . $web_user3->uid . '/contact');
+    $this->assertResponse(403, t('Access to personal contact form denied to anonymous user with permission.'));
+    $this->drupalGet('user/' . $web_user4->uid . '/contact');
+    $this->assertResponse(200, t('Access to personal contact form granted to anonymous user with permission.'));
+    $edit = array();
+    $edit['from'] = $this->randomName(16);
+    $edit['mail'] = 'simpletest@example.com';
+    $edit['subject'] = $this->randomName(16);
+    $edit['message'] = $this->randomName(64);
+    $this->drupalPost('user/' . $web_user4->uid . '/contact', $edit, t('Send e-mail'));
+    $this->assertText(t('The message has been sent.'), t('Message sent.'));
+    
+    // Test allowed access and contact form sending by registered user.
+    $this->drupalLogin($web_user1);
+    $this->drupalGet('user/' . $web_user3->uid . '/contact');
+    $this->assertResponse(403, t('Access to personal contact form denied to authenticated user with permission.'));
     $this->drupalGet('user/' . $web_user4->uid . '/contact');
-    $this->assertResponse(403, t('Access to personal contact form denied.'));
+    $this->assertResponse(200, t('Access to personal contact form granted to authenticated user with permission.'));
+    $edit = array();
+    $edit['subject'] = $this->randomName(16);
+    $edit['message'] = $this->randomName(64);
+    $this->drupalPost('user/' . $web_user4->uid . '/contact', $edit, t('Send e-mail'));
+    $this->assertText(t('The message has been sent.'), t('Message sent.'));
+    $this->drupalLogout();
+  }
+
+  /**
+   * Set permission.
+   *
+   * @param string $role User role to set permissions for.
+   * @param array $permissions Key-value array of permissions to set.
+   */
+  function setPermission($role, $permissions) {
+    // Get role id (rid) for specified role.
+    $rid = db_result(db_query("SELECT rid FROM {role} WHERE name = '%s'", array($role)));
+    if ($rid === FALSE) {
+      $this->fail(t('Role %role not found.', array('%role' => $role)), t('Permission'));
+    }
+
+    // Create edit array from permission.
+    $edit = array();
+    foreach ($permissions as $name => $value) {
+      $edit[$rid . '[' . $name . ']'] = $value;
+    }
+
+    $this->drupalPost('admin/user/permissions', $edit, t('Save permissions'));
+    $this->assertText(t('The changes have been saved.'), t('Saved changes.'), t('Permission'));
   }
 }
