/[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.25 - (hide annotations) (download)
Mon Jul 8 11:49:18 2002 UTC (24 years ago) by wakaba
Branch: MAIN
Changes since 1.24: +4 -3 lines
2002-07-08  Wakaba <w@suika.fam.cx>

	* Entity.pm (parse): Typo fix.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24