/[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.15 - (hide annotations) (download)
Wed May 29 11:05:53 2002 UTC (24 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.14: +163 -81 lines
2002-05-29  wakaba <w@suika.fam.cx>

	* Entity.pm:
	- (%ENCODE, %DECODE): Removed.  (Moved to Message::MIME::Encoding)
	- Uses \x0D\x0A instead of \n.  (Temporary.  `linebreak-strict'
	and related options should be implemented /strictly/.)
	- (_encode_body): Checks media type to choose CTE.
	- (accept_coderange): New option.
	- (body_default_media_type, body_default_charset): Likewise.
	- (fill_ct): Likewise.
	- Uses %Message::Header::NS_phname2uri instead of direct
	access to namespace definition package.
	- (content_type): Returns (type, subtype) list when wantarray'ed.
	- (media_type): New method.  (Alias of content_type)
	* Header.pm (_replace_hash_shift): Checks namespace of field name.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24