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 %DEFAULT); |
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 |
|
%REG = %Message::Util::REG; |
16 |
|
$REG{M_field} = qr/^([^\x3A]+):$REG{FWS}([\x00-\xFF]*)$/; |
17 |
|
$REG{M_fromline} = qr/^\x3E?From$REG{WSP}+([\x00-\xFF]*)$/; |
18 |
|
$REG{ftext} = qr/[\x21-\x39\x3B-\x7E]+/; ## [2]822 |
19 |
|
$REG{NON_ftext} = qr/[^\x21-\x39\x3B-\x7E]/; ## [2]822 |
20 |
|
$REG{NON_ftext_usefor} = qr/[^0-9A-Za-z-]/; ## name-character |
21 |
|
$REG{NON_ftext_http} = $REG{NON_http_token}; |
22 |
|
|
23 |
|
## Namespace support |
24 |
|
our %NS_phname2uri; ## PH-namespace name -> namespace URI |
25 |
|
our %NS_uri2phpackage; ## namespace URI -> PH-package name |
26 |
|
require Message::Header::Default; ## Default namespace |
27 |
|
|
28 |
|
## Initialize of this class -- called by constructors |
29 |
|
%DEFAULT = ( |
30 |
|
-_HASH_NAME => 'value', |
31 |
|
-_METHODS => [qw|field field_exist field_type add replace count delete subject id is|], |
32 |
|
-_MEMBERS => [qw|value|], |
33 |
|
-M_namsepace_prefix_regex => qr/(?!)/, |
34 |
|
-_VALTYPE_DEFAULT => ':default', |
35 |
|
-by => 'name', ## (Reserved for method level option) |
36 |
|
-field_format_pattern => '%s: %s', |
37 |
|
-field_name_case_sensible => 0, |
38 |
|
-field_name_unsafe_rule => 'NON_ftext', |
39 |
|
-field_name_validation => 1, ## Method level option. |
40 |
|
-field_sort => 0, |
41 |
|
#-format => 'mail-rfc2822', |
42 |
|
-linebreak_strict => 0, ## Not implemented completely |
43 |
|
-line_length_max => 60, ## For folding |
44 |
|
-ns_default_uri => $Message::Header::Default::OPTION{namespace_uri}, |
45 |
|
-output_bcc => 0, |
46 |
|
-output_folding => 1, |
47 |
|
-output_mail_from => 0, |
48 |
|
#-parse_all => 0, |
49 |
|
-translate_underscore => 1, |
50 |
|
#-uri_mailto_safe |
51 |
|
-uri_mailto_safe_level => 4, |
52 |
|
-use_folding => 1, |
53 |
|
#-value_type |
54 |
|
); |
55 |
|
|
56 |
|
$DEFAULT{-value_type} = { |
57 |
|
':default' => ['Message::Field::Unstructured'], |
58 |
|
|
59 |
|
p3p => ['Message::Field::Params'], |
60 |
|
link => ['Message::Field::ValueParams'], |
61 |
|
|
62 |
|
'list-software' => ['Message::Field::UA'], |
63 |
|
'user-agent' => ['Message::Field::UA'], |
64 |
|
server => ['Message::Field::UA'], |
65 |
|
}; |
66 |
|
for (qw(pics-label list-id status)) |
67 |
|
{$DEFAULT{-value_type}->{$_} = ['Message::Field::Structured']} |
68 |
|
## Not supported yet, but to be supported... |
69 |
|
# x-list: unstructured, ml name |
70 |
|
for (qw(date expires)) |
71 |
|
{$DEFAULT{-value_type}->{$_} = ['Message::Field::Date']} |
72 |
|
for (qw(accept accept-charset accept-encoding accept-language uri)) |
73 |
|
{$DEFAULT{-value_type}->{$_} = ['Message::Field::CSV']} |
74 |
|
for (qw(location referer)) |
75 |
|
{$DEFAULT{-value_type}->{$_} = ['Message::Field::URI']} |
76 |
|
|
77 |
|
my %header_goodcase = ( |
78 |
|
'article-i.d.' => 'Article-I.D.', |
79 |
|
etag => 'ETag', |
80 |
|
'pics-label' => 'PICS-Label', |
81 |
|
te => 'TE', |
82 |
|
url => 'URL', |
83 |
|
'www-authenticate' => 'WWW-Authenticate', |
84 |
|
); |
85 |
|
|
86 |
|
## taken from L<HTTP::Header> |
87 |
|
# "Good Practice" order of HTTP message headers: |
88 |
|
# - General-Headers |
89 |
|
# - Request-Headers |
90 |
|
# - Response-Headers |
91 |
|
# - Entity-Headers |
92 |
|
# (From draft-ietf-http-v11-spec-rev-01, Nov 21, 1997) |
93 |
|
my @header_order = qw( |
94 |
|
mail-from x-envelope-from relay-version path status |
95 |
|
|
96 |
|
cache-control connection date pragma transfer-encoding upgrade trailer via |
97 |
|
|
98 |
|
accept accept-charset accept-encoding accept-language |
99 |
|
authorization expect from host |
100 |
|
if-modified-since if-match if-none-match if-range if-unmodified-since |
101 |
|
max-forwards proxy-authorization range referer te user-agent |
102 |
|
|
103 |
|
accept-ranges age location proxy-authenticate retry-after server vary |
104 |
|
warning www-authenticate |
105 |
|
|
106 |
|
mime-version |
107 |
|
allow content-base content-encoding content-language content-length |
108 |
|
content-location content-md5 content-range content-type |
109 |
|
etag expires last-modified content-style-type content-script-type |
110 |
|
link |
111 |
|
|
112 |
|
xref |
113 |
|
); |
114 |
|
my %header_order; |
115 |
|
|
116 |
|
=head1 CONSTRUCTORS |
117 |
|
|
118 |
$REG{WSP} = qr/[\x09\x20]/; |
The following methods construct new C<Message::Header> objects: |
|
$REG{FWS} = qr/[\x09\x20]*/; |
|
|
$REG{M_field} = qr/^([^\x3A]+):$REG{FWS}([\x00-\xFF]*)$/; |
|
|
$REG{M_fromline} = qr/^\x3E?From$REG{WSP}+([\x00-\xFF]*)$/; |
|
|
$REG{UNSAFE_field_name} = qr/[\x00-\x20\x3A\x7F-\xFF]/; |
|
119 |
|
|
120 |
=head2 options |
=over 4 |
121 |
|
|
122 |
These options can be getten/set by C<get_option>/C<set_option> |
=cut |
|
method. |
|
123 |
|
|
124 |
=head3 capitalize = 0/1 |
sub _init ($;%) { |
125 |
|
my $self = shift; |
126 |
|
my %options = @_; |
127 |
|
my $DEFAULT = Message::Util::make_clone (\%DEFAULT); |
128 |
|
$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 = (); |
136 |
|
for my $name (keys %options) { |
137 |
|
unless (substr ($name, 0, 1) eq '-') { |
138 |
|
push @new_fields, ($name => $options{$name}); |
139 |
|
} |
140 |
|
} |
141 |
|
$self->_init_by_format ($self->{option}->{format}, $self->{option}); |
142 |
|
# Make alternative representations of @header_order. This is used |
143 |
|
# for sorting. |
144 |
|
my $i = 1; |
145 |
|
for (@header_order) { |
146 |
|
$header_order{$_} = $i++ unless $header_order{$_}; |
147 |
|
} |
148 |
|
|
149 |
|
$self->add (@new_fields, -parse => $self->{option}->{parse_all}) |
150 |
|
if $#new_fields > -1; |
151 |
|
} |
152 |
|
|
153 |
(First character of) C<field-name> is capitalized |
sub _init_by_format ($$\%) { |
154 |
when C<stringify>. (Default = 1) |
my $self = shift; |
155 |
|
my ($format, $option) = @_; |
156 |
|
if ($format =~ /cgi/) { |
157 |
|
unshift @header_order, qw(content-type location); |
158 |
|
$option->{field_sort} = 'good-practice'; |
159 |
|
$option->{use_folding} = 0; |
160 |
|
} elsif ($format =~ /http/) { |
161 |
|
$option->{field_sort} = 'good-practice'; |
162 |
|
} |
163 |
|
if ($format =~ /uri-url-mailto/) { |
164 |
|
$option->{output_bcc} = 0; |
165 |
|
$option->{field_format_pattern} = '%s=%s'; |
166 |
|
$option->{output_folding} = sub { |
167 |
|
$_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge; |
168 |
|
$_[1]; |
169 |
|
}; ## Yes, this is not folding! |
170 |
|
} |
171 |
|
} |
172 |
|
|
173 |
=head3 fold_length = numeric value |
=item $msg = Message::Header->new ([%initial-fields/options]) |
174 |
|
|
175 |
Length of line used to fold. (Default = 70) |
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. |
177 |
|
|
178 |
=head3 mail_from = 0/1 |
Example: |
179 |
|
|
180 |
Outputs "From " line (known as Un*x From, Mail-From, and so on) |
$hdr = new Message::Headers |
181 |
when C<stringify>. (Default = 0) |
Date => 'Thu, 03 Feb 1994 00:00:00 +0000', |
182 |
|
Content_Type => 'text/html', |
183 |
|
Content_Location => 'http://www.foo.example/', |
184 |
|
-format => 'mail-rfc2822' ## not to be header field |
185 |
|
; |
186 |
|
|
187 |
=cut |
=cut |
188 |
|
|
189 |
%DEFAULT = ( |
## Inherited |
|
capitalize => 1, |
|
|
fold_length => 70, |
|
|
field_type => {':DEFAULT' => 'Message::Field::Unstructured'}, |
|
|
format => 'rfc2822', ## rfc2822, usefor, http |
|
|
mail_from => -1, |
|
|
output_bcc => -1, |
|
|
parse_all => -1, |
|
|
); |
|
|
my @field_type_Structured = qw(cancel-lock |
|
|
importance path precedence |
|
|
x-face x-mail-count x-msmail-priority x-priority xref); |
|
|
for (@field_type_Structured) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::Structured'} |
|
|
my @field_type_Address = qw(approved bcc cc delivered-to disposition-notification-to |
|
|
envelope-to |
|
|
errors-to fcc from mail-followup-to mail-followup-cc mail-from reply-to resent-bcc |
|
|
resent-cc resent-to resent-from resent-sender return-path |
|
|
return-receipt-to sender to x-approved x-beenthere |
|
|
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); |
|
|
for (@field_type_Address) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::Address'} |
|
|
my @field_type_Date = qw(date date-received delivery-date expires |
|
|
expire-date nntp-posting-date posted reply-by resent-date x-tcup-date); |
|
|
for (@field_type_Date) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::Date'} |
|
|
my @field_type_MsgID = qw(article-updates content-id in-reply-to message-id |
|
|
references resent-message-id see-also supersedes); |
|
|
for (@field_type_MsgID) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::MsgID'} |
|
|
for (qw(received x-received)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::Received'} |
|
|
$DEFAULT{field_type}->{'content-type'} = 'Message::Field::ContentType'; |
|
|
$DEFAULT{field_type}->{'content-disposition'} = 'Message::Field::ContentDisposition'; |
|
|
for (qw(archive link x-face-type)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::ValueParams'} |
|
|
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 |
|
|
x-brother x-daughter x-respect x-moe x-syster x-wife)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::CSV'} |
|
|
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'} |
|
|
for (qw(list-id)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::Structured'} |
|
|
for (qw(subject title x-nsubject)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::Subject'} |
|
|
for (qw(list-software user-agent server)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::UA'} |
|
|
for (qw(content-length lines max-forwards mime-version)) |
|
|
{$DEFAULT{field_type}->{$_} = 'Message::Field::Numval'} |
|
190 |
|
|
191 |
=head2 Message::Header->new ([%option]) |
=item $msg = Message::Header->parse ($header, [%initial-fields/options]) |
192 |
|
|
193 |
Returns new Message::Header instance. Some options can be |
Parses given C<header> and constructs a new C<Message::Headers> |
194 |
specified as hash. |
object. You might pass some additional C<field-name>-C<field-body> pairs |
195 |
|
or/and initial options as parameters to the constructor. |
196 |
|
|
197 |
=cut |
=cut |
198 |
|
|
199 |
sub new ($;%) { |
sub parse ($$;%) { |
200 |
my $class = shift; |
my $class = shift; |
201 |
my $self = bless {option => {@_}}, $class; |
my $header = shift; |
202 |
for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}} |
my $self = bless {}, $class; |
203 |
|
$self->_init (@_); ## BUG: don't check linebreak_strict |
204 |
|
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding}; |
205 |
|
for my $field (split /\x0D?\x0A/, $header) { |
206 |
|
if ($field =~ /$REG{M_fromline}/) { |
207 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
208 |
|
('mail-from' => $1, $self->{option}); |
209 |
|
push @{$self->{value}}, $value if $s; |
210 |
|
} elsif ($field =~ /$REG{M_field}/) { |
211 |
|
my ($name, $body) = ($1, $2); |
212 |
|
$body =~ s/$REG{WSP}+$//; |
213 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
214 |
|
($name => $body, $self->{option}); |
215 |
|
push @{$self->{value}}, $value if $s; |
216 |
|
} elsif (length $field) { |
217 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
218 |
|
('x-unknown' => $field, $self->{option}); |
219 |
|
push @{$self->{value}}, $value if $s; |
220 |
|
} |
221 |
|
} |
222 |
$self; |
$self; |
223 |
} |
} |
224 |
|
|
225 |
=head2 Message::Header->parse ($header, [%option]) |
=item $msg = Message::Header->parse_array (\@header, [%initial-fields/options]) |
226 |
|
|
227 |
Parses given C<header> and return a new Message::Header |
Parses given C<header> and constructs a new C<Message::Headers> |
228 |
object. Some options can be specified as hash. |
object. Same as C<Message::Header-E<lt>parse> but this method |
229 |
|
is given an array reference. You might pass some additional |
230 |
|
C<field-name>-C<field-body> pairs or/and initial options |
231 |
|
as parameters to the constructor. |
232 |
|
|
233 |
=cut |
=cut |
234 |
|
|
235 |
sub parse ($$;%) { |
sub parse_array ($\@;%) { |
236 |
my $class = shift; |
my $class = shift; |
237 |
my $header = shift; |
my $header = shift; |
238 |
my $self = bless {option => {@_}}, $class; |
Carp::croak "parse_array: first argument is not an array reference" |
239 |
for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}} |
unless ref $header eq 'ARRAY'; |
240 |
$header =~ s/\x0D?\x0A$REG{WSP}+/\x20/gos; ## unfold |
my $self = bless {}, $class; |
241 |
for my $field (split /\x0D?\x0A/, $header) { |
$self->_init (@_); |
242 |
|
while (1) { |
243 |
|
my $field = shift @$header; |
244 |
|
if ($self->{option}->{use_folding}) { |
245 |
|
while (1) { |
246 |
|
if ($$header[0] =~ /^$REG{WSP}/) { |
247 |
|
$field .= shift @$header; |
248 |
|
} else {last} |
249 |
|
} |
250 |
|
} |
251 |
|
if ($self->{option}->{linebreak_strict}) { |
252 |
|
$field =~ s/\x0D\x0A//g; |
253 |
|
} else { |
254 |
|
$field =~ tr/\x0D\x0A//d; |
255 |
|
} |
256 |
|
local $self->{option}->{parse} = $self->{option}->{parse_all}; |
257 |
if ($field =~ /$REG{M_fromline}/) { |
if ($field =~ /$REG{M_fromline}/) { |
258 |
my $body = $1; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
259 |
$body = $self->_field_body ($body, 'mail-from') |
('mail-from' => $1, $self->{option}); |
260 |
if $self->{option}->{parse_all}>0; |
push @{$self->{value}}, $value if $s; |
|
push @{$self->{field}}, {name => 'mail-from', body => $body}; |
|
261 |
} elsif ($field =~ /$REG{M_field}/) { |
} elsif ($field =~ /$REG{M_field}/) { |
262 |
my ($name, $body) = (lc $1, $2); |
my ($name, $body) = ($self->_n11n_field_name ($1), $2); |
|
$name =~ s/$REG{WSP}+$//; |
|
263 |
$body =~ s/$REG{WSP}+$//; |
$body =~ s/$REG{WSP}+$//; |
264 |
$body = $self->_field_body ($body, $name) if $self->{option}->{parse_all}>0; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
265 |
push @{$self->{field}}, {name => $name, body => $body}; |
($name => $body, $self->{option}); |
266 |
|
push @{$self->{value}}, $value if $s; |
267 |
|
} elsif (length $field) { |
268 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
269 |
|
('x-unknown' => $field, $self->{option}); |
270 |
|
push @{$self->{value}}, $value if $s; |
271 |
} |
} |
272 |
|
last if $#$header < 0; |
273 |
} |
} |
274 |
$self; |
$self; |
275 |
} |
} |
276 |
|
|
277 |
|
=back |
278 |
|
|
279 |
|
=head1 METHODS |
280 |
|
|
281 |
=head2 $self->field ($field_name) |
=head2 $self->field ($field_name) |
282 |
|
|
283 |
Returns C<field-body> of given C<field-name>. |
Returns C<field-body> of given C<field-name>. |
287 |
|
|
288 |
=cut |
=cut |
289 |
|
|
290 |
sub field ($$) { |
sub field ($@) {shift->SUPER::item (@_)} |
291 |
|
sub field_exist ($@) {shift->SUPER::item_exist (@_)} |
292 |
|
|
293 |
|
## item-by?, \$checked-item, {item-key => 1}, \%option |
294 |
|
sub _item_match ($$\$\%\%) { |
295 |
my $self = shift; |
my $self = shift; |
296 |
my $name = lc shift; |
my ($by, $i, $list, $option) = @_; |
297 |
my @ret; |
return 0 unless ref $$i; ## Already removed |
298 |
for my $field (@{$self->{field}}) { |
if ($by eq 'name') { |
299 |
if ($field->{name} eq $name) { |
my %o = %$option; $o{parse} = 0; |
300 |
unless (wantarray) { |
my %l; |
301 |
$field->{body} = $self->_field_body ($field->{body}, $name); |
for (keys %$list) { |
302 |
return $field->{body}; |
my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', %o); |
303 |
|
if ($s) { |
304 |
|
$l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1; |
305 |
} else { |
} else { |
306 |
$field->{body} = $self->_field_body ($field->{body}, $name); |
$l{$v->{name} .':'. ( $option->{ns} || $self->{ns}->{default_phuri} ) } = 1; |
|
push @ret, $field->{body}; |
|
307 |
} |
} |
308 |
} |
} |
309 |
|
return 1 if $l{$$i->{name} . ':' . $$i->{ns}}; |
310 |
} |
} |
311 |
if ($#ret < 0) { |
0; |
|
return $self->add ($name); |
|
|
} |
|
|
@ret; |
|
312 |
} |
} |
313 |
|
*_delete_match = \&_item_match; |
314 |
|
|
315 |
sub field_exist ($$) { |
## Returns returned item value \$item-value, \%option |
316 |
my $self = shift; |
sub _item_return_value ($\$\%) { |
317 |
my $name = lc shift; |
if (ref ${$_[1]}->{body}) { |
318 |
my @ret; |
${$_[1]}->{body}; |
319 |
for my $field (@{$self->{field}}) { |
} else { |
320 |
return 1 if ($field->{name} eq $name); |
${$_[1]}->{body} = $_[0]->_parse_value (${$_[1]}->{name} => ${$_[1]}->{body}, |
321 |
|
ns => ${$_[1]}->{ns}); |
322 |
|
${$_[1]}->{body}; |
323 |
} |
} |
|
0; |
|
324 |
} |
} |
325 |
|
|
326 |
=head2 $self->field_name ($index) |
## Returns returned (new created) item value $name, \%option |
327 |
|
sub _item_new_value ($$\%) { |
328 |
Returns C<field-name> of $index'th C<field>. |
my ($s,undef,$value) = $_[0]->_value_to_arrayitem |
329 |
|
($_[1] => '', $_[2]); |
330 |
=head2 $self->field_body ($index) |
$s? $value: undef; |
331 |
|
} |
|
Returns C<field-body> of $index'th C<field>. |
|
332 |
|
|
|
=cut |
|
333 |
|
|
|
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}; |
|
|
} |
|
334 |
|
|
335 |
sub _field_body ($$$) { |
## $self->_parse_value ($type, $value, %options); |
336 |
|
sub _parse_value ($$$;%) { |
337 |
my $self = shift; |
my $self = shift; |
338 |
my ($body, $name) = @_; |
my $name = shift ;#|| $self->{option}->{_VALTYPE_DEFAULT}; |
339 |
unless (ref $body) { |
my $value = shift; return $value if ref $value; |
340 |
my $type = $self->{option}->{field_type}->{$name} |
my %option = @_; |
341 |
|| $self->{option}->{field_type}->{':DEFAULT'}; |
my $vtype; { no strict 'refs'; |
342 |
eval "require $type"; |
$vtype = ${&_NS_uri2phpackage ($option{ns}).'::OPTION'}{value_type}; |
343 |
unless ($body) { |
if (ref $vtype) { $vtype = $vtype->{$name} } |
344 |
$body = $type->new (field_name => $name, format => $self->{option}->{format}); |
unless (ref $vtype) { $vtype = $vtype->{$self->{option}->{_VALTYPE_DEFAULT}} } |
345 |
} else { |
## For compatiblity. |
346 |
$body = $type->parse ($body, field_name => $name, |
unless (ref $vtype) { $vtype = $self->{option}->{value_type}->{$name} |
347 |
format => $self->{option}->{format}); |
|| $self->{option}->{value_type}->{$self->{option}->{_VALTYPE_DEFAULT}} } |
348 |
} |
} |
349 |
|
my $vpackage = $vtype->[0]; |
350 |
|
my %vopt = %{$vtype->[1]} if ref $vtype->[1]; |
351 |
|
if ($vpackage eq ':none:') { |
352 |
|
return $value; |
353 |
|
} elsif (defined $value) { |
354 |
|
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
355 |
|
return $vpackage->parse ($value, |
356 |
|
-format => $self->{option}->{format}, |
357 |
|
-field_ns => $option{ns}, |
358 |
|
-field_name => $name, |
359 |
|
-parse_all => $self->{option}->{parse_all}, |
360 |
|
%vopt); |
361 |
|
} else { |
362 |
|
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
363 |
|
return $vpackage->new ( |
364 |
|
-format => $self->{option}->{format}, |
365 |
|
-field_ns => $option{ns}, |
366 |
|
-field_name => $name, |
367 |
|
-parse_all => $self->{option}->{parse_all}, |
368 |
|
%vopt); |
369 |
} |
} |
|
$body; |
|
370 |
} |
} |
371 |
|
|
372 |
=head2 $self->field_name_list () |
=head2 $self->field_name_list () |
379 |
|
|
380 |
sub field_name_list ($) { |
sub field_name_list ($) { |
381 |
my $self = shift; |
my $self = shift; |
382 |
$self->_delete_empty_field (); |
$self->_delete_empty (); |
383 |
map {$_->{name}} @{$self->{field}}; |
map { $_->{name} . ':' . $_->{ns} } @{$self->{value}}; |
384 |
|
} |
385 |
|
|
386 |
|
sub namespace_ph_default ($;$) { |
387 |
|
my $self = shift; |
388 |
|
if (defined $_[0]) { |
389 |
|
no strict 'refs'; |
390 |
|
$self->{ns}->{default_phuri} = $_[0]; |
391 |
|
$self->_ns_load_ph (${&_NS_uri2phpackage ($self->{ns}->{default_phuri}).'::OPTION'}{namespace_phname}); |
392 |
|
} |
393 |
|
$self->{ns}->{default_phuri}; |
394 |
} |
} |
395 |
|
|
396 |
=head2 $self->add ($field_name, $field_body) |
=item $hdr->add ($field-name, $field-body, [$name, $body, ...]) |
397 |
|
|
398 |
|
Adds some field name/body pairs. Even if there are |
399 |
|
one or more fields named given C<$field-name>, |
400 |
|
given name/body pairs are ADDed. Use C<replace> |
401 |
|
to remove same-name-fields. |
402 |
|
|
403 |
|
Instead of field name-body pair, you might pass some options. |
404 |
|
Four options are available for this method. |
405 |
|
|
406 |
Adds an new C<field>. It is not checked whether |
C<-parse>: Parses and validates C<field-body>, and returns |
407 |
the field which named $field_body is already exist or not. |
C<field-body> object. (When multiple C<field-body>s are |
408 |
If you don't want duplicated C<field>s, use C<replace> method. |
added, returns only last one.) (Default: C<defined wantarray>) |
409 |
|
|
410 |
|
C<-prepend>: New fields are not appended, |
411 |
|
but prepended to current fields. (Default: C<0>) |
412 |
|
|
413 |
|
C<-translate-underscore>: Do C<field-name> =~ tr/_/-/. (Default: C<1>) |
414 |
|
|
415 |
|
C<-validate>: Checks whether C<field-name> is valid or not. |
416 |
|
|
417 |
=cut |
=cut |
418 |
|
|
419 |
sub add ($$;$%) { |
## [Name: Value] pair -> internal array item |
420 |
|
## $self->_value_to_arrayitem ($name => $value, {%options}) |
421 |
|
## or |
422 |
|
## $self->_value_to_arrayitem ($name => [$value, %value_options], {%options}) |
423 |
|
## |
424 |
|
## Return: ((1 = success / 0 = failue), $full_name, $arrayitem) |
425 |
|
sub _value_to_arrayitem ($$$\%) { |
426 |
my $self = shift; |
my $self = shift; |
427 |
my ($name, $body) = (lc shift, shift); |
my ($name, $value, $option) = @_; |
428 |
my %option = @_; |
my $value_option = {}; |
429 |
return 0 if $name =~ /$REG{UNSAFE_field_name}/; |
if (ref $value eq 'ARRAY') { |
430 |
$body = $self->_field_body ($body, $name); |
($value, %$value_option) = @$value; |
431 |
if ($option{prepend}) { |
} |
432 |
unshift @{$self->{field}}, {name => $name, body => $body}; |
my $nsuri = $self->{ns}->{default_phuri}; |
433 |
} else { |
no strict 'refs'; |
434 |
push @{$self->{field}}, {name => $name, body => $body}; |
if ($option->{ns}) { |
435 |
|
$nsuri = $option->{ns}; |
436 |
|
} elsif ($name =~ s/^([Xx]-[A-Za-z]+|[A-Za-z]+)-//) { |
437 |
|
my $oprefix = $1; |
438 |
|
my $prefix |
439 |
|
= &{${&_NS_uri2phpackage ($nsuri).'::OPTION'}{n11n_prefix}} |
440 |
|
($self, &_NS_uri2phpackage ($nsuri), $oprefix); |
441 |
|
$self->_ns_load_ph ($prefix); |
442 |
|
$nsuri = $self->{ns}->{phname2uri}->{$prefix}; |
443 |
|
unless ($nsuri) { |
444 |
|
$name = $oprefix . '-' . $name; |
445 |
|
$nsuri = $self->{ns}->{default_phuri}; |
446 |
|
} |
447 |
} |
} |
448 |
$body; |
$name |
449 |
|
= &{${&_NS_uri2phpackage ($nsuri).'::OPTION'}{n11n_name}} |
450 |
|
($self, &_NS_uri2phpackage ($nsuri), $name); |
451 |
|
Carp::croak "$name: invalid field-name" |
452 |
|
if $option->{field_name_validation} |
453 |
|
&& $name =~ /$REG{$option->{field_name_unsafe_rule}}/; |
454 |
|
$value = $self->_parse_value ($name => $value, ns => $nsuri) if $$option{parse}; |
455 |
|
$$option{parse} = 0; |
456 |
|
(1, $name.':'.$nsuri => {name => $name, body => $value, ns => $nsuri}); |
457 |
} |
} |
458 |
|
*_add_hash_check = \&_value_to_arrayitem; |
459 |
|
*_replace_hash_check = \&_value_to_arrayitem; |
460 |
|
|
461 |
=head2 $self->relace ($field_name, $field_body) |
=head2 $self->relace ($field_name, $field_body) |
462 |
|
|
468 |
|
|
469 |
=cut |
=cut |
470 |
|
|
471 |
sub replace ($$$) { |
sub _replace_hash_shift ($\%$\%) { |
472 |
my $self = shift; |
shift; my $r = shift; my $n = $_[0]->{name} . ':' . $_[0]->{ns}; |
473 |
my ($name, $body) = (lc shift, shift); |
if ($$r{$n}) { |
474 |
return 0 if $name =~ /$REG{UNSAFE_field_name}/; |
my $d = $$r{$n}; |
475 |
$body = $self->_field_body ($body, $name); |
delete $$r{$n}; |
476 |
for my $field (@{$self->{field}}) { |
return $d; |
|
if ($field->{name} eq $name) { |
|
|
$field->{body} = $body; |
|
|
return $body; |
|
|
} |
|
477 |
} |
} |
478 |
push @{$self->{field}}, {name => $name, body => $body}; |
undef; |
|
$body; |
|
479 |
} |
} |
480 |
|
|
481 |
=head2 $self->delete ($field_name, [$index]) |
=head2 $self->delete ($field-name, [$name, ...]) |
482 |
|
|
483 |
Deletes C<field> named as $field_name. |
Deletes C<field> named as $field_name. |
|
If $index is specified, only $index'th C<field> is deleted. |
|
|
($index of first field is C<1>, not C<0>.) |
|
|
If not, ($index == 0), all C<field>s that have the C<field-name> |
|
|
$field_name are deleted. |
|
484 |
|
|
485 |
=cut |
=cut |
486 |
|
|
|
sub delete ($$;$) { |
|
|
my $self = shift; |
|
|
my ($name, $index) = (lc shift, shift); |
|
|
my $i = 0; |
|
|
for my $field (@{$self->{field}}) { |
|
|
if ($field->{name} eq $name) { |
|
|
$i++; |
|
|
if ($index == 0 || $i == $index) { |
|
|
undef $field; |
|
|
return $self if $i == $index; |
|
|
} |
|
|
} |
|
|
} |
|
|
$self; |
|
|
} |
|
487 |
|
|
488 |
=head2 $self->count ([$field_name]) |
=head2 $self->count ([$field_name]) |
489 |
|
|
493 |
|
|
494 |
=cut |
=cut |
495 |
|
|
496 |
sub count ($;$) { |
sub _count_by_name ($$\%) { |
497 |
|
my $self = shift; |
498 |
|
my ($array, $option) = @_; |
499 |
|
my $name = $self->_n11n_field_name ($$option{-name}); |
500 |
|
my @a = grep {$_->{name} eq $name} @{$self->{$array}}; |
501 |
|
$#a + 1; |
502 |
|
} |
503 |
|
|
504 |
|
## Delete empty items |
505 |
|
sub _delete_empty ($) { |
506 |
|
my $self = shift; |
507 |
|
my $array = $self->{option}->{_HASH_NAME}; |
508 |
|
$self->{$array} = [grep {ref $_ && length $_->{name}} @{$self->{$array}}]; |
509 |
|
} |
510 |
|
|
511 |
|
=head2 $self->rename ($field-name, $new-name, [$old, $new,...]) |
512 |
|
|
513 |
|
Renames C<$field-name> as C<$new-name>. |
514 |
|
|
515 |
|
=cut |
516 |
|
|
517 |
|
sub rename ($%) { |
518 |
my $self = shift; |
my $self = shift; |
519 |
my ($name) = (lc shift); |
my %params = @_; |
520 |
unless ($name) { |
my %option = %{$self->{option}}; |
521 |
$self->_delete_empty_field (); |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
522 |
return $#{$self->{field}}+1; |
my %new_name; |
523 |
|
for (grep {/^[^-]/} keys %params) { |
524 |
|
my ($old => $new) |
525 |
|
= ($self->_n11n_field_name ($_) => $self->_n11n_field_name ($params{$_})); |
526 |
|
$old =~ tr/_/-/ if $option{translate_underscore}; |
527 |
|
$new =~ tr/_/-/ if $option{translate_underscore}; |
528 |
|
Carp::croak "rename: $new: invalid field-name" |
529 |
|
if $option{field_name_validation} |
530 |
|
&& $new =~ /$REG{$option{field_name_unsafe_rule}}/; |
531 |
|
$new_name{$old} = $new; |
532 |
} |
} |
533 |
my $count = 0; |
for my $field (@{$self->{value}}) { |
534 |
for my $field (@{$self->{field}}) { |
if (length $new_name{$field->{name}}) { |
535 |
if ($field->{name} eq $name) { |
$field->{name} = $new_name{$field->{name}}; |
|
$count++; |
|
536 |
} |
} |
537 |
} |
} |
538 |
$count; |
$self if defined wantarray; |
539 |
} |
} |
540 |
|
|
|
=head2 $self->rename ($field_name, [$index]) |
|
541 |
|
|
542 |
Renames C<field> named as $field_name. |
=item $self->scan(\&doit) |
543 |
If $index is specified, only $index'th C<field> is renamed. |
|
544 |
($index of first field is C<1>, not C<0>.) |
Apply a subroutine to each header field in turn. The callback routine is |
545 |
If not, ($index == 0), all C<field>s that have the C<field-name> |
called with two parameters; the name of the field and a single value. |
546 |
$field_name are renamed. |
If the header has more than one value, then the routine is called once |
547 |
|
for each value. |
548 |
|
|
549 |
=cut |
=cut |
550 |
|
|
551 |
sub rename ($$$;$) { |
sub _scan_sort ($\@\%) { |
552 |
my $self = shift; |
my $self = shift; |
553 |
my ($name, $newname, $index) = (lc shift, lc shift, shift); |
my ($array, $option) = @_; |
554 |
my $i = 0; |
my $sort; |
555 |
croak "rename: new field-name contains of unsafe character: $newname" |
$sort = \&_header_cmp if $option->{field_sort} eq 'good-practice'; |
556 |
if !$newname || $newname =~ /$REG{UNSAFE_field_name}/; |
$sort = {$a cmp $b} if $option->{field_sort} eq 'alphabetic'; |
557 |
for my $field (@{$self->{field}}) { |
return ( sort $sort @$array ) if ref $sort; |
558 |
if ($field->{name} eq $name) { |
@$array; |
559 |
$i++; |
} |
560 |
if ($index == 0 || $i == $index) { |
|
561 |
$field->{name} = $newname; |
sub _n11n_field_name ($$) { |
562 |
return $self if $i == $index; |
my $self = shift; |
563 |
} |
my $s = shift; |
564 |
} |
$s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//; |
565 |
} |
$s = lc $s ;#unless $self->{option}->{field_name_case_sensible}; |
566 |
$self; |
$s; |
567 |
|
} |
568 |
|
|
569 |
|
# Compare function which makes it easy to sort headers in the |
570 |
|
# recommended "Good Practice" order. |
571 |
|
## taken from HTTP::Header |
572 |
|
sub _header_cmp |
573 |
|
{ |
574 |
|
my ($na, $nb) = ($a->{name}, $b->{name}); |
575 |
|
# Unknown headers are assign a large value so that they are |
576 |
|
# sorted last. This also helps avoiding a warning from -w |
577 |
|
# about comparing undefined values. |
578 |
|
$header_order{$na} = 999 unless defined $header_order{$na}; |
579 |
|
$header_order{$nb} = 999 unless defined $header_order{$nb}; |
580 |
|
|
581 |
|
$header_order{$na} <=> $header_order{$nb} || $na cmp $nb; |
582 |
} |
} |
583 |
|
|
584 |
=head2 $self->stringify ([%option]) |
=head2 $self->stringify ([%option]) |
589 |
|
|
590 |
sub stringify ($;%) { |
sub stringify ($;%) { |
591 |
my $self = shift; |
my $self = shift; |
592 |
my %OPT = @_; |
my %params = @_; |
593 |
|
my %option = %{$self->{option}}; |
594 |
|
$option{format} = $params{-format} if $params{-format}; |
595 |
|
$self->_init_by_format ($option{format}, \%option); |
596 |
|
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
597 |
my @ret; |
my @ret; |
598 |
$OPT{capitalize} ||= $self->{option}->{capitalize}; |
my $_stringify = sub { |
599 |
$OPT{mail_from} ||= $self->{option}->{mail_from}; |
no strict 'refs'; |
600 |
$OPT{output_bcc} ||= $self->{option}->{output_bcc}; |
my ($name, $body, $nsuri) = ($_[1]->{name}, $_[1]->{body}, $_[1]->{ns}); |
601 |
$OPT{format} ||= $self->{option}->{format}; |
return unless length $name; |
602 |
push @ret, 'From '.$self->field ('mail-from') if $OPT{mail_from}>0; |
return if $option{output_mail_from} && $name eq 'mail-from'; |
603 |
for my $field (@{$self->{field}}) { |
return if !$option{output_bcc} && ($name eq 'bcc' || $name eq 'resent-bcc'); |
604 |
my $name = $field->{name}; |
my $nspackage = &_NS_uri2phpackage ($nsuri); |
605 |
next unless $name; |
my $oname; ## Outputed field-name |
606 |
next if $OPT{mail_from}<0 && $name eq 'mail-from'; |
my $prefix = ${$nspackage.'::OPTION'} {namespace_phname_goodcase} |
607 |
next if $OPT{output_bcc}<0 && ($name eq 'bcc' || $name eq 'resent-bcc'); |
|| $self->{ns}->{uri2phname}->{$nsuri}; |
608 |
my $fbody; |
$prefix = undef if $nsuri eq $self->{ns}->{default_phuri}; |
609 |
if (ref $field->{body}) { |
my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase}; |
610 |
$fbody = $field->{body}->stringify (format => $OPT{format}); |
if (ref $gc) { $oname = &$gc ($self, $nspackage, $name, \%option) } |
611 |
|
else { $oname = $name } |
612 |
|
if ($prefix) { $oname = $prefix . '-' . $oname } |
613 |
|
if ($option{format} =~ /uri-url-mailto/) { |
614 |
|
return if (( ${$nspackage.'::OPTION'} {uri_mailto_safe}->{$name} |
615 |
|
|| ${$nspackage.'::OPTION'} {uri_mailto_safe}->{':default'}) |
616 |
|
< $option{uri_mailto_safe_level}); |
617 |
|
if ($name eq 'to') { |
618 |
|
$body = $self->field ('to', -new_item_unless_exist => 0); |
619 |
|
if (ref $body && $body->have_group) { |
620 |
|
# |
621 |
|
} elsif (ref $body && $body->count > 1) { |
622 |
|
$body = $body->clone; |
623 |
|
$body->delete ({-by => 'index'}, 0); |
624 |
|
} |
625 |
|
} |
626 |
|
} |
627 |
|
my $fbody; |
628 |
|
if (ref $body) { |
629 |
|
$fbody = $body->stringify (-format => $option{format}); |
630 |
|
} else { |
631 |
|
$fbody = $body; |
632 |
|
} |
633 |
|
return unless length $fbody; |
634 |
|
unless ($option{linebreak_strict}) { |
635 |
|
## bare \x0D and bare \x0A are unsafe |
636 |
|
$fbody =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g; |
637 |
|
$fbody =~ s/\x0A(?=[^\x09\x20])/\x0A\x20/g; |
638 |
|
} else { |
639 |
|
$fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g; |
640 |
|
} |
641 |
|
if ($option{use_folding}) { |
642 |
|
if (ref $option{output_folding}) { |
643 |
|
$fbody = &{$option{output_folding}} ($self, $fbody, |
644 |
|
-initial_length => length ($oname) +2); |
645 |
|
} elsif ($option{output_folding}) { |
646 |
|
$fbody = $self->_fold ($fbody, -initial_length => length ($oname) +2); |
647 |
|
} |
648 |
|
} |
649 |
|
push @ret, sprintf $option{field_format_pattern}, $oname, $fbody; |
650 |
|
}; |
651 |
|
if ($option{format} =~ /uri-url-mailto/) { |
652 |
|
if ($option{format} =~ /uri-url-mailto-to/) { |
653 |
|
my $to = $self->field ('to', -new_item_unless_exist => 0); |
654 |
|
if ($to) { |
655 |
|
unless ($to->have_group) { |
656 |
|
my $fbody = $to->stringify (-format => $option{format}, -max => 1); |
657 |
|
return &{$option{output_folding}} ($self, $fbody); |
658 |
|
} |
659 |
|
} |
660 |
|
''; |
661 |
|
} elsif ($option{format} =~ /uri-url-mailto-rfc1738/) { |
662 |
|
my $to = $self->field ('to', -new_item_unless_exist => 0); |
663 |
|
if ($to) { |
664 |
|
my $fbody = $to->addr_spec (-format => $option{format}); |
665 |
|
return &{$option{output_folding}} ($self, $fbody); |
666 |
|
} |
667 |
|
''; |
668 |
} else { |
} else { |
669 |
$fbody = $field->{body}; |
$self->scan ($_stringify); |
670 |
|
my $ret = join ('&', @ret); |
671 |
|
$ret; |
672 |
|
} |
673 |
|
} else { |
674 |
|
if ($option{output_mail_from}) { |
675 |
|
my $fromline = $self->field ('mail-from', -new_item_unless_exist => 0); |
676 |
|
push @ret, 'From '.$fromline if $fromline; |
677 |
} |
} |
678 |
next unless $fbody; |
$self->scan ($_stringify); |
679 |
$fbody =~ s/\x0D([^\x09\x0A\x20])/\x0D\x20$1/g; |
my $ret = join ("\x0D\x0A", @ret); |
680 |
$fbody =~ s/\x0A([^\x09\x20])/\x0A\x20$1/g; |
$ret? $ret."\x0D\x0A": ''; |
|
$name =~ s/((?:^|-)[a-z])/uc($1)/ge if $OPT{capitalize}; |
|
|
push @ret, $name.': '.$self->fold ($fbody); |
|
681 |
} |
} |
|
my $ret = join ("\n", @ret); |
|
|
$ret? $ret."\n": ""; |
|
682 |
} |
} |
683 |
|
*as_string = \&stringify; |
684 |
|
|
685 |
=head2 $self->option ($option_name, [$option_value]) |
=head2 $self->option ($option_name, [$option_value]) |
686 |
|
|
688 |
|
|
689 |
=cut |
=cut |
690 |
|
|
691 |
sub option ($$;$) { |
sub option ($@) { |
692 |
my $self = shift; |
my $self = shift; |
693 |
my ($name, $value) = @_; |
if (@_ == 1) { |
694 |
if (defined $value) { |
return $self->{option}->{ shift (@_) }; |
695 |
|
} |
696 |
|
while (my ($name, $value) = splice (@_, 0, 2)) { |
697 |
$self->{option}->{$name} = $value; |
$self->{option}->{$name} = $value; |
698 |
if ($name eq 'format') { |
if ($name eq 'format') { |
699 |
for my $f (@{$self->{field}}) { |
for my $f (@{$self->{field}}) { |
700 |
if (ref $f) { |
if (ref $f->{body}) { |
701 |
$f->option (format => $value); |
$f->{body}->option (-format => $value); |
702 |
} |
} |
703 |
} |
} |
704 |
} |
} |
705 |
} |
} |
|
$self->{option}->{$name}; |
|
706 |
} |
} |
707 |
|
|
708 |
sub field_type ($$;$) { |
sub field_type ($@) {shift->SUPER::value_type (@_)} |
709 |
|
|
710 |
|
## $self->_fold ($string, %option = (-max, -initial_length(for field-name)) ) |
711 |
|
sub _fold ($$;%) { |
712 |
my $self = shift; |
my $self = shift; |
713 |
my $field_name = shift; |
my $string = shift; |
714 |
my $new_field_type = shift; |
my %option = @_; |
715 |
if ($new_field_type) { |
my $max = $self->{option}->{line_length_max}; |
716 |
$self->{option}->{field_type}->{$field_name} = $new_field_type; |
$max = 20 if $max < 20; |
717 |
} |
|
718 |
$self->{option}->{field_type}->{$field_name} |
my $l = $option{-initial_length} || 0; |
719 |
|| $self->{option}->{field_type}->{':DEFAULT'}; |
$string =~ s{([\x09\x20][^\x09\x20]+)}{ |
720 |
|
my $s = $1; |
721 |
|
if ($l + length $s > $max) { |
722 |
|
$s = "\x0D\x0A\x20" . $s; |
723 |
|
$l = length ($s) - 2; |
724 |
|
} else { $l += length $s } |
725 |
|
$s; |
726 |
|
}gex; |
727 |
|
$string; |
728 |
} |
} |
729 |
|
|
730 |
sub _delete_empty_field ($) { |
sub _ns_load_ph ($$) { |
731 |
my $self = shift; |
my $self = shift; |
732 |
my @ret; |
my $name = shift; ## normalized prefix (without HYPHEN-MINUS) |
733 |
for my $field (@{$self->{field}}) { |
return if $self->{ns}->{phname2uri}->{$name}; |
734 |
push @ret, $field if $field->{name}; |
$self->{ns}->{phname2uri}->{$name} = $NS_phname2uri{$name}; |
735 |
} |
return unless $self->{ns}->{phname2uri}->{$name}; |
736 |
$self->{field} = \@ret; |
$self->{ns}->{uri2phname}->{$self->{ns}->{phname2uri}->{$name}} = $name; |
|
$self; |
|
737 |
} |
} |
738 |
|
|
739 |
sub fold ($$;$) { |
sub _NS_uri2phpackage ($) { |
740 |
my $self = shift; |
$NS_uri2phpackage{$_[0]} |
741 |
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; |
|
742 |
} |
} |
743 |
|
|
744 |
|
=head2 $self->clone () |
745 |
|
|
746 |
|
Returns a copy of Message::Header object. |
747 |
|
|
748 |
|
=cut |
749 |
|
|
750 |
|
## Inhreited |
751 |
|
|
752 |
|
=head1 NOTE |
753 |
|
|
754 |
|
=head2 C<field-name> |
755 |
|
|
756 |
|
The header field name is not case sensitive. To make the life |
757 |
|
easier for perl users who wants to avoid quoting before the => operator, |
758 |
|
you can use '_' as a synonym for '-' in header field names |
759 |
|
(this behaviour can be suppressed by setting |
760 |
|
C<translate_underscore> option to C<0> value). |
761 |
|
|
762 |
=head1 EXAMPLE |
=head1 EXAMPLE |
763 |
|
|
764 |
## Print field list |
## Print field list |
766 |
use Message::Header; |
use Message::Header; |
767 |
my $header = Message::Header->parse ($header); |
my $header = Message::Header->parse ($header); |
768 |
|
|
|
## Next sample is better. |
|
|
#for my $field (@$header) { |
|
|
# print $field->{name}, "\t=> ", $field->{body}, "\n"; |
|
|
#} |
|
|
|
|
769 |
for my $i (0..$#$header) { |
for my $i (0..$#$header) { |
770 |
print $header->field_name ($i), "\t=> ", $header->field_body ($i), "\n"; |
print $header->field_name ($i), "\t=> ", $header->field_body ($i), "\n"; |
771 |
} |
} |
788 |
$header->add ('References' => '<hoge.msgid%foo@foo.example>'); |
$header->add ('References' => '<hoge.msgid%foo@foo.example>'); |
789 |
print $header; |
print $header; |
790 |
|
|
791 |
|
=head1 ACKNOWLEDGEMENTS |
792 |
|
|
793 |
|
Some of codes are taken from other modules such as |
794 |
|
HTTP::Header, Mail::Header. |
795 |
|
|
796 |
=head1 LICENSE |
=head1 LICENSE |
797 |
|
|
798 |
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>. |
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>. |