[Rt-commit] rt branch, 3.8/set-mime-encoding-fallback, created. rt-3.8.10-14-gccafbd2

? sunnavy sunnavy at bestpractical.com
Tue Jun 21 01:15:24 EDT 2011


The branch, 3.8/set-mime-encoding-fallback has been created
        at  ccafbd233e4f50bd8ef1889949e91dc6190f495b (commit)

- Log -----------------------------------------------------------------
commit 8087bb0aece69c0c7e358b5ffcc3ab4b5cf769ec
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Jun 21 11:42:54 2011 +0800

    Revert "Encode::from_to don't die unless we tell it to. see also #7126"
    
    This reverts commit 49fa6755dbbf2097646a906e244d1f503be17ddc.
    
    Conflicts:
    
    	lib/RT/I18N.pm

diff --git a/lib/RT/I18N.pm b/lib/RT/I18N.pm
index 2056b3e..4d0ad25 100755
--- a/lib/RT/I18N.pm
+++ b/lib/RT/I18N.pm
@@ -203,11 +203,6 @@ charset encoding (encoded as octets, *not* unicode-strings).  It will
 iterate all the entities in $entity, and try to convert each one into
 specified charset if whose Content-Type is 'text/plain'.
 
-the methods are tries in order:
-1) to convert the entity to $encoding, 
-2) to interpret the entity as iso-8859-1 and then convert it to $encoding,
-3) forcibly convert it to $encoding.
-
 This function doesn't return anything meaningful.
 
 =cut
@@ -248,46 +243,31 @@ sub SetMIMEEntityToEncoding {
 
     if ( $enc ne $charset && $body ) {
         my $string = $body->as_string or return;
-        # NOTE:: see the comments at the end of the sub.
-        Encode::_utf8_off($string);
-        my $orig_string = $string;
 
         # {{{ Convert the body
         eval {
-            $RT::Logger->debug( "Converting '$charset' to '$enc' for "
-                  . $head->mime_type . " - "
-                  . ( $head->get('subject') || 'Subjectless message' ) );
-            Encode::from_to( $string, $charset => $enc, Encode::FB_CROAK );
+            $RT::Logger->debug( "Converting '$charset' to '$enc' for " . $head->mime_type . " - " . ( $head->get('subject') || 'Subjectless message' ) );
+
+            # NOTE:: see the comments at the end of the sub.
+            Encode::_utf8_off( $string);
+            Encode::from_to( $string, $charset => $enc );
         };
 
         if ($@) {
-            $RT::Logger->error( "Encoding error: " 
-                  . $@
-                  . " falling back to iso-8859-1 => $enc" );
-            $string = $orig_string;
-            eval {
-                Encode::from_to(
-                    $string,
-                    'iso-8859-1' => $enc,
-                    Encode::FB_CROAK
-                );
-            };
+            $RT::Logger->error( "Encoding error: " . $@ . " defaulting to ISO-8859-1 -> UTF-8" );
+            eval { Encode::from_to( $string, 'iso-8859-1' => $enc ) };
             if ($@) {
-                $RT::Logger->error( "Encoding error: " 
-                      . $@
-                      . " forcing conversion to $charset => $enc" );
-                $string = $orig_string;
-                Encode::from_to( $string, $charset => $enc );
+                $RT::Logger->crit( "Totally failed to convert to utf-8: " . $@ . " I give up" );
             }
         }
 
         # }}}
 
-        my $new_body = MIME::Body::InCore->new($string);
+        my $new_body = MIME::Body::InCore->new( $string);
 
         # set up the new entity
         $head->mime_attr( "content-type" => 'text/plain' )
-          unless ( $head->mime_attr("content-type") );
+            unless ( $head->mime_attr("content-type") );
         $head->mime_attr( "content-type.charset" => $enc );
         $entity->bodyhandle($new_body);
     }
@@ -362,7 +342,7 @@ sub DecodeMIMEWordsToEncoding {
         # now we have got a decoded subject, try to convert into the encoding
         unless ( $charset eq $to_charset ) {
             my $orig_str = $enc_str;
-            eval { Encode::from_to( $enc_str, $charset, $to_charset, Encode::FB_CROAK ) };
+            eval { Encode::from_to( $enc_str, $charset, $to_charset ) };
             if ($@) {
                 $enc_str = $orig_str;
                 $charset = _GuessCharset( $enc_str );
@@ -514,30 +494,18 @@ sub SetMIMEHeadToEncoding {
         my @values = $head->get_all($tag);
         $head->delete($tag);
         foreach my $value (@values) {
-            Encode::_utf8_off($value);
-            my $orig_value = $value;
             if ( $charset ne $enc ) {
+
                 eval {
-                    Encode::from_to( $value, $charset => $enc, Encode::FB_CROAK );
+                    Encode::_utf8_off($value);
+                    Encode::from_to( $value, $charset => $enc );
                 };
                 if ($@) {
-                    $RT::Logger->error( "Encoding error: " 
-                          . $@
-                          . " falling back to iso-8859-1 => $enc" );
-                    $value = $orig_value;
-                    eval {
-                        Encode::from_to(
-                            $value,
-                            'iso-8859-1' => $enc,
-                            Encode::FB_CROAK
-                        );
-                    };
+                    $RT::Logger->error( "Encoding error: " . $@
+                                       . " defaulting to ISO-8859-1 -> UTF-8" );
+                    eval { Encode::from_to( $value, 'iso-8859-1' => $enc ) };
                     if ($@) {
-                        $RT::Logger->error( "Encoding error: " 
-                              . $@
-                              . " forcing conversion to $charset => $enc" );
-                        $value = $orig_value;
-                        Encode::from_to( $value, $charset => $enc );
+                        $RT::Logger->crit( "Totally failed to convert to utf-8: " . $@ . " I give up" );
                     }
                 }
             }

commit 1a12831804477b5cdbf91bcbf7169cd7e31ada52
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Jun 21 11:51:28 2011 +0800

    TODO revalent tests as we reverted 49fa675

diff --git a/t/mail/wrong_mime_charset.t b/t/mail/wrong_mime_charset.t
index f53c872..d496a4b 100644
--- a/t/mail/wrong_mime_charset.t
+++ b/t/mail/wrong_mime_charset.t
@@ -17,9 +17,15 @@ my $mime           = MIME::Entity->build(
 $mime->head->mime_attr( "Content-Type.charset" => 'utf8' );
 
 RT::I18N::SetMIMEEntityToEncoding( $mime, 'iso-8859-1' );
+
+TODO: {
+        local $TODO =
+'need a better approach of encoding converter, should be fixed in 4.2';
+
 my $subject = decode( 'iso-8859-1', $mime->head->get('Subject') );
 chomp $subject;
 is( $subject, $test_string, 'subject is set to iso-8859-1' );
 my $body = decode( 'iso-8859-1', $mime->stringify_body );
 chomp $body;
 is( $body, $test_string, 'body is set to iso-8859-1' );
+}

commit ccafbd233e4f50bd8ef1889949e91dc6190f495b
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Jun 21 11:54:46 2011 +0800

    removed the eval and the unreachable if ($@) code.
    
    note: a more right solution should be done in 4.2:
    ( see also #17680 )
    
    incoming mail:
    1) find encoding
    2) if found then try to convert to utf-8 in croak mode, return if success
    3) guess encoding
    4) if guessed differently then try to convert to utf-8 in croak mode, return
    if success
    5) mark part as application/octet-stream instead of falling back to any
    encoding
    
    outgoing mail:
    1) find encoding
    2) if didn't find then do nothing, send as is, let MUA deal with it
    3) if found then try to convert it to outgoing encoding in croak mode, return
    if success
    4) do nothing otherwise, keep original encoding

