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