1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC
6 # <jesse@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/copyleft/gpl.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
50 RT::I18N - a base class for localization of RT
59 use Locale::Maketext 1.04;
60 use Locale::Maketext::Lexicon 0.25;
61 use base ('Locale::Maketext::Fuzzy');
67 # I decree that this project's first language is English.
70 'TEST_STRING' => 'Concrete Mixer',
72 '__Content-Type' => 'text/plain; charset=utf-8',
75 # That means that lookup failures can't happen -- if we get as far
76 # as looking for something in this lexicon, and we don't find it,
77 # then automagically set $Lexicon{$key} = $key, before possibly
80 # The exception is keys that start with "_" -- they aren't auto-makeable.
87 Initializes the lexicons used for localization.
101 # Load language-specific functions
102 foreach my $language ( File::Glob::bsd_glob(substr(__FILE__, 0, -3) . "/*.pm")) {
103 if ($language =~ /^([-\w\s.\/\\~:]+)$/) {
107 warn("$language is tainted. not loading");
111 my @lang = @RT::LexiconLanguages;
112 @lang = ('*') unless @lang;
114 # Acquire all .po files and iterate them into lexicons
115 Locale::Maketext::Lexicon->import({
118 Gettext => (substr(__FILE__, 0, -3) . "/$_.po"),
119 Gettext => "$RT::LocalLexiconPath/*/$_.po",
120 Gettext => "$RT::LocalLexiconPath/$_.po",
130 Returns the encoding of the current lexicon, as yanked out of __ContentType's "charset" field.
131 If it can't find anything, it returns 'ISO-8859-1'
135 ok(my $chinese = RT::I18N->get_handle('zh_tw'));
136 ok(UNIVERSAL::can($chinese, 'maketext'));
137 ok($chinese->maketext('__Content-Type') =~ /utf-8/i, "Found the utf-8 charset for traditional chinese in the string ".$chinese->maketext('__Content-Type'));
138 ok($chinese->encoding eq 'utf-8', "The encoding is 'utf-8' -".$chinese->encoding);
140 ok(my $en = RT::I18N->get_handle('en'));
141 ok(UNIVERSAL::can($en, 'maketext'));
142 ok($en->encoding eq 'utf-8', "The encoding ".$en->encoding." is 'utf-8'");
150 sub encoding { 'utf-8' }
152 # {{{ SetMIMEEntityToUTF8
154 =head2 SetMIMEEntityToUTF8 $entity
156 An utility method which will try to convert entity body into utf8.
157 It's now a wrap-up of SetMIMEEntityToEncoding($entity, 'utf-8').
161 sub SetMIMEEntityToUTF8 {
162 RT::I18N::SetMIMEEntityToEncoding(shift, 'utf-8');
167 # {{{ SetMIMEEntityToEncoding
169 =head2 SetMIMEEntityToEncoding $entity, $encoding
171 An utility method which will try to convert entity body into specified
172 charset encoding (encoded as octets, *not* unicode-strings). It will
173 iterate all the entities in $entity, and try to convert each one into
174 specified charset if whose Content-Type is 'text/plain'.
176 This method doesn't return anything meaningful.
180 sub SetMIMEEntityToEncoding {
181 my ( $entity, $enc, $preserve_words ) = ( shift, shift, shift );
183 # do the same for parts first of all
184 SetMIMEEntityToEncoding( $_, $enc, $preserve_words ) foreach $entity->parts;
186 my $charset = _FindOrGuessCharset($entity) or return;
187 # one and only normalization
188 $charset = 'utf-8' if $charset =~ /^utf-?8$/i;
189 $enc = 'utf-8' if $enc =~ /^utf-?8$/i;
191 SetMIMEHeadToEncoding(
193 _FindOrGuessCharset($entity, 1) => $enc,
197 my $head = $entity->head;
199 # convert at least MIME word encoded attachment filename
200 foreach my $attr (qw(content-type.name content-disposition.filename)) {
201 if ( my $name = $head->mime_attr($attr) and !$preserve_words ) {
202 $head->mime_attr( $attr => DecodeMIMEWordsToUTF8($name) );
206 # If this is a textual entity, we'd need to preserve its original encoding
207 $head->add( "X-RT-Original-Encoding" => $charset )
208 if $head->mime_attr('content-type.charset') or $head->mime_type =~ /^text/;
211 return unless ( $head->mime_type =~ qr{^(text/plain|message/rfc822)$}i );
214 my $body = $entity->bodyhandle;
216 if ( $enc ne $charset && $body) {
217 my @lines = $body->as_lines or return;
219 # {{{ Convert the body
221 $RT::Logger->debug("Converting '$charset' to '$enc' for ". $head->mime_type . " - ". ($head->get('subject') || 'Subjectless message'));
223 # NOTE:: see the comments at the end of the sub.
224 Encode::_utf8_off( $lines[$_] ) foreach ( 0 .. $#lines );
225 Encode::from_to( $lines[$_], $charset => $enc ) for ( 0 .. $#lines );
229 $RT::Logger->error( "Encoding error: " . $@ . " defaulting to ISO-8859-1 -> UTF-8" );
231 Encode::from_to( $lines[$_], 'iso-8859-1' => $enc ) foreach ( 0 .. $#lines );
234 $RT::Logger->crit( "Totally failed to convert to utf-8: " . $@ . " I give up" );
239 my $new_body = MIME::Body::InCore->new( \@lines );
241 # set up the new entity
242 $head->mime_attr( "content-type" => 'text/plain' )
243 unless ( $head->mime_attr("content-type") );
244 $head->mime_attr( "content-type.charset" => $enc );
245 $entity->bodyhandle($new_body);
249 # NOTES: Why Encode::_utf8_off before Encode::from_to
251 # All the strings in RT are utf-8 now. Quotes from Encode POD:
253 # [$length =] from_to($octets, FROM_ENC, TO_ENC [, CHECK])
254 # ... The data in $octets must be encoded as octets and not as
255 # characters in Perl's internal format. ...
257 # Not turning off the UTF-8 flag in the string will prevent the string
262 # {{{ DecodeMIMEWordsToUTF8
264 =head2 DecodeMIMEWordsToUTF8 $raw
266 An utility method which mimics MIME::Words::decode_mimewords, but only
267 limited functionality. This function returns an utf-8 string.
269 It returns the decoded string, or the original string if it's not
270 encoded. Since the subroutine converts specified string into utf-8
271 charset, it should not alter a subject written in English.
273 Why not use MIME::Words directly? Because it fails in RT when I
274 tried. Maybe it's ok now.
278 sub DecodeMIMEWordsToUTF8 {
280 DecodeMIMEWordsToEncoding($str, 'utf-8');
283 sub DecodeMIMEWordsToEncoding {
287 @_ = $str =~ m/(.*?)=\?([^?]+)\?([QqBb])\?([^?]+)\?=([^=]*)/gc;
288 return ($str) unless (@_);
290 # add everything that hasn't matched to the end of the latest
291 # string in array this happen when we have 'key="=?encoded?="; key="plain"'
292 $_[-1] .= substr($str, pos $str);
296 my ($prefix, $charset, $encoding, $enc_str, $trailing) =
297 (shift, shift, lc shift, shift, shift);
299 $trailing =~ s/\s?\t?$//; # Observed from Outlook Express
301 if ( $encoding eq 'q' ) {
302 use MIME::QuotedPrint;
303 $enc_str =~ tr/_/ /; # Observed from Outlook Express
304 $enc_str = decode_qp($enc_str);
305 } elsif ( $encoding eq 'b' ) {
307 $enc_str = decode_base64($enc_str);
309 $RT::Logger->warning("Incorrect encoding '$encoding' in '$str', "
310 ."only Q(uoted-printable) and B(ase64) are supported");
313 # now we have got a decoded subject, try to convert into the encoding
314 unless ($charset eq $enc) {
315 eval { Encode::from_to($enc_str, $charset, $enc) };
317 $charset = _GuessCharset( $enc_str );
318 Encode::from_to($enc_str, $charset, $enc);
322 # XXX TODO: RT doesn't currently do the right thing with mime-encoded headers
323 # We _should_ be preserving them encoded until after parsing is completed and
324 # THEN undo the mime-encoding.
326 # This routine should be translating the existing mimeencoding to utf8 but leaving
329 # It's legal for headers to contain mime-encoded commas and semicolons which
330 # should not be treated as address separators. (Encoding == quoting here)
332 # until this is fixed, we must escape any string containing a comma or semicolon
333 # this is only a bandaid
335 $enc_str = qq{"$enc_str"} if ($enc_str =~ /[,;]/);
336 $str .= $prefix . $enc_str . $trailing;
339 # We might have \n without trailing whitespace, which will result in
348 # {{{ _FindOrGuessCharset
350 =head2 _FindOrGuessCharset MIME::Entity, $head_only
352 When handed a MIME::Entity will first attempt to read what charset the message is encoded in. Failing that, will use Encode::Guess to try to figure it out
354 If $head_only is true, only guesses charset for head parts. This is because header's encoding (e.g. filename="...") may be different from that of body's.
358 sub _FindOrGuessCharset {
360 my $head_only = shift;
361 my $head = $entity->head;
363 if ( my $charset = $head->mime_attr("content-type.charset") ) {
367 if ( !$head_only and $head->mime_type =~ m{^text/}) {
368 my $body = $entity->bodyhandle or return;
369 return _GuessCharset( $body->as_string );
372 # potentially binary data -- don't guess the body
373 return _GuessCharset( $head->as_string );
381 =head2 _GuessCharset STRING
383 use Encode::Guess to try to figure it out the string's encoding.
388 my $fallback = 'iso-8859-1';
391 if ( @RT::EmailInputEncodings and eval { require Encode::Guess; 1 } ) {
392 Encode::Guess->set_suspects(@RT::EmailInputEncodings);
393 my $decoder = Encode::Guess->guess( $_[0] );
395 if ( ref $decoder ) {
396 $charset = $decoder->name;
397 $RT::Logger->debug("Guessed encoding: $charset");
400 elsif ($decoder =~ /(\S+ or .+)/) {
401 my %matched = map { $_ => 1 } split(/ or /, $1);
402 return 'utf-8' if $matched{'utf8'}; # one and only normalization
404 foreach my $suspect (@RT::EmailInputEncodings) {
405 next unless $matched{$suspect};
406 $RT::Logger->debug("Encode::Guess ambiguous ($decoder); using $suspect");
412 $RT::Logger->warning("Encode::Guess failed: $decoder; fallback to $fallback");
416 $RT::Logger->warning("Cannot Encode::Guess; fallback to $fallback");
419 return($charset || $fallback);
424 # {{{ SetMIMEHeadToEncoding
426 =head2 SetMIMEHeadToEncoding HEAD OLD_CHARSET NEW_CHARSET
428 Converts a MIME Head from one encoding to another. This totally violates the RFC.
429 We should never need this. But, Surprise!, MUAs are badly broken and do this kind of stuff
435 sub SetMIMEHeadToEncoding {
436 my ( $head, $charset, $enc, $preserve_words ) = ( shift, shift, shift, shift );
438 $charset = 'utf-8' if $charset eq 'utf8';
439 $enc = 'utf-8' if $enc eq 'utf8';
441 return if $charset eq $enc and $preserve_words;
443 foreach my $tag ( $head->tags ) {
444 next unless $tag; # seen in wild: headers with no name
445 my @values = $head->get_all($tag);
447 foreach my $value (@values) {
448 if ( $charset ne $enc ) {
451 Encode::_utf8_off($value);
452 Encode::from_to( $value, $charset => $enc );
455 $RT::Logger->error( "Encoding error: " . $@
456 . " defaulting to ISO-8859-1 -> UTF-8" );
457 eval { Encode::from_to( $value, 'iso-8859-1' => $enc ) };
459 $RT::Logger->crit( "Totally failed to convert to utf-8: " . $@ . " I give up" );
463 $value = DecodeMIMEWordsToEncoding( $value, $enc ) unless $preserve_words;
464 $head->add( $tag, $value );
471 eval "require RT::I18N_Vendor";
472 die $@ if ($@ && $@ !~ qr{^Can't locate RT/I18N_Vendor.pm});
473 eval "require RT::I18N_Local";
474 die $@ if ($@ && $@ !~ qr{^Can't locate RT/I18N_Local.pm});