aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Fripost/Schema/Util.pm
blob: 0f6821cdfe47a5b3fe3d9c102418cdd885ca60a5 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package Fripost::Schema::Util;

=head1 NAME

Util.pm -

=cut

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

use Exporter 'import';
our @EXPORT_OK = qw /concat get_perms explode
                     must_attrs email_valid split_addr
                     canonical_dn ldap_explode_dn ldap_error
                     assert softdie/;
use Email::Valid;
use Net::IDN::Encode;
use Net::LDAP::Util;
use Encode;


# Let the first argument, if defined, intersperse the other arguments.
sub concat {
    my $concat = shift;

    if (defined $concat) {
        return join ($concat, @_);
    }
    else {
        return [ @_ ];
    }
}

# The reverse of 'concat': takes a single line, and split it along
# "concat", if defined. Returns an array reference in any case.
sub explode {
    my $concat = shift;

    my $out;
    if (defined $concat) {
        $out = [ split /$concat/, $_[0] ];
    }
    else {
        $out = [ @_ ];
    }
    [ grep { !/^\s*$/ } @$out ];
}


# This subroutine displays the access that the given DN has on the entry.
# Possible values are :
# - '': no rights
# - a:  can create aliases
# - l:  can create lists
# - al: can create aliases & lists
# - o:  owner
# - p:  postmaster
sub get_perms {
    my ($entry, $dn) = @_;
    my @dn = @{ldap_explode_dn ($dn)};
    shift @dn;
    my $dn2 = canonical_dn (@dn);
    my $perms = '';

    $perms .= 'a'
        if grep { $dn eq $_  or  $dn2 eq $_ }
                $entry->get_value ('fripostCanAddAlias');

    $perms .= 'l'
        if grep { $dn eq $_  or  $dn2 eq $_ }
                $entry->get_value ('fripostCanAddList');

    $perms = 'o'
        if grep { $dn eq $_ } $entry->get_value('fripostOwner');

    $perms = 'p'
        if grep { $dn eq $_ } $entry->get_value('fripostPostmaster');

    return $perms;
}


# "&must_att $h qw/a b c .../" ensures that attributes a b c... are all
# defined in the hash reference.
sub must_attrs {
    my $h = shift;
    foreach (@_) {
        die 'Missing attribute: ‘'.$_."’\n"
            unless defined $h->{$_} and
                   (ref $h->{$_} eq 'ARRAY' ? @{$h->{$_}} : $h->{$_} ne '')
    }
}


# Ensure that the first argument is a valid email. Can also be used to
# check the validity of domains using the '-prefix' option.
# '-exact' forces the input to be a bare email, ("name <email>" is not
# allowed).
sub email_valid {
    my $in = shift;
    my %options = @_;

    my $i = $in;
    $i =~ s/^[^<>]+\s<([^>]+)>/$1/;
    my $mesg = $options{'-error'} // "Invalid e-mail";
    $in = $options{'-prefix'}.$i if defined $options{'-prefix'};
    Encode::_utf8_on($in);
    Encode::_utf8_on($i);
    $in = Net::IDN::Encode::email_to_ascii($in);

    my $addr = Email::Valid::->address( -address => $in,
                                        -tldcheck => 1,
                                        -fqdn => 1 );
    my $match = defined $addr;
    $match &&= $addr eq $in if $options{'-exact'};
    unless ($match) {
      return if $options{'-nodie'};
      die $mesg." ‘".$i."’\n";
    }
    $addr =~ s/^$options{'-prefix'}// if defined $options{'-prefix'};
    return $addr;
}

sub canonical_dn {
    Net::LDAP::Util::canonical_dn(\@_, casefold => 'lower'
                                     , mbcescape => 1
                                     , reverse => 0
                                     , separator => ',');
};

sub ldap_explode_dn {
    Net::LDAP::Util::ldap_explode_dn( join (',', @_), casefold => 'lower' )
}

sub split_addr {
    my $addr = shift;
    my %options = @_;

    if (defined $options{'-encode'}) {
        my $e = $options{'-encode'};
        if ($e eq 'ascii') {
            $addr = Net::IDN::Encode::email_to_ascii($addr);
        }
        elsif ($e eq 'unicode') {
            $addr = Net::IDN::Encode::email_to_unicode($addr);
        }
        else {
            die "Unknown encoding: ". $e;
        }
    }

    split /\@/, $addr, 2;
}

sub ldap_error {
    my $mesg = shift;
    my %options = @_;

    my $error;
    if (defined $options{'-die'}) {
        if (ref $options{'-die'} eq 'HASH') {
            if (exists $options{'-die'}->{$mesg->code}) {
                $error = $options{'-die'}->{$mesg->code};
            }
            elsif (exists $options{'-die'}->{_}) {
                $error = $options{'-die'}->{_};
            }
            else {
                $error = $mesg->error;
            }
        }
        else {
            $error = $options{'-die'} if $mesg->code;
        }
    }
    else {
        $error = $mesg->error if $mesg->code;
    }

    return $mesg unless defined $error;
    return unless $error;

    if (defined $options{'-error'}) {
        ${$options{'-error'}} = $error;
    }
    else {
        die $error, "\n";
    }
}

sub assert {
    my $what = shift;
    my %options = @_;

    return $what if defined $what;
    die "Not defined.\n" unless defined $options{'-die'};

    if (defined $options{'-error'}) {
        ${$options{'-error'}} = $options{'-die'};
    }
    else {
        die $options{'-die'}, "\n";
    }
}

sub softdie {
    my $mesg = shift;
    my %options = @_;

    $options{'-die'} = $mesg;
    &assert (undef, %options);
}

=head1 AUTHOR

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

=head1 COPYRIGHT

Copyright 2012,2013 Guilhem Moulin.

=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__