/[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.24 - (hide annotations) (download)
Tue Jun 11 13:01:21 2002 UTC (22 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.23: +14 -2 lines
2002-06-10  wakaba <w@suika.fam.cx>

	* Entity.pm (force_mime_entity): New option.
	* Header.pm (_item_match): -by => 'ns': New option.
	* Partial.pm: New module.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24