/[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.6 - (hide annotations) (download)
Sun Jul 21 03:23:50 2002 UTC (22 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.5: +14 -3 lines
2002-07-20  Wakaba <w@suika.fam.cx>

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

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24