1 package RT::Action::ExtractCustomFieldValues;
7 use base qw(RT::Action);
9 our $VERSION = 2.99_01;
22 return $self->TransactionObj->Attachments->First;
27 return $self->TicketObj->QueueObj->Id;
32 return $self->TemplateObj->Content;
38 my ($content, $error) = $self->TemplateContent;
39 if (!defined($content)) {
40 return (undef, $error);
44 my @lines = split( /[\n\r]+/, $content);
50 if (/^Separator=(.+)$/) {
55 @line{qw/CFName Field Match PostEdit Options/}
56 = split(/$Separator/);
57 $_ = '' for grep !defined, values %line;
58 push @results, \%line;
65 return 1 unless $self->FirstAttachment;
67 my ($config_lines, $error) = $self->TemplateConfig;
71 for my $config (@$config_lines) {
72 my %config = %{$config};
73 $RT::Logger->debug( "Looking to extract: "
74 . join( " ", map {"$_=$config{$_}"} sort keys %config ) );
76 if ( $config{Options} =~ /\*/ ) {
82 while ( $content =~ /$config{Match}/mg ) {
83 my ( $cf, $value ) = ( $1, $2 );
84 $cf = $self->LoadCF( Name => $cf, Quiet => 1 );
98 $cf = $self->LoadCF( Name => $config{CFName} )
105 return 0 unless $content =~ /($config{Match})/m;
122 my $CustomFieldName = $args{Name};
123 $RT::Logger->debug( "Looking for CF $CustomFieldName");
125 # We do this by hand instead of using LoadByNameAndQueue because
126 # that can find disabled queues
127 my $cfs = RT::CustomFields->new($RT::SystemUser);
128 $cfs->LimitToGlobalOrQueue($self->Queue);
131 VALUE => $CustomFieldName,
134 $cfs->RowsPerPage(1);
136 my $cf = $cfs->First;
137 if ( $cf && $cf->id ) {
138 $RT::Logger->debug( "Found CF id " . $cf->id );
139 } elsif ( not $args{Quiet} ) {
140 $RT::Logger->error( "Couldn't load CF $CustomFieldName!");
149 if ( lc $args{Field} eq "body" ) {
150 my $Attachments = $self->TransactionObj->Attachments;
151 my $LastContent = '';
152 my $AttachmentCount = 0;
154 my @list = @{ $Attachments->ItemsArrayRef };
155 while ( my $Message = shift @list ) {
157 $RT::Logger->debug( "Looking at attachment $AttachmentCount, content-type "
158 . $Message->ContentType );
159 my $ct = $Message->ContentType;
160 unless ( $ct =~ m!^(text/plain|message|text$)!i ) {
161 # don't skip one attachment that is text/*
162 next if @list > 1 || $ct !~ m!^text/!;
165 my $content = $Message->Content;
166 next unless $content;
167 next if $LastContent eq $content;
168 $RT::Logger->debug( "Examining content of body" );
169 $LastContent = $content;
170 $args{Callback}->( $content );
172 } elsif ( lc $args{Field} eq 'headers' ) {
173 my $attachment = $self->FirstAttachment;
174 $RT::Logger->debug( "Looking at the headers of the first attachment" );
175 my $content = $attachment->Headers;
176 return unless $content;
177 $RT::Logger->debug( "Examining content of headers" );
178 $args{Callback}->( $content );
180 my $attachment = $self->FirstAttachment;
181 $RT::Logger->debug( "Looking at $args{Field} header of first attachment" );
182 my $content = $attachment->GetHeader( $args{Field} );
183 return unless defined $content;
184 $RT::Logger->debug( "Examining content of header" );
185 $args{Callback}->( $content );
193 return $self->PostEdit(%args)
194 unless $args{CustomField};
197 if ( $args{CustomField}->SingleValue() ) {
198 push @values, $args{Value};
200 @values = split( ',', $args{Value} );
203 foreach my $value ( grep defined && length, @values ) {
204 $value = $self->PostEdit(%args, Value => $value );
205 next unless defined $value && length $value;
207 $RT::Logger->debug( "Found value for CF: $value");
208 my ( $id, $msg ) = $self->TicketObj->AddCustomFieldValue(
209 Field => $args{CustomField},
211 RecordTransaction => $args{Options} =~ /q/ ? 0 : 1
213 $RT::Logger->info( "CustomFieldValue ("
214 . $args{CustomField}->Name
215 . ",$value) added: $id $msg" );
223 return $args{Value} unless $args{Value} && $args{PostEdit};
225 $RT::Logger->debug( "Running PostEdit for '$args{Value}'");
226 my $value = $args{Value};
227 local $_ = $value; # backwards compatibility
229 eval( $args{PostEdit} );
230 $RT::Logger->error("$@") if $@;