/[suikacvs]/messaging/manakai/lib/Message/Header.pm
Suika

Contents of /messaging/manakai/lib/Message/Header.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.39 - (hide annotations) (download)
Fri Jul 26 12:42:00 2002 UTC (22 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.38: +3 -3 lines
2002-07-25  Wakaba <w@suika.fam.cx>

	* Tool.pm: New module.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24