/[suikacvs]/test/cvs
Suika

Contents of /test/cvs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.31 - (hide annotations) (download)
Sat Jul 6 10:30:10 2002 UTC (21 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.30: +140 -22 lines
2002-07-06  Wakaba <w@suika.fam.cx>

	* Header.pm: Support of RFC 2774 namespace.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24