/[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.39 - (hide annotations) (download)
Sat Dec 28 09:10:16 2002 UTC (23 years, 8 months ago) by wakaba
Branch: MAIN
Branch point for: branch-suikawiki-1
Changes since 1.38: +6 -5 lines
*** empty log message ***

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 wakaba 1.37 require 5.6.0; ## (require: v5.6.0 data type)
14 wakaba 1.1 package Message::Entity;
15     use strict;
16 wakaba 1.37 use vars qw(%DEFAULT %REG $VERSION);
17 wakaba 1.39 $VERSION=do{my @r=(q$Revision: 1.38 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
18 wakaba 1.1
19 wakaba 1.15 require Message::Util;
20 wakaba 1.8 require Message::Header;
21 wakaba 1.15 require Message::MIME::MediaType;
22     require Message::MIME::Encoding;
23 wakaba 1.9 use overload '""' => sub { $_[0]->stringify },
24     fallback => 1;
25 wakaba 1.37 *REG = \%Message::Util::REG;
26 wakaba 1.1
27 wakaba 1.14 ## Initialize of this class -- called by constructors
28     %DEFAULT = (
29 wakaba 1.15 -_METHODS => [qw|header body content_type id|],
30 wakaba 1.17 -_MEMBERS => [qw|header body _cte|],
31 wakaba 1.18 ## entity_header -- Don't clone.
32 wakaba 1.15 -accept_coderange => '7bit', ## 7bit / 8bit / binary
33     -body_default_charset => 'iso-2022-int-1',
34 wakaba 1.18 -body_default_charset_input => 'iso-2022-int-1',
35     -body_default_media_type => 'text',
36     -body_default_media_subtype => 'plain',
37 wakaba 1.17 -cte_default => '7bit',
38 wakaba 1.33 -fill_missing_fields => 1,
39     #add_ua => 1,
40     #fill_date => 1,
41     -fill_date_name => 'date',
42     -fill_destination => 0,
43     #fill_destination_ns
44     #fill_destination_resent_ns
45     #fill_from_ns
46     -fill_md5 => 0,
47     -fill_md5_name => 'md5',
48     #fill_msgid => 1,
49     -fill_msgid_name => 'message-id',
50     #fill_sender_ns
51 wakaba 1.39 -fill_source => 0,
52 wakaba 1.33 -recalc_md5 => 1,
53 wakaba 1.19 -force_mime_entity => 0,
54 wakaba 1.15 -format => 'mail-rfc2822',
55 wakaba 1.28 -guess_media_type => 1,
56 wakaba 1.30 #internal_charset_name
57 wakaba 1.22 -header_default_charset => 'iso-2022-int-1',
58     -header_default_charset_input => 'iso-2022-int-1',
59 wakaba 1.15 -linebreak_strict => 0, ## BUG: not work perfectly
60 wakaba 1.37 -output_magic_line => 1,
61 wakaba 1.15 -parse_all => 0,
62 wakaba 1.37 -remove_post_newline => 0,
63     -remove_pre_newline => 0,
64 wakaba 1.17 -text_coderange => 'binary',
65     ## '8bit' (MIME text/*) / 'binary' (HTTP text/*)
66 wakaba 1.8 #ua_field_name => 'user-agent',
67 wakaba 1.21 -ua_use_Config => 1,
68     -ua_use_Win32 => 1,
69 wakaba 1.15 -uri_mailto_safe_level => 4,
70 wakaba 1.37 -use_magic_line => 1,
71 wakaba 1.14 );
72     sub _init ($;%) {
73     my $self = shift;
74     my %options = @_;
75 wakaba 1.15 $self->{option} = {};
76     my $o = Message::Util::make_clone (\%DEFAULT);
77     for my $name (keys %$o) {
78     if (substr ($name, 0, 1) eq '-') {
79     $self->{option}->{substr ($name, 1)} = $$o{$name};
80     }
81     }
82 wakaba 1.14
83 wakaba 1.8 my @new_fields = ();
84     for my $name (keys %options) {
85     if (substr ($name, 0, 1) eq '-') {
86     $self->{option}->{substr ($name, 1)} = $options{$name};
87 wakaba 1.18 } elsif ($name eq 'entity_header') {
88     $self->{entity_header} = $options{entity_header};
89 wakaba 1.8 } else {
90 wakaba 1.18 push @new_fields, ($name => $options{$name});
91 wakaba 1.8 }
92     }
93 wakaba 1.18
94 wakaba 1.8 my $format = $self->{option}->{format};
95 wakaba 1.37 my $ns822 = $Message::Header::NS_phname2uri{'x-rfc822'};
96     my $nshttp = $Message::Header::NS_phname2uri{'x-http'};
97 wakaba 1.14 if ($format =~ /http/) {
98 wakaba 1.37 $self->{option}->{fill_date_ns} = $nshttp unless defined $options{-fill_date_ns};
99     $self->{option}->{fill_from_ns} = $nshttp unless defined $options{-fill_from_ns};
100     $self->{option}->{fill_ua_ns} = $nshttp unless defined $options{-fill_ua_ns};
101     $self->{option}->{fill_source} = 0 unless defined $options{-fill_source};
102 wakaba 1.15 $self->{option}->{accept_coderange} = 'binary';
103 wakaba 1.17 $self->{option}->{text_coderange} = 'binary';
104     $self->{option}->{cte_default} = 'binary';
105 wakaba 1.14 } else {
106 wakaba 1.38 if ($format =~ /mail-rfc822|mail-rfc2822/ && $format !~ /mime-entity/) {
107 wakaba 1.33 $self->{option}->{fill_destination} = 1;
108 wakaba 1.39 $self->{option}->{fill_source} = 1 unless defined $options{-fill_source};
109 wakaba 1.33 }
110 wakaba 1.37 $self->{option}->{fill_date_ns} = $ns822 unless defined $options{-fill_date_ns};
111     $self->{option}->{fill_from_ns} = $ns822 unless defined $options{-fill_from_ns};
112     $self->{option}->{fill_ua_ns} = $ns822 unless defined $options{-fill_ua_ns};
113 wakaba 1.17 $self->{option}->{text_coderange} = '8bit';
114 wakaba 1.15 if ($format =~ /news-usefor|smtp-8bitmime/) {
115     $self->{option}->{accept_coderange} = '8bit';
116     } else {
117     $self->{option}->{accept_coderange} = '7bit';
118     }
119 wakaba 1.14 }
120 wakaba 1.37 for (qw/fill_msgid_ns fill_msgid_from fill_mimever_ns fill_destination_ns fill_sender_ns/) {
121     $self->{option}->{$_} = $ns822;
122 wakaba 1.33 }
123     $self->{option}->{fill_destination_resent_ns} = $Message::Header::NS_phname2uri{'x-rfc822-resent'};
124 wakaba 1.8 unless (defined $self->{option}->{fill_date}) {
125 wakaba 1.18 $self->{option}->{fill_date} = $format !~ /mime-entity|cgi|uri-url-mailto/;
126 wakaba 1.8 }
127     unless (defined $self->{option}->{fill_msgid}) {
128 wakaba 1.18 $self->{option}->{fill_msgid} = $format !~ /mime-entity|http|uri-url-mailto/;
129 wakaba 1.8 }
130 wakaba 1.15 unless (defined $self->{option}->{fill_ct}) {
131     $self->{option}->{fill_ct} = $format !~ /http/;
132     }
133 wakaba 1.8 unless (defined $self->{option}->{fill_mimever}) {
134 wakaba 1.18 $self->{option}->{fill_mimever} = $format !~ /http|mime-entity/;
135     }
136     unless (defined $self->{option}->{add_ua}) {
137     $self->{option}->{add_ua} = $format !~ /mime-entity/;
138 wakaba 1.8 }
139 wakaba 1.22 unless ($self->{option}->{fill_ua_name}) {
140 wakaba 1.14 $self->{option}->{fill_ua_name} = $format =~ /response|cgi|uri-url-mailto/?
141 wakaba 1.8 'server': 'user-agent';
142     }
143 wakaba 1.37 unless (defined $options{-output_magic_line}) {
144     $self->{option}->{output_magic_line} = 0
145     if $format =~ /mail|news|mime/;
146     }
147 wakaba 1.8 @new_fields;
148     }
149    
150     =head1 CONSTRUCTORS
151    
152     The following methods construct new C<Message::Entity> objects:
153 wakaba 1.3
154 wakaba 1.8 =over 4
155    
156 wakaba 1.9 =item Message::Entity->new ([%initial-fields/options])
157 wakaba 1.1
158 wakaba 1.9 Constructs a new C<Message::Entity> object. You might pass some initial
159     C<field-name>-C<field-body> pairs and/or options as parameters to the constructor.
160    
161     Example:
162    
163     $msg = new Message::Entity
164     Content_Type => 'text/html',
165     X_URI => '<http://www.foo.example/>',
166     -format => 'mail-rfc2822' ## not to be header field
167     ;
168 wakaba 1.1
169     =cut
170    
171     sub new ($;%) {
172     my $class = shift;
173 wakaba 1.8 my $self = bless {}, $class;
174     my %new_field = $self->_init (@_);
175 wakaba 1.22 if (defined $new_field{body}) {
176 wakaba 1.8 $self->{body} = $new_field{body}; $new_field{body} = undef;
177 wakaba 1.18 $self->{body} = $self->_parse_value ([$self->content_type] => $self->{body})
178 wakaba 1.8 if $self->{option}->{parse_all};
179     }
180 wakaba 1.14 $self->{header} = new Message::Header
181     -format => $self->{option}->{format},
182 wakaba 1.22 -header_default_charset => $self->{option}->{header_default_charset},
183     -header_default_charset_input => $self->{option}->{header_default_charset_input},
184 wakaba 1.8 -parse_all => $self->{option}->{parse_all}, %new_field;
185 wakaba 1.1 $self;
186     }
187    
188 wakaba 1.9 =item Message::Entity->parse ($message, [%options])
189 wakaba 1.1
190 wakaba 1.9 Parses given C<message> (a message entity) and constructs a new C<Message::Entity>
191     object. You might pass some additional C<field-name>-C<field-body> pairs
192     or/and initial options as parameters to the constructor.
193 wakaba 1.8
194 wakaba 1.1 =cut
195    
196     sub parse ($$;%) {
197     my $class = shift;
198     my $message = shift;
199 wakaba 1.8 my $self = bless {}, $class;
200     my %new_field = $self->_init (@_);
201 wakaba 1.20 my $nl = "\x0D\x0A";
202 wakaba 1.25 unless ($self->{option}->{linebreak_strict}) {
203 wakaba 1.23 $nl = Message::Util::decide_newline ($message);
204 wakaba 1.20 }
205 wakaba 1.37 my ($hdr, $body);
206     $message =~ s/^(?:$nl)+//s if $self->{option}->{remove_pre_newline};
207     $message =~ s/(?:$nl)+$/$nl/s if $self->{option}->{remove_post_newline};
208     if ($self->{option}->{use_magic_line}) {
209     ## TODO: Reset format option?
210     if ($message =~ s/>?From (.+?)$nl//gs) { ## Mail from line
211     $new_field{'x-rfc822-mail-from'} = $1;
212     } elsif ($message =~ s#^($REG{http_token})\x20($REG{S_uri})\x20($REG{http_token})/([0-9]+)\.([0-9]+)$nl##gs) {
213     ## HTTP Request
214     ($self->{http_method}, $self->{http_request_uri},
215     $self->{http_protocol_name}, $self->{http_protocol_version})
216     = ($1, $2, uc $3, pack ('U2', $4, $5));
217     } elsif ($message =~ s#^($REG{http_token})/([0-9]+)\.([0-9]+)\x20([0-9][0-9][0-9]\x20.*?)$nl##gs) {
218     ## HTTP Response
219     ($self->{http_protocol_name}, $self->{http_protocol_version})
220     = (uc $1, pack ('U2', $2, $3));
221     $new_field{'x-http-status'} = $4;
222     } elsif ($message =~ s#^($REG{http_token})\x20($REG{S_uri})$nl##gs) {
223     ## HTTP/0.9 simple-request
224     ($self->{http_method}, $self->{http_request_uri},
225     $self->{http_protocol_name}, $self->{http_protocol_version})
226     = ($1, $2, 'HTTP', v0.9);
227     $message = $nl . $message; ## Has no header fields
228 wakaba 1.1 }
229     }
230 wakaba 1.37 if ($message !~ s/^$nl//s) {
231     ($hdr, $body) = split /$nl$nl/, $message, 2;
232     } else {
233     $hdr = ''; $body = $message;
234     $body =~ s/^$nl//s;
235     }
236 wakaba 1.8 $new_field{body} = undef if $new_field{body};
237 wakaba 1.37 ## Is this implemention good?
238     $self->{header} = parse Message::Header $hdr,
239 wakaba 1.22 -header_default_charset => $self->{option}->{header_default_charset},
240     -header_default_charset_input => $self->{option}->{header_default_charset_input},
241 wakaba 1.8 -parse_all => $self->{option}->{parse_all},
242 wakaba 1.37 -format => $self->{option}->{format},
243     %new_field; ## Additional header fields
244    
245     $self->{body} = $body;
246     $self->{body} = $self->_parse_value ([ $self->content_type ] => $self->{body})
247 wakaba 1.8 if $self->{option}->{parse_all};
248 wakaba 1.1 $self;
249     }
250    
251 wakaba 1.9 =back
252    
253 wakaba 1.8 =head1 METHODS
254    
255 wakaba 1.1 =head2 $self->header ([$new_header])
256    
257     Returns Message::Header unless $new_header.
258     Set $new_header instead of current C<header>.
259     If !ref $new_header, Message::Header->parse is automatically
260     called.
261    
262     =cut
263    
264 wakaba 1.9 ## TODO: to be compatible with HTTP::Message
265 wakaba 1.1 sub header ($;$) {
266     my $self = shift;
267     my $new_header = shift;
268     if (ref $new_header) {
269     $self->{header} = $new_header;
270     } elsif ($new_header) {
271 wakaba 1.4 $self->{header} = Message::Header->parse ($new_header,
272 wakaba 1.22 -header_default_charset => $self->{option}->{header_default_charset},
273     -header_default_charset_input => $self->{option}->{header_default_charset_input},
274 wakaba 1.8 -parse_all => $self->{option}->{parse_all},
275     -format => $self->{option}->{format});
276 wakaba 1.1 }
277 wakaba 1.14 unless (ref $self->{header} || length $self->{header}) {
278     $self->{header} = new Message::Header (
279 wakaba 1.22 -header_default_charset => $self->{option}->{header_default_charset},
280     -header_default_charset_input => $self->{option}->{header_default_charset_input},
281 wakaba 1.14 -parse_all => $self->{option}->{parse_all},
282     -format => $self->{option}->{format});
283 wakaba 1.2 }
284 wakaba 1.1 $self->{header};
285     }
286    
287     =head2 $self->body ([$new_body])
288    
289     Returns C<body> as string unless $new_body.
290     Set $new_body instead of current C<body>.
291    
292     =cut
293    
294     sub body ($;$) {
295     my $self = shift;
296     my $new_body = shift;
297     if ($new_body) {
298     $self->{body} = $new_body;
299     }
300 wakaba 1.18 $self->{body} = $self->_parse_value ([$self->content_type] => $self->{body})
301 wakaba 1.3 unless ref $self->{body};
302 wakaba 1.1 $self->{body};
303     }
304    
305 wakaba 1.18 ## [SG]et its entity header. This method is or can be used
306     ## when Message::Entity is used as a body (such as message/rfc822).
307     sub entity_header ($;$) {
308     my $self = shift;
309     my $new_header = shift;
310     if (ref $new_header) {
311     $self->{entity_header} = $new_header;
312     }
313     $self->{entity_header};
314     }
315    
316 wakaba 1.24 ## Note: If you once parse body (including parse_all => 1 option),
317     ## it might make validation failed.
318     sub md5_check ($) {
319     my $self = shift;
320     my $md5f = $self->{header}->field ('content-md5', -new_item_unless_exist => 0);
321     my $md5; $md5 = $md5f->value if ref $md5f;
322     unless ($md5) {
323     Carp::carp "md5_check: MD5 checksum not found";
324     return undef;
325     }
326     my $MD5;
327     eval q{
328 wakaba 1.34 require Digest::MD5;
329     $MD5 = ($self->Message::MIME::Encoding::encode_base64 (Digest::MD5::md5 ($self->{body})))[0];
330     $MD5 =~ tr/\x09\x0A\x0D\x20//d;
331     1} or Carp::croak $@;
332 wakaba 1.24 return $MD5 eq $md5? 1 : 0;
333     }
334    
335 wakaba 1.14 ## $self->_parse_value ($type, $value);
336     sub _parse_value ($$$) {
337 wakaba 1.3 my $self = shift;
338 wakaba 1.18 my ($mt,$mst) = @{ shift(@_) };
339 wakaba 1.14 my $value = shift;
340     return $value if ref $value;
341    
342     ## decode
343     $value = $self->_decode_body ($value);
344    
345 wakaba 1.18 my $mt_def = $Message::MIME::MediaType::type{$mt}->{$mst};
346     $mt_def = $Message::MIME::MediaType::type{$mt}->{'/default'} unless ref $mt_def;
347     $mt_def = $Message::MIME::MediaType::type{'/default'}->{'/default'}
348     unless ref $mt_def;
349     my $handler = $mt_def->{handler}
350     || $Message::MIME::MediaType::type{$mt}->{'/default'}->{handler}
351     || $Message::MIME::MediaType::type{'/default'}->{'/default'}->{handler};
352     ## Ummmmmm....
353     if (ref $handler eq 'CODE') {
354     $handler = &$handler ($self, $mt, $mst);
355     }
356     my $vtype = $handler->[0];
357     my %vopt = (
358     -format => $self->{option}->{format},
359 wakaba 1.25 -linebreak_strict => $self->{option}->{linebreak_strict},
360 wakaba 1.18 -media_type => $mt,
361     -media_subtype => $mst,
362     -parse_all => $self->{option}->{parse_all},
363     -body_default_charset => $self->{option}->{body_default_charset},
364     -body_default_charset_input => $self->{option}->{body_default_charset_input},
365 wakaba 1.30 -internal_charset_name => $self->{option}->{internal_charset_name},
366 wakaba 1.18 entity_header => $self->{header},
367     );
368     ## Media type specified option/parameters
369     if (ref $handler->[1] eq 'HASH') {
370     for (keys %{$handler->[1]}) {
371     $vopt{$_} = ${$handler->[1]}{$_};
372     }
373     }
374     ## Inherited options
375     if (ref $handler->[2] eq 'ARRAY') {
376     for (@{$handler->[2]}) {
377     $vopt{'-'.$_} = $self->{option}->{$_};
378     }
379     }
380    
381 wakaba 1.14 if ($vtype eq ':none:') {
382     return $value;
383     } elsif (defined $value) {
384     eval "require $vtype" or Carp::croak qq{<parse>: $vtype: Can't load package: $@};
385 wakaba 1.18 return $vtype->parse ($value, %vopt);
386 wakaba 1.3 } else {
387 wakaba 1.14 eval "require $vtype" or Carp::croak qq{<parse>: $vtype: Can't load package: $@};
388 wakaba 1.18 return $vtype->new (%vopt);
389 wakaba 1.3 }
390     }
391    
392 wakaba 1.14 sub _decode_body ($$) {
393     my $self = shift;
394     my $value = shift;
395     ## MIME CTE
396 wakaba 1.22 my $cte = $self->{_cte} || '';
397 wakaba 1.14 my $ctef = $self->header->field ('content-transfer-encoding',
398     -new_item_unless_exist => 0);
399     $cte = $ctef->value if ref $ctef;
400 wakaba 1.15 my $f = $Message::MIME::Encoding::DECODER{$cte};
401 wakaba 1.14 if (ref $f) {
402     ($value, $cte) = &$f ($self, $value);
403     }
404     $self->{_cte} = $cte;
405     $value;
406     }
407    
408 wakaba 1.15 sub _encode_body ($$\%) {
409 wakaba 1.14 my $self = shift;
410     my $value = shift;
411 wakaba 1.15 my $option = shift;
412 wakaba 1.14 ## MIME CTE
413 wakaba 1.17 my $current_cte = $self->{_cte} || 'binary';
414     my $ctef = $self->{header}->field ('content-transfer-encoding',
415 wakaba 1.14 -new_item_unless_exist => 0);
416 wakaba 1.15 my $cte = ''; $cte = lc $ctef->value if ref $ctef;
417 wakaba 1.16 my %enoption;
418 wakaba 1.15 ## Get media type of entity body and its accept CTE list
419     my ($mt,$mst) = $self->content_type;
420     my $mt_def = $Message::MIME::MediaType::type{$mt}->{$mst};
421     $mt_def = $Message::MIME::MediaType::type{$mt}->{'/default'}
422 wakaba 1.18 unless ref $mt_def;
423 wakaba 1.15 $mt_def = $Message::MIME::MediaType::type{'/default'}->{'/default'}
424     unless ref $mt_def;
425 wakaba 1.16 $enoption{mt_is_text} = 1
426     if $mt eq 'text' || $mt eq 'multipart' || $mt eq 'message';
427 wakaba 1.23 $enoption{mt_is_text} = 1 if $mt_def->{text_content};
428 wakaba 1.16 my ($charset, $charset_def) = '';
429     if ($mt_def->{mime_charset}) {
430     ## If CT is able to have its charset parameter,
431 wakaba 1.17 my $ct = $self->{header}->field ('content-type',
432 wakaba 1.16 -new_item_unless_exist => 0);
433     $charset = $ct->parameter ('charset') if ref $ct;
434     if ($charset) {
435     $charset_def = $Message::MIME::Charset::CHARSET{$charset};
436     } else {
437     $charset_def = $Message::MIME::Charset::CHARSET{'*default'};
438 wakaba 1.31 ## Note: 'encoding_after_encode' option's value is hardcoded.
439 wakaba 1.16 }
440 wakaba 1.18 } else { ## Don't have mime style "charset" parameter
441     $charset_def = {mime_text => 1};
442 wakaba 1.16 }
443     $charset_def = {} unless ref $charset_def; ## dummy
444 wakaba 1.31 #if ($charset_def->{mime_text} != 1) { ## See also Note above
445     if (Message::MIME::Charset::is_mime_text ($charset || '*default') != 1) {
446 wakaba 1.18 $enoption{mt_is_text} = 0 if $mt eq 'text';
447 wakaba 1.17 my $ct = $self->{header}->field ('content-type');
448     $ct->not_mime_text ($option->{text_coderange} eq 'binary'? 0:1);
449     }
450 wakaba 1.15 ## If accept CTE list is defined,
451 wakaba 1.16 for my $def ($charset_def, $mt_def) {
452     if (ref $def->{accept_cte} eq 'ARRAY') {
453     my $f = 1; for (@{$def->{accept_cte}}) {
454     if ($cte eq $_) {$f = 0; last}
455     }
456     if ($f) { ## If CTE is not accepted,
457     $cte = $def->{accept_cte}->[0];
458     }
459 wakaba 1.15 }
460     }
461     if ($current_cte eq 'binary' || ($current_cte && $current_cte ne $cte)) {
462     my $de = $Message::MIME::Encoding::DECODER{$current_cte};
463     my $en = $Message::MIME::Encoding::ENCODER{$cte || 'binary'};
464     if (ref $de && ref $en) {
465     my ($e, $decoded);
466     ($decoded, $e) = &$de ($self, $value);
467     ## Check transparent coderange
468 wakaba 1.16 my $cr = $self->Message::MIME::Encoding::decide_coderange
469     ($decoded, \%enoption);
470 wakaba 1.15 if ($option->{accept_coderange} eq '8bit') {
471     if ($cr eq 'binary') {
472 wakaba 1.16 $cte = $charset_def->{cte_7bit_preferred}
473     || $mt_def->{cte_7bit_preferred} || 'base64';
474 wakaba 1.15 $en = $Message::MIME::Encoding::ENCODER{$cte};
475     }
476     } elsif ($option->{accept_coderange} eq '7bit') {
477     if ($cr eq 'binary' || $cr eq '8bit') {
478 wakaba 1.16 $cte = $charset_def->{cte_7bit_preferred}
479     || $mt_def->{cte_7bit_preferred} || 'base64';
480 wakaba 1.15 $en = $Message::MIME::Encoding::ENCODER{$cte};
481 wakaba 1.18 if ($mt eq 'message') {
482     my $ct = $self->{header}->field ('content-type');
483     $ct->not_mime_text ($option->{text_coderange} eq 'binary'? 0:1);
484     }
485 wakaba 1.15 }
486     }
487     if ($e eq 'binary') {
488 wakaba 1.16 ($value, $e) = &$en ($self, $decoded, \%enoption);
489 wakaba 1.17 $e = '' if ($e eq $option->{cte_default});
490     $e = '' if $e eq '7bit'
491     && ( $option->{cte_default} eq '8bit'
492     || $option->{cte_default} eq 'binary');
493     $e = '' if $e eq '8bit' && $option->{cte_default} eq 'binary';
494     if ($e) {
495     $ctef = $self->{header}->field ('content-transfer-encoding')
496 wakaba 1.15 unless ref $ctef;
497     $ctef->value ($e);
498 wakaba 1.17 } elsif (ref $ctef) {
499     $ctef->value ('');
500     }
501 wakaba 1.15 } else {
502 wakaba 1.17 $ctef = $self->{header}->field ('content-transfer-encoding')
503 wakaba 1.15 unless ref $ctef;
504     $ctef->value ($current_cte);
505     }
506 wakaba 1.14 } else { ## Can't encode by given CTE
507 wakaba 1.17 $ctef = $self->{header}->field ('content-transfer-encoding')
508 wakaba 1.15 unless ref $ctef;
509 wakaba 1.14 $ctef->value ($current_cte);
510     }
511     }
512 wakaba 1.17 if (ref $ctef && $ctef->value eq '') {
513     $self->{header}->delete ('content-transfer-encoding');
514     }
515 wakaba 1.14 $value;
516     }
517    
518 wakaba 1.1 =head2 $self->stringify ([%option])
519    
520     Returns the C<message> as a string.
521    
522     =cut
523    
524     sub stringify ($;%) {
525     my $self = shift;
526 wakaba 1.8 my %params = @_;
527     my %option = %{$self->{option}};
528     for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}
529 wakaba 1.18 my ($header, $body, $body0);
530 wakaba 1.14 if (ref $self->{body}) {
531 wakaba 1.18 $self->{body}->entity_header ($self->{header});
532 wakaba 1.27 $body0 = $self->{body}->stringify (-parent_format => $option{format},
533 wakaba 1.14 -linebreak_strict => $option{linebreak_strict});
534     } else {
535 wakaba 1.18 $body0 = $self->{body};
536 wakaba 1.14 }
537 wakaba 1.18 $body = $self->_encode_body ($body0, \%option);
538 wakaba 1.14 if (ref $self->{header}) {
539 wakaba 1.15 my $ns_content = $Message::Header::NS_phname2uri{content};
540 wakaba 1.33 my $filler;
541     $filler = sub {
542     my ($hdr, $exist, $hdr_option) = @_;
543     ## Date: (RFC 822, HTTP)
544     if ($option{fill_date}
545 wakaba 1.34 && !$exist->{$option{fill_date_name}.':'.$option{fill_date_ns}}) {
546     $hdr->field
547 wakaba 1.33 ($option{fill_date_name}, -ns => $option{fill_date_ns})->unix_time (time);
548     }
549     ## Message-ID: (RFC 822)
550     if ($option{fill_msgid}
551 wakaba 1.34 && !$exist->{$option{fill_msgid_name}.':'.$option{fill_msgid_ns}}) {
552     my $from = $hdr->field
553 wakaba 1.33 ('from', -ns => $option{fill_msgid_from_ns}, -new_item_unless_exist => 0);
554     $from = $from->addr_spec if ref $from;
555 wakaba 1.34 $hdr->field ($option{fill_msgid_name}, -ns => $option{fill_msgid_ns})
556     ->generate (addr_spec => $from) if $from;
557 wakaba 1.33 } # fill_msgid
558     ## To:, CC:, BCC:, Resent-To:, Resent-Cc:, Resent-Bcc: (RFC 822)
559     if ($option{fill_destination}) {
560 wakaba 1.34 if ( !$exist->{ 'to:'.$option{fill_destination_ns} }
561     && !$exist->{ 'cc:'.$option{fill_destination_ns} }
562     && !$exist->{ 'bcc:'.$option{fill_destination_ns} }
563     && !$exist->{ 'to:'.$option{fill_destination_resent_ns} }
564     && !$exist->{ 'cc:'.$option{fill_destination_resent_ns} }
565     && !$exist->{ 'bcc:'.$option{fill_destination_resent_ns} } ) {
566 wakaba 1.33 $hdr->add (bcc => '');
567     }
568     }
569     ## From:, Sender:
570     if ($option{fill_source}) {
571     ## From:
572 wakaba 1.34 if (!$exist->{ 'from:'.$option{fill_from_ns} }) {
573 wakaba 1.33 $hdr->add (from => 'Unknown source <[email protected]>',
574     -ns => $option{fill_from_ns});
575     ## From: exists, Sender: not exist
576 wakaba 1.34 } elsif (!$exist->{ 'sender:'.$option{fill_sender_ns} }) {
577 wakaba 1.33 my $from = $hdr->field ('from', -ns => $option{fill_from_ns});
578     if ($from->count > 1) {
579     $hdr->field ('sender', -ns => $option{fill_sender_ns})
580     ->add ($from->item (0, -by => 'index'));
581     }
582     }
583     }
584     ## Content-MD5:
585 wakaba 1.34 if (($option{fill_md5} && !$exist->{ $option{fill_md5_name} .':'. $ns_content})
586     || ($option{recalc_md5} && $exist->{ $option{fill_md5_name} .':'. $ns_content})) {
587 wakaba 1.33 my $md5;
588     eval q{
589 wakaba 1.34 require Digest::MD5;
590     $md5 = ($self->Message::MIME::Encoding::encode_base64 (Digest::MD5::md5 ($body0)))[0];
591 wakaba 1.33 $md5 =~ tr/\x09\x0A\x0D\x20//d;
592 wakaba 1.34 1} or Carp::carp $@;
593 wakaba 1.33 if ($md5) {
594 wakaba 1.34 my $md5f = $hdr->field ($option{fill_md5_name}, -ns => $ns_content);
595 wakaba 1.33 $md5f->value ($md5);
596     }
597 wakaba 1.24 }
598 wakaba 1.33 my $ismime = 0;
599 wakaba 1.34 for (keys %$exist) {if (/:$ns_content$/) { $ismime = 1; last }}
600 wakaba 1.33 unless ($ismime) {
601 wakaba 1.34 $ismime = 1 if $option{force_mime_entity}
602     || $option{fill_md5}
603     || $option{body_default_media_type} ne 'text'
604     || $option{body_default_media_subtype} ne 'plain';
605 wakaba 1.15 }
606 wakaba 1.33 if ($ismime) {
607     ## Content-Type: (MIME, HTTP)
608 wakaba 1.34 if ($option{fill_ct} && !$exist->{'type:'.$ns_content}) {
609     my $ct = $hdr->field ('type', -ns => $ns_content);
610 wakaba 1.33 $ct->media_type ($option{body_default_media_type}.'/'
611     .$option{body_default_media_subtype});
612     $ct->replace (Message::MIME::Charset::name_minimumize ($option{body_default_charset} => $body0));
613     }
614     ## MIME-Version: (MIME)
615     if ($option{fill_mimever}
616 wakaba 1.34 && !$exist->{'mime-version:'.$option{fill_mimever_ns}}) {
617     ## BUG: doesn't support rfc1049, HTTP (ie. non-MIME) content-*: fields
618     $hdr->add ('mime-version' => '1.0',
619 wakaba 1.33 -parse => 0, -ns => $option{fill_mimever_ns});
620     }
621     } # $ismime
622     ## User-Agent: (USEFOR, HTTP)
623     if ($option{add_ua}) {
624 wakaba 1.34 $hdr->field ($option{fill_ua_name})->add_our_name (
625 wakaba 1.33 -use_Config => $option{ua_use_Config},
626     -use_Win32 => $option{ua_use_Win32},
627 wakaba 1.39 -date => q$Date: 2002/11/13 08:08:51 $,
628 wakaba 1.33 );
629 wakaba 1.8 }
630 wakaba 1.33 } if $option{fill_missing_fields};
631    
632     if ($option{format} =~ /uri-url-mailto/
633     && $self->{header}->field_exist ('type', -ns => $ns_content)
634 wakaba 1.12 && $option{uri_mailto_safe_level} > 1) {
635 wakaba 1.33 $self->{header}->field ('type', -ns => $ns_content)->media_type ('text/plain');
636 wakaba 1.12 }
637 wakaba 1.33 $header = $self->{header}->stringify (
638     -format => $option{format},
639     -linebreak_strict => $option{linebreak_strict},
640     -uri_mailto_safe_level => $option{uri_mailto_safe_level},
641     ($filler? (-hook_stringify_fill_fields => $filler) :()),
642     );
643 wakaba 1.7 } else {
644     $header = $self->{header};
645 wakaba 1.12 unless ($option{linebreak_strict}) {
646     ## bare \x0D and bare \x0A are unsafe
647     $header =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g;
648     $header =~ s/\x0A(?=[^\x09\x20])/\x0A\x20/g;
649     }
650 wakaba 1.7 }
651 wakaba 1.12 if ($option{format} =~ /uri-url-mailto/) {
652 wakaba 1.14 if ($option{format} =~ /rfc1738/) {
653     my $to = $self->{header}->stringify (-format => $option{format},
654     -uri_mailto_safe_level => $option{uri_mailto_safe_level});
655     $to? 'mailto:'.$to: '';
656     } else {
657     my $f = $option{format}; $f =~ s/-mailto/-mailto-to/;
658     my $to = $self->{header}->stringify (-format => $f,
659     -uri_mailto_safe_level => $option{uri_mailto_safe_level});
660     $body =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge;
661     if (length $body) {
662     $header .= '&' if $header;
663     $header .= 'body='.$body;
664     }
665     $header = '?'.$header if $header;
666     $to||$header? 'mailto:'.$to.$header: '';
667 wakaba 1.12 }
668     } else {
669 wakaba 1.37 ## Magic line (<- named by the author of this module:-)
670     my $mline;
671     if ($option{use_magic_line} && $option{output_magic_line}) {
672     if ($option{format} =~ /mail|news/ && ref $self->{header}) {
673     my $mfrom = $self->{header}->field ('mail-from', -new_item_unless_exist=>0);
674     $mline = sprintf 'From %s', $mfrom->value if ref $mfrom;
675     } elsif ($option{format} =~ /http.+?request/) {
676     $mline = sprintf '%s %s %s/%vd', $self->{http_method} || 'GET',
677     $self->{http_request_uri} || '/', $self->{http_protocol_name} || 'HTTP',
678     $self->{http_protocol_version} || v1.0;
679     } elsif ($option{format} =~ /http.+?response/ && ref $self->{header}
680     && $option{format} !~ /cgi/) {
681     my $s = $self->{header}->field ('x-http-status');
682     $mline = sprintf '%s/%vd %s', $self->{http_protocol_name} || 'HTTP',
683     $self->{http_protocol_version} || v1.0, $s->value || '200 OK';
684     }
685     }
686     $mline .= "\x0D\x0A" if $mline;
687     $mline = '' unless $mline;
688    
689 wakaba 1.15 $header .= "\x0D\x0A" if $header && $header !~ /\x0D\x0A$/;
690 wakaba 1.37 $mline.$header."\x0D\x0A".$body;
691 wakaba 1.12 }
692 wakaba 1.1 }
693 wakaba 1.8 *as_string = \&stringify;
694 wakaba 1.1
695    
696 wakaba 1.9 =head1 SHORTCUT METHOD FOR MESSAGE PROPERTIES
697    
698     =over 4
699    
700     =item $self->content_type ([%options])
701 wakaba 1.1
702 wakaba 1.9 Returns Internet media type of message body
703     (aka MIME type, content type). Only media type
704     (type/subtype pair) is returned, i.e. no parameter
705     is returned, if any. To get such value, or to set
706     new value, use C<field> method.
707 wakaba 1.1
708 wakaba 1.9 Default is C<text/plain>.
709 wakaba 1.3
710 wakaba 1.9 Example:
711 wakaba 1.3
712 wakaba 1.9 $msg->field ('Content-Type')->media_type ('text/html');
713     print $msg->content_type; ## text/html
714 wakaba 1.3
715     =cut
716    
717     sub content_type ($;%) {
718 wakaba 1.8 my $self = shift;
719 wakaba 1.15 my $ct = $self->{header}->field ('content-type', -new_item_unless_exist => 0);
720 wakaba 1.28 my ($mt, $mst);
721 wakaba 1.15 unless (ref $ct) {
722 wakaba 1.28 $mt = $self->{option}->{body_default_media_type};
723     $mst = $self->{option}->{body_default_media_subtype};
724     if ($mt ne 'text' || $mst ne 'plain') {
725 wakaba 1.18 $ct = $self->{header}->field ('content-type');
726 wakaba 1.28 $ct->media_type_major ($mt);
727     $ct->media_type_minor ($mst);
728     }
729 wakaba 1.37 if ($self->{option}->{guess_media_type} && $self->{body} && !ref $self->{body}
730     && $self->{option}->{format} =~ /mail|news/) {
731 wakaba 1.29 if ($self->{body} =~ /^-----BEGIN PGP SIGNED MESSAGE-----\x0D?$/m
732     && $self->{body} =~ /^-----BEGIN PGP SIGNATURE-----\x0D?$/m
733     && $self->{body} =~ /^-----END PGP SIGNATURE-----\x0D?$/m) {
734     $ct = $self->{header}->field ('content-type') unless ref $ct;
735     $mt = $ct->media_type_major ('text');
736     $mst = $ct->media_type_minor ('x-pgp-cleartext-signed');
737     } elsif ($self->{body} =~ /^-----BEGIN PGP [A-Z\x20]+-----\x0D?$/m
738     && $self->{body} =~ /^-----END PGP [A-Z\x20]+-----\x0D?$/m) {
739     $ct = $self->{header}->field ('content-type') unless ref $ct;
740     $mt = $ct->media_type_major ('application');
741     $mst = $ct->media_type_minor ('pgp');
742     $ct->parameter (format => 'text');
743     } elsif ($self->{body} =~ /^-+ start of forwarded message \(RFC 934 encapsulation\) -+\x0D?$/m) {
744     $ct = $self->{header}->field ('content-type') unless ref $ct;
745     $mt = $ct->media_type_major ('text');
746     $mst = $ct->media_type_minor ('x-message-rfc934');
747     } elsif ($self->{body} =~ /^-{70,70}\x0D?$/m
748     && $self->{body} =~ /^-{30,30}\x0D?$/m
749     && $self->{body} =~ /\x0D?\x0A-{30,30}\x0D?\x0A\x0D?\x0AEnd of.+?Digest.*?\x0D?\x0A\*+(?:\x0D?\x0A)*$/s) {
750     $ct = $self->{header}->field ('content-type') unless ref $ct;
751     $mt = $ct->media_type_major ('text');
752     $mst = $ct->media_type_minor ('x-message-rfc1153');
753     } elsif ($self->{body} =~ /^-----PRIVACY-ENHANCED MESSAGE BOUNDARY-----\x0D?$/m) {
754     $ct = $self->{header}->field ('content-type') unless ref $ct;
755     $mt = $ct->media_type_major ('text');
756     $mst = $ct->media_type_minor ('x-message-pem');
757 wakaba 1.37 } elsif ($self->{body} =~ /^#(?:HELO|EHLO)/mi
758     && $self->{body} =~ /^#MAIL FROM:/mi
759     && $self->{body} !~ /^[^#]/m) {
760     ## RFC 976 Batch SMTP (BSMTP) message
761     $ct = $self->{header}->field ('content-type') unless ref $ct;
762     $mt = $ct->media_type_major ('application');
763     $mst = $ct->media_type_minor ('x-batch-smtp');
764 wakaba 1.29 }
765 wakaba 1.28 }
766     } else {
767     ($mt, $mst) = ($ct->media_type_major, $ct->media_type_minor);
768     }
769     if ($self->{option}->{guess_media_type}) {
770     if ($mt eq 'text' && $mst eq 'plain') {
771     my $mls = $self->{header}->field ('x-mlserver', -new_item_unless_exist => 0);
772     if (ref $mls && $mls =~ /fml/) {
773     my $s = $self->{header}->field ('subject', -new_item_unless_exist => 0);
774     if (index ($s, 'RFC934(mh-burst)') >= 0) {
775     $ct = $self->{header}->field ('content-type') unless ref $ct;
776 wakaba 1.29 $mt = $ct->media_type_major ('text');
777     $mst = $ct->media_type_minor ('x-message-rfc934');
778     $ct->delete ('charset');
779     } elsif (index ($s, 'Digest (RFC1153)') >= 0) {
780     $ct = $self->{header}->field ('content-type') unless ref $ct;
781     $mt = $ct->media_type_major ('text');
782     $mst = $ct->media_type_minor ('x-message-rfc1153');
783 wakaba 1.28 $ct->delete ('charset');
784     }
785     }
786 wakaba 1.18 }
787 wakaba 1.15 }
788     if (wantarray) {
789 wakaba 1.28 ($mt, $mst);
790 wakaba 1.15 } else {
791 wakaba 1.38 ref $ct ? $ct->media_type : sprintf '%s/%s', $mt, $mst;
792 wakaba 1.15 }
793 wakaba 1.1 }
794 wakaba 1.15 *media_type = \&content_type;
795 wakaba 1.1
796 wakaba 1.9 =item $self->id
797    
798     Returns ID of message entity. If there are C<Message-ID:>
799     field, its value is returned. Unless, but there are
800     C<Content-ID:> field, it is returned. Without both of
801     fields, C<""> is returned.
802    
803     =cut
804    
805 wakaba 1.5 sub id ($) {
806     my $self = shift;
807     return scalar $self->{header}->field ('message-id')->id
808     if $self->{header}->field_exist ('message-id');
809 wakaba 1.9 return scalar $self->{header}->field ('content-id')->id
810     if $self->{header}->field_exist ('content-id');
811 wakaba 1.8 '';
812 wakaba 1.5 }
813    
814 wakaba 1.35 sub sender ($;%) {
815     my $self = shift;
816     my %params = @_;
817     my %option = %{$self->{option}};
818     $option{use_x_envelope_from} = 1; ## X-Envelope-From:
819     $option{use_normal} = 1; ## From:, Sender:
820     $option{use_resent} = 1; ## Resent-From:, Resent-Sender:
821     my $hdr = $self->{header};
822     if ($option{use_x_envelope_from} && $hdr->field_exist ('x-envelope-from')) {
823     return $hdr->field ('x-envelope-from')->addr_spec;
824     } elsif ($option{use_resent} && $hdr->field_exist ('resent-sender')) {
825     ## TODO: Resent block support
826     return $hdr->field ('resent-sender')->addr_spec;
827     } elsif ($option{use_resent} && $hdr->field_exist ('resent-from')) {
828     ## TODO: Resent block support
829     return $hdr->field ('resent-from')->addr_spec;
830     } elsif ($option{use_normal} && $hdr->field_exist ('sender')) {
831     return $hdr->field ('sender')->addr_spec;
832     } elsif ($option{use_normal} && $hdr->field_exist ('from')) {
833     return $hdr->field ('from')->addr_spec;
834     }
835     undef;
836     }
837    
838     sub destination ($;%) {
839     my $self = shift;
840     my %params = @_;
841     my %option = %{$self->{option}};
842     $option{use_x_envelope_to} = 1; ## X-Envelope-To:
843     $option{use_normal} = 1; ## To:, Cc:, Bcc:
844     $option{use_resent} = 1; ## Resent-To:, Resent-Cc:, Resent-Bcc:
845     for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}
846     my $hdr = $self->{header};
847     my @to;
848 wakaba 1.37 if ($option{use_x_envelope_to} && $hdr->field_exist ('x-rfc822-x-envelope-to')) {
849     @to = $hdr->field ('x-rfc822-x-envelope-to')->addr_spec;
850     } elsif ($option{use_resent} && $hdr->field_exist ('x-rfc822-resent-from')) {
851 wakaba 1.35 ## TODO: Resent block support
852 wakaba 1.37 @to = ($hdr->field ('x-rfc822-resent-to')->addr_spec,
853     $hdr->field ('x-rfc822-resent-cc')->addr_spec,
854     $hdr->field ('x-rfc822-resent-bcc')->addr_spec);
855 wakaba 1.35 } elsif ($option{use_normal}) {
856 wakaba 1.37 @to = ($hdr->field ('x-rfc822-to')->addr_spec,
857     $hdr->field ('x-rfc822-cc')->addr_spec,
858     $hdr->field ('x-rfc822-bcc')->addr_spec);
859 wakaba 1.35 }
860     @to;
861     }
862    
863 wakaba 1.36 sub list_name ($) {
864     my $self = shift;
865     my $hdr = $self->{header};
866     if ($hdr->field_exist ('list-id')) {
867     my $lname = $hdr->field ('list-id')->display_name;
868     return $lname if length $lname;
869     }
870     my $v = sub {
871     my $name = shift;
872     my $f = $hdr->field ($name, -new_item_unless_exist => 0);
873     return $f->value if ref $f;
874     undef;
875     };
876     ## BUG: list-name "0" is not supported.
877     my $lname = &$v ('x-ml-name') || &$v ('x-mailing-list-name')
878     || &$v ('x-mailinglist-name') || &$v ('ml-name');
879     return $lname if length $lname;
880     if ($hdr->field_exist ('x-sequence')) {
881     if ($hdr->field ('x-sequence')->value =~ /^(\S+)\s+\d+$/) {
882     return $1;
883     }
884     }
885     if ($hdr->field_exist ('subject')) {
886 wakaba 1.37 my $s = $hdr->field ('subject');
887     return $s->list_name if $s->method_available ('list_name');
888 wakaba 1.36 }
889     undef;
890     }
891    
892     sub list_count ($) {
893     my $self = shift;
894     my $hdr = $self->{header};
895     my $v = sub {
896     my $name = shift;
897     my $f = $hdr->field ($name, -new_item_unless_exist => 0);
898     return $f->value if ref $f;
899     undef;
900     };
901     ## BUG: list-count 0 is not supported.
902     my $lc = &$v ('x-mail-count') || &$v ('x-ml-count') || &$v ('x-mailinglist-id')
903     || &$v ('mail-count') || &$v ('x-article-no') || &$v ('x-ml-counter')
904     || &$v ('x-ml-id') || &$v ('x-ml-sequence') || &$v ('x-serial-no')
905     || &$v ('x-seqno');
906     return $lc if $lc;
907     if ($hdr->field_exist ('x-sequence')) {
908 wakaba 1.38 my $s = $hdr->field ('x-sequence')->value;
909     if ($s =~ /^\S+\s+(\d+)$/) {
910     return $1;
911     } elsif ($s =~ /(\d+)/) {
912 wakaba 1.36 return $1;
913     }
914     }
915     if ($hdr->field_exist ('subject')) {
916 wakaba 1.37 my $s = $hdr->field ('subject');
917     return $s->list_count if $s->method_available ('list_count');
918 wakaba 1.36 }
919     undef;
920     }
921    
922 wakaba 1.9 =back
923    
924     =head1 MISC. METHODS
925    
926     =over 4
927    
928     =item $self->option ( $option-name / $option-name, $option-value, ...)
929    
930     If @_ == 1, returns option value. Else...
931    
932     Set option value. You can pass multiple option name-value pair
933     as parameter. Example:
934    
935     $msg->option (format => 'mail-rfc822',
936     capitalize => 0);
937     print $msg->option ('format'); ## mail-rfc822
938    
939     =cut
940    
941     sub option ($@) {
942     my $self = shift;
943     if (@_ == 1) {
944     return $self->{option}->{ $_[0] };
945     }
946 wakaba 1.18 my %option = @_;
947 wakaba 1.9 while (my ($name, $value) = splice (@_, 0, 2)) {
948     $self->{option}->{$name} = $value;
949     }
950 wakaba 1.32 if ($option{-recursive} && ($self->content_type)[0] ne 'message') {
951 wakaba 1.18 $self->{header}->option (%option);
952     $self->{body}->option (%option) if ref $self->{body};
953     }
954 wakaba 1.9 }
955    
956     =item $self->clone ()
957 wakaba 1.8
958     Returns a copy of Message::Entity object.
959    
960     =cut
961    
962     sub clone ($) {
963     my $self = shift;
964     my $clone = new Message::Entity;
965 wakaba 1.14 $clone->{option} = Message::Util::make_clone ($self->{option});
966     for (@{$self->{option}->{_MEMBERS}}) {
967     $clone->{$_} = Message::Util::make_clone ($self->{$_});
968 wakaba 1.8 }
969     $clone;
970     }
971 wakaba 1.5
972 wakaba 1.14 my %_method_default_list = qw(new 1 parse 1 stringify 1 option 1 clone 1 method_available 1);
973     sub method_available ($$) {
974     my $self = shift;
975     my $name = shift;
976     return 1 if $_method_default_list{$name};
977     for (@{$self->{option}->{_METHODS}}) {
978     return 1 if $_ eq $name;
979     }
980     0;
981     }
982    
983 wakaba 1.30 sub import ($;%) {
984     my $self = shift;
985     my %option = @_;
986     for (keys %option) {
987     $DEFAULT{$_} = $option{$_};
988     }
989     if ($option{-body_default_charset} && !$option{-body_default_charset_input}) {
990     $DEFAULT{-body_default_charset_input} = $option{-body_default_charset};
991     }
992     if ($option{-header_default_charset} && !$option{-header_default_charset_input}) {
993     $DEFAULT{-header_default_charset_input} = $option{-header_default_charset};
994     }
995     }
996    
997 wakaba 1.9 =back
998    
999 wakaba 1.11 =head1 C<format>
1000    
1001     =over 2
1002    
1003     =item mail-rfc650
1004    
1005     Internet mail message, defined by IETF RFC 650
1006    
1007     =item mail-rfc724
1008    
1009     Internet mail message, defined by IETF RFC 724
1010    
1011     =item mail-rfc733
1012    
1013     Internet mail message, defined by IETF RFC 733
1014    
1015     =item mail-rfc822
1016    
1017     Internet mail message, defined by IETF RFC 822
1018    
1019 wakaba 1.35 =item mail-rfc822+rfc1123
1020    
1021     Internet mail message, defined by IETF RFC 822, ammended by RFC 1123
1022    
1023 wakaba 1.11 =item mail-rfc2822
1024    
1025     Internet mail message, defined by IETF RFC 2822
1026    
1027     =item mime-1.0
1028    
1029     MIME entity
1030    
1031     =item mime-1.0-rfc1341
1032    
1033     MIME entity, defined by RFC 1341 (and RFC 1342)
1034    
1035     =item mime-1.0-rfc1521
1036    
1037     MIME entity, defined by RFC 1521 and 1522
1038    
1039     =item mime-1.0-rfc2045
1040    
1041     MIME entity, defined by RFC 2045,..., 2049
1042    
1043     =item news-bnews
1044    
1045     Usenet Bnews format
1046    
1047     =item news-rfc850
1048    
1049     Usenet news format, defined by IETF RFC 850
1050    
1051     =item news-rfc1036
1052    
1053     Usenet news format, defined by IETF RFC 1036
1054    
1055     =item news-son-of-rfc1036
1056    
1057     Usenet news format, defined by son-of-RFC1036
1058    
1059     =item news-usefor
1060    
1061     Usenet news format, defined by usefor-article (IETF Internet Draft)
1062    
1063     =item http-1.0-rfc1945
1064    
1065     HTTP/1.0 message, defined by IETF RFC 1945
1066    
1067     =item http-1.0-rfc1945-request
1068    
1069     HTTP/1.0 request message, defined by IETF RFC 1945
1070    
1071     =item http-1.0-rfc1945-response
1072    
1073     HTTP/1.0 response message, defined by IETF RFC 1945
1074    
1075     =item http-1.1-rfc2068
1076    
1077     HTTP/1.1 message, defined by IETF RFC 2068
1078    
1079     =item http-1.1-rfc2068-request
1080    
1081     HTTP/1.1 request message, defined by IETF RFC 2068
1082    
1083     =item http-1.1-rfc2068-response
1084    
1085     HTTP/1.1 response message, defined by IETF RFC 2068
1086    
1087     =item http-1.1-rfc2616
1088    
1089     HTTP/1.1 message, defined by IETF RFC 2616
1090    
1091     =item http-1.1-rfc2616-request
1092    
1093     HTTP/1.1 request message, defined by IETF RFC 2616
1094    
1095     =item http-1.1-rfc2616-response
1096    
1097     HTTP/1.1 response message, defined by IETF RFC 2616
1098    
1099     =item http-cgi-1.1
1100    
1101     CGI/1.1 output (for HTTP), defined by coar-cgi-v11 (IETF Internet Draft)
1102    
1103     =item http-cgi-1.2
1104    
1105     CGI/1.2 output, defined by coar-cgi-v12 (to be IETF Internet Draft)
1106    
1107     =item http-sip-2.0
1108    
1109     SIP/2.0 message, defined by IETF RFC 2543
1110    
1111     =item http-sip-2.0-request
1112    
1113     SIP/2.0 request message, defined by IETF RFC 2543
1114    
1115     =item http-sip-2.0-response
1116    
1117     SIP/2.0 response message, defined by IETF RFC 2543
1118    
1119     =item http-sip-cgi
1120    
1121     SIP/2.0 CGI (IETF Internet Draft)
1122    
1123     =item cpim-1.0
1124    
1125     CPIM/1.0 (IETF Internet Draft)
1126    
1127 wakaba 1.35 =item uri-url-mailto
1128 wakaba 1.12
1129     mailto: URL scheme
1130    
1131     =item uri-url-mailto-rfc1738
1132    
1133     mailto: URL scheme (defined by RFC 1738)
1134    
1135 wakaba 1.35 =item uri-url-mailto-rfc2368
1136 wakaba 1.12
1137     mailto: URL scheme (defined by RFC 2368)
1138    
1139 wakaba 1.35 =item uri-url-mailto-to
1140 wakaba 1.12
1141     C<to> part of mailto: URL scheme (for internal use only)
1142    
1143 wakaba 1.11 =back
1144    
1145 wakaba 1.1 =head1 EXAMPLE
1146    
1147     use Message::Entity;
1148 wakaba 1.9 my $msg = new Message::Entity From => '[email protected]',
1149     Subject => 'Example message',
1150     To => '[email protected]',
1151     -format => 'mail-rfc2822',
1152     body => $body;
1153 wakaba 1.1 $msg->header ($header);
1154     $msg->body ($body);
1155     print $msg;
1156    
1157 wakaba 1.3 =head1 SEE ALSO
1158    
1159     Message::* Perl modules
1160     <http://suika.fam.cx/~wakaba/Message-pm/>
1161    
1162 wakaba 1.1 =head1 LICENSE
1163    
1164 wakaba 1.39 Copyright 2002 Wakaba <[email protected]>.
1165 wakaba 1.1
1166     This program is free software; you can redistribute it and/or modify
1167     it under the terms of the GNU General Public License as published by
1168     the Free Software Foundation; either version 2 of the License, or
1169     (at your option) any later version.
1170    
1171     This program is distributed in the hope that it will be useful,
1172     but WITHOUT ANY WARRANTY; without even the implied warranty of
1173     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1174     GNU General Public License for more details.
1175    
1176     You should have received a copy of the GNU General Public License
1177     along with this program; see the file COPYING. If not, write to
1178     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1179     Boston, MA 02111-1307, USA.
1180    
1181     =head1 CHANGE
1182    
1183     See F<ChangeLog>.
1184 wakaba 1.39 $Date: 2002/11/13 08:08:51 $
1185 wakaba 1.1
1186     =cut
1187    
1188     1;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24