1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6 # <sales@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/licenses/old-licenses/gpl-2.0.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 }}}
51 RT::I18N - a base class for localization of RT
61 use Locale::Maketext 1.04;
62 use Locale::Maketext::Lexicon 0.25;
63 use base 'Locale::Maketext::Fuzzy';
70 # I decree that this project's first language is English.
73 'TEST_STRING' => 'Concrete Mixer',
75 '__Content-Type' => 'text/plain; charset=utf-8',
78 # That means that lookup failures can't happen -- if we get as far
79 # as looking for something in this lexicon, and we don't find it,
80 # then automagically set $Lexicon{$key} = $key, before possibly
83 # The exception is keys that start with "_" -- they aren't auto-makeable.
90 Initializes the lexicons used for localization.
97 my @lang = RT->Config->Get('LexiconLanguages');
98 @lang = ('*') unless @lang;
100 # load default functions
101 require substr(__FILE__, 0, -3) . '/i_default.pm';
103 # Load language-specific functions
104 foreach my $file ( File::Glob::bsd_glob(substr(__FILE__, 0, -3) . "/*.pm") ) {
105 unless ( $file =~ /^([-\w\s\.\/\\~:]+)$/ ) {
106 warn("$file is tainted. not loading");
111 my ($lang) = ($file =~ /([^\\\/]+?)\.pm$/);
112 next unless grep $_ eq '*' || $_ eq $lang, @lang;
117 foreach my $l ( @lang ) {
119 Gettext => $RT::LexiconPath."/$l.po",
121 push @{ $import{$l} }, map {(Gettext => "$_/$l.po")} RT->PluginDirs('po');
122 push @{ $import{$l} }, (Gettext => $RT::LocalLexiconPath."/*/$l.po",
123 Gettext => $RT::LocalLexiconPath."/$l.po");
126 # Acquire all .po files and iterate them into lexicons
127 Locale::Maketext::Lexicon->import({ _decode => 1, %import });
135 foreach my $k (keys %{RT::I18N::} ) {
136 next if $k eq 'main::';
137 next unless index($k, '::', -2) >= 0;
138 next unless exists ${ 'RT::I18N::'. $k }{'Lexicon'};
140 my $lex = *{ ${'RT::I18N::'. $k }{'Lexicon'} }{HASH};
141 # run fetch to force load
142 my $tmp = $lex->{'foo'};
143 # XXX: untie may fail with "untie attempted
144 # while 1 inner references still exist"
145 # TODO: untie that has to lower fetch impact
146 # untie %$lex if tied %$lex;
152 Returns the encoding of the current lexicon, as yanked out of __ContentType's "charset" field.
153 If it can't find anything, it returns 'ISO-8859-1'
160 sub encoding { 'utf-8' }
163 =head2 SetMIMEEntityToUTF8 $entity
165 An utility function which will try to convert entity body into utf8.
166 It's now a wrap-up of SetMIMEEntityToEncoding($entity, 'utf-8').
170 sub SetMIMEEntityToUTF8 {
171 RT::I18N::SetMIMEEntityToEncoding(shift, 'utf-8');
176 =head2 IsTextualContentType $type
178 An utility function that determines whether $type is I<textual>, meaning
179 that it can sensibly be converted to Unicode text.
181 Currently, it returns true iff $type matches this regular expression
182 (case-insensitively):
184 ^(?:text/(?:plain|html)|message/rfc822)\b
189 sub IsTextualContentType {
191 ($type =~ m{^(?:text/(?:plain|html)|message/rfc822)\b}i) ? 1 : 0;
195 =head2 SetMIMEEntityToEncoding $entity, $encoding
197 An utility function which will try to convert entity body into specified
198 charset encoding (encoded as octets, *not* unicode-strings). It will
199 iterate all the entities in $entity, and try to convert each one into
200 specified charset if whose Content-Type is 'text/plain'.
202 This function doesn't return anything meaningful.
206 sub SetMIMEEntityToEncoding {
207 my ( $entity, $enc, $preserve_words ) = ( shift, shift, shift );
209 # do the same for parts first of all
210 SetMIMEEntityToEncoding( $_, $enc, $preserve_words ) foreach $entity->parts;
212 my $charset = _FindOrGuessCharset($entity) or return;
214 SetMIMEHeadToEncoding(
216 _FindOrGuessCharset($entity, 1) => $enc,
220 my $head = $entity->head;
222 # convert at least MIME word encoded attachment filename
223 foreach my $attr (qw(content-type.name content-disposition.filename)) {
224 if ( my $name = $head->mime_attr($attr) and !$preserve_words ) {
225 $head->mime_attr( $attr => DecodeMIMEWordsToUTF8($name) );
229 # If this is a textual entity, we'd need to preserve its original encoding
230 $head->replace( "X-RT-Original-Encoding" => $charset )
231 if $head->mime_attr('content-type.charset') or IsTextualContentType($head->mime_type);
233 return unless IsTextualContentType($head->mime_type);
235 my $body = $entity->bodyhandle;
237 if ( $enc ne $charset && $body ) {
238 my $string = $body->as_string or return;
240 $RT::Logger->debug( "Converting '$charset' to '$enc' for "
241 . $head->mime_type . " - "
242 . ( $head->get('subject') || 'Subjectless message' ) );
244 # NOTE:: see the comments at the end of the sub.
245 Encode::_utf8_off($string);
246 Encode::from_to( $string, $charset => $enc );
248 my $new_body = MIME::Body::InCore->new($string);
250 # set up the new entity
251 $head->mime_attr( "content-type" => 'text/plain' )
252 unless ( $head->mime_attr("content-type") );
253 $head->mime_attr( "content-type.charset" => $enc );
254 $entity->bodyhandle($new_body);
258 # NOTES: Why Encode::_utf8_off before Encode::from_to
260 # All the strings in RT are utf-8 now. Quotes from Encode POD:
262 # [$length =] from_to($octets, FROM_ENC, TO_ENC [, CHECK])
263 # ... The data in $octets must be encoded as octets and not as
264 # characters in Perl's internal format. ...
266 # Not turning off the UTF-8 flag in the string will prevent the string
271 =head2 DecodeMIMEWordsToUTF8 $raw
273 An utility method which mimics MIME::Words::decode_mimewords, but only
274 limited functionality. This function returns an utf-8 string.
276 It returns the decoded string, or the original string if it's not
277 encoded. Since the subroutine converts specified string into utf-8
278 charset, it should not alter a subject written in English.
280 Why not use MIME::Words directly? Because it fails in RT when I
281 tried. Maybe it's ok now.
285 sub DecodeMIMEWordsToUTF8 {
287 return DecodeMIMEWordsToEncoding($str, 'utf-8', @_);
290 sub DecodeMIMEWordsToEncoding {
292 my $to_charset = _CanonicalizeCharset(shift);
293 my $field = shift || '';
295 my @list = $str =~ m/(.*?)=\?([^?]+)\?([QqBb])\?([^?]+)\?=([^=]*)/gcs;
298 # add everything that hasn't matched to the end of the latest
299 # string in array this happen when we have 'key="=?encoded?="; key="plain"'
300 $list[-1] .= substr($str, pos $str);
304 my ($prefix, $charset, $encoding, $enc_str, $trailing) =
306 $charset = _CanonicalizeCharset($charset);
307 $encoding = lc $encoding;
309 $trailing =~ s/\s?\t?$//; # Observed from Outlook Express
311 if ( $encoding eq 'q' ) {
312 use MIME::QuotedPrint;
313 $enc_str =~ tr/_/ /; # Observed from Outlook Express
314 $enc_str = decode_qp($enc_str);
315 } elsif ( $encoding eq 'b' ) {
317 $enc_str = decode_base64($enc_str);
319 $RT::Logger->warning("Incorrect encoding '$encoding' in '$str', "
320 ."only Q(uoted-printable) and B(ase64) are supported");
323 # now we have got a decoded subject, try to convert into the encoding
324 unless ( $charset eq $to_charset ) {
325 Encode::from_to( $enc_str, $charset, $to_charset );
328 # XXX TODO: RT doesn't currently do the right thing with mime-encoded headers
329 # We _should_ be preserving them encoded until after parsing is completed and
330 # THEN undo the mime-encoding.
332 # This routine should be translating the existing mimeencoding to utf8 but leaving
335 # It's legal for headers to contain mime-encoded commas and semicolons which
336 # should not be treated as address separators. (Encoding == quoting here)
338 # until this is fixed, we must escape any string containing a comma or semicolon
339 # this is only a bandaid
341 # Some _other_ MUAs encode quotes _already_, and double quotes
342 # confuse us a lot, so only quote it if it isn't quoted
344 $enc_str = qq{"$enc_str"}
345 if $enc_str =~ /[,;]/
346 and $enc_str !~ /^".*"$/
347 and (!$field || $field =~ /^(?:To$|From$|B?Cc$|Content-)/i);
349 $str .= $prefix . $enc_str . $trailing;
353 # handle filename*=ISO-8859-1''%74%E9%73%74%2E%74%78%74, see also rfc 2231
354 @list = $str =~ m/(.*?\*=)([^']*?)'([^']*?)'(\S+)(.*?)(?=(?:\*=|$))/gcs;
358 my ( $prefix, $charset, $language, $enc_str, $trailing ) =
360 $prefix =~ s/\*=$/=/; # remove the *
361 $charset = _CanonicalizeCharset($charset);
362 $enc_str =~ s/%(\w{2})/chr hex $1/eg;
363 unless ( $charset eq $to_charset ) {
364 Encode::from_to( $enc_str, $charset, $to_charset );
366 $enc_str = qq{"$enc_str"}
367 if $enc_str =~ /[,;]/
368 and $enc_str !~ /^".*"$/
369 and (!$field || $field =~ /^(?:To$|From$|B?Cc$|Content-)/i);
370 $str .= $prefix . $enc_str . $trailing;
374 # We might have \n without trailing whitespace, which will result in
383 =head2 _FindOrGuessCharset MIME::Entity, $head_only
385 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
387 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.
391 sub _FindOrGuessCharset {
393 my $head_only = shift;
394 my $head = $entity->head;
396 if ( my $charset = $head->mime_attr("content-type.charset") ) {
397 return _CanonicalizeCharset($charset);
400 if ( !$head_only and $head->mime_type =~ m{^text/} ) {
401 my $body = $entity->bodyhandle or return;
402 return _GuessCharset( $body->as_string );
406 # potentially binary data -- don't guess the body
407 return _GuessCharset( $head->as_string );
413 =head2 _GuessCharset STRING
415 use Encode::Guess to try to figure it out the string's encoding.
419 use constant HAS_ENCODE_GUESS => do { local $@; eval { require Encode::Guess; 1 } };
420 use constant HAS_ENCODE_DETECT => do { local $@; eval { require Encode::Detect::Detector; 1 } };
423 my $fallback = _CanonicalizeCharset('iso-8859-1');
425 # if $_[0] is null/empty, we don't guess its encoding
427 unless defined $_[0] && length $_[0];
429 my @encodings = RT->Config->Get('EmailInputEncodings');
430 unless ( @encodings ) {
431 $RT::Logger->warning("No EmailInputEncodings set, fallback to $fallback");
435 if ( $encodings[0] eq '*' ) {
437 if ( HAS_ENCODE_DETECT ) {
438 my $charset = Encode::Detect::Detector::detect( $_[0] );
440 $RT::Logger->debug("Encode::Detect::Detector guessed encoding: $charset");
441 return _CanonicalizeCharset( Encode::resolve_alias( $charset ) );
444 $RT::Logger->debug("Encode::Detect::Detector failed to guess encoding");
449 "You requested to guess encoding, but we couldn't"
450 ." load Encode::Detect::Detector module"
455 unless ( @encodings ) {
456 $RT::Logger->warning("No EmailInputEncodings set except '*', fallback to $fallback");
460 unless ( HAS_ENCODE_GUESS ) {
461 $RT::Logger->error("We couldn't load Encode::Guess module, fallback to $fallback");
465 Encode::Guess->set_suspects( @encodings );
466 my $decoder = Encode::Guess->guess( $_[0] );
467 unless ( defined $decoder ) {
468 $RT::Logger->warning("Encode::Guess failed: decoder is undefined; fallback to $fallback");
472 if ( ref $decoder ) {
473 my $charset = $decoder->name;
474 $RT::Logger->debug("Encode::Guess guessed encoding: $charset");
475 return _CanonicalizeCharset( $charset );
477 elsif ($decoder =~ /(\S+ or .+)/) {
478 my %matched = map { $_ => 1 } split(/ or /, $1);
479 return 'utf-8' if $matched{'utf8'}; # one and only normalization
481 foreach my $suspect (RT->Config->Get('EmailInputEncodings')) {
482 next unless $matched{$suspect};
483 $RT::Logger->debug("Encode::Guess ambiguous ($decoder); using $suspect");
484 return _CanonicalizeCharset( $suspect );
488 $RT::Logger->warning("Encode::Guess failed: $decoder; fallback to $fallback");
494 =head2 _CanonicalizeCharset NAME
496 canonicalize charset, return lowercase version.
497 special cases are: gb2312 => gbk, utf8 => utf-8
501 sub _CanonicalizeCharset {
502 my $charset = lc shift;
503 return $charset unless $charset;
505 # Canonicalize aliases if they're known
506 if (my $canonical = Encode::resolve_alias($charset)) {
507 $charset = $canonical;
510 if ( $charset eq 'utf8' || $charset eq 'utf-8-strict' ) {
513 elsif ( $charset eq 'euc-cn' ) {
514 # gbk is superset of gb2312/euc-cn so it's safe
516 # XXX TODO: gb18030 is an even larger, more permissive superset of gbk,
517 # but needs Encode::HanExtra installed
525 =head2 SetMIMEHeadToEncoding HEAD OLD_CHARSET NEW_CHARSET
527 Converts a MIME Head from one encoding to another. This totally violates the RFC.
528 We should never need this. But, Surprise!, MUAs are badly broken and do this kind of stuff
534 sub SetMIMEHeadToEncoding {
535 my ( $head, $charset, $enc, $preserve_words ) = ( shift, shift, shift, shift );
537 $charset = _CanonicalizeCharset($charset);
538 $enc = _CanonicalizeCharset($enc);
540 return if $charset eq $enc and $preserve_words;
542 foreach my $tag ( $head->tags ) {
543 next unless $tag; # seen in wild: headers with no name
544 my @values = $head->get_all($tag);
546 foreach my $value (@values) {
547 if ( $charset ne $enc ) {
548 Encode::_utf8_off($value);
549 Encode::from_to( $value, $charset => $enc );
551 $value = DecodeMIMEWordsToEncoding( $value, $enc, $tag )
552 unless $preserve_words;
554 # We intentionally add a leading space when re-adding the
555 # header; Mail::Header strips it before storing, but it
556 # serves to prevent it from "helpfully" canonicalizing
557 # $head->add("Subject", "Subject: foo") into the same as
558 # $head->add("Subject", "foo");
559 $head->add( $tag, " " . $value );
565 RT::Base->_ImportOverlays();