/[suikacvs]/test/cvs
Suika

Contents of /test/cvs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.34 - (show annotations) (download)
Mon Jul 8 11:49:18 2002 UTC (21 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.33: +22 -16 lines
2002-07-08  Wakaba <w@suika.fam.cx>

	* Entity.pm (parse): Typo fix.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24