1 |
wakaba |
1.1 |
|
2 |
|
|
=head1 NAME
|
3 |
|
|
|
4 |
|
|
Message::MIME::Charset Perl module
|
5 |
|
|
|
6 |
|
|
=head1 DESCRIPTION
|
7 |
|
|
|
8 |
|
|
Perl module for MIME charset.
|
9 |
|
|
|
10 |
|
|
=cut
|
11 |
|
|
|
12 |
|
|
package Message::MIME::Charset;
|
13 |
|
|
use strict;
|
14 |
|
|
use vars qw(%ENCODER %DECODER %N11NTABLE %REG $VERSION);
|
15 |
wakaba |
1.6 |
$VERSION=do{my @r=(q$Revision: 1.5 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
|
16 |
wakaba |
1.1 |
|
17 |
wakaba |
1.4 |
our %CHARSET;
|
18 |
|
|
|
19 |
|
|
$CHARSET{'*DEFAULT'} = {
|
20 |
|
|
preferred_name => '',
|
21 |
|
|
|
22 |
|
|
encoder => sub { $_[1] },
|
23 |
|
|
decoder => sub { $_[1] },
|
24 |
|
|
|
25 |
|
|
mime_text => 1, ## Suitability in use as MIME text/* charset
|
26 |
|
|
#accept_cte => [qw/7bit .../],
|
27 |
|
|
cte_7bit_preferred => 'quoted-printable',
|
28 |
|
|
};
|
29 |
|
|
$CHARSET{'*default'} = $CHARSET{'*DEFAULT'};
|
30 |
|
|
|
31 |
|
|
$CHARSET{'us-ascii'} = {
|
32 |
|
|
preferred_name => 'us-ascii',
|
33 |
|
|
|
34 |
|
|
encoder => sub { $_[1] },
|
35 |
|
|
decoder => sub { $_[1] },
|
36 |
wakaba |
1.5 |
|
37 |
|
|
mime_text => 1,
|
38 |
wakaba |
1.4 |
};
|
39 |
|
|
|
40 |
|
|
$CHARSET{'iso-2022-int-1'} = {
|
41 |
|
|
preferred_name => 'iso-2022-int-1',
|
42 |
|
|
|
43 |
|
|
encoder => sub { $_[1] },
|
44 |
|
|
decoder => sub { $_[1] },
|
45 |
wakaba |
1.6 |
name_minimumizer => sub {
|
46 |
|
|
shift; my $s = shift;
|
47 |
|
|
return (charset => 'us-ascii') unless $s =~ /[\x1B\x0E\x0F]/;
|
48 |
|
|
return (charset => 'iso-2022-jp') unless $s =~ /\x1B[^\x24\x28]|\x1B\x24[^\x40B]|\x1B\x28[^BJ]|\x0E|\x0F/;
|
49 |
|
|
return (charset => 'iso-2022-jp-1') unless $s =~ /\x1B[^\x24\x28]|\x1B\x24[^\x40B\x28]|\x1B\x28[^BJ]|\x1B\x24\x28[^D]|\x0E|\x0F/;
|
50 |
|
|
return (charset => 'iso-2022-jp-3-plane1') unless $s =~ /\x1B[^\x24\x28]|\x1B\x24[^B\x28]|\x1B\x28[^B]|\x1B\x24\x28[^O]|\x0E|\x0F/;
|
51 |
|
|
return (charset => 'iso-2022-jp-3') unless $s =~ /\x1B[^\x24\x28]|\x1B\x24[^B\x28]|\x1B\x28[^B]|\x1B\x24\x28[^OP]|\x0E|\x0F/;
|
52 |
|
|
return (charset => 'iso-2022-kr') unless $s =~ /\x1B[^\x24]|\x1B\x24[^\x29]|\x1B\x24\x29C/;
|
53 |
|
|
return (charset => 'iso-2022-cn') unless $s =~ /\x1B[^\x24\x28]|\x1B\x24[^B]|\x1B\x28[^A]|\x1B\x24\x28[^GH]|\x0E|\x0F/;
|
54 |
|
|
(charset => 'iso-2022-int-1');
|
55 |
|
|
},
|
56 |
wakaba |
1.5 |
|
57 |
|
|
mime_text => 1,
|
58 |
wakaba |
1.4 |
};
|
59 |
|
|
|
60 |
|
|
$CHARSET{'unknown-8bit'} = {
|
61 |
|
|
preferred_name => 'unknown-8bit',
|
62 |
|
|
|
63 |
|
|
encoder => sub { $_[1] },
|
64 |
|
|
decoder => sub { $_[1] },
|
65 |
|
|
|
66 |
|
|
mime_text => 0,
|
67 |
wakaba |
1.6 |
cte_7bit_preferred => 'base64',
|
68 |
wakaba |
1.4 |
};
|
69 |
|
|
$CHARSET{'x-unknown'} = $CHARSET{'unknown-8bit'};
|
70 |
|
|
|
71 |
|
|
sub make_charset ($%) {
|
72 |
|
|
my $name = shift;
|
73 |
|
|
return unless $name; ## Note: charset "0" is not supported.
|
74 |
|
|
my %definition = @_;
|
75 |
wakaba |
1.6 |
|
76 |
|
|
$definition{preferred_name} ||= $name;
|
77 |
wakaba |
1.4 |
if ($definition{preferred_name} ne $name
|
78 |
|
|
&& ref $CHARSET{$definition{preferred_name}}) {
|
79 |
|
|
## New charset is an alias of defined charset,
|
80 |
|
|
$CHARSET{$name} = $CHARSET{$definition{preferred_name}};
|
81 |
|
|
return;
|
82 |
|
|
} elsif ($definition{alias_of} && ref $CHARSET{$definition{alias_of}}) {
|
83 |
|
|
## New charset is an alias of defined charset,
|
84 |
|
|
$CHARSET{$name} = $CHARSET{$definition{alias_of}};
|
85 |
|
|
return;
|
86 |
|
|
}
|
87 |
|
|
$CHARSET{$name} = \%definition;
|
88 |
|
|
|
89 |
|
|
## Set default values
|
90 |
wakaba |
1.6 |
#$definition{encoder} ||= sub { $_[1] };
|
91 |
|
|
#$definition{decoder} ||= sub { $_[1] };
|
92 |
wakaba |
1.4 |
|
93 |
|
|
$definition{mime_text} = 0 unless defined $definition{mime_text};
|
94 |
|
|
$definition{cte_7bit_preferred} = 'base64'
|
95 |
|
|
unless defined $definition{cte_7bit_preferred};
|
96 |
|
|
}
|
97 |
wakaba |
1.1 |
|
98 |
|
|
sub encode ($$) {
|
99 |
|
|
my ($charset, $s) = (lc shift, shift);
|
100 |
wakaba |
1.4 |
if (ref $CHARSET{$charset}->{encoder}) {
|
101 |
wakaba |
1.5 |
return (&{$CHARSET{$charset}->{encoder}} ($charset, $s), success => 1);
|
102 |
wakaba |
1.1 |
}
|
103 |
wakaba |
1.5 |
($s, success => 0);
|
104 |
wakaba |
1.1 |
}
|
105 |
|
|
|
106 |
|
|
sub decode ($$) {
|
107 |
|
|
my ($charset, $s) = (lc shift, shift);
|
108 |
wakaba |
1.4 |
if (ref $CHARSET{$charset}->{decoder}) {
|
109 |
|
|
return (&{$CHARSET{$charset}->{decoder}} ($charset, $s), 1);
|
110 |
wakaba |
1.1 |
}
|
111 |
wakaba |
1.2 |
($s, 0);
|
112 |
wakaba |
1.1 |
}
|
113 |
|
|
|
114 |
|
|
sub name_normalize ($) {
|
115 |
|
|
my $name = lc shift;
|
116 |
wakaba |
1.4 |
$CHARSET{$name}->{preferred_name} || $name;
|
117 |
wakaba |
1.1 |
}
|
118 |
|
|
|
119 |
wakaba |
1.6 |
sub name_minimumize ($$) {
|
120 |
|
|
my ($charset, $s) = (lc shift, shift);
|
121 |
|
|
if (ref $CHARSET{$charset}->{name_minimumizer}) {
|
122 |
|
|
return &{$CHARSET{$charset}->{name_minimumizer}} ($charset, $s);
|
123 |
|
|
}
|
124 |
|
|
$charset;
|
125 |
|
|
}
|
126 |
|
|
|
127 |
wakaba |
1.1 |
=head1 LICENSE
|
128 |
|
|
|
129 |
|
|
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
|
130 |
|
|
|
131 |
|
|
This program is free software; you can redistribute it and/or modify
|
132 |
|
|
it under the terms of the GNU General Public License as published by
|
133 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
134 |
|
|
(at your option) any later version.
|
135 |
|
|
|
136 |
|
|
This program is distributed in the hope that it will be useful,
|
137 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
138 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
139 |
|
|
GNU General Public License for more details.
|
140 |
|
|
|
141 |
|
|
You should have received a copy of the GNU General Public License
|
142 |
|
|
along with this program; see the file COPYING. If not, write to
|
143 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
144 |
|
|
Boston, MA 02111-1307, USA.
|
145 |
|
|
|
146 |
|
|
=head1 CHANGE
|
147 |
|
|
|
148 |
|
|
See F<ChangeLog>.
|
149 |
wakaba |
1.6 |
$Date: 2002/06/01 05:37:18 $
|
150 |
wakaba |
1.1 |
|
151 |
|
|
=cut
|
152 |
|
|
|
153 |
|
|
1;
|