1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2011 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
60 use Locale::Maketext 1.04;
61 use Locale::Maketext::Lexicon 0.25;
62 use base ('Locale::Maketext::Fuzzy');
68 # I decree that this project's first language is English.
71 'TEST_STRING' => 'Concrete Mixer',
73 '__Content-Type' => 'text/plain; charset=utf-8',
76 # That means that lookup failures can't happen -- if we get as far
77 # as looking for something in this lexicon, and we don't find it,
78 # then automagically set $Lexicon{$key} = $key, before possibly
81 # The exception is keys that start with "_" -- they aren't auto-makeable.
88 Initializes the lexicons used for localization.
96 my @lang = RT->Config->Get('LexiconLanguages');
97 @lang = ('*') unless @lang;
99 # load default functions
100 require substr(__FILE__, 0, -3) . '/i_default.pm';
102 # Load language-specific functions
103 foreach my $file ( File::Glob::bsd_glob(substr(__FILE__, 0, -3) . "/*.pm") ) {
104 unless ( $file =~ /^([-\w\s\.\/\\~:]+)$/ ) {
105 warn("$file is tainted. not loading");
110 my ($lang) = ($file =~ /([^\\\/]+?)\.pm$/);
111 next unless grep $_ eq '*' || $_ eq $lang, @lang;
116 foreach my $l ( @lang ) {
118 Gettext => (substr(__FILE__, 0, -3) . "/$l.po"),
119 Gettext => "$RT::LocalLexiconPath/*/$l.po",
120 Gettext => "$RT::LocalLexiconPath/$l.po",
122 push @{ $import{$l} }, map {(Gettext => "$_/$l.po")} RT->PluginDirs('po');
125 # Acquire all .po files and iterate them into lexicons
126 Locale::Maketext::Lexicon->import({ _decode => 1, %import });
134 foreach my $k (keys %{RT::I18N::} ) {
135 next if $k eq 'main::';
136 next unless index($k, '::', -2) >= 0;
137 next unless exists ${ 'RT::I18N::'. $k }{'Lexicon'};
139 my $lex = *{ ${'RT::I18N::'. $k }{'Lexicon'} }{HASH};
140 # run fetch to force load
141 my $tmp = $lex->{'foo'};
142 # XXX: untie may fail with "untie attempted
143 # while 1 inner references still exist"
144 # TODO: untie that has to lower fetch impact
145 # untie %$lex if tied %$lex;
151 Returns the encoding of the current lexicon, as yanked out of __ContentType's "charset" field.
152 If it can't find anything, it returns 'ISO-8859-1'
159 sub encoding { 'utf-8' }
161 # {{{ SetMIMEEntityToUTF8
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 # {{{ IsTextualContentType
178 =head2 IsTextualContentType $type
180 An utility function that determines whether $type is I<textual>, meaning
181 that it can sensibly be converted to Unicode text.
183 Currently, it returns true iff $type matches this regular expression
184 (case-insensitively):
186 ^(?:text/(?:plain|html)|message/rfc822)\b
192 sub IsTextualContentType {
194 ($type =~ m{^(?:text/(?:plain|html)|message/rfc822)\b}i) ? 1 : 0;
197 # {{{ SetMIMEEntityToEncoding
199 =head2 SetMIMEEntityToEncoding $entity, $encoding
201 An utility function which will try to convert entity body into specified
202 charset encoding (encoded as octets, *not* unicode-strings). It will
203 iterate all the entities in $entity, and try to convert each one into
204 specified charset if whose Content-Type is 'text/plain'.
206 This function doesn't return anything meaningful.
210 sub SetMIMEEntityToEncoding {
211 my ( $entity, $enc, $preserve_words ) = ( shift, shift, shift );
213 # do the same for parts first of all
214 SetMIMEEntityToEncoding( $_, $enc, $preserve_words ) foreach $entity->parts;
216 my $charset = _FindOrGuessCharset($entity) or return;
217 # one and only normalization
218 $charset = 'utf-8' if $charset =~ /^utf-?8$/i;
219 $enc = 'utf-8' if $enc =~ /^utf-?8$/i;
221 SetMIMEHeadToEncoding(
223 _FindOrGuessCharset($entity, 1) => $enc,
227 my $head = $entity->head;
229 # convert at least MIME word encoded attachment filename
230 foreach my $attr (qw(content-type.name content-disposition.filename)) {
231 if ( my $name = $head->mime_attr($attr) and !$preserve_words ) {
232 $head->mime_attr( $attr => DecodeMIMEWordsToUTF8($name) );
236 # If this is a textual entity, we'd need to preserve its original encoding
237 $head->replace( "X-RT-Original-Encoding" => $charset )
238 if $head->mime_attr('content-type.charset') or IsTextualContentType($head->mime_type);
240 return unless IsTextualContentType($head->mime_type);
242 my $body = $entity->bodyhandle;
244 if ( $enc ne $charset && $body ) {
245 my $string = $body->as_string or return;
247 # {{{ Convert the body
248 $RT::Logger->debug( "Converting '$charset' to '$enc' for " . $head->mime_type . " - " . ( $head->get('subject') || 'Subjectless message' ) );
250 # NOTE:: see the comments at the end of the sub.
251 Encode::_utf8_off( $string);
252 Encode::from_to( $string, $charset => $enc );
256 my $new_body = MIME::Body::InCore->new( $string);
258 # set up the new entity
259 $head->mime_attr( "content-type" => 'text/plain' )
260 unless ( $head->mime_attr("content-type") );
261 $head->mime_attr( "content-type.charset" => $enc );
262 $entity->bodyhandle($new_body);
266 # NOTES: Why Encode::_utf8_off before Encode::from_to
268 # All the strings in RT are utf-8 now. Quotes from Encode POD:
270 # [$length =] from_to($octets, FROM_ENC, TO_ENC [, CHECK])
271 # ... The data in $octets must be encoded as octets and not as
272 # characters in Perl's internal format. ...
274 # Not turning off the UTF-8 flag in the string will prevent the string
279 # {{{ DecodeMIMEWordsToUTF8
281 =head2 DecodeMIMEWordsToUTF8 $raw
283 An utility method which mimics MIME::Words::decode_mimewords, but only
284 limited functionality. This function returns an utf-8 string.
286 It returns the decoded string, or the original string if it's not
287 encoded. Since the subroutine converts specified string into utf-8
288 charset, it should not alter a subject written in English.
290 Why not use MIME::Words directly? Because it fails in RT when I
291 tried. Maybe it's ok now.
295 sub DecodeMIMEWordsToUTF8 {
297 return DecodeMIMEWordsToEncoding($str, 'utf-8', @_);
300 sub DecodeMIMEWordsToEncoding {
302 my $to_charset = shift;
303 my $field = shift || '';
305 my @list = $str =~ m/(.*?)=\?([^?]+)\?([QqBb])\?([^?]+)\?=([^=]*)/gcs;
306 return ($str) unless (@list);
308 # add everything that hasn't matched to the end of the latest
309 # string in array this happen when we have 'key="=?encoded?="; key="plain"'
310 $list[-1] .= substr($str, pos $str);
314 my ($prefix, $charset, $encoding, $enc_str, $trailing) =
316 $encoding = lc $encoding;
318 $trailing =~ s/\s?\t?$//; # Observed from Outlook Express
320 if ( $encoding eq 'q' ) {
321 use MIME::QuotedPrint;
322 $enc_str =~ tr/_/ /; # Observed from Outlook Express
323 $enc_str = decode_qp($enc_str);
324 } elsif ( $encoding eq 'b' ) {
326 $enc_str = decode_base64($enc_str);
328 $RT::Logger->warning("Incorrect encoding '$encoding' in '$str', "
329 ."only Q(uoted-printable) and B(ase64) are supported");
332 # now we have got a decoded subject, try to convert into the encoding
333 unless ( $charset eq $to_charset ) {
334 Encode::from_to( $enc_str, $charset, $to_charset );
337 # XXX TODO: RT doesn't currently do the right thing with mime-encoded headers
338 # We _should_ be preserving them encoded until after parsing is completed and
339 # THEN undo the mime-encoding.
341 # This routine should be translating the existing mimeencoding to utf8 but leaving
344 # It's legal for headers to contain mime-encoded commas and semicolons which
345 # should not be treated as address separators. (Encoding == quoting here)
347 # until this is fixed, we must escape any string containing a comma or semicolon
348 # this is only a bandaid
350 # Some _other_ MUAs encode quotes _already_, and double quotes
351 # confuse us a lot, so only quote it if it isn't quoted
353 $enc_str = qq{"$enc_str"}
354 if $enc_str =~ /[,;]/
355 and $enc_str !~ /^".*"$/
356 and (!$field || $field =~ /^(?:To$|From$|B?Cc$|Content-)/i);
358 $str .= $prefix . $enc_str . $trailing;
361 # We might have \n without trailing whitespace, which will result in
370 # {{{ _FindOrGuessCharset
372 =head2 _FindOrGuessCharset MIME::Entity, $head_only
374 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
376 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.
380 sub _FindOrGuessCharset {
382 my $head_only = shift;
383 my $head = $entity->head;
385 if ( my $charset = $head->mime_attr("content-type.charset") ) {
389 if ( !$head_only and $head->mime_type =~ m{^text/}) {
390 my $body = $entity->bodyhandle or return;
391 return _GuessCharset( $body->as_string );
394 # potentially binary data -- don't guess the body
395 return _GuessCharset( $head->as_string );
403 =head2 _GuessCharset STRING
405 use Encode::Guess to try to figure it out the string's encoding.
410 my $fallback = 'iso-8859-1';
412 # if $_[0] is null/empty, we don't guess its encoding
413 return $fallback unless defined $_[0] && length $_[0];
416 my @encodings = RT->Config->Get('EmailInputEncodings');
417 if ( @encodings and eval { require Encode::Guess; 1 } ) {
418 Encode::Guess->set_suspects( @encodings );
419 my $decoder = Encode::Guess->guess( $_[0] );
421 if ( defined($decoder) ) {
422 if ( ref $decoder ) {
423 $charset = $decoder->name;
424 $RT::Logger->debug("Guessed encoding: $charset");
427 elsif ($decoder =~ /(\S+ or .+)/) {
428 my %matched = map { $_ => 1 } split(/ or /, $1);
429 return 'utf-8' if $matched{'utf8'}; # one and only normalization
431 foreach my $suspect (RT->Config->Get('EmailInputEncodings')) {
432 next unless $matched{$suspect};
433 $RT::Logger->debug("Encode::Guess ambiguous ($decoder); using $suspect");
439 $RT::Logger->warning("Encode::Guess failed: $decoder; fallback to $fallback");
443 $RT::Logger->warning("Encode::Guess failed: decoder is undefined; fallback to $fallback");
446 elsif ( @encodings && $@ ) {
447 $RT::Logger->error("You have set EmailInputEncodings, but we couldn't load Encode::Guess: $@");
449 $RT::Logger->warning("No EmailInputEncodings set, fallback to $fallback");
452 return ($charset || $fallback);
457 # {{{ SetMIMEHeadToEncoding
459 =head2 SetMIMEHeadToEncoding HEAD OLD_CHARSET NEW_CHARSET
461 Converts a MIME Head from one encoding to another. This totally violates the RFC.
462 We should never need this. But, Surprise!, MUAs are badly broken and do this kind of stuff
468 sub SetMIMEHeadToEncoding {
469 my ( $head, $charset, $enc, $preserve_words ) = ( shift, shift, shift, shift );
471 $charset = 'utf-8' if $charset eq 'utf8';
472 $enc = 'utf-8' if $enc eq 'utf8';
474 return if $charset eq $enc and $preserve_words;
476 foreach my $tag ( $head->tags ) {
477 next unless $tag; # seen in wild: headers with no name
478 my @values = $head->get_all($tag);
480 foreach my $value (@values) {
481 if ( $charset ne $enc ) {
483 Encode::_utf8_off($value);
484 Encode::from_to( $value, $charset => $enc );
486 $value = DecodeMIMEWordsToEncoding( $value, $enc, $tag )
487 unless $preserve_words;
488 $head->add( $tag, $value );
495 RT::Base->_ImportOverlays();