/[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.26 - (hide annotations) (download)
Sun Jun 16 10:45:54 2002 UTC (22 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.25: +23 -24 lines
2002-06-16  wakaba <w@suika.fam.cx>

	* Header.pm (_n11n_field_name): Check namespace definition's
	case_sensible option.
	* Entity.pm (_add_ua): Removed.  (Moved to Message::Field::UA.)

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24