Index: email_registration.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/email_registration/email_registration.module,v
retrieving revision 1.5.2.5
diff -u -p -r1.5.2.5 email_registration.module
--- email_registration.module	31 Mar 2009 21:32:13 -0000	1.5.2.5
+++ email_registration.module	2 Apr 2009 19:55:20 -0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: email_registration.module,v 1.5.2.5 2009/03/31 21:32:13 chrisherberte Exp $
+// $Id: email_registration.module,v 1.5.2.4 2009/02/11 23:32:07 chrisherberte Exp $
 
 /**
  * Implementation of hook_user().
@@ -9,17 +9,33 @@
 function email_registration_user($op, &$edit, &$account, $category = NULL) {
   switch ($op) {
     case 'insert':
-    $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);
-    }
-    // replace with generated username
-    db_query("UPDATE {users} SET name = '%s' WHERE uid = '%s'", $namenew, $account->uid);
-  break;
+      // 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);
+      }
+
+      // replace with generated username
+      db_query("UPDATE {users} SET name = '%s' WHERE uid = '%s'", $namenew, $account->uid);
+      $account->name = $namenew;
+      break;
   }
   return;
 }
