package Fripost::Commands::add_alias; use 5.010_000; use strict; use warnings; use utf8; =head1 NAME add_alias - Add a new virtual alias =cut use FindBin qw($Bin); use lib "$Bin/lib"; use Email::Valid; use IO::Prompter; use Fripost::Prompt; use Fripost::Schema; use Fripost::Email; our $VERSION = '0.01'; sub main { my $ldap = shift; my $conf = shift; # Get information my $goto = fix_username(shift); $goto //= prompt_email("Alias goto address: ", 'is_user'); my @addr = @_; @addr || push @addr, (split /, */, prompt "Alias from address(es): "); # Show goto adress say "Goto adress: $goto"; # Show from adresses @addr = grep { if (Email::Valid->address('fake'.$_)) { # Warn if the domain is unknown. my $domain = (split /\@/, $_, 2)[1]; if ($ldap->domain->search({ domain => $domain })->count) { 1; } else { warn "WARN: Skipping unknown domain `" .$domain. "'.\n"; undef; } } else { warn "WARN: Skipping invalid address `" .$_. "'.\n"; undef; } } @addr; if (@addr == 0) { warn "No valid destination adresses. Aborting...\n"; exit 1; } say "From adress(es): " . (join ", ", @addr); confirm_or_abort(); my $msg = new_alias_info_message ($conf, $goto, \@addr); ## Insert alias for my $addr (@addr) { my ($u,$d) = split /\@/, $addr, 2; # Ensure that the alias doesn't already exist. my $rs = $ldap->alias->search({ address => $addr }); if ($rs->count and not (defined $conf->{force})) { print STDERR "Error: Alias $addr already exists. "; print STDERR "(Targetting to "; print STDERR (join ', ', map { $_->{goto} } $rs->entries); say STDERR ".)"; exit 1; } die "Error: Username $addr exists.\n" if ($ldap->user->search({ username => $addr })->count); if ($conf->{pretend}) { say STDERR "Did not create alias since we are pretending." if $conf->{verbose} or $conf->{debug}; } else { $ldap->alias->add({ address => $addr, goto => $goto, isActive => 'TRUE' }); say "New alias added from $addr to $goto."; } } if ($conf->{pretend}) { say STDERR "Did not send confirmation since we are pretending." if $conf->{verbose} or $conf->{debug}; } else { if (confirm_or_abort("Send confirmation? ")) { $msg->send(); say "Sent confirmation (". (security_status $msg) .")."; } } } =head1 AUTHOR Stefan Kangas C<< >> Guilhem Moulin C<< >> =head1 COPYRIGHT Copyright 2010,2011 Stefan Kangas. Copyright 2012 Guilhem Moulin. =head1 LICENSE This program is free software; you can redistribute it and/or modify it under the same terms as perl itself. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. =cut 1; # End of add_alias.pm __END__