/[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.5 - (hide annotations) (download)
Sat Jun 1 05:30:59 2002 UTC (22 years, 6 months ago) by wakaba
Branch: MAIN
Changes since 1.4: +116 -64 lines
2002-06-01  wakaba <w@suika.fam.cx>

	* TextPlain.pm: Reimplemented with parent class
	Message::Field::Structured.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24