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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24