/[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.11 - (show annotations) (download)
Fri Jul 19 11:49:23 2002 UTC (22 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.10: +5 -4 lines
2002-07-19  Wakaba <w@suika.fam.cx>

	* TextMessageRFC934.pm: New module.

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.10 $=~/\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 #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} =~ /rfc2822|news-usefor|http|mime|news-son-of-rfc1036/;
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 (Message::MIME::Charset::name_minimumize ($option{body_default_charset}, $e{value}));
152 } elsif ($option{fill_ct}) {
153 $ct = $self->{header}->field ('content-type');
154 $ct->media_type_major ($option{media_type});
155 $ct->media_type_minor ($option{media_subtype});
156 $ct->replace (Message::MIME::Charset::name_minimumize ($option{body_default_charset}, $e{value}));
157 }
158 }
159 $e{value};
160 }
161 *as_string = \&stringify;
162
163 ## Inherited: option, clone
164
165 =head1 SEE ALSO
166
167 RFC 822 <urn:ietf:rfc:822>,
168 RFC 2046 <urn:ietf:rfc:2046>, RFC 2646 <urn:ietf:rfc:2646>.
169
170 =head1 LICENSE
171
172 Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
173
174 This program is free software; you can redistribute it and/or modify
175 it under the terms of the GNU General Public License as published by
176 the Free Software Foundation; either version 2 of the License, or
177 (at your option) any later version.
178
179 This program is distributed in the hope that it will be useful,
180 but WITHOUT ANY WARRANTY; without even the implied warranty of
181 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
182 GNU General Public License for more details.
183
184 You should have received a copy of the GNU General Public License
185 along with this program; see the file COPYING. If not, write to
186 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
187 Boston, MA 02111-1307, USA.
188
189 =head1 CHANGE
190
191 See F<ChangeLog>.
192 $Date: 2002/07/17 00:33:29 $
193
194 =cut
195
196 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24