view make-username.pl @ 2:6adfd593a32b

removed lowercase requirement for first letter of username
author Eris Caffee <discordia@eldalin.com>
date Sat, 16 Jul 2011 22:04:38 -0500
parents 706c20861682
children
line source
1 #!/usr/bin/env perl
3 use warnings;
4 use strict;
6 my $base;
7 my $user ;
9 $base = "";
10 $user = make_username($base);
11 printf("%30s %s\n", $base, $user);
13 $base = "eightchr";
14 $user = make_username($base);
15 printf("%30s %s\n", $base, $user);
17 $base = "short";
18 $user = make_username($base);
19 printf("%30s %s\n", $base, $user);
21 $base = "this *&% is not allowed &^$%&^m";
22 $user = make_username($base);
23 printf("%30s %s\n", $base, $user);
25 $base = "123digits";
26 $user = make_username($base);
27 printf("%30s %s\n", $base, $user);
29 $base = "this-is-a-domain.com";
30 $user = make_username($base);
31 printf("%30s %s\n", $base, $user);
33 $base = '&$%#&$*&$%*&';
34 $user = make_username($base);
35 printf("%30s %s\n", $base, $user);
37 # For the next test, run this first in the shell as root (not on a production system, obviously!):
38 #
39 # [ ! -d /var/cpanel/users ] && mkdir -p /var/cpanel/users
40 # touch /var/cpanel/users/testqwer
41 # touch /var/cpanel/users/testabc
42 # i=1 ; while [ $i -lt 10 ] ; do touch /var/cpanel/users/testabc${i} ; let i=$i+1 ; done
43 # touch /var/cpanel/users/testzx
44 # i=1 ; while [ $i -lt 100 ] ; do touch /var/cpanel/users/testzx${i} ; let i=$i+1 ; done
45 # touch /var/cpanel/users/test
46 # i=1 ; while [ $i -lt 100 ] ; do touch /var/cpanel/users/test${i} ; let i=$i+1 ; done
47 # touch /var/cpanel/users/t
48 # i=1 ; while [ $i -lt 10346 ] ; do touch /var/cpanel/users/t${i} ; let i=$i+1 ; done
50 $base = "testqwer";
51 $user = make_username($base);
52 printf("%30s %s\n", $base, $user);
54 $base = "testabc";
55 $user = make_username($base);
56 printf("%30s %s\n", $base, $user);
58 $base = "testzx";
59 $user = make_username($base);
60 printf("%30s %s\n", $base, $user);
62 $base = "test";
63 $user = make_username($base);
64 printf("%30s %s\n", $base, $user);
66 $base = "t";
67 $user = make_username($base);
68 printf("%30s %s\n", $base, $user);
71 sub make_username {
72 # Arguments:
73 # The string to use as the basis of the new username.
74 # Can be any string (such as an existing username or a domain name)
75 # But at most 8 characters will be used. Invalid characters will
76 # be removed.
77 # If the first character would be a number, an 'a' is prepended to the name.
78 # If blank, the string "user" will be used.
79 #
80 # Returns:
81 # A string containing the new username or undef if no username
82 # could be generated (unlikely)
84 my ($base) = @_;
86 if ($base eq "") {
87 $base = "user";
88 }
90 # Remove non-alpha-numeric characters and make sure the first character is a letter.
91 $base =~ s/[^A-Za-z0-9]//g;
92 if ($base !~ /^[A-Za-z]/ ) {
93 $base = "a".$base;
94 }
96 # Use the specified parameter as the base of the new username.
97 # Print it into a string of exactly 8 characters.
98 # Print the number into the last portion of the new username.
99 # Delete spaces (in case the passed username candidate was short).
100 # Churn until we find an unused name.
101 my $i=1;
102 my $numstr;
103 my $newuser = sprintf("%.8s", $base);
104 while ( (-e "/var/cpanel/users/$newuser") && ($i < 10000000) ) {
105 $numstr = sprintf("%d", $i);
106 $newuser = sprintf("%-8s", $base);
107 substr($newuser, -length($numstr), length($numstr), $numstr);
108 $newuser =~ s/ //g;
109 $i++;
110 }
112 $i == 10000000 && return undef;
113 return $newuser;
114 }