/[suikacvs]/test/cvs
Suika

Contents of /test/cvs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.30 - (hide annotations) (download)
Thu Jul 4 06:38:21 2002 UTC (21 years, 10 months ago) by wakaba
Branch: MAIN
Changes since 1.29: +12 -35 lines
2002-07-04  Wakaba <w@suika.fam.cx>

	* Util.pm (encode_printable_string, decode_printable_string,
	encode_t71_string, decode_t71_string, encode_restricted_rfc822,
	decode_restricted_rfc822): New functions.

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.30 $VERSION=do{my @r=(q$Revision: 1.30 $=~/\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     $self->_ns_load_ph ('rfc822');
103 wakaba 1.26 $self->{option}->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'rfc822'}
104     unless $self->{option}->{ns_default_phuri};
105 wakaba 1.20
106 wakaba 1.29 ## For text/rfc822-headers
107     if (ref $options{entity_header}) {
108     $self->{entity_header} = $options{entity_header};
109     delete $options{entity_header};
110     }
111 wakaba 1.14 my @new_fields = ();
112     for my $name (keys %options) {
113 wakaba 1.20 unless (substr ($name, 0, 1) eq '-') {
114 wakaba 1.14 push @new_fields, ($name => $options{$name});
115     }
116     }
117 wakaba 1.18 $self->_init_by_format ($self->{option}->{format}, $self->{option});
118 wakaba 1.14 # Make alternative representations of @header_order. This is used
119     # for sorting.
120     my $i = 1;
121     for (@header_order) {
122     $header_order{$_} = $i++ unless $header_order{$_};
123     }
124 wakaba 1.18
125     $self->add (@new_fields, -parse => $self->{option}->{parse_all})
126     if $#new_fields > -1;
127     }
128    
129     sub _init_by_format ($$\%) {
130     my $self = shift;
131     my ($format, $option) = @_;
132 wakaba 1.21 if ($format =~ /cgi/) {
133 wakaba 1.18 unshift @header_order, qw(content-type location);
134 wakaba 1.20 $option->{field_sort} = 'good-practice';
135     $option->{use_folding} = 0;
136 wakaba 1.18 } elsif ($format =~ /http/) {
137 wakaba 1.20 $option->{field_sort} = 'good-practice';
138 wakaba 1.18 }
139     if ($format =~ /uri-url-mailto/) {
140     $option->{output_bcc} = 0;
141     $option->{field_format_pattern} = '%s=%s';
142 wakaba 1.20 $option->{output_folding} = sub {
143 wakaba 1.18 $_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge;
144     $_[1];
145 wakaba 1.20 }; ## Yes, this is not folding!
146 wakaba 1.18 }
147 wakaba 1.14 }
148    
149 wakaba 1.20 =item $msg = Message::Header->new ([%initial-fields/options])
150 wakaba 1.14
151     Constructs a new C<Message::Headers> object. You might pass some initial
152     C<field-name>-C<field-body> pairs and/or options as parameters to the constructor.
153    
154 wakaba 1.15 Example:
155 wakaba 1.1
156 wakaba 1.14 $hdr = new Message::Headers
157     Date => 'Thu, 03 Feb 1994 00:00:00 +0000',
158     Content_Type => 'text/html',
159     Content_Location => 'http://www.foo.example/',
160     -format => 'mail-rfc2822' ## not to be header field
161     ;
162 wakaba 1.1
163     =cut
164    
165 wakaba 1.20 ## Inherited
166 wakaba 1.1
167 wakaba 1.20 =item $msg = Message::Header->parse ($header, [%initial-fields/options])
168 wakaba 1.1
169 wakaba 1.14 Parses given C<header> and constructs a new C<Message::Headers>
170     object. You might pass some additional C<field-name>-C<field-body> pairs
171     or/and initial options as parameters to the constructor.
172 wakaba 1.1
173     =cut
174    
175     sub parse ($$;%) {
176     my $class = shift;
177     my $header = shift;
178 wakaba 1.14 my $self = bless {}, $class;
179 wakaba 1.28 $self->_init (@_);
180     if ($self->{option}->{linebreak_strict}) {
181     $header =~ s/\x0D\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding};
182     } else {
183     $header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding};
184     }
185 wakaba 1.1 for my $field (split /\x0D?\x0A/, $header) {
186     if ($field =~ /$REG{M_fromline}/) {
187 wakaba 1.20 my ($s,undef,$value) = $self->_value_to_arrayitem
188     ('mail-from' => $1, $self->{option});
189     push @{$self->{value}}, $value if $s;
190 wakaba 1.1 } elsif ($field =~ /$REG{M_field}/) {
191 wakaba 1.20 my ($name, $body) = ($1, $2);
192 wakaba 1.1 $body =~ s/$REG{WSP}+$//;
193 wakaba 1.20 my ($s,undef,$value) = $self->_value_to_arrayitem
194     ($name => $body, $self->{option});
195     push @{$self->{value}}, $value if $s;
196 wakaba 1.23 } elsif (length $field) {
197     my ($s,undef,$value) = $self->_value_to_arrayitem
198     ('x-unknown' => $field, $self->{option});
199     push @{$self->{value}}, $value if $s;
200 wakaba 1.1 }
201     }
202     $self;
203     }
204    
205 wakaba 1.20 =item $msg = Message::Header->parse_array (\@header, [%initial-fields/options])
206 wakaba 1.15
207     Parses given C<header> and constructs a new C<Message::Headers>
208     object. Same as C<Message::Header-E<lt>parse> but this method
209     is given an array reference. You might pass some additional
210     C<field-name>-C<field-body> pairs or/and initial options
211     as parameters to the constructor.
212    
213     =cut
214    
215 wakaba 1.14 sub parse_array ($\@;%) {
216     my $class = shift;
217     my $header = shift;
218     Carp::croak "parse_array: first argument is not an array reference"
219     unless ref $header eq 'ARRAY';
220     my $self = bless {}, $class;
221     $self->_init (@_);
222     while (1) {
223     my $field = shift @$header;
224 wakaba 1.20 if ($self->{option}->{use_folding}) {
225     while (1) {
226     if ($$header[0] =~ /^$REG{WSP}/) {
227     $field .= shift @$header;
228     } else {last}
229     }
230 wakaba 1.14 }
231 wakaba 1.18 if ($self->{option}->{linebreak_strict}) {
232     $field =~ s/\x0D\x0A//g;
233     } else {
234     $field =~ tr/\x0D\x0A//d;
235     }
236 wakaba 1.20 local $self->{option}->{parse} = $self->{option}->{parse_all};
237 wakaba 1.14 if ($field =~ /$REG{M_fromline}/) {
238 wakaba 1.20 my ($s,undef,$value) = $self->_value_to_arrayitem
239     ('mail-from' => $1, $self->{option});
240     push @{$self->{value}}, $value if $s;
241 wakaba 1.14 } elsif ($field =~ /$REG{M_field}/) {
242 wakaba 1.20 my ($name, $body) = ($self->_n11n_field_name ($1), $2);
243 wakaba 1.14 $body =~ s/$REG{WSP}+$//;
244 wakaba 1.20 my ($s,undef,$value) = $self->_value_to_arrayitem
245     ($name => $body, $self->{option});
246     push @{$self->{value}}, $value if $s;
247 wakaba 1.23 } elsif (length $field) {
248     my ($s,undef,$value) = $self->_value_to_arrayitem
249     ('x-unknown' => $field, $self->{option});
250     push @{$self->{value}}, $value if $s;
251 wakaba 1.14 }
252     last if $#$header < 0;
253     }
254     $self;
255     }
256    
257 wakaba 1.15 =back
258    
259     =head1 METHODS
260    
261 wakaba 1.1 =head2 $self->field ($field_name)
262    
263     Returns C<field-body> of given C<field-name>.
264     When there are two or more C<field>s whose name is C<field-name>,
265     this method return all C<field-body>s as array. (On scalar
266     context, only first one is returned.)
267    
268     =cut
269    
270 wakaba 1.20 sub field ($@) {shift->SUPER::item (@_)}
271     sub field_exist ($@) {shift->SUPER::item_exist (@_)}
272    
273     ## item-by?, \$checked-item, {item-key => 1}, \%option
274     sub _item_match ($$\$\%\%) {
275 wakaba 1.1 my $self = shift;
276 wakaba 1.20 my ($by, $i, $list, $option) = @_;
277     return 0 unless ref $$i; ## Already removed
278     if ($by eq 'name') {
279     my %o = %$option; $o{parse} = 0;
280     my %l;
281     for (keys %$list) {
282     my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', %o);
283     if ($s) {
284     $l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1;
285 wakaba 1.1 } else {
286 wakaba 1.26 $l{$v->{name} .':'. ( $option->{ns} || $self->{option}->{ns_default_phuri} ) } = 1;
287 wakaba 1.1 }
288     }
289 wakaba 1.20 return 1 if $l{$$i->{name} . ':' . $$i->{ns}};
290 wakaba 1.24 } elsif ($by eq 'ns') {
291 wakaba 1.25 return 1 if $list->{ $$i->{ns} };
292 wakaba 1.1 }
293 wakaba 1.20 0;
294 wakaba 1.1 }
295 wakaba 1.20 *_delete_match = \&_item_match;
296 wakaba 1.1
297 wakaba 1.20 ## Returns returned item value \$item-value, \%option
298     sub _item_return_value ($\$\%) {
299     if (ref ${$_[1]}->{body}) {
300     ${$_[1]}->{body};
301     } else {
302     ${$_[1]}->{body} = $_[0]->_parse_value (${$_[1]}->{name} => ${$_[1]}->{body},
303     ns => ${$_[1]}->{ns});
304     ${$_[1]}->{body};
305 wakaba 1.9 }
306     }
307 wakaba 1.26 *_add_return_value = \&_item_return_value;
308     *_replace_return_value = \&_item_return_value;
309 wakaba 1.9
310 wakaba 1.20 ## Returns returned (new created) item value $name, \%option
311     sub _item_new_value ($$\%) {
312     my ($s,undef,$value) = $_[0]->_value_to_arrayitem
313     ($_[1] => '', $_[2]);
314     $s? $value: undef;
315     }
316 wakaba 1.2
317    
318    
319 wakaba 1.20 ## $self->_parse_value ($type, $value, %options);
320     sub _parse_value ($$$;%) {
321     my $self = shift;
322     my $name = shift ;#|| $self->{option}->{_VALTYPE_DEFAULT};
323     my $value = shift; return $value if ref $value;
324     my %option = @_;
325     my $vtype; { no strict 'refs';
326 wakaba 1.26 my $vt = ${&_NS_uri2phpackage ($option{ns}).'::OPTION'}{value_type};
327     if (ref $vt) {
328     $vtype = $vt->{$name} || $vt->{$self->{option}->{_VALTYPE_DEFAULT}};
329     }
330 wakaba 1.20 ## For compatiblity.
331     unless (ref $vtype) { $vtype = $self->{option}->{value_type}->{$name}
332     || $self->{option}->{value_type}->{$self->{option}->{_VALTYPE_DEFAULT}} }
333     }
334     my $vpackage = $vtype->[0];
335     my %vopt = %{$vtype->[1]} if ref $vtype->[1];
336     if ($vpackage eq ':none:') {
337     return $value;
338     } elsif (defined $value) {
339     eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@};
340     return $vpackage->parse ($value,
341     -format => $self->{option}->{format},
342 wakaba 1.21 -field_ns => $option{ns},
343 wakaba 1.20 -field_name => $name,
344 wakaba 1.27 -header_default_charset => $self->{option}->{header_default_charset},
345     -header_default_charset_input => $self->{option}->{header_default_charset_input},
346 wakaba 1.20 -parse_all => $self->{option}->{parse_all},
347     %vopt);
348     } else {
349     eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@};
350     return $vpackage->new (
351     -format => $self->{option}->{format},
352 wakaba 1.21 -field_ns => $option{ns},
353 wakaba 1.20 -field_name => $name,
354 wakaba 1.27 -header_default_charset => $self->{option}->{header_default_charset},
355     -header_default_charset_input => $self->{option}->{header_default_charset_input},
356 wakaba 1.20 -parse_all => $self->{option}->{parse_all},
357     %vopt);
358 wakaba 1.4 }
359 wakaba 1.2 }
360    
361 wakaba 1.29 ## Defined for text/rfc822-headers
362     sub entity_header ($;$) {
363     my $self = shift;
364     my $new_header = shift;
365     if (ref $new_header) {
366     $self->{header} = $new_header;
367     }
368     $self->{header};
369     }
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 wakaba 1.27 no strict 'refs';
566 wakaba 1.20 my $self = shift;
567     my $s = shift;
568     $s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//;
569 wakaba 1.26 $s = lc $s unless ${&_NS_uri2phpackage ($self->{option}->{ns_default_phuri}).'::OPTION'}{case_sensible};
570 wakaba 1.20 $s;
571 wakaba 1.14 }
572    
573     # Compare function which makes it easy to sort headers in the
574     # recommended "Good Practice" order.
575     ## taken from HTTP::Header
576     sub _header_cmp
577     {
578     my ($na, $nb) = ($a->{name}, $b->{name});
579     # Unknown headers are assign a large value so that they are
580     # sorted last. This also helps avoiding a warning from -w
581     # about comparing undefined values.
582     $header_order{$na} = 999 unless defined $header_order{$na};
583     $header_order{$nb} = 999 unless defined $header_order{$nb};
584    
585     $header_order{$na} <=> $header_order{$nb} || $na cmp $nb;
586 wakaba 1.12 }
587    
588 wakaba 1.1 =head2 $self->stringify ([%option])
589    
590     Returns the C<header> as a string.
591    
592     =cut
593    
594     sub stringify ($;%) {
595     my $self = shift;
596 wakaba 1.14 my %params = @_;
597     my %option = %{$self->{option}};
598 wakaba 1.18 $option{format} = $params{-format} if $params{-format};
599     $self->_init_by_format ($option{format}, \%option);
600 wakaba 1.14 for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}
601 wakaba 1.1 my @ret;
602 wakaba 1.18 my $_stringify = sub {
603 wakaba 1.20 no strict 'refs';
604     my ($name, $body, $nsuri) = ($_[1]->{name}, $_[1]->{body}, $_[1]->{ns});
605 wakaba 1.18 return unless length $name;
606 wakaba 1.20 return if $option{output_mail_from} && $name eq 'mail-from';
607 wakaba 1.30 $body = '' if !$option{output_bcc} && $name eq 'bcc';
608 wakaba 1.20 my $nspackage = &_NS_uri2phpackage ($nsuri);
609     my $oname; ## Outputed field-name
610     my $prefix = ${$nspackage.'::OPTION'} {namespace_phname_goodcase}
611     || $self->{ns}->{uri2phname}->{$nsuri};
612 wakaba 1.26 $prefix = undef if $nsuri eq $self->{option}->{ns_default_phuri};
613 wakaba 1.20 my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase};
614 wakaba 1.21 if (ref $gc) { $oname = &$gc ($self, $nspackage, $name, \%option) }
615 wakaba 1.20 else { $oname = $name }
616     if ($prefix) { $oname = $prefix . '-' . $oname }
617 wakaba 1.18 if ($option{format} =~ /uri-url-mailto/) {
618 wakaba 1.21 return if (( ${$nspackage.'::OPTION'} {uri_mailto_safe}->{$name}
619     || ${$nspackage.'::OPTION'} {uri_mailto_safe}->{':default'})
620     < $option{uri_mailto_safe_level});
621 wakaba 1.18 if ($name eq 'to') {
622 wakaba 1.20 $body = $self->field ('to', -new_item_unless_exist => 0);
623     if (ref $body && $body->have_group) {
624     #
625     } elsif (ref $body && $body->count > 1) {
626     $body = $body->clone;
627     $body->delete ({-by => 'index'}, 0);
628     }
629 wakaba 1.18 }
630     }
631     my $fbody;
632     if (ref $body) {
633     $fbody = $body->stringify (-format => $option{format});
634     } else {
635     $fbody = $body;
636     }
637 wakaba 1.30 unless (${$nspackage.'::OPTION'} {field}->{$name}->{empty_body}) {
638     return unless length $fbody;
639     }
640 wakaba 1.18 unless ($option{linebreak_strict}) {
641     ## bare \x0D and bare \x0A are unsafe
642     $fbody =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g;
643     $fbody =~ s/\x0A(?=[^\x09\x20])/\x0A\x20/g;
644     } else {
645     $fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g;
646     }
647 wakaba 1.20 if ($option{use_folding}) {
648     if (ref $option{output_folding}) {
649     $fbody = &{$option{output_folding}} ($self, $fbody,
650     -initial_length => length ($oname) +2);
651     } elsif ($option{output_folding}) {
652     $fbody = $self->_fold ($fbody, -initial_length => length ($oname) +2);
653     }
654 wakaba 1.18 }
655 wakaba 1.20 push @ret, sprintf $option{field_format_pattern}, $oname, $fbody;
656     };
657     if ($option{format} =~ /uri-url-mailto/) {
658     if ($option{format} =~ /uri-url-mailto-to/) {
659     my $to = $self->field ('to', -new_item_unless_exist => 0);
660     if ($to) {
661     unless ($to->have_group) {
662     my $fbody = $to->stringify (-format => $option{format}, -max => 1);
663     return &{$option{output_folding}} ($self, $fbody);
664     }
665 wakaba 1.18 }
666 wakaba 1.20 '';
667     } elsif ($option{format} =~ /uri-url-mailto-rfc1738/) {
668     my $to = $self->field ('to', -new_item_unless_exist => 0);
669     if ($to) {
670     my $fbody = $to->addr_spec (-format => $option{format});
671     return &{$option{output_folding}} ($self, $fbody);
672 wakaba 1.18 }
673 wakaba 1.20 '';
674     } else {
675     $self->scan ($_stringify);
676     my $ret = join ('&', @ret);
677     $ret;
678 wakaba 1.12 }
679 wakaba 1.18 } else {
680 wakaba 1.20 if ($option{output_mail_from}) {
681     my $fromline = $self->field ('mail-from', -new_item_unless_exist => 0);
682 wakaba 1.18 push @ret, 'From '.$fromline if $fromline;
683     }
684     $self->scan ($_stringify);
685 wakaba 1.20 my $ret = join ("\x0D\x0A", @ret);
686     $ret? $ret."\x0D\x0A": '';
687 wakaba 1.18 }
688 wakaba 1.1 }
689 wakaba 1.14 *as_string = \&stringify;
690 wakaba 1.1
691 wakaba 1.12 =head2 $self->option ($option_name, [$option_value])
692 wakaba 1.1
693 wakaba 1.12 Set/gets new value of the option.
694 wakaba 1.1
695     =cut
696    
697 wakaba 1.14 sub option ($@) {
698 wakaba 1.1 my $self = shift;
699 wakaba 1.14 if (@_ == 1) {
700     return $self->{option}->{ shift (@_) };
701     }
702     while (my ($name, $value) = splice (@_, 0, 2)) {
703 wakaba 1.12 $self->{option}->{$name} = $value;
704     if ($name eq 'format') {
705     for my $f (@{$self->{field}}) {
706 wakaba 1.14 if (ref $f->{body}) {
707     $f->{body}->option (-format => $value);
708 wakaba 1.12 }
709     }
710     }
711     }
712 wakaba 1.1 }
713    
714 wakaba 1.20 sub field_type ($@) {shift->SUPER::value_type (@_)}
715    
716     ## $self->_fold ($string, %option = (-max, -initial_length(for field-name)) )
717     sub _fold ($$;%) {
718 wakaba 1.4 my $self = shift;
719 wakaba 1.20 my $string = shift;
720     my %option = @_;
721     my $max = $self->{option}->{line_length_max};
722     $max = 20 if $max < 20;
723    
724     my $l = $option{-initial_length} || 0;
725 wakaba 1.27 $l += length $1 if $string =~ /^([^\x09\x20]+)/;
726     $string =~ s{([\x09\x20][^\x09\x20]+)}{
727 wakaba 1.20 my $s = $1;
728 wakaba 1.27 if (($l + length $s) > $max) {
729 wakaba 1.20 $s = "\x0D\x0A\x20" . $s;
730 wakaba 1.27 $l = 1 + length $s;
731 wakaba 1.20 } else { $l += length $s }
732     $s;
733     }gex;
734     $string;
735 wakaba 1.4 }
736    
737 wakaba 1.20 sub _ns_load_ph ($$) {
738 wakaba 1.1 my $self = shift;
739 wakaba 1.20 my $name = shift; ## normalized prefix (without HYPHEN-MINUS)
740     return if $self->{ns}->{phname2uri}->{$name};
741     $self->{ns}->{phname2uri}->{$name} = $NS_phname2uri{$name};
742     return unless $self->{ns}->{phname2uri}->{$name};
743     $self->{ns}->{uri2phname}->{$self->{ns}->{phname2uri}->{$name}} = $name;
744 wakaba 1.1 }
745    
746 wakaba 1.20 sub _NS_uri2phpackage ($) {
747     $NS_uri2phpackage{$_[0]}
748     || $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}};
749 wakaba 1.1 }
750    
751 wakaba 1.14 =head2 $self->clone ()
752    
753     Returns a copy of Message::Header object.
754    
755     =cut
756    
757 wakaba 1.20 ## Inhreited
758 wakaba 1.14
759     =head1 NOTE
760    
761     =head2 C<field-name>
762    
763     The header field name is not case sensitive. To make the life
764     easier for perl users who wants to avoid quoting before the => operator,
765     you can use '_' as a synonym for '-' in header field names
766     (this behaviour can be suppressed by setting
767     C<translate_underscore> option to C<0> value).
768    
769 wakaba 1.1 =head1 EXAMPLE
770    
771     ## Print field list
772    
773     use Message::Header;
774     my $header = Message::Header->parse ($header);
775    
776 wakaba 1.2 for my $i (0..$#$header) {
777     print $header->field_name ($i), "\t=> ", $header->field_body ($i), "\n";
778 wakaba 1.1 }
779    
780    
781     ## Make simple header
782    
783 wakaba 1.2 use Message::Header;
784 wakaba 1.1 use Message::Field::Address;
785     my $header = new Message::Header;
786    
787     my $from = new Message::Field::Address;
788     $from->add ('foo@foo.example', name => 'F. Foo');
789     my $to = new Message::Field::Address;
790     $to->add ('bar@bar.example', name => 'Mr. Bar');
791     $to->add ('hoge@foo.example', name => 'Hoge-san');
792     $header->add ('From' => $from);
793     $header->add ('To' => $to);
794     $header->add ('Subject' => 'Re: Meeting');
795     $header->add ('References' => '<hoge.msgid%foo@foo.example>');
796     print $header;
797    
798 wakaba 1.14 =head1 ACKNOWLEDGEMENTS
799    
800     Some of codes are taken from other modules such as
801     HTTP::Header, Mail::Header.
802    
803 wakaba 1.1 =head1 LICENSE
804    
805     Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
806    
807     This program is free software; you can redistribute it and/or modify
808     it under the terms of the GNU General Public License as published by
809     the Free Software Foundation; either version 2 of the License, or
810     (at your option) any later version.
811    
812     This program is distributed in the hope that it will be useful,
813     but WITHOUT ANY WARRANTY; without even the implied warranty of
814     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
815     GNU General Public License for more details.
816    
817     You should have received a copy of the GNU General Public License
818     along with this program; see the file COPYING. If not, write to
819     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
820     Boston, MA 02111-1307, USA.
821    
822     =head1 CHANGE
823    
824     See F<ChangeLog>.
825 wakaba 1.30 $Date: 2002/07/04 06:38:21 $
826 wakaba 1.1
827     =cut
828    
829     1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24