aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Fripost/Commands/domain_add.pm
blob: a727623fc5863aa672c8a6a8e359a35965182e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package Fripost::Commands::domain_add;

use 5.010_000;
use warnings;
use strict;
use utf8;

=head1 NAME

domain_add - Add a new virtual domain.

=cut

use FindBin qw($Bin);
use lib "$Bin/lib";

use Fripost::Password;
use Fripost::Prompt;
use Fripost::Schema;
use Email::Valid;
use IO::Prompter;

our $VERSION = '0.01';

sub main {
    my $ldap = shift;
    my $conf = shift;

    # Define the domain that is to be added.
    my %domain;
    $domain{domain}   = $_[0];
    $domain{domain} //= prompt "Domain name: ";
    $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";
    }

    if ($conf->{pretend}) {
        say STDERR "Did not add the domain since we are pretending."
            if $conf->{verbose} or $conf->{debug};
    }
    else {
        # 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 ".";
        }
        else {
            say "New non self-managed domain `" .$domain{domain}. "' added.";
        }

        create_alias($ldap, 'abuse@' . $domain{domain},
                            'abuse@fripost.org');
        create_alias($ldap, 'postmaster@' . $domain{domain},
                            'postmaster@fripost.org'); 
    }
}


# Create aliases.
sub create_alias {
    my ($ldap, $from, $to) = @_;

    my %alias = (address => $from, goto => $to);

    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;
    }

    $alias{isActive} = 'TRUE';
    $ldap->alias->add( \%alias );
    say "Created alias from $from to $to.";
}



=head1 AUTHOR

Stefan Kangas C<< <skangas at skangas.se> >>

Guilhem Moulin C<< <guilhem at fripost.org> >>

=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 domain_add.pm

__END__