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_capitalize => 1, |
38 |
|
-field_name_case_sensible => 0, |
39 |
Outputs "From " line (known as Un*x From, Mail-From, and so on) |
-field_name_unsafe_rule => 'NON_ftext', |
40 |
when C<stringify>. (Default = 0) |
-field_name_validation => 1, ## Method level option. |
41 |
|
-field_sort => 0, |
42 |
=cut |
#-format => 'mail-rfc2822', |
43 |
|
-linebreak_strict => 0, ## Not implemented completely |
44 |
=head1 CONSTRUCTORS |
-line_length_max => 60, ## For folding |
45 |
|
-ns_default_uri => $Message::Header::Default::OPTION{namespace_uri}, |
46 |
The following methods construct new C<Message::Header> objects: |
-output_bcc => 0, |
47 |
|
-output_folding => 1, |
48 |
=over 4 |
-output_mail_from => 0, |
49 |
|
#-parse_all => 0, |
50 |
=cut |
-translate_underscore => 1, |
51 |
|
#-uri_mailto_safe |
52 |
## Initialize |
-uri_mailto_safe_level => 4, |
53 |
my %DEFAULT = ( |
-use_folding => 1, |
54 |
capitalize => 1, |
#-value_type |
55 |
fold => 1, |
); |
56 |
fold_length => 70, |
$DEFAULT{-uri_mailto_safe} = { |
|
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 => { |
|
57 |
## 1 all (no check) 2 no trace & bcc & from |
## 1 all (no check) 2 no trace & bcc & from |
58 |
## 3 no sender's info 4 (default) (currently not used) |
## 3 no sender's info 4 (default) (currently not used) |
59 |
## 5 only a few |
## 5 only a few |
60 |
':default' => 4, |
':default' => 4, |
61 |
'cc' => 4, |
'cc' => 5, |
62 |
'bcc' => 1, |
'bcc' => 1, |
63 |
'body' => 1, |
'body' => 1, |
64 |
'comment' => 5, |
'comment' => 5, |
78 |
'sender' => 1, |
'sender' => 1, |
79 |
'subject' => 5, |
'subject' => 5, |
80 |
'summary' => 5, |
'summary' => 5, |
81 |
'to' => 4, |
'to' => 5, |
82 |
'user-agent' => 3, |
'user-agent' => 3, |
83 |
'x-face' => 2, |
'x-face' => 2, |
84 |
'x-mailer' => 3, |
'x-mailer' => 3, |
85 |
'x-nsubject' => 5, |
'x-nsubject' => 5, |
86 |
'x-received' => 1, |
'x-received' => 1, |
87 |
'x400-received' => 1, |
'x400-received' => 1, |
88 |
}, |
}; |
89 |
uri_mailto_safe_level => 4, |
|
90 |
validate => 1, |
$DEFAULT{-value_type} = { |
91 |
); |
':default' => ['Message::Field::Unstructured'], |
|
$DEFAULT{field_type} = { |
|
|
':DEFAULT' => 'Message::Field::Unstructured', |
|
92 |
|
|
93 |
received => 'Message::Field::Received', |
received => ['Message::Field::Received'], |
94 |
'x-received' => 'Message::Field::Received', |
'x-received' => ['Message::Field::Received'], |
95 |
|
|
96 |
'content-type' => 'Message::Field::ContentType', |
p3p => ['Message::Field::Params'], |
97 |
p3p => 'Message::Field::Params', |
'auto-submitted' => ['Message::Field::ValueParams'], |
98 |
'auto-submitted' => 'Message::Field::ValueParams', |
link => ['Message::Field::ValueParams'], |
99 |
'content-disposition' => 'Message::Field::ValueParams', |
archive => ['Message::Field::ValueParams'], |
100 |
link => 'Message::Field::ValueParams', |
'x-face-type' => ['Message::Field::ValueParams'], |
101 |
archive => 'Message::Field::ValueParams', |
'x-mozilla-draft-info' => ['Message::Field::ValueParams'], |
|
'x-face-type' => 'Message::Field::ValueParams', |
|
|
'x-mozilla-draft-info' => 'Message::Field::ValueParams', |
|
102 |
|
|
103 |
subject => 'Message::Field::Subject', |
subject => ['Message::Field::Subject'], |
104 |
'x-nsubject' => 'Message::Field::Subject', |
'x-nsubject' => ['Message::Field::Subject'], |
105 |
|
|
106 |
'list-software' => 'Message::Field::UA', |
'list-software' => ['Message::Field::UA'], |
107 |
'user-agent' => 'Message::Field::UA', |
'user-agent' => ['Message::Field::UA'], |
108 |
'resent-user-agent' => 'Message::Field::UA', |
'resent-user-agent' => ['Message::Field::UA'], |
109 |
server => 'Message::Field::UA', |
server => ['Message::Field::UA'], |
110 |
|
|
111 |
## A message id |
## A message id |
112 |
'content-id' => 'Message::Field::MsgID', |
'message-id' => ['Message::Field::MsgID'], |
113 |
'message-id' => 'Message::Field::MsgID', |
'resent-message-id' => ['Message::Field::MsgID'], |
|
'resent-message-id' => 'Message::Field::MsgID', |
|
114 |
|
|
115 |
## Numeric value |
## Numeric value |
116 |
'content-length' => 'Message::Field::Numval', |
lines => ['Message::Field::Numval'], |
117 |
lines => 'Message::Field::Numval', |
'max-forwards' => ['Message::Field::Numval'], |
118 |
'max-forwards' => 'Message::Field::Numval', |
'mime-version' => ['Message::Field::Numval'], |
119 |
'mime-version' => 'Message::Field::Numval', |
'x-jsmail-priority' => ['Message::Field::Numval'], |
120 |
'x-jsmail-priority' => 'Message::Field::Numval', |
'x-mail-count' => ['Message::Field::Numval'], |
121 |
'x-mail-count' => 'Message::Field::Numval', |
'x-ml-count' => ['Message::Field::Numval'], |
122 |
'x-ml-count' => 'Message::Field::Numval', |
'x-priority' => ['Message::Field::Numval'], |
|
'x-priority' => 'Message::Field::Numval', |
|
123 |
|
|
124 |
path => 'Message::Field::Path', |
path => ['Message::Field::Path'], |
125 |
}; |
}; |
126 |
for (qw(archive cancel-lock content-features content-md5 |
for (qw(archive cancel-lock |
127 |
disposition-notification-options encoding |
disposition-notification-options encoding |
128 |
importance injector-info |
importance injector-info |
129 |
pics-label posted-and-mailed precedence list-id message-type |
pics-label posted-and-mailed precedence list-id message-type |
130 |
original-recipient priority x-list-id |
original-recipient priority x-list-id |
131 |
sensitivity status x-face x-msmail-priority xref)) |
sensitivity status x-face x-msmail-priority xref)) |
132 |
{$DEFAULT{field_type}->{$_} = 'Message::Field::Structured'} |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::Structured']} |
133 |
## Not supported yet, but to be supported... |
## Not supported yet, but to be supported... |
134 |
# x-list: unstructured, ml name |
# x-list: unstructured, ml name |
135 |
for (qw(abuse-reports-to apparently-to approved approved-by bcc cc complaints-to |
for (qw(abuse-reports-to apparently-to approved approved-by bcc cc complaints-to |
143 |
x-complaints-to x-envelope-from x-envelope-sender |
x-complaints-to x-envelope-from x-envelope-sender |
144 |
x-envelope-to x-ml-address x-ml-command x-ml-to x-nfrom x-nto |
x-envelope-to x-ml-address x-ml-command x-ml-to x-nfrom x-nto |
145 |
x-rcpt-to x-sender x-x-sender)) |
x-rcpt-to x-sender x-x-sender)) |
146 |
{$DEFAULT{field_type}->{$_} = 'Message::Field::Addresses'} |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::Addresses']} |
147 |
for (qw(client-date date date-received delivery-date expires |
for (qw(client-date date date-received delivery-date expires |
148 |
expire-date nntp-posting-date posted posted-date received-date |
expire-date nntp-posting-date posted posted-date received-date |
149 |
reply-by resent-date |
reply-by resent-date |
150 |
x-originalarrivaltime x-tcup-date)) |
x-originalarrivaltime x-tcup-date)) |
151 |
{$DEFAULT{field_type}->{$_} = 'Message::Field::Date'} |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::Date']} |
152 |
for (qw(article-updates in-reply-to |
for (qw(article-updates in-reply-to |
153 |
obsoletes references replaces see-also supersedes)) |
obsoletes references replaces see-also supersedes)) |
154 |
{$DEFAULT{field_type}->{$_} = 'Message::Field::MsgIDs'} |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::MsgIDs']} |
155 |
for (qw(accept accept-charset accept-encoding accept-language |
for (qw(accept accept-charset accept-encoding accept-language |
156 |
content-language |
content-language |
157 |
content-transfer-encoding encrypted followup-to keywords |
encrypted followup-to keywords |
158 |
list-archive list-digest list-help list-owner |
list-archive list-digest list-help list-owner |
159 |
list-post list-subscribe list-unsubscribe list-url uri newsgroups |
list-post list-subscribe list-unsubscribe list-url uri newsgroups |
160 |
posted-to)) |
posted-to)) |
161 |
{$DEFAULT{field_type}->{$_} = 'Message::Field::CSV'} |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::CSV']} |
162 |
for (qw(x-brother x-boss x-classmate x-daughter x-dearfriend x-favoritesong |
for (qw(x-brother x-boss x-classmate x-daughter x-dearfriend x-favoritesong |
163 |
x-friend x-me |
x-friend x-me |
164 |
x-moe x-respect |
x-moe x-respect |
165 |
x-sublimate x-son x-sister x-wife)) |
x-sublimate x-son x-sister x-wife)) |
166 |
{$DEFAULT{field_type}->{$_} = 'Message::Field::CSV'} ## NOT M::F::XMOE! |
{$DEFAULT{-value_type}->{$_} =[ 'Message::Field::CSV']} ## NOT M::F::XMOE! |
167 |
for (qw(content-alias content-base content-location location referer |
for (qw(location referer url x-home-page x-http_referer |
|
url x-home-page x-http_referer |
|
168 |
x-info x-pgp-key x-ml-url x-uri x-url x-web)) |
x-info x-pgp-key x-ml-url x-uri x-url x-web)) |
169 |
{$DEFAULT{field_type}->{$_} = 'Message::Field::URI'} |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::URI']} |
170 |
|
|
171 |
my %header_goodcase = ( |
my %header_goodcase = ( |
172 |
'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', |
|
173 |
etag => 'ETag', |
etag => 'ETag', |
|
fax => 'FAX', |
|
174 |
'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', |
|
175 |
te => 'TE', |
te => 'TE', |
176 |
url => 'URL', |
url => 'URL', |
177 |
'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', |
|
178 |
); |
); |
179 |
$DEFAULT{capitalize} = sub { |
$DEFAULT{-field_name_capitalize} = sub { |
180 |
my $self = shift; |
my $self = shift; |
181 |
my $name = shift; |
my $name = shift; |
182 |
if ($header_goodcase{$name}) { |
if ($header_goodcase{$name}) { |
217 |
); |
); |
218 |
my %header_order; |
my %header_order; |
219 |
|
|
220 |
|
=head1 CONSTRUCTORS |
221 |
|
|
222 |
|
The following methods construct new C<Message::Header> objects: |
223 |
|
|
224 |
|
=over 4 |
225 |
|
|
226 |
|
=cut |
227 |
|
|
228 |
sub _init ($;%) { |
sub _init ($;%) { |
229 |
my $self = shift; |
my $self = shift; |
230 |
my %options = @_; |
my %options = @_; |
231 |
$self->{field} = []; |
my $DEFAULT = Message::Util::make_clone (\%DEFAULT); |
232 |
$self->{option} = \%DEFAULT; |
$self->SUPER::_init (%$DEFAULT, %options); |
233 |
|
$self->{value} = []; |
234 |
|
$self->_ns_load_ph ('default'); |
235 |
|
$self->{ns}->{default_phuri} = $self->{ns}->{phname2uri}->{'default'}; |
236 |
|
$self->_ns_load_ph ('rfc822'); |
237 |
|
$self->{ns}->{default_phuri} = $self->{ns}->{phname2uri}->{'rfc822'}; |
238 |
|
|
239 |
my @new_fields = (); |
my @new_fields = (); |
240 |
for my $name (keys %options) { |
for my $name (keys %options) { |
241 |
if (substr ($name, 0, 1) eq '-') { |
unless (substr ($name, 0, 1) eq '-') { |
|
$self->{option}->{substr ($name, 1)} = $options{$name}; |
|
|
} else { |
|
242 |
push @new_fields, ($name => $options{$name}); |
push @new_fields, ($name => $options{$name}); |
243 |
} |
} |
244 |
} |
} |
264 |
$header_goodcase{'resent-cc'} = 'Resent-cc'; |
$header_goodcase{'resent-cc'} = 'Resent-cc'; |
265 |
} elsif ($format =~ /cgi/) { |
} elsif ($format =~ /cgi/) { |
266 |
unshift @header_order, qw(content-type location); |
unshift @header_order, qw(content-type location); |
267 |
$option->{sort} = 'good-practice'; |
$option->{field_sort} = 'good-practice'; |
268 |
$option->{fold} = 0; |
$option->{use_folding} = 0; |
269 |
} elsif ($format =~ /http/) { |
} elsif ($format =~ /http/) { |
270 |
$option->{sort} = 'good-practice'; |
$option->{field_sort} = 'good-practice'; |
271 |
} |
} |
272 |
if ($format =~ /uri-url-mailto/) { |
if ($format =~ /uri-url-mailto/) { |
273 |
$option->{output_bcc} = 0; |
$option->{output_bcc} = 0; |
274 |
$option->{capitalize} = 0; |
$option->{field_name_capitalize} = 0; |
275 |
$option->{field_format_pattern} = '%s=%s'; |
$option->{field_format_pattern} = '%s=%s'; |
276 |
$option->{fold} = sub { |
$option->{output_folding} = sub { |
277 |
$_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge; |
$_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge; |
278 |
$_[1]; |
$_[1]; |
279 |
}; |
}; ## Yes, this is not folding! |
280 |
} |
} |
281 |
} |
} |
282 |
|
|
283 |
=item Message::Header->new ([%initial-fields/options]) |
=item $msg = Message::Header->new ([%initial-fields/options]) |
284 |
|
|
285 |
Constructs a new C<Message::Headers> object. You might pass some initial |
Constructs a new C<Message::Headers> object. You might pass some initial |
286 |
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. |
296 |
|
|
297 |
=cut |
=cut |
298 |
|
|
299 |
sub new ($;%) { |
## Inherited |
|
my $class = shift; |
|
|
my $self = bless {}, $class; |
|
|
$self->_init (@_); |
|
|
$self; |
|
|
} |
|
300 |
|
|
301 |
=item Message::Header->parse ($header, [%initial-fields/options]) |
=item $msg = Message::Header->parse ($header, [%initial-fields/options]) |
302 |
|
|
303 |
Parses given C<header> and constructs a new C<Message::Headers> |
Parses given C<header> and constructs a new C<Message::Headers> |
304 |
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 |
311 |
my $header = shift; |
my $header = shift; |
312 |
my $self = bless {}, $class; |
my $self = bless {}, $class; |
313 |
$self->_init (@_); ## BUG: don't check linebreak_strict |
$self->_init (@_); ## BUG: don't check linebreak_strict |
314 |
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos; ## unfold |
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding}; |
315 |
for my $field (split /\x0D?\x0A/, $header) { |
for my $field (split /\x0D?\x0A/, $header) { |
316 |
if ($field =~ /$REG{M_fromline}/) { |
if ($field =~ /$REG{M_fromline}/) { |
317 |
my $body = $1; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
318 |
$body = $self->_field_body ($body, 'mail-from') |
('mail-from' => $1, $self->{option}); |
319 |
if $self->{option}->{parse_all}; |
push @{$self->{value}}, $value if $s; |
|
push @{$self->{field}}, {name => 'mail-from', body => $body}; |
|
320 |
} elsif ($field =~ /$REG{M_field}/) { |
} elsif ($field =~ /$REG{M_field}/) { |
321 |
my ($name, $body) = (lc $1, $2); |
my ($name, $body) = ($1, $2); |
|
$name =~ s/$REG{WSP}+$//; |
|
322 |
$body =~ s/$REG{WSP}+$//; |
$body =~ s/$REG{WSP}+$//; |
323 |
$body = $self->_field_body ($body, $name) if $self->{option}->{parse_all}; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
324 |
push @{$self->{field}}, {name => $name, body => $body}; |
($name => $body, $self->{option}); |
325 |
|
push @{$self->{value}}, $value if $s; |
326 |
} |
} |
327 |
} |
} |
328 |
$self; |
$self; |
329 |
} |
} |
330 |
|
|
331 |
=item Message::Header->parse_array (\@header, [%initial-fields/options]) |
=item $msg = Message::Header->parse_array (\@header, [%initial-fields/options]) |
332 |
|
|
333 |
Parses given C<header> and constructs a new C<Message::Headers> |
Parses given C<header> and constructs a new C<Message::Headers> |
334 |
object. Same as C<Message::Header-E<lt>parse> but this method |
object. Same as C<Message::Header-E<lt>parse> but this method |
347 |
$self->_init (@_); |
$self->_init (@_); |
348 |
while (1) { |
while (1) { |
349 |
my $field = shift @$header; |
my $field = shift @$header; |
350 |
while (1) { |
if ($self->{option}->{use_folding}) { |
351 |
if ($$header[0] =~ /^$REG{WSP}/) { |
while (1) { |
352 |
$field .= shift @$header; |
if ($$header[0] =~ /^$REG{WSP}/) { |
353 |
} else {last} |
$field .= shift @$header; |
354 |
|
} else {last} |
355 |
|
} |
356 |
} |
} |
357 |
if ($self->{option}->{linebreak_strict}) { |
if ($self->{option}->{linebreak_strict}) { |
358 |
$field =~ s/\x0D\x0A//g; |
$field =~ s/\x0D\x0A//g; |
359 |
} else { |
} else { |
360 |
$field =~ tr/\x0D\x0A//d; |
$field =~ tr/\x0D\x0A//d; |
361 |
} |
} |
362 |
|
local $self->{option}->{parse} = $self->{option}->{parse_all}; |
363 |
if ($field =~ /$REG{M_fromline}/) { |
if ($field =~ /$REG{M_fromline}/) { |
364 |
my $body = $1; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
365 |
$body = $self->_field_body ($body, 'mail-from') |
('mail-from' => $1, $self->{option}); |
366 |
if $self->{option}->{parse_all}; |
push @{$self->{value}}, $value if $s; |
|
push @{$self->{field}}, {name => 'mail-from', body => $body}; |
|
367 |
} elsif ($field =~ /$REG{M_field}/) { |
} elsif ($field =~ /$REG{M_field}/) { |
368 |
my ($name, $body) = (lc $1, $2); |
my ($name, $body) = ($self->_n11n_field_name ($1), $2); |
|
$name =~ s/$REG{WSP}+$//; |
|
369 |
$body =~ s/$REG{WSP}+$//; |
$body =~ s/$REG{WSP}+$//; |
370 |
$body = $self->_field_body ($body, $name) if $self->{option}->{parse_all}; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
371 |
push @{$self->{field}}, {name => $name, body => $body}; |
($name => $body, $self->{option}); |
372 |
|
push @{$self->{value}}, $value if $s; |
373 |
} |
} |
374 |
last if $#$header < 0; |
last if $#$header < 0; |
375 |
} |
} |
389 |
|
|
390 |
=cut |
=cut |
391 |
|
|
392 |
sub field ($$) { |
sub field ($@) {shift->SUPER::item (@_)} |
393 |
|
sub field_exist ($@) {shift->SUPER::item_exist (@_)} |
394 |
|
|
395 |
|
## item-by?, \$checked-item, {item-key => 1}, \%option |
396 |
|
sub _item_match ($$\$\%\%) { |
397 |
my $self = shift; |
my $self = shift; |
398 |
my $name = lc shift; |
my ($by, $i, $list, $option) = @_; |
399 |
my @ret; |
return 0 unless ref $$i; ## Already removed |
400 |
for my $field (@{$self->{field}}) { |
if ($by eq 'name') { |
401 |
if ($field->{name} eq $name) { |
my %o = %$option; $o{parse} = 0; |
402 |
unless (wantarray) { |
my %l; |
403 |
$field->{body} = $self->_field_body ($field->{body}, $name); |
for (keys %$list) { |
404 |
return $field->{body}; |
my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', %o); |
405 |
|
if ($s) { |
406 |
|
$l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1; |
407 |
} else { |
} else { |
408 |
$field->{body} = $self->_field_body ($field->{body}, $name); |
$l{$v->{name} .':'. ( $option->{ns} || $self->{ns}->{default_phuri} ) } = 1; |
|
push @ret, $field->{body}; |
|
409 |
} |
} |
410 |
} |
} |
411 |
|
return 1 if $l{$$i->{name} . ':' . $$i->{ns}}; |
412 |
} |
} |
413 |
if ($#ret < 0) { |
0; |
|
return $self->add ($name); |
|
|
} |
|
|
@ret; |
|
414 |
} |
} |
415 |
|
*_delete_match = \&_item_match; |
416 |
|
|
417 |
sub field_exist ($$) { |
## Returns returned item value \$item-value, \%option |
418 |
my $self = shift; |
sub _item_return_value ($\$\%) { |
419 |
my $name = lc shift; |
if (ref ${$_[1]}->{body}) { |
420 |
my @ret; |
${$_[1]}->{body}; |
421 |
for my $field (@{$self->{field}}) { |
} else { |
422 |
return 1 if ($field->{name} eq $name); |
${$_[1]}->{body} = $_[0]->_parse_value (${$_[1]}->{name} => ${$_[1]}->{body}, |
423 |
|
ns => ${$_[1]}->{ns}); |
424 |
|
${$_[1]}->{body}; |
425 |
} |
} |
|
0; |
|
426 |
} |
} |
427 |
|
|
428 |
=head2 $self->field_name ($index) |
## Returns returned (new created) item value $name, \%option |
429 |
|
sub _item_new_value ($$\%) { |
430 |
Returns C<field-name> of $index'th C<field>. |
my ($s,undef,$value) = $_[0]->_value_to_arrayitem |
431 |
|
($_[1] => '', $_[2]); |
432 |
=head2 $self->field_body ($index) |
$s? $value: undef; |
433 |
|
} |
|
Returns C<field-body> of $index'th C<field>. |
|
434 |
|
|
|
=cut |
|
435 |
|
|
|
sub field_name ($$) { |
|
|
my $self = shift; |
|
|
$self->{field}->[shift]->{name}; |
|
|
} |
|
|
sub field_body ($$) { |
|
|
my $self = shift; |
|
|
my $i = shift; |
|
|
$self->{field}->[$i]->{body} |
|
|
= $self->_field_body ($self->{field}->[$i]->{body}, $self->{field}->[$i]->{name}); |
|
|
$self->{field}->[$i]->{body}; |
|
|
} |
|
436 |
|
|
437 |
sub _field_body ($$$) { |
## $self->_parse_value ($type, $value, %options); |
438 |
my $self = shift; |
sub _parse_value ($$$;%) { |
439 |
my ($body, $name) = @_; |
my $self = shift; |
440 |
unless (ref $body) { |
my $name = shift ;#|| $self->{option}->{_VALTYPE_DEFAULT}; |
441 |
my $type = $self->{option}->{field_type}->{$name} |
my $value = shift; return $value if ref $value; |
442 |
|| $self->{option}->{field_type}->{':DEFAULT'}; |
my %option = @_; |
443 |
eval "require $type" or Carp::croak ("_field_body: $type: $@"); |
my $vtype; { no strict 'refs'; |
444 |
unless ($body) { |
$vtype = ${&_NS_uri2phpackage ($option{ns}).'::OPTION'}{value_type}; |
445 |
$body = $type->new (-field_name => $name, |
if (ref $vtype) { $vtype = $vtype->{$name} } |
446 |
-format => $self->{option}->{format}, |
unless (ref $vtype) { $vtype = $vtype->{$self->{option}->{_VALTYPE_DEFAULT}} } |
447 |
-parse_all => $self->{option}->{parse_all}); |
## For compatiblity. |
448 |
} else { |
unless (ref $vtype) { $vtype = $self->{option}->{value_type}->{$name} |
449 |
$body = $type->parse ($body, -field_name => $name, |
|| $self->{option}->{value_type}->{$self->{option}->{_VALTYPE_DEFAULT}} } |
450 |
-format => $self->{option}->{format}, |
} |
451 |
-parse_all => $self->{option}->{parse_all}); |
my $vpackage = $vtype->[0]; |
452 |
} |
my %vopt = %{$vtype->[1]} if ref $vtype->[1]; |
453 |
|
if ($vpackage eq ':none:') { |
454 |
|
return $value; |
455 |
|
} elsif (defined $value) { |
456 |
|
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
457 |
|
return $vpackage->parse ($value, |
458 |
|
-format => $self->{option}->{format}, |
459 |
|
-field_name => $name, |
460 |
|
-parse_all => $self->{option}->{parse_all}, |
461 |
|
%vopt); |
462 |
|
} else { |
463 |
|
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
464 |
|
return $vpackage->new ( |
465 |
|
-format => $self->{option}->{format}, |
466 |
|
-field_name => $name, |
467 |
|
-parse_all => $self->{option}->{parse_all}, |
468 |
|
%vopt); |
469 |
} |
} |
|
$body; |
|
470 |
} |
} |
471 |
|
|
472 |
=head2 $self->field_name_list () |
=head2 $self->field_name_list () |
479 |
|
|
480 |
sub field_name_list ($) { |
sub field_name_list ($) { |
481 |
my $self = shift; |
my $self = shift; |
482 |
$self->_delete_empty_field (); |
$self->_delete_empty (); |
483 |
map {$_->{name}} @{$self->{field}}; |
map { $_->{name} . ':' . $_->{ns} } @{$self->{value}}; |
484 |
|
} |
485 |
|
|
486 |
|
sub namespace_ph_default ($;$) { |
487 |
|
my $self = shift; |
488 |
|
if (defined $_[0]) { |
489 |
|
no strict 'refs'; |
490 |
|
$self->{ns}->{default_phuri} = $_[0]; |
491 |
|
$self->_ns_load_ph (${&_NS_uri2phpackage ($self->{ns}->{default_phuri}).'::OPTION'}{namespace_phname}); |
492 |
|
} |
493 |
|
$self->{ns}->{default_phuri}; |
494 |
} |
} |
495 |
|
|
496 |
=item $hdr->add ($field-name, $field-body, [$name, $body, ...]) |
=item $hdr->add ($field-name, $field-body, [$name, $body, ...]) |
516 |
|
|
517 |
=cut |
=cut |
518 |
|
|
519 |
sub add ($%) { |
## [Name: Value] pair -> internal array item |
520 |
my $self = shift; |
## $self->_value_to_arrayitem ($name => $value, {%options}) |
521 |
my %fields = @_; |
## or |
522 |
my %option = %{$self->{option}}; |
## $self->_value_to_arrayitem ($name => [$value, %value_options], {%options}) |
523 |
$option{parse} = 1 if defined wantarray; |
## |
524 |
for (grep {/^-/} keys %fields) {$option{substr ($_, 1)} = $fields{$_}} |
## Return: ((1 = success / 0 = failue), $full_name, $arrayitem) |
525 |
my $body; |
sub _value_to_arrayitem ($$$\%) { |
526 |
for (grep {/^[^-]/} keys %fields) { |
my $self = shift; |
527 |
my $name = lc $_; $body = $fields{$_}; |
my ($name, $value, $option) = @_; |
528 |
$name =~ tr/_/-/ if $option{translate_underscore}; |
my $value_option = {}; |
529 |
Carp::croak "add: $name: invalid field-name" |
if (ref $value eq 'ARRAY') { |
530 |
if $option{validate} && $name =~ /$REG{UNSAFE_field_name}/; |
($value, %$value_option) = @$value; |
531 |
$body = $self->_field_body ($body, $name) if $option{parse}; |
} |
532 |
if ($option{prepend}) { |
my $nsuri = $self->{ns}->{default_phuri}; |
533 |
unshift @{$self->{field}}, {name => $name, body => $body}; |
no strict 'refs'; |
534 |
} else { |
if ($option->{ns}) { |
535 |
push @{$self->{field}}, {name => $name, body => $body}; |
$nsuri = $option->{ns}; |
536 |
} |
} elsif ($name =~ s/^([Xx]-[A-Za-z]+|[A-Za-z]+)-//) { |
537 |
} |
my $oprefix = $1; |
538 |
$body if $option{parse}; |
my $prefix |
539 |
|
= &{${&_NS_uri2phpackage ($nsuri).'::OPTION'}{n11n_prefix}} |
540 |
|
($self, &_NS_uri2phpackage ($nsuri), $oprefix); |
541 |
|
$self->_ns_load_ph ($prefix); |
542 |
|
$nsuri = $self->{ns}->{phname2uri}->{$prefix}; |
543 |
|
unless ($nsuri) { |
544 |
|
$name = $oprefix . '-' . $name; |
545 |
|
$nsuri = $self->{ns}->{default_phuri}; |
546 |
|
} |
547 |
|
} |
548 |
|
$name |
549 |
|
= &{${&_NS_uri2phpackage ($nsuri).'::OPTION'}{n11n_name}} |
550 |
|
($self, &_NS_uri2phpackage ($nsuri), $name); |
551 |
|
Carp::croak "$name: invalid field-name" |
552 |
|
if $option->{field_name_validation} |
553 |
|
&& $name =~ /$REG{$option->{field_name_unsafe_rule}}/; |
554 |
|
$value = $self->_parse_value ($name => $value, ns => $nsuri) if $$option{parse}; |
555 |
|
$$option{parse} = 0; |
556 |
|
(1, $name.':'.$nsuri => {name => $name, body => $value, ns => $nsuri}); |
557 |
} |
} |
558 |
|
*_add_hash_check = \&_value_to_arrayitem; |
559 |
|
*_replace_hash_check = \&_value_to_arrayitem; |
560 |
|
|
561 |
=head2 $self->relace ($field_name, $field_body) |
=head2 $self->relace ($field_name, $field_body) |
562 |
|
|
568 |
|
|
569 |
=cut |
=cut |
570 |
|
|
571 |
sub replace ($%) { |
sub _replace_hash_shift ($\%$\%) { |
572 |
my $self = shift; |
shift; my $r = shift; my $n = $_[0]->{name}; |
573 |
my %params = @_; |
if ($$r{$n}) { |
574 |
my %option = %{$self->{option}}; |
my $d = $$r{$n}; |
575 |
$option{parse} = defined wantarray unless defined $option{parse}; |
delete $$r{$n}; |
576 |
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; |
|
|
} |
|
577 |
} |
} |
578 |
for (keys %new_field) { |
undef; |
|
push @{$self->{field}}, {name => $_, body => $new_field{$_}}; |
|
|
} |
|
|
$body if $option{parse}; |
|
579 |
} |
} |
580 |
|
|
581 |
=head2 $self->delete ($field-name, [$name, ...]) |
=head2 $self->delete ($field-name, [$name, ...]) |
584 |
|
|
585 |
=cut |
=cut |
586 |
|
|
|
sub delete ($@) { |
|
|
my $self = shift; |
|
|
my %delete; for (@_) {$delete{lc $_} = 1} |
|
|
for my $field (@{$self->{field}}) { |
|
|
undef $field if $delete{$field->{name}}; |
|
|
} |
|
|
} |
|
587 |
|
|
588 |
=head2 $self->count ([$field_name]) |
=head2 $self->count ([$field_name]) |
589 |
|
|
593 |
|
|
594 |
=cut |
=cut |
595 |
|
|
596 |
sub count ($;$) { |
sub _count_by_name ($$\%) { |
597 |
my $self = shift; |
my $self = shift; |
598 |
my ($name) = (lc shift); |
my ($array, $option) = @_; |
599 |
unless ($name) { |
my $name = $self->_n11n_field_name ($$option{-name}); |
600 |
$self->_delete_empty_field (); |
my @a = grep {$_->{name} eq $name} @{$self->{$array}}; |
601 |
return $#{$self->{field}}+1; |
$#a + 1; |
602 |
} |
} |
603 |
my $count = 0; |
|
604 |
for my $field (@{$self->{field}}) { |
## Delete empty items |
605 |
if ($field->{name} eq $name) { |
sub _delete_empty ($) { |
606 |
$count++; |
my $self = shift; |
607 |
} |
my $array = $self->{option}->{_HASH_NAME}; |
608 |
} |
$self->{$array} = [grep {ref $_ && length $_->{name}} @{$self->{$array}}]; |
|
$count; |
|
609 |
} |
} |
610 |
|
|
611 |
=head2 $self->rename ($field-name, $new-name, [$old, $new,...]) |
=head2 $self->rename ($field-name, $new-name, [$old, $new,...]) |
621 |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
622 |
my %new_name; |
my %new_name; |
623 |
for (grep {/^[^-]/} keys %params) { |
for (grep {/^[^-]/} keys %params) { |
624 |
my ($old => $new) = (lc $_ => lc $params{$_}); |
my ($old => $new) |
625 |
|
= ($self->_n11n_field_name ($_) => $self->_n11n_field_name ($params{$_})); |
626 |
|
$old =~ tr/_/-/ if $option{translate_underscore}; |
627 |
$new =~ tr/_/-/ if $option{translate_underscore}; |
$new =~ tr/_/-/ if $option{translate_underscore}; |
628 |
Carp::croak "rename: $new: invalid field-name" |
Carp::croak "rename: $new: invalid field-name" |
629 |
if $option{validate} && $new =~ /$REG{UNSAFE_field_name}/; |
if $option{field_name_validation} |
630 |
|
&& $new =~ /$REG{$option{field_name_unsafe_rule}}/; |
631 |
$new_name{$old} = $new; |
$new_name{$old} = $new; |
632 |
} |
} |
633 |
for my $field (@{$self->{field}}) { |
for my $field (@{$self->{value}}) { |
634 |
if (length $new_name{$field->{name}}) { |
if (length $new_name{$field->{name}}) { |
635 |
$field->{name} = $new_name{$field->{name}}; |
$field->{name} = $new_name{$field->{name}}; |
636 |
} |
} |
648 |
|
|
649 |
=cut |
=cut |
650 |
|
|
651 |
sub scan ($&) { |
sub _scan_sort ($\@\%) { |
652 |
my ($self, $sub) = @_; |
my $self = shift; |
653 |
|
my ($array, $option) = @_; |
654 |
my $sort; |
my $sort; |
655 |
$sort = \&_header_cmp if $self->{option}->{sort} eq 'good-practice'; |
$sort = \&_header_cmp if $option->{field_sort} eq 'good-practice'; |
656 |
$sort = {$a cmp $b} if $self->{option}->{sort} eq 'alphabetic'; |
$sort = {$a cmp $b} if $option->{field_sort} eq 'alphabetic'; |
657 |
my @field = @{$self->{field}}; |
return ( sort $sort @$array ) if ref $sort; |
658 |
if (ref $sort) { |
@$array; |
659 |
@field = sort $sort @{$self->{field}}; |
} |
660 |
} |
|
661 |
for my $field (@field) { |
sub _n11n_field_name ($$) { |
662 |
next if $field->{name} =~ /^_/; |
my $self = shift; |
663 |
&$sub($field->{name} => $field->{body}); |
my $s = shift; |
664 |
} |
$s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//; |
665 |
|
$s = lc $s ;#unless $self->{option}->{field_name_case_sensible}; |
666 |
|
$s; |
667 |
} |
} |
668 |
|
|
669 |
# Compare function which makes it easy to sort headers in the |
# Compare function which makes it easy to sort headers in the |
696 |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
697 |
my @ret; |
my @ret; |
698 |
my $_stringify = sub { |
my $_stringify = sub { |
699 |
my ($name, $body) = (@_); |
no strict 'refs'; |
700 |
|
my ($name, $body, $nsuri) = ($_[1]->{name}, $_[1]->{body}, $_[1]->{ns}); |
701 |
return unless length $name; |
return unless length $name; |
702 |
return if $option{mail_from} && $name eq 'mail-from'; |
return if $option{output_mail_from} && $name eq 'mail-from'; |
703 |
return if !$option{output_bcc} && ($name eq 'bcc' || $name eq 'resent-bcc'); |
return if !$option{output_bcc} && ($name eq 'bcc' || $name eq 'resent-bcc'); |
704 |
|
my $nspackage = &_NS_uri2phpackage ($nsuri); |
705 |
|
my $oname; ## Outputed field-name |
706 |
|
my $prefix = ${$nspackage.'::OPTION'} {namespace_phname_goodcase} |
707 |
|
|| $self->{ns}->{uri2phname}->{$nsuri}; |
708 |
|
$prefix = undef if $nsuri eq $self->{ns}->{default_phuri}; |
709 |
|
my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase}; |
710 |
|
if (ref $gc) { $oname = &$gc ($self, $nspackage, $name) } |
711 |
|
else { $oname = $name } |
712 |
|
if ($prefix) { $oname = $prefix . '-' . $oname } |
713 |
if ($option{format} =~ /uri-url-mailto/) { |
if ($option{format} =~ /uri-url-mailto/) { |
714 |
return if (( $option{uri_mailto_safe}->{$name} |
return if (( $option{uri_mailto_safe}->{$name} |
715 |
|| $option{uri_mailto_safe}->{':default'}) |
|| $option{uri_mailto_safe}->{':default'}) |
716 |
< $option{uri_mailto_safe_level}); |
< $option{uri_mailto_safe_level}); |
717 |
if ($name eq 'to') { |
if ($name eq 'to') { |
718 |
$body = $self->field ('to'); |
$body = $self->field ('to', -new_item_unless_exist => 0); |
719 |
return unless ref $body && $body->have_group; |
if (ref $body && $body->have_group) { |
720 |
|
# |
721 |
|
} elsif (ref $body && $body->count > 1) { |
722 |
|
$body = $body->clone; |
723 |
|
$body->delete ({-by => 'index'}, 0); |
724 |
|
} |
725 |
} |
} |
726 |
} |
} |
727 |
my $fbody; |
my $fbody; |
738 |
} else { |
} else { |
739 |
$fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g; |
$fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g; |
740 |
} |
} |
741 |
if (ref $option{capitalize}) { |
if ($option{use_folding}) { |
742 |
$name = &{$option{capitalize}} ($self, $name); |
if (ref $option{output_folding}) { |
743 |
} elsif ($option{capitalize}) { |
$fbody = &{$option{output_folding}} ($self, $fbody, |
744 |
$name =~ s/((?:^|-)[a-z])/uc($1)/ge; |
-initial_length => length ($oname) +2); |
745 |
} |
} elsif ($option{output_folding}) { |
746 |
if (ref $option{fold}) { |
$fbody = $self->_fold ($fbody, -initial_length => length ($oname) +2); |
747 |
$fbody = &{$option{fold}} ($self, $fbody); |
} |
|
} elsif ($option{fold}) { |
|
|
$fbody = $self->_fold ($fbody); |
|
748 |
} |
} |
749 |
push @ret, sprintf $option{field_format_pattern}, $name, $fbody; |
push @ret, sprintf $option{field_format_pattern}, $oname, $fbody; |
750 |
}; |
}; |
751 |
if ($option{format} =~ /uri-url-mailto-to/) { |
if ($option{format} =~ /uri-url-mailto/) { |
752 |
if ($self->field_exist ('to')) { |
if ($option{format} =~ /uri-url-mailto-to/) { |
753 |
my $to = $self->field ('to'); |
my $to = $self->field ('to', -new_item_unless_exist => 0); |
754 |
unless ($to->have_group) { |
if ($to) { |
755 |
my $fbody = $to->stringify (-format => $option{format}); |
unless ($to->have_group) { |
756 |
return &{$option{fold}} ($self, $fbody); |
my $fbody = $to->stringify (-format => $option{format}, -max => 1); |
757 |
|
return &{$option{output_folding}} ($self, $fbody); |
758 |
|
} |
759 |
} |
} |
760 |
|
''; |
761 |
|
} elsif ($option{format} =~ /uri-url-mailto-rfc1738/) { |
762 |
|
my $to = $self->field ('to', -new_item_unless_exist => 0); |
763 |
|
if ($to) { |
764 |
|
my $fbody = $to->addr_spec (-format => $option{format}); |
765 |
|
return &{$option{output_folding}} ($self, $fbody); |
766 |
|
} |
767 |
|
''; |
768 |
|
} else { |
769 |
|
$self->scan ($_stringify); |
770 |
|
my $ret = join ('&', @ret); |
771 |
|
$ret; |
772 |
} |
} |
|
''; |
|
|
} elsif ($option{format} =~ /uri-url-mailto/) { |
|
|
$self->scan ($_stringify); |
|
|
my $ret = join ('&', @ret); |
|
|
$ret; |
|
773 |
} else { |
} else { |
774 |
if ($option{mail_from}) { |
if ($option{output_mail_from}) { |
775 |
my $fromline = $self->field ('mail-from'); |
my $fromline = $self->field ('mail-from', -new_item_unless_exist => 0); |
776 |
push @ret, 'From '.$fromline if $fromline; |
push @ret, 'From '.$fromline if $fromline; |
777 |
} |
} |
778 |
$self->scan ($_stringify); |
$self->scan ($_stringify); |
779 |
my $ret = join ("\n", @ret); |
my $ret = join ("\x0D\x0A", @ret); |
780 |
$ret? $ret."\n": ''; |
$ret? $ret."\x0D\x0A": ''; |
781 |
} |
} |
782 |
} |
} |
783 |
*as_string = \&stringify; |
*as_string = \&stringify; |
794 |
return $self->{option}->{ shift (@_) }; |
return $self->{option}->{ shift (@_) }; |
795 |
} |
} |
796 |
while (my ($name, $value) = splice (@_, 0, 2)) { |
while (my ($name, $value) = splice (@_, 0, 2)) { |
|
$name =~ s/^-//; |
|
797 |
$self->{option}->{$name} = $value; |
$self->{option}->{$name} = $value; |
798 |
if ($name eq 'format') { |
if ($name eq 'format') { |
799 |
for my $f (@{$self->{field}}) { |
for my $f (@{$self->{field}}) { |
805 |
} |
} |
806 |
} |
} |
807 |
|
|
808 |
sub field_type ($$;$) { |
sub field_type ($@) {shift->SUPER::value_type (@_)} |
809 |
|
|
810 |
|
## $self->_fold ($string, %option = (-max, -initial_length(for field-name)) ) |
811 |
|
sub _fold ($$;%) { |
812 |
my $self = shift; |
my $self = shift; |
813 |
my $field_name = shift; |
my $string = shift; |
814 |
my $new_field_type = shift; |
my %option = @_; |
815 |
if ($new_field_type) { |
my $max = $self->{option}->{line_length_max}; |
816 |
$self->{option}->{field_type}->{$field_name} = $new_field_type; |
$max = 20 if $max < 20; |
817 |
} |
|
818 |
$self->{option}->{field_type}->{$field_name} |
my $l = $option{-initial_length} || 0; |
819 |
|| $self->{option}->{field_type}->{':DEFAULT'}; |
$string =~ s{([\x09\x20][^\x09\x20]+)}{ |
820 |
|
my $s = $1; |
821 |
|
if ($l + length $s > $max) { |
822 |
|
$s = "\x0D\x0A\x20" . $s; |
823 |
|
$l = length ($s) - 2; |
824 |
|
} else { $l += length $s } |
825 |
|
$s; |
826 |
|
}gex; |
827 |
|
$string; |
828 |
} |
} |
829 |
|
|
830 |
sub _delete_empty_field ($) { |
sub _ns_load_ph ($$) { |
831 |
my $self = shift; |
my $self = shift; |
832 |
my @ret; |
my $name = shift; ## normalized prefix (without HYPHEN-MINUS) |
833 |
for my $field (@{$self->{field}}) { |
return if $self->{ns}->{phname2uri}->{$name}; |
834 |
push @ret, $field if $field->{name}; |
$self->{ns}->{phname2uri}->{$name} = $NS_phname2uri{$name}; |
835 |
} |
return unless $self->{ns}->{phname2uri}->{$name}; |
836 |
$self->{field} = \@ret; |
$self->{ns}->{uri2phname}->{$self->{ns}->{phname2uri}->{$name}} = $name; |
|
$self; |
|
837 |
} |
} |
838 |
|
|
839 |
sub _fold ($$;$) { |
sub _NS_uri2phpackage ($) { |
840 |
my $self = shift; |
$NS_uri2phpackage{$_[0]} |
841 |
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; |
|
842 |
} |
} |
843 |
|
|
844 |
=head2 $self->clone () |
=head2 $self->clone () |
847 |
|
|
848 |
=cut |
=cut |
849 |
|
|
850 |
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; |
|
|
} |
|
851 |
|
|
852 |
=head1 NOTE |
=head1 NOTE |
853 |
|
|