X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=rt%2Flib%2FRT%2FEmailParser.pm;h=2954505ff8e622779125f9c34023c9cb33a15a1e;hb=1c538bfabc2cd31f27067505f0c3d1a46cba6ef0;hp=19dc2c9e8a452feea095187730b1a2bfa5d11dd5;hpb=3d0a1bb06b895c5be6e3f0517d355442a6b1e125;p=freeside.git diff --git a/rt/lib/RT/EmailParser.pm b/rt/lib/RT/EmailParser.pm index 19dc2c9e8..2954505ff 100644 --- a/rt/lib/RT/EmailParser.pm +++ b/rt/lib/RT/EmailParser.pm @@ -2,7 +2,7 @@ # # COPYRIGHT: # -# This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC +# This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC # # # (Except where explicitly superseded by other copyright notices) @@ -110,7 +110,7 @@ sub SmartParseMIMEEntityFromScalar { # accommodate this by pausing and retrying. last if ( $fh, $temp_file ) = - eval { File::Temp::tempfile( undef, UNLINK => 0 ) }; + eval { File::Temp::tempfile( UNLINK => 0 ) }; sleep 1; } if ($fh) { @@ -122,10 +122,8 @@ sub SmartParseMIMEEntityFromScalar { close($fh); if ( -f $temp_file ) { - # We have to trust the temp file's name -- untaint it - $temp_file =~ /(.*)/; - my $entity = $self->ParseMIMEEntityFromFile( $1, $args{'Decode'}, $args{'Exact'} ); - unlink($1); + my $entity = $self->ParseMIMEEntityFromFile( $temp_file, $args{'Decode'}, $args{'Exact'} ); + unlink($temp_file); return $entity; } } @@ -299,8 +297,8 @@ sub ParseCcAddressesFromHead { my (@Addresses); - my @ToObjs = Email::Address->parse( $self->Head->get('To') ); - my @CcObjs = Email::Address->parse( $self->Head->get('Cc') ); + my @ToObjs = Email::Address->parse( Encode::decode( "UTF-8", $self->Head->get('To') ) ); + my @CcObjs = Email::Address->parse( Encode::decode( "UTF-8", $self->Head->get('Cc') ) ); foreach my $AddrObj ( @ToObjs, @CcObjs ) { my $Address = $AddrObj->address; @@ -328,6 +326,8 @@ sub IsRTAddress { my $self = shift; my $address = shift; + return undef unless defined($address) and $address =~ /\S/; + if ( my $address_re = RT->Config->Get('RTAddressRegexp') ) { return $address =~ /$address_re/i ? 1 : undef; } @@ -526,28 +526,71 @@ we can use that removes the bandaid =cut +use Email::Address::List; + sub ParseEmailAddress { my $self = shift; my $address_string = shift; - $address_string =~ s/^\s+|\s+$//g; + my @list = Email::Address::List->parse( + $address_string, + skip_comments => 1, + skip_groups => 1, + ); + my $logger = sub { RT->Logger->error( + "Unable to parse an email address from $address_string: ". shift + ) }; my @addresses; - # if it looks like a username / local only email - if ($address_string !~ /@/ && $address_string =~ /^\w+$/) { - my $user = RT::User->new( RT->SystemUser ); - my ($id, $msg) = $user->Load($address_string); - if ($id) { - push @addresses, Email::Address->new($user->Name,$user->EmailAddress); + foreach my $e ( @list ) { + if ($e->{'type'} eq 'mailbox') { + if ($e->{'not_ascii'}) { + $logger->($e->{'value'} ." contains not ASCII values"); + next; + } + push @addresses, $e->{'value'} + } elsif ( $e->{'value'} =~ /^\s*(\w+)\s*$/ ) { + my $user = RT::User->new( RT->SystemUser ); + $user->Load( $1 ); + if ($user->id) { + push @addresses, Email::Address->new($user->Name, $user->EmailAddress); + } else { + $logger->($e->{'value'} ." is not a valid email address and is not user name"); + } } else { - $RT::Logger->error("Unable to parse an email address from $address_string: $msg"); + $logger->($e->{'value'} ." is not a valid email address"); } - } else { - @addresses = Email::Address->parse($address_string); } + $self->CleanupAddresses(@addresses); + return @addresses; +} + +=head2 CleanupAddresses ARRAY +Massages an array of L objects to make their email addresses +more palatable. + +Currently this strips off surrounding single quotes around C<< ->address >> and +B<< modifies the L objects in-place >>. + +Returns the list of objects for convienence in C/C chains. + +=cut + +sub CleanupAddresses { + my $self = shift; + + for my $addr (@_) { + next unless defined $addr; + # Outlook sometimes sends addresses surrounded by single quotes; + # clean them all up + if ((my $email = $addr->address) =~ s/^'(.+)'$/$1/) { + $addr->address($email); + } + } + return @_; } =head2 RescueOutlook @@ -590,7 +633,7 @@ sub RescueOutlook { # Add base64 since we've seen examples of double newlines with # this type too. Need an example of a multi-part base64 to # handle that permutation if it exists. - elsif ( $mime->head->get('Content-Transfer-Encoding') =~ m{base64} ) { + elsif ( ($mime->head->get('Content-Transfer-Encoding')||'') =~ m{base64} ) { $text_part = $mime; # Assuming single part, already decoded. }