#!/usr/bin/perl -w
our $usage1 = <<'END';
-usage: ../fyvzl [OPTIONS DATABASE-FILE ACTION ARG
+usage: ../fyvzl [<options>] <database-file> <action> <arg>...
options
- -l<minrandlength> (for create/choose, number of letters)
-m<maxperuser>
-d<dom>
-q<qualdom>
-C (show comments in output)
-h (display help)
+options for alphanum generation
+ -l<minrandlength> (for create/choose alphanum, minimum randlength)
END
our $usage2 = <<'END';
actions
- create [REDIRECT] [#COMMENT] (default for REDIRECT is your username)
- choose [REDIRECT] [#COMMENT] (interactively see and allocate suggestions)
- update ADDR [REDIRECT] [#COMMENT]
- show ADDR
+ create [<genopts>] [<redirect>] [#<comment>] (default redirect is username)
+ choose [<genopts>] [<redirect>] [#<comment>] (interactively allocate)
+ update <addr> [<redirect>] [#<comment>]
+ show <addr>
list
list-actions
-empty string for REDIRECT means reject
+empty string for redirect means reject
END
our $usage3 = <<'END';
privileged actions
- list-user USER
- insert-exact ADDR USER REDIRECT COMMENT
- donate ADDR USER
- enable-user|disable-user USER
+ list-user <user>
+ insert-exact <addr> <user> <redirect> <comment>
+ donate <addr> <user>
+ enable-user|disable-user <user>
+default generation method is alphanum
END
+our %usage_genopts = (
+'alphanum' => <<END,
+ -l<randlength> (number of letters+digits)
+END
+);
#/
use strict;
use DBI;
use POSIX;
-our $randlength = 6;
our $maxperuser = 10000;
our $qualdom;
our $dbh;
our $showcomment;
our $genmethod = 'alphanum';
+# for alphanum
+# options
+our $minrandlength = 6;
+our $maxrandlength = 100;
+# genopts
+our $randlength;
+
sub nextarg () {
die "too few arguments\n" unless @ARGV;
my $v = shift @ARGV;
binmode R;
}
+sub genopt_alphanum {
+ local ($_) = @_;
+ if (m/^-l(\d+)$/) {
+ $randlength = 0+$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;
}
sub action_choose {
+ genopts;
my $template = rhsargs({'redirect'=>$user, 'comment'=>''});
$template->{'user'} = $user;
prepare_create();
sub action_list_actions {
print $usage2 or die $!;
+ print "genopts\n" or die $!;
+ print $usage_genopts{$genmethod} or die $!;
}
while (@ARGV) {
for (;;) {
last unless m/^-./;
if (s/^-l(\d+)$//) {
- $randlength = $1;
+ $minrandlength = $1;
} elsif (s/^-m(\d+)$//) {
$maxperuser = $1;
} elsif (s/^-d(\S+)$//) {
$showcomment = 1;
} elsif (s/^-h/-/) {
print $usage1.$usage2.$usage3 or die $!;
+ foreach my $meth (qw(alphanum)) {
+ print "genopts for $meth generation method\n" or die $!;
+ print $usage_genopts{$meth} or die $!;
+ }
exit 0;
} else {
die "unknown option \`$_'\n";
}
$usage2 .= defined $dom
- ? "ADDR may be a local part, implicitly qualified with \@$dom\n"
- : "ADDR must be a local part (only)\n";
-$usage2 .= "REDIRECT is implicitly qualified with \@$qualdom if it has no \@\n"
+ ? "addr may be a local part, implicitly qualified with \@$dom\n"
+ : "addr must be a local part (only)\n";
+$usage2 .= "redirect is implicitly qualified with \@$qualdom".
+ " if it has no \@\n"
if defined $qualdom;
$dbh = DBI->connect("dbi:SQLite:dbname=$dbfile","","",