diff options
-rwxr-xr-x | fripost-adduser | 33 | ||||
-rwxr-xr-x | lib/Fripost/Prompt.pm | 71 |
2 files changed, 84 insertions, 20 deletions
diff --git a/fripost-adduser b/fripost-adduser index d7a0fd3..1dab56e 100755 --- a/fripost-adduser +++ b/fripost-adduser @@ -27,29 +27,18 @@ use lib "$Bin/lib"; use Data::Dumper; use DateTime; -use Email::Valid; use Fripost::Password; +use Fripost::Prompt; use Fripost::Schema; use IO::Prompt; use Getopt::Long; -use String::MkPasswd qw/mkpasswd/; use YAML::Syck; # Prompt for user info sub read_user_info { my %user; - # Get the full e-mail of the user (aka e-mail) - while (not defined $user{username}) { - $user{username} = prompt "New username: "; - if (!($user{username} =~ /\@/)) { - $user{username} .= '@fripost.org'; - say "Using $user{username}"; - } - if (!Email::Valid->address($user{username})) { - undef $user{username}; - say "This is not a valid e-mail address. Try again." - } - } + + $user{username} = prompt_username("New username: "); # Full name of user $user{name} = prompt "Full (real) name: "; @@ -76,16 +65,12 @@ sub read_user_info { $user{active} = 1; # Generate password - my $password = mkpasswd( - -length => 20, - -minnum => 5, - -minspecial => 3 - ); + my $password = prompt_password(); $user{password} = smd5($password); # Show the information that will be inserted say Dumper \%user; - say "Generated password: $password"; + say "Using password $password"; # Ask the user if the information is OK my $confirmed = prompt "Is this OK? ", -yn; @@ -104,6 +89,7 @@ GetOptions( 'dbi_dsn' => \$conf->{dbi_dsn}, 'admuser=s' => \$conf->{admuser}, 'admpass=s' => \$conf->{admpass}, + 'pretend' => \$conf->{pretend}, ) or die "Unable to get command line options."; # Connect to the database @@ -120,6 +106,11 @@ if (!defined $user) { exit 1; } +if ($conf->{pretend}) { + say "Nothing to do since we are pretending..."; + exit 0; +} + ## Create maildir my ($login,$pass,$uid,$gid) = getpwnam($conf->{maildir_user}) or die "maildir_user not found: $conf->{maildir_user}"; @@ -133,6 +124,8 @@ system(qw/sudo chown -R/, "$conf->{maildir_user}:$conf->{maildir_group}", $conf- say "Created maildir in $maildir_loc"; +## TODO: Make sure the user does not already exist + ## Insert user into database my $db_user = $schema->resultset('Mailbox')->new($user); $db_user->insert; diff --git a/lib/Fripost/Prompt.pm b/lib/Fripost/Prompt.pm new file mode 100755 index 0000000..ad6f1ae --- /dev/null +++ b/lib/Fripost/Prompt.pm @@ -0,0 +1,71 @@ +#!/usr/bin/perl + +use 5.010_000; +use warnings; +use strict; + +=head1 NAME + +Prompt.pm - Lots of prompt helper functions + +=cut + +our $VERSION = '0.01'; + +use Data::Dumper; +use Email::Valid; +use Exporter; +use IO::Prompt; +use String::MkPasswd qw/mkpasswd/; + +our @EXPORT = qw(prompt_password prompt_username); + +sub prompt_password { + my $prompt = shift; + $prompt //= "Enter new password (blank for random): "; + my $password = prompt $prompt; + if (!length $password) { + $password = mkpasswd( + -length => 10, + -minnum => 2, + -minspecial => 2, + ); + say "Generated password: $password"; + } + return $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; + say "This is not a valid e-mail address. Try again." + } + } + return $username; +} + +=head1 AUTHOR + +Stefan Kangas C<< <skangas at skangas.se> >> + +=head1 COPYRIGHT + +Copyright 2010 Stefan Kangas. + +=head1 LICENSE + +This program is free software; you can redistribute it and/or modify it +under the same terms as perl itself. + +=cut + +1; # End of Prompt.pm |