/[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.29 - (hide annotations) (download)
Sat Jul 20 03:11:47 2002 UTC (24 years ago) by wakaba
Branch: MAIN
Changes since 1.28: +38 -8 lines
2002-07-20  Wakaba <w@suika.fam.cx>

	* Entity.pm (content_type): Guess media type: 
	text/x-message-rfc1153, text/x-pgp-cleatext-singed,
	application/pgp, text/x-message-pem.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24