view make-username.pl @ 0:b20384a94d7d

initial import
author Eris Caffee <discordia@eldalin.com>
date Sat, 16 Jul 2011 13:13:18 -0500
parents
children 706c20861682
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 # i=1 ; while [ $i -lt 10 ] ; do touch /var/cpanel/users/testabc${i} ; let i=$i+1 ; done
42 # i=1 ; while [ $i -lt 100 ] ; do touch /var/cpanel/users/testzx${i} ; let i=$i+1 ; done
43 # i=1 ; while [ $i -lt 100 ] ; do touch /var/cpanel/users/test${i} ; let i=$i+1 ; done
44 # i=1 ; while [ $i -lt 10346 ] ; do touch /var/cpanel/users/t${i} ; let i=$i+1 ; done
46 $base = "testqwer";
47 $user = make_username($base);
48 printf("%30s %s\n", $base, $user);
50 $base = "testabc";
51 $user = make_username($base);
52 printf("%30s %s\n", $base, $user);
54 $base = "testzx";
55 $user = make_username($base);
56 printf("%30s %s\n", $base, $user);
58 $base = "test";
59 $user = make_username($base);
60 printf("%30s %s\n", $base, $user);
62 $base = "t";
63 $user = make_username($base);
64 printf("%30s %s\n", $base, $user);
67 sub make_username {
68 # Arguments:
69 # The string to use as the basis of the new username.
70 # Can be any string (such as an existing username or a domain name)
71 # But at most 8 characters will be used. Invalid characters will
72 # be removed.
73 # If the first character would be a number, an 'a' is prepended to the name.
74 # If blank, the string "user" will be used.
75 #
76 # Returns:
77 # A string containing the new username or undef if no username
78 # could be generated (unlikely)
80 my ($base) = @_;
82 if ($base eq "") {
83 $base = "user";
84 }
86 # Remove non-alpha-numeric characters and make sure the first character is a letter.
87 $base =~ s/[^A-Za-z0-9]//g;
88 if ($base !~ /^[a-z]/ ) {
89 $base = "a".$base;
90 }
92 # Use the specified parameter as the base of the new username.
93 # Print it into a string of exactly 8 characters.
94 # Print the number into the last portion of the new username.
95 # Delete spaces (in case the passed username candidate was short).
96 # Churn until we find an unused name.
97 my $i=1;
98 my $numstr = sprintf("%d", $i);
99 my $newuser = sprintf("%-8.8s", $base);
100 substr($newuser, -length($numstr), length($numstr), $numstr);
101 $newuser =~ s/ //g;
102 while ( (-e "/var/cpanel/users/$newuser") && ($i < 10000000) ) {
103 $i++;
104 $numstr = sprintf("%d", $i);
105 $newuser = sprintf("%-8s", $base);
106 substr($newuser, -length($numstr), length($numstr), $numstr);
107 $newuser =~ s/ //g;
108 }
110 $i == 10000000 && return undef;
111 return $newuser;
112 }