| 1 |
wakaba |
1.1 |
|
| 2 |
|
|
=head1 NAME |
| 3 |
|
|
|
| 4 |
|
|
Message::Body::Text --- Perl Module for Internet Media Types "text/*" |
| 5 |
|
|
|
| 6 |
|
|
=cut |
| 7 |
|
|
|
| 8 |
|
|
package Message::Body::Text; |
| 9 |
|
|
use strict; |
| 10 |
|
|
use vars qw(%DEFAULT @ISA %REG $VERSION); |
| 11 |
wakaba |
1.4 |
$VERSION=do{my @r=(q$Revision: 1.3 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
| 12 |
wakaba |
1.1 |
|
| 13 |
|
|
require Message::Field::Structured; |
| 14 |
|
|
push @ISA, qw(Message::Field::Structured); |
| 15 |
|
|
require Message::Header; |
| 16 |
|
|
require Message::MIME::Charset; |
| 17 |
|
|
use overload '""' => sub { $_[0]->stringify }, |
| 18 |
|
|
fallback => 1; |
| 19 |
|
|
%REG = %Message::Util::REG; |
| 20 |
|
|
|
| 21 |
|
|
%DEFAULT = ( |
| 22 |
|
|
-_METHODS => [qw|value|], |
| 23 |
|
|
-_MEMBERS => [qw|_charset|], |
| 24 |
|
|
## header -- Don't clone |
| 25 |
wakaba |
1.4 |
-body_default_charset => 'us-ascii', |
| 26 |
|
|
-body_default_charset_input => 'iso-2022-int-1', |
| 27 |
wakaba |
1.1 |
-hook_encode_string => \&Message::Util::encode_body_string, |
| 28 |
|
|
-hook_decode_string => \&Message::Util::decode_body_string, |
| 29 |
|
|
-media_type => 'text', |
| 30 |
|
|
-media_subtype => 'plain', |
| 31 |
|
|
-parse_all => 0, |
| 32 |
|
|
-use_normalization => 0, |
| 33 |
|
|
-use_param_charset => 0, |
| 34 |
|
|
); |
| 35 |
|
|
|
| 36 |
|
|
=head1 CONSTRUCTORS |
| 37 |
|
|
|
| 38 |
|
|
The following methods construct new C<Message::Field::Structured> objects: |
| 39 |
|
|
|
| 40 |
|
|
=over 4 |
| 41 |
|
|
|
| 42 |
|
|
=cut |
| 43 |
|
|
|
| 44 |
|
|
## Initialize of this class -- called by constructors |
| 45 |
|
|
sub _init ($;%) { |
| 46 |
|
|
my $self = shift; |
| 47 |
|
|
my $DEFAULT = Message::Util::make_clone (\%DEFAULT); |
| 48 |
|
|
my %option = @_; |
| 49 |
|
|
$self->SUPER::_init (%$DEFAULT, %option); |
| 50 |
|
|
|
| 51 |
|
|
if (ref $option{entity_header}) { |
| 52 |
|
|
$self->{header} = $option{entity_header}; |
| 53 |
|
|
} |
| 54 |
|
|
my $mt = $self->{option}->{media_type}; |
| 55 |
|
|
my $mst = $self->{option}->{media_subtype}; |
| 56 |
|
|
my $mt_def = $Message::MIME::MediaType::type{$mt}->{$mst}; |
| 57 |
|
|
$mt_def = $Message::MIME::MediaType::type{$mt}->{'/default'} unless ref $mt_def; |
| 58 |
|
|
$mt_def = $Message::MIME::MediaType::type{'/default'}->{'/default'} |
| 59 |
|
|
unless ref $mt_def; |
| 60 |
|
|
if ($self->{option}->{format} =~ /http/) { |
| 61 |
|
|
$self->{option}->{use_normalization} = 0; |
| 62 |
|
|
} else { |
| 63 |
|
|
$self->{option}->{use_normalization} = 1; |
| 64 |
|
|
} |
| 65 |
|
|
if ($mt_def->{mime_charset}) { |
| 66 |
|
|
$self->{option}->{use_param_charset} = 1; |
| 67 |
|
|
if ($self->{option}->{format} =~ /http/) { |
| 68 |
wakaba |
1.4 |
$self->{option}->{body_default_charset} = 'iso-8859-1'; |
| 69 |
|
|
$self->{option}->{body_default_charset_input} = 'iso-8859-1'; |
| 70 |
wakaba |
1.3 |
} elsif ($self->{option}->{format} =~ /news-usefor|sip/) { |
| 71 |
wakaba |
1.4 |
$self->{option}->{body_default_charset} = 'utf-8'; |
| 72 |
|
|
$self->{option}->{body_default_charset_input} = 'utf-8'; |
| 73 |
wakaba |
1.1 |
} else { |
| 74 |
wakaba |
1.4 |
$self->{option}->{body_default_charset} = 'us-ascii'; |
| 75 |
|
|
$self->{option}->{body_default_charset_input} = 'iso-2022-int-1'; |
| 76 |
wakaba |
1.1 |
} |
| 77 |
|
|
} |
| 78 |
|
|
if ($mt_def->{default_charset}) { |
| 79 |
wakaba |
1.4 |
$self->{option}->{body_default_charset} = $mt_def->{default_charset}; |
| 80 |
wakaba |
1.1 |
} |
| 81 |
|
|
} |
| 82 |
|
|
|
| 83 |
|
|
=item $body = Message::Body::TextPlain->new ([%options]) |
| 84 |
|
|
|
| 85 |
|
|
Constructs a new object. You might pass some options as parameters |
| 86 |
|
|
to the constructor. |
| 87 |
|
|
|
| 88 |
|
|
=cut |
| 89 |
|
|
|
| 90 |
|
|
## Inherited |
| 91 |
|
|
|
| 92 |
|
|
=item $body = Message::Body::TextPlain->parse ($body, [%options]) |
| 93 |
|
|
|
| 94 |
|
|
Constructs a new object with given field body. You might pass |
| 95 |
|
|
some options as parameters to the constructor. |
| 96 |
|
|
|
| 97 |
|
|
=cut |
| 98 |
|
|
|
| 99 |
|
|
sub parse ($$;%) { |
| 100 |
|
|
my $class = shift; |
| 101 |
|
|
my $self = bless {}, $class; |
| 102 |
|
|
my $body = shift; |
| 103 |
|
|
$self->_init (@_); |
| 104 |
|
|
$self->_parse ($body); |
| 105 |
|
|
$self; |
| 106 |
|
|
} |
| 107 |
|
|
|
| 108 |
|
|
sub _parse ($$) { |
| 109 |
|
|
my $self = shift; |
| 110 |
|
|
my $body = shift; |
| 111 |
|
|
my $charset; |
| 112 |
wakaba |
1.4 |
if ($self->{option}->{use_param_charset}) { |
| 113 |
|
|
my $ct; |
| 114 |
|
|
$ct = $self->{header}->field ('content-type', -new_item_unless_exist => 0) |
| 115 |
|
|
if ref $self->{header}; |
| 116 |
|
|
$charset = $ct->parameter ('charset') if ref $ct; |
| 117 |
|
|
} |
| 118 |
|
|
unless ($charset) { |
| 119 |
|
|
$charset = $self->{option}->{encoding_before_decode}; |
| 120 |
|
|
} |
| 121 |
wakaba |
1.1 |
my %s = &{$self->{option}->{hook_decode_string}} ($self, $body, |
| 122 |
|
|
type => 'body', charset => $charset); |
| 123 |
|
|
$self->{value} = $s{value}; |
| 124 |
wakaba |
1.4 |
$self->{_charset} = $s{charset}; ## In case convertion failed |
| 125 |
wakaba |
1.1 |
} |
| 126 |
|
|
|
| 127 |
|
|
=back |
| 128 |
|
|
|
| 129 |
|
|
=cut |
| 130 |
|
|
|
| 131 |
|
|
=item $body->header ([$new_header]) |
| 132 |
|
|
|
| 133 |
|
|
|
| 134 |
|
|
=cut |
| 135 |
|
|
|
| 136 |
|
|
sub entity_header ($;$) { |
| 137 |
|
|
my $self = shift; |
| 138 |
|
|
my $new_header = shift; |
| 139 |
|
|
if (ref $new_header) { |
| 140 |
|
|
$self->{header} = $new_header; |
| 141 |
|
|
} |
| 142 |
|
|
$self->{header}; |
| 143 |
|
|
} |
| 144 |
|
|
|
| 145 |
|
|
=item $body->value ([$new_body]) |
| 146 |
|
|
|
| 147 |
|
|
Returns C<body> as string unless $new_body. |
| 148 |
|
|
Set $new_body instead of current C<body>. |
| 149 |
|
|
|
| 150 |
|
|
=cut |
| 151 |
|
|
|
| 152 |
|
|
sub value ($;$) { |
| 153 |
|
|
my $self = shift; |
| 154 |
|
|
my $new_body = shift; |
| 155 |
|
|
if ($new_body) { |
| 156 |
|
|
$self->{value} = $new_body; |
| 157 |
|
|
} |
| 158 |
|
|
$self->{value}; |
| 159 |
|
|
} |
| 160 |
|
|
|
| 161 |
|
|
=head2 $self->stringify ([%option]) |
| 162 |
|
|
|
| 163 |
|
|
Returns the C<body> as a string. |
| 164 |
|
|
|
| 165 |
|
|
=cut |
| 166 |
|
|
|
| 167 |
|
|
sub stringify ($;%) { |
| 168 |
|
|
my $self = shift; |
| 169 |
|
|
my %o = @_; my %option = %{$self->{option}}; |
| 170 |
|
|
for (grep {/^-/} keys %o) {$option{substr ($_, 1)} = $o{$_}} |
| 171 |
|
|
my $v = $self->_prep_stringify ($self->{value}, \%option); |
| 172 |
|
|
my $ct = $self->{header}->field ('content-type', -new_item_unless_exist => 0) |
| 173 |
|
|
if ref $self->{header}; |
| 174 |
|
|
unless ($option{use_param_charset}) { |
| 175 |
|
|
if ($option{use_normalization}) { |
| 176 |
|
|
$v =~ s/\x0D(?!\x0A)/\x0D\x0A/gs; |
| 177 |
|
|
$v =~ s/(?<!\x0D)\x0A/\x0D\x0A/gs; |
| 178 |
|
|
#$v .= "\x0D\x0A" unless $v =~ /\x0D\x0A$/s; |
| 179 |
|
|
} |
| 180 |
|
|
return $v; |
| 181 |
|
|
} |
| 182 |
|
|
my %e; |
| 183 |
|
|
unless ($self->{_charset}) { |
| 184 |
wakaba |
1.4 |
my $charset; |
| 185 |
|
|
if ($option{use_param_charset}) { |
| 186 |
|
|
$charset = $ct->parameter ('charset') if ref $ct; |
| 187 |
|
|
} |
| 188 |
|
|
$charset ||= $option{encoding_after_encode}; |
| 189 |
wakaba |
1.1 |
(%e) = &{$option{hook_encode_string}} ($self, $v, |
| 190 |
|
|
type => 'body', charset => $charset); |
| 191 |
|
|
## Normalize |
| 192 |
|
|
if ($option{use_normalization}) { |
| 193 |
wakaba |
1.4 |
if ($Message::MIME::Charset::CHARSET{ $charset }->{mime_text}) { |
| 194 |
wakaba |
1.1 |
$e{value} =~ s/\x0D(?!\x0A)/\x0D\x0A/gs; |
| 195 |
|
|
$e{value} =~ s/(?<!\x0D)\x0A/\x0D\x0A/gs; |
| 196 |
|
|
#$e{value} .= "\x0D\x0A" unless $e{value} =~ /\x0D\x0A$/s; |
| 197 |
|
|
} |
| 198 |
|
|
} |
| 199 |
|
|
} else { ## if $self->{_charset}, |
| 200 |
|
|
%e = (value => $v, charset => $self->{_charset}); |
| 201 |
|
|
} |
| 202 |
|
|
if (ref $self->{header}) { |
| 203 |
|
|
unless (ref $ct) { |
| 204 |
|
|
$ct = $self->{header}->field ('content-type'); |
| 205 |
|
|
$ct->value ($option{parent_type}); |
| 206 |
|
|
} |
| 207 |
wakaba |
1.4 |
if ($e{charset}) { |
| 208 |
|
|
$ct->replace (charset => $e{charset}); |
| 209 |
|
|
} else { |
| 210 |
|
|
$ct->replace (Message::MIME::Charset::name_minimumize |
| 211 |
|
|
($option{body_default_charset}, $e{value})); |
| 212 |
|
|
} |
| 213 |
wakaba |
1.1 |
} |
| 214 |
|
|
$e{value}; |
| 215 |
|
|
} |
| 216 |
|
|
*as_string = \&stringify; |
| 217 |
|
|
|
| 218 |
|
|
## $self->_prep_stringify ($value, \%option) |
| 219 |
|
|
sub _prep_stringify ($$\%) { |
| 220 |
|
|
my $self = shift; |
| 221 |
|
|
shift; |
| 222 |
|
|
} |
| 223 |
|
|
|
| 224 |
|
|
## Inherited: option, clone |
| 225 |
|
|
|
| 226 |
|
|
=head1 SEE ALSO |
| 227 |
|
|
|
| 228 |
|
|
RFC 822 <urn:ietf:rfc:822>, |
| 229 |
|
|
RFC 2046 <urn:ietf:rfc:2046>, RFC 2646 <urn:ietf:rfc:2646>. |
| 230 |
|
|
|
| 231 |
|
|
=head1 LICENSE |
| 232 |
|
|
|
| 233 |
|
|
Copyright 2002 wakaba E<lt>[email protected]<gt>. |
| 234 |
|
|
|
| 235 |
|
|
This program is free software; you can redistribute it and/or modify |
| 236 |
|
|
it under the terms of the GNU General Public License as published by |
| 237 |
|
|
the Free Software Foundation; either version 2 of the License, or |
| 238 |
|
|
(at your option) any later version. |
| 239 |
|
|
|
| 240 |
|
|
This program is distributed in the hope that it will be useful, |
| 241 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 242 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 243 |
|
|
GNU General Public License for more details. |
| 244 |
|
|
|
| 245 |
|
|
You should have received a copy of the GNU General Public License |
| 246 |
|
|
along with this program; see the file COPYING. If not, write to |
| 247 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 248 |
|
|
Boston, MA 02111-1307, USA. |
| 249 |
|
|
|
| 250 |
|
|
=head1 CHANGE |
| 251 |
|
|
|
| 252 |
|
|
See F<ChangeLog>. |
| 253 |
wakaba |
1.4 |
$Date: 2002/07/03 23:39:15 $ |
| 254 |
wakaba |
1.1 |
|
| 255 |
|
|
=cut |
| 256 |
|
|
|
| 257 |
|
|
1; |