/[suikacvs]/messaging/manakai/lib/Message/Entity.pm
Suika

Contents of /messaging/manakai/lib/Message/Entity.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.22 - (hide annotations) (download)
Sun Jun 23 12:20:11 2002 UTC (24 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.21: +15 -5 lines
2002-06-23  Wakaba <w@suika.fam.cx>

	* Entity.pm (header_default_charset, header_default_charset_input):
	New options.
	* Header.pm (ditto): Likewise.
	* Util.pm
	- (angle_quoted, angle_qcontent, M_angle_quoted): New regexs
	(Moved from Message::Field::AngleQuoted).
	- (decode_ccontent): Order of arguments of 
	Message::MIME::EncodedWord::decode_ccontent is changed.
	- (unquote_if_angle_quoted): New function.

1 wakaba 1.1
2     =head1 NAME
3    
4     Message::Entity Perl module
5    
6     =head1 DESCRIPTION
7    
8     Perl module for RFC 822/2822 C<message>.
9     MIME multipart will be also supported (but not implemented yet).
10    
11     =cut
12    
13     package Message::Entity;
14     use strict;
15 wakaba 1.15 use vars qw(%DEFAULT $VERSION);
16 wakaba 1.22 $VERSION=do{my @r=(q$Revision: 1.21 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
17 wakaba 1.1
18 wakaba 1.15 require Message::Util;
19 wakaba 1.8 require Message::Header;
20 wakaba 1.15 require Message::MIME::MediaType;
21     require Message::MIME::Encoding;
22 wakaba 1.9 use overload '""' => sub { $_[0]->stringify },
23     fallback => 1;
24 wakaba 1.1
25 wakaba 1.14 ## Initialize of this class -- called by constructors
26     %DEFAULT = (
27 wakaba 1.15 -_METHODS => [qw|header body content_type id|],
28 wakaba 1.17 -_MEMBERS => [qw|header body _cte|],
29 wakaba 1.18 ## entity_header -- Don't clone.
30 wakaba 1.15 -accept_coderange => '7bit', ## 7bit / 8bit / binary
31 wakaba 1.18 #add_ua => 1,
32 wakaba 1.15 -body_default_charset => 'iso-2022-int-1',
33 wakaba 1.18 -body_default_charset_input => 'iso-2022-int-1',
34     -body_default_media_type => 'text',
35     -body_default_media_subtype => 'plain',
36 wakaba 1.17 -cte_default => '7bit',
37 wakaba 1.8 #fill_date => 1,
38 wakaba 1.15 -fill_date_name => 'date',
39 wakaba 1.8 #fill_msgid => 1,
40 wakaba 1.15 -fill_msgid_name => 'message-id',
41 wakaba 1.19 -force_mime_entity => 0,
42 wakaba 1.15 -format => 'mail-rfc2822',
43 wakaba 1.22 -header_default_charset => 'iso-2022-int-1',
44     -header_default_charset_input => 'iso-2022-int-1',
45 wakaba 1.15 -linebreak_strict => 0, ## BUG: not work perfectly
46     -parse_all => 0,
47 wakaba 1.17 -text_coderange => 'binary',
48     ## '8bit' (MIME text/*) / 'binary' (HTTP text/*)
49 wakaba 1.8 #ua_field_name => 'user-agent',
50 wakaba 1.21 -ua_use_Config => 1,
51     -ua_use_Win32 => 1,
52 wakaba 1.15 -uri_mailto_safe_level => 4,
53 wakaba 1.14 );
54     sub _init ($;%) {
55     my $self = shift;
56     my %options = @_;
57 wakaba 1.15 $self->{option} = {};
58     my $o = Message::Util::make_clone (\%DEFAULT);
59     for my $name (keys %$o) {
60     if (substr ($name, 0, 1) eq '-') {
61     $self->{option}->{substr ($name, 1)} = $$o{$name};
62     }
63     }
64 wakaba 1.14
65 wakaba 1.8 my @new_fields = ();
66     for my $name (keys %options) {
67     if (substr ($name, 0, 1) eq '-') {
68     $self->{option}->{substr ($name, 1)} = $options{$name};
69 wakaba 1.18 } elsif ($name eq 'entity_header') {
70     $self->{entity_header} = $options{entity_header};
71 wakaba 1.8 } else {
72 wakaba 1.18 push @new_fields, ($name => $options{$name});
73 wakaba 1.8 }
74     }
75 wakaba 1.18
76 wakaba 1.8 my $format = $self->{option}->{format};
77 wakaba 1.14 if ($format =~ /http/) {
78 wakaba 1.15 $self->{option}->{fill_date_ns} = $Message::Header::NS_phname2uri{http};
79     $self->{option}->{fill_msgid_from_ns} = $Message::Header::NS_phname2uri{http};
80     $self->{option}->{fill_ua_ns} = $Message::Header::NS_phname2uri{http};
81     $self->{option}->{accept_coderange} = 'binary';
82 wakaba 1.17 $self->{option}->{text_coderange} = 'binary';
83     $self->{option}->{cte_default} = 'binary';
84 wakaba 1.14 } else {
85 wakaba 1.15 $self->{option}->{fill_date_ns} = $Message::Header::NS_phname2uri{rfc822};
86     $self->{option}->{fill_msgid_from_ns} = $Message::Header::NS_phname2uri{rfc822};
87     $self->{option}->{fill_ua_ns} = $Message::Header::NS_phname2uri{rfc822};
88 wakaba 1.17 $self->{option}->{text_coderange} = '8bit';
89 wakaba 1.15 if ($format =~ /news-usefor|smtp-8bitmime/) {
90     $self->{option}->{accept_coderange} = '8bit';
91 wakaba 1.18 #$self->{option}->{cte_default} = '8bit';
92 wakaba 1.15 } else {
93     $self->{option}->{accept_coderange} = '7bit';
94     }
95 wakaba 1.14 }
96 wakaba 1.15 $self->{option}->{fill_msgid_ns} = $Message::Header::NS_phname2uri{rfc822};
97     $self->{option}->{fill_mimever_ns} = $Message::Header::NS_phname2uri{rfc822};
98 wakaba 1.8 unless (defined $self->{option}->{fill_date}) {
99 wakaba 1.18 $self->{option}->{fill_date} = $format !~ /mime-entity|cgi|uri-url-mailto/;
100 wakaba 1.8 }
101     unless (defined $self->{option}->{fill_msgid}) {
102 wakaba 1.18 $self->{option}->{fill_msgid} = $format !~ /mime-entity|http|uri-url-mailto/;
103 wakaba 1.8 }
104 wakaba 1.15 unless (defined $self->{option}->{fill_ct}) {
105     $self->{option}->{fill_ct} = $format !~ /http/;
106     }
107 wakaba 1.8 unless (defined $self->{option}->{fill_mimever}) {
108 wakaba 1.18 $self->{option}->{fill_mimever} = $format !~ /http|mime-entity/;
109     }
110     unless (defined $self->{option}->{add_ua}) {
111     $self->{option}->{add_ua} = $format !~ /mime-entity/;
112 wakaba 1.8 }
113 wakaba 1.22 unless ($self->{option}->{fill_ua_name}) {
114 wakaba 1.14 $self->{option}->{fill_ua_name} = $format =~ /response|cgi|uri-url-mailto/?
115 wakaba 1.8 'server': 'user-agent';
116     }
117     @new_fields;
118     }
119    
120     =head1 CONSTRUCTORS
121    
122     The following methods construct new C<Message::Entity> objects:
123 wakaba 1.3
124 wakaba 1.8 =over 4
125    
126 wakaba 1.9 =item Message::Entity->new ([%initial-fields/options])
127 wakaba 1.1
128 wakaba 1.9 Constructs a new C<Message::Entity> object. You might pass some initial
129     C<field-name>-C<field-body> pairs and/or options as parameters to the constructor.
130    
131     Example:
132    
133     $msg = new Message::Entity
134     Date => 'Thu, 03 Feb 1994 00:00:00 +0000',
135     Content_Type => 'text/html',
136     X_URI => '<http://www.foo.example/>',
137     -format => 'mail-rfc2822' ## not to be header field
138     ;
139 wakaba 1.1
140     =cut
141    
142     sub new ($;%) {
143     my $class = shift;
144 wakaba 1.8 my $self = bless {}, $class;
145     my %new_field = $self->_init (@_);
146 wakaba 1.22 if (defined $new_field{body}) {
147 wakaba 1.8 $self->{body} = $new_field{body}; $new_field{body} = undef;
148 wakaba 1.18 $self->{body} = $self->_parse_value ([$self->content_type] => $self->{body})
149 wakaba 1.8 if $self->{option}->{parse_all};
150     }
151 wakaba 1.14 $self->{header} = new Message::Header
152     -format => $self->{option}->{format},
153 wakaba 1.22 -header_default_charset => $self->{option}->{header_default_charset},
154     -header_default_charset_input => $self->{option}->{header_default_charset_input},
155 wakaba 1.8 -parse_all => $self->{option}->{parse_all}, %new_field;
156 wakaba 1.1 $self;
157     }
158    
159 wakaba 1.9 =item Message::Entity->parse ($message, [%options])
160 wakaba 1.1
161 wakaba 1.9 Parses given C<message> (a message entity) and constructs a new C<Message::Entity>
162     object. You might pass some additional C<field-name>-C<field-body> pairs
163     or/and initial options as parameters to the constructor.
164 wakaba 1.8
165 wakaba 1.1 =cut
166    
167     sub parse ($$;%) {
168     my $class = shift;
169     my $message = shift;
170 wakaba 1.8 my $self = bless {}, $class;
171     my %new_field = $self->_init (@_);
172 wakaba 1.20 my $nl = "\x0D\x0A";
173     unless ($self->{option}->{strict_linebreak}) {
174     unless ($message =~ /\x0D\x0A/s) {
175     my $lfcr = $message =~ s/\x0A\x0D/\x0A\x0D/gs;
176     my $cr = $message =~ s/\x0D(?!\x0A)/\x0D/gs;
177     my $lf = $message =~ s/(?<!\x0D)\x0A/\x0A/gs;
178     if ($lfcr >= $cr && $lfcr >= $lf) { $nl = "\x0A\x0D" }
179     elsif ($cr >= $lf) { $nl = "\x0D" }
180     else { $nl = "\x0A" }
181     }
182     }
183     my @header = ();
184     my @body = split /$nl/, $message;
185 wakaba 1.8 while (1) {
186     my $line = shift @body;
187     unless (length($line)) {
188     last;
189 wakaba 1.1 } else {
190 wakaba 1.8 push @header, $line;
191 wakaba 1.1 }
192     }
193 wakaba 1.8 $new_field{body} = undef if $new_field{body};
194     $self->{header} = parse_array Message::Header \@header,
195 wakaba 1.22 -header_default_charset => $self->{option}->{header_default_charset},
196     -header_default_charset_input => $self->{option}->{header_default_charset_input},
197 wakaba 1.8 -parse_all => $self->{option}->{parse_all},
198     -format => $self->{option}->{format}, %new_field;
199 wakaba 1.20 $self->{body} = join $nl, @body;
200 wakaba 1.18 $self->{body} = $self->_parse_value ([$self->content_type] => $self->{body})
201 wakaba 1.8 if $self->{option}->{parse_all};
202 wakaba 1.1 $self;
203     }
204    
205 wakaba 1.9 =back
206    
207 wakaba 1.8 =head1 METHODS
208    
209 wakaba 1.1 =head2 $self->header ([$new_header])
210    
211     Returns Message::Header unless $new_header.
212     Set $new_header instead of current C<header>.
213     If !ref $new_header, Message::Header->parse is automatically
214     called.
215    
216     =cut
217    
218 wakaba 1.9 ## TODO: to be compatible with HTTP::Message
219 wakaba 1.1 sub header ($;$) {
220     my $self = shift;
221     my $new_header = shift;
222     if (ref $new_header) {
223     $self->{header} = $new_header;
224     } elsif ($new_header) {
225 wakaba 1.4 $self->{header} = Message::Header->parse ($new_header,
226 wakaba 1.22 -header_default_charset => $self->{option}->{header_default_charset},
227     -header_default_charset_input => $self->{option}->{header_default_charset_input},
228 wakaba 1.8 -parse_all => $self->{option}->{parse_all},
229     -format => $self->{option}->{format});
230 wakaba 1.1 }
231 wakaba 1.14 unless (ref $self->{header} || length $self->{header}) {
232     $self->{header} = new Message::Header (
233 wakaba 1.22 -header_default_charset => $self->{option}->{header_default_charset},
234     -header_default_charset_input => $self->{option}->{header_default_charset_input},
235 wakaba 1.14 -parse_all => $self->{option}->{parse_all},
236     -format => $self->{option}->{format});
237 wakaba 1.2 }
238 wakaba 1.1 $self->{header};
239     }
240    
241     =head2 $self->body ([$new_body])
242    
243     Returns C<body> as string unless $new_body.
244     Set $new_body instead of current C<body>.
245    
246     =cut
247    
248     sub body ($;$) {
249     my $self = shift;
250     my $new_body = shift;
251     if ($new_body) {
252     $self->{body} = $new_body;
253     }
254 wakaba 1.18 $self->{body} = $self->_parse_value ([$self->content_type] => $self->{body})
255 wakaba 1.3 unless ref $self->{body};
256 wakaba 1.1 $self->{body};
257     }
258    
259 wakaba 1.18 ## [SG]et its entity header. This method is or can be used
260     ## when Message::Entity is used as a body (such as message/rfc822).
261     sub entity_header ($;$) {
262     my $self = shift;
263     my $new_header = shift;
264     if (ref $new_header) {
265     $self->{entity_header} = $new_header;
266     }
267     $self->{entity_header};
268     }
269    
270 wakaba 1.14 ## $self->_parse_value ($type, $value);
271     sub _parse_value ($$$) {
272 wakaba 1.3 my $self = shift;
273 wakaba 1.18 my ($mt,$mst) = @{ shift(@_) };
274 wakaba 1.14 my $value = shift;
275     return $value if ref $value;
276    
277     ## decode
278     $value = $self->_decode_body ($value);
279    
280 wakaba 1.18 my $mt_def = $Message::MIME::MediaType::type{$mt}->{$mst};
281     $mt_def = $Message::MIME::MediaType::type{$mt}->{'/default'} unless ref $mt_def;
282     $mt_def = $Message::MIME::MediaType::type{'/default'}->{'/default'}
283     unless ref $mt_def;
284     my $handler = $mt_def->{handler}
285     || $Message::MIME::MediaType::type{$mt}->{'/default'}->{handler}
286     || $Message::MIME::MediaType::type{'/default'}->{'/default'}->{handler};
287     ## Ummmmmm....
288     if (ref $handler eq 'CODE') {
289     $handler = &$handler ($self, $mt, $mst);
290     }
291     my $vtype = $handler->[0];
292     my %vopt = (
293     -format => $self->{option}->{format},
294     -media_type => $mt,
295     -media_subtype => $mst,
296     -parse_all => $self->{option}->{parse_all},
297     -body_default_charset => $self->{option}->{body_default_charset},
298     -body_default_charset_input => $self->{option}->{body_default_charset_input},
299     entity_header => $self->{header},
300     );
301     ## Media type specified option/parameters
302     if (ref $handler->[1] eq 'HASH') {
303     for (keys %{$handler->[1]}) {
304     $vopt{$_} = ${$handler->[1]}{$_};
305     }
306     }
307     ## Inherited options
308     if (ref $handler->[2] eq 'ARRAY') {
309     for (@{$handler->[2]}) {
310     $vopt{'-'.$_} = $self->{option}->{$_};
311     }
312     }
313    
314 wakaba 1.14 if ($vtype eq ':none:') {
315     return $value;
316     } elsif (defined $value) {
317     eval "require $vtype" or Carp::croak qq{<parse>: $vtype: Can't load package: $@};
318 wakaba 1.18 return $vtype->parse ($value, %vopt);
319 wakaba 1.3 } else {
320 wakaba 1.14 eval "require $vtype" or Carp::croak qq{<parse>: $vtype: Can't load package: $@};
321 wakaba 1.18 return $vtype->new (%vopt);
322 wakaba 1.3 }
323     }
324    
325 wakaba 1.14 sub _decode_body ($$) {
326     my $self = shift;
327     my $value = shift;
328     ## MIME CTE
329 wakaba 1.22 my $cte = $self->{_cte} || '';
330 wakaba 1.14 my $ctef = $self->header->field ('content-transfer-encoding',
331     -new_item_unless_exist => 0);
332     $cte = $ctef->value if ref $ctef;
333 wakaba 1.15 my $f = $Message::MIME::Encoding::DECODER{$cte};
334 wakaba 1.14 if (ref $f) {
335     ($value, $cte) = &$f ($self, $value);
336     }
337     $self->{_cte} = $cte;
338     $value;
339     }
340    
341 wakaba 1.15 sub _encode_body ($$\%) {
342 wakaba 1.14 my $self = shift;
343     my $value = shift;
344 wakaba 1.15 my $option = shift;
345 wakaba 1.14 ## MIME CTE
346 wakaba 1.17 my $current_cte = $self->{_cte} || 'binary';
347     my $ctef = $self->{header}->field ('content-transfer-encoding',
348 wakaba 1.14 -new_item_unless_exist => 0);
349 wakaba 1.15 my $cte = ''; $cte = lc $ctef->value if ref $ctef;
350 wakaba 1.16 my %enoption;
351 wakaba 1.15 ## Get media type of entity body and its accept CTE list
352     my ($mt,$mst) = $self->content_type;
353     my $mt_def = $Message::MIME::MediaType::type{$mt}->{$mst};
354     $mt_def = $Message::MIME::MediaType::type{$mt}->{'/default'}
355 wakaba 1.18 unless ref $mt_def;
356 wakaba 1.15 $mt_def = $Message::MIME::MediaType::type{'/default'}->{'/default'}
357     unless ref $mt_def;
358 wakaba 1.16 $enoption{mt_is_text} = 1
359     if $mt eq 'text' || $mt eq 'multipart' || $mt eq 'message';
360     my ($charset, $charset_def) = '';
361     if ($mt_def->{mime_charset}) {
362     ## If CT is able to have its charset parameter,
363 wakaba 1.17 my $ct = $self->{header}->field ('content-type',
364 wakaba 1.16 -new_item_unless_exist => 0);
365     $charset = $ct->parameter ('charset') if ref $ct;
366     if ($charset) {
367     $charset_def = $Message::MIME::Charset::CHARSET{$charset};
368     } else {
369     $charset_def = $Message::MIME::Charset::CHARSET{'*default'};
370     ## Note: 'encoding_after_encode' option (available for header
371     ## field, but not for message body) is hardcoded.
372     }
373 wakaba 1.18 } else { ## Don't have mime style "charset" parameter
374     $charset_def = {mime_text => 1};
375 wakaba 1.16 }
376     $charset_def = {} unless ref $charset_def; ## dummy
377 wakaba 1.17 if ($charset_def->{mime_text} != 1) {
378 wakaba 1.18 $enoption{mt_is_text} = 0 if $mt eq 'text';
379 wakaba 1.17 my $ct = $self->{header}->field ('content-type');
380     $ct->not_mime_text ($option->{text_coderange} eq 'binary'? 0:1);
381     }
382 wakaba 1.15 ## If accept CTE list is defined,
383 wakaba 1.16 for my $def ($charset_def, $mt_def) {
384     if (ref $def->{accept_cte} eq 'ARRAY') {
385     my $f = 1; for (@{$def->{accept_cte}}) {
386     if ($cte eq $_) {$f = 0; last}
387     }
388     if ($f) { ## If CTE is not accepted,
389     $cte = $def->{accept_cte}->[0];
390     }
391 wakaba 1.15 }
392     }
393     if ($current_cte eq 'binary' || ($current_cte && $current_cte ne $cte)) {
394     my $de = $Message::MIME::Encoding::DECODER{$current_cte};
395     my $en = $Message::MIME::Encoding::ENCODER{$cte || 'binary'};
396     if (ref $de && ref $en) {
397     my ($e, $decoded);
398     ($decoded, $e) = &$de ($self, $value);
399     ## Check transparent coderange
400 wakaba 1.16 my $cr = $self->Message::MIME::Encoding::decide_coderange
401     ($decoded, \%enoption);
402 wakaba 1.15 if ($option->{accept_coderange} eq '8bit') {
403     if ($cr eq 'binary') {
404 wakaba 1.16 $cte = $charset_def->{cte_7bit_preferred}
405     || $mt_def->{cte_7bit_preferred} || 'base64';
406 wakaba 1.15 $en = $Message::MIME::Encoding::ENCODER{$cte};
407     }
408     } elsif ($option->{accept_coderange} eq '7bit') {
409     if ($cr eq 'binary' || $cr eq '8bit') {
410 wakaba 1.16 $cte = $charset_def->{cte_7bit_preferred}
411     || $mt_def->{cte_7bit_preferred} || 'base64';
412 wakaba 1.15 $en = $Message::MIME::Encoding::ENCODER{$cte};
413 wakaba 1.18 if ($mt eq 'message') {
414     my $ct = $self->{header}->field ('content-type');
415     $ct->not_mime_text ($option->{text_coderange} eq 'binary'? 0:1);
416     }
417 wakaba 1.15 }
418     }
419     if ($e eq 'binary') {
420 wakaba 1.16 ($value, $e) = &$en ($self, $decoded, \%enoption);
421 wakaba 1.17 $e = '' if ($e eq $option->{cte_default});
422     $e = '' if $e eq '7bit'
423     && ( $option->{cte_default} eq '8bit'
424     || $option->{cte_default} eq 'binary');
425     $e = '' if $e eq '8bit' && $option->{cte_default} eq 'binary';
426     if ($e) {
427     $ctef = $self->{header}->field ('content-transfer-encoding')
428 wakaba 1.15 unless ref $ctef;
429     $ctef->value ($e);
430 wakaba 1.17 } elsif (ref $ctef) {
431     $ctef->value ('');
432     }
433 wakaba 1.15 } else {
434 wakaba 1.17 $ctef = $self->{header}->field ('content-transfer-encoding')
435 wakaba 1.15 unless ref $ctef;
436     $ctef->value ($current_cte);
437     }
438 wakaba 1.14 } else { ## Can't encode by given CTE
439 wakaba 1.17 $ctef = $self->{header}->field ('content-transfer-encoding')
440 wakaba 1.15 unless ref $ctef;
441 wakaba 1.14 $ctef->value ($current_cte);
442     }
443     }
444 wakaba 1.17 if (ref $ctef && $ctef->value eq '') {
445     $self->{header}->delete ('content-transfer-encoding');
446     }
447 wakaba 1.14 $value;
448     }
449    
450 wakaba 1.1 =head2 $self->stringify ([%option])
451    
452     Returns the C<message> as a string.
453    
454     =cut
455    
456     sub stringify ($;%) {
457     my $self = shift;
458 wakaba 1.8 my %params = @_;
459     my %option = %{$self->{option}};
460     for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}
461 wakaba 1.18 my ($header, $body, $body0);
462 wakaba 1.14 if (ref $self->{body}) {
463 wakaba 1.18 $self->{body}->entity_header ($self->{header});
464     $body0 = $self->{body}->stringify (-format => $option{format},
465 wakaba 1.14 -linebreak_strict => $option{linebreak_strict});
466     } else {
467 wakaba 1.18 $body0 = $self->{body};
468 wakaba 1.14 }
469 wakaba 1.18 $body = $self->_encode_body ($body0, \%option);
470 wakaba 1.14 if (ref $self->{header}) {
471 wakaba 1.15 my %exist;
472     for ($self->{header}->field_name_list) {$exist{$_} = 1}
473     my $ns_content = $Message::Header::NS_phname2uri{content};
474 wakaba 1.14 if ($option{fill_date}
475     && !$exist{$option{fill_date_name}.':'.$option{fill_date_ns}}) {
476     $self->{header}->field
477     ($option{fill_date_name}, -ns => $option{fill_date_ns})->unix_time (time);
478     }
479     if ($option{fill_msgid}
480     && !$exist{$option{fill_msgid_name}.':'.$option{fill_msgid_ns}}) {
481     my $from = $self->{header}->field
482     ('from', -ns => $option{fill_msgid_from_ns}, -new_item_unless_exist => 0);
483     $from = $from->addr_spec if ref $from;
484     $self->{header}->field
485     ($option{fill_msgid_name}, -ns => $option{fill_msgid_ns})
486     ->generate (addr_spec => $from)
487 wakaba 1.8 if $from;
488 wakaba 1.15 } # fill_msgid
489     my $ismime = 0;
490     for (keys %exist) {if (/:$ns_content$/) { $ismime = 1; last }}
491 wakaba 1.18 unless ($ismime) {
492 wakaba 1.19 $ismime = 1 if $option{force_mime_entity};
493 wakaba 1.18 $ismime = 1 if $option{body_default_media_type} ne 'text';
494     $ismime = 1 if $option{body_default_media_subtype} ne 'plain';
495     }
496 wakaba 1.15 if ($ismime) {
497     if ($option{fill_ct} && !$exist{'type:'.$ns_content}) {
498     my $ct = $self->{header}->field ('type',
499     -parse => 1, -ns => $ns_content);
500 wakaba 1.18 $ct->media_type ($option{body_default_media_type}.'/'
501     .$option{body_default_media_subtype});
502     $ct->replace (Message::MIME::Charset::name_minimumize ($option{body_default_charset} => $body0));
503 wakaba 1.15 }
504     if ($option{fill_mimever}
505     && !$exist{'mime-version:'.$option{fill_mimever_ns}}) {
506     ## BUG: doesn't support rfc10]49, HTTP (ie. non-MIME) content-*: fields
507 wakaba 1.14 $self->{header}->add ('mime-version' => '1.0',
508     -parse => 0, -ns => $option{fill_mimever_ns});
509 wakaba 1.8 }
510 wakaba 1.15 } # $ismime
511 wakaba 1.14 if ($option{format} =~ /uri-url-mailto/ && $exist{'type:$ns_content'}
512 wakaba 1.12 && $option{uri_mailto_safe_level} > 1) {
513     $self->{header}->field ('content-type')->media_type ('text/plain');
514     }
515 wakaba 1.21 if ($option{add_ua}) {
516     $self->{header}->field ($option{fill_ua_name})->add_our_name (
517     -use_Config => $option{ua_use_Config},
518     -use_Win32 => $option{ua_use_Win32},
519     );
520     }
521 wakaba 1.12 $header = $self->{header}->stringify (-format => $option{format},
522     -linebreak_strict => $option{linebreak_strict},
523     -uri_mailto_safe_level => $option{uri_mailto_safe_level});
524 wakaba 1.7 } else {
525     $header = $self->{header};
526 wakaba 1.12 unless ($option{linebreak_strict}) {
527     ## bare \x0D and bare \x0A are unsafe
528     $header =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g;
529     $header =~ s/\x0A(?=[^\x09\x20])/\x0A\x20/g;
530     }
531 wakaba 1.7 }
532 wakaba 1.12 if ($option{format} =~ /uri-url-mailto/) {
533 wakaba 1.14 if ($option{format} =~ /rfc1738/) {
534     my $to = $self->{header}->stringify (-format => $option{format},
535     -uri_mailto_safe_level => $option{uri_mailto_safe_level});
536     $to? 'mailto:'.$to: '';
537     } else {
538     my $f = $option{format}; $f =~ s/-mailto/-mailto-to/;
539     my $to = $self->{header}->stringify (-format => $f,
540     -uri_mailto_safe_level => $option{uri_mailto_safe_level});
541     $body =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge;
542     if (length $body) {
543     $header .= '&' if $header;
544     $header .= 'body='.$body;
545     }
546     $header = '?'.$header if $header;
547     $to||$header? 'mailto:'.$to.$header: '';
548 wakaba 1.12 }
549     } else {
550 wakaba 1.15 $header .= "\x0D\x0A" if $header && $header !~ /\x0D\x0A$/;
551     $header."\x0D\x0A".$body;
552 wakaba 1.12 }
553 wakaba 1.1 }
554 wakaba 1.8 *as_string = \&stringify;
555 wakaba 1.1
556    
557 wakaba 1.9 =head1 SHORTCUT METHOD FOR MESSAGE PROPERTIES
558    
559     =over 4
560    
561     =item $self->content_type ([%options])
562 wakaba 1.1
563 wakaba 1.9 Returns Internet media type of message body
564     (aka MIME type, content type). Only media type
565     (type/subtype pair) is returned, i.e. no parameter
566     is returned, if any. To get such value, or to set
567     new value, use C<field> method.
568 wakaba 1.1
569 wakaba 1.9 Default is C<text/plain>.
570 wakaba 1.3
571 wakaba 1.9 Example:
572 wakaba 1.3
573 wakaba 1.9 $msg->field ('Content-Type')->media_type ('text/html');
574     print $msg->content_type; ## text/html
575 wakaba 1.3
576     =cut
577    
578     sub content_type ($;%) {
579 wakaba 1.8 my $self = shift;
580 wakaba 1.15 my $ct = $self->{header}->field ('content-type', -new_item_unless_exist => 0);
581     unless (ref $ct) {
582 wakaba 1.18 if ($self->{option}->{body_default_media_type} ne 'text'
583     || $self->{option}->{body_default_media_subtype} ne 'plain') {
584     $ct = $self->{header}->field ('content-type');
585     $ct->media_type_major ($self->{option}->{body_default_media_type});
586     $ct->media_type_minor ($self->{option}->{body_default_media_subtype});
587     }
588     return wantarray? ($self->{option}->{body_default_media_type},
589     $self->{option}->{body_default_media_subtype}):
590     $self->{option}->{body_default_media_type}.'/'
591     .$self->{option}->{body_default_media_subtype};
592 wakaba 1.15 }
593     if (wantarray) {
594     ($ct->media_type_major, $ct->media_type_minor);
595     } else {
596     $ct->media_type;
597     }
598 wakaba 1.1 }
599 wakaba 1.15 *media_type = \&content_type;
600 wakaba 1.1
601 wakaba 1.9 =item $self->id
602    
603     Returns ID of message entity. If there are C<Message-ID:>
604     field, its value is returned. Unless, but there are
605     C<Content-ID:> field, it is returned. Without both of
606     fields, C<""> is returned.
607    
608     =cut
609    
610 wakaba 1.5 sub id ($) {
611     my $self = shift;
612     return scalar $self->{header}->field ('message-id')->id
613     if $self->{header}->field_exist ('message-id');
614 wakaba 1.9 return scalar $self->{header}->field ('content-id')->id
615     if $self->{header}->field_exist ('content-id');
616 wakaba 1.8 '';
617 wakaba 1.5 }
618    
619 wakaba 1.9 =back
620    
621     =head1 MISC. METHODS
622    
623     =over 4
624    
625     =item $self->option ( $option-name / $option-name, $option-value, ...)
626    
627     If @_ == 1, returns option value. Else...
628    
629     Set option value. You can pass multiple option name-value pair
630     as parameter. Example:
631    
632     $msg->option (format => 'mail-rfc822',
633     capitalize => 0);
634     print $msg->option ('format'); ## mail-rfc822
635    
636     =cut
637    
638     sub option ($@) {
639     my $self = shift;
640     if (@_ == 1) {
641     return $self->{option}->{ $_[0] };
642     }
643 wakaba 1.18 my %option = @_;
644 wakaba 1.9 while (my ($name, $value) = splice (@_, 0, 2)) {
645     $self->{option}->{$name} = $value;
646     if ($name eq 'format') {
647     $self->header->option (-format => $value);
648     }
649     }
650 wakaba 1.18 if ($option{-recursive}) {
651     $self->{header}->option (%option);
652     $self->{body}->option (%option) if ref $self->{body};
653     }
654 wakaba 1.9 }
655    
656     =item $self->clone ()
657 wakaba 1.8
658     Returns a copy of Message::Entity object.
659    
660     =cut
661    
662     sub clone ($) {
663     my $self = shift;
664     my $clone = new Message::Entity;
665 wakaba 1.14 $clone->{option} = Message::Util::make_clone ($self->{option});
666     for (@{$self->{option}->{_MEMBERS}}) {
667     $clone->{$_} = Message::Util::make_clone ($self->{$_});
668 wakaba 1.8 }
669     $clone;
670     }
671 wakaba 1.5
672 wakaba 1.14 my %_method_default_list = qw(new 1 parse 1 stringify 1 option 1 clone 1 method_available 1);
673     sub method_available ($$) {
674     my $self = shift;
675     my $name = shift;
676     return 1 if $_method_default_list{$name};
677     for (@{$self->{option}->{_METHODS}}) {
678     return 1 if $_ eq $name;
679     }
680     0;
681     }
682    
683 wakaba 1.9 =back
684    
685 wakaba 1.11 =head1 C<format>
686    
687     =over 2
688    
689     =item mail-rfc650
690    
691     Internet mail message, defined by IETF RFC 650
692    
693     =item mail-rfc724
694    
695     Internet mail message, defined by IETF RFC 724
696    
697     =item mail-rfc733
698    
699     Internet mail message, defined by IETF RFC 733
700    
701     =item mail-rfc822
702    
703     Internet mail message, defined by IETF RFC 822
704    
705     =item mail-rfc2822
706    
707     Internet mail message, defined by IETF RFC 2822
708    
709     =item mime-1.0
710    
711     MIME entity
712    
713     =item mime-1.0-rfc1341
714    
715     MIME entity, defined by RFC 1341 (and RFC 1342)
716    
717     =item mime-1.0-rfc1521
718    
719     MIME entity, defined by RFC 1521 and 1522
720    
721     =item mime-1.0-rfc2045
722    
723     MIME entity, defined by RFC 2045,..., 2049
724    
725     =item news-bnews
726    
727     Usenet Bnews format
728    
729     =item news-rfc850
730    
731     Usenet news format, defined by IETF RFC 850
732    
733     =item news-rfc1036
734    
735     Usenet news format, defined by IETF RFC 1036
736    
737     =item news-son-of-rfc1036
738    
739     Usenet news format, defined by son-of-RFC1036
740    
741     =item news-usefor
742    
743     Usenet news format, defined by usefor-article (IETF Internet Draft)
744    
745     =item http-1.0-rfc1945
746    
747     HTTP/1.0 message, defined by IETF RFC 1945
748    
749     =item http-1.0-rfc1945-request
750    
751     HTTP/1.0 request message, defined by IETF RFC 1945
752    
753     =item http-1.0-rfc1945-response
754    
755     HTTP/1.0 response message, defined by IETF RFC 1945
756    
757     =item http-1.1-rfc2068
758    
759     HTTP/1.1 message, defined by IETF RFC 2068
760    
761     =item http-1.1-rfc2068-request
762    
763     HTTP/1.1 request message, defined by IETF RFC 2068
764    
765     =item http-1.1-rfc2068-response
766    
767     HTTP/1.1 response message, defined by IETF RFC 2068
768    
769     =item http-1.1-rfc2616
770    
771     HTTP/1.1 message, defined by IETF RFC 2616
772    
773     =item http-1.1-rfc2616-request
774    
775     HTTP/1.1 request message, defined by IETF RFC 2616
776    
777     =item http-1.1-rfc2616-response
778    
779     HTTP/1.1 response message, defined by IETF RFC 2616
780    
781     =item http-cgi-1.1
782    
783     CGI/1.1 output (for HTTP), defined by coar-cgi-v11 (IETF Internet Draft)
784    
785     =item http-1.0-cgi-1.1
786    
787     CGI/1.1 output (for HTTP/1.0), defined by coar-cgi-v11 (IETF Internet Draft)
788    
789     =item http-1.1-cgi-1.1
790    
791     CGI/1.1 output (for HTTP/1.1), defined by coar-cgi-v11 (IETF Internet Draft)
792    
793     =item http-cgi-1.2
794    
795     CGI/1.2 output, defined by coar-cgi-v12 (to be IETF Internet Draft)
796    
797     =item http-1.0-cgi-1.2
798    
799     CGI/1.2 output (for HTTP/1.0), defined by coar-cgi-v11 (IETF Internet Draft)
800    
801     =item http-1.1-cgi-1.2
802    
803     CGI/1.2 output (for HTTP/1.1), defined by coar-cgi-v11 (IETF Internet Draft)
804    
805     =item http-sip-2.0
806    
807     SIP/2.0 message, defined by IETF RFC 2543
808    
809     =item http-sip-2.0-request
810    
811     SIP/2.0 request message, defined by IETF RFC 2543
812    
813     =item http-sip-2.0-response
814    
815     SIP/2.0 response message, defined by IETF RFC 2543
816    
817     =item http-sip-cgi
818    
819     SIP/2.0 CGI (IETF Internet Draft)
820    
821     =item cpim-1.0
822    
823     CPIM/1.0 (IETF Internet Draft)
824    
825 wakaba 1.12 =item uri-url-mailto-mail-rfc822, uri-url-mailto-mail-rfc2822
826    
827     mailto: URL scheme
828    
829     =item uri-url-mailto-rfc1738
830    
831     mailto: URL scheme (defined by RFC 1738)
832    
833     =item uri-url-mailto-rfc2368, uri-url-mailto-rfc2822
834    
835     mailto: URL scheme (defined by RFC 2368)
836    
837     =item uri-url-mailto-to-mail-rfc822, uri-url-mailto-to-mail-rfc2822
838    
839     C<to> part of mailto: URL scheme (for internal use only)
840    
841 wakaba 1.11 =back
842    
843 wakaba 1.1 =head1 EXAMPLE
844    
845     use Message::Entity;
846 wakaba 1.9 my $msg = new Message::Entity From => '[email protected]',
847     Subject => 'Example message',
848     To => '[email protected]',
849     -format => 'mail-rfc2822',
850     body => $body;
851 wakaba 1.1 $msg->header ($header);
852     $msg->body ($body);
853     print $msg;
854    
855 wakaba 1.3 =head1 SEE ALSO
856    
857     Message::* Perl modules
858     <http://suika.fam.cx/~wakaba/Message-pm/>
859    
860 wakaba 1.1 =head1 LICENSE
861    
862     Copyright 2002 wakaba E<lt>[email protected]<gt>.
863    
864     This program is free software; you can redistribute it and/or modify
865     it under the terms of the GNU General Public License as published by
866     the Free Software Foundation; either version 2 of the License, or
867     (at your option) any later version.
868    
869     This program is distributed in the hope that it will be useful,
870     but WITHOUT ANY WARRANTY; without even the implied warranty of
871     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
872     GNU General Public License for more details.
873    
874     You should have received a copy of the GNU General Public License
875     along with this program; see the file COPYING. If not, write to
876     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
877     Boston, MA 02111-1307, USA.
878    
879     =head1 CHANGE
880    
881     See F<ChangeLog>.
882 wakaba 1.22 $Date: 2002/06/16 10:45:54 $
883 wakaba 1.1
884     =cut
885    
886     1;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24