Index: signup.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/signup/signup.module,v
retrieving revision 1.131
diff -u -r1.131 signup.module
--- signup.module	23 May 2008 10:18:32 -0000	1.131
+++ signup.module	25 Jul 2008 19:07:30 -0000
@@ -496,17 +496,85 @@
  *   Node id of the node the user is being signed up for.
  */
 function signup_form_validate_username($form, $nid) {
-  $username = $form['#value'];
+  signup_validate_username($nid, $form['#value'], 'signup_username');
+}
+
+/**
+ * Validates a username
+ *
+ * @param $nid The node the user is signing up for.
+ * @param $username The username to validate.
+ * @param $name Optional. The form element being validated.
+ *
+ * @return Boolean.  TRUE if the address validates, FALSE otherwise.
+ */
+function signup_validate_username($nid, $username, $name = FALSE) {
   $account = user_load(array('name' => $username));
   if (empty($account)) {
-    form_error($form, t('User %username does not exist.', array('%username' => $username)));
+    $message = t('User %username does not exist.', array('%username' => $username));
   }
   elseif (!user_access('sign up for content', $account)) {
-    form_error($form, t('User !user does not have permission to sign up.', array('!user' => theme('username', $account))));
+    $message = t('User !user does not have permission to sign up.', array('!user' => theme('username', $account)));
   }
   elseif (db_result(db_query("SELECT COUNT(*) FROM {signup_log} WHERE uid = %d AND nid = %d", $account->uid, $nid)) > 0) {
     $node = node_load($nid);
-    form_error($form, t('User !user is already signed up for %title', array('!user' => theme('username', $account), '%title' => $node->title)));
+    $message = t('User !user is already signed up for %title', array('!user' => theme('username', $account), '%title' => $node->title));
+  }
+  
+  // If there's no message, it's a valid email, so return success.
+  if (!isset($message)) {
+    return TRUE;
+  }
+
+  // Depending on how we were called, propagate the error accordinly.
+  if ($name) {
+    form_set_error($name, $message);
+  }
+  else {
+    drupal_set_message($message, 'error');
+  }
+  return FALSE;
+}
+
+/**
+ * Validates the username or email address on the admin form to signup another user.
+ *
+ * @param &$form
+ *   Form array for the username field. We pass by reference so we can break out the value into
+ *   the right fields for usernames or emails.
+ * @param $nid
+ *   Node id of the node the user is being signed up for.
+ */
+function signup_form_validate_username_or_anon(&$form, $nid) {
+  dpr($form);
+  $username = $form['#value'];
+  $account = user_load(array('name' => $username));
+  if (!empty($account)) {
+    if (!user_access('sign up for content', $account)) {
+      form_error($form, t('User !user does not have permission to sign up.', array('!user' => theme('username', $account))));
+    }
+    elseif (db_result(db_query("SELECT COUNT(*) FROM {signup_log} WHERE uid = %d AND nid = %d", $account->uid, $nid)) > 0) {
+      $node = node_load($nid);
+      form_error($form, t('User !user is already signed up for %title', array('!user' => theme('username', $account), '%title' => $node->title)));
+    }
+    else {
+      // We found a valid user, so we don't need to worry about anonymous users
+      return;
+    }
+  }
+  
+  // Now, check for anonymous users.
+  if(valid_email_address($username)) {
+    // It's a valid email address, and we didn't find a user, so let other functions set any errors
+    // Unset username, move data to signup_anon_mail
+    $form['signup_anon_mail'] = array(
+      '#value' => $form['#value'],
+    );
+    signup_validate_anon_email($nid, $form['#value'], 'signup_anon_mail');
+  }
+  else {
+    // It's not a valid email address, and not a valid user, so set our own error
+    form_error($form, t('User or email address %username does not exist or is not a valid email address.', array('%username' => $username)));
   }
 }
 
@@ -880,11 +948,11 @@
   elseif ($signup_type == 'admin') {
     $admin_form = array();
     $admin_form['signup_username'] = array(
-      '#title' => t('Username'),
+      '#title' => t('Username or email address for an unregistered user'),
       '#type' => 'textfield',
       '#autocomplete_path' => 'user/autocomplete',
       '#maxlength' => USERNAME_MAX_LENGTH,
-      '#validate' => array('signup_form_validate_username' => array($node->nid)),
+      '#validate' => array('signup_form_validate_username_or_anon' => array($node->nid)),
       '#size' => 40,
       '#weight' => -1,
       '#required' => TRUE,
