/[suikacvs]/test/cvs
Suika

Contents of /test/cvs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.27 - (show annotations) (download)
Sun Jun 23 12:20:11 2002 UTC (21 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.26: +13 -5 lines
2002-06-23  Wakaba <w@suika.fam.cx>

	* Entity.pm (header_default_charset, header_default_charset_input):
	New options.
	* Header.pm (ditto): Likewise.
	* Util.pm
	- (angle_quoted, angle_qcontent, M_angle_quoted): New regexs
	(Moved from Message::Field::AngleQuoted).
	- (decode_ccontent): Order of arguments of 
	Message::MIME::EncodedWord::decode_ccontent is changed.
	- (unquote_if_angle_quoted): New function.

1
2 =head1 NAME
3
4 Message::Header --- A Perl Module for Internet Message Headers
5
6 =cut
7
8 package Message::Header;
9 use strict;
10 use vars qw(%DEFAULT @ISA %REG $VERSION);
11 $VERSION=do{my @r=(q$Revision: 1.27 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
12 require Message::Field::Structured; ## This may seem silly:-)
13 push @ISA, qw(Message::Field::Structured);
14
15 %REG = %Message::Util::REG;
16 $REG{M_field} = qr/^([^\x3A]+):$REG{FWS}([\x00-\xFF]*)$/;
17 $REG{M_fromline} = qr/^\x3E?From$REG{WSP}+([\x00-\xFF]*)$/;
18 $REG{ftext} = qr/[\x21-\x39\x3B-\x7E]+/; ## [2]822
19 $REG{NON_ftext} = qr/[^\x21-\x39\x3B-\x7E]/; ## [2]822
20 $REG{NON_ftext_usefor} = qr/[^0-9A-Za-z-]/; ## name-character
21 $REG{NON_ftext_http} = $REG{NON_http_token};
22
23 ## Namespace support
24 our %NS_phname2uri; ## PH-namespace name -> namespace URI
25 our %NS_uri2phpackage; ## namespace URI -> PH-package name
26 require Message::Header::Default; ## Default namespace
27
28 ## Initialize of this class -- called by constructors
29 %DEFAULT = (
30 -_HASH_NAME => 'value',
31 -_METHODS => [qw|field field_exist field_type add replace count delete subject id is|],
32 -_MEMBERS => [qw|value|],
33 -_VALTYPE_DEFAULT => ':default',
34 -by => 'name', ## (Reserved for method level option)
35 -field_format_pattern => '%s: %s',
36 -field_name_case_sensible => 0,
37 -field_name_unsafe_rule => 'NON_ftext',
38 -field_name_validation => 1, ## Method level option.
39 -field_sort => 0,
40 #-format => 'mail-rfc2822',
41 -header_default_charset => 'iso-2022-int-1',
42 -header_default_charset_input => 'iso-2022-int-1',
43 -linebreak_strict => 0, ## Not implemented completely
44 -line_length_max => 60, ## For folding
45 #ns_default_phuri
46 -output_bcc => 0,
47 -output_folding => 1,
48 -output_mail_from => 0,
49 #-parse_all => 0,
50 -translate_underscore => 1,
51 #-uri_mailto_safe
52 -uri_mailto_safe_level => 4,
53 -use_folding => 1,
54 #-value_type
55 );
56
57 $DEFAULT{-value_type} = {
58 ':default' => ['Message::Field::Unstructured'],
59
60 p3p => ['Message::Field::Params'],
61 link => ['Message::Field::ValueParams'],
62
63 'user-agent' => ['Message::Field::UA'],
64 server => ['Message::Field::UA'],
65 };
66 for (qw(date expires))
67 {$DEFAULT{-value_type}->{$_} = ['Message::Field::Date']}
68 for (qw(accept accept-charset accept-encoding accept-language uri))
69 {$DEFAULT{-value_type}->{$_} = ['Message::Field::CSV']}
70 for (qw(location referer))
71 {$DEFAULT{-value_type}->{$_} = ['Message::Field::URI']}
72
73 my %header_goodcase = (
74 'article-i.d.' => 'Article-I.D.',
75 etag => 'ETag',
76 'pics-label' => 'PICS-Label',
77 te => 'TE',
78 url => 'URL',
79 'www-authenticate' => 'WWW-Authenticate',
80 );
81
82 ## taken from L<HTTP::Header>
83 # "Good Practice" order of HTTP message headers:
84 # - General-Headers
85 # - Request-Headers
86 # - Response-Headers
87 # - Entity-Headers
88 # (From draft-ietf-http-v11-spec-rev-01, Nov 21, 1997)
89 my @header_order = qw(
90 mail-from x-envelope-from relay-version path status
91
92 cache-control connection date pragma transfer-encoding upgrade trailer via
93
94 accept accept-charset accept-encoding accept-language
95 authorization expect from host
96 if-modified-since if-match if-none-match if-range if-unmodified-since
97 max-forwards proxy-authorization range referer te user-agent
98
99 accept-ranges age location proxy-authenticate retry-after server vary
100 warning www-authenticate
101
102 mime-version
103 allow content-base content-encoding content-language content-length
104 content-location content-md5 content-range content-type
105 etag expires last-modified content-style-type content-script-type
106 link
107
108 xref
109 );
110 my %header_order;
111
112 =head1 CONSTRUCTORS
113
114 The following methods construct new C<Message::Header> objects:
115
116 =over 4
117
118 =cut
119
120 sub _init ($;%) {
121 my $self = shift;
122 my %options = @_;
123 my $DEFAULT = Message::Util::make_clone (\%DEFAULT);
124 $self->SUPER::_init (%$DEFAULT, %options);
125 $self->{value} = [];
126 $self->_ns_load_ph ('default');
127 $self->_ns_load_ph ('rfc822');
128 $self->{option}->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'rfc822'}
129 unless $self->{option}->{ns_default_phuri};
130
131 my @new_fields = ();
132 for my $name (keys %options) {
133 unless (substr ($name, 0, 1) eq '-') {
134 push @new_fields, ($name => $options{$name});
135 }
136 }
137 $self->_init_by_format ($self->{option}->{format}, $self->{option});
138 # Make alternative representations of @header_order. This is used
139 # for sorting.
140 my $i = 1;
141 for (@header_order) {
142 $header_order{$_} = $i++ unless $header_order{$_};
143 }
144
145 $self->add (@new_fields, -parse => $self->{option}->{parse_all})
146 if $#new_fields > -1;
147 }
148
149 sub _init_by_format ($$\%) {
150 my $self = shift;
151 my ($format, $option) = @_;
152 if ($format =~ /cgi/) {
153 unshift @header_order, qw(content-type location);
154 $option->{field_sort} = 'good-practice';
155 $option->{use_folding} = 0;
156 } elsif ($format =~ /http/) {
157 $option->{field_sort} = 'good-practice';
158 }
159 if ($format =~ /uri-url-mailto/) {
160 $option->{output_bcc} = 0;
161 $option->{field_format_pattern} = '%s=%s';
162 $option->{output_folding} = sub {
163 $_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge;
164 $_[1];
165 }; ## Yes, this is not folding!
166 }
167 }
168
169 =item $msg = Message::Header->new ([%initial-fields/options])
170
171 Constructs a new C<Message::Headers> object. You might pass some initial
172 C<field-name>-C<field-body> pairs and/or options as parameters to the constructor.
173
174 Example:
175
176 $hdr = new Message::Headers
177 Date => 'Thu, 03 Feb 1994 00:00:00 +0000',
178 Content_Type => 'text/html',
179 Content_Location => 'http://www.foo.example/',
180 -format => 'mail-rfc2822' ## not to be header field
181 ;
182
183 =cut
184
185 ## Inherited
186
187 =item $msg = Message::Header->parse ($header, [%initial-fields/options])
188
189 Parses given C<header> and constructs a new C<Message::Headers>
190 object. You might pass some additional C<field-name>-C<field-body> pairs
191 or/and initial options as parameters to the constructor.
192
193 =cut
194
195 sub parse ($$;%) {
196 my $class = shift;
197 my $header = shift;
198 my $self = bless {}, $class;
199 $self->_init (@_); ## BUG: don't check linebreak_strict
200 $header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding};
201 for my $field (split /\x0D?\x0A/, $header) {
202 if ($field =~ /$REG{M_fromline}/) {
203 my ($s,undef,$value) = $self->_value_to_arrayitem
204 ('mail-from' => $1, $self->{option});
205 push @{$self->{value}}, $value if $s;
206 } elsif ($field =~ /$REG{M_field}/) {
207 my ($name, $body) = ($1, $2);
208 $body =~ s/$REG{WSP}+$//;
209 my ($s,undef,$value) = $self->_value_to_arrayitem
210 ($name => $body, $self->{option});
211 push @{$self->{value}}, $value if $s;
212 } elsif (length $field) {
213 my ($s,undef,$value) = $self->_value_to_arrayitem
214 ('x-unknown' => $field, $self->{option});
215 push @{$self->{value}}, $value if $s;
216 }
217 }
218 $self;
219 }
220
221 =item $msg = Message::Header->parse_array (\@header, [%initial-fields/options])
222
223 Parses given C<header> and constructs a new C<Message::Headers>
224 object. Same as C<Message::Header-E<lt>parse> but this method
225 is given an array reference. You might pass some additional
226 C<field-name>-C<field-body> pairs or/and initial options
227 as parameters to the constructor.
228
229 =cut
230
231 sub parse_array ($\@;%) {
232 my $class = shift;
233 my $header = shift;
234 Carp::croak "parse_array: first argument is not an array reference"
235 unless ref $header eq 'ARRAY';
236 my $self = bless {}, $class;
237 $self->_init (@_);
238 while (1) {
239 my $field = shift @$header;
240 if ($self->{option}->{use_folding}) {
241 while (1) {
242 if ($$header[0] =~ /^$REG{WSP}/) {
243 $field .= shift @$header;
244 } else {last}
245 }
246 }
247 if ($self->{option}->{linebreak_strict}) {
248 $field =~ s/\x0D\x0A//g;
249 } else {
250 $field =~ tr/\x0D\x0A//d;
251 }
252 local $self->{option}->{parse} = $self->{option}->{parse_all};
253 if ($field =~ /$REG{M_fromline}/) {
254 my ($s,undef,$value) = $self->_value_to_arrayitem
255 ('mail-from' => $1, $self->{option});
256 push @{$self->{value}}, $value if $s;
257 } elsif ($field =~ /$REG{M_field}/) {
258 my ($name, $body) = ($self->_n11n_field_name ($1), $2);
259 $body =~ s/$REG{WSP}+$//;
260 my ($s,undef,$value) = $self->_value_to_arrayitem
261 ($name => $body, $self->{option});
262 push @{$self->{value}}, $value if $s;
263 } elsif (length $field) {
264 my ($s,undef,$value) = $self->_value_to_arrayitem
265 ('x-unknown' => $field, $self->{option});
266 push @{$self->{value}}, $value if $s;
267 }
268 last if $#$header < 0;
269 }
270 $self;
271 }
272
273 =back
274
275 =head1 METHODS
276
277 =head2 $self->field ($field_name)
278
279 Returns C<field-body> of given C<field-name>.
280 When there are two or more C<field>s whose name is C<field-name>,
281 this method return all C<field-body>s as array. (On scalar
282 context, only first one is returned.)
283
284 =cut
285
286 sub field ($@) {shift->SUPER::item (@_)}
287 sub field_exist ($@) {shift->SUPER::item_exist (@_)}
288
289 ## item-by?, \$checked-item, {item-key => 1}, \%option
290 sub _item_match ($$\$\%\%) {
291 my $self = shift;
292 my ($by, $i, $list, $option) = @_;
293 return 0 unless ref $$i; ## Already removed
294 if ($by eq 'name') {
295 my %o = %$option; $o{parse} = 0;
296 my %l;
297 for (keys %$list) {
298 my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', %o);
299 if ($s) {
300 $l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1;
301 } else {
302 $l{$v->{name} .':'. ( $option->{ns} || $self->{option}->{ns_default_phuri} ) } = 1;
303 }
304 }
305 return 1 if $l{$$i->{name} . ':' . $$i->{ns}};
306 } elsif ($by eq 'ns') {
307 return 1 if $list->{ $$i->{ns} };
308 }
309 0;
310 }
311 *_delete_match = \&_item_match;
312
313 ## Returns returned item value \$item-value, \%option
314 sub _item_return_value ($\$\%) {
315 if (ref ${$_[1]}->{body}) {
316 ${$_[1]}->{body};
317 } else {
318 ${$_[1]}->{body} = $_[0]->_parse_value (${$_[1]}->{name} => ${$_[1]}->{body},
319 ns => ${$_[1]}->{ns});
320 ${$_[1]}->{body};
321 }
322 }
323 *_add_return_value = \&_item_return_value;
324 *_replace_return_value = \&_item_return_value;
325
326 ## Returns returned (new created) item value $name, \%option
327 sub _item_new_value ($$\%) {
328 my ($s,undef,$value) = $_[0]->_value_to_arrayitem
329 ($_[1] => '', $_[2]);
330 $s? $value: undef;
331 }
332
333
334
335 ## $self->_parse_value ($type, $value, %options);
336 sub _parse_value ($$$;%) {
337 my $self = shift;
338 my $name = shift ;#|| $self->{option}->{_VALTYPE_DEFAULT};
339 my $value = shift; return $value if ref $value;
340 my %option = @_;
341 my $vtype; { no strict 'refs';
342 my $vt = ${&_NS_uri2phpackage ($option{ns}).'::OPTION'}{value_type};
343 if (ref $vt) {
344 $vtype = $vt->{$name} || $vt->{$self->{option}->{_VALTYPE_DEFAULT}};
345 }
346 ## For compatiblity.
347 unless (ref $vtype) { $vtype = $self->{option}->{value_type}->{$name}
348 || $self->{option}->{value_type}->{$self->{option}->{_VALTYPE_DEFAULT}} }
349 }
350 my $vpackage = $vtype->[0];
351 my %vopt = %{$vtype->[1]} if ref $vtype->[1];
352 if ($vpackage eq ':none:') {
353 return $value;
354 } elsif (defined $value) {
355 eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@};
356 return $vpackage->parse ($value,
357 -format => $self->{option}->{format},
358 -field_ns => $option{ns},
359 -field_name => $name,
360 -header_default_charset => $self->{option}->{header_default_charset},
361 -header_default_charset_input => $self->{option}->{header_default_charset_input},
362 -parse_all => $self->{option}->{parse_all},
363 %vopt);
364 } else {
365 eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@};
366 return $vpackage->new (
367 -format => $self->{option}->{format},
368 -field_ns => $option{ns},
369 -field_name => $name,
370 -header_default_charset => $self->{option}->{header_default_charset},
371 -header_default_charset_input => $self->{option}->{header_default_charset_input},
372 -parse_all => $self->{option}->{parse_all},
373 %vopt);
374 }
375 }
376
377 =head2 $self->field_name_list ()
378
379 Returns list of all C<field-name>s. (Even if there are two
380 or more C<field>s which have same C<field-name>, this method
381 returns ALL names.)
382
383 =cut
384
385 sub field_name_list ($) {
386 my $self = shift;
387 $self->_delete_empty ();
388 map { $_->{name} . ':' . $_->{ns} } @{$self->{value}};
389 }
390
391 sub namespace_ph_default ($;$) {
392 my $self = shift;
393 if (defined $_[0]) {
394 no strict 'refs';
395 $self->{option}->{ns_default_phuri} = $_[0];
396 $self->_ns_load_ph (${&_NS_uri2phpackage ($self->{option}->{ns_default_phuri}).'::OPTION'}{namespace_phname});
397 }
398 $self->{option}->{ns_default_phuri};
399 }
400
401 =item $hdr->add ($field-name, $field-body, [$name, $body, ...])
402
403 Adds some field name/body pairs. Even if there are
404 one or more fields named given C<$field-name>,
405 given name/body pairs are ADDed. Use C<replace>
406 to remove same-name-fields.
407
408 Instead of field name-body pair, you might pass some options.
409 Four options are available for this method.
410
411 C<-parse>: Parses and validates C<field-body>, and returns
412 C<field-body> object. (When multiple C<field-body>s are
413 added, returns only last one.) (Default: C<defined wantarray>)
414
415 C<-prepend>: New fields are not appended,
416 but prepended to current fields. (Default: C<0>)
417
418 C<-translate-underscore>: Do C<field-name> =~ tr/_/-/. (Default: C<1>)
419
420 C<-validate>: Checks whether C<field-name> is valid or not.
421
422 =cut
423
424 ## [Name: Value] pair -> internal array item
425 ## $self->_value_to_arrayitem ($name => $value, {%options})
426 ## or
427 ## $self->_value_to_arrayitem ($name => [$value, %value_options], {%options})
428 ##
429 ## Return: ((1 = success / 0 = failue), $full_name, $arrayitem)
430 sub _value_to_arrayitem ($$$\%) {
431 my $self = shift;
432 my ($name, $value, $option) = @_;
433 my $value_option = {};
434 if (ref $value eq 'ARRAY') {
435 ($value, %$value_option) = @$value;
436 }
437 my $nsuri = $self->{option}->{ns_default_phuri};
438
439 no strict 'refs';
440 if ($value_option->{ns}) {
441 $nsuri = $value_option->{ns};
442 } elsif ($option->{ns}) {
443 $nsuri = $option->{ns};
444 } elsif ($name =~ s/^([Xx]-[A-Za-z]+|[A-Za-z]+)-//) {
445 my $oprefix = $1;
446 my $prefix
447 = &{${&_NS_uri2phpackage ($nsuri).'::OPTION'}{n11n_prefix}}
448 ($self, &_NS_uri2phpackage ($nsuri), $oprefix);
449 $self->_ns_load_ph ($prefix);
450 $nsuri = $self->{ns}->{phname2uri}->{$prefix};
451 unless ($nsuri) {
452 $name = $oprefix . '-' . $name;
453 $nsuri = $self->{option}->{ns_default_phuri};
454 }
455 }
456 $name
457 = &{${&_NS_uri2phpackage ($nsuri).'::OPTION'}{n11n_name}}
458 ($self, &_NS_uri2phpackage ($nsuri), $name);
459 Carp::croak "$name: invalid field-name"
460 if $option->{field_name_validation}
461 && $name =~ /$REG{$option->{field_name_unsafe_rule}}/;
462 $value = $self->_parse_value ($name => $value, ns => $nsuri)
463 if $$option{parse} || $$option{parse_all};
464 $$option{parse} = 0;
465 (1, $name.':'.$nsuri => {name => $name, body => $value, ns => $nsuri});
466 }
467 *_add_hash_check = \&_value_to_arrayitem;
468 *_replace_hash_check = \&_value_to_arrayitem;
469
470 =head2 $self->relace ($field_name, $field_body)
471
472 Set the C<field-body> named C<field-name> as $field_body.
473 If $field_name C<field> is already exists, it is replaced
474 by new $field_body value. If not, new C<field> is inserted.
475 (If there are some C<field> named as $field_name,
476 first one is used and the others are not changed.)
477
478 =cut
479
480 sub _replace_hash_shift ($\%$\%) {
481 shift; my $r = shift; my $n = $_[0]->{name} . ':' . $_[0]->{ns};
482 if ($$r{$n}) {
483 my $d = $$r{$n};
484 delete $$r{$n};
485 return $d;
486 }
487 undef;
488 }
489
490 =head2 $self->delete ($field-name, [$name, ...])
491
492 Deletes C<field> named as $field_name.
493
494 =cut
495
496
497 =head2 $self->count ([$field_name])
498
499 Returns the number of times the given C<field> appears.
500 If no $field_name is given, returns the number
501 of fields. (Same as $#$self+1)
502
503 =cut
504
505 sub _count_by_name ($$\%) {
506 my $self = shift;
507 my ($array, $option) = @_;
508 my $name = $self->_n11n_field_name ($$option{-name});
509 my @a = grep {$_->{name} eq $name} @{$self->{$array}};
510 $#a + 1;
511 }
512
513 ## Delete empty items
514 sub _delete_empty ($) {
515 my $self = shift;
516 my $array = $self->{option}->{_HASH_NAME};
517 $self->{$array} = [grep {ref $_ && length $_->{name}} @{$self->{$array}}];
518 }
519
520 =head2 $self->rename ($field-name, $new-name, [$old, $new,...])
521
522 Renames C<$field-name> as C<$new-name>.
523
524 =cut
525
526 sub rename ($%) {
527 my $self = shift;
528 my %params = @_;
529 my %option = %{$self->{option}};
530 for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}
531 my %new_name;
532 for (grep {/^[^-]/} keys %params) {
533 my ($old => $new)
534 = ($self->_n11n_field_name ($_) => $self->_n11n_field_name ($params{$_}));
535 $old =~ tr/_/-/ if $option{translate_underscore};
536 $new =~ tr/_/-/ if $option{translate_underscore};
537 Carp::croak "rename: $new: invalid field-name"
538 if $option{field_name_validation}
539 && $new =~ /$REG{$option{field_name_unsafe_rule}}/;
540 $new_name{$old} = $new;
541 }
542 for my $field (@{$self->{value}}) {
543 if (length $new_name{$field->{name}}) {
544 $field->{name} = $new_name{$field->{name}};
545 }
546 }
547 $self if defined wantarray;
548 }
549
550
551 =item $self->scan(\&doit)
552
553 Apply a subroutine to each header field in turn. The callback routine is
554 called with two parameters; the name of the field and a single value.
555 If the header has more than one value, then the routine is called once
556 for each value.
557
558 =cut
559
560 sub _scan_sort ($\@\%) {
561 my $self = shift;
562 my ($array, $option) = @_;
563 my $sort;
564 $sort = \&_header_cmp if $option->{field_sort} eq 'good-practice';
565 $sort = {$a cmp $b} if $option->{field_sort} eq 'alphabetic';
566 return ( sort $sort @$array ) if ref $sort;
567 @$array;
568 }
569
570 sub _n11n_field_name ($$) {
571 no strict 'refs';
572 my $self = shift;
573 my $s = shift;
574 $s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//;
575 $s = lc $s unless ${&_NS_uri2phpackage ($self->{option}->{ns_default_phuri}).'::OPTION'}{case_sensible};
576 $s;
577 }
578
579 # Compare function which makes it easy to sort headers in the
580 # recommended "Good Practice" order.
581 ## taken from HTTP::Header
582 sub _header_cmp
583 {
584 my ($na, $nb) = ($a->{name}, $b->{name});
585 # Unknown headers are assign a large value so that they are
586 # sorted last. This also helps avoiding a warning from -w
587 # about comparing undefined values.
588 $header_order{$na} = 999 unless defined $header_order{$na};
589 $header_order{$nb} = 999 unless defined $header_order{$nb};
590
591 $header_order{$na} <=> $header_order{$nb} || $na cmp $nb;
592 }
593
594 =head2 $self->stringify ([%option])
595
596 Returns the C<header> as a string.
597
598 =cut
599
600 sub stringify ($;%) {
601 my $self = shift;
602 my %params = @_;
603 my %option = %{$self->{option}};
604 $option{format} = $params{-format} if $params{-format};
605 $self->_init_by_format ($option{format}, \%option);
606 for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}
607 my @ret;
608 my $_stringify = sub {
609 no strict 'refs';
610 my ($name, $body, $nsuri) = ($_[1]->{name}, $_[1]->{body}, $_[1]->{ns});
611 return unless length $name;
612 return if $option{output_mail_from} && $name eq 'mail-from';
613 return if !$option{output_bcc} && ($name eq 'bcc' || $name eq 'resent-bcc');
614 my $nspackage = &_NS_uri2phpackage ($nsuri);
615 my $oname; ## Outputed field-name
616 my $prefix = ${$nspackage.'::OPTION'} {namespace_phname_goodcase}
617 || $self->{ns}->{uri2phname}->{$nsuri};
618 $prefix = undef if $nsuri eq $self->{option}->{ns_default_phuri};
619 my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase};
620 if (ref $gc) { $oname = &$gc ($self, $nspackage, $name, \%option) }
621 else { $oname = $name }
622 if ($prefix) { $oname = $prefix . '-' . $oname }
623 if ($option{format} =~ /uri-url-mailto/) {
624 return if (( ${$nspackage.'::OPTION'} {uri_mailto_safe}->{$name}
625 || ${$nspackage.'::OPTION'} {uri_mailto_safe}->{':default'})
626 < $option{uri_mailto_safe_level});
627 if ($name eq 'to') {
628 $body = $self->field ('to', -new_item_unless_exist => 0);
629 if (ref $body && $body->have_group) {
630 #
631 } elsif (ref $body && $body->count > 1) {
632 $body = $body->clone;
633 $body->delete ({-by => 'index'}, 0);
634 }
635 }
636 }
637 my $fbody;
638 if (ref $body) {
639 $fbody = $body->stringify (-format => $option{format});
640 } else {
641 $fbody = $body;
642 }
643 return unless length $fbody;
644 unless ($option{linebreak_strict}) {
645 ## bare \x0D and bare \x0A are unsafe
646 $fbody =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g;
647 $fbody =~ s/\x0A(?=[^\x09\x20])/\x0A\x20/g;
648 } else {
649 $fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g;
650 }
651 if ($option{use_folding}) {
652 if (ref $option{output_folding}) {
653 $fbody = &{$option{output_folding}} ($self, $fbody,
654 -initial_length => length ($oname) +2);
655 } elsif ($option{output_folding}) {
656 $fbody = $self->_fold ($fbody, -initial_length => length ($oname) +2);
657 }
658 }
659 push @ret, sprintf $option{field_format_pattern}, $oname, $fbody;
660 };
661 if ($option{format} =~ /uri-url-mailto/) {
662 if ($option{format} =~ /uri-url-mailto-to/) {
663 my $to = $self->field ('to', -new_item_unless_exist => 0);
664 if ($to) {
665 unless ($to->have_group) {
666 my $fbody = $to->stringify (-format => $option{format}, -max => 1);
667 return &{$option{output_folding}} ($self, $fbody);
668 }
669 }
670 '';
671 } elsif ($option{format} =~ /uri-url-mailto-rfc1738/) {
672 my $to = $self->field ('to', -new_item_unless_exist => 0);
673 if ($to) {
674 my $fbody = $to->addr_spec (-format => $option{format});
675 return &{$option{output_folding}} ($self, $fbody);
676 }
677 '';
678 } else {
679 $self->scan ($_stringify);
680 my $ret = join ('&', @ret);
681 $ret;
682 }
683 } else {
684 if ($option{output_mail_from}) {
685 my $fromline = $self->field ('mail-from', -new_item_unless_exist => 0);
686 push @ret, 'From '.$fromline if $fromline;
687 }
688 $self->scan ($_stringify);
689 my $ret = join ("\x0D\x0A", @ret);
690 $ret? $ret."\x0D\x0A": '';
691 }
692 }
693 *as_string = \&stringify;
694
695 =head2 $self->option ($option_name, [$option_value])
696
697 Set/gets new value of the option.
698
699 =cut
700
701 sub option ($@) {
702 my $self = shift;
703 if (@_ == 1) {
704 return $self->{option}->{ shift (@_) };
705 }
706 while (my ($name, $value) = splice (@_, 0, 2)) {
707 $self->{option}->{$name} = $value;
708 if ($name eq 'format') {
709 for my $f (@{$self->{field}}) {
710 if (ref $f->{body}) {
711 $f->{body}->option (-format => $value);
712 }
713 }
714 }
715 }
716 }
717
718 sub field_type ($@) {shift->SUPER::value_type (@_)}
719
720 ## $self->_fold ($string, %option = (-max, -initial_length(for field-name)) )
721 sub _fold ($$;%) {
722 my $self = shift;
723 my $string = shift;
724 my %option = @_;
725 my $max = $self->{option}->{line_length_max};
726 $max = 20 if $max < 20;
727
728 my $l = $option{-initial_length} || 0;
729 $l += length $1 if $string =~ /^([^\x09\x20]+)/;
730 $string =~ s{([\x09\x20][^\x09\x20]+)}{
731 my $s = $1;
732 if (($l + length $s) > $max) {
733 $s = "\x0D\x0A\x20" . $s;
734 $l = 1 + length $s;
735 } else { $l += length $s }
736 $s;
737 }gex;
738 $string;
739 }
740
741 sub _ns_load_ph ($$) {
742 my $self = shift;
743 my $name = shift; ## normalized prefix (without HYPHEN-MINUS)
744 return if $self->{ns}->{phname2uri}->{$name};
745 $self->{ns}->{phname2uri}->{$name} = $NS_phname2uri{$name};
746 return unless $self->{ns}->{phname2uri}->{$name};
747 $self->{ns}->{uri2phname}->{$self->{ns}->{phname2uri}->{$name}} = $name;
748 }
749
750 sub _NS_uri2phpackage ($) {
751 $NS_uri2phpackage{$_[0]}
752 || $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}};
753 }
754
755 =head2 $self->clone ()
756
757 Returns a copy of Message::Header object.
758
759 =cut
760
761 ## Inhreited
762
763 =head1 NOTE
764
765 =head2 C<field-name>
766
767 The header field name is not case sensitive. To make the life
768 easier for perl users who wants to avoid quoting before the => operator,
769 you can use '_' as a synonym for '-' in header field names
770 (this behaviour can be suppressed by setting
771 C<translate_underscore> option to C<0> value).
772
773 =head1 EXAMPLE
774
775 ## Print field list
776
777 use Message::Header;
778 my $header = Message::Header->parse ($header);
779
780 for my $i (0..$#$header) {
781 print $header->field_name ($i), "\t=> ", $header->field_body ($i), "\n";
782 }
783
784
785 ## Make simple header
786
787 use Message::Header;
788 use Message::Field::Address;
789 my $header = new Message::Header;
790
791 my $from = new Message::Field::Address;
792 $from->add ('foo@foo.example', name => 'F. Foo');
793 my $to = new Message::Field::Address;
794 $to->add ('bar@bar.example', name => 'Mr. Bar');
795 $to->add ('hoge@foo.example', name => 'Hoge-san');
796 $header->add ('From' => $from);
797 $header->add ('To' => $to);
798 $header->add ('Subject' => 'Re: Meeting');
799 $header->add ('References' => '<hoge.msgid%foo@foo.example>');
800 print $header;
801
802 =head1 ACKNOWLEDGEMENTS
803
804 Some of codes are taken from other modules such as
805 HTTP::Header, Mail::Header.
806
807 =head1 LICENSE
808
809 Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
810
811 This program is free software; you can redistribute it and/or modify
812 it under the terms of the GNU General Public License as published by
813 the Free Software Foundation; either version 2 of the License, or
814 (at your option) any later version.
815
816 This program is distributed in the hope that it will be useful,
817 but WITHOUT ANY WARRANTY; without even the implied warranty of
818 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
819 GNU General Public License for more details.
820
821 You should have received a copy of the GNU General Public License
822 along with this program; see the file COPYING. If not, write to
823 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
824 Boston, MA 02111-1307, USA.
825
826 =head1 CHANGE
827
828 See F<ChangeLog>.
829 $Date: 2002/06/23 12:20:11 $
830
831 =cut
832
833 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24