/[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.42 - (hide annotations) (download)
Wed Nov 13 08:08:51 2002 UTC (22 years ago) by wakaba
Branch: MAIN
CVS Tags: before-dis2-200411, manakai-release-0-3-2, manakai-release-0-3-1, manakai-release-0-4-0, manakai-200612, msg-0-1, HEAD
Branch point for: branch-suikawiki-1, experimental-xml-parser-200401, stable
Changes since 1.41: +3 -3 lines
2002-08-05  Wakaba <w@suika.fam.cx>

	* Util.pm:
	- (sprintxf): Use Message::Util::Wide::unquote_if_quoted_string
	instead of Message::Util::unquote_if_quoted_string.
	- (Message::Util::Wide): New package.
	- (%Message::Util::Wide::REG): New hash.
	- (Message::Util::unquote_if_quoted_string): New function.
	- NOTE: "Wide" package is created to support utf8 string
	of perl 5.7.3 or later.  Utf8 string does not work
	only for [\x00-\xFF] regex of current functions,
	and this regex is used as (?:.|\x0D|\x0A).  (Without
	's' option, "." does not match with newline character.)
	When we can do away problematic code from all
	Message::* modules, we can also do away "Wide" package.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24