diff --git a/lib/RT/I18N.pm b/lib/RT/I18N.pm
index 4d0ad25..4c70922 100755
--- a/lib/RT/I18N.pm
+++ b/lib/RT/I18N.pm
@@ -245,21 +245,11 @@ sub SetMIMEEntityToEncoding {
         my $string = $body->as_string or return;
 
         # {{{ Convert the body
-        eval {
-            $RT::Logger->debug( "Converting '$charset' to '$enc' for " . $head->mime_type . " - " . ( $head->get('subject') || 'Subjectless message' ) );
-
-            # NOTE:: see the comments at the end of the sub.
-            Encode::_utf8_off( $string);
-            Encode::from_to( $string, $charset => $enc );
-        };
-
-        if ($@) {
-            $RT::Logger->error( "Encoding error: " . $@ . " defaulting to ISO-8859-1 -> UTF-8" );
-            eval { Encode::from_to( $string, 'iso-8859-1' => $enc ) };
-            if ($@) {
-                $RT::Logger->crit( "Totally failed to convert to utf-8: " . $@ . " I give up" );
-            }
-        }
+        $RT::Logger->debug( "Converting '$charset' to '$enc' for " . $head->mime_type . " - " . ( $head->get('subject') || 'Subjectless message' ) );
+
+        # NOTE:: see the comments at the end of the sub.
+        Encode::_utf8_off( $string);
+        Encode::from_to( $string, $charset => $enc );
 
         # }}}
 
@@ -341,13 +331,7 @@ sub DecodeMIMEWordsToEncoding {
 
         # now we have got a decoded subject, try to convert into the encoding
         unless ( $charset eq $to_charset ) {
-            my $orig_str = $enc_str;
-            eval { Encode::from_to( $enc_str, $charset, $to_charset ) };
-            if ($@) {
-                $enc_str = $orig_str;
-                $charset = _GuessCharset( $enc_str );
-                Encode::from_to( $enc_str, $charset, $to_charset );
-            }
+            Encode::from_to( $enc_str, $charset, $to_charset );
         }
 
         # XXX TODO: RT doesn't currently do the right thing with mime-encoded headers
@@ -496,18 +480,8 @@ sub SetMIMEHeadToEncoding {
         foreach my $value (@values) {
             if ( $charset ne $enc ) {
 
-                eval {
-                    Encode::_utf8_off($value);
-                    Encode::from_to( $value, $charset => $enc );
-                };
-                if ($@) {
-                    $RT::Logger->error( "Encoding error: " . $@
-                                       . " defaulting to ISO-8859-1 -> UTF-8" );
-                    eval { Encode::from_to( $value, 'iso-8859-1' => $enc ) };
-                    if ($@) {
-                        $RT::Logger->crit( "Totally failed to convert to utf-8: " . $@ . " I give up" );
-                    }
-                }
+                Encode::_utf8_off($value);
+                Encode::from_to( $value, $charset => $enc );
             }
             $value = DecodeMIMEWordsToEncoding( $value, $enc, $tag )
                 unless $preserve_words;

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


More information about the Rt-commit mailing list