# $Header: /home/rt2/etc/site_scrips/Condition/RCS/IncomingEmail.pm,v 1.1 2002/01/30 15:53:15 rt2 Exp $

# RT is Copyright 1996-2002 Jesse Vincent <jesse@fsck.com> 
# Released under the terms of the GNU General Public License

# Decide whether a Transaction was generated via Email or not.
# Uses the Argument to match patterns in the first Attachment's header.
# Make sure this is set only to patterns added by your MTA.  'To' cannot
# be relied upon.

package RT::Condition::IncomingEmail;
require RT::Condition::Generic;

@ISA = qw(RT::Condition::Generic);


=head2 IsApplicable

If the Transaction's first Attachment's Header contains any of the patterns.

=cut

sub IsApplicable {
    my $self = shift;

    my $retval = undef;

    my $loop = 0;
    if( $self->Argument ){
        my $Header = $self->TransactionObj->Message->First->Headers;
        foreach my $pt ( split( ',', $self->Argument ) ){
            # Don't bother matching on spaces, use \s in the argument.
            chomp $pt;
            $pt =~ s/^\s*//g;
            $pt =~ s/\s*$//g;
            $loop++;
            # If we match this in the header.
            if( $Header =~ /$pt/m ){
                $retval = 0 if( ! defined( $retval ) );
                $retval++;
            }
        }
        if( defined( $retval ) ){
            # We can only return if we matched them all.
            if( $retval != $loop ){
                $retval = undef;
            }
        }
    }
                
    return( $retval );
}

1;

