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 |
=head1 CONSTRUCTORS |
-hook_init_fill_options => sub {}, |
45 |
|
-hook_stringify_fill_fields => sub {}, |
46 |
The following methods construct new C<Message::Header> objects: |
-linebreak_strict => 0, |
47 |
|
-line_length_max => 60, ## For folding |
48 |
=over 4 |
#ns_default_phuri |
49 |
|
-output_bcc => 0, |
50 |
=cut |
-output_folding => 1, |
51 |
|
-output_mail_from => 0, |
52 |
## Initialize |
#parse_all |
53 |
my %DEFAULT = ( |
-translate_underscore => 1, |
54 |
capitalize => 1, |
#uri_mailto_safe |
55 |
fold => 1, |
-uri_mailto_safe_level => 4, |
56 |
fold_length => 70, |
-use_folding => 1, |
57 |
field_format_pattern => '%s: %s', |
#value_type |
|
#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, |
|
58 |
); |
); |
|
$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; |
|
|
}; |
|
59 |
|
|
60 |
## taken from L<HTTP::Header> |
## taken from L<HTTP::Header> |
61 |
# "Good Practice" order of HTTP message headers: |
# "Good Practice" order of HTTP message headers: |
87 |
); |
); |
88 |
my %header_order; |
my %header_order; |
89 |
|
|
90 |
|
=head1 CONSTRUCTORS |
91 |
|
|
92 |
|
The following methods construct new C<Message::Header> objects: |
93 |
|
|
94 |
|
=over 4 |
95 |
|
|
96 |
|
=cut |
97 |
|
|
98 |
sub _init ($;%) { |
sub _init ($;%) { |
99 |
my $self = shift; |
my $self = shift; |
100 |
my %options = @_; |
my %options = @_; |
101 |
$self->{field} = []; |
my $DEFAULT = Message::Util::make_clone (\%DEFAULT); |
102 |
$self->{option} = \%DEFAULT; |
$self->SUPER::_init (%$DEFAULT, %options); |
103 |
|
$self->{value} = []; |
104 |
|
$self->_ns_load_ph ('default'); |
105 |
|
$self->_ns_load_ph ('x-rfc822'); |
106 |
|
$self->_ns_load_ph ('x-http'); |
107 |
|
$self->{option}->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-rfc822'} |
108 |
|
unless $self->{option}->{ns_default_phuri}; |
109 |
|
|
110 |
|
## For text/rfc822-headers |
111 |
|
if (ref $options{entity_header}) { |
112 |
|
$self->{entity_header} = $options{entity_header}; |
113 |
|
delete $options{entity_header}; |
114 |
|
} |
115 |
my @new_fields = (); |
my @new_fields = (); |
116 |
for my $name (keys %options) { |
for my $name (keys %options) { |
117 |
if (substr ($name, 0, 1) eq '-') { |
unless (substr ($name, 0, 1) eq '-') { |
|
$self->{option}->{substr ($name, 1)} = $options{$name}; |
|
|
} else { |
|
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. |
134 |
sub _init_by_format ($$\%) { |
sub _init_by_format ($$\%) { |
135 |
my $self = shift; |
my $self = shift; |
136 |
my ($format, $option) = @_; |
my ($format, $option) = @_; |
137 |
if ($format =~ /rfc822/) { |
return if $format eq $option->{format}; |
138 |
$header_goodcase{bcc} = 'bcc'; |
if ($format =~ /http/) { |
139 |
$header_goodcase{cc} = 'cc'; |
$option->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-http'}; |
140 |
$header_goodcase{'resent-bcc'} = 'Resent-bcc'; |
if ($format =~ /cgi/) { |
141 |
$header_goodcase{'resent-cc'} = 'Resent-cc'; |
unshift @header_order, qw(content-type location); |
142 |
} elsif ($format =~ /cgi/) { |
$option->{field_sort} = 'good-practice'; |
143 |
unshift @header_order, qw(content-type location); |
$option->{use_folding} = 0; |
144 |
$option->{sort} = 'good-practice'; |
} else { |
145 |
$option->{fold} = 0; |
$option->{field_sort} = 'good-practice'; |
146 |
} elsif ($format =~ /http/) { |
} |
147 |
$option->{sort} = 'good-practice'; |
} elsif ($format =~ /mail|news/) { ## RFC 822 |
148 |
|
$option->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-rfc822'}; |
149 |
} |
} |
150 |
if ($format =~ /uri-url-mailto/) { |
if ($format =~ /uri-url-mailto/) { |
151 |
$option->{output_bcc} = 0; |
$option->{output_bcc} = 0; |
|
$option->{capitalize} = 0; |
|
152 |
$option->{field_format_pattern} = '%s=%s'; |
$option->{field_format_pattern} = '%s=%s'; |
153 |
$option->{fold} = sub { |
$option->{output_folding} = sub { |
154 |
$_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge; |
$_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge; |
155 |
$_[1]; |
$_[1]; |
156 |
}; |
}; ## Yes, this is not folding! |
157 |
} |
} |
158 |
} |
} |
159 |
|
|
160 |
=item Message::Header->new ([%initial-fields/options]) |
=item $msg = Message::Header->new ([%initial-fields/options]) |
161 |
|
|
162 |
Constructs a new C<Message::Headers> object. You might pass some initial |
Constructs a new C<Message::Headers> object. You might pass some initial |
163 |
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. |
173 |
|
|
174 |
=cut |
=cut |
175 |
|
|
176 |
sub new ($;%) { |
## Inherited |
|
my $class = shift; |
|
|
my $self = bless {}, $class; |
|
|
$self->_init (@_); |
|
|
$self; |
|
|
} |
|
177 |
|
|
178 |
=item Message::Header->parse ($header, [%initial-fields/options]) |
=item $msg = Message::Header->parse ($header, [%initial-fields/options]) |
179 |
|
|
180 |
Parses given C<header> and constructs a new C<Message::Headers> |
Parses given C<header> and constructs a new C<Message::Headers> |
181 |
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 |
187 |
my $class = shift; |
my $class = shift; |
188 |
my $header = shift; |
my $header = shift; |
189 |
my $self = bless {}, $class; |
my $self = bless {}, $class; |
190 |
$self->_init (@_); ## BUG: don't check linebreak_strict |
$self->_init (@_); |
191 |
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos; ## unfold |
if ($self->{option}->{linebreak_strict}) { |
192 |
|
$header =~ s/\x0D\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding}; |
193 |
|
} else { |
194 |
|
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding}; |
195 |
|
} |
196 |
|
my %option = (%{ $self->{option} }); |
197 |
|
$option{parse_all} = 0; |
198 |
for my $field (split /\x0D?\x0A/, $header) { |
for my $field (split /\x0D?\x0A/, $header) { |
199 |
if ($field =~ /$REG{M_fromline}/) { |
if ($field =~ /$REG{M_fromline}/) { |
200 |
my $body = $1; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
201 |
$body = $self->_field_body ($body, 'mail-from') |
('mail-from' => $1, \%option); |
202 |
if $self->{option}->{parse_all}; |
push @{$self->{value}}, $value if $s; |
|
push @{$self->{field}}, {name => 'mail-from', body => $body}; |
|
203 |
} elsif ($field =~ /$REG{M_field}/) { |
} elsif ($field =~ /$REG{M_field}/) { |
204 |
my ($name, $body) = (lc $1, $2); |
my ($name, $body) = ($1, $2); |
|
$name =~ s/$REG{WSP}+$//; |
|
205 |
$body =~ s/$REG{WSP}+$//; |
$body =~ s/$REG{WSP}+$//; |
206 |
$body = $self->_field_body ($body, $name) if $self->{option}->{parse_all}; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
207 |
push @{$self->{field}}, {name => $name, body => $body}; |
($name => $body, \%option); |
208 |
} |
push @{$self->{value}}, $value if $s; |
209 |
|
} elsif (length $field) { |
210 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
211 |
|
('x-unknown' => $field, \%option); |
212 |
|
push @{$self->{value}}, $value if $s; |
213 |
|
} |
214 |
|
} |
215 |
|
$self->_ns_associate_numerical_prefix; ## RFC 2774 namespace |
216 |
|
for (@{ $self->{value} }) { |
217 |
|
no strict 'refs'; |
218 |
|
$_->{name} |
219 |
|
= &{ ${ &_NS_uri2package ($_->{ns}).'::OPTION' }{n11n_name} } |
220 |
|
($self, &_NS_uri2package ($_->{ns}), $_->{name}); |
221 |
|
$_->{body} = $self->_parse_value ($_->{name} => $_->{body}, ns => $_->{ns}) |
222 |
|
if $self->{option}->{parse_all}; |
223 |
} |
} |
224 |
$self; |
$self; |
225 |
} |
} |
226 |
|
|
227 |
=item Message::Header->parse_array (\@header, [%initial-fields/options]) |
=item $msg = Message::Header->parse_array (\@header, [%initial-fields/options]) |
228 |
|
|
229 |
Parses given C<header> and constructs a new C<Message::Headers> |
Parses given C<header> and constructs a new C<Message::Headers> |
230 |
object. Same as C<Message::Header-E<lt>parse> but this method |
object. Same as C<Message::Header-E<lt>parse> but this method |
243 |
$self->_init (@_); |
$self->_init (@_); |
244 |
while (1) { |
while (1) { |
245 |
my $field = shift @$header; |
my $field = shift @$header; |
246 |
while (1) { |
if ($self->{option}->{use_folding}) { |
247 |
if ($$header[0] =~ /^$REG{WSP}/) { |
while (1) { |
248 |
$field .= shift @$header; |
if ($$header[0] =~ /^$REG{WSP}/) { |
249 |
} else {last} |
$field .= shift @$header; |
250 |
|
} else {last} |
251 |
|
} |
252 |
} |
} |
253 |
if ($self->{option}->{linebreak_strict}) { |
if ($self->{option}->{linebreak_strict}) { |
254 |
$field =~ s/\x0D\x0A//g; |
$field =~ s/\x0D\x0A//g; |
255 |
} else { |
} else { |
256 |
$field =~ tr/\x0D\x0A//d; |
$field =~ tr/\x0D\x0A//d; |
257 |
} |
} |
258 |
|
local $self->{option}->{parse} = $self->{option}->{parse_all}; |
259 |
if ($field =~ /$REG{M_fromline}/) { |
if ($field =~ /$REG{M_fromline}/) { |
260 |
my $body = $1; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
261 |
$body = $self->_field_body ($body, 'mail-from') |
('mail-from' => $1, $self->{option}); |
262 |
if $self->{option}->{parse_all}; |
push @{$self->{value}}, $value if $s; |
|
push @{$self->{field}}, {name => 'mail-from', body => $body}; |
|
263 |
} elsif ($field =~ /$REG{M_field}/) { |
} elsif ($field =~ /$REG{M_field}/) { |
264 |
my ($name, $body) = (lc $1, $2); |
my ($name, $body) = ($self->_n11n_field_name ($1), $2); |
|
$name =~ s/$REG{WSP}+$//; |
|
265 |
$body =~ s/$REG{WSP}+$//; |
$body =~ s/$REG{WSP}+$//; |
266 |
$body = $self->_field_body ($body, $name) if $self->{option}->{parse_all}; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
267 |
push @{$self->{field}}, {name => $name, body => $body}; |
($name => $body, $self->{option}); |
268 |
|
push @{$self->{value}}, $value if $s; |
269 |
|
} elsif (length $field) { |
270 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
271 |
|
('x-unknown' => $field, $self->{option}); |
272 |
|
push @{$self->{value}}, $value if $s; |
273 |
} |
} |
274 |
last if $#$header < 0; |
last if $#$header < 0; |
275 |
} |
} |
276 |
|
$self->_ns_associate_numerical_prefix; ## RFC 2774 namespace |
277 |
$self; |
$self; |
278 |
} |
} |
279 |
|
|
290 |
|
|
291 |
=cut |
=cut |
292 |
|
|
293 |
sub field ($$) { |
sub field ($@) {shift->SUPER::item (@_)} |
294 |
|
sub field_exist ($@) {shift->SUPER::item_exist (@_)} |
295 |
|
|
296 |
|
## item-by?, \$checked-item, {item-key => 1}, \%option |
297 |
|
sub _item_match ($$\$\%\%) { |
298 |
my $self = shift; |
my $self = shift; |
299 |
my $name = lc shift; |
my ($by, $i, $list, $option) = @_; |
300 |
my @ret; |
return 0 unless ref $$i; ## Already removed |
301 |
for my $field (@{$self->{field}}) { |
if ($by eq 'name') { |
302 |
if ($field->{name} eq $name) { |
my %o = %$option; #$o{parse} = 0; |
303 |
unless (wantarray) { |
my %l; |
304 |
$field->{body} = $self->_field_body ($field->{body}, $name); |
for (keys %$list) { |
305 |
return $field->{body}; |
my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', \%o); |
306 |
|
if ($s) { |
307 |
|
$l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1; |
308 |
} else { |
} else { |
309 |
$field->{body} = $self->_field_body ($field->{body}, $name); |
$l{$v->{name} .':'. ( $option->{ns} || $self->{option}->{ns_default_phuri} ) } = 1; |
310 |
push @ret, $field->{body}; |
} |
311 |
|
} |
312 |
|
return 1 if $l{$$i->{name} . ':' . $$i->{ns}}; |
313 |
|
} elsif ($by eq 'ns') { |
314 |
|
return 1 if $list->{ $$i->{ns} }; |
315 |
|
} elsif ($by eq 'http-ns-define') { |
316 |
|
if ($$i->{ns} eq $self->{ns}->{phname2uri}->{'x-http'} |
317 |
|
|| $$i->{ns} eq $self->{ns}->{phname2uri}->{'x-http-c'}) { |
318 |
|
my $n = $$i->{name}; |
319 |
|
if ($n eq 'opt' || $n eq 'c-opt' || $n eq 'man' || $n eq 'c-man') { |
320 |
|
$option->{parse} = 0; |
321 |
|
$$i->{body} = $self->_parse_value ($$i->{name} => $$i->{body}, ns => $$i->{ns}); |
322 |
|
for my $j (0..$$i->{body}->count-1) { |
323 |
|
return 1 if $list->{ ($$i->{body}->value ($j))[0]->value }; |
324 |
|
} |
325 |
} |
} |
326 |
} |
} |
327 |
} |
} |
328 |
if ($#ret < 0) { |
0; |
|
return $self->add ($name); |
|
|
} |
|
|
@ret; |
|
329 |
} |
} |
330 |
|
*_delete_match = \&_item_match; |
331 |
|
|
332 |
sub field_exist ($$) { |
## Returns returned item value \$item-value, \%option |
333 |
my $self = shift; |
sub _item_return_value ($\$\%) { |
334 |
my $name = lc shift; |
if (ref ${$_[1]}->{body}) { |
335 |
my @ret; |
${$_[1]}->{body}; |
336 |
for my $field (@{$self->{field}}) { |
} else { |
337 |
return 1 if ($field->{name} eq $name); |
${$_[1]}->{body} = $_[0]->_parse_value (${$_[1]}->{name} => ${$_[1]}->{body}, |
338 |
|
ns => ${$_[1]}->{ns}); |
339 |
|
${$_[1]}->{body}; |
340 |
|
} |
341 |
|
} |
342 |
|
*_add_return_value = \&_item_return_value; |
343 |
|
*_replace_return_value = \&_item_return_value; |
344 |
|
|
345 |
|
## Returns returned (new created) item value $name, \%option |
346 |
|
sub _item_new_value ($$\%) { |
347 |
|
my $self = shift; |
348 |
|
my ($name, $option) = @_; |
349 |
|
if ($option->{by} eq 'http-ns-define') { |
350 |
|
my $value = $self->_parse_value (opt => '', ns => $self->{ns}->{phname2uri}->{'x-http'}); |
351 |
|
($value->value (0))[0]->value ($name); |
352 |
|
{name => 'opt', body => $value, ns => $self->{ns}->{phname2uri}->{'x-http'}}; |
353 |
|
} else { |
354 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
355 |
|
($name => '', $option); |
356 |
|
$s? $value: undef; |
357 |
} |
} |
|
0; |
|
358 |
} |
} |
359 |
|
|
|
=head2 $self->field_name ($index) |
|
360 |
|
|
|
Returns C<field-name> of $index'th C<field>. |
|
361 |
|
|
362 |
=head2 $self->field_body ($index) |
## $self->_parse_value ($type, $value, %options); |
363 |
|
sub _parse_value ($$$;%) { |
364 |
Returns C<field-body> of $index'th C<field>. |
my $self = shift; |
365 |
|
my $name = shift ;#|| $self->{option}->{_VALTYPE_DEFAULT}; |
366 |
=cut |
my $value = shift; return $value if ref $value; |
367 |
|
my %option = @_; |
368 |
sub field_name ($$) { |
my $vtype; { no strict 'refs'; |
369 |
my $self = shift; |
my $vt = ${&_NS_uri2package ($option{ns}).'::OPTION'}{value_type}; |
370 |
$self->{field}->[shift]->{name}; |
if (ref $vt) { |
371 |
} |
$vtype = $vt->{$name} || $vt->{$self->{option}->{_VALTYPE_DEFAULT}}; |
372 |
sub field_body ($$) { |
} |
373 |
my $self = shift; |
## For compatiblity. |
374 |
my $i = shift; |
unless (ref $vtype) { $vtype = $self->{option}->{value_type}->{$name} |
375 |
$self->{field}->[$i]->{body} |
|| $self->{option}->{value_type}->{$self->{option}->{_VALTYPE_DEFAULT}} } |
376 |
= $self->_field_body ($self->{field}->[$i]->{body}, $self->{field}->[$i]->{name}); |
} |
377 |
$self->{field}->[$i]->{body}; |
my $vpackage = $vtype->[0]; |
378 |
|
my %vopt = %{$vtype->[1]} if ref $vtype->[1]; |
379 |
|
if ($vpackage eq ':none:') { |
380 |
|
return $value; |
381 |
|
} elsif (defined $value) { |
382 |
|
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
383 |
|
return $vpackage->parse ($value, |
384 |
|
-format => $self->{option}->{format}, |
385 |
|
-field_ns => $option{ns}, |
386 |
|
-field_name => $name, |
387 |
|
-header_default_charset => $self->{option}->{header_default_charset}, |
388 |
|
-header_default_charset_input => $self->{option}->{header_default_charset_input}, |
389 |
|
-parse_all => $self->{option}->{parse_all}, |
390 |
|
%vopt); |
391 |
|
} else { |
392 |
|
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
393 |
|
return $vpackage->new ( |
394 |
|
-format => $self->{option}->{format}, |
395 |
|
-field_ns => $option{ns}, |
396 |
|
-field_name => $name, |
397 |
|
-header_default_charset => $self->{option}->{header_default_charset}, |
398 |
|
-header_default_charset_input => $self->{option}->{header_default_charset_input}, |
399 |
|
-parse_all => $self->{option}->{parse_all}, |
400 |
|
%vopt); |
401 |
|
} |
402 |
} |
} |
403 |
|
|
404 |
sub _field_body ($$$) { |
## Defined for text/rfc822-headers |
405 |
my $self = shift; |
sub entity_header ($;$) { |
406 |
my ($body, $name) = @_; |
my $self = shift; |
407 |
unless (ref $body) { |
my $new_header = shift; |
408 |
my $type = $self->{option}->{field_type}->{$name} |
if (ref $new_header) { |
409 |
|| $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}); |
|
|
} |
|
410 |
} |
} |
411 |
$body; |
$self->{header}; |
412 |
} |
} |
413 |
|
|
414 |
=head2 $self->field_name_list () |
=head2 $self->field_name_list () |
421 |
|
|
422 |
sub field_name_list ($) { |
sub field_name_list ($) { |
423 |
my $self = shift; |
my $self = shift; |
424 |
$self->_delete_empty_field (); |
$self->_delete_empty (); |
425 |
map {$_->{name}} @{$self->{field}}; |
map { $_->{name} . ':' . $_->{ns} } @{$self->{value}}; |
426 |
|
} |
427 |
|
|
428 |
|
sub namespace_ph_default ($;$) { |
429 |
|
my $self = shift; |
430 |
|
if (defined $_[0]) { |
431 |
|
no strict 'refs'; |
432 |
|
$self->{option}->{ns_default_phuri} = $_[0]; |
433 |
|
$self->_ns_load_ph (${&_NS_uri2package ($self->{option}->{ns_default_phuri}).'::OPTION'}{namespace_phname}); |
434 |
|
} |
435 |
|
$self->{option}->{ns_default_phuri}; |
436 |
} |
} |
437 |
|
|
438 |
=item $hdr->add ($field-name, $field-body, [$name, $body, ...]) |
=item $hdr->add ($field-name, $field-body, [$name, $body, ...]) |
458 |
|
|
459 |
=cut |
=cut |
460 |
|
|
461 |
sub add ($%) { |
## [Name: Value] pair -> internal array item |
462 |
my $self = shift; |
## $self->_value_to_arrayitem ($name => $value, {%options}) |
463 |
my %fields = @_; |
## or |
464 |
my %option = %{$self->{option}}; |
## $self->_value_to_arrayitem ($name => [$value, %value_options], {%options}) |
465 |
$option{parse} = 1 if defined wantarray; |
## |
466 |
for (grep {/^-/} keys %fields) {$option{substr ($_, 1)} = $fields{$_}} |
## Return: ((1 = success / 0 = failue), $full_name, $arrayitem) |
467 |
my $body; |
sub _value_to_arrayitem ($$$\%) { |
468 |
for (grep {/^[^-]/} keys %fields) { |
my $self = shift; |
469 |
my $name = lc $_; $body = $fields{$_}; |
my ($name, $value, $option) = @_; |
470 |
$name =~ tr/_/-/ if $option{translate_underscore}; |
my $value_option = {}; |
471 |
Carp::croak "add: $name: invalid field-name" |
if (ref $value eq 'ARRAY') { |
472 |
if $option{validate} && $name =~ /$REG{UNSAFE_field_name}/; |
($value, %$value_option) = @$value; |
473 |
$body = $self->_field_body ($body, $name) if $option{parse}; |
} |
474 |
if ($option{prepend}) { |
my $default_ns = $option->{ns_default_phuri}; |
475 |
unshift @{$self->{field}}, {name => $name, body => $body}; |
my $nsuri = $default_ns; |
476 |
} else { |
$name =~ s/^$REG{WSP}+//; $name =~ s/$REG{WSP}+$//; |
477 |
push @{$self->{field}}, {name => $name, body => $body}; |
|
478 |
|
no strict 'refs'; |
479 |
|
if ($value_option->{ns}) { |
480 |
|
$nsuri = $value_option->{ns}; |
481 |
|
} elsif ($option->{ns}) { |
482 |
|
$nsuri = $option->{ns}; |
483 |
|
} elsif (($default_ns eq $self->{ns}->{uri2phname}->{'x-http'} |
484 |
|
&& $name =~ s/^([0-9]+)-//) |
485 |
|
|| ($name =~ s/^x-http-([0-9]+)-//i)) { ## Numric namespace prefix, RFC 2774 |
486 |
|
my $prefix = 0+$1; |
487 |
|
$nsuri = $self->{ns}->{number2uri}->{ $prefix }; |
488 |
|
unless ($nsuri) { |
489 |
|
$self->{ns}->{number2uri}->{ $prefix } = 'urn:x-suika-fam-cx:msgpm:header:x-temp:'.$prefix; |
490 |
|
$nsuri = $self->{ns}->{number2uri}->{ $prefix }; |
491 |
|
} |
492 |
|
} elsif ( |
493 |
|
${ &_NS_uri2package ($default_ns).'::OPTION' }{use_ph_namespace} |
494 |
|
&& ( |
495 |
|
($name =~ s/^([Xx]-[A-Za-z0-9]+|[A-Za-z]*[A-WYZa-wyz0-9][A-Za-z0-9]*)- |
496 |
|
([Xx]-[A-Za-z0-9]+|[A-Za-z0-9]*[A-WYZa-wyz0-9][A-Za-z0-9]*)-//x) |
497 |
|
|| $name =~ s/^([Xx]-[A-Za-z0-9]+|[A-Za-z0-9]*[A-WYZa-wyz0-9][A-Za-z0-9]*)-// |
498 |
|
)) { |
499 |
|
my ($prefix1, $prefix2) = ($1, $2); |
500 |
|
my $original_prefix = $&; my $one_prefix = 0; |
501 |
|
unless ($prefix2) { |
502 |
|
$prefix2 = $prefix1; |
503 |
|
$prefix1 = $self->{ns}->{uri2phname}->{ $default_ns }; |
504 |
|
$one_prefix = 1; |
505 |
|
} |
506 |
|
my $prefix |
507 |
|
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_prefix} } |
508 |
|
($self, &_NS_uri2package ($nsuri), $prefix1.'-'.$prefix2); |
509 |
|
$self->_ns_load_ph ($prefix); |
510 |
|
$nsuri = $self->{ns}->{phname2uri}->{ $prefix }; |
511 |
|
unless ($nsuri) { |
512 |
|
$nsuri = $default_ns; |
513 |
|
$prefix |
514 |
|
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_prefix} } |
515 |
|
($self, &_NS_uri2package ($nsuri), $one_prefix? $prefix2: $prefix1); |
516 |
|
$self->_ns_load_ph ($prefix); |
517 |
|
$nsuri = $self->{ns}->{phname2uri}->{ $prefix }; |
518 |
|
if ($nsuri) { |
519 |
|
$name = $prefix2 . '-' . $name unless $one_prefix; |
520 |
|
} else { |
521 |
|
$name = $original_prefix . $name; |
522 |
|
$nsuri = $default_ns; |
523 |
|
} |
524 |
} |
} |
525 |
} |
} |
526 |
$body if $option{parse}; |
$name |
527 |
|
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_name} } |
528 |
|
($self, &_NS_uri2package ($nsuri), $name); |
529 |
|
Carp::croak "$name: invalid field-name" |
530 |
|
if $option->{field_name_validation} |
531 |
|
&& $name =~ /$REG{ $option->{field_name_unsafe_rule} }/; |
532 |
|
$value = $self->_parse_value ($name => $value, ns => $nsuri) |
533 |
|
if $option->{parse} || $option->{parse_all}; |
534 |
|
$option->{parse} = 0; |
535 |
|
(1, $name.':'.$nsuri => {name => $name, body => $value, ns => $nsuri}); |
536 |
} |
} |
537 |
|
*_add_hash_check = \&_value_to_arrayitem; |
538 |
|
*_replace_hash_check = \&_value_to_arrayitem; |
539 |
|
|
540 |
=head2 $self->relace ($field_name, $field_body) |
=head2 $self->relace ($field_name, $field_body) |
541 |
|
|
547 |
|
|
548 |
=cut |
=cut |
549 |
|
|
550 |
sub replace ($%) { |
sub _replace_hash_shift ($\%$\%) { |
551 |
my $self = shift; |
shift; my $r = shift; my $n = $_[0]->{name} . ':' . $_[0]->{ns}; |
552 |
my %params = @_; |
if ($$r{$n}) { |
553 |
my %option = %{$self->{option}}; |
my $d = $$r{$n}; |
554 |
$option{parse} = defined wantarray unless defined $option{parse}; |
delete $$r{$n}; |
555 |
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; |
|
|
} |
|
556 |
} |
} |
557 |
for (keys %new_field) { |
undef; |
|
push @{$self->{field}}, {name => $_, body => $new_field{$_}}; |
|
|
} |
|
|
$body if $option{parse}; |
|
558 |
} |
} |
559 |
|
|
560 |
=head2 $self->delete ($field-name, [$name, ...]) |
=head2 $self->delete ($field-name, [$name, ...]) |
563 |
|
|
564 |
=cut |
=cut |
565 |
|
|
|
sub delete ($@) { |
|
|
my $self = shift; |
|
|
my %delete; for (@_) {$delete{lc $_} = 1} |
|
|
for my $field (@{$self->{field}}) { |
|
|
undef $field if $delete{$field->{name}}; |
|
|
} |
|
|
} |
|
566 |
|
|
567 |
=head2 $self->count ([$field_name]) |
=head2 $self->count ([$field_name]) |
568 |
|
|
572 |
|
|
573 |
=cut |
=cut |
574 |
|
|
575 |
sub count ($;$) { |
sub _count_by_name ($$\%) { |
576 |
my $self = shift; |
my $self = shift; |
577 |
my ($name) = (lc shift); |
my ($array, $option) = @_; |
578 |
unless ($name) { |
my $name = $self->_n11n_field_name ($$option{-name}); |
579 |
$self->_delete_empty_field (); |
my @a = grep {$_->{name} eq $name} @{$self->{$array}}; |
580 |
return $#{$self->{field}}+1; |
$#a + 1; |
581 |
} |
} |
582 |
my $count = 0; |
|
583 |
for my $field (@{$self->{field}}) { |
## Delete empty items |
584 |
if ($field->{name} eq $name) { |
sub _delete_empty ($) { |
585 |
$count++; |
my $self = shift; |
586 |
} |
my $array = $self->{option}->{_HASH_NAME}; |
587 |
} |
$self->{$array} = [grep {ref $_ && length $_->{name}} @{$self->{$array}}]; |
|
$count; |
|
588 |
} |
} |
589 |
|
|
590 |
=head2 $self->rename ($field-name, $new-name, [$old, $new,...]) |
=head2 $self->rename ($field-name, $new-name, [$old, $new,...]) |
600 |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
601 |
my %new_name; |
my %new_name; |
602 |
for (grep {/^[^-]/} keys %params) { |
for (grep {/^[^-]/} keys %params) { |
603 |
my ($old => $new) = (lc $_ => lc $params{$_}); |
my ($old => $new) |
604 |
|
= ($self->_n11n_field_name ($_) => $self->_n11n_field_name ($params{$_})); |
605 |
|
$old =~ tr/_/-/ if $option{translate_underscore}; |
606 |
$new =~ tr/_/-/ if $option{translate_underscore}; |
$new =~ tr/_/-/ if $option{translate_underscore}; |
607 |
Carp::croak "rename: $new: invalid field-name" |
Carp::croak "rename: $new: invalid field-name" |
608 |
if $option{validate} && $new =~ /$REG{UNSAFE_field_name}/; |
if $option{field_name_validation} |
609 |
|
&& $new =~ /$REG{$option{field_name_unsafe_rule}}/; |
610 |
$new_name{$old} = $new; |
$new_name{$old} = $new; |
611 |
} |
} |
612 |
for my $field (@{$self->{field}}) { |
for my $field (@{$self->{value}}) { |
613 |
if (length $new_name{$field->{name}}) { |
if (length $new_name{$field->{name}}) { |
614 |
$field->{name} = $new_name{$field->{name}}; |
$field->{name} = $new_name{$field->{name}}; |
615 |
} |
} |
627 |
|
|
628 |
=cut |
=cut |
629 |
|
|
630 |
sub scan ($&) { |
sub _scan_sort ($\@\%) { |
631 |
my ($self, $sub) = @_; |
my $self = shift; |
632 |
|
my ($array, $option) = @_; |
633 |
my $sort; |
my $sort; |
634 |
$sort = \&_header_cmp if $self->{option}->{sort} eq 'good-practice'; |
$sort = \&_header_cmp if $option->{field_sort} eq 'good-practice'; |
635 |
$sort = {$a cmp $b} if $self->{option}->{sort} eq 'alphabetic'; |
$sort = {$a cmp $b} if $option->{field_sort} eq 'alphabetic'; |
636 |
my @field = @{$self->{field}}; |
return ( sort $sort @$array ) if ref $sort; |
637 |
if (ref $sort) { |
@$array; |
638 |
@field = sort $sort @{$self->{field}}; |
} |
639 |
} |
|
640 |
for my $field (@field) { |
sub _n11n_field_name ($$) { |
641 |
next if $field->{name} =~ /^_/; |
no strict 'refs'; |
642 |
&$sub($field->{name} => $field->{body}); |
my $self = shift; |
643 |
} |
my $s = shift; |
644 |
|
$s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//; |
645 |
|
$s = lc $s unless ${&_NS_uri2package ($self->{option}->{ns_default_phuri}).'::OPTION'}{case_sensible}; |
646 |
|
$s; |
647 |
} |
} |
648 |
|
|
649 |
# Compare function which makes it easy to sort headers in the |
# Compare function which makes it easy to sort headers in the |
674 |
$option{format} = $params{-format} if $params{-format}; |
$option{format} = $params{-format} if $params{-format}; |
675 |
$self->_init_by_format ($option{format}, \%option); |
$self->_init_by_format ($option{format}, \%option); |
676 |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
677 |
|
## Fill required fields |
678 |
|
my %exist; |
679 |
|
for ($self->field_name_list) {$exist{$_} = 1} |
680 |
|
&{ $option{hook_stringify_fill_fields} } ($self, \%exist, \%option); |
681 |
my @ret; |
my @ret; |
682 |
|
## RFC 2774 numerical field name prefix |
683 |
|
my %nprefix; |
684 |
|
{no strict 'refs'; |
685 |
|
%nprefix = reverse %{ $self->{ns}->{number2uri} }; |
686 |
|
my $i = (sort { $a <=> $b } keys %{ $self->{ns}->{number2uri} })[-1] + 1; |
687 |
|
$i = 10 if $i < 10; |
688 |
|
my $hprefix = ${ &_NS_uri2package |
689 |
|
($self->{ns}->{phname2uri}->{'x-http'}) |
690 |
|
.'::OPTION' } {namespace_phname_goodcase}; |
691 |
|
for my $uri (keys %nprefix) { |
692 |
|
if ($nprefix{ $uri } < 10) { |
693 |
|
$nprefix{ $uri } = $i++; |
694 |
|
} |
695 |
|
my $nsfs = $self->item ($uri, -by => 'http-ns-define'); |
696 |
|
for my $i (0..$nsfs->count-1) { |
697 |
|
my $nsf = ($nsfs->value ($i))[0]; |
698 |
|
if ($nsf->value eq $uri) { |
699 |
|
$nsf->replace (ns => $nprefix{ $uri }); |
700 |
|
$nprefix{ $uri } = $hprefix . '-' . $nprefix{ $uri }; |
701 |
|
last; |
702 |
|
} |
703 |
|
} |
704 |
|
} |
705 |
|
} |
706 |
my $_stringify = sub { |
my $_stringify = sub { |
707 |
my ($name, $body) = (@_); |
no strict 'refs'; |
708 |
|
my ($name, $body, $nsuri) = ($_[1]->{name}, $_[1]->{body}, $_[1]->{ns}); |
709 |
return unless length $name; |
return unless length $name; |
710 |
return if $option{mail_from} && $name eq 'mail-from'; |
return if $option{output_mail_from} && $name eq 'mail-from'; |
711 |
return if !$option{output_bcc} && ($name eq 'bcc' || $name eq 'resent-bcc'); |
$body = '' if !$option{output_bcc} && $name eq 'bcc'; |
712 |
|
my $nspackage = &_NS_uri2package ($nsuri); |
713 |
|
my $oname; ## Outputed field-name |
714 |
|
my $prefix = $nprefix{ $nsuri } |
715 |
|
|| ${$nspackage.'::OPTION'} {namespace_phname_goodcase} |
716 |
|
|| $self->{ns}->{uri2phname}->{ $nsuri }; |
717 |
|
my $default_prefix = ${ &_NS_uri2package ($option{ns_default_phuri}) |
718 |
|
.'::OPTION'} {namespace_phname_goodcase}; |
719 |
|
$prefix = '' if $prefix eq $default_prefix; |
720 |
|
$prefix =~ s/^\Q$default_prefix\E-//; |
721 |
|
my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase}; |
722 |
|
if (ref $gc) { $oname = &$gc ($self, $nspackage, $name, \%option) } |
723 |
|
else { $oname = $name } |
724 |
|
if ($prefix) { $oname = $prefix . '-' . $oname } |
725 |
if ($option{format} =~ /uri-url-mailto/) { |
if ($option{format} =~ /uri-url-mailto/) { |
726 |
return if (( $option{uri_mailto_safe}->{$name} |
return if (( ${$nspackage.'::OPTION'} {uri_mailto_safe}->{$name} |
727 |
|| $option{uri_mailto_safe}->{':default'}) |
|| ${$nspackage.'::OPTION'} {uri_mailto_safe}->{':default'}) |
728 |
< $option{uri_mailto_safe_level}); |
< $option{uri_mailto_safe_level}); |
729 |
if ($name eq 'to') { |
if ($name eq 'to') { |
730 |
$body = $self->field ('to'); |
$body = $self->field ('to', -new_item_unless_exist => 0); |
731 |
return unless ref $body && $body->have_group; |
if (ref $body && $body->have_group) { |
732 |
|
# |
733 |
|
} elsif (ref $body && $body->count > 1) { |
734 |
|
$body = $body->clone; |
735 |
|
$body->delete ({-by => 'index'}, 0); |
736 |
|
} |
737 |
} |
} |
738 |
} |
} |
739 |
my $fbody; |
my $fbody; |
742 |
} else { |
} else { |
743 |
$fbody = $body; |
$fbody = $body; |
744 |
} |
} |
745 |
return unless length $fbody; |
unless (${$nspackage.'::OPTION'} {field}->{$name}->{empty_body}) { |
746 |
|
return unless length $fbody; |
747 |
|
} |
748 |
unless ($option{linebreak_strict}) { |
unless ($option{linebreak_strict}) { |
749 |
## bare \x0D and bare \x0A are unsafe |
## bare \x0D and bare \x0A are unsafe |
750 |
$fbody =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g; |
$fbody =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g; |
752 |
} else { |
} else { |
753 |
$fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g; |
$fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g; |
754 |
} |
} |
755 |
if (ref $option{capitalize}) { |
if ($option{use_folding}) { |
756 |
$name = &{$option{capitalize}} ($self, $name); |
if (ref $option{output_folding}) { |
757 |
} elsif ($option{capitalize}) { |
$fbody = &{$option{output_folding}} ($self, $fbody, |
758 |
$name =~ s/((?:^|-)[a-z])/uc($1)/ge; |
-initial_length => length ($oname) +2); |
759 |
} |
} elsif ($option{output_folding}) { |
760 |
if (ref $option{fold}) { |
$fbody = $self->_fold ($fbody, -initial_length => length ($oname) +2); |
761 |
$fbody = &{$option{fold}} ($self, $fbody); |
} |
|
} elsif ($option{fold}) { |
|
|
$fbody = $self->_fold ($fbody); |
|
762 |
} |
} |
763 |
push @ret, sprintf $option{field_format_pattern}, $name, $fbody; |
push @ret, sprintf $option{field_format_pattern}, $oname, $fbody; |
764 |
}; |
}; |
765 |
if ($option{format} =~ /uri-url-mailto-to/) { |
if ($option{format} =~ /uri-url-mailto/) { |
766 |
if ($self->field_exist ('to')) { |
if ($option{format} =~ /uri-url-mailto-to/) { |
767 |
my $to = $self->field ('to'); |
my $to = $self->field ('to', -new_item_unless_exist => 0); |
768 |
unless ($to->have_group) { |
if ($to) { |
769 |
my $fbody = $to->stringify (-format => $option{format}); |
unless ($to->have_group) { |
770 |
return &{$option{fold}} ($self, $fbody); |
my $fbody = $to->stringify (-format => $option{format}, -max => 1); |
771 |
|
return &{$option{output_folding}} ($self, $fbody); |
772 |
|
} |
773 |
|
} |
774 |
|
''; |
775 |
|
} elsif ($option{format} =~ /uri-url-mailto-rfc1738/) { |
776 |
|
my $to = $self->field ('to', -new_item_unless_exist => 0); |
777 |
|
if ($to) { |
778 |
|
my $fbody = $to->addr_spec (-format => $option{format}); |
779 |
|
return &{$option{output_folding}} ($self, $fbody); |
780 |
} |
} |
781 |
|
''; |
782 |
|
} else { |
783 |
|
$self->scan ($_stringify); |
784 |
|
my $ret = join ('&', @ret); |
785 |
|
$ret; |
786 |
} |
} |
|
''; |
|
|
} elsif ($option{format} =~ /uri-url-mailto/) { |
|
|
$self->scan ($_stringify); |
|
|
my $ret = join ('&', @ret); |
|
|
$ret; |
|
787 |
} else { |
} else { |
788 |
if ($option{mail_from}) { |
if ($option{output_mail_from}) { |
789 |
my $fromline = $self->field ('mail-from'); |
my $fromline = $self->field ('mail-from', -new_item_unless_exist => 0); |
790 |
push @ret, 'From '.$fromline if $fromline; |
push @ret, 'From '.$fromline if $fromline; |
791 |
} |
} |
792 |
$self->scan ($_stringify); |
$self->scan ($_stringify); |
793 |
my $ret = join ("\n", @ret); |
my $ret = join ("\x0D\x0A", @ret); |
794 |
$ret? $ret."\n": ''; |
$ret? $ret."\x0D\x0A": ''; |
795 |
} |
} |
796 |
} |
} |
797 |
*as_string = \&stringify; |
*as_string = \&stringify; |
808 |
return $self->{option}->{ shift (@_) }; |
return $self->{option}->{ shift (@_) }; |
809 |
} |
} |
810 |
while (my ($name, $value) = splice (@_, 0, 2)) { |
while (my ($name, $value) = splice (@_, 0, 2)) { |
|
$name =~ s/^-//; |
|
811 |
$self->{option}->{$name} = $value; |
$self->{option}->{$name} = $value; |
812 |
if ($name eq 'format') { |
if ($name eq 'format') { |
813 |
for my $f (@{$self->{field}}) { |
for my $f (@{$self->{field}}) { |
819 |
} |
} |
820 |
} |
} |
821 |
|
|
822 |
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; |
|
|
} |
|
823 |
|
|
824 |
sub _fold ($$;$) { |
## $self->_fold ($string, %option = (-max, -initial_length(for field-name)) ) |
825 |
|
sub _fold ($$;%) { |
826 |
my $self = shift; |
my $self = shift; |
827 |
my $string = shift; |
my $string = shift; |
828 |
my $len = shift || $self->{option}->{fold_length}; |
my %option = @_; |
829 |
$len = 60 if $len < 60; |
my $max = $self->{option}->{line_length_max}; |
830 |
|
$max = 20 if $max < 20; |
831 |
|
|
832 |
## This code is taken from Mail::Header 1.43 in MailTools, |
my $l = $option{-initial_length} || 0; |
833 |
## by Graham Barr (Maintained by Mark Overmeer <mailtools@overmeer.net>). |
$l += length $1 if $string =~ /^([^\x09\x20]+)/; |
834 |
my $max = int($len - 5); # 4 for leading spcs + 1 for [\,\;] |
$string =~ s{([\x09\x20][^\x09\x20]+)}{ |
835 |
my $min = int($len * 4 / 5) - 4; |
my $s = $1; |
836 |
my $ml = $len; |
if (($l + length $s) > $max) { |
837 |
|
$s = "\x0D\x0A\x20" . $s; |
838 |
if (length($string) > $ml) { |
$l = 1 + length $s; |
839 |
#Split the line up |
} else { $l += length $s } |
840 |
# first bias towards splitting at a , or a ; >4/5 along the line |
$s; |
841 |
# 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; |
|
|
} |
|
842 |
$string; |
$string; |
843 |
} |
} |
844 |
|
|
845 |
|
sub _ns_load_ph ($$) { |
846 |
|
my $self = shift; |
847 |
|
my $name = shift; ## normalized prefix (without HYPHEN-MINUS) |
848 |
|
return if $self->{ns}->{phname2uri}->{$name}; |
849 |
|
$self->{ns}->{phname2uri}->{$name} = $NS_phname2uri{$name}; |
850 |
|
return unless $self->{ns}->{phname2uri}->{$name}; |
851 |
|
$self->{ns}->{uri2phname}->{$self->{ns}->{phname2uri}->{$name}} = $name; |
852 |
|
} |
853 |
|
|
854 |
|
sub _ns_associate_numerical_prefix ($) { |
855 |
|
my $self = shift; |
856 |
|
$self->scan (sub {shift; |
857 |
|
my $f = shift; return unless $f->{name}; |
858 |
|
if ($f->{ns} eq $self->{ns}->{phname2uri}->{'x-http'} |
859 |
|
|| $f->{ns} eq $self->{ns}->{phname2uri}->{'x-http-c'}) { |
860 |
|
my $fn = $f->{name}; |
861 |
|
if ($fn eq 'opt' || $fn eq 'man') { |
862 |
|
$f->{body} = $self->_parse_value ($fn => $f->{body}, ns => $f->{ns}); |
863 |
|
for ($f->{body}->value (0..$f->{body}->count-1)) { |
864 |
|
my ($nsuri, $number) = ($_->value, $_->item ('ns')); |
865 |
|
if ($number && $nsuri) { |
866 |
|
$self->{ns}->{number2uri}->{ $number } = $nsuri; |
867 |
|
} |
868 |
|
} |
869 |
|
} |
870 |
|
} |
871 |
|
}); |
872 |
|
$self->scan (sub {shift; |
873 |
|
my $f = shift; |
874 |
|
if ($f->{ns} =~ /urn:x-suika-fam-cx:msgpm:header:x-temp:([0-9]+)$/ && $self->{ns}->{number2uri}->{ $1 }) { |
875 |
|
$f->{ns} = $self->{ns}->{number2uri}->{ $1 }; |
876 |
|
} |
877 |
|
}); |
878 |
|
} |
879 |
|
|
880 |
|
## $package_name = Message::Header::_NS_uri2phpackage ($nsuri) |
881 |
|
## (For internal use of Message::* modules) |
882 |
|
sub _NS_uri2phpackage ($) { |
883 |
|
$NS_uri2phpackage{$_[0]} |
884 |
|
|| $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}}; |
885 |
|
} |
886 |
|
sub _NS_uri2package ($) { |
887 |
|
$NS_uri2package{$_[0]} |
888 |
|
|| $NS_uri2phpackage{$_[0]} |
889 |
|
|| $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}}; |
890 |
|
} |
891 |
|
|
892 |
=head2 $self->clone () |
=head2 $self->clone () |
893 |
|
|
894 |
Returns a copy of Message::Header object. |
Returns a copy of Message::Header object. |
895 |
|
|
896 |
=cut |
=cut |
897 |
|
|
898 |
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; |
|
|
} |
|
899 |
|
|
900 |
=head1 NOTE |
=head1 NOTE |
901 |
|
|