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