package LATMOS::Accounts::Bases::Mail;

use 5.010000;
use strict;
use warnings;

use base qw(LATMOS::Accounts::Bases);
use LATMOS::Accounts::Log;

our $VERSION = (q$Rev: 641 $ =~ /^Rev: (\d+) /)[0];

sub new {
    my ($class, %options) = @_;

    my $base = {
        file => {
            aliases => $options{aliases} || '/etc/aliases',
            revaliases => $options{revaliases} || '/etc/revaliases',
        },
        aliases => {},
        revaliases => {},
    };

    bless($base, $class);
}

sub load {
    my ($self) = @_;

    # warning, comment will be strip !
    if (open(my $handle, '<', $self->{file}{aliases})) {
        while (my $line = <$handle>) {
            chomp($line);
            $line =~ s/^\s*#.*//;
            $line or next;
            my ($name, $forward) = $line =~ /^([^:]+):?\s*(.*)$/;
            $name or do {
                next;
            };
            $forward or next;
            $self->{aliases}{$name} = $forward;
        }
        close($handle);
    } else {
        $self->log(LA_ERR, "Cannot open file '%s` for reading",
            $self->{file}{aliases});
        return;
    }

    if (open(my $handle, '<', $self->{file}{revaliases})) { 
        while (my $line = <$handle>) {
            chomp($line);
            $line =~ s/^\s*#.*//;
            my ($name, $as) = $line =~ /^(\S+)(?::\s*|\s+)(\S+)$/;
            $name or next;
            $as or do { warn $line; next};
            $self->{revaliases}{$name} = $as;
        }
        close($handle);
    } else {
        $self->log(LA_ERR, "Cannot open file '%s` for reading",
            $self->{file}{revaliases});
        return;
    }

    return 1;
}

sub _commit {
    my ($self) = @_;

    if (open(my $handle, '>', $self->{file}{aliases})) {
        foreach (sort keys %{ $self->{aliases} }) {
            printf $handle "%s:%s\n", $_, $self->{aliases}{$_};
        }
        close($handle);
        $self->log(LA_INFO, $self->{file}{aliases} . " saved");
    } else {
        $self->log(LA_ERR, "Cannot open file '%s` for writing",
            $self->{file}{aliases});
        return;
    }

    if (open(my $handle, '>', $self->{file}{revaliases})) {
        foreach (sort keys %{ $self->{revaliases} }) {
            printf $handle "%s:%s\n", $_, $self->{revaliases}{$_};
        }
        close($handle);
        $self->log(LA_INFO, $self->{file}{revaliases} . " saved");
    } else {
        $self->log(LA_ERR, "Cannot open file '%s` for writing",
            $self->{file}{revaliases});
        return;
    }

}

sub list_objects {
    my ($self, $type) = @_;

    $type = lc($type);
    lc($type) =~ /^(aliases|revaliases)$/ or return;

    return sort keys %{ $self->{$type} };
}

sub create_object {
    my ($self, $otype, $id, %data) = @_;

    $otype = lc($otype);
    if ($self->{$otype}{$id}) {
        return;
    } elsif ($data{forward} && $otype =~ /^aliases$/i) {
        $self->{$otype}{$id} = $data{forward};
        return 1;
    } elsif ($data{as} && $otype =~ /^revaliases$/i) {
        $self->{$otype}{$id} = $data{as};
        return 1;
    } else {
        return;
    }
}

sub delete_object {
    my ($self, $otype, $id) = @_;
    
    $otype = lc($otype);
    $otype =~ /^(aliases|revaliases)$/ or return;
    if ($self->{$otype}{$id}) {
        delete($self->{$otype}{$id});
        return 1;
    } else {
        return;
    }
}

1;
