Index: email_registration.module
===================================================================
--- email_registration.module (revision 144)
+++ email_registration.module (working copy)
@@ -14,38 +14,17 @@
 function email_registration_user($op, &$edit, &$account, $category = NULL) {
   switch ($op) {
     case 'insert':
-      // Other modules may implement hook_email_registration_name($edit, $account)
-      // to generate a username (return a string to be used as the username, NULL
-      // to have email_registration generate it)
-      $names = module_invoke_all('email_registration_name', $edit, $account);
-      // Remove any empty entries
-      $names = array_filter($names);

-      if (empty($names)) {
-        // Default implementation of name generation
-        $namenew = preg_replace('/@.*$/', '', $edit['mail']);
-        // if username generated from email record already exists, append underscore and number eg:(chris_123)
-        if (db_result(db_query("SELECT count(*) FROM {users} WHERE uid <> %d AND LOWER(name) = LOWER('%s')", $account->uid, $namenew)) > 0) {
-          // find the next number available to append to the name
-          $sql = "SELECT SUBSTRING_INDEX(name,'_',-1) FROM {users} WHERE name REGEXP '%s' ORDER BY CAST(SUBSTRING_INDEX(name,'_',-1) AS UNSIGNED) DESC LIMIT 1";
-          $nameidx = db_result(db_query($sql, '^'. $namenew .'_[0-9]+$'));
-          $namenew .= '_'. ($nameidx + 1);
-        }
-      }
-      else {
-        // One would expect a single implementation of the hook, but if there
-        // are multiples out there use the last one
-        $namenew = array_pop($names);
-      }
+      $namenew = email_registration_generate_name($edit, $account);

-      // replace with generated username
+      // Replace with generated username
       if (db_query("UPDATE {users} SET name = '%s' WHERE uid = '%s'", $namenew, $account->uid)) {
         $edit['name'] = $namenew; // update in the user array for access by other modules
       }

-      // if email verification is off and a new user is the one creating account, log the new user in with correct name
+      // If email verification is off and a new user is the one creating account, log the new user in with correct name
       global $user;
-      if (!variable_get('user_email_verification', 1) && $user->uid == 0) {  
+      if (!variable_get('user_email_verification', 1) && $user->uid == 0) {
         $user = $account;
         $user->name = $namenew;
       }
@@ -63,12 +42,12 @@
   switch ($form_id) {
     case 'user_register':
       if (isset($form['account']) && is_array($form['account'])) {
-        $form['account']['name']['#type'] = 'hidden';
+        $form['account']['name']['#type'] = 'value';
         $form['account']['name']['#value'] = user_password();
         $form['account']['mail']['#title'] = t('E-mail');
       }
       else {
-        $form['name']['#type'] = 'hidden';
+        $form['name']['#type'] = 'value';
         $form['name']['#value'] = user_password();
         $form['mail']['#title'] = t('E-mail');
       }
@@ -80,9 +59,16 @@
       break;

     case 'user_login':
-      $form['name']['#title'] = t('E-mail');
-      $form['name']['#description'] = t('Enter your e-mail address.');
-      $form['pass']['#description'] = t('Enter the password that accompanies your e-mail.');
+      if (variable_get('email_registration_login_with_username', FALSE)) {
+        $form['name']['#title'] = t('Username or E-mail Address');
+        $form['name']['#description'] = t('Enter your username or e-mail address.');
+        $form['pass']['#description'] = t('Enter the password that accompanies your username or e-mail address.');
+      }
+      else {
+        $form['name']['#title'] = t('E-mail');
+        $form['name']['#description'] = t('Enter your e-mail address.');
+        $form['pass']['#description'] = t('Enter the password that accompanies your e-mail.');
+      }
       $form['name']['#element_validate'][] = 'email_registration_user_login_validate';
       break;

@@ -90,6 +76,30 @@
       $form['name']['#title'] = t('E-mail');
       $form['name']['#element_validate'][] = 'email_registration_user_login_validate';
       break;
+
+    case 'user_admin_settings':
+
+      // The User Settings form does not use weights, so we have to splice in our fieldset
+      // to keep it from being below the buttons or the first fieldset on the form.
+      $new_form = array();
+      $new_form['registration'] = $form['registration'];
+
+      $new_form['email_registration'] = array(
+        '#type'   => 'fieldset',
+        '#title'  => t('Email Registration settings'),
+        'email_registration_login_with_username' => array(
+          '#type' => 'checkbox',
+          '#title' => t('Allow users to login with their email address OR their username'),
+          '#default_value' => variable_get('email_registration_login_with_username', FALSE),
+        ),
+      );
+
+      $old_form = $form;
+      unset($old_form['registration']);
+
+      $form = $new_form + $old_form;
+      $form['#submit'][] = 'email_registration_user_admin_settings_submit';
+      break;
   }
 }

@@ -100,8 +110,68 @@
  */
 function email_registration_user_login_validate($form, &$form_state) {
   if (isset($form_state['values']['name'])) {
-    if ($name = db_result(db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER('%s')", $form_state['values']['name']))) {
+
+    $name = db_result(db_query("SELECT name FROM {users} WHERE LOWER(mail) = LOWER('%s')", $form_state['values']['name']));
+
+    if (!variable_get('email_registration_login_with_username', FALSE) || $name !== FALSE) {
       $form_state['values']['name'] = $name;
     }
   }
-}
\ No newline at end of file
+}
+
+/**
+ * Submit function: Save the Email Registration settings from the User Settings form.
+ */
+function email_registration_user_admin_settings_submit($form, &$form_state) {
+
+  $login_with_username = $form_state['values']['email_registration_login_with_username'];
+
+  if (!empty($login_with_username)) {
+    variable_set('email_registration_login_with_username', $login_with_username);
+  }
+}
+
+/**
+ * Generate a username.
+ */
+function email_registration_generate_name($edit, $account) {
+
+  // Other modules may implement hook_email_registration_name($edit, $account)
+  // to generate a username (return a string to be used as the username, NULL
+  // to have email_registration generate it)
+  $names = module_invoke_all('email_registration_name', $edit, $account);
+
+  // Remove any empty entries
+  $names = array_filter($names);
+
+  if (empty($names)) {
+    // Default implementation of name generation
+    $namenew = _email_registration_generate_name($edit, $account);
+  }
+  else {
+    // One would expect a single implementation of the hook, but if there
+    // are multiples out there use the last one
+    $namenew = array_pop($names);
+  }
+
+  return $namenew;
+}
+
+/**
+ * Helper function: Generate a username by the default algorithm.
+ */
+function _email_registration_generate_name($edit, $account) {
+
+  $namenew = preg_replace('/@.*$/', '', $edit['mail']);
+
+  // If username generated from email record already exists, append underscore and number eg:(chris_123)
+  if (db_result(db_query("SELECT count(*) FROM {users} WHERE uid <> %d AND LOWER(name) = LOWER('%s')", $account->uid, $namenew)) > 0) {
+
+    // Find the next number available to append to the name
+    $sql = "SELECT SUBSTRING_INDEX(name, '_', -1) FROM {users} WHERE name REGEXP '%s' ORDER BY CAST(SUBSTRING_INDEX(name, '_', -1) AS UNSIGNED) DESC LIMIT 1";
+    $nameidx = db_result(db_query($sql, '^'. $namenew .'_[0-9]+$'));
+    $namenew .= '_'. ($nameidx + 1);
+  }
+
+  return $namenew;
+}
Index: email_registration.install
===================================================================
--- email_registration.install  (revision 144)
+++ email_registration.install  (working copy)
@@ -2,10 +2,10 @@
 // $Id: email_registration.install,v 1.2.2.3 2009/07/14 00:22:45 chrisherberte Exp $

 /**
- * Implementation of hook_install().
+ * Implementation of hook_uninstall().
  */
-function email_registration_install() {
-  db_query("UPDATE {system} SET weight = 10 WHERE name = 'email_registration'");
+function email_registration_uninstall() {
+  variable_del('email_registration_login_with_username');
 }

 function email_registration_update_2() {
