#!/usr/bin/perl -w
our $usage1 = <<'END';
-usage: ../fyvzl [OPTIONS DATABASE-FILE ACTION ARG
+usage: ../fyvzl [<options>] <database-file> <action> <arg>...
options
- -lLENGTH (for create)
- -mMAXPERUSER
- -dDOM
- -qQUALDOM
+ -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 $user;
our $priv;
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;
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);
}
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();
print "\n" or die $!;
$s{$s} = 1;
}
- print "# enter addresses or local parts to create,".
+ print "# ready - enter addrs or local-parts to create,".
" then \`.' on a line by itself\n"
or die $!;
prow($newrow);
} else {
$dbh->rollback();
- print "!error: $@" or die $!;
+ print "! error: $@" or die $!;
}
}
}
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","","",