[rt-users] ExtractCustomFieldValues with attachments.

Jean-Sebastien Morisset jsmoriss at mvlan.net
Tue Jul 29 13:30:59 EDT 2008


The problem is with the FindMatch function. It only checks the first
attachment. I overhauled this perl module quite a bit. Here's my version
attached. Diff this one to yours to see the differences. :-)

js.

On Tue, Jul 29, 2008 at 01:01:49PM -0400, Roy Sowa wrote:
> I am currently using RT 3.6.5 and ECFV 1.2.b3
> 
> I cannot get the extract to work if the MatchString is not in the first attachment.
> I have been using ECFV for a while on basic standard type email tickets, but now I have the requirement
> to extract the MatchString from an attachment. 
> 
> It does not seem to matter if the ticket is created via GUI or email.
> The simplest test being;
>    1. Create ticket using GUI with a description that has the MatchString .... ( this works  CF get populated)
>    2. Create ticket using GUI without any description and attach text file that contains MatchString .... ( this works  Cf gets populated)
>    3. Create ticket using GUI with general desc and attach text file with MatchString ..... ( this fails  No CF populated ) 
> 
>  Questions:
>      Does ExtractCustomFieldValues work with multi part messages if the matchstring is in the second attachment?
>     
>     Where did I go wrong ?
> 
>    I have read the list , and  ContentObj is what is being used.  
>   Any hints or guidance would be much appreciated. 
> 
>  Thanks in advance.
> 
>  Roy
> 
> Community help: http://wiki.bestpractical.com
> Commercial support: sales at bestpractical.com
> 
> 
> Discover RT's hidden secrets with RT Essentials from O'Reilly Media. 
> Buy a copy at http://rtbook.bestpractical.com
> 

-- 
Jean-Sebastien Morisset, Sr. UNIX Administrator <jsmoriss at mvlan.net>
-------------- next part --------------
package RT::Action::ExtractCustomFieldValues;
require RT::Action::Generic;

use strict;
use vars qw/@ISA/;
@ISA=qw(RT::Action::Generic);

our $VERSION = 1.3;

sub Describe  {
  my $self = shift;
  return (ref $self );
}

sub Prepare {
  return (1);
}

sub Commit {
    my $self = shift;
    my $Transaction = $self->TransactionObj;
    my $FirstAttachment = $Transaction->Attachments->First;
    unless ( $FirstAttachment ) { return 1; }

    my $Ticket = $self->TicketObj;
    my $TmplText = $self->TemplateObj->Content;
    my $Queue = $Ticket->QueueObj->Id;
    my $Separator = '\|';

    for (split(/[\n\r]+/, $TmplText)) {
        chomp;
        next if (/^#/);
        next if (/^\s*$/);
        if (/^Separator=(.*)$/) {
            $Separator=$1;
            next;
        }
        my ($CustomFieldName,$InspectField,$MatchString,$PostEdit,$Options) = split(/$Separator/);

        my $cf;
        if ($CustomFieldName) {
            $cf = LoadCF( Field => $CustomFieldName, Queue => $Queue );
        }

        my $match = FindMatch( Field => $InspectField, 
		Match => $MatchString,
		Transaction => $Transaction,
		FirstAttachment => $FirstAttachment );

        if ($cf) {
    		$RT::Logger->debug("running ProcessCF...");
		ProcessCF ( PostEdit => $PostEdit,
			Ticket      => $Ticket,
			Options     => $Options,
			CustomField => $cf,
			Match       => $match );
        } else {
    		$RT::Logger->debug("running ProcessMatch...");
		ProcessMatch( PostEdit => $PostEdit, 
			Ticket => $Ticket,
			Options => $Options,
			Transaction => $Transaction,
			FirstAttachment  => $FirstAttachment,
			Match => $match );
        }
    }
    return(1);
}

sub LoadCF {
    my %args = @_;
    my $CustomFieldName = $args{Field};
    my $Queue = $args{Queue};

    $RT::Logger->debug("load cf $CustomFieldName");
    my $cf = new RT::CustomField($RT::SystemUser);
    my ($id,$msg) = $cf->LoadByNameAndQueue (Name=>"$CustomFieldName", Queue=>$Queue);
    if (! $id) {
      ($id,$msg) = $cf->LoadByNameAndQueue (Name=>"$CustomFieldName", Queue=>0);
    }
    $RT::Logger->debug("load cf done: $id $msg");

    return $cf;

}

sub FindMatch {
    my %args = @_;
    my $match = '';
    if ($args{Field} =~ /^body$/i) {
        $RT::Logger->info("look for match /".$args{Match}."/ in body");

	my $Attachments = $args{Transaction}->Attachments;
	my $LastContent = '';
	my $AttachNumber = 0;

        while (my $Message = $Attachments->Next) {
                $AttachNumber++;
                $RT::Logger->info("considering attachment #".$AttachNumber." of type ".$Message->ContentType);
                next unless $Message->ContentType =~ m!^(text/plain|message|text$)!i;
                next unless $Message->Content;
                next if $LastContent eq $Message->Content;
                $RT::Logger->info("accepted attachment #".$AttachNumber." for consideration");
                $LastContent = $Message->Content;
		if ($Message->Content =~ /$args{Match}/m) {
			$match = $1||$&;
			$RT::Logger->info("matched value");
		} else {
			$RT::Logger->debug("no match");
		}
	}
    } else {
	$RT::Logger->info("look for match in Header $args{Field} (".$args{FirstAttachment}->GetHeader("$args{Field}").")");
	if ($args{FirstAttachment}->GetHeader("$args{Field}") =~ /$args{Match}/) {
		$match = $1||$&;
		$RT::Logger->info("matched value");
        }
    }
    return $match;
}

sub ProcessCF {
	my %args = @_;
	my @values = ();
	if ($args{CustomField}->SingleValue()) {
		push @values, $args{Match};
	} else {
		@values = split(',', $args{Match});
	}

    foreach my $value (@values) {
        if ($value && $args{PostEdit}) {
            local $_ = $value; # backwards compatibility
            eval($args{PostEdit});
            $RT::Logger->info("transformed ($args{PostEdit}) value ($value)");
        }
        if ($value) {
            $RT::Logger->info("found value for cf ($value)");
            my ($id,$msg) = $args{Ticket}->AddCustomFieldValue( Field => $args{CustomField}, 
		Value => $value , RecordTransaction => $args{Options} =~ /q/ ? 0 : 1);
            $RT::Logger->info("CustomFieldValue (".$args{CustomField}->Name.",$value) added: $id $msg");
        }
    }
}

sub ProcessMatch {
    my %args = @_;
    my $Ticket = $args{Ticket};
    my $Transaction = $args{Transaction};
    my $FirstAttachment = $args{FirstAttachment};
    if ($args{Match} && $args{PostEdit}) {
        local $_ = $args{Match}; # backwards compatibility
        eval($args{PostEdit});
        $RT::Logger->debug("ran code $args{PostEdit} $@");
    }
}

1;


More information about the rt-users mailing list