# HG changeset patch # User Eris Caffee # Date 1310839998 18000 # Node ID b20384a94d7d633336597d6100c520e41e860dda initial import diff -r 000000000000 -r b20384a94d7d make-username.pl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/make-username.pl Sat Jul 16 13:13:18 2011 -0500 @@ -0,0 +1,112 @@ +#!/usr/bin/env perl + +use warnings; +use strict; + +my $base; +my $user ; + +$base = ""; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + +$base = "eightchr"; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + +$base = "short"; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + +$base = "this *&% is not allowed &^$%&^m"; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + +$base = "123digits"; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + +$base = "this-is-a-domain.com"; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + +$base = '&$%#&$*&$%*&'; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + +# For the next test, run this first in the shell as root (not on a production system, obviously!): +# +# [ ! -d /var/cpanel/users ] && mkdir -p /var/cpanel/users +# touch /var/cpanel/users/testqwer +# i=1 ; while [ $i -lt 10 ] ; do touch /var/cpanel/users/testabc${i} ; let i=$i+1 ; done +# i=1 ; while [ $i -lt 100 ] ; do touch /var/cpanel/users/testzx${i} ; let i=$i+1 ; done +# i=1 ; while [ $i -lt 100 ] ; do touch /var/cpanel/users/test${i} ; let i=$i+1 ; done +# i=1 ; while [ $i -lt 10346 ] ; do touch /var/cpanel/users/t${i} ; let i=$i+1 ; done + +$base = "testqwer"; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + +$base = "testabc"; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + +$base = "testzx"; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + +$base = "test"; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + +$base = "t"; +$user = make_username($base); +printf("%30s %s\n", $base, $user); + + +sub make_username { + # Arguments: + # The string to use as the basis of the new username. + # Can be any string (such as an existing username or a domain name) + # But at most 8 characters will be used. Invalid characters will + # be removed. + # If the first character would be a number, an 'a' is prepended to the name. + # If blank, the string "user" will be used. + # + # Returns: + # A string containing the new username or undef if no username + # could be generated (unlikely) + + my ($base) = @_; + + if ($base eq "") { + $base = "user"; + } + + # Remove non-alpha-numeric characters and make sure the first character is a letter. + $base =~ s/[^A-Za-z0-9]//g; + if ($base !~ /^[a-z]/ ) { + $base = "a".$base; + } + + # Use the specified parameter as the base of the new username. + # Print it into a string of exactly 8 characters. + # Print the number into the last portion of the new username. + # Delete spaces (in case the passed username candidate was short). + # Churn until we find an unused name. + my $i=1; + my $numstr = sprintf("%d", $i); + my $newuser = sprintf("%-8.8s", $base); + substr($newuser, -length($numstr), length($numstr), $numstr); + $newuser =~ s/ //g; + while ( (-e "/var/cpanel/users/$newuser") && ($i < 10000000) ) { + $i++; + $numstr = sprintf("%d", $i); + $newuser = sprintf("%-8s", $base); + substr($newuser, -length($numstr), length($numstr), $numstr); + $newuser =~ s/ //g; + } + + $i == 10000000 && return undef; + return $newuser; +}