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