chiark / gitweb /
support genopts
[evade-mail-usrlocal.git] / service
diff --git a/service b/service
index f9992a36044d67125690e3542e32665446e69b8e..9739cbce34edfdb563c6e2fc0a2899625a591803 100755 (executable)
--- a/service
+++ b/service
@@ -2,17 +2,17 @@
 our $usage1 = <<'END';
 usage: ../fyvzl [OPTIONS DATABASE-FILE ACTION ARG
 options
-  -lLENGTH   (for create)
-  -mMAXPERUSER
-  -dDOM
-  -qQUALDOM
+  -l<minrandlength>   (for create/choose, number of letters)
+  -m<maxperuser>
+  -d<dom>
+  -q<qualdom>
   -C     (show comments in output)
   -h     (display help)
 END
 our $usage2 = <<'END';
 actions
-  create [REDIRECT] [#COMMENT]    (default for REDIRECT is your username)
-  choose [REDIRECT] [#COMMENT]    (interactively see and allocate suggestions)
+  create [GENOPTS] [REDIRECT] [#COMMENT]  (default for REDIRECT is username)
+  choose [GENOPTS] [REDIRECT] [#COMMENT]  (generate and interactively allocate)
   update ADDR [REDIRECT] [#COMMENT]
   show ADDR
   list
@@ -33,7 +33,10 @@ use strict;
 use DBI;
 use POSIX;
 
-our $randlength = 6;
+our $minrandlength = 6;
+our $randlength;
+our $maxrandlength = 50;
+
 our $maxperuser = 10000;
 our $qualdom;
 our $dbh;
@@ -41,6 +44,7 @@ our $dom;
 our $user;
 our $priv;
 our $showcomment;
+our $genmethod = 'alphanum';
 
 sub nextarg () {
     die "too few arguments\n" unless @ARGV;
@@ -160,16 +164,21 @@ sub local_part_inuse ($) {
     return !!$row;
 }
 
+sub gen_local_part_alphanum {
+    my $s = chr(ord('a')+goodrand(26));
+    while (length $s < $randlength) {
+       my $v = goodrand(36);
+       $s .= chr($v < 26
+                 ? ord('a')+($v)
+                 : ord('0')+($v-26));
+    }
+    return $s;
+}
+
 sub generate_local_part () {
     my $s;
     for (;;) {
-       $s = chr(ord('a')+goodrand(26));
-       while (length $s < $randlength) {
-           my $v = goodrand(36);
-           $s .= chr($v < 26
-                     ? ord('a')+($v)
-                     : ord('0')+($v-26));
-       }
+       { no strict qw(refs); $s = &{"gen_local_part_$genmethod"}; }
 #      print STDERR "$s\n";
        last if !local_part_inuse($s);
     }
@@ -186,7 +195,33 @@ sub prepare_create () {
     binmode R;
 }
 
+sub genopt_alphanum {
+    local ($_) = @_;
+    if (m/^-l(\d+)$/) {
+       $randlength = $1;
+       die "length out of range $minrandlength..$maxrandlength\n"
+           unless ($minrandlength<=$randlength &&
+                   $randlength<=$maxrandlength);
+    } else {
+       die "unknown alphanumeric generation option\n";
+    }
+}
+
+sub gendefaults_alphanum {
+    $randlength ||= $minrandlength;
+}
+
+sub genopts {
+    while (@ARGV && $ARGV[0] =~ m/^-/) {
+       my $arg = shift @ARGV;
+       last if $arg =~ m/^--?$/;
+       { no strict qw(refs); &{"genopt_$genmethod"}($arg); }
+    }
+    { no strict qw(refs); &{"gendefaults_$genmethod"}(); }
+}
+
 sub action_create {
+    genopts;
     my $newrow = rhsargs({'redirect'=>$user, 'comment'=>''});
     prepare_create();
     $newrow->{'user'} = $user;
@@ -197,6 +232,7 @@ sub action_create {
 }
 
 sub action_choose {
+    genopts;
     my $template = rhsargs({'redirect'=>$user, 'comment'=>''});
     $template->{'user'} = $user;
     prepare_create();
@@ -356,7 +392,7 @@ while (@ARGV) {
     for (;;) {
        last unless m/^-./;
        if (s/^-l(\d+)$//) {
-           $randlength = $1;
+           $minrandlength = $1;
        } elsif (s/^-m(\d+)$//) {
            $maxperuser = $1;
        } elsif (s/^-d(\S+)$//) {