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 |
|
require Message::Field::Structured; ## This may seem silly:-) |
13 |
|
push @ISA, qw(Message::Field::Structured); |
14 |
|
|
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_uri2package; ## namespace URI -> Package name |
26 |
|
our %NS_uri2phpackage; ## namespace URI -> PH-Package name |
27 |
|
require Message::Header::Default; ## Default namespace |
28 |
|
|
29 |
|
## Initialize of this class -- called by constructors |
30 |
|
%DEFAULT = ( |
31 |
|
-_HASH_NAME => 'value', |
32 |
|
-_METHODS => [qw|field field_exist field_type add replace count delete subject id is|], |
33 |
|
-_MEMBERS => [qw|value|], |
34 |
|
-_VALTYPE_DEFAULT => ':default', |
35 |
|
-by => 'name', |
36 |
|
-field_format_pattern => '%s: %s', |
37 |
|
-field_name_case_sensible => 0, |
38 |
|
-field_name_unsafe_rule => 'NON_ftext', |
39 |
|
-field_name_validation => 0, |
40 |
|
-field_sort => 0, |
41 |
|
-format => 'mail-rfc2822', |
42 |
|
-header_default_charset => 'iso-2022-int-1', |
43 |
|
-header_default_charset_input => 'iso-2022-int-1', |
44 |
|
-hook_init_fill_options => sub {}, |
45 |
|
-hook_stringify_fill_fields => sub {}, |
46 |
|
-linebreak_strict => 0, |
47 |
|
-line_length_max => 60, ## For folding |
48 |
|
#ns_default_phuri |
49 |
|
-output_bcc => 0, |
50 |
|
-output_folding => 1, |
51 |
|
-output_mail_from => 0, |
52 |
|
#parse_all |
53 |
|
-translate_underscore => 1, |
54 |
|
#uri_mailto_safe |
55 |
|
-uri_mailto_safe_level => 4, |
56 |
|
-use_folding => 1, |
57 |
|
#value_type |
58 |
|
); |
59 |
|
|
60 |
use overload '@{}' => sub {shift->_delete_empty_field()->{field}}, |
## taken from L<HTTP::Header> |
61 |
'""' => sub {shift->stringify}; |
# "Good Practice" order of HTTP message headers: |
62 |
|
# - General-Headers |
63 |
|
# - Request-Headers |
64 |
|
# - Response-Headers |
65 |
|
# - Entity-Headers |
66 |
|
# (From draft-ietf-http-v11-spec-rev-01, Nov 21, 1997) |
67 |
|
my @header_order = qw( |
68 |
|
mail-from x-envelope-from relay-version path status |
69 |
|
|
70 |
|
cache-control connection date pragma transfer-encoding upgrade trailer via |
71 |
|
|
72 |
|
accept accept-charset accept-encoding accept-language |
73 |
|
authorization expect from host |
74 |
|
if-modified-since if-match if-none-match if-range if-unmodified-since |
75 |
|
max-forwards proxy-authorization range referer te user-agent |
76 |
|
|
77 |
|
accept-ranges age location proxy-authenticate retry-after server vary |
78 |
|
warning www-authenticate |
79 |
|
|
80 |
|
mime-version |
81 |
|
allow content-base content-encoding content-language content-length |
82 |
|
content-location content-md5 content-range content-type |
83 |
|
etag expires last-modified content-style-type content-script-type |
84 |
|
link |
85 |
|
|
86 |
$REG{WSP} = qr/[\x09\x20]/; |
xref |
87 |
$REG{FWS} = qr/[\x09\x20]*/; |
); |
88 |
$REG{M_field} = qr/^([^\x3A]+):$REG{FWS}([\x00-\xFF]*)$/; |
my %header_order; |
89 |
$REG{M_fromline} = qr/^\x3E?From$REG{WSP}+([\x00-\xFF]*)$/; |
|
90 |
$REG{UNSAFE_field_name} = qr/[\x00-\x20\x3A\x7F-\xFF]/; |
=head1 CONSTRUCTORS |
91 |
|
|
92 |
=head2 options |
The following methods construct new C<Message::Header> objects: |
93 |
|
|
94 |
These options can be getten/set by C<get_option>/C<set_option> |
=over 4 |
|
method. |
|
95 |
|
|
96 |
=head3 capitalize = 0/1 |
=cut |
97 |
|
|
98 |
|
sub _init ($;%) { |
99 |
|
my $self = shift; |
100 |
|
my %options = @_; |
101 |
|
my $DEFAULT = Message::Util::make_clone (\%DEFAULT); |
102 |
|
$self->SUPER::_init (%$DEFAULT, %options); |
103 |
|
$self->{value} = []; |
104 |
|
$self->_ns_load_ph ('default'); |
105 |
|
$self->_ns_load_ph ('x-rfc822'); |
106 |
|
$self->_ns_load_ph ('x-http'); |
107 |
|
$self->{option}->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-rfc822'} |
108 |
|
unless $self->{option}->{ns_default_phuri}; |
109 |
|
|
110 |
|
## For text/rfc822-headers |
111 |
|
if (ref $options{entity_header}) { |
112 |
|
$self->{entity_header} = $options{entity_header}; |
113 |
|
delete $options{entity_header}; |
114 |
|
} |
115 |
|
my @new_fields = (); |
116 |
|
for my $name (keys %options) { |
117 |
|
unless (substr ($name, 0, 1) eq '-') { |
118 |
|
push @new_fields, ($name => $options{$name}); |
119 |
|
} |
120 |
|
} |
121 |
|
&{ $self->{option}->{hook_init_fill_options} } ($self, $self->{option}); |
122 |
|
$self->_init_by_format ($self->{option}->{format}, $self->{option}); |
123 |
|
# Make alternative representations of @header_order. This is used |
124 |
|
# for sorting. |
125 |
|
my $i = 1; |
126 |
|
for (@header_order) { |
127 |
|
$header_order{$_} = $i++ unless $header_order{$_}; |
128 |
|
} |
129 |
|
|
130 |
|
$self->add (@new_fields, -parse => $self->{option}->{parse_all}) |
131 |
|
if $#new_fields > -1; |
132 |
|
} |
133 |
|
|
134 |
(First character of) C<field-name> is capitalized |
sub _init_by_format ($$\%) { |
135 |
when C<stringify>. (Default = 1) |
my $self = shift; |
136 |
|
my ($format, $option) = @_; |
137 |
|
if ($format =~ /http/) { |
138 |
|
$option->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-http'}; |
139 |
|
if ($format =~ /cgi/) { |
140 |
|
unshift @header_order, qw(content-type location); |
141 |
|
$option->{field_sort} = 'good-practice'; |
142 |
|
$option->{use_folding} = 0; |
143 |
|
} else { |
144 |
|
$option->{field_sort} = 'good-practice'; |
145 |
|
} |
146 |
|
} elsif ($format =~ /mail|news/) { ## RFC 822 |
147 |
|
$option->{ns_default_phuri} = $self->{ns}->{phname2uri}->{'x-rfc822'}; |
148 |
|
} |
149 |
|
if ($format =~ /uri-url-mailto/) { |
150 |
|
$option->{output_bcc} = 0; |
151 |
|
$option->{field_format_pattern} = '%s=%s'; |
152 |
|
$option->{output_folding} = sub { |
153 |
|
$_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge; |
154 |
|
$_[1]; |
155 |
|
}; ## Yes, this is not folding! |
156 |
|
} |
157 |
|
} |
158 |
|
|
159 |
=head3 fold_length = numeric value |
=item $msg = Message::Header->new ([%initial-fields/options]) |
160 |
|
|
161 |
Length of line used to fold. (Default = 70) |
Constructs a new C<Message::Headers> object. You might pass some initial |
162 |
|
C<field-name>-C<field-body> pairs and/or options as parameters to the constructor. |
163 |
|
|
164 |
=head3 mail_from = 0/1 |
Example: |
165 |
|
|
166 |
Outputs "From " line (known as Un*x From, Mail-From, and so on) |
$hdr = new Message::Headers |
167 |
when C<stringify>. (Default = 0) |
Date => 'Thu, 03 Feb 1994 00:00:00 +0000', |
168 |
|
Content_Type => 'text/html', |
169 |
|
Content_Location => 'http://www.foo.example/', |
170 |
|
-format => 'mail-rfc2822' ## not to be header field |
171 |
|
; |
172 |
|
|
173 |
=cut |
=cut |
174 |
|
|
175 |
%DEFAULT = ( |
## Inherited |
|
capitalize => 1, |
|
|
fold_length => 70, |
|
|
mail_from => 0, |
|
|
); |
|
176 |
|
|
177 |
=head2 Message::Header->new ([%option]) |
=item $msg = Message::Header->parse ($header, [%initial-fields/options]) |
178 |
|
|
179 |
Returns new Message::Header instance. Some options can be |
Parses given C<header> and constructs a new C<Message::Headers> |
180 |
specified as hash. |
object. You might pass some additional C<field-name>-C<field-body> pairs |
181 |
|
or/and initial options as parameters to the constructor. |
182 |
|
|
183 |
=cut |
=cut |
184 |
|
|
185 |
sub new ($;%) { |
sub parse ($$;%) { |
186 |
my $class = shift; |
my $class = shift; |
187 |
my $self = bless {option => {@_}}, $class; |
my $header = shift; |
188 |
for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}} |
my $self = bless {}, $class; |
189 |
|
$self->_init (@_); |
190 |
|
if ($self->{option}->{linebreak_strict}) { |
191 |
|
$header =~ s/\x0D\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding}; |
192 |
|
} else { |
193 |
|
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding}; |
194 |
|
} |
195 |
|
my %option = (%{ $self->{option} }); |
196 |
|
$option{parse_all} = 0; |
197 |
|
for my $field (split /\x0D?\x0A/, $header) { |
198 |
|
if ($field =~ /$REG{M_fromline}/) { |
199 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
200 |
|
('mail-from' => $1, \%option); |
201 |
|
push @{$self->{value}}, $value if $s; |
202 |
|
} elsif ($field =~ /$REG{M_field}/) { |
203 |
|
my ($name, $body) = ($1, $2); |
204 |
|
$body =~ s/$REG{WSP}+$//; |
205 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
206 |
|
($name => $body, \%option); |
207 |
|
push @{$self->{value}}, $value if $s; |
208 |
|
} elsif (length $field) { |
209 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
210 |
|
('x-unknown' => $field, \%option); |
211 |
|
push @{$self->{value}}, $value if $s; |
212 |
|
} |
213 |
|
} |
214 |
|
$self->_ns_associate_numerical_prefix; ## RFC 2774 namespace |
215 |
|
for (@{ $self->{value} }) { |
216 |
|
no strict 'refs'; |
217 |
|
$_->{name} |
218 |
|
= &{ ${ &_NS_uri2package ($_->{ns}).'::OPTION' }{n11n_name} } |
219 |
|
($self, &_NS_uri2package ($_->{ns}), $_->{name}); |
220 |
|
$_->{body} = $self->_parse_value ($_->{name} => $_->{body}, ns => $_->{ns}) |
221 |
|
if $self->{option}->{parse_all}; |
222 |
|
} |
223 |
$self; |
$self; |
224 |
} |
} |
225 |
|
|
226 |
=head2 Message::Header->parse ($header, [%option]) |
=item $msg = Message::Header->parse_array (\@header, [%initial-fields/options]) |
227 |
|
|
228 |
Parses given C<header> and return a new Message::Header |
Parses given C<header> and constructs a new C<Message::Headers> |
229 |
object. Some options can be specified as hash. |
object. Same as C<Message::Header-E<lt>parse> but this method |
230 |
|
is given an array reference. You might pass some additional |
231 |
|
C<field-name>-C<field-body> pairs or/and initial options |
232 |
|
as parameters to the constructor. |
233 |
|
|
234 |
=cut |
=cut |
235 |
|
|
236 |
sub parse ($$;%) { |
sub parse_array ($\@;%) { |
237 |
my $class = shift; |
my $class = shift; |
238 |
my $header = shift; |
my $header = shift; |
239 |
my $self = bless {option => {@_}}, $class; |
Carp::croak "parse_array: first argument is not an array reference" |
240 |
for (keys %DEFAULT) {$self->{option}->{$_} ||= $DEFAULT{$_}} |
unless ref $header eq 'ARRAY'; |
241 |
$header =~ s/\x0D?\x0A$REG{WSP}+/\x20/gos; ## unfold |
my $self = bless {}, $class; |
242 |
for my $field (split /\x0D?\x0A/, $header) { |
$self->_init (@_); |
243 |
|
while (1) { |
244 |
|
my $field = shift @$header; |
245 |
|
if ($self->{option}->{use_folding}) { |
246 |
|
while (1) { |
247 |
|
if ($$header[0] =~ /^$REG{WSP}/) { |
248 |
|
$field .= shift @$header; |
249 |
|
} else {last} |
250 |
|
} |
251 |
|
} |
252 |
|
if ($self->{option}->{linebreak_strict}) { |
253 |
|
$field =~ s/\x0D\x0A//g; |
254 |
|
} else { |
255 |
|
$field =~ tr/\x0D\x0A//d; |
256 |
|
} |
257 |
|
local $self->{option}->{parse} = $self->{option}->{parse_all}; |
258 |
if ($field =~ /$REG{M_fromline}/) { |
if ($field =~ /$REG{M_fromline}/) { |
259 |
push @{$self->{field}}, {name => 'mail-from', body => $1}; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
260 |
|
('mail-from' => $1, $self->{option}); |
261 |
|
push @{$self->{value}}, $value if $s; |
262 |
} elsif ($field =~ /$REG{M_field}/) { |
} elsif ($field =~ /$REG{M_field}/) { |
263 |
my ($name, $body) = ($1, $2); |
my ($name, $body) = ($self->_n11n_field_name ($1), $2); |
|
$name =~ s/$REG{WSP}+$//; |
|
264 |
$body =~ s/$REG{WSP}+$//; |
$body =~ s/$REG{WSP}+$//; |
265 |
push @{$self->{field}}, {name => lc $name, body => $body}; |
my ($s,undef,$value) = $self->_value_to_arrayitem |
266 |
|
($name => $body, $self->{option}); |
267 |
|
push @{$self->{value}}, $value if $s; |
268 |
|
} elsif (length $field) { |
269 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
270 |
|
('x-unknown' => $field, $self->{option}); |
271 |
|
push @{$self->{value}}, $value if $s; |
272 |
} |
} |
273 |
|
last if $#$header < 0; |
274 |
} |
} |
275 |
|
$self->_ns_associate_numerical_prefix; ## RFC 2774 namespace |
276 |
$self; |
$self; |
277 |
} |
} |
278 |
|
|
279 |
|
=back |
280 |
|
|
281 |
|
=head1 METHODS |
282 |
|
|
283 |
=head2 $self->field ($field_name) |
=head2 $self->field ($field_name) |
284 |
|
|
285 |
Returns C<field-body> of given C<field-name>. |
Returns C<field-body> of given C<field-name>. |
289 |
|
|
290 |
=cut |
=cut |
291 |
|
|
292 |
sub field ($$) { |
sub field ($@) {shift->SUPER::item (@_)} |
293 |
|
sub field_exist ($@) {shift->SUPER::item_exist (@_)} |
294 |
|
|
295 |
|
## item-by?, \$checked-item, {item-key => 1}, \%option |
296 |
|
sub _item_match ($$\$\%\%) { |
297 |
my $self = shift; |
my $self = shift; |
298 |
my $name = lc shift; |
my ($by, $i, $list, $option) = @_; |
299 |
my @ret; |
return 0 unless ref $$i; ## Already removed |
300 |
for my $field (@{$self->{field}}) { |
if ($by eq 'name') { |
301 |
if ($field->{name} eq $name) { |
my %o = %$option; #$o{parse} = 0; |
302 |
unless (wantarray) { |
my %l; |
303 |
return $field->{body}; |
for (keys %$list) { |
304 |
|
my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', \%o); |
305 |
|
if ($s) { |
306 |
|
$l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1; |
307 |
} else { |
} else { |
308 |
push @ret, $field->{body}; |
$l{$v->{name} .':'. ( $option->{ns} || $self->{option}->{ns_default_phuri} ) } = 1; |
309 |
|
} |
310 |
|
} |
311 |
|
return 1 if $l{$$i->{name} . ':' . $$i->{ns}}; |
312 |
|
} elsif ($by eq 'ns') { |
313 |
|
return 1 if $list->{ $$i->{ns} }; |
314 |
|
} elsif ($by eq 'http-ns-define') { |
315 |
|
if ($$i->{ns} eq $self->{ns}->{phname2uri}->{'x-http'} |
316 |
|
|| $$i->{ns} eq $self->{ns}->{phname2uri}->{'x-http-c'}) { |
317 |
|
my $n = $$i->{name}; |
318 |
|
if ($n eq 'opt' || $n eq 'c-opt' || $n eq 'man' || $n eq 'c-man') { |
319 |
|
$option->{parse} = 0; |
320 |
|
$$i->{body} = $self->_parse_value ($$i->{name} => $$i->{body}, ns => $$i->{ns}); |
321 |
|
for my $j (0..$$i->{body}->count-1) { |
322 |
|
return 1 if $list->{ ($$i->{body}->value ($j))[0]->value }; |
323 |
|
} |
324 |
} |
} |
325 |
} |
} |
326 |
} |
} |
327 |
@ret; |
0; |
328 |
} |
} |
329 |
|
*_delete_match = \&_item_match; |
330 |
|
|
331 |
=head2 $self->field_name ($index) |
## Returns returned item value \$item-value, \%option |
332 |
|
sub _item_return_value ($\$\%) { |
333 |
Returns C<field-name> of $index'th C<field>. |
if (ref ${$_[1]}->{body}) { |
334 |
|
${$_[1]}->{body}; |
335 |
|
} else { |
336 |
|
${$_[1]}->{body} = $_[0]->_parse_value (${$_[1]}->{name} => ${$_[1]}->{body}, |
337 |
|
ns => ${$_[1]}->{ns}); |
338 |
|
${$_[1]}->{body}; |
339 |
|
} |
340 |
|
} |
341 |
|
*_add_return_value = \&_item_return_value; |
342 |
|
*_replace_return_value = \&_item_return_value; |
343 |
|
|
344 |
=head2 $self->field_body ($index) |
## Returns returned (new created) item value $name, \%option |
345 |
|
sub _item_new_value ($$\%) { |
346 |
|
my $self = shift; |
347 |
|
my ($name, $option) = @_; |
348 |
|
if ($option->{by} eq 'http-ns-define') { |
349 |
|
my $value = $self->_parse_value (opt => '', ns => $self->{ns}->{phname2uri}->{'x-http'}); |
350 |
|
($value->value (0))[0]->value ($name); |
351 |
|
{name => 'opt', body => $value, ns => $self->{ns}->{phname2uri}->{'x-http'}}; |
352 |
|
} else { |
353 |
|
my ($s,undef,$value) = $self->_value_to_arrayitem |
354 |
|
($name => '', $option); |
355 |
|
$s? $value: undef; |
356 |
|
} |
357 |
|
} |
358 |
|
|
|
Returns C<field-body> of $index'th C<field>. |
|
359 |
|
|
|
=cut |
|
360 |
|
|
361 |
sub field_name ($$) { |
## $self->_parse_value ($type, $value, %options); |
362 |
|
sub _parse_value ($$$;%) { |
363 |
my $self = shift; |
my $self = shift; |
364 |
$self->{field}->[shift]->{name}; |
my $name = shift ;#|| $self->{option}->{_VALTYPE_DEFAULT}; |
365 |
|
my $value = shift; return $value if ref $value; |
366 |
|
my %option = @_; |
367 |
|
my $vtype; { no strict 'refs'; |
368 |
|
my $vt = ${&_NS_uri2package ($option{ns}).'::OPTION'}{value_type}; |
369 |
|
if (ref $vt) { |
370 |
|
$vtype = $vt->{$name} || $vt->{$self->{option}->{_VALTYPE_DEFAULT}}; |
371 |
|
} |
372 |
|
## For compatiblity. |
373 |
|
unless (ref $vtype) { $vtype = $self->{option}->{value_type}->{$name} |
374 |
|
|| $self->{option}->{value_type}->{$self->{option}->{_VALTYPE_DEFAULT}} } |
375 |
|
} |
376 |
|
my $vpackage = $vtype->[0]; |
377 |
|
my %vopt = %{$vtype->[1]} if ref $vtype->[1]; |
378 |
|
if ($vpackage eq ':none:') { |
379 |
|
return $value; |
380 |
|
} elsif (length $value) { |
381 |
|
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
382 |
|
return $vpackage->parse ($value, |
383 |
|
-format => $self->{option}->{format}, |
384 |
|
-field_ns => $option{ns}, |
385 |
|
-field_name => $name, |
386 |
|
-header_default_charset => $self->{option}->{header_default_charset}, |
387 |
|
-header_default_charset_input => $self->{option}->{header_default_charset_input}, |
388 |
|
-parse_all => $self->{option}->{parse_all}, |
389 |
|
%vopt); |
390 |
|
} else { |
391 |
|
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@}; |
392 |
|
return $vpackage->new ( |
393 |
|
-format => $self->{option}->{format}, |
394 |
|
-field_ns => $option{ns}, |
395 |
|
-field_name => $name, |
396 |
|
-header_default_charset => $self->{option}->{header_default_charset}, |
397 |
|
-header_default_charset_input => $self->{option}->{header_default_charset_input}, |
398 |
|
-parse_all => $self->{option}->{parse_all}, |
399 |
|
%vopt); |
400 |
|
} |
401 |
} |
} |
402 |
sub field_body ($$) { |
|
403 |
|
## Defined for text/rfc822-headers |
404 |
|
sub entity_header ($;$) { |
405 |
my $self = shift; |
my $self = shift; |
406 |
$self->{field}->[shift]->{body}; |
my $new_header = shift; |
407 |
|
if (ref $new_header) { |
408 |
|
$self->{header} = $new_header; |
409 |
|
} |
410 |
|
$self->{header}; |
411 |
} |
} |
412 |
|
|
413 |
=head2 $self->field_name_list () |
=head2 $self->field_name_list () |
420 |
|
|
421 |
sub field_name_list ($) { |
sub field_name_list ($) { |
422 |
my $self = shift; |
my $self = shift; |
423 |
$self->_delete_empty_field (); |
$self->_delete_empty (); |
424 |
map {$_->{name}} @{$self->{field}}; |
map { $_->{name} . ':' . $_->{ns} } @{$self->{value}}; |
425 |
|
} |
426 |
|
|
427 |
|
sub namespace_ph_default ($;$) { |
428 |
|
my $self = shift; |
429 |
|
if (defined $_[0]) { |
430 |
|
no strict 'refs'; |
431 |
|
$self->{option}->{ns_default_phuri} = $_[0]; |
432 |
|
$self->_ns_load_ph (${&_NS_uri2package ($self->{option}->{ns_default_phuri}).'::OPTION'}{namespace_phname}); |
433 |
|
} |
434 |
|
$self->{option}->{ns_default_phuri}; |
435 |
} |
} |
436 |
|
|
437 |
=head2 $self->add ($field_name, $field_body) |
=item $hdr->add ($field-name, $field-body, [$name, $body, ...]) |
438 |
|
|
439 |
|
Adds some field name/body pairs. Even if there are |
440 |
|
one or more fields named given C<$field-name>, |
441 |
|
given name/body pairs are ADDed. Use C<replace> |
442 |
|
to remove same-name-fields. |
443 |
|
|
444 |
|
Instead of field name-body pair, you might pass some options. |
445 |
|
Four options are available for this method. |
446 |
|
|
447 |
Adds an new C<field>. It is not checked whether |
C<-parse>: Parses and validates C<field-body>, and returns |
448 |
the field which named $field_body is already exist or not. |
C<field-body> object. (When multiple C<field-body>s are |
449 |
If you don't want duplicated C<field>s, use C<replace> method. |
added, returns only last one.) (Default: C<defined wantarray>) |
450 |
|
|
451 |
|
C<-prepend>: New fields are not appended, |
452 |
|
but prepended to current fields. (Default: C<0>) |
453 |
|
|
454 |
|
C<-translate-underscore>: Do C<field-name> =~ tr/_/-/. (Default: C<1>) |
455 |
|
|
456 |
|
C<-validate>: Checks whether C<field-name> is valid or not. |
457 |
|
|
458 |
=cut |
=cut |
459 |
|
|
460 |
sub add ($$$) { |
## [Name: Value] pair -> internal array item |
461 |
|
## $self->_value_to_arrayitem ($name => $value, {%options}) |
462 |
|
## or |
463 |
|
## $self->_value_to_arrayitem ($name => [$value, %value_options], {%options}) |
464 |
|
## |
465 |
|
## Return: ((1 = success / 0 = failue), $full_name, $arrayitem) |
466 |
|
sub _value_to_arrayitem ($$$\%) { |
467 |
my $self = shift; |
my $self = shift; |
468 |
my ($name, $body) = (lc shift, shift); |
my ($name, $value, $option) = @_; |
469 |
return 0 if $name =~ /$REG{UNSAFE_field_name}/; |
my $value_option = {}; |
470 |
push @{$self->{field}}, {name => $name, body => $body}; |
if (ref $value eq 'ARRAY') { |
471 |
$self; |
($value, %$value_option) = @$value; |
472 |
|
} |
473 |
|
my $default_ns = $option->{ns_default_phuri}; |
474 |
|
my $nsuri = $default_ns; |
475 |
|
$name =~ s/^$REG{WSP}+//; $name =~ s/$REG{WSP}+$//; |
476 |
|
|
477 |
|
no strict 'refs'; |
478 |
|
if ($value_option->{ns}) { |
479 |
|
$nsuri = $value_option->{ns}; |
480 |
|
} elsif ($option->{ns}) { |
481 |
|
$nsuri = $option->{ns}; |
482 |
|
} elsif (($default_ns eq $self->{ns}->{uri2phname}->{'x-http'} |
483 |
|
&& $name =~ s/^([0-9]+)-//) |
484 |
|
|| ($name =~ s/^x-http-([0-9]+)-//i)) { ## Numric namespace prefix, RFC 2774 |
485 |
|
my $prefix = 0+$1; |
486 |
|
$nsuri = $self->{ns}->{number2uri}->{ $prefix }; |
487 |
|
unless ($nsuri) { |
488 |
|
$self->{ns}->{number2uri}->{ $prefix } = 'urn:x-suika-fam-cx:msgpm:header:x-temp:'.$prefix; |
489 |
|
$nsuri = $self->{ns}->{number2uri}->{ $prefix }; |
490 |
|
} |
491 |
|
} elsif ( |
492 |
|
${ &_NS_uri2package ($default_ns).'::OPTION' }{use_ph_namespace} |
493 |
|
&& ( |
494 |
|
($name =~ s/^([Xx]-[A-Za-z0-9]+|[A-Za-z]*[A-WYZa-wyz0-9][A-Za-z0-9]*)- |
495 |
|
([Xx]-[A-Za-z0-9]+|[A-Za-z0-9]*[A-WYZa-wyz0-9][A-Za-z0-9]*)-//x) |
496 |
|
|| $name =~ s/^([Xx]-[A-Za-z0-9]+|[A-Za-z0-9]*[A-WYZa-wyz0-9][A-Za-z0-9]*)-// |
497 |
|
)) { |
498 |
|
my ($prefix1, $prefix2) = ($1, $2); |
499 |
|
my $original_prefix = $&; my $one_prefix = 0; |
500 |
|
unless ($prefix2) { |
501 |
|
$prefix2 = $prefix1; |
502 |
|
$prefix1 = $self->{ns}->{uri2phname}->{ $default_ns }; |
503 |
|
$one_prefix = 1; |
504 |
|
} |
505 |
|
my $prefix |
506 |
|
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_prefix} } |
507 |
|
($self, &_NS_uri2package ($nsuri), $prefix1.'-'.$prefix2); |
508 |
|
$self->_ns_load_ph ($prefix); |
509 |
|
$nsuri = $self->{ns}->{phname2uri}->{ $prefix }; |
510 |
|
unless ($nsuri) { |
511 |
|
$nsuri = $default_ns; |
512 |
|
$prefix |
513 |
|
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_prefix} } |
514 |
|
($self, &_NS_uri2package ($nsuri), $one_prefix? $prefix2: $prefix1); |
515 |
|
$self->_ns_load_ph ($prefix); |
516 |
|
$nsuri = $self->{ns}->{phname2uri}->{ $prefix }; |
517 |
|
if ($nsuri) { |
518 |
|
$name = $prefix2 . '-' . $name unless $one_prefix; |
519 |
|
} else { |
520 |
|
$name = $original_prefix . $name; |
521 |
|
$nsuri = $default_ns; |
522 |
|
} |
523 |
|
} |
524 |
|
} |
525 |
|
$name |
526 |
|
= &{ ${ &_NS_uri2package ($nsuri).'::OPTION' }{n11n_name} } |
527 |
|
($self, &_NS_uri2package ($nsuri), $name); |
528 |
|
Carp::croak "$name: invalid field-name" |
529 |
|
if $option->{field_name_validation} |
530 |
|
&& $name =~ /$REG{ $option->{field_name_unsafe_rule} }/; |
531 |
|
$value = $self->_parse_value ($name => $value, ns => $nsuri) |
532 |
|
if $option->{parse} || $option->{parse_all}; |
533 |
|
$option->{parse} = 0; |
534 |
|
(1, $name.':'.$nsuri => {name => $name, body => $value, ns => $nsuri}); |
535 |
} |
} |
536 |
|
*_add_hash_check = \&_value_to_arrayitem; |
537 |
|
*_replace_hash_check = \&_value_to_arrayitem; |
538 |
|
|
539 |
=head2 $self->relace ($field_name, $field_body) |
=head2 $self->relace ($field_name, $field_body) |
540 |
|
|
546 |
|
|
547 |
=cut |
=cut |
548 |
|
|
549 |
sub replace ($$$) { |
sub _replace_hash_shift ($\%$\%) { |
550 |
my $self = shift; |
shift; my $r = shift; my $n = $_[0]->{name} . ':' . $_[0]->{ns}; |
551 |
my ($name, $body) = (lc shift, shift); |
if ($$r{$n}) { |
552 |
return 0 if $name =~ /$REG{UNSAFE_field_name}/; |
my $d = $$r{$n}; |
553 |
for my $field (@{$self->{field}}) { |
delete $$r{$n}; |
554 |
if ($field->{name} eq $name) { |
return $d; |
|
$field->{body} = $body; |
|
|
return $self; |
|
|
} |
|
555 |
} |
} |
556 |
push @{$self->{field}}, {name => $name, body => $body}; |
undef; |
|
$self; |
|
557 |
} |
} |
558 |
|
|
559 |
=head2 $self->delete ($field_name, [$index]) |
=head2 $self->delete ($field-name, [$name, ...]) |
560 |
|
|
561 |
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. |
|
|
If not, ($index == 0), all C<field>s that have the C<field-name> |
|
|
$field_name are deleted. |
|
562 |
|
|
563 |
=cut |
=cut |
564 |
|
|
|
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; |
|
|
} |
|
565 |
|
|
566 |
=head2 $self->count ([$field_name]) |
=head2 $self->count ([$field_name]) |
567 |
|
|
571 |
|
|
572 |
=cut |
=cut |
573 |
|
|
574 |
sub count ($;$) { |
sub _count_by_name ($$\%) { |
575 |
my $self = shift; |
my $self = shift; |
576 |
my ($name) = (lc shift); |
my ($array, $option) = @_; |
577 |
unless ($name) { |
my $name = $self->_n11n_field_name ($$option{-name}); |
578 |
$self->_delete_empty_field (); |
my @a = grep {$_->{name} eq $name} @{$self->{$array}}; |
579 |
return $#{$self->{field}}+1; |
$#a + 1; |
|
} |
|
|
my $count = 0; |
|
|
for my $field (@{$self->{field}}) { |
|
|
if ($field->{name} eq $name) { |
|
|
$count++; |
|
|
} |
|
|
} |
|
|
$count; |
|
580 |
} |
} |
581 |
|
|
582 |
=head2 $self->stringify ([%option]) |
## Delete empty items |
583 |
|
sub _delete_empty ($) { |
584 |
|
my $self = shift; |
585 |
|
my $array = $self->{option}->{_HASH_NAME}; |
586 |
|
$self->{$array} = [grep {ref $_ && length $_->{name}} @{$self->{$array}}]; |
587 |
|
} |
588 |
|
|
589 |
Returns the C<header> as a string. |
=head2 $self->rename ($field-name, $new-name, [$old, $new,...]) |
590 |
|
|
591 |
|
Renames C<$field-name> as C<$new-name>. |
592 |
|
|
593 |
=cut |
=cut |
594 |
|
|
595 |
sub stringify ($;%) { |
sub rename ($%) { |
596 |
my $self = shift; |
my $self = shift; |
597 |
my %OPT = @_; |
my %params = @_; |
598 |
my @ret; |
my %option = %{$self->{option}}; |
599 |
$OPT{capitalize} ||= $self->{option}->{capitalize}; |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
600 |
$OPT{mail_from} ||= $self->{option}->{mail_from}; |
my %new_name; |
601 |
push @ret, 'From '.$self->field ('mail-from') if $OPT{mail_from}; |
for (grep {/^[^-]/} keys %params) { |
602 |
for my $field (@{$self->{field}}) { |
my ($old => $new) |
603 |
my $name = $field->{name}; |
= ($self->_n11n_field_name ($_) => $self->_n11n_field_name ($params{$_})); |
604 |
next unless $field->{name}; |
$old =~ tr/_/-/ if $option{translate_underscore}; |
605 |
next if !$OPT{mail_from} && $name eq 'mail-from'; |
$new =~ tr/_/-/ if $option{translate_underscore}; |
606 |
$name =~ s/((?:^|-)[a-z])/uc($1)/ge if $OPT{capitalize}; |
Carp::croak "rename: $new: invalid field-name" |
607 |
push @ret, $name.': '.$self->fold ($field->{body}); |
if $option{field_name_validation} |
608 |
|
&& $new =~ /$REG{$option{field_name_unsafe_rule}}/; |
609 |
|
$new_name{$old} = $new; |
610 |
} |
} |
611 |
my $ret = join ("\n", @ret); |
for my $field (@{$self->{value}}) { |
612 |
$ret? $ret."\n": ""; |
if (length $new_name{$field->{name}}) { |
613 |
|
$field->{name} = $new_name{$field->{name}}; |
614 |
|
} |
615 |
|
} |
616 |
|
$self if defined wantarray; |
617 |
} |
} |
618 |
|
|
|
=head2 $self->get_option ($option_name) |
|
|
|
|
|
Returns value of the option. |
|
619 |
|
|
620 |
=head2 $self->set_option ($option_name, $option_value) |
=item $self->scan(\&doit) |
621 |
|
|
622 |
Set new value of the option. |
Apply a subroutine to each header field in turn. The callback routine is |
623 |
|
called with two parameters; the name of the field and a single value. |
624 |
|
If the header has more than one value, then the routine is called once |
625 |
|
for each value. |
626 |
|
|
627 |
=cut |
=cut |
628 |
|
|
629 |
sub get_option ($$) { |
sub _scan_sort ($\@\%) { |
630 |
my $self = shift; |
my $self = shift; |
631 |
my ($name) = @_; |
my ($array, $option) = @_; |
632 |
$self->{option}->{$name}; |
my $sort; |
633 |
|
$sort = \&_header_cmp if $option->{field_sort} eq 'good-practice'; |
634 |
|
$sort = {$a cmp $b} if $option->{field_sort} eq 'alphabetic'; |
635 |
|
return ( sort $sort @$array ) if ref $sort; |
636 |
|
@$array; |
637 |
} |
} |
638 |
sub set_option ($$$) { |
|
639 |
|
sub _n11n_field_name ($$) { |
640 |
|
no strict 'refs'; |
641 |
my $self = shift; |
my $self = shift; |
642 |
my ($name, $value) = @_; |
my $s = shift; |
643 |
$self->{option}->{$name} = $value; |
$s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//; |
644 |
$self; |
$s = lc $s unless ${&_NS_uri2package ($self->{option}->{ns_default_phuri}).'::OPTION'}{case_sensible}; |
645 |
|
$s; |
646 |
} |
} |
647 |
|
|
648 |
sub _delete_empty_field ($) { |
# Compare function which makes it easy to sort headers in the |
649 |
|
# recommended "Good Practice" order. |
650 |
|
## taken from HTTP::Header |
651 |
|
sub _header_cmp |
652 |
|
{ |
653 |
|
my ($na, $nb) = ($a->{name}, $b->{name}); |
654 |
|
# Unknown headers are assign a large value so that they are |
655 |
|
# sorted last. This also helps avoiding a warning from -w |
656 |
|
# about comparing undefined values. |
657 |
|
$header_order{$na} = 999 unless defined $header_order{$na}; |
658 |
|
$header_order{$nb} = 999 unless defined $header_order{$nb}; |
659 |
|
|
660 |
|
$header_order{$na} <=> $header_order{$nb} || $na cmp $nb; |
661 |
|
} |
662 |
|
|
663 |
|
=head2 $self->stringify ([%option]) |
664 |
|
|
665 |
|
Returns the C<header> as a string. |
666 |
|
|
667 |
|
=cut |
668 |
|
|
669 |
|
sub stringify ($;%) { |
670 |
my $self = shift; |
my $self = shift; |
671 |
|
my %params = @_; |
672 |
|
my %option = %{$self->{option}}; |
673 |
|
$option{format} = $params{-format} if $params{-format}; |
674 |
|
$self->_init_by_format ($option{format}, \%option) |
675 |
|
if $self->{option}->{format} ne $option{format}; |
676 |
|
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}} |
677 |
|
## Fill required fields |
678 |
|
my %exist; |
679 |
|
for ($self->field_name_list) {$exist{$_} = 1} |
680 |
|
&{ $option{hook_stringify_fill_fields} } ($self, \%exist, \%option); |
681 |
my @ret; |
my @ret; |
682 |
for my $field (@{$self->{field}}) { |
## RFC 2774 numerical field name prefix |
683 |
push @ret, $field if $field->{name}; |
my %nprefix; |
684 |
|
{no strict 'refs'; |
685 |
|
%nprefix = reverse %{ $self->{ns}->{number2uri} }; |
686 |
|
my $i = (sort { $a <=> $b } keys %{ $self->{ns}->{number2uri} })[-1] + 1; |
687 |
|
$i = 10 if $i < 10; |
688 |
|
my $hprefix = ${ &_NS_uri2package |
689 |
|
($self->{ns}->{phname2uri}->{'x-http'}) |
690 |
|
.'::OPTION' } {namespace_phname_goodcase}; |
691 |
|
for my $uri (keys %nprefix) { |
692 |
|
if ($nprefix{ $uri } < 10) { |
693 |
|
$nprefix{ $uri } = $i++; |
694 |
|
} |
695 |
|
my $nsfs = $self->item ($uri, -by => 'http-ns-define'); |
696 |
|
for my $i (0..$nsfs->count-1) { |
697 |
|
my $nsf = ($nsfs->value ($i))[0]; |
698 |
|
if ($nsf->value eq $uri) { |
699 |
|
$nsf->replace (ns => $nprefix{ $uri }); |
700 |
|
$nprefix{ $uri } = $hprefix . '-' . $nprefix{ $uri }; |
701 |
|
last; |
702 |
|
} |
703 |
|
} |
704 |
|
} |
705 |
|
} |
706 |
|
my $_stringify = sub { |
707 |
|
no strict 'refs'; |
708 |
|
my ($name, $body, $nsuri) = ($_[1]->{name}, $_[1]->{body}, $_[1]->{ns}); |
709 |
|
return unless length $name; |
710 |
|
return if $option{output_mail_from} && $name eq 'mail-from'; |
711 |
|
$body = '' if !$option{output_bcc} && $name eq 'bcc'; |
712 |
|
my $nspackage = &_NS_uri2package ($nsuri); |
713 |
|
my $oname; ## Outputed field-name |
714 |
|
my $prefix = $nprefix{ $nsuri } |
715 |
|
|| ${$nspackage.'::OPTION'} {namespace_phname_goodcase} |
716 |
|
|| $self->{ns}->{uri2phname}->{ $nsuri }; |
717 |
|
my $default_prefix = ${ &_NS_uri2package ($option{ns_default_phuri}) |
718 |
|
.'::OPTION'} {namespace_phname_goodcase}; |
719 |
|
$prefix = '' if $prefix eq $default_prefix; |
720 |
|
$prefix =~ s/^\Q$default_prefix\E-//; |
721 |
|
my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase}; |
722 |
|
if (ref $gc) { $oname = &$gc ($self, $nspackage, $name, \%option) } |
723 |
|
else { $oname = $name } |
724 |
|
if ($prefix) { $oname = $prefix . '-' . $oname } |
725 |
|
if ($option{format} =~ /uri-url-mailto/) { |
726 |
|
return if (( ${$nspackage.'::OPTION'} {uri_mailto_safe}->{$name} |
727 |
|
|| ${$nspackage.'::OPTION'} {uri_mailto_safe}->{':default'}) |
728 |
|
< $option{uri_mailto_safe_level}); |
729 |
|
if ($name eq 'to') { |
730 |
|
$body = $self->field ('to', -new_item_unless_exist => 0); |
731 |
|
if (ref $body && $body->have_group) { |
732 |
|
# |
733 |
|
} elsif (ref $body && $body->count > 1) { |
734 |
|
$body = $body->clone; |
735 |
|
$body->delete ({-by => 'index'}, 0); |
736 |
|
} |
737 |
|
} |
738 |
|
} |
739 |
|
my $fbody; |
740 |
|
if (ref $body) { |
741 |
|
$fbody = $body->stringify (-format => $option{format}); |
742 |
|
} else { |
743 |
|
$fbody = $body; |
744 |
|
} |
745 |
|
unless (${$nspackage.'::OPTION'} {field}->{$name}->{empty_body}) { |
746 |
|
return unless length $fbody; |
747 |
|
} |
748 |
|
unless ($option{linebreak_strict}) { |
749 |
|
## bare \x0D and bare \x0A are unsafe |
750 |
|
$fbody =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g; |
751 |
|
$fbody =~ s/\x0A(?=[^\x09\x20])/\x0A\x20/g; |
752 |
|
} else { |
753 |
|
$fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g; |
754 |
|
} |
755 |
|
if ($option{use_folding}) { |
756 |
|
if (ref $option{output_folding}) { |
757 |
|
$fbody = &{$option{output_folding}} ($self, $fbody, |
758 |
|
-initial_length => length ($oname) +2); |
759 |
|
} elsif ($option{output_folding}) { |
760 |
|
$fbody = $self->_fold ($fbody, -initial_length => length ($oname) +2); |
761 |
|
} |
762 |
|
} |
763 |
|
push @ret, sprintf $option{field_format_pattern}, $oname, $fbody; |
764 |
|
}; |
765 |
|
if ($option{format} =~ /uri-url-mailto/) { |
766 |
|
if ($option{format} =~ /uri-url-mailto-to/) { |
767 |
|
my $to = $self->field ('to', -new_item_unless_exist => 0); |
768 |
|
if ($to) { |
769 |
|
unless ($to->have_group) { |
770 |
|
my $fbody = $to->stringify (-format => $option{format}, -max => 1); |
771 |
|
return &{$option{output_folding}} ($self, $fbody); |
772 |
|
} |
773 |
|
} |
774 |
|
''; |
775 |
|
} elsif ($option{format} =~ /uri-url-mailto-rfc1738/) { |
776 |
|
my $to = $self->field ('to', -new_item_unless_exist => 0); |
777 |
|
if ($to) { |
778 |
|
my $fbody = $to->addr_spec (-format => $option{format}); |
779 |
|
return &{$option{output_folding}} ($self, $fbody); |
780 |
|
} |
781 |
|
''; |
782 |
|
} else { |
783 |
|
$self->scan ($_stringify); |
784 |
|
my $ret = join ('&', @ret); |
785 |
|
$ret; |
786 |
|
} |
787 |
|
} else { |
788 |
|
if ($option{output_mail_from}) { |
789 |
|
my $fromline = $self->field ('mail-from', -new_item_unless_exist => 0); |
790 |
|
push @ret, 'From '.$fromline if $fromline; |
791 |
|
} |
792 |
|
$self->scan ($_stringify); |
793 |
|
my $ret = join ("\x0D\x0A", @ret); |
794 |
|
$ret? $ret."\x0D\x0A": ''; |
795 |
|
} |
796 |
|
} |
797 |
|
*as_string = \&stringify; |
798 |
|
|
799 |
|
=head2 $self->option ($option_name, [$option_value]) |
800 |
|
|
801 |
|
Set/gets new value of the option. |
802 |
|
|
803 |
|
=cut |
804 |
|
|
805 |
|
sub option ($@) { |
806 |
|
my $self = shift; |
807 |
|
if (@_ == 1) { |
808 |
|
return $self->{option}->{ shift (@_) }; |
809 |
|
} |
810 |
|
while (my ($name, $value) = splice (@_, 0, 2)) { |
811 |
|
$self->{option}->{$name} = $value; |
812 |
|
if ($name eq 'format') { |
813 |
|
for my $f (@{$self->{field}}) { |
814 |
|
if (ref $f->{body}) { |
815 |
|
$f->{body}->option (-format => $value); |
816 |
|
} |
817 |
|
} |
818 |
|
} |
819 |
} |
} |
|
$self->{field} = \@ret; |
|
|
$self; |
|
820 |
} |
} |
821 |
|
|
822 |
sub fold ($$;$) { |
sub field_type ($@) {shift->SUPER::value_type (@_)} |
823 |
|
|
824 |
|
## $self->_fold ($string, %option = (-max, -initial_length(for field-name)) ) |
825 |
|
sub _fold ($$;%) { |
826 |
my $self = shift; |
my $self = shift; |
827 |
my $string = shift; |
my $string = shift; |
828 |
my $len = shift || $self->{option}->{fold_length}; |
my %option = @_; |
829 |
$len = 60 if $len < 60; |
my $max = $self->{option}->{line_length_max}; |
830 |
|
$max = 20 if $max < 20; |
831 |
|
|
832 |
## This code is taken from Mail::Header 1.43 in MailTools, |
my $l = $option{-initial_length} || 0; |
833 |
## by Graham Barr (Maintained by Mark Overmeer <mailtools@overmeer.net>). |
$l += length $1 if $string =~ /^([^\x09\x20]+)/; |
834 |
my $max = int($len - 5); # 4 for leading spcs + 1 for [\,\;] |
$string =~ s{([\x09\x20][^\x09\x20]+)}{ |
835 |
my $min = int($len * 4 / 5) - 4; |
my $s = $1; |
836 |
my $ml = $len; |
if (($l + length $s) > $max) { |
837 |
|
$s = "\x0D\x0A\x20" . $s; |
838 |
if (length($string) > $ml) { |
$l = 1 + length $s; |
839 |
#Split the line up |
} else { $l += length $s } |
840 |
# first bias towards splitting at a , or a ; >4/5 along the line |
$s; |
841 |
# next split a whitespace |
}gex; |
|
# else we are looking at a single word and probably don't want to split |
|
|
my $x = ""; |
|
|
$x .= "$1\n " |
|
|
while($string =~ s/^$REG{WSP}*( |
|
|
[^"]{$min,$max}?[\,\;] |
|
|
|[^"]{1,$max}$REG{WSP} |
|
|
|[^\s"]*(?:"[^"]*"[^\s"]*)+$REG{WSP} |
|
|
|[^\s"]+$REG{WSP} |
|
|
) |
|
|
//x); |
|
|
$x .= $string; |
|
|
$string = $x; |
|
|
$string =~ s/(\A$REG{WSP}+|$REG{WSP}+\Z)//sog; |
|
|
$string =~ s/\s+\n/\n/sog; |
|
|
} |
|
842 |
$string; |
$string; |
843 |
} |
} |
844 |
|
|
845 |
|
sub _ns_load_ph ($$) { |
846 |
|
my $self = shift; |
847 |
|
my $name = shift; ## normalized prefix (without HYPHEN-MINUS) |
848 |
|
return if $self->{ns}->{phname2uri}->{$name}; |
849 |
|
$self->{ns}->{phname2uri}->{$name} = $NS_phname2uri{$name}; |
850 |
|
return unless $self->{ns}->{phname2uri}->{$name}; |
851 |
|
$self->{ns}->{uri2phname}->{$self->{ns}->{phname2uri}->{$name}} = $name; |
852 |
|
} |
853 |
|
|
854 |
|
sub _ns_associate_numerical_prefix ($) { |
855 |
|
my $self = shift; |
856 |
|
$self->scan (sub {shift; |
857 |
|
my $f = shift; return unless $f->{name}; |
858 |
|
if ($f->{ns} eq $self->{ns}->{phname2uri}->{'x-http'} |
859 |
|
|| $f->{ns} eq $self->{ns}->{phname2uri}->{'x-http-c'}) { |
860 |
|
my $fn = $f->{name}; |
861 |
|
if ($fn eq 'opt' || $fn eq 'man') { |
862 |
|
$f->{body} = $self->_parse_value ($fn => $f->{body}, ns => $f->{ns}); |
863 |
|
for ($f->{body}->value (0..$f->{body}->count-1)) { |
864 |
|
my ($nsuri, $number) = ($_->value, $_->item ('ns')); |
865 |
|
if ($number && $nsuri) { |
866 |
|
$self->{ns}->{number2uri}->{ $number } = $nsuri; |
867 |
|
} |
868 |
|
} |
869 |
|
} |
870 |
|
} |
871 |
|
}); |
872 |
|
$self->scan (sub {shift; |
873 |
|
my $f = shift; |
874 |
|
if ($f->{ns} =~ /urn:x-suika-fam-cx:msgpm:header:x-temp:([0-9]+)$/ && $self->{ns}->{number2uri}->{ $1 }) { |
875 |
|
$f->{ns} = $self->{ns}->{number2uri}->{ $1 }; |
876 |
|
} |
877 |
|
}); |
878 |
|
} |
879 |
|
|
880 |
|
## $package_name = Message::Header::_NS_uri2phpackage ($nsuri) |
881 |
|
## (For internal use of Message::* modules) |
882 |
|
sub _NS_uri2phpackage ($) { |
883 |
|
$NS_uri2phpackage{$_[0]} |
884 |
|
|| $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}}; |
885 |
|
} |
886 |
|
sub _NS_uri2package ($) { |
887 |
|
$NS_uri2package{$_[0]} |
888 |
|
|| $NS_uri2phpackage{$_[0]} |
889 |
|
|| $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}}; |
890 |
|
} |
891 |
|
|
892 |
|
=head2 $self->clone () |
893 |
|
|
894 |
|
Returns a copy of Message::Header object. |
895 |
|
|
896 |
|
=cut |
897 |
|
|
898 |
|
## Inhreited |
899 |
|
|
900 |
|
=head1 NOTE |
901 |
|
|
902 |
|
=head2 C<field-name> |
903 |
|
|
904 |
|
The header field name is not case sensitive. To make the life |
905 |
|
easier for perl users who wants to avoid quoting before the => operator, |
906 |
|
you can use '_' as a synonym for '-' in header field names |
907 |
|
(this behaviour can be suppressed by setting |
908 |
|
C<translate_underscore> option to C<0> value). |
909 |
|
|
910 |
=head1 EXAMPLE |
=head1 EXAMPLE |
911 |
|
|
912 |
## Print field list |
## Print field list |
914 |
use Message::Header; |
use Message::Header; |
915 |
my $header = Message::Header->parse ($header); |
my $header = Message::Header->parse ($header); |
916 |
|
|
|
## Next sample is better. |
|
|
#for my $field (@$header) { |
|
|
# print $field->{name}, "\t=> ", $field->{body}, "\n"; |
|
|
#} |
|
|
|
|
917 |
for my $i (0..$#$header) { |
for my $i (0..$#$header) { |
918 |
print $header->field_name ($i), "\t=> ", $header->field_body ($i), "\n"; |
print $header->field_name ($i), "\t=> ", $header->field_body ($i), "\n"; |
919 |
} |
} |
936 |
$header->add ('References' => '<hoge.msgid%foo@foo.example>'); |
$header->add ('References' => '<hoge.msgid%foo@foo.example>'); |
937 |
print $header; |
print $header; |
938 |
|
|
939 |
|
=head1 ACKNOWLEDGEMENTS |
940 |
|
|
941 |
|
Some of codes are taken from other modules such as |
942 |
|
HTTP::Header, Mail::Header. |
943 |
|
|
944 |
=head1 LICENSE |
=head1 LICENSE |
945 |
|
|
946 |
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>. |
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>. |