aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xfripost-adduser21
-rwxr-xr-xfripost-newalias33
-rwxr-xr-xlib/Fripost/Prompt.pm38
3 files changed, 59 insertions, 33 deletions
diff --git a/fripost-adduser b/fripost-adduser
index 86da15d..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...";
@@ -99,7 +88,7 @@ my $msg = MIME::Lite->new(
From => 'admin@fripost.org',
To => $user->{username},
Subject => 'Välkommen till Fripost!',
- Data => slurp('templ/new_user_mail.tt'), # TODO: actually use TT
+ Data => scalar slurp('templ/new_user_mail.tt'), # TODO: actually use TT
);
$msg->send();
diff --git a/fripost-newalias b/fripost-newalias
index 2668943..162f787 100755
--- a/fripost-newalias
+++ b/fripost-newalias
@@ -8,14 +8,18 @@ use strict;
fripost-newalias - Add a new alias to the system
-=cut
+=head1 SYNOPSIS
+
+fripost-newalias
+fripost-newalias GOTO FROM...
-our $VERSION = '0.01';
+=cut
use FindBin qw($Bin);
use lib "$Bin/lib";
use Data::Dumper;
+use Email::Valid;
use Fripost::Password;
use Fripost::Prompt;
use Fripost::Schema;
@@ -38,11 +42,10 @@ my $schema = Fripost::Schema->connect(
$conf->{dbi_dsn}, $conf->{admuser}, $conf->{admpass}, {} #\%dbi_params
);
-my $goto = shift @ARGV;
+# Get information
+my $goto = fix_username(shift @ARGV);
my @addr = @ARGV;
-
$goto //= prompt_username("Alias goto address: ");
-
@addr || push @addr, prompt "Alias address: ";
if ($conf->{pretend}) {
@@ -50,6 +53,26 @@ if ($conf->{pretend}) {
exit 0;
}
+# Show goto adress
+say "goto adress: $goto";
+
+# Show from adresses
+@addr = grep {
+ if (Email::Valid->address($_)) {
+ 1;
+ }
+ else {
+ say "WARN: skipping invalid address $_";
+ undef;
+ }
+} @addr;
+if (@addr == 0) {
+ say "No valid destination adresses. Aborting...";
+ exit 1;
+}
+say "dest adress: " . (join " ", @addr);
+ask_if_ok_or_abort();
+
## Insert alias into database
for my $addr (@addr) {
my $rs = $schema->resultset('Alias')->search({
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