aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xfripost-adduser19
-rwxr-xr-xlib/Fripost/Prompt.pm38
2 files changed, 30 insertions, 27 deletions
diff --git a/fripost-adduser b/fripost-adduser
index 28141da..15a4447 100755
--- a/fripost-adduser
+++ b/fripost-adduser
@@ -41,26 +41,18 @@ sub read_user_info {
domain => $domain,
maildir => $maildir,
active => $active,
- password => smd5($password),
+ password => $password,
};
print Dumper $user;
- say "Using password $password";
- # Ask the user if the information is OK
- my $confirmed = prompt "Is this OK? ", -yn;
-
- if ($confirmed) {
- return $user;
- } else {
- return undef;
- }
+ return $user;
}
## Get command line options
our $conf = LoadFile('default.yml');
GetOptions(
- 'dbi_dsn' => \$conf->{dbi_dsn},
+ 'dbi_dsn' => \$conf->{dbi_dsn},
'admuser=s' => \$conf->{admuser},
'admpass=s' => \$conf->{admpass},
'pretend' => \$conf->{pretend},
@@ -75,10 +67,7 @@ say "Adding a new virtual user.";
my $user = read_user_info();
-if (!defined $user) {
- say "Aborted by user.";
- exit 1;
-}
+ask_if_ok_or_abort();
if ($conf->{pretend}) {
say "Nothing to do since we are pretending...";
diff --git a/lib/Fripost/Prompt.pm b/lib/Fripost/Prompt.pm
index ad6f1ae..a857cc1 100755
--- a/lib/Fripost/Prompt.pm
+++ b/lib/Fripost/Prompt.pm
@@ -20,10 +20,19 @@ use String::MkPasswd qw/mkpasswd/;
our @EXPORT = qw(prompt_password prompt_username);
+sub fix_username {
+ my ($nam) = @_;
+ if ($nam !~ /\@/) {
+ $nam .= '@fripost.org';
+ say "Using $nam";
+ }
+ return $nam;
+}
+
sub prompt_password {
my $prompt = shift;
$prompt //= "Enter new password (blank for random): ";
- my $password = prompt $prompt;
+ my $password = prompt $prompt, -e => '*';
if (!length $password) {
$password = mkpasswd(
-length => 10,
@@ -32,25 +41,30 @@ sub prompt_password {
);
say "Generated password: $password";
}
- return $password;
+ return smd5($password);
}
sub prompt_username {
my $prompt = shift;
$prompt //= "Enter username: ";
- my $username;
- while (not defined $username) {
- $username = prompt $prompt;
- if (!($username =~ /\@/)) {
- $username .= '@fripost.org';
- say "Using $username";
- }
- if (!Email::Valid->address($username)) {
- undef $username;
+ my $nam;
+ while (not defined $nam) {
+ $nam = prompt $prompt;
+ $nam = fix_username($nam);
+ if (!Email::Valid->address($nam)) {
+ undef $nam;
say "This is not a valid e-mail address. Try again."
}
}
- return $username;
+ return $nam;
+}
+
+sub ask_if_ok_or_abort {
+ my $confirmed = prompt "Is this OK? [no will abort]", -ynt;
+ unless ($confirmed) {
+ say "User aborted";
+ exit 1;
+ }
}
=head1 AUTHOR