[rt-devel] Contrib: ScripCondition IncomingEmail

Bruce Campbell bruce_campbell at ripe.net
Wed Jan 30 11:37:35 EST 2002


This is a ScripCondition to fire if the transaction originated via Email.
It is part of work I'm doing to avoid contributing to possible mail loops.

It uses the Headers of the first Attachment of the Transaction, and looks
for patterns to check in the Argument (see the insert_condition).  These
should be set to the Headers that your MTA will _always_ ensure are in the
message.  Do not use the 'To' field.

The Condition will not fire if the Transaction originated via the Web
interface.

-- 
                             Bruce Campbell                            RIPE
                   Systems/Network Engineer                             NCC
                 www.ripe.net - PGP562C8B1B                      Operations
-------------- next part --------------
# $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 at 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;

-------------- next part --------------
#!/usr/bin/perl -w
#
# $Header: /raid/cvsroot/rt-addons/ScripConditions/OnOwnerChange/insert_condition.pl,v 1.1 2001/06/17 19:18:36 jesse Exp $
# RT is (c) 1996-2000 Jesse Vincent (jesse at fsck.com);
# IncomingEmail Condition by Bruce Campbell

package RT;
use strict;
use vars qw($VERSION $Handle $Nobody $SystemUser $item);

use lib "/home/rt2/lib";
use lib "/home/rt2/etc";

#This drags in  RT's config.pm
use config;
use Carp;

use RT::Handle;
use RT::User;
use RT::CurrentUser;

#connect to the db
$RT::Handle = new RT::Handle($RT::DatabaseType);
$RT::Handle->Connect();


#Put together a current user object so we can create a User object
my $CurrentUser = new RT::CurrentUser();


#now that we bootstrapped that little bit, we can use the standard RT cli
# helpers  to do what we need

use RT::Interface::CLI  qw(CleanEnv LoadConfig DBConnect 
			   GetCurrentUser GetMessageContent);

#Clean out all the nasties from the environment
CleanEnv();

#Load etc/config.pm and drop privs
LoadConfig();

#Connect to the database and get RT::SystemUser and RT::Nobody loaded
DBConnect();


$CurrentUser->LoadByName('RT_System');

# {{{ ScripConditions

# The Argument is a comma-seperate list of patterns to check for in the
# Transaction's first Attachment's Header to decide whether its an Email 
# or not.  Only choose Header lines that you *know* will be added by your
# MTA.
my @ScripConditions = (
		       {
			Name => 'OnIncomingEmail',
			Description =>  'Matches when the transaction was generated via Email',
			ApplicableTransTypes =>  'Any',
			Argument => '^Received, ^Date, ^From',  
			ExecModule => 'IncomingEmail',
		       },
		       
		       
		      );

# }}}
print "Creating ScripConditions...";

use RT::ScripCondition;
for $item (@ScripConditions) {
    my $new_entry = new RT::ScripCondition($CurrentUser);
    my $return = $new_entry->Create(%$item);
    print $return.".";
}

print "done.\n";

$RT::Handle->Disconnect();


1;



More information about the Rt-devel mailing list