aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Fripost/Commands/add_domain.pm
diff options
context:
space:
mode:
authorGuilhem Moulin <guilhem.moulin@fripost.org>2012-06-10 15:38:56 +0200
committerGuilhem Moulin <guilhem.moulin@fripost.org>2012-06-10 15:39:26 +0200
commit9508574dcb8c37ff1cb8211e2fe845b2703d9141 (patch)
tree644f3447611c04aea5356373224043d8ad560ed1 /lib/Fripost/Commands/add_domain.pm
parent05d7924b1c44e85a379b3ff5cca7b512383df769 (diff)
A more modular prompt.
Diffstat (limited to 'lib/Fripost/Commands/add_domain.pm')
-rw-r--r--lib/Fripost/Commands/add_domain.pm88
1 files changed, 34 insertions, 54 deletions
diff --git a/lib/Fripost/Commands/add_domain.pm b/lib/Fripost/Commands/add_domain.pm
index 4d53e35..fc78396 100644
--- a/lib/Fripost/Commands/add_domain.pm
+++ b/lib/Fripost/Commands/add_domain.pm
@@ -18,7 +18,6 @@ use Fripost::Password;
use Fripost::Prompt;
use Fripost::Schema;
use Email::Valid;
-use IO::Prompter;
our $VERSION = '0.01';
@@ -28,53 +27,36 @@ sub main {
# Define the domain that is to be added.
my %domain;
- $domain{domain} = $_[0];
- $domain{domain} //= prompt "Domain name: ";
+ $domain{domain} = shift;
+ $domain{owner} = shift;
$domain{isActive} = 'TRUE';
- # Ensure that the domain is valid.
- die "Error: Invalid domain `$domain{domain}'.\n"
- unless Email::Valid->address('fake@'.$domain{domain});
-
- $domain{owner} = $_[1];
- $domain{owner} //= prompt_email("Belongs to user: ", 'is_user');
-
- if ($domain{owner} eq '') {
- $domain{owner} = undef
- }
- else {
- die "Error: $domain{owner} is not a valid e-mail.\n"
- unless Email::Valid->address($domain{owner});
- }
-
- # Check that the owner exists.
- die "Error: Unknown user `" .$domain{owner}. "'.\n"
- unless (not defined $domain{owner})
- or $ldap->user->search({ username => $domain{owner} })->count;
-
- # Check that the owner doesn't already own this very domain, or that the
- # domain isn't an existing "global" domain.
- if ($ldap->domain->search(\%domain)->count) {
- print STDERR "Error: Domain `" .$domain{domain}. "' already exists";
- print STDERR " for user `" .$domain{owner}. "'" if defined $domain{owner};
- say STDERR ".";
- exit 1;
- }
-
- # If the domain exists (but is eg, owned by someone else), produce a
- # warning.
- my $res = $ldap->domain->search({ domain => $domain{domain} });
- if ($res->count) {
- print STDERR "WARN: Domain `" .$domain{domain}. "' already exists.";
- my @owners;
- map { push @owners, @{$_->{owner}} if defined $_->{owner} } $res->entries;
- if (@owners) {
- print STDERR " (Owned by ";
- print STDERR (join ', ', map { '`' .$_. "'"} @owners);
- print STDERR ".)";
- }
- print STDERR "\n";
- }
+ prompt_if_undefined ( "New domain name: ", \$domain{domain},
+ [ 'Invalid domain' => sub { Email::Valid->address('fake@'.$_) }
+ , 'Domain exists' => sub { defined $conf->{force} or
+ $ldap->domain->search({
+ domain => $_
+ })->count == 0 }
+ ]
+ );
+
+ prompt_if_undefined ( "Belongs to user: ", \$domain{owner},
+ [ rewrite => sub { fix_username $_ }
+ , 'Invalid e-mail' => sub { Email::Valid->address($_) }
+ , 'Unknown domain' => sub { $ldap->domain->search({
+ domain => (split /\@/, $_, 2)[1]
+ })->count }
+ , 'Unknown username' => sub { $ldap->user->search({
+ username => $_
+ })->count }
+ , "Already owns `$domain{domain}'" =>
+ sub { $ldap->domain->search({
+ domain => $domain{domain}, owner => $_
+ })->count == 0 }
+ ]
+ )
+ unless defined $domain{owner} and $domain{owner} eq '';
+ undef $domain{owner} if $domain{owner} eq '';
if ($conf->{pretend}) {
say STDERR "Did not add the domain since we are pretending."
@@ -84,9 +66,8 @@ sub main {
# Add the domain.
$ldap->domain->add(\%domain);
if (defined $domain{owner}) {
- print "New domain `" .$domain{domain}. "' added";
- print " for user `" .$domain{owner}. "'" if defined $domain{owner};
- say ".";
+ say "New domain `" .$domain{domain}. "' added for user `"
+ .$domain{owner}. "'.";
}
else {
say "New non self-managed domain `" .$domain{domain}. "' added.";
@@ -108,11 +89,10 @@ sub create_alias {
my $res = $ldap->alias->search(\%alias);
if ($res->count) {
- print STDERR "WARN: Alias $alias{address} already exists.";
- print STDERR "(Targetting to ";
- print STDERR (join ', ', map { $_->{goto} } $res->entries);
- say STDERR ".)";
- return unless grep { $_->{goto} eq $alias{goto} } $res->entries;
+ say STDERR "WARN: Alias $alias{address} already exists."
+ ." (Targetting to " . (join ', ', map { $_->{goto} } $res->entries)
+ .".)";
+ return if grep { $_->{goto} eq $alias{goto} } $res->entries;
}
$alias{isActive} = 'TRUE';