1 |
|
|
2 |
=head1 NAME |
=head1 NAME |
3 |
|
|
4 |
Message::Header Perl module |
Message::Header --- A Perl Module for Internet Message Headers |
|
|
|
|
=head1 DESCRIPTION |
|
|
|
|
|
Perl module for RFC 822/2822 message C<header>. |
|
5 |
|
|
6 |
=cut |
=cut |
7 |
|
|
8 |
package Message::Header; |
package Message::Header; |
9 |
use strict; |
use strict; |
10 |
use vars qw($VERSION %REG); |
use vars qw(%DEFAULT @ISA %REG $VERSION); |
11 |
$VERSION = '1.00'; |
$VERSION=do{my @r=(q$Revision$=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
12 |
use Carp (); |
require Message::Field::Structured; ## This may seem silly:-) |
13 |
use overload '@{}' => sub { shift->_delete_empty_field->{field} }, |
push @ISA, qw(Message::Field::Structured); |
14 |
'""' => sub { shift->stringify }, |
|
15 |
fallback => 1; |
%REG = %Message::Util::REG; |
16 |
|
$REG{M_field} = qr/^([^\x3A]+):$REG{FWS}([\x00-\xFF]*)$/; |
17 |
$REG{WSP} = qr/[\x09\x20]/; |
$REG{M_fromline} = qr/^\x3E?From$REG{WSP}+([\x00-\xFF]*)$/; |
18 |
$REG{FWS} = qr/[\x09\x20]*/; |
$REG{ftext} = qr/[\x21-\x39\x3B-\x7E]+/; ## [2]822 |
19 |
$REG{M_field} = qr/^([^\x3A]+):$REG{FWS}([\x00-\xFF]*)$/; |
$REG{NON_ftext} = qr/[^\x21-\x39\x3B-\x7E]/; ## [2]822 |
20 |
$REG{M_fromline} = qr/^\x3E?From$REG{WSP}+([\x00-\xFF]*)$/; |
$REG{NON_ftext_usefor} = qr/[^0-9A-Za-z-]/; ## name-character |
21 |
$REG{UNSAFE_field_name} = qr/[\x00-\x20\x3A\x7F-\xFF]/; |
$REG{NON_ftext_http} = $REG{NON_http_token}; |
22 |
|
|
23 |
=head2 options |
## Namespace support |
24 |
|
our %NS_phname2uri; ## PH-namespace name -> namespace URI |
25 |
These options can be getten/set by C<get_option>/C<set_option> |
our %NS_uri2package; ## namespace URI -> Package name |
26 |
method. |
our %NS_uri2phpackage; ## namespace URI -> PH-Package name |
27 |
|
require Message::Header::Default; ## Default namespace |
28 |
=head3 capitalize = 0/1 |
|
29 |
|
## Initialize of this class -- called by constructors |
30 |
(First character of) C<field-name> is capitalized |
%DEFAULT = ( |
31 |
when C<stringify>. (Default = 1) |
-_HASH_NAME => 'value', |
32 |
|
-_METHODS => [qw|field field_exist field_type add replace count delete subject id is|], |
33 |
=head3 fold_length = numeric value |
-_MEMBERS => [qw|value|], |
34 |
|
-_VALTYPE_DEFAULT => ':default', |
35 |
Length of line used to fold. (Default = 70) |
-by => 'name', |
36 |
|
-field_format_pattern => '%s: %s', |
37 |
=head3 mail_from = 0/1 |
-field_name_case_sensible => 0, |
38 |
|
-field_name_unsafe_rule => 'NON_ftext', |
39 |
Outputs "From " line (known as Un*x From, Mail-From, and so on) |
-field_name_validation => 0, |
40 |
when C<stringify>. (Default = 0) |
-field_sort => 0, |
41 |
|
-format => 'mail-rfc2822', |
42 |
=cut |
-header_default_charset => 'iso-2022-int-1', |
43 |
|
-header_default_charset_input => 'iso-2022-int-1', |
44 |
|
-hook_init_fill_options => sub {}, |
45 |
|
-hook_stringify_fill_fields => sub {}, |
46 |
|
-linebreak_strict => 0, |
47 |
|
-line_length_max => 60, ## For folding |
48 |
|
#ns_default_phuri |
49 |
|
-output_bcc => 0, |
50 |
|
-output_folding => 1, |
51 |
|
-output_mail_from => 0, |
52 |
|
#parse_all |
53 |
|
-translate_underscore => 1, |
54 |
|
#uri_mailto_safe |
55 |
|
-uri_mailto_safe_level => 4, |
56 |
|
-use_folding => 1, |
57 |
|
#value_type |
58 |
|
); |
59 |
|
|
60 |
=head1 CONSTRUCTORS |
=head1 CONSTRUCTORS |
61 |
|
|
65 |
|
|
66 |
=cut |
=cut |
67 |
|
|
|
## Initialize |
|
|
my %DEFAULT = ( |
|
|
capitalize => 1, |
|
|
fold => 1, |
|
|
fold_length => 70, |
|
|
field_format_pattern => '%s: %s', |
|
|
#field_type => {}, |
|
|
format => 'mail-rfc2822', |
|
|
linebreak_strict => 0, |
|
|
mail_from => 0, |
|
|
output_bcc => 0, |
|
|
parse_all => 0, |
|
|
sort => 'none', |
|
|
translate_underscore => 1, |
|
|
uri_mailto_safe => { |
|
|
## 1 all (no check) 2 no trace & bcc & from |
|
|
## 3 no sender's info 4 (default) (currently not used) |
|
|
## 5 only a few |
|
|
':default' => 4, |
|
|
'cc' => 4, |
|
|
'bcc' => 1, |
|
|
'body' => 1, |
|
|
'comment' => 5, |
|
|
'content-id' => 1, |
|
|
'date' => 1, |
|
|
'from' => 1, |
|
|
'keywords' => 5, |
|
|
'list-id' => 1, |
|
|
'mail-from' => 1, |
|
|
'message-id' => 1, |
|
|
'received' => 1, |
|
|
'resent-bcc' => 1, |
|
|
'resent-date' => 1, |
|
|
'resent-from' => 1, |
|
|
'resent-sender' => 1, |
|
|
'return-path' => 1, |
|
|
'sender' => 1, |
|
|
'subject' => 5, |
|
|
'summary' => 5, |
|
|
'to' => 4, |
|
|
'user-agent' => 3, |
|
|
'x-face' => 2, |
|
|
'x-mailer' => 3, |
|
|
'x-nsubject' => 5, |
|
|
'x-received' => 1, |
|
|
'x400-received' => 1, |
|
|
}, |
|
|
uri_mailto_safe_level => 4, |
|
|
validate => 1, |
|
|
); |
|
|
$DEFAULT{field_type} = { |
|
|
':DEFAULT' => 'Message::Field::Unstructured', |
|
|
|
|
|
received => 'Message::Field::Received', |
|
|
'x-received' => 'Message::Field::Received', |
|
|
|
|
|
'content-type' => 'Message::Field::ContentType', |
|
|
p3p => 'Message::Field::Params', |
|
|
'auto-submitted' => 'Message::Field::ValueParams', |
|
|
'content-disposition' => 'Message::Field::ValueParams', |
|
|
link => 'Message::Field::ValueParams', |
|
|
archive => 'Message::Field::ValueParams', |
|
|
'x-face-type' => 'Message::Field::ValueParams', |
|
|
'x-mozilla-draft-info' => 'Message::Field::ValueParams', |
|
|
|
|
|
subject => 'Message::Field::Subject', |
|
|
'x-nsubject' => 'Message::Field::Subject', |
|
|
|
|
|
'list-software' => 'Message::Field::UA', |
|
|
'user-agent' => 'Message::Field::UA', |
|
|
'resent-user-agent' => 'Message::Field::UA', |
|
|
server => 'Message::Field::UA', |
|
|
|
|
|
## A message id |
|
|
'content-id' => 'Message::Field::MsgID', |
|
|
'message-id' => 'Message::Field::MsgID', |
|
|
'resent-message-id' => 'Message::Field::MsgID', |
|
|
|
|
|
## Numeric value |
|
|
'content-length' => 'Message::Field::Numval', |
|
|
lines => 'Message::Field::Numval', |
|
|
'max-forwards' => 'Message::Field::Numval', |
|
|
'mime-version' => 'Message::Field::Numval', |
|
|
'x-jsmail-priority' => 'Message::Field::Numval', |
|
|
'x-mail-count' => 'Message::Field::Numval', |
|
|
'x-ml-count' => 'Message::Field::Numval', |
|
|
'x-priority' => 'Message::Field::Numval', |
|
|
|
|
|
path => 'Message::Field::Path', |
|
|
}; |
|
|
for (qw(archive cancel-lock content-features content-md5 |
|
|
disposition-notification-options encoding |
|
|
importance injector-info |
|
|
pics-label posted-and-mailed precedence list-id message-type |
|
|
original-recipient priority x-list-id |
|
|
sensitivity status x-face x-msmail-priority xref)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::Structured'} |
|
|
## Not supported yet, but to be supported... |
|
|
# x-list: unstructured, ml name |
|
|
for (qw(abuse-reports-to apparently-to approved approved-by bcc cc complaints-to |
|
|
delivered-to disposition-notification-to envelope-to |
|
|
errors-to from mail-copies-to mail-followup-to mail-reply-to |
|
|
notice-requested-upon-delivery-to read-receipt-to register-mail-reply-requested-by |
|
|
reply-to resent-bcc |
|
|
resent-cc resent-to resent-from resent-sender return-path |
|
|
return-receipt-to return-receipt-requested-to sender to x-abuse-reports-to |
|
|
x-admin x-approved x-beenthere x-confirm-reading-to |
|
|
x-complaints-to x-envelope-from x-envelope-sender |
|
|
x-envelope-to x-ml-address x-ml-command x-ml-to x-nfrom x-nto |
|
|
x-rcpt-to x-sender x-x-sender)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::Addresses'} |
|
|
for (qw(client-date date date-received delivery-date expires |
|
|
expire-date nntp-posting-date posted posted-date received-date |
|
|
reply-by resent-date |
|
|
x-originalarrivaltime x-tcup-date)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::Date'} |
|
|
for (qw(article-updates in-reply-to |
|
|
obsoletes references replaces see-also supersedes)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::MsgIDs'} |
|
|
for (qw(accept accept-charset accept-encoding accept-language |
|
|
content-language |
|
|
content-transfer-encoding encrypted followup-to keywords |
|
|
list-archive list-digest list-help list-owner |
|
|
list-post list-subscribe list-unsubscribe list-url uri newsgroups |
|
|
posted-to)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::CSV'} |
|
|
for (qw(x-brother x-boss x-classmate x-daughter x-dearfriend x-favoritesong |
|
|
x-friend x-me |
|
|
x-moe x-respect |
|
|
x-sublimate x-son x-sister x-wife)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::CSV'} ## NOT M::F::XMOE! |
|
|
for (qw(content-alias content-base content-location location referer |
|
|
url x-home-page x-http_referer |
|
|
x-info x-pgp-key x-ml-url x-uri x-url x-web)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::URI'} |
|
|
|
|
|
my %header_goodcase = ( |
|
|
'article-i.d.' => 'Article-I.D.', |
|
|
'content-id' => 'Content-ID', |
|
|
'content-md5' => 'Content-MD5', |
|
|
'content-sgml-entity' => 'Content-SGML-Entity', |
|
|
etag => 'ETag', |
|
|
fax => 'FAX', |
|
|
'pics-label' => 'PICS-Label', |
|
|
'list-url' => 'List-URL', |
|
|
'list-id' => 'List-ID', |
|
|
'message-id' => 'Message-ID', |
|
|
'mime-version' => 'MIME-Version', |
|
|
'nic' => 'NIC', |
|
|
'nntp-posting-date' => 'NNTP-Posting-Date', |
|
|
'nntp-posting-host' => 'NNTP-Posting-Host', |
|
|
'resent-message-id' => 'Resent-Message-ID', |
|
|
te => 'TE', |
|
|
url => 'URL', |
|
|
'www-authenticate' => 'WWW-Authenticate', |
|
|
'x-dearfriend' => 'X-DearFriend', |
|
|
'x-mime-autoconverted' => 'X-MIME-Autoconverted', |
|
|
'x-nntp-posting-date' => 'X-NNTP-Posting-Date', |
|
|
'x-nntp-posting-host' => 'X-NNTP-Posting-Host', |
|
|
'x-uri' => 'X-URI', |
|
|
'x-url' => 'X-URL', |
|
|
); |
|
|
$DEFAULT{capitalize} = sub { |
|
|
my $self = shift; |
|
|
my $name = shift; |
|
|
if ($header_goodcase{$name}) { |
|
|
return $header_goodcase{$name}; |
|
|
} |
|
|
$name =~ s/(?:^|-)cgi-/uc $&/ge; |
|
|
$name =~ s/(?:^|-)[a-z]/uc $&/ge; |
|
|
$name; |
|
|
}; |
|
|
|
|
|
## taken from L<HTTP::Header> |
|
|
# "Good Practice" order of HTTP message headers: |
|
|
# - General-Headers |
|
|
# - Request-Headers |
|
|
# - Response-Headers |
|
|
# - Entity-Headers |
|
|
# (From draft-ietf-http-v11-spec-rev-01, Nov 21, 1997) |
|
|
my @header_order = qw( |
|
|
mail-from x-envelope-from relay-version path status |
|
|
|
|
|
cache-control connection date pragma transfer-encoding upgrade trailer via |
|
|
|
|
|
accept accept-charset accept-encoding accept-language |
|
|
authorization expect from host |
|
|
if-modified-since if-match if-none-match if-range if-unmodified-since |
|
|
max-forwards proxy-authorization range referer te user-agent |
|
|
|
|
|
accept-ranges age location proxy-authenticate retry-after server vary |
|
|
warning www-authenticate |
|
|
|
|
|
mime-version |
|
|
allow content-base content-encoding content-language content-length |
|
|
content-location content-md5 content-range content-type |
|
|
etag expires last-modified content-style-type content-script-type |
|
|
link |
|
|
|
|
|
xref |
|
|
); |
|
|
my %header_order; |
|
|
|
|
68 |
sub _init ($;%) { |
sub _init ($;%) { |
69 |
my $self = shift; |
my $self = shift; |
70 |
my %options = @_; |
my %options = @_; |
71 |
$self->{field} = []; |
my $DEFAULT = Message::Util::make_clone (\%DEFAULT); |
72 |
$self->{option} = \%DEFAULT; |
$self->SUPER::_init (%$DEFAULT, %options); |
73 |
|
$self->{value} = []; |
74 |
|
$self->_ns_load_ph ('default'); |
75 |
|
$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 |
|
unless $self->{option}->{ns_default_phuri}; |
79 |
|
|
80 |
|
## For text/rfc822-headers |
81 |
|
if (ref $options{entity_header}) { |
82 |
|
$self->{entity_header} = $options{entity_header}; |
83 |
|
delete $options{entity_header}; |
84 |
|
} |
85 |
my @new_fields = (); |
my @new_fields = (); |
86 |
for my $name (keys %options) { |
for my $name (keys %options) { |
87 |
if (substr ($name, 0, 1) eq '-') { |
unless (substr ($name, 0, 1) eq '-') { |
|
$self->{option}->{substr ($name, 1)} = $options{$name}; |
|
|
} else { |
|
88 |
push @new_fields, ($name => $options{$name}); |
push @new_fields, ($name => $options{$name}); |
89 |
} |
} |
90 |
} |
} |
91 |
|
&{ $self->{option}->{hook_init_fill_options} } ($self, $self->{option}); |
92 |
$self->_init_by_format ($self->{option}->{format}, $self->{option}); |
$self->_init_by_format ($self->{option}->{format}, $self->{option}); |
|
# Make alternative representations of @header_order. This is used |
|
|
# for sorting. |
|
|
my $i = 1; |
|
|
for (@header_order) { |
|
|
$header_order{$_} = $i++ unless $header_order{$_}; |
|
|
} |
|
|
|
|
93 |
$self->add (@new_fields, -parse => $self->{option}->{parse_all}) |
$self->add (@new_fields, -parse => $self->{option}->{parse_all}) |
94 |
if $#new_fields > -1; |
if $#new_fields > -1; |
95 |
} |
} |
97 |
sub _init_by_format ($$\%) { |
sub _init_by_format ($$\%) { |
98 |
my $self = shift; |
my $self = shift; |
99 |
my ($format, $option) = @_; |
my ($format, $option) = @_; |
100 |
if ($format =~ /rfc822/) { |
if ($format =~ /http/) { |
101 |
$header_goodcase{bcc} = 'bcc'; |
$option->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-http'}; |
102 |
$header_goodcase{cc} = 'cc'; |
if ($format =~ /cgi/) { |
103 |
$header_goodcase{'resent-bcc'} = 'Resent-bcc'; |
#unshift @header_order, qw(content-type location); |
104 |
$header_goodcase{'resent-cc'} = 'Resent-cc'; |
$option->{field_sort} = 'good-practice'; |
105 |
} elsif ($format =~ /cgi/) { |
$option->{use_folding} = 0; |
106 |
unshift @header_order, qw(content-type location); |
} else { |
107 |
$option->{sort} = 'good-practice'; |
$option->{field_sort} = 'good-practice'; |
108 |
$option->{fold} = 0; |
} |
109 |
} elsif ($format =~ /http/) { |
} elsif ($format =~ /mail|news|mime/) { ## RFC 822 |
110 |
$option->{sort} = 'good-practice'; |
$option->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-rfc822'}; |
111 |
} |
} |
112 |
if ($format =~ /uri-url-mailto/) { |
if ($format =~ /uri-url-mailto/) { |
113 |
$option->{output_bcc} = 0; |
$option->{output_bcc} = 0; |
|
$option->{capitalize} = 0; |
|
114 |
$option->{field_format_pattern} = '%s=%s'; |
$option->{field_format_pattern} = '%s=%s'; |
115 |
$option->{fold} = sub { |
$option->{output_folding} = sub { |
116 |
$_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge; |
$_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge; |
117 |
$_[1]; |
$_[1]; |
118 |
}; |
}; ## Yes, this is not folding! |
119 |
} |
} |
120 |
} |
} |
121 |
|
|
122 |
=item Message::Header->new ([%initial-fields/options]) |
=item $msg = Message::Header->new ([%initial-fields/options]) |
123 |
|
|
124 |
Constructs a new C<Message::Headers> object. You might pass some initial |
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. |
C<field-name>-C<field-body> pairs and/or options as parameters to the constructor. |
135 |
|
|
136 |
=cut |
=cut |
137 |
|
|
138 |
sub new ($;%) { |
## Inherited |
|
my $class = shift; |
|
|
my $self = bless {}, $class; |
|
|
$self->_init (@_); |
|
|
$self; |
|
|
} |
|
139 |
|
|
140 |
=item Message::Header->parse ($header, [%initial-fields/options]) |
=item $msg = Message::Header->parse ($header, [%initial-fields/options]) |
141 |
|
|
142 |
Parses given C<header> and constructs a new C<Message::Headers> |
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 |
object. You might pass some additional C<field-name>-C<field-body> pairs |
149 |
my $class = shift; |
my $class = shift; |
150 |
my $header = shift; |
my $header = shift; |
151 |
my $self = bless {}, $class; |
my $self = bless {}, $class; |
152 |
$self->_init (@_); ## BUG: don't check linebreak_strict |
$self->_init (@_); |
153 |
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos; ## unfold |
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 |
|
my %option = (%{ $self->{option} }); |
159 |
|
$option{parse_all} = 0; |
160 |
for my $field (split /\x0D?\x0A/, $header) { |
for my $field (split /\x0D?\x0A/, $header) { |
161 |
if ($field =~ /$REG{M_fromline}/) { |
if ($field =~ /$REG{M_fromline}/) { |
162 |
my $body = $1; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
163 |
$body = $self->_field_body ($body, 'mail-from') |
('mail-from' => $1, \%option); |
164 |
if $self->{option}->{parse_all}; |
push @{$self->{value}}, $value if $s; |
|
push @{$self->{field}}, {name => 'mail-from', body => $body}; |
|
165 |
} elsif ($field =~ /$REG{M_field}/) { |
} elsif ($field =~ /$REG{M_field}/) { |
166 |
my ($name, $body) = (lc $1, $2); |
my ($name, $body) = ($1, $2); |
|
$name =~ s/$REG{WSP}+$//; |
|
167 |
$body =~ s/$REG{WSP}+$//; |
$body =~ s/$REG{WSP}+$//; |
168 |
$body = $self->_field_body ($body, $name) if $self->{option}->{parse_all}; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
169 |
push @{$self->{field}}, {name => $name, body => $body}; |
($name => $body, \%option); |
170 |
} |
push @{$self->{value}}, $value if $s; |
171 |
|
} elsif (length $field) { |
172 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
173 |
|
('x-unknown' => $field, \%option); |
174 |
|
push @{$self->{value}}, $value if $s; |
175 |
|
} |
176 |
|
} |
177 |
|
$self->_ns_associate_numerical_prefix; ## RFC 2774 namespace |
178 |
|
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 |
$self; |
$self; |
187 |
} |
} |
188 |
|
|
189 |
=item Message::Header->parse_array (\@header, [%initial-fields/options]) |
=item $msg = Message::Header->parse_array (\@header, [%initial-fields/options]) |
190 |
|
|
191 |
Parses given C<header> and constructs a new C<Message::Headers> |
Parses given C<header> and constructs a new C<Message::Headers> |
192 |
object. Same as C<Message::Header-E<lt>parse> but this method |
object. Same as C<Message::Header-E<lt>parse> but this method |
205 |
$self->_init (@_); |
$self->_init (@_); |
206 |
while (1) { |
while (1) { |
207 |
my $field = shift @$header; |
my $field = shift @$header; |
208 |
while (1) { |
if ($self->{option}->{use_folding}) { |
209 |
if ($$header[0] =~ /^$REG{WSP}/) { |
while (1) { |
210 |
$field .= shift @$header; |
if ($$header[0] =~ /^$REG{WSP}/) { |
211 |
} else {last} |
$field .= shift @$header; |
212 |
|
} else {last} |
213 |
|
} |
214 |
} |
} |
215 |
if ($self->{option}->{linebreak_strict}) { |
if ($self->{option}->{linebreak_strict}) { |
216 |
$field =~ s/\x0D\x0A//g; |
$field =~ s/\x0D\x0A//g; |
217 |
} else { |
} else { |
218 |
$field =~ tr/\x0D\x0A//d; |
$field =~ tr/\x0D\x0A//d; |
219 |
} |
} |
220 |
|
local $self->{option}->{parse} = $self->{option}->{parse_all}; |
221 |
if ($field =~ /$REG{M_fromline}/) { |
if ($field =~ /$REG{M_fromline}/) { |
222 |
my $body = $1; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
223 |
$body = $self->_field_body ($body, 'mail-from') |
('mail-from' => $1, $self->{option}); |
224 |
if $self->{option}->{parse_all}; |
push @{$self->{value}}, $value if $s; |
|
push @{$self->{field}}, {name => 'mail-from', body => $body}; |
|
225 |
} elsif ($field =~ /$REG{M_field}/) { |
} elsif ($field =~ /$REG{M_field}/) { |
226 |
my ($name, $body) = (lc $1, $2); |
my ($name, $body) = ($self->_n11n_field_name ($1), $2); |
|
$name =~ s/$REG{WSP}+$//; |
|
227 |
$body =~ s/$REG{WSP}+$//; |
$body =~ s/$REG{WSP}+$//; |
228 |
$body = $self->_field_body ($body, $name) if $self->{option}->{parse_all}; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
229 |
push @{$self->{field}}, {name => $name, body => $body}; |
($name => $body, $self->{option}); |
230 |
|
push @{$self->{value}}, $value if $s; |
231 |
|
} 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 |
} |
} |
236 |
last if $#$header < 0; |
last if $#$header < 0; |
237 |
} |
} |
238 |
|
$self->_ns_associate_numerical_prefix; ## RFC 2774 namespace |
239 |
$self; |
$self; |
240 |
} |
} |
241 |
|
|
252 |
|
|
253 |
=cut |
=cut |
254 |
|
|
255 |
sub field ($$) { |
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 |
my $self = shift; |
my $self = shift; |
261 |
my $name = lc shift; |
my ($by, $i, $list, $option) = @_; |
262 |
my @ret; |
return 0 unless ref $$i; ## Already removed |
263 |
for my $field (@{$self->{field}}) { |
if ($by eq 'name') { |
264 |
if ($field->{name} eq $name) { |
my %o = %$option; #$o{parse} = 0; |
265 |
unless (wantarray) { |
my %l; |
266 |
$field->{body} = $self->_field_body ($field->{body}, $name); |
for (keys %$list) { |
267 |
return $field->{body}; |
my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', \%o); |
268 |
|
if ($s) { |
269 |
|
$l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1; |
270 |
} else { |
} else { |
271 |
$field->{body} = $self->_field_body ($field->{body}, $name); |
$l{$v->{name} .':'. ( $option->{ns} || $self->{option}->{ns_default_phuri} ) } = 1; |
272 |
push @ret, $field->{body}; |
} |
273 |
|
} |
274 |
|
return 1 if $l{$$i->{name} . ':' . $$i->{ns}}; |
275 |
|
} elsif ($by eq 'ns') { |
276 |
|
return 1 if $list->{ $$i->{ns} }; |
277 |
|
} 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 |
} |
} |
290 |
if ($#ret < 0) { |
0; |
|
return $self->add ($name); |
|
|
} |
|
|
@ret; |
|
291 |
} |
} |
292 |
|
*_delete_match = \&_item_match; |
293 |
|
|
294 |
sub field_exist ($$) { |
## Returns returned item value \$item-value, \%option |
295 |
my $self = shift; |
sub _item_return_value ($\$\%) { |
296 |
my $name = lc shift; |
if (ref ${$_[1]}->{body}) { |
297 |
my @ret; |
${$_[1]}->{body}; |
298 |
for my $field (@{$self->{field}}) { |
} else { |
299 |
return 1 if ($field->{name} eq $name); |
${$_[1]}->{body} = $_[0]->_parse_value (${$_[1]}->{name} => ${$_[1]}->{body}, |
300 |
|
ns => ${$_[1]}->{ns}); |
301 |
|
${$_[1]}->{body}; |
302 |
|
} |
303 |
|
} |
304 |
|
*_add_return_value = \&_item_return_value; |
305 |
|
*_replace_return_value = \&_item_return_value; |
306 |
|
|
307 |
|
## Returns returned (new created) item value $name, \%option |
308 |
|
sub _item_new_value ($$\%) { |
309 |
|
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 |
|
$s? $value: undef; |
319 |
} |
} |
|
0; |
|
320 |
} |
} |
321 |
|
|
|
=head2 $self->field_name ($index) |
|
|
|
|
|
Returns C<field-name> of $index'th C<field>. |
|
|
|
|
|
=head2 $self->field_body ($index) |
|
322 |
|
|
|
Returns C<field-body> of $index'th C<field>. |
|
323 |
|
|
324 |
=cut |
## $self->_parse_value ($type, $value, %options); |
325 |
|
sub _parse_value ($$$;%) { |
326 |
sub field_name ($$) { |
my $self = shift; |
327 |
my $self = shift; |
my $name = shift ;#|| $self->{option}->{_VALTYPE_DEFAULT}; |
328 |
$self->{field}->[shift]->{name}; |
my $value = shift; return $value if ref $value; |
329 |
} |
my %option = @_; |
330 |
sub field_body ($$) { |
my $vtype; { no strict 'refs'; |
331 |
my $self = shift; |
my $vt = ${&_NS_uri2package ($option{ns}).'::OPTION'}{value_type}; |
332 |
my $i = shift; |
if (ref $vt) { |
333 |
$self->{field}->[$i]->{body} |
$vtype = $vt->{$name} || $vt->{$self->{option}->{_VALTYPE_DEFAULT}}; |
334 |
= $self->_field_body ($self->{field}->[$i]->{body}, $self->{field}->[$i]->{name}); |
} |
335 |
$self->{field}->[$i]->{body}; |
## 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 |
|
} elsif (length $value) { |
344 |
|
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
345 |
|
return $vpackage->parse ($value, |
346 |
|
-format => $self->{option}->{format}, |
347 |
|
-field_ns => $option{ns}, |
348 |
|
-field_name => $name, |
349 |
|
-header_default_charset => $self->{option}->{header_default_charset}, |
350 |
|
-header_default_charset_input => $self->{option}->{header_default_charset_input}, |
351 |
|
-internal_charset_name => $self->{option}->{internal_charset_name}, |
352 |
|
-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 |
|
-field_ns => $option{ns}, |
359 |
|
-field_name => $name, |
360 |
|
-header_default_charset => $self->{option}->{header_default_charset}, |
361 |
|
-header_default_charset_input => $self->{option}->{header_default_charset_input}, |
362 |
|
-internal_charset_name => $self->{option}->{internal_charset_name}, |
363 |
|
-parse_all => $self->{option}->{parse_all}, |
364 |
|
%vopt); |
365 |
|
} |
366 |
} |
} |
367 |
|
|
368 |
sub _field_body ($$$) { |
## Defined for text/rfc822-headers |
369 |
my $self = shift; |
sub entity_header ($;$) { |
370 |
my ($body, $name) = @_; |
my $self = shift; |
371 |
unless (ref $body) { |
my $new_header = shift; |
372 |
my $type = $self->{option}->{field_type}->{$name} |
if (ref $new_header) { |
373 |
|| $self->{option}->{field_type}->{':DEFAULT'}; |
$self->{header} = $new_header; |
|
eval "require $type" or Carp::croak ("_field_body: $type: $@"); |
|
|
unless ($body) { |
|
|
$body = $type->new (-field_name => $name, |
|
|
-format => $self->{option}->{format}, |
|
|
-parse_all => $self->{option}->{parse_all}); |
|
|
} else { |
|
|
$body = $type->parse ($body, -field_name => $name, |
|
|
-format => $self->{option}->{format}, |
|
|
-parse_all => $self->{option}->{parse_all}); |
|
|
} |
|
374 |
} |
} |
375 |
$body; |
$self->{header}; |
376 |
} |
} |
377 |
|
|
378 |
=head2 $self->field_name_list () |
=head2 $self->field_name_list () |
385 |
|
|
386 |
sub field_name_list ($) { |
sub field_name_list ($) { |
387 |
my $self = shift; |
my $self = shift; |
388 |
$self->_delete_empty_field (); |
$self->_delete_empty (); |
389 |
map {$_->{name}} @{$self->{field}}; |
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 |
|
$self->{option}->{ns_default_phuri} = $_[0]; |
397 |
|
$self->_ns_load_ph (${&_NS_uri2package ($self->{option}->{ns_default_phuri}).'::OPTION'}{namespace_phname}); |
398 |
|
} |
399 |
|
$self->{option}->{ns_default_phuri}; |
400 |
} |
} |
401 |
|
|
402 |
=item $hdr->add ($field-name, $field-body, [$name, $body, ...]) |
=item $hdr->add ($field-name, $field-body, [$name, $body, ...]) |
422 |
|
|
423 |
=cut |
=cut |
424 |
|
|
425 |
sub add ($%) { |
## [Name: Value] pair -> internal array item |
426 |
my $self = shift; |
## $self->_value_to_arrayitem ($name => $value, {%options}) |
427 |
my %fields = @_; |
## or |
428 |
my %option = %{$self->{option}}; |
## $self->_value_to_arrayitem ($name => [$value, %value_options], {%options}) |
429 |
$option{parse} = 1 if defined wantarray; |
## |
430 |
for (grep {/^-/} keys %fields) {$option{substr ($_, 1)} = $fields{$_}} |
## Return: ((1 = success / 0 = failue), $full_name, $arrayitem) |
431 |
my $body; |
sub _value_to_arrayitem ($$$\%) { |
432 |
for (grep {/^[^-]/} keys %fields) { |
my $self = shift; |
433 |
my $name = lc $_; $body = $fields{$_}; |
my ($name, $value, $option) = @_; |
434 |
$name =~ tr/_/-/ if $option{translate_underscore}; |
my $value_option = {}; |
435 |
Carp::croak "add: $name: invalid field-name" |
if (ref $value eq 'ARRAY') { |
436 |
if $option{validate} && $name =~ /$REG{UNSAFE_field_name}/; |
($value, %$value_option) = @$value; |
437 |
$body = $self->_field_body ($body, $name) if $option{parse}; |
} |
438 |
if ($option{prepend}) { |
my $default_ns = $option->{ns_default_phuri}; |
439 |
unshift @{$self->{field}}, {name => $name, body => $body}; |
my $nsuri = $default_ns; |
440 |
} else { |
$name =~ s/^$REG{WSP}+//; $name =~ s/$REG{WSP}+$//; |
441 |
push @{$self->{field}}, {name => $name, body => $body}; |
|
442 |
|
no strict 'refs'; |
443 |
|
if ($value_option->{ns}) { |
444 |
|
$nsuri = $value_option->{ns}; |
445 |
|
} elsif ($option->{ns}) { |
446 |
|
$nsuri = $option->{ns}; |
447 |
|
} elsif (($default_ns eq $self->{ns}->{phname2uri}->{'x-http'} |
448 |
|
&& $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 |
|
} 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 |
|
my ($prefix1, $prefix2) = ($1, $2); |
464 |
|
my $original_prefix = $&; my $one_prefix = 0; |
465 |
|
unless ($prefix2) { |
466 |
|
$prefix2 = $prefix1; |
467 |
|
$prefix1 = $self->{ns}->{uri2phname}->{ $default_ns }; |
468 |
|
$one_prefix = 1; |
469 |
|
} |
470 |
|
my $prefix |
471 |
|
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_prefix} } |
472 |
|
($self, &_NS_uri2package ($nsuri), $prefix1.'-'.$prefix2); |
473 |
|
$self->_ns_load_ph ($prefix); |
474 |
|
$nsuri = $self->{ns}->{phname2uri}->{ $prefix }; |
475 |
|
unless ($nsuri) { |
476 |
|
$nsuri = $default_ns; |
477 |
|
$prefix |
478 |
|
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_prefix} } |
479 |
|
($self, &_NS_uri2package ($nsuri), $one_prefix? $prefix2: |
480 |
|
$prefix1); |
481 |
|
$self->_ns_load_ph ($prefix); |
482 |
|
$nsuri = $self->{ns}->{phname2uri}->{ $prefix }; |
483 |
|
if ($nsuri) { |
484 |
|
$name = $prefix2 . '-' . $name unless $one_prefix; |
485 |
|
} else { |
486 |
|
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 |
|
} |
502 |
} |
} |
503 |
} |
} |
504 |
$body if $option{parse}; |
$name |
505 |
|
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_name} } |
506 |
|
($self, &_NS_uri2package ($nsuri), $name); |
507 |
|
Carp::croak "$name: invalid field-name" |
508 |
|
if $option->{field_name_validation} |
509 |
|
&& $name =~ /$REG{ $option->{field_name_unsafe_rule} }/; |
510 |
|
$value = $self->_parse_value ($name => $value, ns => $nsuri) |
511 |
|
if $option->{parse} || $option->{parse_all}; |
512 |
|
$option->{parse} = 0; |
513 |
|
(1, $name.':'.$nsuri => {name => $name, body => $value, ns => $nsuri}); |
514 |
} |
} |
515 |
|
*_add_hash_check = \&_value_to_arrayitem; |
516 |
|
*_replace_hash_check = \&_value_to_arrayitem; |
517 |
|
|
518 |
=head2 $self->relace ($field_name, $field_body) |
=head2 $self->relace ($field_name, $field_body) |
519 |
|
|
525 |
|
|
526 |
=cut |
=cut |
527 |
|
|
528 |
sub replace ($%) { |
sub _replace_hash_shift ($\%$\%) { |
529 |
my $self = shift; |
shift; my $r = shift; my $n = $_[0]->{name} . ':' . $_[0]->{ns}; |
530 |
my %params = @_; |
if ($$r{$n}) { |
531 |
my %option = %{$self->{option}}; |
my $d = $$r{$n}; |
532 |
$option{parse} = defined wantarray unless defined $option{parse}; |
delete $$r{$n}; |
533 |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
return $d; |
|
my (%new_field); |
|
|
for (grep {/^[^-]/} keys %params) { |
|
|
my $name = lc $_; |
|
|
$name =~ tr/_/-/ if $option{translate_underscore}; |
|
|
Carp::croak "replace: $name: invalid field-name" |
|
|
if $option{validate} && $name =~ /$REG{UNSAFE_field_name}/; |
|
|
$params{$_} = $self->_field_body ($params{$_}, $name) if $option{parse}; |
|
|
$new_field{$name} = $params{$_}; |
|
|
} |
|
|
my $body = (%new_field)[-1]; |
|
|
for my $field (@{$self->{field}}) { |
|
|
if (defined $new_field{$field->{name}}) { |
|
|
$field->{body} = $new_field {$field->{name}}; |
|
|
$new_field{$field->{name}} = undef; |
|
|
} |
|
534 |
} |
} |
535 |
for (keys %new_field) { |
undef; |
|
push @{$self->{field}}, {name => $_, body => $new_field{$_}}; |
|
|
} |
|
|
$body if $option{parse}; |
|
536 |
} |
} |
537 |
|
|
538 |
=head2 $self->delete ($field-name, [$name, ...]) |
=head2 $self->delete ($field-name, [$name, ...]) |
541 |
|
|
542 |
=cut |
=cut |
543 |
|
|
|
sub delete ($@) { |
|
|
my $self = shift; |
|
|
my %delete; for (@_) {$delete{lc $_} = 1} |
|
|
for my $field (@{$self->{field}}) { |
|
|
undef $field if $delete{$field->{name}}; |
|
|
} |
|
|
} |
|
544 |
|
|
545 |
=head2 $self->count ([$field_name]) |
=head2 $self->count ([$field_name]) |
546 |
|
|
550 |
|
|
551 |
=cut |
=cut |
552 |
|
|
553 |
sub count ($;$) { |
sub _count_by_name ($$\%) { |
554 |
my $self = shift; |
my $self = shift; |
555 |
my ($name) = (lc shift); |
my ($array, $option) = @_; |
556 |
unless ($name) { |
my $name = $self->_n11n_field_name ($$option{-name}); |
557 |
$self->_delete_empty_field (); |
my @a = grep {$_->{name} eq $name} @{$self->{$array}}; |
558 |
return $#{$self->{field}}+1; |
$#a + 1; |
559 |
} |
} |
560 |
my $count = 0; |
|
561 |
for my $field (@{$self->{field}}) { |
## Delete empty items |
562 |
if ($field->{name} eq $name) { |
sub _delete_empty ($) { |
563 |
$count++; |
my $self = shift; |
564 |
} |
my $array = $self->{option}->{_HASH_NAME}; |
565 |
} |
$self->{$array} = [grep {ref $_ && length $_->{name}} @{$self->{$array}}]; |
|
$count; |
|
566 |
} |
} |
567 |
|
|
568 |
=head2 $self->rename ($field-name, $new-name, [$old, $new,...]) |
=head2 $self->rename ($field-name, $new-name, [$old, $new,...]) |
578 |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
579 |
my %new_name; |
my %new_name; |
580 |
for (grep {/^[^-]/} keys %params) { |
for (grep {/^[^-]/} keys %params) { |
581 |
my ($old => $new) = (lc $_ => lc $params{$_}); |
my ($old => $new) |
582 |
|
= ($self->_n11n_field_name ($_) => $self->_n11n_field_name ($params{$_})); |
583 |
|
$old =~ tr/_/-/ if $option{translate_underscore}; |
584 |
$new =~ tr/_/-/ if $option{translate_underscore}; |
$new =~ tr/_/-/ if $option{translate_underscore}; |
585 |
Carp::croak "rename: $new: invalid field-name" |
Carp::croak "rename: $new: invalid field-name" |
586 |
if $option{validate} && $new =~ /$REG{UNSAFE_field_name}/; |
if $option{field_name_validation} |
587 |
|
&& $new =~ /$REG{$option{field_name_unsafe_rule}}/; |
588 |
$new_name{$old} = $new; |
$new_name{$old} = $new; |
589 |
} |
} |
590 |
for my $field (@{$self->{field}}) { |
for my $field (@{$self->{value}}) { |
591 |
if (length $new_name{$field->{name}}) { |
if (length $new_name{$field->{name}}) { |
592 |
$field->{name} = $new_name{$field->{name}}; |
$field->{name} = $new_name{$field->{name}}; |
593 |
} |
} |
605 |
|
|
606 |
=cut |
=cut |
607 |
|
|
608 |
sub scan ($&) { |
sub _scan_sort ($\@\%) { |
609 |
my ($self, $sub) = @_; |
no strict 'refs'; |
610 |
my $sort; |
my $self = shift; |
611 |
$sort = \&_header_cmp if $self->{option}->{sort} eq 'good-practice'; |
my ($array, $option) = @_; |
612 |
$sort = {$a cmp $b} if $self->{option}->{sort} eq 'alphabetic'; |
my $nspack = &_NS_uri2package ($self->{option}->{ns_default_phuri}); |
613 |
my @field = @{$self->{field}}; |
my $sort = ${ $nspack.'::OPTION' }{field_sort}; |
614 |
if (ref $sort) { |
if ($option->{field_sort} eq 'good-practice' && $sort->{'good-practice'}) { |
615 |
@field = sort $sort @{$self->{field}}; |
return $self->Message::Header::Default::sort_good_practice ($array, $nspack, $option); |
616 |
} |
} elsif ($option->{field_sort} eq 'alphabetic' && $sort->{'alphabetic'}) { |
617 |
for my $field (@field) { |
## TODO: How treat namespace prefix? |
618 |
next if $field->{name} =~ /^_/; |
return sort {$a->{name} cmp $b->{name}} @$array; |
619 |
&$sub($field->{name} => $field->{body}); |
} |
620 |
} |
@$array; |
621 |
|
} |
622 |
|
|
623 |
|
sub _n11n_field_name ($$) { |
624 |
|
no strict 'refs'; |
625 |
|
my $self = shift; |
626 |
|
my $s = shift; |
627 |
|
$s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//; |
628 |
|
$s = lc $s unless ${&_NS_uri2package ($self->{option}->{ns_default_phuri}).'::OPTION'}{case_sensible}; |
629 |
|
$s; |
630 |
} |
} |
631 |
|
|
|
# Compare function which makes it easy to sort headers in the |
|
|
# recommended "Good Practice" order. |
|
|
## taken from HTTP::Header |
|
|
sub _header_cmp |
|
|
{ |
|
|
my ($na, $nb) = ($a->{name}, $b->{name}); |
|
|
# Unknown headers are assign a large value so that they are |
|
|
# sorted last. This also helps avoiding a warning from -w |
|
|
# about comparing undefined values. |
|
|
$header_order{$na} = 999 unless defined $header_order{$na}; |
|
|
$header_order{$nb} = 999 unless defined $header_order{$nb}; |
|
|
|
|
|
$header_order{$na} <=> $header_order{$nb} || $na cmp $nb; |
|
|
} |
|
632 |
|
|
633 |
=head2 $self->stringify ([%option]) |
=head2 $self->stringify ([%option]) |
634 |
|
|
641 |
my %params = @_; |
my %params = @_; |
642 |
my %option = %{$self->{option}}; |
my %option = %{$self->{option}}; |
643 |
$option{format} = $params{-format} if $params{-format}; |
$option{format} = $params{-format} if $params{-format}; |
644 |
$self->_init_by_format ($option{format}, \%option); |
$self->_init_by_format ($option{format}, \%option) |
645 |
|
if $self->{option}->{format} ne $option{format}; |
646 |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
647 |
|
## Fill required fields |
648 |
|
my %exist; |
649 |
|
for ($self->field_name_list) {$exist{$_} = 1} |
650 |
|
&{ $option{hook_stringify_fill_fields} } ($self, \%exist, \%option); |
651 |
my @ret; |
my @ret; |
652 |
|
## RFC 2774 numerical field name prefix |
653 |
|
my %nprefix; |
654 |
|
{no strict 'refs'; |
655 |
|
%nprefix = reverse %{ $self->{ns}->{number2uri} }; |
656 |
|
my $i = (sort { $a <=> $b } keys %{ $self->{ns}->{number2uri} })[-1] + 1; |
657 |
|
$i = 10 if $i < 10; |
658 |
|
my $hprefix = ${ &_NS_uri2package |
659 |
|
($self->{ns}->{phname2uri}->{'x-http'}) |
660 |
|
.'::OPTION' } {namespace_phname_goodcase}; |
661 |
|
for my $uri (keys %nprefix) { |
662 |
|
if ($nprefix{ $uri } < 10) { |
663 |
|
$nprefix{ $uri } = $i++; |
664 |
|
} |
665 |
|
my $nsfs = $self->item ($uri, -by => 'http-ns-define'); |
666 |
|
for my $i (0..$nsfs->count-1) { |
667 |
|
my $nsf = ($nsfs->value ($i))[0]; |
668 |
|
if ($nsf->value eq $uri) { |
669 |
|
$nsf->replace (ns => $nprefix{ $uri }); |
670 |
|
$nprefix{ $uri } = $hprefix . '-' . $nprefix{ $uri }; |
671 |
|
last; |
672 |
|
} |
673 |
|
} |
674 |
|
} |
675 |
|
} |
676 |
my $_stringify = sub { |
my $_stringify = sub { |
677 |
my ($name, $body) = (@_); |
no strict 'refs'; |
678 |
|
my ($name, $body, $nsuri) = ($_[1]->{name}, $_[1]->{body}, $_[1]->{ns}); |
679 |
return unless length $name; |
return unless length $name; |
680 |
return if $option{mail_from} && $name eq 'mail-from'; |
return if $option{output_mail_from} && $name eq 'mail-from'; |
681 |
return if !$option{output_bcc} && ($name eq 'bcc' || $name eq 'resent-bcc'); |
$body = '' if !$option{output_bcc} && $name eq 'bcc'; |
682 |
|
my $nspackage = &_NS_uri2package ($nsuri); |
683 |
|
my $oname; ## Outputed field-name |
684 |
|
my $prefix = $nprefix{ $nsuri } |
685 |
|
|| ${$nspackage.'::OPTION'} {namespace_phname_goodcase} |
686 |
|
|| $self->{ns}->{uri2phname}->{ $nsuri }; |
687 |
|
my $default_prefix = ${ &_NS_uri2package ($option{ns_default_phuri}) |
688 |
|
.'::OPTION'} {namespace_phname_goodcase}; |
689 |
|
$prefix = '' if $prefix eq $default_prefix; |
690 |
|
$prefix =~ s/^\Q$default_prefix\E-//; |
691 |
|
my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase}; |
692 |
|
if (ref $gc) { $oname = &$gc ($self, $nspackage, $name, \%option) } |
693 |
|
else { $oname = $name } |
694 |
|
if ($prefix) { $oname = $prefix . '-' . $oname } |
695 |
if ($option{format} =~ /uri-url-mailto/) { |
if ($option{format} =~ /uri-url-mailto/) { |
696 |
return if (( $option{uri_mailto_safe}->{$name} |
return if (( ${$nspackage.'::OPTION'} {uri_mailto_safe}->{$name} |
697 |
|| $option{uri_mailto_safe}->{':default'}) |
|| ${$nspackage.'::OPTION'} {uri_mailto_safe}->{':default'}) |
698 |
< $option{uri_mailto_safe_level}); |
< $option{uri_mailto_safe_level}); |
699 |
if ($name eq 'to') { |
if ($name eq 'to') { |
700 |
$body = $self->field ('to'); |
$body = $self->field ('to', -new_item_unless_exist => 0); |
701 |
return unless ref $body && $body->have_group; |
if (ref $body && $body->have_group) { |
702 |
|
# |
703 |
|
} elsif (ref $body && $body->count > 1) { |
704 |
|
$body = $body->clone; |
705 |
|
$body->delete ({-by => 'index'}, 0); |
706 |
|
} |
707 |
} |
} |
708 |
} |
} |
709 |
my $fbody; |
my $fbody; |
712 |
} else { |
} else { |
713 |
$fbody = $body; |
$fbody = $body; |
714 |
} |
} |
715 |
return unless length $fbody; |
unless (${$nspackage.'::OPTION'} {field}->{$name}->{empty_body}) { |
716 |
|
return unless length $fbody; |
717 |
|
} |
718 |
unless ($option{linebreak_strict}) { |
unless ($option{linebreak_strict}) { |
719 |
## bare \x0D and bare \x0A are unsafe |
## bare \x0D and bare \x0A are unsafe |
720 |
$fbody =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g; |
$fbody =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g; |
722 |
} else { |
} else { |
723 |
$fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g; |
$fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g; |
724 |
} |
} |
725 |
if (ref $option{capitalize}) { |
if ($option{use_folding}) { |
726 |
$name = &{$option{capitalize}} ($self, $name); |
if (ref $option{output_folding}) { |
727 |
} elsif ($option{capitalize}) { |
$fbody = &{$option{output_folding}} ($self, $fbody, |
728 |
$name =~ s/((?:^|-)[a-z])/uc($1)/ge; |
-initial_length => length ($oname) +2); |
729 |
} |
} elsif ($option{output_folding}) { |
730 |
if (ref $option{fold}) { |
$fbody = $self->_fold ($fbody, -initial_length => length ($oname) +2); |
731 |
$fbody = &{$option{fold}} ($self, $fbody); |
} |
|
} elsif ($option{fold}) { |
|
|
$fbody = $self->_fold ($fbody); |
|
732 |
} |
} |
733 |
push @ret, sprintf $option{field_format_pattern}, $name, $fbody; |
push @ret, sprintf $option{field_format_pattern}, $oname, $fbody; |
734 |
}; |
}; |
735 |
if ($option{format} =~ /uri-url-mailto-to/) { |
if ($option{format} =~ /uri-url-mailto/) { |
736 |
if ($self->field_exist ('to')) { |
if ($option{format} =~ /uri-url-mailto-to/) { |
737 |
my $to = $self->field ('to'); |
my $to = $self->field ('to', -new_item_unless_exist => 0); |
738 |
unless ($to->have_group) { |
if ($to) { |
739 |
my $fbody = $to->stringify (-format => $option{format}); |
unless ($to->have_group) { |
740 |
return &{$option{fold}} ($self, $fbody); |
my $fbody = $to->stringify (-format => $option{format}, -max => 1); |
741 |
|
return &{$option{output_folding}} ($self, $fbody); |
742 |
|
} |
743 |
} |
} |
744 |
|
''; |
745 |
|
} elsif ($option{format} =~ /uri-url-mailto-rfc1738/) { |
746 |
|
my $to = $self->field ('to', -new_item_unless_exist => 0); |
747 |
|
if ($to) { |
748 |
|
my $fbody = $to->addr_spec (-format => $option{format}); |
749 |
|
return &{$option{output_folding}} ($self, $fbody); |
750 |
|
} |
751 |
|
''; |
752 |
|
} else { |
753 |
|
$self->scan ($_stringify); |
754 |
|
my $ret = join ('&', @ret); |
755 |
|
$ret; |
756 |
} |
} |
|
''; |
|
|
} elsif ($option{format} =~ /uri-url-mailto/) { |
|
|
$self->scan ($_stringify); |
|
|
my $ret = join ('&', @ret); |
|
|
$ret; |
|
757 |
} else { |
} else { |
758 |
if ($option{mail_from}) { |
if ($option{output_mail_from}) { |
759 |
my $fromline = $self->field ('mail-from'); |
my $fromline = $self->field ('mail-from', -new_item_unless_exist => 0); |
760 |
push @ret, 'From '.$fromline if $fromline; |
push @ret, 'From '.$fromline if $fromline; |
761 |
} |
} |
762 |
$self->scan ($_stringify); |
$self->scan ($_stringify); |
763 |
my $ret = join ("\n", @ret); |
my $ret = join ("\x0D\x0A", @ret); |
764 |
$ret? $ret."\n": ''; |
$ret? $ret."\x0D\x0A": ''; |
765 |
} |
} |
766 |
} |
} |
767 |
*as_string = \&stringify; |
*as_string = \&stringify; |
778 |
return $self->{option}->{ shift (@_) }; |
return $self->{option}->{ shift (@_) }; |
779 |
} |
} |
780 |
while (my ($name, $value) = splice (@_, 0, 2)) { |
while (my ($name, $value) = splice (@_, 0, 2)) { |
|
$name =~ s/^-//; |
|
781 |
$self->{option}->{$name} = $value; |
$self->{option}->{$name} = $value; |
782 |
if ($name eq 'format') { |
if ($name eq 'format') { |
783 |
for my $f (@{$self->{field}}) { |
for my $f (@{$self->{field}}) { |
789 |
} |
} |
790 |
} |
} |
791 |
|
|
792 |
sub field_type ($$;$) { |
sub field_type ($@) {shift->SUPER::value_type (@_)} |
|
my $self = shift; |
|
|
my $field_name = shift; |
|
|
my $new_field_type = shift; |
|
|
if ($new_field_type) { |
|
|
$self->{option}->{field_type}->{$field_name} = $new_field_type; |
|
|
} |
|
|
$self->{option}->{field_type}->{$field_name} |
|
|
|| $self->{option}->{field_type}->{':DEFAULT'}; |
|
|
} |
|
|
|
|
|
sub _delete_empty_field ($) { |
|
|
my $self = shift; |
|
|
my @ret; |
|
|
for my $field (@{$self->{field}}) { |
|
|
push @ret, $field if $field->{name}; |
|
|
} |
|
|
$self->{field} = \@ret; |
|
|
$self; |
|
|
} |
|
793 |
|
|
794 |
sub _fold ($$;$) { |
## $self->_fold ($string, %option = (-max, -initial_length(for field-name)) ) |
795 |
|
sub _fold ($$;%) { |
796 |
my $self = shift; |
my $self = shift; |
797 |
my $string = shift; |
my $string = shift; |
798 |
my $len = shift || $self->{option}->{fold_length}; |
my %option = @_; |
799 |
$len = 60 if $len < 60; |
my $max = $self->{option}->{line_length_max}; |
800 |
|
$max = 20 if $max < 20; |
801 |
|
|
802 |
## This code is taken from Mail::Header 1.43 in MailTools, |
my $l = $option{-initial_length} || 0; |
803 |
## by Graham Barr (Maintained by Mark Overmeer <mailtools@overmeer.net>). |
$l += length $1 if $string =~ /^([^\x09\x20]+)/; |
804 |
my $max = int($len - 5); # 4 for leading spcs + 1 for [\,\;] |
$string =~ s{([\x09\x20][^\x09\x20]+)}{ |
805 |
my $min = int($len * 4 / 5) - 4; |
my $s = $1; |
806 |
my $ml = $len; |
if (($l + length $s) > $max) { |
807 |
|
$s = "\x0D\x0A\x20" . $s; |
808 |
if (length($string) > $ml) { |
$l = 1 + length $s; |
809 |
#Split the line up |
} else { $l += length $s } |
810 |
# first bias towards splitting at a , or a ; >4/5 along the line |
$s; |
811 |
# next split a whitespace |
}gex; |
|
# else we are looking at a single word and probably don't want to split |
|
|
my $x = ""; |
|
|
$x .= "$1\n " |
|
|
while($string =~ s/^$REG{WSP}*( |
|
|
[^"]{$min,$max}?[\,\;] |
|
|
|[^"]{1,$max}$REG{WSP} |
|
|
|[^\s"]*(?:"[^"]*"[^\s"]*)+$REG{WSP} |
|
|
|[^\s"]+$REG{WSP} |
|
|
) |
|
|
//x); |
|
|
$x .= $string; |
|
|
$string = $x; |
|
|
$string =~ s/(\A$REG{WSP}+|$REG{WSP}+\Z)//sog; |
|
|
$string =~ s/\s+\n/\n/sog; |
|
|
} |
|
812 |
$string; |
$string; |
813 |
} |
} |
814 |
|
|
815 |
|
sub _ns_load_ph ($$) { |
816 |
|
my $self = shift; |
817 |
|
my $name = shift; ## normalized prefix (without HYPHEN-MINUS) |
818 |
|
return if $self->{ns}->{phname2uri}->{$name}; |
819 |
|
$self->{ns}->{phname2uri}->{$name} = $NS_phname2uri{$name}; |
820 |
|
$self->{ns}->{uri2phname}->{ $self->{ns}->{phname2uri}->{$name} } = $name; |
821 |
|
} |
822 |
|
|
823 |
|
sub _ns_associate_numerical_prefix ($) { |
824 |
|
my $self = shift; |
825 |
|
$self->scan (sub {shift; |
826 |
|
my $f = shift; return unless $f->{name}; |
827 |
|
if ($f->{ns} eq $self->{ns}->{phname2uri}->{'x-http'} |
828 |
|
|| $f->{ns} eq $self->{ns}->{phname2uri}->{'x-http-c'}) { |
829 |
|
my $fn = $f->{name}; |
830 |
|
if ($fn eq 'opt' || $fn eq 'man') { |
831 |
|
$f->{body} = $self->_parse_value ($fn => $f->{body}, ns => $f->{ns}); |
832 |
|
for ($f->{body}->value (0..$f->{body}->count-1)) { |
833 |
|
my ($nsuri, $number) = ($_->value, $_->item ('ns')); |
834 |
|
if ($number && $nsuri) { |
835 |
|
$self->{ns}->{number2uri}->{ $number } = $nsuri; |
836 |
|
} |
837 |
|
} |
838 |
|
} |
839 |
|
} |
840 |
|
}); |
841 |
|
$self->scan (sub {shift; |
842 |
|
my $f = shift; |
843 |
|
if ($f->{ns} =~ /urn:x-suika-fam-cx:msgpm:header:x-temp:([0-9]+)$/ && $self->{ns}->{number2uri}->{ $1 }) { |
844 |
|
$f->{ns} = $self->{ns}->{number2uri}->{ $1 }; |
845 |
|
} |
846 |
|
}); |
847 |
|
} |
848 |
|
|
849 |
|
## $package_name = Message::Header::_NS_uri2phpackage ($nsuri) |
850 |
|
## (For internal use of Message::* modules) |
851 |
|
sub _NS_uri2phpackage ($) { |
852 |
|
$NS_uri2phpackage{$_[0]} |
853 |
|
|| $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}}; |
854 |
|
} |
855 |
|
sub _NS_uri2package ($) { |
856 |
|
$NS_uri2package{$_[0]} |
857 |
|
|| $NS_uri2phpackage{$_[0]} |
858 |
|
|| $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}}; |
859 |
|
} |
860 |
|
|
861 |
=head2 $self->clone () |
=head2 $self->clone () |
862 |
|
|
863 |
Returns a copy of Message::Header object. |
Returns a copy of Message::Header object. |
864 |
|
|
865 |
=cut |
=cut |
866 |
|
|
867 |
sub clone ($) { |
## Inhreited |
|
my $self = shift; |
|
|
my $clone = new Message::Header; |
|
|
for my $name (%{$self->{option}}) { |
|
|
if (ref $self->{option}->{$name} eq 'HASH') { |
|
|
$clone->{option}->{$name} = {%{$self->{option}->{$name}}}; |
|
|
} elsif (ref $self->{option}->{$name} eq 'ARRAY') { |
|
|
$clone->{option}->{$name} = [@{$self->{option}->{$name}}]; |
|
|
} else { |
|
|
$clone->{option}->{$name} = $self->{option}->{$name}; |
|
|
} |
|
|
} |
|
|
for (@{$self->{field}}) { |
|
|
$clone->add ($_->{name}, scalar $_->{body}); |
|
|
} |
|
|
$clone; |
|
|
} |
|
868 |
|
|
869 |
=head1 NOTE |
=head1 NOTE |
870 |
|
|