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

use strict;
use warnings;

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

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

=head1 NAME

LATMOS::Accounts::Bases::Mail::Aliases - Aliases support for mail base.

=cut

sub new {
    my ($class, $base, $id, @args) = @_;
    if (exists($base->{aliases}{$id})) {
        return bless({}, $class);
    } else { return }
}

sub _get_attr_schema {
    my ($class, $base) = @_;
    {
        forward => {
            multiple => 1,
        },
    }
}

sub get_field {
    my ($self, $field) = @_;
    $field eq 'forward' and do {
        my @forw = split(',', $self->base->{aliases}{$self->id});
        return scalar(@forw) > 1
            ? [ @forw ]
            : $self->base->{aliases}{$self->id};
    };
    return
}

sub set_fields {
    my ($self, %data) = @_;
    if ($data{forward}) {
        $self->base->{aliases}{$self->id} = ref $data{forward}
            ? join(',', @{$data{forward}})
            : $data{forward};
        return 1;
    }
    return;
}

1;

__END__

=head1 SEE ALSO

L<LATMOS::Accounts::Bases::Mail>

=head1 AUTHOR

Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008, 2009, 2010, 2011, 2012 CNRS SA/CETP/LATMOS

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.

=cut

