[Rt-commit] rt branch, 4.0/unknown-email-charset, created. rt-4.0.5-135-g8a8aad6

Ruslan Zakirov ruz at bestpractical.com
Wed May 9 08:23:11 EDT 2012


The branch, 4.0/unknown-email-charset has been created
        at  8a8aad65e142c89453221e37f989f627d6410b34 (commit)

- Log -----------------------------------------------------------------
commit 8a8aad65e142c89453221e37f989f627d6410b34
Author: Ruslan Zakirov <ruz at bestpractical.com>
Date:   Wed May 9 16:15:24 2012 +0400

    convert text part to binary when we can not decode
    
    If we couldn't find or guess charset or if charset
    is not supported or failed to parse properly then
    RT was dropping email.
    
    Now instead of dropping we set content type to
    'application/octet-stream' and save original value
    to X-RT-Original-Content-Type header. In this
    case users can download part and deal with it.
    
    In most cases it would be related to spam, but
    there are valid charsets that are not supported
    by Encode module.

diff --git a/lib/RT/I18N.pm b/lib/RT/I18N.pm
index cadf7cc..e2751c4 100644
--- a/lib/RT/I18N.pm
+++ b/lib/RT/I18N.pm
@@ -209,16 +209,27 @@ sub SetMIMEEntityToEncoding {
     # do the same for parts first of all
     SetMIMEEntityToEncoding( $_, $enc, $preserve_words ) foreach $entity->parts;
 
-    my $charset = _FindOrGuessCharset($entity) or return;
+    my $head = $entity->head;
+
+    my $charset = _FindOrGuessCharset($entity);
+    if ( $charset ) {
+        unless( Encode::find_encoding($charset) ) {
+            $RT::Logger->warning("Encoding '$charset' is not supported");
+            $charset = undef;
+        }
+    }
+    unless ( $charset ) {
+        $head->replace( "X-RT-Original-Content-Type" => $head->mime_attr('Content-Type') );
+        $head->mime_attr('Content-Type' => 'application/octet-stream');
+        return;
+    }
 
     SetMIMEHeadToEncoding(
-	$entity->head,
+	$head,
 	_FindOrGuessCharset($entity, 1) => $enc,
 	$preserve_words
     );
 
-    my $head = $entity->head;
-
     # If this is a textual entity, we'd need to preserve its original encoding
     $head->replace( "X-RT-Original-Encoding" => $charset )
 	if $head->mime_attr('content-type.charset') or IsTextualContentType($head->mime_type);
diff --git a/t/mail/not-supported-charset.t b/t/mail/not-supported-charset.t
new file mode 100644
index 0000000..aefe80c
--- /dev/null
+++ b/t/mail/not-supported-charset.t
@@ -0,0 +1,42 @@
+use strict;
+use warnings;
+
+use RT::Test tests => undef;
+
+my $queue = RT::Test->load_or_create_queue( Name => 'General' );
+ok $queue->id, 'loaded queue';
+
+{
+    my $mail = <<'END';
+From: root at localhost
+Subject: test
+Content-type: text/plain; charset="not-supported-encoding"
+
+ho hum just some text
+
+END
+
+    my ($stat, $id) = RT::Test->send_via_mailgate($mail);
+    is( $stat >> 8, 0, "The mail gateway exited normally" );
+    ok( $id, "created ticket" );
+
+    my $ticket = RT::Ticket->new( RT->SystemUser );
+    $ticket->Load($id);
+    ok $ticket->id, "loaded ticket";
+
+    my $txn = $ticket->Transactions->First;
+    ok !$txn->ContentObj, 'no content';
+
+    my $attach = $txn->Attachments->First;
+    like $attach->Content, qr{ho hum just some text}, 'attachment is there';
+    is $attach->GetHeader('Content-Type'),
+        'application/octet-stream; charset="not-supported-encoding"',
+        'content type is changed'
+    ;
+    is $attach->GetHeader('X-RT-Original-Content-Type'),
+        'text/plain',
+        'original content type is saved'
+    ;
+}
+
+done_testing;
\ No newline at end of file

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


More information about the Rt-commit mailing list