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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations) (download)
Sun Jun 16 10:44:08 2002 UTC (22 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.1: +2 -4 lines
2002-06-16  wakaba <w@suika.fam.cx>

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

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24