import rt 3.6.6
[freeside.git] / rt / lib / RT / I18N.pm
index c2746f7..9d0b2b5 100644 (file)
@@ -153,7 +153,7 @@ sub encoding { 'utf-8' }
 
 =head2 SetMIMEEntityToUTF8 $entity
 
-An utility method which will try to convert entity body into utf8.
+An utility function which will try to convert entity body into utf8.
 It's now a wrap-up of SetMIMEEntityToEncoding($entity, 'utf-8').
 
 =cut
@@ -164,16 +164,37 @@ sub SetMIMEEntityToUTF8 {
 
 # }}}
 
+# {{{ IsTextualContentType
+
+=head2 IsTextualContentType $type
+
+An utility function that determines whether $type is I<textual>, meaning
+that it can sensibly be converted to Unicode text.
+
+Currently, it returns true iff $type matches this regular expression
+(case-insensitively):
+
+    ^(?:text/(?:plain|html)|message/rfc822)\b
+
+# }}}
+
+=cut
+
+sub IsTextualContentType {
+    my $type = shift;
+    ($type =~ m{^(?:text/(?:plain|html)|message/rfc822)\b}i) ? 1 : 0;
+}
+
 # {{{ SetMIMEEntityToEncoding
 
 =head2 SetMIMEEntityToEncoding $entity, $encoding
 
-An utility method which will try to convert entity body into specified
+An utility function which will try to convert entity body into specified
 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'.
 
-This method doesn't return anything meaningful.
+This function doesn't return anything meaningful.
 
 =cut
 
@@ -205,11 +226,9 @@ sub SetMIMEEntityToEncoding {
 
     # If this is a textual entity, we'd need to preserve its original encoding
     $head->add( "X-RT-Original-Encoding" => $charset )
-       if $head->mime_attr('content-type.charset') or $head->mime_type =~ /^text/;
-
+       if $head->mime_attr('content-type.charset') or IsTextualContentType($head->mime_type);
 
-    return unless ( $head->mime_type =~ qr{^(text/plain|message/rfc822)$}i  );
-    
+    return unless IsTextualContentType($head->mime_type);
 
     my $body = $entity->bodyhandle;
 
@@ -284,7 +303,7 @@ sub DecodeMIMEWordsToEncoding {
     my $str = shift;
     my $enc = shift;
 
-    @_ = $str =~ m/(.*?)=\?([^?]+)\?([QqBb])\?([^?]+)\?=([^=]*)/gc;
+    @_ = $str =~ m/(.*?)=\?([^?]+)\?([QqBb])\?([^?]+)\?=([^=]*)/gcs;
     return ($str) unless (@_);
 
     # add everything that hasn't matched to the end of the latest
@@ -392,6 +411,7 @@ sub _GuessCharset {
        Encode::Guess->set_suspects(@RT::EmailInputEncodings);
        my $decoder = Encode::Guess->guess( $_[0] );
 
+      if ( defined($decoder) ) {
        if ( ref $decoder ) {
            $charset = $decoder->name;
            $RT::Logger->debug("Guessed encoding: $charset");
@@ -411,6 +431,10 @@ sub _GuessCharset {
        else {
            $RT::Logger->warning("Encode::Guess failed: $decoder; fallback to $fallback");
        }
+      }
+      else {
+         $RT::Logger->warning("Encode::Guess failed: decoder is undefined; fallback to $fallback");
+      }
     }
     else {
        $RT::Logger->warning("Cannot Encode::Guess; fallback to $fallback");