[305] | 1 | package LATMOS::Accounts::Bases::Mail; |
---|
| 2 | |
---|
| 3 | use 5.010000; |
---|
| 4 | use strict; |
---|
| 5 | use warnings; |
---|
| 6 | |
---|
| 7 | use base qw(LATMOS::Accounts::Bases); |
---|
| 8 | use LATMOS::Accounts::Log; |
---|
[815] | 9 | use Fcntl qw(:flock); |
---|
[305] | 10 | |
---|
| 11 | our $VERSION = (q$Rev: 641 $ =~ /^Rev: (\d+) /)[0]; |
---|
| 12 | |
---|
[1023] | 13 | =head1 NAME |
---|
| 14 | |
---|
| 15 | LATMOS::Accounts::Bases::Mail - Base support for mail files |
---|
| 16 | |
---|
| 17 | =head1 DESCRIPTION |
---|
| 18 | |
---|
| 19 | This base is designed to generated aliases/revaliases files used by postfix or |
---|
| 20 | sendmail. This allow to centralise all information and to automated alias |
---|
| 21 | generation from user real name. |
---|
| 22 | |
---|
| 23 | =cut |
---|
| 24 | |
---|
[305] | 25 | sub new { |
---|
[1071] | 26 | my ($class, %config) = @_; |
---|
[305] | 27 | |
---|
| 28 | my $base = { |
---|
[861] | 29 | file => {}, |
---|
[305] | 30 | aliases => {}, |
---|
| 31 | revaliases => {}, |
---|
| 32 | }; |
---|
| 33 | |
---|
[861] | 34 | foreach (qw(aliases revaliases)) { |
---|
[1071] | 35 | if ($config{$_}) { |
---|
| 36 | $base->{file}{$_} = $config{$_}; |
---|
| 37 | } elsif ($config{directory}) { |
---|
| 38 | $base->{file}{$_} = $config{directory} . '/' . $_; |
---|
[861] | 39 | } else { |
---|
| 40 | $base->{file}{$_} = "/etc/$_"; |
---|
| 41 | } |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | |
---|
[305] | 45 | bless($base, $class); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | sub load { |
---|
| 49 | my ($self) = @_; |
---|
| 50 | |
---|
[792] | 51 | # If aleady loaded just say ok ! |
---|
| 52 | $self->{_loaded} and return 1; |
---|
| 53 | |
---|
[305] | 54 | # warning, comment will be strip ! |
---|
| 55 | if (open(my $handle, '<', $self->{file}{aliases})) { |
---|
[792] | 56 | $self->log(LA_DEBUG, "Reading file %s", $self->{file}{aliases}); |
---|
[815] | 57 | flock($handle, LOCK_EX); |
---|
[305] | 58 | while (my $line = <$handle>) { |
---|
| 59 | chomp($line); |
---|
[348] | 60 | $line =~ s/^\s*#.*//; |
---|
| 61 | $line or next; |
---|
[351] | 62 | my ($name, $forward) = $line =~ /^([^:]+):?\s*(.*)$/; |
---|
[348] | 63 | $name or do { |
---|
| 64 | next; |
---|
| 65 | }; |
---|
[351] | 66 | $forward or next; |
---|
[305] | 67 | $self->{aliases}{$name} = $forward; |
---|
| 68 | } |
---|
| 69 | close($handle); |
---|
| 70 | } else { |
---|
| 71 | $self->log(LA_ERR, "Cannot open file '%s` for reading", |
---|
| 72 | $self->{file}{aliases}); |
---|
| 73 | return; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | if (open(my $handle, '<', $self->{file}{revaliases})) { |
---|
[792] | 77 | $self->log(LA_DEBUG, "Reading file %s", $self->{file}{revaliases}); |
---|
[815] | 78 | flock($handle, LOCK_EX); |
---|
[305] | 79 | while (my $line = <$handle>) { |
---|
| 80 | chomp($line); |
---|
[351] | 81 | $line =~ s/^\s*#.*//; |
---|
| 82 | my ($name, $as) = $line =~ /^(\S+)(?::\s*|\s+)(\S+)$/; |
---|
| 83 | $name or next; |
---|
| 84 | $as or do { warn $line; next}; |
---|
[305] | 85 | $self->{revaliases}{$name} = $as; |
---|
| 86 | } |
---|
| 87 | close($handle); |
---|
| 88 | } else { |
---|
| 89 | $self->log(LA_ERR, "Cannot open file '%s` for reading", |
---|
| 90 | $self->{file}{revaliases}); |
---|
| 91 | return; |
---|
| 92 | } |
---|
| 93 | |
---|
[792] | 94 | $self->{_loaded} = 1; |
---|
| 95 | |
---|
[305] | 96 | return 1; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | sub _commit { |
---|
| 100 | my ($self) = @_; |
---|
| 101 | |
---|
[815] | 102 | if (open(my $handle, '>>', $self->{file}{aliases})) { |
---|
| 103 | flock($handle, LOCK_EX); |
---|
| 104 | truncate($handle, 0); |
---|
[305] | 105 | foreach (sort keys %{ $self->{aliases} }) { |
---|
| 106 | printf $handle "%s:%s\n", $_, $self->{aliases}{$_}; |
---|
| 107 | } |
---|
| 108 | close($handle); |
---|
| 109 | $self->log(LA_INFO, $self->{file}{aliases} . " saved"); |
---|
| 110 | } else { |
---|
| 111 | $self->log(LA_ERR, "Cannot open file '%s` for writing", |
---|
| 112 | $self->{file}{aliases}); |
---|
| 113 | return; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | if (open(my $handle, '>', $self->{file}{revaliases})) { |
---|
[815] | 117 | flock($handle, LOCK_EX); |
---|
| 118 | truncate($handle, 0); |
---|
[305] | 119 | foreach (sort keys %{ $self->{revaliases} }) { |
---|
[817] | 120 | printf $handle "%s %s\n", $_, $self->{revaliases}{$_}; |
---|
[305] | 121 | } |
---|
| 122 | close($handle); |
---|
| 123 | $self->log(LA_INFO, $self->{file}{revaliases} . " saved"); |
---|
| 124 | } else { |
---|
| 125 | $self->log(LA_ERR, "Cannot open file '%s` for writing", |
---|
| 126 | $self->{file}{revaliases}); |
---|
| 127 | return; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | sub list_objects { |
---|
| 133 | my ($self, $type) = @_; |
---|
| 134 | |
---|
| 135 | $type = lc($type); |
---|
| 136 | lc($type) =~ /^(aliases|revaliases)$/ or return; |
---|
| 137 | |
---|
| 138 | return sort keys %{ $self->{$type} }; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | sub create_object { |
---|
| 142 | my ($self, $otype, $id, %data) = @_; |
---|
| 143 | |
---|
| 144 | $otype = lc($otype); |
---|
| 145 | if ($self->{$otype}{$id}) { |
---|
| 146 | return; |
---|
[1822] | 147 | } elsif ($otype =~ /^aliases$/i) { |
---|
| 148 | $data{forward} ||= []; |
---|
[811] | 149 | $self->{$otype}{$id} = ref $data{forward} |
---|
| 150 | ? join(',', @{$data{forward}}) |
---|
| 151 | : $data{forward}; |
---|
[305] | 152 | return 1; |
---|
| 153 | } elsif ($data{as} && $otype =~ /^revaliases$/i) { |
---|
| 154 | $self->{$otype}{$id} = $data{as}; |
---|
| 155 | return 1; |
---|
| 156 | } else { |
---|
| 157 | return; |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
[488] | 161 | sub _delete_object { |
---|
[305] | 162 | my ($self, $otype, $id) = @_; |
---|
| 163 | |
---|
| 164 | $otype = lc($otype); |
---|
| 165 | $otype =~ /^(aliases|revaliases)$/ or return; |
---|
| 166 | if ($self->{$otype}{$id}) { |
---|
| 167 | delete($self->{$otype}{$id}); |
---|
| 168 | return 1; |
---|
| 169 | } else { |
---|
| 170 | return; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | 1; |
---|
[1023] | 175 | |
---|
| 176 | __END__ |
---|
| 177 | |
---|
| 178 | =head1 SEE ALSO |
---|
| 179 | |
---|
| 180 | =head1 AUTHOR |
---|
| 181 | |
---|
| 182 | Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt> |
---|
| 183 | |
---|
| 184 | =head1 COPYRIGHT AND LICENSE |
---|
| 185 | |
---|
| 186 | Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS |
---|
| 187 | |
---|
| 188 | This library is free software; you can redistribute it and/or modify |
---|
| 189 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 190 | at your option, any later version of Perl 5 you may have available. |
---|
| 191 | |
---|
| 192 | =cut |
---|