/[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.21 - (hide annotations) (download)
Sun Jun 16 10:45:54 2002 UTC (24 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.20: +10 -64 lines
2002-06-16  wakaba <w@suika.fam.cx>

	* Header.pm (_n11n_field_name): Check namespace definition's
	case_sensible option.
	* Entity.pm (_add_ua): Removed.  (Moved to Message::Field::UA.)

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24