/[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.28 - (hide annotations) (download)
Tue Jul 2 06:37:56 2002 UTC (22 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.27: +9 -5 lines
2002-07-02  Wakaba <w@suika.fam.cx>

	* Util.pm (decide_newline): New function.
	* Entity.pm (parse): Call Message::Util::decide_newline
	instead of local code.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24