chiark / gitweb /
support genopts
[evade-mail-usrlocal.git] / service
diff --git a/service b/service
index 3e1197d141b6b21dc4b768e4aaf7b05465ea3e85..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;
@@ -192,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;
@@ -203,6 +232,7 @@ sub action_create {
 }
 
 sub action_choose {
+    genopts;
     my $template = rhsargs({'redirect'=>$user, 'comment'=>''});
     $template->{'user'} = $user;
     prepare_create();
@@ -362,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+)$//) {