[Rt-commit] rt branch, 3.9-trunk, updated. rt-3.8.8-124-gcdc72fe

? sunnavy sunnavy at bestpractical.com
Thu Jul 8 08:14:20 EDT 2010


The branch, 3.9-trunk has been updated
       via  cdc72fee298e9a27682c5f2ff3968f6beae41642 (commit)
      from  ead9056e2c7da9611a4b51b82d0ac695e3cc1505 (commit)

Summary of changes:
 lib/RT/I18N.pm         |   30 +++++++++++++++++++++++++++++-
 t/mail/mime_decoding.t |   13 ++++++++++++-
 2 files changed, 41 insertions(+), 2 deletions(-)

- Log -----------------------------------------------------------------
commit cdc72fee298e9a27682c5f2ff3968f6beae41642
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Jul 8 20:15:23 2010 +0800

    handle rfc 2231 encoding

diff --git a/lib/RT/I18N.pm b/lib/RT/I18N.pm
index 902701a..665a8db 100755
--- a/lib/RT/I18N.pm
+++ b/lib/RT/I18N.pm
@@ -332,8 +332,8 @@ sub DecodeMIMEWordsToEncoding {
     my $enc = shift;
 
     @_ = $str =~ m/(.*?)=\?([^?]+)\?([QqBb])\?([^?]+)\?=([^=]*)/gcs;
-    return ($str) unless (@_);
 
+    if ( @_ ) {
     # add everything that hasn't matched to the end of the latest
     # string in array this happen when we have 'key="=?encoded?="; key="plain"'
     $_[-1] .= substr($str, pos $str);
@@ -388,6 +388,34 @@ sub DecodeMIMEWordsToEncoding {
 
 	$str .= $prefix . $enc_str . $trailing;
     }
+    }
+
+# handle filename*=ISO-8859-1''%74%E9%73%74%2E%74%78%74, see also rfc 2231
+    @_ = $str =~ m/(.*?\*=)([^']*?)'([^']*?)'(\S+)(.*?)(?=(?:\*=|$))/gcs;
+    if (@_) {
+        $str = '';
+        while (@_) {
+            my ( $prefix, $charset, $language, $enc_str, $trailing ) =
+              ( shift, shift, shift, shift, shift );
+            $prefix =~ s/\*=$/=/; # remove the *
+            $enc_str =~ s/%(\w{2})/chr hex $1/eg;
+            unless ( $charset eq $enc ) {
+                my $orig_str = $enc_str;
+                eval {
+                    Encode::from_to( $enc_str, $charset, $enc,
+                        Encode::FB_CROAK );
+                };
+                if ($@) {
+                    $enc_str = $orig_str;
+                    $charset = _GuessCharset($enc_str);
+                    Encode::from_to( $enc_str, $charset, $enc );
+                }
+            }
+            $enc_str = qq{"$enc_str"}
+              if $enc_str =~ /[,;]/ and $enc_str !~ /^".*"$/;
+            $str .= $prefix . $enc_str . $trailing;
+        }
+     }
 
     # We might have \n without trailing whitespace, which will result in
     # invalid headers.
diff --git a/t/mail/mime_decoding.t b/t/mail/mime_decoding.t
index 8257aee..7e48046 100644
--- a/t/mail/mime_decoding.t
+++ b/t/mail/mime_decoding.t
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 use strict;
 use warnings;
-use RT::Test nodata => 1, tests => 6;
+use RT::Test nodata => 1, tests => 7;
 
 use_ok('RT::I18N');
 
@@ -57,3 +57,14 @@ diag q{newline and encoded file name} if $ENV{TEST_VERBOSE};
     );
 }
 
+diag q{rfc2231} if $ENV{TEST_VERBOSE};
+{
+    my $str =
+"filename*=ISO-8859-1''%74%E9%73%74%2E%74%78%74 filename*=ISO-8859-1''%74%E9%73%74%2E%74%78%74";
+    is(
+        RT::I18N::DecodeMIMEWordsToEncoding( $str, 'utf-8' ),
+        'filename=tést.txt filename=tést.txt',
+        'right decodig'
+    );
+}
+

-----------------------------------------------------------------------


More information about the Rt-commit mailing list