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_uri2phpackage; ## namespace URI -> PH-package name |
26 |
method. |
require Message::Header::Default; ## Default namespace |
27 |
|
|
28 |
=head3 capitalize = 0/1 |
## Initialize of this class -- called by constructors |
29 |
|
%DEFAULT = ( |
30 |
(First character of) C<field-name> is capitalized |
-_HASH_NAME => 'value', |
31 |
when C<stringify>. (Default = 1) |
-_METHODS => [qw|field field_exist field_type add replace count delete subject id is|], |
32 |
|
-_MEMBERS => [qw|value|], |
33 |
=head3 fold_length = numeric value |
-M_namsepace_prefix_regex => qr/(?!)/, |
34 |
|
-_VALTYPE_DEFAULT => ':default', |
35 |
Length of line used to fold. (Default = 70) |
-by => 'name', ## (Reserved for method level option) |
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 => 1, ## Method level option. |
40 |
when C<stringify>. (Default = 0) |
-field_sort => 0, |
41 |
|
#-format => 'mail-rfc2822', |
42 |
=cut |
-linebreak_strict => 0, ## Not implemented completely |
43 |
|
-line_length_max => 60, ## For folding |
44 |
=head1 CONSTRUCTORS |
-ns_default_uri => $Message::Header::Default::OPTION{namespace_uri}, |
45 |
|
-output_bcc => 0, |
46 |
The following methods construct new C<Message::Header> objects: |
-output_folding => 1, |
47 |
|
-output_mail_from => 0, |
48 |
=over 4 |
#-parse_all => 0, |
49 |
|
-translate_underscore => 1, |
50 |
=cut |
#-uri_mailto_safe |
51 |
|
-uri_mailto_safe_level => 4, |
52 |
## Initialize |
-use_folding => 1, |
53 |
my %DEFAULT = ( |
#-value_type |
|
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, |
|
54 |
); |
); |
55 |
$DEFAULT{field_type} = { |
|
56 |
':DEFAULT' => 'Message::Field::Unstructured', |
$DEFAULT{-value_type} = { |
57 |
|
':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', |
|
58 |
|
|
59 |
## Numeric value |
p3p => ['Message::Field::Params'], |
60 |
'content-length' => 'Message::Field::Numval', |
link => ['Message::Field::ValueParams'], |
|
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', |
|
61 |
|
|
62 |
path => 'Message::Field::Path', |
'list-software' => ['Message::Field::UA'], |
63 |
|
'user-agent' => ['Message::Field::UA'], |
64 |
|
server => ['Message::Field::UA'], |
65 |
}; |
}; |
66 |
for (qw(archive cancel-lock content-features content-md5 |
for (qw(pics-label list-id status)) |
67 |
disposition-notification-options encoding |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::Structured']} |
|
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'} |
|
68 |
## Not supported yet, but to be supported... |
## Not supported yet, but to be supported... |
69 |
# x-list: unstructured, ml name |
# x-list: unstructured, ml name |
70 |
for (qw(abuse-reports-to apparently-to approved approved-by bcc cc complaints-to |
for (qw(date expires)) |
71 |
delivered-to disposition-notification-to envelope-to |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::Date']} |
72 |
errors-to from mail-copies-to mail-followup-to mail-reply-to |
for (qw(accept accept-charset accept-encoding accept-language uri)) |
73 |
notice-requested-upon-delivery-to read-receipt-to register-mail-reply-requested-by |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::CSV']} |
74 |
reply-to resent-bcc |
for (qw(location referer)) |
75 |
resent-cc resent-to resent-from resent-sender return-path |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::URI']} |
|
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'} |
|
76 |
|
|
77 |
my %header_goodcase = ( |
my %header_goodcase = ( |
78 |
'article-i.d.' => 'Article-I.D.', |
'article-i.d.' => 'Article-I.D.', |
|
'content-id' => 'Content-ID', |
|
|
'content-md5' => 'Content-MD5', |
|
|
'content-sgml-entity' => 'Content-SGML-Entity', |
|
79 |
etag => 'ETag', |
etag => 'ETag', |
|
fax => 'FAX', |
|
80 |
'pics-label' => 'PICS-Label', |
'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', |
|
81 |
te => 'TE', |
te => 'TE', |
82 |
url => 'URL', |
url => 'URL', |
83 |
'www-authenticate' => 'WWW-Authenticate', |
'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', |
|
84 |
); |
); |
|
$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; |
|
|
}; |
|
85 |
|
|
86 |
## taken from L<HTTP::Header> |
## taken from L<HTTP::Header> |
87 |
# "Good Practice" order of HTTP message headers: |
# "Good Practice" order of HTTP message headers: |
113 |
); |
); |
114 |
my %header_order; |
my %header_order; |
115 |
|
|
116 |
|
=head1 CONSTRUCTORS |
117 |
|
|
118 |
|
The following methods construct new C<Message::Header> objects: |
119 |
|
|
120 |
|
=over 4 |
121 |
|
|
122 |
|
=cut |
123 |
|
|
124 |
sub _init ($;%) { |
sub _init ($;%) { |
125 |
my $self = shift; |
my $self = shift; |
126 |
my %options = @_; |
my %options = @_; |
127 |
$self->{field} = []; |
my $DEFAULT = Message::Util::make_clone (\%DEFAULT); |
128 |
$self->{option} = \%DEFAULT; |
$self->SUPER::_init (%$DEFAULT, %options); |
129 |
|
$self->{value} = []; |
130 |
|
$self->_ns_load_ph ('default'); |
131 |
|
$self->{ns}->{default_phuri} = $self->{ns}->{phname2uri}->{'default'}; |
132 |
|
$self->_ns_load_ph ('rfc822'); |
133 |
|
$self->{ns}->{default_phuri} = $self->{ns}->{phname2uri}->{'rfc822'}; |
134 |
|
|
135 |
my @new_fields = (); |
my @new_fields = (); |
136 |
for my $name (keys %options) { |
for my $name (keys %options) { |
137 |
if (substr ($name, 0, 1) eq '-') { |
unless (substr ($name, 0, 1) eq '-') { |
|
$self->{option}->{substr ($name, 1)} = $options{$name}; |
|
|
} else { |
|
138 |
push @new_fields, ($name => $options{$name}); |
push @new_fields, ($name => $options{$name}); |
139 |
} |
} |
140 |
} |
} |
153 |
sub _init_by_format ($$\%) { |
sub _init_by_format ($$\%) { |
154 |
my $self = shift; |
my $self = shift; |
155 |
my ($format, $option) = @_; |
my ($format, $option) = @_; |
156 |
if ($format =~ /rfc822/) { |
if ($format =~ /cgi/) { |
|
$header_goodcase{bcc} = 'bcc'; |
|
|
$header_goodcase{cc} = 'cc'; |
|
|
$header_goodcase{'resent-bcc'} = 'Resent-bcc'; |
|
|
$header_goodcase{'resent-cc'} = 'Resent-cc'; |
|
|
} elsif ($format =~ /cgi/) { |
|
157 |
unshift @header_order, qw(content-type location); |
unshift @header_order, qw(content-type location); |
158 |
$option->{sort} = 'good-practice'; |
$option->{field_sort} = 'good-practice'; |
159 |
$option->{fold} = 0; |
$option->{use_folding} = 0; |
160 |
} elsif ($format =~ /http/) { |
} elsif ($format =~ /http/) { |
161 |
$option->{sort} = 'good-practice'; |
$option->{field_sort} = 'good-practice'; |
162 |
} |
} |
163 |
if ($format =~ /uri-url-mailto/) { |
if ($format =~ /uri-url-mailto/) { |
164 |
$option->{output_bcc} = 0; |
$option->{output_bcc} = 0; |
|
$option->{capitalize} = 0; |
|
165 |
$option->{field_format_pattern} = '%s=%s'; |
$option->{field_format_pattern} = '%s=%s'; |
166 |
$option->{fold} = sub { |
$option->{output_folding} = sub { |
167 |
$_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge; |
$_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge; |
168 |
$_[1]; |
$_[1]; |
169 |
}; |
}; ## Yes, this is not folding! |
170 |
} |
} |
171 |
} |
} |
172 |
|
|
173 |
=item Message::Header->new ([%initial-fields/options]) |
=item $msg = Message::Header->new ([%initial-fields/options]) |
174 |
|
|
175 |
Constructs a new C<Message::Headers> object. You might pass some initial |
Constructs a new C<Message::Headers> object. You might pass some initial |
176 |
C<field-name>-C<field-body> pairs and/or options as parameters to the constructor. |
C<field-name>-C<field-body> pairs and/or options as parameters to the constructor. |
186 |
|
|
187 |
=cut |
=cut |
188 |
|
|
189 |
sub new ($;%) { |
## Inherited |
|
my $class = shift; |
|
|
my $self = bless {}, $class; |
|
|
$self->_init (@_); |
|
|
$self; |
|
|
} |
|
190 |
|
|
191 |
=item Message::Header->parse ($header, [%initial-fields/options]) |
=item $msg = Message::Header->parse ($header, [%initial-fields/options]) |
192 |
|
|
193 |
Parses given C<header> and constructs a new C<Message::Headers> |
Parses given C<header> and constructs a new C<Message::Headers> |
194 |
object. You might pass some additional C<field-name>-C<field-body> pairs |
object. You might pass some additional C<field-name>-C<field-body> pairs |
201 |
my $header = shift; |
my $header = shift; |
202 |
my $self = bless {}, $class; |
my $self = bless {}, $class; |
203 |
$self->_init (@_); ## BUG: don't check linebreak_strict |
$self->_init (@_); ## BUG: don't check linebreak_strict |
204 |
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos; ## unfold |
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding}; |
205 |
for my $field (split /\x0D?\x0A/, $header) { |
for my $field (split /\x0D?\x0A/, $header) { |
206 |
if ($field =~ /$REG{M_fromline}/) { |
if ($field =~ /$REG{M_fromline}/) { |
207 |
my $body = $1; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
208 |
$body = $self->_field_body ($body, 'mail-from') |
('mail-from' => $1, $self->{option}); |
209 |
if $self->{option}->{parse_all}; |
push @{$self->{value}}, $value if $s; |
|
push @{$self->{field}}, {name => 'mail-from', body => $body}; |
|
210 |
} elsif ($field =~ /$REG{M_field}/) { |
} elsif ($field =~ /$REG{M_field}/) { |
211 |
my ($name, $body) = (lc $1, $2); |
my ($name, $body) = ($1, $2); |
|
$name =~ s/$REG{WSP}+$//; |
|
212 |
$body =~ s/$REG{WSP}+$//; |
$body =~ s/$REG{WSP}+$//; |
213 |
$body = $self->_field_body ($body, $name) if $self->{option}->{parse_all}; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
214 |
push @{$self->{field}}, {name => $name, body => $body}; |
($name => $body, $self->{option}); |
215 |
|
push @{$self->{value}}, $value if $s; |
216 |
} |
} |
217 |
} |
} |
218 |
$self; |
$self; |
219 |
} |
} |
220 |
|
|
221 |
=item Message::Header->parse_array (\@header, [%initial-fields/options]) |
=item $msg = Message::Header->parse_array (\@header, [%initial-fields/options]) |
222 |
|
|
223 |
Parses given C<header> and constructs a new C<Message::Headers> |
Parses given C<header> and constructs a new C<Message::Headers> |
224 |
object. Same as C<Message::Header-E<lt>parse> but this method |
object. Same as C<Message::Header-E<lt>parse> but this method |
237 |
$self->_init (@_); |
$self->_init (@_); |
238 |
while (1) { |
while (1) { |
239 |
my $field = shift @$header; |
my $field = shift @$header; |
240 |
while (1) { |
if ($self->{option}->{use_folding}) { |
241 |
if ($$header[0] =~ /^$REG{WSP}/) { |
while (1) { |
242 |
$field .= shift @$header; |
if ($$header[0] =~ /^$REG{WSP}/) { |
243 |
} else {last} |
$field .= shift @$header; |
244 |
|
} else {last} |
245 |
|
} |
246 |
} |
} |
247 |
if ($self->{option}->{linebreak_strict}) { |
if ($self->{option}->{linebreak_strict}) { |
248 |
$field =~ s/\x0D\x0A//g; |
$field =~ s/\x0D\x0A//g; |
249 |
} else { |
} else { |
250 |
$field =~ tr/\x0D\x0A//d; |
$field =~ tr/\x0D\x0A//d; |
251 |
} |
} |
252 |
|
local $self->{option}->{parse} = $self->{option}->{parse_all}; |
253 |
if ($field =~ /$REG{M_fromline}/) { |
if ($field =~ /$REG{M_fromline}/) { |
254 |
my $body = $1; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
255 |
$body = $self->_field_body ($body, 'mail-from') |
('mail-from' => $1, $self->{option}); |
256 |
if $self->{option}->{parse_all}; |
push @{$self->{value}}, $value if $s; |
|
push @{$self->{field}}, {name => 'mail-from', body => $body}; |
|
257 |
} elsif ($field =~ /$REG{M_field}/) { |
} elsif ($field =~ /$REG{M_field}/) { |
258 |
my ($name, $body) = (lc $1, $2); |
my ($name, $body) = ($self->_n11n_field_name ($1), $2); |
|
$name =~ s/$REG{WSP}+$//; |
|
259 |
$body =~ s/$REG{WSP}+$//; |
$body =~ s/$REG{WSP}+$//; |
260 |
$body = $self->_field_body ($body, $name) if $self->{option}->{parse_all}; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
261 |
push @{$self->{field}}, {name => $name, body => $body}; |
($name => $body, $self->{option}); |
262 |
|
push @{$self->{value}}, $value if $s; |
263 |
} |
} |
264 |
last if $#$header < 0; |
last if $#$header < 0; |
265 |
} |
} |
279 |
|
|
280 |
=cut |
=cut |
281 |
|
|
282 |
sub field ($$) { |
sub field ($@) {shift->SUPER::item (@_)} |
283 |
|
sub field_exist ($@) {shift->SUPER::item_exist (@_)} |
284 |
|
|
285 |
|
## item-by?, \$checked-item, {item-key => 1}, \%option |
286 |
|
sub _item_match ($$\$\%\%) { |
287 |
my $self = shift; |
my $self = shift; |
288 |
my $name = lc shift; |
my ($by, $i, $list, $option) = @_; |
289 |
my @ret; |
return 0 unless ref $$i; ## Already removed |
290 |
for my $field (@{$self->{field}}) { |
if ($by eq 'name') { |
291 |
if ($field->{name} eq $name) { |
my %o = %$option; $o{parse} = 0; |
292 |
unless (wantarray) { |
my %l; |
293 |
$field->{body} = $self->_field_body ($field->{body}, $name); |
for (keys %$list) { |
294 |
return $field->{body}; |
my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', %o); |
295 |
|
if ($s) { |
296 |
|
$l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1; |
297 |
} else { |
} else { |
298 |
$field->{body} = $self->_field_body ($field->{body}, $name); |
$l{$v->{name} .':'. ( $option->{ns} || $self->{ns}->{default_phuri} ) } = 1; |
|
push @ret, $field->{body}; |
|
299 |
} |
} |
300 |
} |
} |
301 |
|
return 1 if $l{$$i->{name} . ':' . $$i->{ns}}; |
302 |
} |
} |
303 |
if ($#ret < 0) { |
0; |
|
return $self->add ($name); |
|
|
} |
|
|
@ret; |
|
304 |
} |
} |
305 |
|
*_delete_match = \&_item_match; |
306 |
|
|
307 |
sub field_exist ($$) { |
## Returns returned item value \$item-value, \%option |
308 |
my $self = shift; |
sub _item_return_value ($\$\%) { |
309 |
my $name = lc shift; |
if (ref ${$_[1]}->{body}) { |
310 |
my @ret; |
${$_[1]}->{body}; |
311 |
for my $field (@{$self->{field}}) { |
} else { |
312 |
return 1 if ($field->{name} eq $name); |
${$_[1]}->{body} = $_[0]->_parse_value (${$_[1]}->{name} => ${$_[1]}->{body}, |
313 |
|
ns => ${$_[1]}->{ns}); |
314 |
|
${$_[1]}->{body}; |
315 |
} |
} |
|
0; |
|
316 |
} |
} |
317 |
|
|
318 |
=head2 $self->field_name ($index) |
## Returns returned (new created) item value $name, \%option |
319 |
|
sub _item_new_value ($$\%) { |
320 |
Returns C<field-name> of $index'th C<field>. |
my ($s,undef,$value) = $_[0]->_value_to_arrayitem |
321 |
|
($_[1] => '', $_[2]); |
322 |
=head2 $self->field_body ($index) |
$s? $value: undef; |
323 |
|
} |
324 |
|
|
|
Returns C<field-body> of $index'th C<field>. |
|
325 |
|
|
|
=cut |
|
326 |
|
|
327 |
sub field_name ($$) { |
## $self->_parse_value ($type, $value, %options); |
328 |
my $self = shift; |
sub _parse_value ($$$;%) { |
329 |
$self->{field}->[shift]->{name}; |
my $self = shift; |
330 |
} |
my $name = shift ;#|| $self->{option}->{_VALTYPE_DEFAULT}; |
331 |
sub field_body ($$) { |
my $value = shift; return $value if ref $value; |
332 |
my $self = shift; |
my %option = @_; |
333 |
my $i = shift; |
my $vtype; { no strict 'refs'; |
334 |
$self->{field}->[$i]->{body} |
$vtype = ${&_NS_uri2phpackage ($option{ns}).'::OPTION'}{value_type}; |
335 |
= $self->_field_body ($self->{field}->[$i]->{body}, $self->{field}->[$i]->{name}); |
if (ref $vtype) { $vtype = $vtype->{$name} } |
336 |
$self->{field}->[$i]->{body}; |
unless (ref $vtype) { $vtype = $vtype->{$self->{option}->{_VALTYPE_DEFAULT}} } |
337 |
} |
## For compatiblity. |
338 |
|
unless (ref $vtype) { $vtype = $self->{option}->{value_type}->{$name} |
339 |
sub _field_body ($$$) { |
|| $self->{option}->{value_type}->{$self->{option}->{_VALTYPE_DEFAULT}} } |
340 |
my $self = shift; |
} |
341 |
my ($body, $name) = @_; |
my $vpackage = $vtype->[0]; |
342 |
unless (ref $body) { |
my %vopt = %{$vtype->[1]} if ref $vtype->[1]; |
343 |
my $type = $self->{option}->{field_type}->{$name} |
if ($vpackage eq ':none:') { |
344 |
|| $self->{option}->{field_type}->{':DEFAULT'}; |
return $value; |
345 |
eval "require $type" or Carp::croak ("_field_body: $type: $@"); |
} elsif (defined $value) { |
346 |
unless ($body) { |
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
347 |
$body = $type->new (-field_name => $name, |
return $vpackage->parse ($value, |
348 |
-format => $self->{option}->{format}, |
-format => $self->{option}->{format}, |
349 |
-parse_all => $self->{option}->{parse_all}); |
-field_ns => $option{ns}, |
350 |
} else { |
-field_name => $name, |
351 |
$body = $type->parse ($body, -field_name => $name, |
-parse_all => $self->{option}->{parse_all}, |
352 |
-format => $self->{option}->{format}, |
%vopt); |
353 |
-parse_all => $self->{option}->{parse_all}); |
} else { |
354 |
} |
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
355 |
|
return $vpackage->new ( |
356 |
|
-format => $self->{option}->{format}, |
357 |
|
-field_ns => $option{ns}, |
358 |
|
-field_name => $name, |
359 |
|
-parse_all => $self->{option}->{parse_all}, |
360 |
|
%vopt); |
361 |
} |
} |
|
$body; |
|
362 |
} |
} |
363 |
|
|
364 |
=head2 $self->field_name_list () |
=head2 $self->field_name_list () |
371 |
|
|
372 |
sub field_name_list ($) { |
sub field_name_list ($) { |
373 |
my $self = shift; |
my $self = shift; |
374 |
$self->_delete_empty_field (); |
$self->_delete_empty (); |
375 |
map {$_->{name}} @{$self->{field}}; |
map { $_->{name} . ':' . $_->{ns} } @{$self->{value}}; |
376 |
|
} |
377 |
|
|
378 |
|
sub namespace_ph_default ($;$) { |
379 |
|
my $self = shift; |
380 |
|
if (defined $_[0]) { |
381 |
|
no strict 'refs'; |
382 |
|
$self->{ns}->{default_phuri} = $_[0]; |
383 |
|
$self->_ns_load_ph (${&_NS_uri2phpackage ($self->{ns}->{default_phuri}).'::OPTION'}{namespace_phname}); |
384 |
|
} |
385 |
|
$self->{ns}->{default_phuri}; |
386 |
} |
} |
387 |
|
|
388 |
=item $hdr->add ($field-name, $field-body, [$name, $body, ...]) |
=item $hdr->add ($field-name, $field-body, [$name, $body, ...]) |
408 |
|
|
409 |
=cut |
=cut |
410 |
|
|
411 |
sub add ($%) { |
## [Name: Value] pair -> internal array item |
412 |
my $self = shift; |
## $self->_value_to_arrayitem ($name => $value, {%options}) |
413 |
my %fields = @_; |
## or |
414 |
my %option = %{$self->{option}}; |
## $self->_value_to_arrayitem ($name => [$value, %value_options], {%options}) |
415 |
$option{parse} = 1 if defined wantarray; |
## |
416 |
for (grep {/^-/} keys %fields) {$option{substr ($_, 1)} = $fields{$_}} |
## Return: ((1 = success / 0 = failue), $full_name, $arrayitem) |
417 |
my $body; |
sub _value_to_arrayitem ($$$\%) { |
418 |
for (grep {/^[^-]/} keys %fields) { |
my $self = shift; |
419 |
my $name = lc $_; $body = $fields{$_}; |
my ($name, $value, $option) = @_; |
420 |
$name =~ tr/_/-/ if $option{translate_underscore}; |
my $value_option = {}; |
421 |
Carp::croak "add: $name: invalid field-name" |
if (ref $value eq 'ARRAY') { |
422 |
if $option{validate} && $name =~ /$REG{UNSAFE_field_name}/; |
($value, %$value_option) = @$value; |
423 |
$body = $self->_field_body ($body, $name) if $option{parse}; |
} |
424 |
if ($option{prepend}) { |
my $nsuri = $self->{ns}->{default_phuri}; |
425 |
unshift @{$self->{field}}, {name => $name, body => $body}; |
no strict 'refs'; |
426 |
} else { |
if ($option->{ns}) { |
427 |
push @{$self->{field}}, {name => $name, body => $body}; |
$nsuri = $option->{ns}; |
428 |
} |
} elsif ($name =~ s/^([Xx]-[A-Za-z]+|[A-Za-z]+)-//) { |
429 |
} |
my $oprefix = $1; |
430 |
$body if $option{parse}; |
my $prefix |
431 |
|
= &{${&_NS_uri2phpackage ($nsuri).'::OPTION'}{n11n_prefix}} |
432 |
|
($self, &_NS_uri2phpackage ($nsuri), $oprefix); |
433 |
|
$self->_ns_load_ph ($prefix); |
434 |
|
$nsuri = $self->{ns}->{phname2uri}->{$prefix}; |
435 |
|
unless ($nsuri) { |
436 |
|
$name = $oprefix . '-' . $name; |
437 |
|
$nsuri = $self->{ns}->{default_phuri}; |
438 |
|
} |
439 |
|
} |
440 |
|
$name |
441 |
|
= &{${&_NS_uri2phpackage ($nsuri).'::OPTION'}{n11n_name}} |
442 |
|
($self, &_NS_uri2phpackage ($nsuri), $name); |
443 |
|
Carp::croak "$name: invalid field-name" |
444 |
|
if $option->{field_name_validation} |
445 |
|
&& $name =~ /$REG{$option->{field_name_unsafe_rule}}/; |
446 |
|
$value = $self->_parse_value ($name => $value, ns => $nsuri) if $$option{parse}; |
447 |
|
$$option{parse} = 0; |
448 |
|
(1, $name.':'.$nsuri => {name => $name, body => $value, ns => $nsuri}); |
449 |
} |
} |
450 |
|
*_add_hash_check = \&_value_to_arrayitem; |
451 |
|
*_replace_hash_check = \&_value_to_arrayitem; |
452 |
|
|
453 |
=head2 $self->relace ($field_name, $field_body) |
=head2 $self->relace ($field_name, $field_body) |
454 |
|
|
460 |
|
|
461 |
=cut |
=cut |
462 |
|
|
463 |
sub replace ($%) { |
sub _replace_hash_shift ($\%$\%) { |
464 |
my $self = shift; |
shift; my $r = shift; my $n = $_[0]->{name}; |
465 |
my %params = @_; |
if ($$r{$n}) { |
466 |
my %option = %{$self->{option}}; |
my $d = $$r{$n}; |
467 |
$option{parse} = defined wantarray unless defined $option{parse}; |
delete $$r{$n}; |
468 |
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; |
|
|
} |
|
|
} |
|
|
for (keys %new_field) { |
|
|
push @{$self->{field}}, {name => $_, body => $new_field{$_}}; |
|
469 |
} |
} |
470 |
$body if $option{parse}; |
undef; |
471 |
} |
} |
472 |
|
|
473 |
=head2 $self->delete ($field-name, [$name, ...]) |
=head2 $self->delete ($field-name, [$name, ...]) |
476 |
|
|
477 |
=cut |
=cut |
478 |
|
|
|
sub delete ($@) { |
|
|
my $self = shift; |
|
|
my %delete; for (@_) {$delete{lc $_} = 1} |
|
|
for my $field (@{$self->{field}}) { |
|
|
undef $field if $delete{$field->{name}}; |
|
|
} |
|
|
} |
|
479 |
|
|
480 |
=head2 $self->count ([$field_name]) |
=head2 $self->count ([$field_name]) |
481 |
|
|
485 |
|
|
486 |
=cut |
=cut |
487 |
|
|
488 |
sub count ($;$) { |
sub _count_by_name ($$\%) { |
489 |
my $self = shift; |
my $self = shift; |
490 |
my ($name) = (lc shift); |
my ($array, $option) = @_; |
491 |
unless ($name) { |
my $name = $self->_n11n_field_name ($$option{-name}); |
492 |
$self->_delete_empty_field (); |
my @a = grep {$_->{name} eq $name} @{$self->{$array}}; |
493 |
return $#{$self->{field}}+1; |
$#a + 1; |
494 |
} |
} |
495 |
my $count = 0; |
|
496 |
for my $field (@{$self->{field}}) { |
## Delete empty items |
497 |
if ($field->{name} eq $name) { |
sub _delete_empty ($) { |
498 |
$count++; |
my $self = shift; |
499 |
} |
my $array = $self->{option}->{_HASH_NAME}; |
500 |
} |
$self->{$array} = [grep {ref $_ && length $_->{name}} @{$self->{$array}}]; |
|
$count; |
|
501 |
} |
} |
502 |
|
|
503 |
=head2 $self->rename ($field-name, $new-name, [$old, $new,...]) |
=head2 $self->rename ($field-name, $new-name, [$old, $new,...]) |
513 |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
514 |
my %new_name; |
my %new_name; |
515 |
for (grep {/^[^-]/} keys %params) { |
for (grep {/^[^-]/} keys %params) { |
516 |
my ($old => $new) = (lc $_ => lc $params{$_}); |
my ($old => $new) |
517 |
|
= ($self->_n11n_field_name ($_) => $self->_n11n_field_name ($params{$_})); |
518 |
|
$old =~ tr/_/-/ if $option{translate_underscore}; |
519 |
$new =~ tr/_/-/ if $option{translate_underscore}; |
$new =~ tr/_/-/ if $option{translate_underscore}; |
520 |
Carp::croak "rename: $new: invalid field-name" |
Carp::croak "rename: $new: invalid field-name" |
521 |
if $option{validate} && $new =~ /$REG{UNSAFE_field_name}/; |
if $option{field_name_validation} |
522 |
|
&& $new =~ /$REG{$option{field_name_unsafe_rule}}/; |
523 |
$new_name{$old} = $new; |
$new_name{$old} = $new; |
524 |
} |
} |
525 |
for my $field (@{$self->{field}}) { |
for my $field (@{$self->{value}}) { |
526 |
if (length $new_name{$field->{name}}) { |
if (length $new_name{$field->{name}}) { |
527 |
$field->{name} = $new_name{$field->{name}}; |
$field->{name} = $new_name{$field->{name}}; |
528 |
} |
} |
540 |
|
|
541 |
=cut |
=cut |
542 |
|
|
543 |
sub scan ($&) { |
sub _scan_sort ($\@\%) { |
544 |
my ($self, $sub) = @_; |
my $self = shift; |
545 |
|
my ($array, $option) = @_; |
546 |
my $sort; |
my $sort; |
547 |
$sort = \&_header_cmp if $self->{option}->{sort} eq 'good-practice'; |
$sort = \&_header_cmp if $option->{field_sort} eq 'good-practice'; |
548 |
$sort = {$a cmp $b} if $self->{option}->{sort} eq 'alphabetic'; |
$sort = {$a cmp $b} if $option->{field_sort} eq 'alphabetic'; |
549 |
my @field = @{$self->{field}}; |
return ( sort $sort @$array ) if ref $sort; |
550 |
if (ref $sort) { |
@$array; |
551 |
@field = sort $sort @{$self->{field}}; |
} |
552 |
} |
|
553 |
for my $field (@field) { |
sub _n11n_field_name ($$) { |
554 |
next if $field->{name} =~ /^_/; |
my $self = shift; |
555 |
&$sub($field->{name} => $field->{body}); |
my $s = shift; |
556 |
} |
$s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//; |
557 |
|
$s = lc $s ;#unless $self->{option}->{field_name_case_sensible}; |
558 |
|
$s; |
559 |
} |
} |
560 |
|
|
561 |
# Compare function which makes it easy to sort headers in the |
# Compare function which makes it easy to sort headers in the |
588 |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
589 |
my @ret; |
my @ret; |
590 |
my $_stringify = sub { |
my $_stringify = sub { |
591 |
my ($name, $body) = (@_); |
no strict 'refs'; |
592 |
|
my ($name, $body, $nsuri) = ($_[1]->{name}, $_[1]->{body}, $_[1]->{ns}); |
593 |
return unless length $name; |
return unless length $name; |
594 |
return if $option{mail_from} && $name eq 'mail-from'; |
return if $option{output_mail_from} && $name eq 'mail-from'; |
595 |
return if !$option{output_bcc} && ($name eq 'bcc' || $name eq 'resent-bcc'); |
return if !$option{output_bcc} && ($name eq 'bcc' || $name eq 'resent-bcc'); |
596 |
|
my $nspackage = &_NS_uri2phpackage ($nsuri); |
597 |
|
my $oname; ## Outputed field-name |
598 |
|
my $prefix = ${$nspackage.'::OPTION'} {namespace_phname_goodcase} |
599 |
|
|| $self->{ns}->{uri2phname}->{$nsuri}; |
600 |
|
$prefix = undef if $nsuri eq $self->{ns}->{default_phuri}; |
601 |
|
my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase}; |
602 |
|
if (ref $gc) { $oname = &$gc ($self, $nspackage, $name, \%option) } |
603 |
|
else { $oname = $name } |
604 |
|
if ($prefix) { $oname = $prefix . '-' . $oname } |
605 |
if ($option{format} =~ /uri-url-mailto/) { |
if ($option{format} =~ /uri-url-mailto/) { |
606 |
return if (( $option{uri_mailto_safe}->{$name} |
return if (( ${$nspackage.'::OPTION'} {uri_mailto_safe}->{$name} |
607 |
|| $option{uri_mailto_safe}->{':default'}) |
|| ${$nspackage.'::OPTION'} {uri_mailto_safe}->{':default'}) |
608 |
< $option{uri_mailto_safe_level}); |
< $option{uri_mailto_safe_level}); |
609 |
if ($name eq 'to') { |
if ($name eq 'to') { |
610 |
$body = $self->field ('to'); |
$body = $self->field ('to', -new_item_unless_exist => 0); |
611 |
return unless ref $body && $body->have_group; |
if (ref $body && $body->have_group) { |
612 |
|
# |
613 |
|
} elsif (ref $body && $body->count > 1) { |
614 |
|
$body = $body->clone; |
615 |
|
$body->delete ({-by => 'index'}, 0); |
616 |
|
} |
617 |
} |
} |
618 |
} |
} |
619 |
my $fbody; |
my $fbody; |
630 |
} else { |
} else { |
631 |
$fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g; |
$fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g; |
632 |
} |
} |
633 |
if (ref $option{capitalize}) { |
if ($option{use_folding}) { |
634 |
$name = &{$option{capitalize}} ($self, $name); |
if (ref $option{output_folding}) { |
635 |
} elsif ($option{capitalize}) { |
$fbody = &{$option{output_folding}} ($self, $fbody, |
636 |
$name =~ s/((?:^|-)[a-z])/uc($1)/ge; |
-initial_length => length ($oname) +2); |
637 |
} |
} elsif ($option{output_folding}) { |
638 |
if (ref $option{fold}) { |
$fbody = $self->_fold ($fbody, -initial_length => length ($oname) +2); |
639 |
$fbody = &{$option{fold}} ($self, $fbody); |
} |
|
} elsif ($option{fold}) { |
|
|
$fbody = $self->_fold ($fbody); |
|
640 |
} |
} |
641 |
push @ret, sprintf $option{field_format_pattern}, $name, $fbody; |
push @ret, sprintf $option{field_format_pattern}, $oname, $fbody; |
642 |
}; |
}; |
643 |
if ($option{format} =~ /uri-url-mailto-to/) { |
if ($option{format} =~ /uri-url-mailto/) { |
644 |
if ($self->field_exist ('to')) { |
if ($option{format} =~ /uri-url-mailto-to/) { |
645 |
my $to = $self->field ('to'); |
my $to = $self->field ('to', -new_item_unless_exist => 0); |
646 |
unless ($to->have_group) { |
if ($to) { |
647 |
my $fbody = $to->stringify (-format => $option{format}); |
unless ($to->have_group) { |
648 |
return &{$option{fold}} ($self, $fbody); |
my $fbody = $to->stringify (-format => $option{format}, -max => 1); |
649 |
|
return &{$option{output_folding}} ($self, $fbody); |
650 |
|
} |
651 |
|
} |
652 |
|
''; |
653 |
|
} elsif ($option{format} =~ /uri-url-mailto-rfc1738/) { |
654 |
|
my $to = $self->field ('to', -new_item_unless_exist => 0); |
655 |
|
if ($to) { |
656 |
|
my $fbody = $to->addr_spec (-format => $option{format}); |
657 |
|
return &{$option{output_folding}} ($self, $fbody); |
658 |
} |
} |
659 |
|
''; |
660 |
|
} else { |
661 |
|
$self->scan ($_stringify); |
662 |
|
my $ret = join ('&', @ret); |
663 |
|
$ret; |
664 |
} |
} |
|
''; |
|
|
} elsif ($option{format} =~ /uri-url-mailto/) { |
|
|
$self->scan ($_stringify); |
|
|
my $ret = join ('&', @ret); |
|
|
$ret; |
|
665 |
} else { |
} else { |
666 |
if ($option{mail_from}) { |
if ($option{output_mail_from}) { |
667 |
my $fromline = $self->field ('mail-from'); |
my $fromline = $self->field ('mail-from', -new_item_unless_exist => 0); |
668 |
push @ret, 'From '.$fromline if $fromline; |
push @ret, 'From '.$fromline if $fromline; |
669 |
} |
} |
670 |
$self->scan ($_stringify); |
$self->scan ($_stringify); |
671 |
my $ret = join ("\n", @ret); |
my $ret = join ("\x0D\x0A", @ret); |
672 |
$ret? $ret."\n": ''; |
$ret? $ret."\x0D\x0A": ''; |
673 |
} |
} |
674 |
} |
} |
675 |
*as_string = \&stringify; |
*as_string = \&stringify; |
686 |
return $self->{option}->{ shift (@_) }; |
return $self->{option}->{ shift (@_) }; |
687 |
} |
} |
688 |
while (my ($name, $value) = splice (@_, 0, 2)) { |
while (my ($name, $value) = splice (@_, 0, 2)) { |
|
$name =~ s/^-//; |
|
689 |
$self->{option}->{$name} = $value; |
$self->{option}->{$name} = $value; |
690 |
if ($name eq 'format') { |
if ($name eq 'format') { |
691 |
for my $f (@{$self->{field}}) { |
for my $f (@{$self->{field}}) { |
697 |
} |
} |
698 |
} |
} |
699 |
|
|
700 |
sub field_type ($$;$) { |
sub field_type ($@) {shift->SUPER::value_type (@_)} |
701 |
|
|
702 |
|
## $self->_fold ($string, %option = (-max, -initial_length(for field-name)) ) |
703 |
|
sub _fold ($$;%) { |
704 |
my $self = shift; |
my $self = shift; |
705 |
my $field_name = shift; |
my $string = shift; |
706 |
my $new_field_type = shift; |
my %option = @_; |
707 |
if ($new_field_type) { |
my $max = $self->{option}->{line_length_max}; |
708 |
$self->{option}->{field_type}->{$field_name} = $new_field_type; |
$max = 20 if $max < 20; |
709 |
} |
|
710 |
$self->{option}->{field_type}->{$field_name} |
my $l = $option{-initial_length} || 0; |
711 |
|| $self->{option}->{field_type}->{':DEFAULT'}; |
$string =~ s{([\x09\x20][^\x09\x20]+)}{ |
712 |
|
my $s = $1; |
713 |
|
if ($l + length $s > $max) { |
714 |
|
$s = "\x0D\x0A\x20" . $s; |
715 |
|
$l = length ($s) - 2; |
716 |
|
} else { $l += length $s } |
717 |
|
$s; |
718 |
|
}gex; |
719 |
|
$string; |
720 |
} |
} |
721 |
|
|
722 |
sub _delete_empty_field ($) { |
sub _ns_load_ph ($$) { |
723 |
my $self = shift; |
my $self = shift; |
724 |
my @ret; |
my $name = shift; ## normalized prefix (without HYPHEN-MINUS) |
725 |
for my $field (@{$self->{field}}) { |
return if $self->{ns}->{phname2uri}->{$name}; |
726 |
push @ret, $field if $field->{name}; |
$self->{ns}->{phname2uri}->{$name} = $NS_phname2uri{$name}; |
727 |
} |
return unless $self->{ns}->{phname2uri}->{$name}; |
728 |
$self->{field} = \@ret; |
$self->{ns}->{uri2phname}->{$self->{ns}->{phname2uri}->{$name}} = $name; |
|
$self; |
|
729 |
} |
} |
730 |
|
|
731 |
sub _fold ($$;$) { |
sub _NS_uri2phpackage ($) { |
732 |
my $self = shift; |
$NS_uri2phpackage{$_[0]} |
733 |
my $string = shift; |
|| $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}}; |
|
my $len = shift || $self->{option}->{fold_length}; |
|
|
$len = 60 if $len < 60; |
|
|
|
|
|
## This code is taken from Mail::Header 1.43 in MailTools, |
|
|
## by Graham Barr (Maintained by Mark Overmeer <mailtools@overmeer.net>). |
|
|
my $max = int($len - 5); # 4 for leading spcs + 1 for [\,\;] |
|
|
my $min = int($len * 4 / 5) - 4; |
|
|
my $ml = $len; |
|
|
|
|
|
if (length($string) > $ml) { |
|
|
#Split the line up |
|
|
# first bias towards splitting at a , or a ; >4/5 along the line |
|
|
# next split a whitespace |
|
|
# 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; |
|
|
} |
|
|
$string; |
|
734 |
} |
} |
735 |
|
|
736 |
=head2 $self->clone () |
=head2 $self->clone () |
739 |
|
|
740 |
=cut |
=cut |
741 |
|
|
742 |
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; |
|
|
} |
|
743 |
|
|
744 |
=head1 NOTE |
=head1 NOTE |
745 |
|
|