/[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.7 - (show annotations) (download)
Sun Jun 16 10:44:08 2002 UTC (22 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.6: +7 -2 lines
2002-06-16  wakaba <w@suika.fam.cx>

	* Text.pm, TextPlain.pm (_init): Bug fix of normalization
	option (was not worked).

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24