/[suikacvs]/messaging/manakai/lib/Message/Body/TextPlain.pm
Suika

Contents of /messaging/manakai/lib/Message/Body/TextPlain.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations) (download)
Sun Jul 21 03:23:50 2002 UTC (22 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.11: +15 -6 lines
2002-07-20  Wakaba <w@suika.fam.cx>

	* TextMessageRFC1153.pm: New module.
	* Text.pm, TextPlain.pm (check_msmime): New option.

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.11 $=~/\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;
97 }
98
99 =back
100
101 =cut
102
103 =item $body->header ([$new_header])
104
105
106 =cut
107
108 ## Inherited
109
110 =item $body->value ([$new_body])
111
112 Returns C<body> as string unless $new_body.
113 Set $new_body instead of current C<body>.
114
115 =cut
116
117 ## Inherited
118
119 =head2 $self->stringify ([%option])
120
121 Returns the C<body> as a string.
122
123 =cut
124
125 sub stringify ($;%) {
126 my $self = shift;
127 my %o = @_; my %option = %{$self->{option}};
128 for (grep {/^-/} keys %o) {$option{substr ($_, 1)} = $o{$_}}
129 my $ct = $self->{header}->field ('content-type', -new_item_unless_exist => 0)
130 if ref $self->{header};
131 my %e;
132 unless ($self->{_charset}) {
133 my $charset;
134 $charset = $ct->parameter ('*charset-to-be', -new_item_unless_exist => 0) if ref $ct;
135 $charset = $ct->parameter ('charset', -new_item_unless_exist => 0) if !$charset && ref $ct;
136 $charset ||= $self->{option}->{encoding_after_encode};
137 (%e) = &{$self->{option}->{hook_encode_string}} ($self,
138 $self->{value}, type => 'body',
139 charset => $charset);
140 $e{charset} ||= $self->{option}->{internal_charset_name} if $e{failed};
141 ## Normalize
142 if ($option{use_normalization}) {
143 if ($Message::MIME::Charset::CHARSET{$charset || '*default'}->{mime_text}) {
144 $e{value} =~ s/\x0D(?!\x0A)/\x0D\x0A/gs;
145 $e{value} =~ s/(?<!\x0D)\x0A/\x0D\x0A/gs;
146 #$e{value} .= "\x0D\x0A" unless $e{value} =~ /\x0D\x0A$/s;
147 }
148 }
149 } else {
150 %e = (value => $self->{value}, charset => $self->{_charset});
151 }
152 if (ref $self->{header}) {
153 if ($e{charset}) {
154 unless (ref $ct) {
155 $ct = $self->{header}->field ('content-type');
156 $ct->value ($option{media_type}.'/'.$option{media_subtype});
157 }
158 $ct->replace (charset => $e{charset});
159 } elsif (ref $ct) {
160 $ct->replace (Message::MIME::Charset::name_minimumize ($option{body_default_charset}, $e{value}));
161 } elsif ($option{fill_ct}) {
162 $ct = $self->{header}->field ('content-type');
163 $ct->media_type_major ($option{media_type});
164 $ct->media_type_minor ($option{media_subtype});
165 $ct->replace (Message::MIME::Charset::name_minimumize ($option{body_default_charset}, $e{value}));
166 }
167 }
168 $e{value};
169 }
170 *as_string = \&stringify;
171
172 ## Inherited: option, clone
173
174 =head1 SEE ALSO
175
176 RFC 822 <urn:ietf:rfc:822>,
177 RFC 2046 <urn:ietf:rfc:2046>, RFC 2646 <urn:ietf:rfc:2646>.
178
179 =head1 LICENSE
180
181 Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
182
183 This program is free software; you can redistribute it and/or modify
184 it under the terms of the GNU General Public License as published by
185 the Free Software Foundation; either version 2 of the License, or
186 (at your option) any later version.
187
188 This program is distributed in the hope that it will be useful,
189 but WITHOUT ANY WARRANTY; without even the implied warranty of
190 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
191 GNU General Public License for more details.
192
193 You should have received a copy of the GNU General Public License
194 along with this program; see the file COPYING. If not, write to
195 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
196 Boston, MA 02111-1307, USA.
197
198 =head1 CHANGE
199
200 See F<ChangeLog>.
201 $Date: 2002/07/19 11:49:23 $
202
203 =cut
204
205 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24