# --
# Kernel/System/PostMaster/Filter/NotCustomer.pm - sub part of PostMaster.pm
# Copyright (C) 2009 RDTEX LLC, http://rdtex.com.ua/
# --
# $Id: CMD.pm,v 0.01 10.12.2009$
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::System::PostMaster::Filter::NotCustomer;

use strict;
use warnings;

use vars qw($VERSION);
$VERSION = qw($Revision: 0.01 $) [1];

sub new {
    my ( $Type, %Param ) = @_;

    # allocate new hash for object
    my $Self = {};
    bless( $Self, $Type );

    $Self->{Debug} = $Param{Debug} || 0;

    # get all objects
    for (qw(DBObject ConfigObject LogObject ParserObject)) {
        $Self->{$_} = $Param{$_} || die 'Got no $_';
    }

    $Self->{CustomerUserObject} = Kernel::System::CustomerUser->new(%Param);

    return $Self;
}

sub Run {
    my ( $Self, %Param ) = @_;

    # get config options
    my %Config = ();
    my %Set    = ();
    my $StopAfterMatch;
    if ( $Param{JobConfig} && ref( $Param{JobConfig} ) eq 'HASH' ) {
        %Config = %{ $Param{JobConfig} };
        if ( $Config{Set} ) {
            %Set = %{ $Config{Set} };
        }
        $StopAfterMatch = $Config{StopAfterMatch} || 0;
    }

    # check needed stuff
    for (qw(GetParam)) {
        if ( !$Param{$_} ) {
            $Self->{LogObject}->Log( Priority => 'error', Message => "Need $_!" );
            return;
        }
    }
    my %GetParam = %{ $Param{GetParam} };

    my %CustomerData = ();
    if ( $GetParam{From} ) {
        my @EmailAddresses = $Self->{ParserObject}->SplitAddressLine(
            Line => $GetParam{From},
        );
        for (@EmailAddresses) {
            $GetParam{EmailForm} = $Self->{ParserObject}->GetEmailAddress(
                Email => $_,
            );
        }
        my %List = $Self->{CustomerUserObject}->CustomerSearch(
            PostMasterSearch => lc( $GetParam{EmailForm} ),
        );
        for ( keys %List ) {
            %CustomerData = $Self->{CustomerUserObject}->CustomerUserDataGet(
                User => $_,
            );
        }
    }

    if ( !$CustomerData{UserLogin} ) {
        $Self->{LogObject}->Log(
            Priority => 'notice',
            Message  => "UserLogin not found into "
                . "customer source backend based on ($GetParam{'EmailForm'}).",
        );
        for ( keys %Set ) {
            $Param{GetParam}->{$_} = $Set{$_};
            $Self->{LogObject}->Log(
                Priority => 'notice',
                Message  => "Set param '$_' to '$Set{$_}' (Message-ID: $Param{GetParam}->{'Message-ID'}) ",
            );
        }

        # stop after match
        if ($StopAfterMatch) {
            $Self->{LogObject}->Log(
                Priority => 'notice',
                Message  => "Stopped filter procesing because of used 'StopAfterMatch' (Message-ID: $Param{GetParam}->{'Message-ID'}) ",
            );
            return 1;
        }
    }
    return 1;
}

1;
