22 |
|
|
23 |
## Namespace support |
## Namespace support |
24 |
our %NS_phname2uri; ## PH-namespace name -> namespace URI |
our %NS_phname2uri; ## PH-namespace name -> namespace URI |
25 |
our %NS_uri2phpackage; ## namespace URI -> PH-package name |
our %NS_uri2package; ## namespace URI -> Package name |
26 |
|
our %NS_uri2phpackage; ## namespace URI -> PH-Package name |
27 |
require Message::Header::Default; ## Default namespace |
require Message::Header::Default; ## Default namespace |
28 |
|
|
29 |
## Initialize of this class -- called by constructors |
## Initialize of this class -- called by constructors |
41 |
-format => 'mail-rfc2822', |
-format => 'mail-rfc2822', |
42 |
-header_default_charset => 'iso-2022-int-1', |
-header_default_charset => 'iso-2022-int-1', |
43 |
-header_default_charset_input => 'iso-2022-int-1', |
-header_default_charset_input => 'iso-2022-int-1', |
44 |
|
-hook_init_fill_options => sub {}, |
45 |
|
-hook_stringify_fill_fields => sub {}, |
46 |
-linebreak_strict => 0, |
-linebreak_strict => 0, |
47 |
-line_length_max => 60, ## For folding |
-line_length_max => 60, ## For folding |
48 |
#ns_default_phuri |
#ns_default_phuri |
118 |
push @new_fields, ($name => $options{$name}); |
push @new_fields, ($name => $options{$name}); |
119 |
} |
} |
120 |
} |
} |
121 |
|
&{ $self->{option}->{hook_init_fill_options} } ($self, $self->{option}); |
122 |
$self->_init_by_format ($self->{option}->{format}, $self->{option}); |
$self->_init_by_format ($self->{option}->{format}, $self->{option}); |
123 |
# Make alternative representations of @header_order. This is used |
# Make alternative representations of @header_order. This is used |
124 |
# for sorting. |
# for sorting. |
143 |
} else { |
} else { |
144 |
$option->{field_sort} = 'good-practice'; |
$option->{field_sort} = 'good-practice'; |
145 |
} |
} |
146 |
} else { ## RFC 822 |
} elsif ($format =~ /mail|news|mime/) { ## RFC 822 |
147 |
$option->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-rfc822'}; |
$option->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-rfc822'}; |
148 |
} |
} |
149 |
if ($format =~ /uri-url-mailto/) { |
if ($format =~ /uri-url-mailto/) { |
192 |
} else { |
} else { |
193 |
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding}; |
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding}; |
194 |
} |
} |
195 |
|
my %option = (%{ $self->{option} }); |
196 |
|
$option{parse_all} = 0; |
197 |
for my $field (split /\x0D?\x0A/, $header) { |
for my $field (split /\x0D?\x0A/, $header) { |
198 |
if ($field =~ /$REG{M_fromline}/) { |
if ($field =~ /$REG{M_fromline}/) { |
199 |
my ($s,undef,$value) = $self->_value_to_arrayitem |
my ($s,undef,$value) = $self->_value_to_arrayitem |
200 |
('mail-from' => $1, $self->{option}); |
('mail-from' => $1, \%option); |
201 |
push @{$self->{value}}, $value if $s; |
push @{$self->{value}}, $value if $s; |
202 |
} elsif ($field =~ /$REG{M_field}/) { |
} elsif ($field =~ /$REG{M_field}/) { |
203 |
my ($name, $body) = ($1, $2); |
my ($name, $body) = ($1, $2); |
204 |
$body =~ s/$REG{WSP}+$//; |
$body =~ s/$REG{WSP}+$//; |
205 |
my ($s,undef,$value) = $self->_value_to_arrayitem |
my ($s,undef,$value) = $self->_value_to_arrayitem |
206 |
($name => $body, $self->{option}); |
($name => $body, \%option); |
207 |
push @{$self->{value}}, $value if $s; |
push @{$self->{value}}, $value if $s; |
208 |
} elsif (length $field) { |
} elsif (length $field) { |
209 |
my ($s,undef,$value) = $self->_value_to_arrayitem |
my ($s,undef,$value) = $self->_value_to_arrayitem |
210 |
('x-unknown' => $field, $self->{option}); |
('x-unknown' => $field, \%option); |
211 |
push @{$self->{value}}, $value if $s; |
push @{$self->{value}}, $value if $s; |
212 |
} |
} |
213 |
} |
} |
214 |
$self->_ns_associate_numerical_prefix; ## RFC 2774 namespace |
$self->_ns_associate_numerical_prefix; ## RFC 2774 namespace |
215 |
|
for (@{ $self->{value} }) { |
216 |
|
no strict 'refs'; |
217 |
|
$_->{name} |
218 |
|
= &{ ${ &_NS_uri2package ($_->{ns}).'::OPTION' }{n11n_name} } |
219 |
|
($self, &_NS_uri2package ($_->{ns}), $_->{name}); |
220 |
|
$_->{body} = $self->_parse_value ($_->{name} => $_->{body}, ns => $_->{ns}) |
221 |
|
if $self->{option}->{parse_all}; |
222 |
|
} |
223 |
$self; |
$self; |
224 |
} |
} |
225 |
|
|
301 |
my %o = %$option; #$o{parse} = 0; |
my %o = %$option; #$o{parse} = 0; |
302 |
my %l; |
my %l; |
303 |
for (keys %$list) { |
for (keys %$list) { |
304 |
my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', %o); |
my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', \%o); |
305 |
if ($s) { |
if ($s) { |
306 |
$l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1; |
$l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1; |
307 |
} else { |
} else { |
365 |
my $value = shift; return $value if ref $value; |
my $value = shift; return $value if ref $value; |
366 |
my %option = @_; |
my %option = @_; |
367 |
my $vtype; { no strict 'refs'; |
my $vtype; { no strict 'refs'; |
368 |
my $vt = ${&_NS_uri2phpackage ($option{ns}).'::OPTION'}{value_type}; |
my $vt = ${&_NS_uri2package ($option{ns}).'::OPTION'}{value_type}; |
369 |
if (ref $vt) { |
if (ref $vt) { |
370 |
$vtype = $vt->{$name} || $vt->{$self->{option}->{_VALTYPE_DEFAULT}}; |
$vtype = $vt->{$name} || $vt->{$self->{option}->{_VALTYPE_DEFAULT}}; |
371 |
} |
} |
377 |
my %vopt = %{$vtype->[1]} if ref $vtype->[1]; |
my %vopt = %{$vtype->[1]} if ref $vtype->[1]; |
378 |
if ($vpackage eq ':none:') { |
if ($vpackage eq ':none:') { |
379 |
return $value; |
return $value; |
380 |
} elsif (defined $value) { |
} elsif (length $value) { |
381 |
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
382 |
return $vpackage->parse ($value, |
return $vpackage->parse ($value, |
383 |
-format => $self->{option}->{format}, |
-format => $self->{option}->{format}, |
385 |
-field_name => $name, |
-field_name => $name, |
386 |
-header_default_charset => $self->{option}->{header_default_charset}, |
-header_default_charset => $self->{option}->{header_default_charset}, |
387 |
-header_default_charset_input => $self->{option}->{header_default_charset_input}, |
-header_default_charset_input => $self->{option}->{header_default_charset_input}, |
388 |
|
-internal_charset_name => $self->{option}->{internal_charset_name}, |
389 |
-parse_all => $self->{option}->{parse_all}, |
-parse_all => $self->{option}->{parse_all}, |
390 |
%vopt); |
%vopt); |
391 |
} else { |
} else { |
396 |
-field_name => $name, |
-field_name => $name, |
397 |
-header_default_charset => $self->{option}->{header_default_charset}, |
-header_default_charset => $self->{option}->{header_default_charset}, |
398 |
-header_default_charset_input => $self->{option}->{header_default_charset_input}, |
-header_default_charset_input => $self->{option}->{header_default_charset_input}, |
399 |
|
-internal_charset_name => $self->{option}->{internal_charset_name}, |
400 |
-parse_all => $self->{option}->{parse_all}, |
-parse_all => $self->{option}->{parse_all}, |
401 |
%vopt); |
%vopt); |
402 |
} |
} |
431 |
if (defined $_[0]) { |
if (defined $_[0]) { |
432 |
no strict 'refs'; |
no strict 'refs'; |
433 |
$self->{option}->{ns_default_phuri} = $_[0]; |
$self->{option}->{ns_default_phuri} = $_[0]; |
434 |
$self->_ns_load_ph (${&_NS_uri2phpackage ($self->{option}->{ns_default_phuri}).'::OPTION'}{namespace_phname}); |
$self->_ns_load_ph (${&_NS_uri2package ($self->{option}->{ns_default_phuri}).'::OPTION'}{namespace_phname}); |
435 |
} |
} |
436 |
$self->{option}->{ns_default_phuri}; |
$self->{option}->{ns_default_phuri}; |
437 |
} |
} |
472 |
if (ref $value eq 'ARRAY') { |
if (ref $value eq 'ARRAY') { |
473 |
($value, %$value_option) = @$value; |
($value, %$value_option) = @$value; |
474 |
} |
} |
475 |
my $nsuri = $self->{option}->{ns_default_phuri}; |
my $default_ns = $option->{ns_default_phuri}; |
476 |
|
my $nsuri = $default_ns; |
477 |
$name =~ s/^$REG{WSP}+//; $name =~ s/$REG{WSP}+$//; |
$name =~ s/^$REG{WSP}+//; $name =~ s/$REG{WSP}+$//; |
478 |
|
|
479 |
no strict 'refs'; |
no strict 'refs'; |
481 |
$nsuri = $value_option->{ns}; |
$nsuri = $value_option->{ns}; |
482 |
} elsif ($option->{ns}) { |
} elsif ($option->{ns}) { |
483 |
$nsuri = $option->{ns}; |
$nsuri = $option->{ns}; |
484 |
} elsif (($option->{ns_default_phuri} eq $self->{ns}->{uri2phname}->{'x-http'} |
} elsif (($default_ns eq $self->{ns}->{uri2phname}->{'x-http'} |
485 |
&& $name =~ s/^([0-9]+)-//) |
&& $name =~ s/^([0-9]+)-//) |
486 |
|| ($name =~ s/^x-http-([0-9]+)-//i)) { ## Numric namespace prefix, RFC 2774 |
|| ($name =~ s/^x-http-([0-9]+)-//i)) { ## Numric namespace prefix, RFC 2774 |
487 |
my $prefix = 0+$1; |
my $prefix = 0+$1; |
490 |
$self->{ns}->{number2uri}->{ $prefix } = 'urn:x-suika-fam-cx:msgpm:header:x-temp:'.$prefix; |
$self->{ns}->{number2uri}->{ $prefix } = 'urn:x-suika-fam-cx:msgpm:header:x-temp:'.$prefix; |
491 |
$nsuri = $self->{ns}->{number2uri}->{ $prefix }; |
$nsuri = $self->{ns}->{number2uri}->{ $prefix }; |
492 |
} |
} |
493 |
} elsif (($name =~ s/^([Xx]-[A-Za-z]+|[A-YZa-yz][A-Za-z]*)- |
} elsif ( |
494 |
([Xx]-[A-Za-z]+|[A-YZa-yz][A-Za-z]*)-//x) |
${ &_NS_uri2package ($default_ns).'::OPTION' }{use_ph_namespace} |
495 |
|| $name =~ s/^([Xx]-[A-Za-z]+|[A-Za-z]+)-//) { |
&& ( |
496 |
|
($name =~ s/^([Xx]-[A-Za-z0-9]+|[A-Za-z]*[A-WYZa-wyz0-9][A-Za-z0-9]*)- |
497 |
|
([Xx]-[A-Za-z0-9]+|[A-Za-z0-9]*[A-WYZa-wyz0-9][A-Za-z0-9]*)-//x) |
498 |
|
|| $name =~ s/^([Xx]-[A-Za-z0-9]+|[A-Za-z0-9]*[A-WYZa-wyz0-9][A-Za-z0-9]*)-// |
499 |
|
)) { |
500 |
my ($prefix1, $prefix2) = ($1, $2); |
my ($prefix1, $prefix2) = ($1, $2); |
501 |
my $original_prefix = $&; my $one_prefix = 0; |
my $original_prefix = $&; my $one_prefix = 0; |
502 |
unless ($prefix2) { |
unless ($prefix2) { |
503 |
$prefix2 = $prefix1; |
$prefix2 = $prefix1; |
504 |
$prefix1 = $self->{ns}->{uri2phname}->{ $option->{ns_default_phuri} }; |
$prefix1 = $self->{ns}->{uri2phname}->{ $default_ns }; |
505 |
$one_prefix = 1; |
$one_prefix = 1; |
506 |
} |
} |
507 |
my $prefix |
my $prefix |
508 |
= &{ ${ &_NS_uri2phpackage ($nsuri).'::OPTION' }{n11n_prefix} } |
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_prefix} } |
509 |
($self, &_NS_uri2phpackage ($nsuri), $prefix1.'-'.$prefix2); |
($self, &_NS_uri2package ($nsuri), $prefix1.'-'.$prefix2); |
510 |
$self->_ns_load_ph ($prefix); |
$self->_ns_load_ph ($prefix); |
511 |
$nsuri = $self->{ns}->{phname2uri}->{ $prefix }; |
$nsuri = $self->{ns}->{phname2uri}->{ $prefix }; |
512 |
unless ($nsuri) { |
unless ($nsuri) { |
513 |
$nsuri = $self->{option}->{ns_default_phuri}; |
$nsuri = $default_ns; |
514 |
$prefix |
$prefix |
515 |
= &{ ${ &_NS_uri2phpackage ($nsuri).'::OPTION' }{n11n_prefix} } |
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_prefix} } |
516 |
($self, &_NS_uri2phpackage ($nsuri), $one_prefix? $prefix2: $prefix1); |
($self, &_NS_uri2package ($nsuri), $one_prefix? $prefix2: $prefix1); |
517 |
$self->_ns_load_ph ($prefix); |
$self->_ns_load_ph ($prefix); |
518 |
$nsuri = $self->{ns}->{phname2uri}->{ $prefix }; |
$nsuri = $self->{ns}->{phname2uri}->{ $prefix }; |
519 |
if ($nsuri) { |
if ($nsuri) { |
520 |
$name = $prefix2 . '-' . $name unless $one_prefix; |
$name = $prefix2 . '-' . $name unless $one_prefix; |
521 |
} else { |
} else { |
522 |
$name = $original_prefix . $name; |
$name = $original_prefix . $name; |
523 |
$nsuri = $self->{option}->{ns_default_phuri}; |
$nsuri = $default_ns; |
524 |
} |
} |
525 |
} |
} |
526 |
} |
} |
527 |
$name |
$name |
528 |
= &{${&_NS_uri2phpackage ($nsuri).'::OPTION'}{n11n_name}} |
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_name} } |
529 |
($self, &_NS_uri2phpackage ($nsuri), $name); |
($self, &_NS_uri2package ($nsuri), $name); |
530 |
Carp::croak "$name: invalid field-name" |
Carp::croak "$name: invalid field-name" |
531 |
if $option->{field_name_validation} |
if $option->{field_name_validation} |
532 |
&& $name =~ /$REG{$option->{field_name_unsafe_rule}}/; |
&& $name =~ /$REG{ $option->{field_name_unsafe_rule} }/; |
533 |
$value = $self->_parse_value ($name => $value, ns => $nsuri) |
$value = $self->_parse_value ($name => $value, ns => $nsuri) |
534 |
if $$option{parse} || $$option{parse_all}; |
if $option->{parse} || $option->{parse_all}; |
535 |
$$option{parse} = 0; |
$option->{parse} = 0; |
536 |
(1, $name.':'.$nsuri => {name => $name, body => $value, ns => $nsuri}); |
(1, $name.':'.$nsuri => {name => $name, body => $value, ns => $nsuri}); |
537 |
} |
} |
538 |
*_add_hash_check = \&_value_to_arrayitem; |
*_add_hash_check = \&_value_to_arrayitem; |
643 |
my $self = shift; |
my $self = shift; |
644 |
my $s = shift; |
my $s = shift; |
645 |
$s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//; |
$s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//; |
646 |
$s = lc $s unless ${&_NS_uri2phpackage ($self->{option}->{ns_default_phuri}).'::OPTION'}{case_sensible}; |
$s = lc $s unless ${&_NS_uri2package ($self->{option}->{ns_default_phuri}).'::OPTION'}{case_sensible}; |
647 |
$s; |
$s; |
648 |
} |
} |
649 |
|
|
673 |
my %params = @_; |
my %params = @_; |
674 |
my %option = %{$self->{option}}; |
my %option = %{$self->{option}}; |
675 |
$option{format} = $params{-format} if $params{-format}; |
$option{format} = $params{-format} if $params{-format}; |
676 |
$self->_init_by_format ($option{format}, \%option); |
$self->_init_by_format ($option{format}, \%option) |
677 |
|
if $self->{option}->{format} ne $option{format}; |
678 |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
679 |
|
## Fill required fields |
680 |
|
my %exist; |
681 |
|
for ($self->field_name_list) {$exist{$_} = 1} |
682 |
|
&{ $option{hook_stringify_fill_fields} } ($self, \%exist, \%option); |
683 |
my @ret; |
my @ret; |
684 |
## RFC 2774 numerical field name prefix |
## RFC 2774 numerical field name prefix |
685 |
my %nprefix; |
my %nprefix; |
687 |
%nprefix = reverse %{ $self->{ns}->{number2uri} }; |
%nprefix = reverse %{ $self->{ns}->{number2uri} }; |
688 |
my $i = (sort { $a <=> $b } keys %{ $self->{ns}->{number2uri} })[-1] + 1; |
my $i = (sort { $a <=> $b } keys %{ $self->{ns}->{number2uri} })[-1] + 1; |
689 |
$i = 10 if $i < 10; |
$i = 10 if $i < 10; |
690 |
my $hprefix = ${ &_NS_uri2phpackage |
my $hprefix = ${ &_NS_uri2package |
691 |
($self->{ns}->{phname2uri}->{'x-http'}) |
($self->{ns}->{phname2uri}->{'x-http'}) |
692 |
.'::OPTION' } {namespace_phname_goodcase}; |
.'::OPTION' } {namespace_phname_goodcase}; |
693 |
for my $uri (keys %nprefix) { |
for my $uri (keys %nprefix) { |
711 |
return unless length $name; |
return unless length $name; |
712 |
return if $option{output_mail_from} && $name eq 'mail-from'; |
return if $option{output_mail_from} && $name eq 'mail-from'; |
713 |
$body = '' if !$option{output_bcc} && $name eq 'bcc'; |
$body = '' if !$option{output_bcc} && $name eq 'bcc'; |
714 |
my $nspackage = &_NS_uri2phpackage ($nsuri); |
my $nspackage = &_NS_uri2package ($nsuri); |
715 |
my $oname; ## Outputed field-name |
my $oname; ## Outputed field-name |
716 |
my $prefix = $nprefix{ $nsuri } |
my $prefix = $nprefix{ $nsuri } |
717 |
|| ${$nspackage.'::OPTION'} {namespace_phname_goodcase} |
|| ${$nspackage.'::OPTION'} {namespace_phname_goodcase} |
718 |
|| $self->{ns}->{uri2phname}->{ $nsuri }; |
|| $self->{ns}->{uri2phname}->{ $nsuri }; |
719 |
my $default_prefix = ${ &_NS_uri2phpackage ($option{ns_default_phuri}) |
my $default_prefix = ${ &_NS_uri2package ($option{ns_default_phuri}) |
720 |
.'::OPTION'} {namespace_phname_goodcase}; |
.'::OPTION'} {namespace_phname_goodcase}; |
721 |
$prefix = '' if $prefix eq $default_prefix; |
$prefix = '' if $prefix eq $default_prefix; |
722 |
$prefix =~ s/^\Q$default_prefix\E-//; |
$prefix =~ s/^\Q$default_prefix\E-//; |
|
#$prefix = undef if $nsuri eq $option{ns_default_phuri}; |
|
|
#if ($prefix && $prefix eq $nprefix{ $nsuri }) { ## RFC 2774 prefix |
|
|
# $prefix = ${ &_NS_uri2phpackage ($self->{ns}->{phname2uri}->{'x-http'}) |
|
|
# .'::OPTION' } {namespace_phname_goodcase} . '-' . $prefix |
|
|
# unless $option{ns_default_phuri} eq $self->{ns}->{phname2uri}->{'x-http'}; |
|
|
#} |
|
723 |
my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase}; |
my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase}; |
724 |
if (ref $gc) { $oname = &$gc ($self, $nspackage, $name, \%option) } |
if (ref $gc) { $oname = &$gc ($self, $nspackage, $name, \%option) } |
725 |
else { $oname = $name } |
else { $oname = $name } |
885 |
$NS_uri2phpackage{$_[0]} |
$NS_uri2phpackage{$_[0]} |
886 |
|| $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}}; |
|| $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}}; |
887 |
} |
} |
888 |
|
sub _NS_uri2package ($) { |
889 |
|
$NS_uri2package{$_[0]} |
890 |
|
|| $NS_uri2phpackage{$_[0]} |
891 |
|
|| $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}}; |
892 |
|
} |
893 |
|
|
894 |
=head2 $self->clone () |
=head2 $self->clone () |
895 |
|
|