1 |
wakaba |
1.1 |
|
2 |
|
|
=head1 NAME
|
3 |
|
|
|
4 |
wakaba |
1.20 |
Message::Header --- A Perl Module for Internet Message Headers
|
5 |
wakaba |
1.1 |
|
6 |
|
|
=cut
|
7 |
|
|
|
8 |
|
|
package Message::Header;
|
9 |
|
|
use strict;
|
10 |
wakaba |
1.20 |
use vars qw(%DEFAULT @ISA %REG $VERSION);
|
11 |
|
|
$VERSION=do{my @r=(q$Revision: 1.3 $=~/\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 |
|
|
-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_capitalize => 1,
|
38 |
|
|
-field_name_case_sensible => 0,
|
39 |
|
|
-field_name_unsafe_rule => 'NON_ftext',
|
40 |
|
|
-field_name_validation => 1, ## Method level option.
|
41 |
|
|
-field_sort => 0,
|
42 |
|
|
#-format => 'mail-rfc2822',
|
43 |
|
|
-linebreak_strict => 0, ## Not implemented completely
|
44 |
|
|
-line_length_max => 60, ## For folding
|
45 |
|
|
-ns_default_uri => $Message::Header::Default::OPTION{namespace_uri},
|
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 |
|
|
$DEFAULT{-uri_mailto_safe} = {
|
57 |
wakaba |
1.18 |
## 1 all (no check) 2 no trace & bcc & from
|
58 |
|
|
## 3 no sender's info 4 (default) (currently not used)
|
59 |
|
|
## 5 only a few
|
60 |
|
|
':default' => 4,
|
61 |
wakaba |
1.20 |
'cc' => 5,
|
62 |
wakaba |
1.18 |
'bcc' => 1,
|
63 |
|
|
'body' => 1,
|
64 |
|
|
'comment' => 5,
|
65 |
|
|
'content-id' => 1,
|
66 |
|
|
'date' => 1,
|
67 |
|
|
'from' => 1,
|
68 |
|
|
'keywords' => 5,
|
69 |
|
|
'list-id' => 1,
|
70 |
|
|
'mail-from' => 1,
|
71 |
|
|
'message-id' => 1,
|
72 |
|
|
'received' => 1,
|
73 |
|
|
'resent-bcc' => 1,
|
74 |
|
|
'resent-date' => 1,
|
75 |
|
|
'resent-from' => 1,
|
76 |
|
|
'resent-sender' => 1,
|
77 |
|
|
'return-path' => 1,
|
78 |
|
|
'sender' => 1,
|
79 |
|
|
'subject' => 5,
|
80 |
|
|
'summary' => 5,
|
81 |
wakaba |
1.20 |
'to' => 5,
|
82 |
wakaba |
1.18 |
'user-agent' => 3,
|
83 |
|
|
'x-face' => 2,
|
84 |
|
|
'x-mailer' => 3,
|
85 |
|
|
'x-nsubject' => 5,
|
86 |
|
|
'x-received' => 1,
|
87 |
|
|
'x400-received' => 1,
|
88 |
wakaba |
1.20 |
};
|
89 |
|
|
|
90 |
|
|
$DEFAULT{-value_type} = {
|
91 |
|
|
':default' => ['Message::Field::Unstructured'],
|
92 |
wakaba |
1.14 |
|
93 |
wakaba |
1.20 |
received => ['Message::Field::Received'],
|
94 |
|
|
'x-received' => ['Message::Field::Received'],
|
95 |
wakaba |
1.14 |
|
96 |
wakaba |
1.20 |
p3p => ['Message::Field::Params'],
|
97 |
|
|
'auto-submitted' => ['Message::Field::ValueParams'],
|
98 |
|
|
link => ['Message::Field::ValueParams'],
|
99 |
|
|
archive => ['Message::Field::ValueParams'],
|
100 |
|
|
'x-face-type' => ['Message::Field::ValueParams'],
|
101 |
|
|
'x-mozilla-draft-info' => ['Message::Field::ValueParams'],
|
102 |
wakaba |
1.14 |
|
103 |
wakaba |
1.20 |
subject => ['Message::Field::Subject'],
|
104 |
|
|
'x-nsubject' => ['Message::Field::Subject'],
|
105 |
wakaba |
1.14 |
|
106 |
wakaba |
1.20 |
'list-software' => ['Message::Field::UA'],
|
107 |
|
|
'user-agent' => ['Message::Field::UA'],
|
108 |
|
|
'resent-user-agent' => ['Message::Field::UA'],
|
109 |
|
|
server => ['Message::Field::UA'],
|
110 |
wakaba |
1.14 |
|
111 |
wakaba |
1.18 |
## A message id
|
112 |
wakaba |
1.20 |
'message-id' => ['Message::Field::MsgID'],
|
113 |
|
|
'resent-message-id' => ['Message::Field::MsgID'],
|
114 |
wakaba |
1.18 |
|
115 |
wakaba |
1.15 |
## Numeric value
|
116 |
wakaba |
1.20 |
lines => ['Message::Field::Numval'],
|
117 |
|
|
'max-forwards' => ['Message::Field::Numval'],
|
118 |
|
|
'mime-version' => ['Message::Field::Numval'],
|
119 |
|
|
'x-jsmail-priority' => ['Message::Field::Numval'],
|
120 |
|
|
'x-mail-count' => ['Message::Field::Numval'],
|
121 |
|
|
'x-ml-count' => ['Message::Field::Numval'],
|
122 |
|
|
'x-priority' => ['Message::Field::Numval'],
|
123 |
wakaba |
1.14 |
|
124 |
wakaba |
1.20 |
path => ['Message::Field::Path'],
|
125 |
wakaba |
1.14 |
};
|
126 |
wakaba |
1.20 |
for (qw(archive cancel-lock
|
127 |
wakaba |
1.16 |
disposition-notification-options encoding
|
128 |
|
|
importance injector-info
|
129 |
|
|
pics-label posted-and-mailed precedence list-id message-type
|
130 |
wakaba |
1.18 |
original-recipient priority x-list-id
|
131 |
wakaba |
1.16 |
sensitivity status x-face x-msmail-priority xref))
|
132 |
wakaba |
1.20 |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::Structured']}
|
133 |
wakaba |
1.16 |
## Not supported yet, but to be supported...
|
134 |
wakaba |
1.18 |
# x-list: unstructured, ml name
|
135 |
wakaba |
1.16 |
for (qw(abuse-reports-to apparently-to approved approved-by bcc cc complaints-to
|
136 |
wakaba |
1.15 |
delivered-to disposition-notification-to envelope-to
|
137 |
wakaba |
1.16 |
errors-to from mail-copies-to mail-followup-to mail-reply-to
|
138 |
|
|
notice-requested-upon-delivery-to read-receipt-to register-mail-reply-requested-by
|
139 |
|
|
reply-to resent-bcc
|
140 |
wakaba |
1.4 |
resent-cc resent-to resent-from resent-sender return-path
|
141 |
wakaba |
1.16 |
return-receipt-to return-receipt-requested-to sender to x-abuse-reports-to
|
142 |
wakaba |
1.19 |
x-admin x-approved x-beenthere x-confirm-reading-to
|
143 |
wakaba |
1.4 |
x-complaints-to x-envelope-from x-envelope-sender
|
144 |
wakaba |
1.16 |
x-envelope-to x-ml-address x-ml-command x-ml-to x-nfrom x-nto
|
145 |
|
|
x-rcpt-to x-sender x-x-sender))
|
146 |
wakaba |
1.20 |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::Addresses']}
|
147 |
wakaba |
1.18 |
for (qw(client-date date date-received delivery-date expires
|
148 |
|
|
expire-date nntp-posting-date posted posted-date received-date
|
149 |
|
|
reply-by resent-date
|
150 |
wakaba |
1.16 |
x-originalarrivaltime x-tcup-date))
|
151 |
wakaba |
1.20 |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::Date']}
|
152 |
wakaba |
1.18 |
for (qw(article-updates in-reply-to
|
153 |
|
|
obsoletes references replaces see-also supersedes))
|
154 |
wakaba |
1.20 |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::MsgIDs']}
|
155 |
wakaba |
1.7 |
for (qw(accept accept-charset accept-encoding accept-language
|
156 |
wakaba |
1.8 |
content-language
|
157 |
wakaba |
1.20 |
encrypted followup-to keywords
|
158 |
wakaba |
1.12 |
list-archive list-digest list-help list-owner
|
159 |
|
|
list-post list-subscribe list-unsubscribe list-url uri newsgroups
|
160 |
wakaba |
1.18 |
posted-to))
|
161 |
wakaba |
1.20 |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::CSV']}
|
162 |
wakaba |
1.18 |
for (qw(x-brother x-boss x-classmate x-daughter x-dearfriend x-favoritesong
|
163 |
|
|
x-friend x-me
|
164 |
|
|
x-moe x-respect
|
165 |
|
|
x-sublimate x-son x-sister x-wife))
|
166 |
wakaba |
1.20 |
{$DEFAULT{-value_type}->{$_} =[ 'Message::Field::CSV']} ## NOT M::F::XMOE!
|
167 |
|
|
for (qw(location referer url x-home-page x-http_referer
|
168 |
wakaba |
1.12 |
x-info x-pgp-key x-ml-url x-uri x-url x-web))
|
169 |
wakaba |
1.20 |
{$DEFAULT{-value_type}->{$_} = ['Message::Field::URI']}
|
170 |
wakaba |
1.1 |
|
171 |
wakaba |
1.18 |
my %header_goodcase = (
|
172 |
|
|
'article-i.d.' => 'Article-I.D.',
|
173 |
|
|
etag => 'ETag',
|
174 |
|
|
'pics-label' => 'PICS-Label',
|
175 |
|
|
te => 'TE',
|
176 |
|
|
url => 'URL',
|
177 |
|
|
'www-authenticate' => 'WWW-Authenticate',
|
178 |
|
|
);
|
179 |
wakaba |
1.20 |
$DEFAULT{-field_name_capitalize} = sub {
|
180 |
wakaba |
1.18 |
my $self = shift;
|
181 |
|
|
my $name = shift;
|
182 |
|
|
if ($header_goodcase{$name}) {
|
183 |
|
|
return $header_goodcase{$name};
|
184 |
|
|
}
|
185 |
|
|
$name =~ s/(?:^|-)cgi-/uc $&/ge;
|
186 |
|
|
$name =~ s/(?:^|-)[a-z]/uc $&/ge;
|
187 |
|
|
$name;
|
188 |
|
|
};
|
189 |
|
|
|
190 |
wakaba |
1.14 |
## taken from L<HTTP::Header>
|
191 |
|
|
# "Good Practice" order of HTTP message headers:
|
192 |
|
|
# - General-Headers
|
193 |
|
|
# - Request-Headers
|
194 |
|
|
# - Response-Headers
|
195 |
|
|
# - Entity-Headers
|
196 |
|
|
# (From draft-ietf-http-v11-spec-rev-01, Nov 21, 1997)
|
197 |
|
|
my @header_order = qw(
|
198 |
|
|
mail-from x-envelope-from relay-version path status
|
199 |
|
|
|
200 |
|
|
cache-control connection date pragma transfer-encoding upgrade trailer via
|
201 |
|
|
|
202 |
|
|
accept accept-charset accept-encoding accept-language
|
203 |
|
|
authorization expect from host
|
204 |
|
|
if-modified-since if-match if-none-match if-range if-unmodified-since
|
205 |
|
|
max-forwards proxy-authorization range referer te user-agent
|
206 |
|
|
|
207 |
|
|
accept-ranges age location proxy-authenticate retry-after server vary
|
208 |
|
|
warning www-authenticate
|
209 |
|
|
|
210 |
|
|
mime-version
|
211 |
|
|
allow content-base content-encoding content-language content-length
|
212 |
|
|
content-location content-md5 content-range content-type
|
213 |
|
|
etag expires last-modified content-style-type content-script-type
|
214 |
|
|
link
|
215 |
|
|
|
216 |
|
|
xref
|
217 |
|
|
);
|
218 |
|
|
my %header_order;
|
219 |
|
|
|
220 |
wakaba |
1.20 |
=head1 CONSTRUCTORS
|
221 |
|
|
|
222 |
|
|
The following methods construct new C<Message::Header> objects:
|
223 |
|
|
|
224 |
|
|
=over 4
|
225 |
|
|
|
226 |
|
|
=cut
|
227 |
|
|
|
228 |
wakaba |
1.14 |
sub _init ($;%) {
|
229 |
|
|
my $self = shift;
|
230 |
|
|
my %options = @_;
|
231 |
wakaba |
1.20 |
my $DEFAULT = Message::Util::make_clone (\%DEFAULT);
|
232 |
|
|
$self->SUPER::_init (%$DEFAULT, %options);
|
233 |
|
|
$self->{value} = [];
|
234 |
|
|
$self->_ns_load_ph ('default');
|
235 |
|
|
$self->{ns}->{default_phuri} = $self->{ns}->{phname2uri}->{'default'};
|
236 |
|
|
$self->_ns_load_ph ('rfc822');
|
237 |
|
|
$self->{ns}->{default_phuri} = $self->{ns}->{phname2uri}->{'rfc822'};
|
238 |
|
|
|
239 |
wakaba |
1.14 |
my @new_fields = ();
|
240 |
|
|
for my $name (keys %options) {
|
241 |
wakaba |
1.20 |
unless (substr ($name, 0, 1) eq '-') {
|
242 |
wakaba |
1.14 |
push @new_fields, ($name => $options{$name});
|
243 |
|
|
}
|
244 |
|
|
}
|
245 |
wakaba |
1.18 |
$self->_init_by_format ($self->{option}->{format}, $self->{option});
|
246 |
wakaba |
1.14 |
# Make alternative representations of @header_order. This is used
|
247 |
|
|
# for sorting.
|
248 |
|
|
my $i = 1;
|
249 |
|
|
for (@header_order) {
|
250 |
|
|
$header_order{$_} = $i++ unless $header_order{$_};
|
251 |
|
|
}
|
252 |
wakaba |
1.18 |
|
253 |
|
|
$self->add (@new_fields, -parse => $self->{option}->{parse_all})
|
254 |
|
|
if $#new_fields > -1;
|
255 |
|
|
}
|
256 |
|
|
|
257 |
|
|
sub _init_by_format ($$\%) {
|
258 |
|
|
my $self = shift;
|
259 |
|
|
my ($format, $option) = @_;
|
260 |
|
|
if ($format =~ /rfc822/) {
|
261 |
|
|
$header_goodcase{bcc} = 'bcc';
|
262 |
|
|
$header_goodcase{cc} = 'cc';
|
263 |
|
|
$header_goodcase{'resent-bcc'} = 'Resent-bcc';
|
264 |
|
|
$header_goodcase{'resent-cc'} = 'Resent-cc';
|
265 |
|
|
} elsif ($format =~ /cgi/) {
|
266 |
|
|
unshift @header_order, qw(content-type location);
|
267 |
wakaba |
1.20 |
$option->{field_sort} = 'good-practice';
|
268 |
|
|
$option->{use_folding} = 0;
|
269 |
wakaba |
1.18 |
} elsif ($format =~ /http/) {
|
270 |
wakaba |
1.20 |
$option->{field_sort} = 'good-practice';
|
271 |
wakaba |
1.18 |
}
|
272 |
|
|
if ($format =~ /uri-url-mailto/) {
|
273 |
|
|
$option->{output_bcc} = 0;
|
274 |
wakaba |
1.20 |
$option->{field_name_capitalize} = 0;
|
275 |
wakaba |
1.18 |
$option->{field_format_pattern} = '%s=%s';
|
276 |
wakaba |
1.20 |
$option->{output_folding} = sub {
|
277 |
wakaba |
1.18 |
$_[1] =~ s/([^:@+\$A-Za-z0-9\-_.!~*])/sprintf('%%%02X', ord $1)/ge;
|
278 |
|
|
$_[1];
|
279 |
wakaba |
1.20 |
}; ## Yes, this is not folding!
|
280 |
wakaba |
1.18 |
}
|
281 |
wakaba |
1.14 |
}
|
282 |
|
|
|
283 |
wakaba |
1.20 |
=item $msg = Message::Header->new ([%initial-fields/options])
|
284 |
wakaba |
1.14 |
|
285 |
|
|
Constructs a new C<Message::Headers> object. You might pass some initial
|
286 |
|
|
C<field-name>-C<field-body> pairs and/or options as parameters to the constructor.
|
287 |
|
|
|
288 |
wakaba |
1.15 |
Example:
|
289 |
wakaba |
1.1 |
|
290 |
wakaba |
1.14 |
$hdr = new Message::Headers
|
291 |
|
|
Date => 'Thu, 03 Feb 1994 00:00:00 +0000',
|
292 |
|
|
Content_Type => 'text/html',
|
293 |
|
|
Content_Location => 'http://www.foo.example/',
|
294 |
|
|
-format => 'mail-rfc2822' ## not to be header field
|
295 |
|
|
;
|
296 |
wakaba |
1.1 |
|
297 |
|
|
=cut
|
298 |
|
|
|
299 |
wakaba |
1.20 |
## Inherited
|
300 |
wakaba |
1.1 |
|
301 |
wakaba |
1.20 |
=item $msg = Message::Header->parse ($header, [%initial-fields/options])
|
302 |
wakaba |
1.1 |
|
303 |
wakaba |
1.14 |
Parses given C<header> and constructs a new C<Message::Headers>
|
304 |
|
|
object. You might pass some additional C<field-name>-C<field-body> pairs
|
305 |
|
|
or/and initial options as parameters to the constructor.
|
306 |
wakaba |
1.1 |
|
307 |
|
|
=cut
|
308 |
|
|
|
309 |
|
|
sub parse ($$;%) {
|
310 |
|
|
my $class = shift;
|
311 |
|
|
my $header = shift;
|
312 |
wakaba |
1.14 |
my $self = bless {}, $class;
|
313 |
wakaba |
1.18 |
$self->_init (@_); ## BUG: don't check linebreak_strict
|
314 |
wakaba |
1.20 |
$header =~ s/\x0D?\x0A$REG{WSP}/\x20/gos if $self->{option}->{use_folding};
|
315 |
wakaba |
1.1 |
for my $field (split /\x0D?\x0A/, $header) {
|
316 |
|
|
if ($field =~ /$REG{M_fromline}/) {
|
317 |
wakaba |
1.20 |
my ($s,undef,$value) = $self->_value_to_arrayitem
|
318 |
|
|
('mail-from' => $1, $self->{option});
|
319 |
|
|
push @{$self->{value}}, $value if $s;
|
320 |
wakaba |
1.1 |
} elsif ($field =~ /$REG{M_field}/) {
|
321 |
wakaba |
1.20 |
my ($name, $body) = ($1, $2);
|
322 |
wakaba |
1.1 |
$body =~ s/$REG{WSP}+$//;
|
323 |
wakaba |
1.20 |
my ($s,undef,$value) = $self->_value_to_arrayitem
|
324 |
|
|
($name => $body, $self->{option});
|
325 |
|
|
push @{$self->{value}}, $value if $s;
|
326 |
wakaba |
1.1 |
}
|
327 |
|
|
}
|
328 |
|
|
$self;
|
329 |
|
|
}
|
330 |
|
|
|
331 |
wakaba |
1.20 |
=item $msg = Message::Header->parse_array (\@header, [%initial-fields/options])
|
332 |
wakaba |
1.15 |
|
333 |
|
|
Parses given C<header> and constructs a new C<Message::Headers>
|
334 |
|
|
object. Same as C<Message::Header-E<lt>parse> but this method
|
335 |
|
|
is given an array reference. You might pass some additional
|
336 |
|
|
C<field-name>-C<field-body> pairs or/and initial options
|
337 |
|
|
as parameters to the constructor.
|
338 |
|
|
|
339 |
|
|
=cut
|
340 |
|
|
|
341 |
wakaba |
1.14 |
sub parse_array ($\@;%) {
|
342 |
|
|
my $class = shift;
|
343 |
|
|
my $header = shift;
|
344 |
|
|
Carp::croak "parse_array: first argument is not an array reference"
|
345 |
|
|
unless ref $header eq 'ARRAY';
|
346 |
|
|
my $self = bless {}, $class;
|
347 |
|
|
$self->_init (@_);
|
348 |
|
|
while (1) {
|
349 |
|
|
my $field = shift @$header;
|
350 |
wakaba |
1.20 |
if ($self->{option}->{use_folding}) {
|
351 |
|
|
while (1) {
|
352 |
|
|
if ($$header[0] =~ /^$REG{WSP}/) {
|
353 |
|
|
$field .= shift @$header;
|
354 |
|
|
} else {last}
|
355 |
|
|
}
|
356 |
wakaba |
1.14 |
}
|
357 |
wakaba |
1.18 |
if ($self->{option}->{linebreak_strict}) {
|
358 |
|
|
$field =~ s/\x0D\x0A//g;
|
359 |
|
|
} else {
|
360 |
|
|
$field =~ tr/\x0D\x0A//d;
|
361 |
|
|
}
|
362 |
wakaba |
1.20 |
local $self->{option}->{parse} = $self->{option}->{parse_all};
|
363 |
wakaba |
1.14 |
if ($field =~ /$REG{M_fromline}/) {
|
364 |
wakaba |
1.20 |
my ($s,undef,$value) = $self->_value_to_arrayitem
|
365 |
|
|
('mail-from' => $1, $self->{option});
|
366 |
|
|
push @{$self->{value}}, $value if $s;
|
367 |
wakaba |
1.14 |
} elsif ($field =~ /$REG{M_field}/) {
|
368 |
wakaba |
1.20 |
my ($name, $body) = ($self->_n11n_field_name ($1), $2);
|
369 |
wakaba |
1.14 |
$body =~ s/$REG{WSP}+$//;
|
370 |
wakaba |
1.20 |
my ($s,undef,$value) = $self->_value_to_arrayitem
|
371 |
|
|
($name => $body, $self->{option});
|
372 |
|
|
push @{$self->{value}}, $value if $s;
|
373 |
wakaba |
1.14 |
}
|
374 |
|
|
last if $#$header < 0;
|
375 |
|
|
}
|
376 |
|
|
$self;
|
377 |
|
|
}
|
378 |
|
|
|
379 |
wakaba |
1.15 |
=back
|
380 |
|
|
|
381 |
|
|
=head1 METHODS
|
382 |
|
|
|
383 |
wakaba |
1.1 |
=head2 $self->field ($field_name)
|
384 |
|
|
|
385 |
|
|
Returns C<field-body> of given C<field-name>.
|
386 |
|
|
When there are two or more C<field>s whose name is C<field-name>,
|
387 |
|
|
this method return all C<field-body>s as array. (On scalar
|
388 |
|
|
context, only first one is returned.)
|
389 |
|
|
|
390 |
|
|
=cut
|
391 |
|
|
|
392 |
wakaba |
1.20 |
sub field ($@) {shift->SUPER::item (@_)}
|
393 |
|
|
sub field_exist ($@) {shift->SUPER::item_exist (@_)}
|
394 |
|
|
|
395 |
|
|
## item-by?, \$checked-item, {item-key => 1}, \%option
|
396 |
|
|
sub _item_match ($$\$\%\%) {
|
397 |
wakaba |
1.1 |
my $self = shift;
|
398 |
wakaba |
1.20 |
my ($by, $i, $list, $option) = @_;
|
399 |
|
|
return 0 unless ref $$i; ## Already removed
|
400 |
|
|
if ($by eq 'name') {
|
401 |
|
|
my %o = %$option; $o{parse} = 0;
|
402 |
|
|
my %l;
|
403 |
|
|
for (keys %$list) {
|
404 |
|
|
my ($s, undef, $v) = $self->_value_to_arrayitem ($_, '', %o);
|
405 |
|
|
if ($s) {
|
406 |
|
|
$l{$v->{name} . ':' . ( $option->{ns} || $v->{ns} ) } = 1;
|
407 |
wakaba |
1.1 |
} else {
|
408 |
wakaba |
1.20 |
$l{$v->{name} .':'. ( $option->{ns} || $self->{ns}->{default_phuri} ) } = 1;
|
409 |
wakaba |
1.1 |
}
|
410 |
|
|
}
|
411 |
wakaba |
1.20 |
return 1 if $l{$$i->{name} . ':' . $$i->{ns}};
|
412 |
wakaba |
1.1 |
}
|
413 |
wakaba |
1.20 |
0;
|
414 |
wakaba |
1.1 |
}
|
415 |
wakaba |
1.20 |
*_delete_match = \&_item_match;
|
416 |
wakaba |
1.1 |
|
417 |
wakaba |
1.20 |
## Returns returned item value \$item-value, \%option
|
418 |
|
|
sub _item_return_value ($\$\%) {
|
419 |
|
|
if (ref ${$_[1]}->{body}) {
|
420 |
|
|
${$_[1]}->{body};
|
421 |
|
|
} else {
|
422 |
|
|
${$_[1]}->{body} = $_[0]->_parse_value (${$_[1]}->{name} => ${$_[1]}->{body},
|
423 |
|
|
ns => ${$_[1]}->{ns});
|
424 |
|
|
${$_[1]}->{body};
|
425 |
wakaba |
1.9 |
}
|
426 |
|
|
}
|
427 |
|
|
|
428 |
wakaba |
1.20 |
## Returns returned (new created) item value $name, \%option
|
429 |
|
|
sub _item_new_value ($$\%) {
|
430 |
|
|
my ($s,undef,$value) = $_[0]->_value_to_arrayitem
|
431 |
|
|
($_[1] => '', $_[2]);
|
432 |
|
|
$s? $value: undef;
|
433 |
|
|
}
|
434 |
wakaba |
1.2 |
|
435 |
|
|
|
436 |
|
|
|
437 |
wakaba |
1.20 |
## $self->_parse_value ($type, $value, %options);
|
438 |
|
|
sub _parse_value ($$$;%) {
|
439 |
|
|
my $self = shift;
|
440 |
|
|
my $name = shift ;#|| $self->{option}->{_VALTYPE_DEFAULT};
|
441 |
|
|
my $value = shift; return $value if ref $value;
|
442 |
|
|
my %option = @_;
|
443 |
|
|
my $vtype; { no strict 'refs';
|
444 |
|
|
$vtype = ${&_NS_uri2phpackage ($option{ns}).'::OPTION'}{value_type};
|
445 |
|
|
if (ref $vtype) { $vtype = $vtype->{$name} }
|
446 |
|
|
unless (ref $vtype) { $vtype = $vtype->{$self->{option}->{_VALTYPE_DEFAULT}} }
|
447 |
|
|
## For compatiblity.
|
448 |
|
|
unless (ref $vtype) { $vtype = $self->{option}->{value_type}->{$name}
|
449 |
|
|
|| $self->{option}->{value_type}->{$self->{option}->{_VALTYPE_DEFAULT}} }
|
450 |
|
|
}
|
451 |
|
|
my $vpackage = $vtype->[0];
|
452 |
|
|
my %vopt = %{$vtype->[1]} if ref $vtype->[1];
|
453 |
|
|
if ($vpackage eq ':none:') {
|
454 |
|
|
return $value;
|
455 |
|
|
} elsif (defined $value) {
|
456 |
|
|
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@};
|
457 |
|
|
return $vpackage->parse ($value,
|
458 |
|
|
-format => $self->{option}->{format},
|
459 |
|
|
-field_name => $name,
|
460 |
|
|
-parse_all => $self->{option}->{parse_all},
|
461 |
|
|
%vopt);
|
462 |
|
|
} else {
|
463 |
|
|
eval "require $vpackage" or Carp::croak qq{<parse>: $vpackage: Can't load package: $@};
|
464 |
|
|
return $vpackage->new (
|
465 |
|
|
-format => $self->{option}->{format},
|
466 |
|
|
-field_name => $name,
|
467 |
|
|
-parse_all => $self->{option}->{parse_all},
|
468 |
|
|
%vopt);
|
469 |
wakaba |
1.4 |
}
|
470 |
wakaba |
1.2 |
}
|
471 |
|
|
|
472 |
wakaba |
1.1 |
=head2 $self->field_name_list ()
|
473 |
|
|
|
474 |
|
|
Returns list of all C<field-name>s. (Even if there are two
|
475 |
|
|
or more C<field>s which have same C<field-name>, this method
|
476 |
|
|
returns ALL names.)
|
477 |
|
|
|
478 |
|
|
=cut
|
479 |
|
|
|
480 |
|
|
sub field_name_list ($) {
|
481 |
|
|
my $self = shift;
|
482 |
wakaba |
1.20 |
$self->_delete_empty ();
|
483 |
|
|
map { $_->{name} . ':' . $_->{ns} } @{$self->{value}};
|
484 |
|
|
}
|
485 |
|
|
|
486 |
|
|
sub namespace_ph_default ($;$) {
|
487 |
|
|
my $self = shift;
|
488 |
|
|
if (defined $_[0]) {
|
489 |
|
|
no strict 'refs';
|
490 |
|
|
$self->{ns}->{default_phuri} = $_[0];
|
491 |
|
|
$self->_ns_load_ph (${&_NS_uri2phpackage ($self->{ns}->{default_phuri}).'::OPTION'}{namespace_phname});
|
492 |
|
|
}
|
493 |
|
|
$self->{ns}->{default_phuri};
|
494 |
wakaba |
1.1 |
}
|
495 |
|
|
|
496 |
wakaba |
1.16 |
=item $hdr->add ($field-name, $field-body, [$name, $body, ...])
|
497 |
wakaba |
1.1 |
|
498 |
wakaba |
1.16 |
Adds some field name/body pairs. Even if there are
|
499 |
|
|
one or more fields named given C<$field-name>,
|
500 |
|
|
given name/body pairs are ADDed. Use C<replace>
|
501 |
|
|
to remove same-name-fields.
|
502 |
wakaba |
1.1 |
|
503 |
wakaba |
1.14 |
Instead of field name-body pair, you might pass some options.
|
504 |
|
|
Four options are available for this method.
|
505 |
|
|
|
506 |
|
|
C<-parse>: Parses and validates C<field-body>, and returns
|
507 |
|
|
C<field-body> object. (When multiple C<field-body>s are
|
508 |
|
|
added, returns only last one.) (Default: C<defined wantarray>)
|
509 |
|
|
|
510 |
|
|
C<-prepend>: New fields are not appended,
|
511 |
|
|
but prepended to current fields. (Default: C<0>)
|
512 |
|
|
|
513 |
|
|
C<-translate-underscore>: Do C<field-name> =~ tr/_/-/. (Default: C<1>)
|
514 |
|
|
|
515 |
|
|
C<-validate>: Checks whether C<field-name> is valid or not.
|
516 |
|
|
|
517 |
wakaba |
1.1 |
=cut
|
518 |
|
|
|
519 |
wakaba |
1.20 |
## [Name: Value] pair -> internal array item
|
520 |
|
|
## $self->_value_to_arrayitem ($name => $value, {%options})
|
521 |
|
|
## or
|
522 |
|
|
## $self->_value_to_arrayitem ($name => [$value, %value_options], {%options})
|
523 |
|
|
##
|
524 |
|
|
## Return: ((1 = success / 0 = failue), $full_name, $arrayitem)
|
525 |
|
|
sub _value_to_arrayitem ($$$\%) {
|
526 |
|
|
my $self = shift;
|
527 |
|
|
my ($name, $value, $option) = @_;
|
528 |
|
|
my $value_option = {};
|
529 |
|
|
if (ref $value eq 'ARRAY') {
|
530 |
|
|
($value, %$value_option) = @$value;
|
531 |
|
|
}
|
532 |
|
|
my $nsuri = $self->{ns}->{default_phuri};
|
533 |
|
|
no strict 'refs';
|
534 |
|
|
if ($option->{ns}) {
|
535 |
|
|
$nsuri = $option->{ns};
|
536 |
|
|
} elsif ($name =~ s/^([Xx]-[A-Za-z]+|[A-Za-z]+)-//) {
|
537 |
|
|
my $oprefix = $1;
|
538 |
|
|
my $prefix
|
539 |
|
|
= &{${&_NS_uri2phpackage ($nsuri).'::OPTION'}{n11n_prefix}}
|
540 |
|
|
($self, &_NS_uri2phpackage ($nsuri), $oprefix);
|
541 |
|
|
$self->_ns_load_ph ($prefix);
|
542 |
|
|
$nsuri = $self->{ns}->{phname2uri}->{$prefix};
|
543 |
|
|
unless ($nsuri) {
|
544 |
|
|
$name = $oprefix . '-' . $name;
|
545 |
|
|
$nsuri = $self->{ns}->{default_phuri};
|
546 |
|
|
}
|
547 |
|
|
}
|
548 |
|
|
$name
|
549 |
|
|
= &{${&_NS_uri2phpackage ($nsuri).'::OPTION'}{n11n_name}}
|
550 |
|
|
($self, &_NS_uri2phpackage ($nsuri), $name);
|
551 |
|
|
Carp::croak "$name: invalid field-name"
|
552 |
|
|
if $option->{field_name_validation}
|
553 |
|
|
&& $name =~ /$REG{$option->{field_name_unsafe_rule}}/;
|
554 |
|
|
$value = $self->_parse_value ($name => $value, ns => $nsuri) if $$option{parse};
|
555 |
|
|
$$option{parse} = 0;
|
556 |
|
|
(1, $name.':'.$nsuri => {name => $name, body => $value, ns => $nsuri});
|
557 |
wakaba |
1.1 |
}
|
558 |
wakaba |
1.20 |
*_add_hash_check = \&_value_to_arrayitem;
|
559 |
|
|
*_replace_hash_check = \&_value_to_arrayitem;
|
560 |
wakaba |
1.1 |
|
561 |
|
|
=head2 $self->relace ($field_name, $field_body)
|
562 |
|
|
|
563 |
|
|
Set the C<field-body> named C<field-name> as $field_body.
|
564 |
|
|
If $field_name C<field> is already exists, it is replaced
|
565 |
|
|
by new $field_body value. If not, new C<field> is inserted.
|
566 |
|
|
(If there are some C<field> named as $field_name,
|
567 |
|
|
first one is used and the others are not changed.)
|
568 |
|
|
|
569 |
|
|
=cut
|
570 |
|
|
|
571 |
wakaba |
1.20 |
sub _replace_hash_shift ($\%$\%) {
|
572 |
|
|
shift; my $r = shift; my $n = $_[0]->{name};
|
573 |
|
|
if ($$r{$n}) {
|
574 |
|
|
my $d = $$r{$n};
|
575 |
|
|
delete $$r{$n};
|
576 |
|
|
return $d;
|
577 |
wakaba |
1.1 |
}
|
578 |
wakaba |
1.20 |
undef;
|
579 |
wakaba |
1.1 |
}
|
580 |
|
|
|
581 |
wakaba |
1.14 |
=head2 $self->delete ($field-name, [$name, ...])
|
582 |
wakaba |
1.1 |
|
583 |
|
|
Deletes C<field> named as $field_name.
|
584 |
|
|
|
585 |
|
|
=cut
|
586 |
|
|
|
587 |
|
|
|
588 |
wakaba |
1.2 |
=head2 $self->count ([$field_name])
|
589 |
wakaba |
1.1 |
|
590 |
|
|
Returns the number of times the given C<field> appears.
|
591 |
wakaba |
1.2 |
If no $field_name is given, returns the number
|
592 |
|
|
of fields. (Same as $#$self+1)
|
593 |
wakaba |
1.1 |
|
594 |
|
|
=cut
|
595 |
|
|
|
596 |
wakaba |
1.20 |
sub _count_by_name ($$\%) {
|
597 |
wakaba |
1.1 |
my $self = shift;
|
598 |
wakaba |
1.20 |
my ($array, $option) = @_;
|
599 |
|
|
my $name = $self->_n11n_field_name ($$option{-name});
|
600 |
|
|
my @a = grep {$_->{name} eq $name} @{$self->{$array}};
|
601 |
|
|
$#a + 1;
|
602 |
|
|
}
|
603 |
|
|
|
604 |
|
|
## Delete empty items
|
605 |
|
|
sub _delete_empty ($) {
|
606 |
|
|
my $self = shift;
|
607 |
|
|
my $array = $self->{option}->{_HASH_NAME};
|
608 |
|
|
$self->{$array} = [grep {ref $_ && length $_->{name}} @{$self->{$array}}];
|
609 |
wakaba |
1.1 |
}
|
610 |
|
|
|
611 |
wakaba |
1.14 |
=head2 $self->rename ($field-name, $new-name, [$old, $new,...])
|
612 |
wakaba |
1.12 |
|
613 |
wakaba |
1.14 |
Renames C<$field-name> as C<$new-name>.
|
614 |
wakaba |
1.12 |
|
615 |
|
|
=cut
|
616 |
|
|
|
617 |
wakaba |
1.14 |
sub rename ($%) {
|
618 |
wakaba |
1.12 |
my $self = shift;
|
619 |
wakaba |
1.14 |
my %params = @_;
|
620 |
|
|
my %option = %{$self->{option}};
|
621 |
|
|
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}
|
622 |
|
|
my %new_name;
|
623 |
|
|
for (grep {/^[^-]/} keys %params) {
|
624 |
wakaba |
1.20 |
my ($old => $new)
|
625 |
|
|
= ($self->_n11n_field_name ($_) => $self->_n11n_field_name ($params{$_}));
|
626 |
|
|
$old =~ tr/_/-/ if $option{translate_underscore};
|
627 |
wakaba |
1.14 |
$new =~ tr/_/-/ if $option{translate_underscore};
|
628 |
|
|
Carp::croak "rename: $new: invalid field-name"
|
629 |
wakaba |
1.20 |
if $option{field_name_validation}
|
630 |
|
|
&& $new =~ /$REG{$option{field_name_unsafe_rule}}/;
|
631 |
wakaba |
1.14 |
$new_name{$old} = $new;
|
632 |
|
|
}
|
633 |
wakaba |
1.20 |
for my $field (@{$self->{value}}) {
|
634 |
wakaba |
1.14 |
if (length $new_name{$field->{name}}) {
|
635 |
|
|
$field->{name} = $new_name{$field->{name}};
|
636 |
wakaba |
1.12 |
}
|
637 |
|
|
}
|
638 |
wakaba |
1.14 |
$self if defined wantarray;
|
639 |
|
|
}
|
640 |
|
|
|
641 |
|
|
|
642 |
|
|
=item $self->scan(\&doit)
|
643 |
|
|
|
644 |
|
|
Apply a subroutine to each header field in turn. The callback routine is
|
645 |
|
|
called with two parameters; the name of the field and a single value.
|
646 |
|
|
If the header has more than one value, then the routine is called once
|
647 |
|
|
for each value.
|
648 |
|
|
|
649 |
|
|
=cut
|
650 |
|
|
|
651 |
wakaba |
1.20 |
sub _scan_sort ($\@\%) {
|
652 |
|
|
my $self = shift;
|
653 |
|
|
my ($array, $option) = @_;
|
654 |
wakaba |
1.14 |
my $sort;
|
655 |
wakaba |
1.20 |
$sort = \&_header_cmp if $option->{field_sort} eq 'good-practice';
|
656 |
|
|
$sort = {$a cmp $b} if $option->{field_sort} eq 'alphabetic';
|
657 |
|
|
return ( sort $sort @$array ) if ref $sort;
|
658 |
|
|
@$array;
|
659 |
|
|
}
|
660 |
|
|
|
661 |
|
|
sub _n11n_field_name ($$) {
|
662 |
|
|
my $self = shift;
|
663 |
|
|
my $s = shift;
|
664 |
|
|
$s =~ s/^$REG{WSP}+//; $s =~ s/$REG{WSP}+$//;
|
665 |
|
|
$s = lc $s ;#unless $self->{option}->{field_name_case_sensible};
|
666 |
|
|
$s;
|
667 |
wakaba |
1.14 |
}
|
668 |
|
|
|
669 |
|
|
# Compare function which makes it easy to sort headers in the
|
670 |
|
|
# recommended "Good Practice" order.
|
671 |
|
|
## taken from HTTP::Header
|
672 |
|
|
sub _header_cmp
|
673 |
|
|
{
|
674 |
|
|
my ($na, $nb) = ($a->{name}, $b->{name});
|
675 |
|
|
# Unknown headers are assign a large value so that they are
|
676 |
|
|
# sorted last. This also helps avoiding a warning from -w
|
677 |
|
|
# about comparing undefined values.
|
678 |
|
|
$header_order{$na} = 999 unless defined $header_order{$na};
|
679 |
|
|
$header_order{$nb} = 999 unless defined $header_order{$nb};
|
680 |
|
|
|
681 |
|
|
$header_order{$na} <=> $header_order{$nb} || $na cmp $nb;
|
682 |
wakaba |
1.12 |
}
|
683 |
|
|
|
684 |
wakaba |
1.1 |
=head2 $self->stringify ([%option])
|
685 |
|
|
|
686 |
|
|
Returns the C<header> as a string.
|
687 |
|
|
|
688 |
|
|
=cut
|
689 |
|
|
|
690 |
|
|
sub stringify ($;%) {
|
691 |
|
|
my $self = shift;
|
692 |
wakaba |
1.14 |
my %params = @_;
|
693 |
|
|
my %option = %{$self->{option}};
|
694 |
wakaba |
1.18 |
$option{format} = $params{-format} if $params{-format};
|
695 |
|
|
$self->_init_by_format ($option{format}, \%option);
|
696 |
wakaba |
1.14 |
for (grep {/^-/} keys %params) {$option{substr ($_, 1)} = $params{$_}}
|
697 |
wakaba |
1.1 |
my @ret;
|
698 |
wakaba |
1.18 |
my $_stringify = sub {
|
699 |
wakaba |
1.20 |
no strict 'refs';
|
700 |
|
|
my ($name, $body, $nsuri) = ($_[1]->{name}, $_[1]->{body}, $_[1]->{ns});
|
701 |
wakaba |
1.18 |
return unless length $name;
|
702 |
wakaba |
1.20 |
return if $option{output_mail_from} && $name eq 'mail-from';
|
703 |
wakaba |
1.18 |
return if !$option{output_bcc} && ($name eq 'bcc' || $name eq 'resent-bcc');
|
704 |
wakaba |
1.20 |
my $nspackage = &_NS_uri2phpackage ($nsuri);
|
705 |
|
|
my $oname; ## Outputed field-name
|
706 |
|
|
my $prefix = ${$nspackage.'::OPTION'} {namespace_phname_goodcase}
|
707 |
|
|
|| $self->{ns}->{uri2phname}->{$nsuri};
|
708 |
|
|
$prefix = undef if $nsuri eq $self->{ns}->{default_phuri};
|
709 |
|
|
my $gc = ${$nspackage.'::OPTION'} {to_be_goodcase};
|
710 |
|
|
if (ref $gc) { $oname = &$gc ($self, $nspackage, $name) }
|
711 |
|
|
else { $oname = $name }
|
712 |
|
|
if ($prefix) { $oname = $prefix . '-' . $oname }
|
713 |
wakaba |
1.18 |
if ($option{format} =~ /uri-url-mailto/) {
|
714 |
|
|
return if (( $option{uri_mailto_safe}->{$name}
|
715 |
|
|
|| $option{uri_mailto_safe}->{':default'})
|
716 |
|
|
< $option{uri_mailto_safe_level});
|
717 |
|
|
if ($name eq 'to') {
|
718 |
wakaba |
1.20 |
$body = $self->field ('to', -new_item_unless_exist => 0);
|
719 |
|
|
if (ref $body && $body->have_group) {
|
720 |
|
|
#
|
721 |
|
|
} elsif (ref $body && $body->count > 1) {
|
722 |
|
|
$body = $body->clone;
|
723 |
|
|
$body->delete ({-by => 'index'}, 0);
|
724 |
|
|
}
|
725 |
wakaba |
1.18 |
}
|
726 |
|
|
}
|
727 |
|
|
my $fbody;
|
728 |
|
|
if (ref $body) {
|
729 |
|
|
$fbody = $body->stringify (-format => $option{format});
|
730 |
|
|
} else {
|
731 |
|
|
$fbody = $body;
|
732 |
|
|
}
|
733 |
|
|
return unless length $fbody;
|
734 |
|
|
unless ($option{linebreak_strict}) {
|
735 |
|
|
## bare \x0D and bare \x0A are unsafe
|
736 |
|
|
$fbody =~ s/\x0D(?=[^\x09\x0A\x20])/\x0D\x20/g;
|
737 |
|
|
$fbody =~ s/\x0A(?=[^\x09\x20])/\x0A\x20/g;
|
738 |
|
|
} else {
|
739 |
|
|
$fbody =~ s/\x0D\x0A(?=[^\x09\x20])/\x0D\x0A\x20/g;
|
740 |
|
|
}
|
741 |
wakaba |
1.20 |
if ($option{use_folding}) {
|
742 |
|
|
if (ref $option{output_folding}) {
|
743 |
|
|
$fbody = &{$option{output_folding}} ($self, $fbody,
|
744 |
|
|
-initial_length => length ($oname) +2);
|
745 |
|
|
} elsif ($option{output_folding}) {
|
746 |
|
|
$fbody = $self->_fold ($fbody, -initial_length => length ($oname) +2);
|
747 |
|
|
}
|
748 |
wakaba |
1.18 |
}
|
749 |
wakaba |
1.20 |
push @ret, sprintf $option{field_format_pattern}, $oname, $fbody;
|
750 |
|
|
};
|
751 |
|
|
if ($option{format} =~ /uri-url-mailto/) {
|
752 |
|
|
if ($option{format} =~ /uri-url-mailto-to/) {
|
753 |
|
|
my $to = $self->field ('to', -new_item_unless_exist => 0);
|
754 |
|
|
if ($to) {
|
755 |
|
|
unless ($to->have_group) {
|
756 |
|
|
my $fbody = $to->stringify (-format => $option{format}, -max => 1);
|
757 |
|
|
return &{$option{output_folding}} ($self, $fbody);
|
758 |
|
|
}
|
759 |
wakaba |
1.18 |
}
|
760 |
wakaba |
1.20 |
'';
|
761 |
|
|
} elsif ($option{format} =~ /uri-url-mailto-rfc1738/) {
|
762 |
|
|
my $to = $self->field ('to', -new_item_unless_exist => 0);
|
763 |
|
|
if ($to) {
|
764 |
|
|
my $fbody = $to->addr_spec (-format => $option{format});
|
765 |
|
|
return &{$option{output_folding}} ($self, $fbody);
|
766 |
wakaba |
1.18 |
}
|
767 |
wakaba |
1.20 |
'';
|
768 |
|
|
} else {
|
769 |
|
|
$self->scan ($_stringify);
|
770 |
|
|
my $ret = join ('&', @ret);
|
771 |
|
|
$ret;
|
772 |
wakaba |
1.12 |
}
|
773 |
wakaba |
1.18 |
} else {
|
774 |
wakaba |
1.20 |
if ($option{output_mail_from}) {
|
775 |
|
|
my $fromline = $self->field ('mail-from', -new_item_unless_exist => 0);
|
776 |
wakaba |
1.18 |
push @ret, 'From '.$fromline if $fromline;
|
777 |
|
|
}
|
778 |
|
|
$self->scan ($_stringify);
|
779 |
wakaba |
1.20 |
my $ret = join ("\x0D\x0A", @ret);
|
780 |
|
|
$ret? $ret."\x0D\x0A": '';
|
781 |
wakaba |
1.18 |
}
|
782 |
wakaba |
1.1 |
}
|
783 |
wakaba |
1.14 |
*as_string = \&stringify;
|
784 |
wakaba |
1.1 |
|
785 |
wakaba |
1.12 |
=head2 $self->option ($option_name, [$option_value])
|
786 |
wakaba |
1.1 |
|
787 |
wakaba |
1.12 |
Set/gets new value of the option.
|
788 |
wakaba |
1.1 |
|
789 |
|
|
=cut
|
790 |
|
|
|
791 |
wakaba |
1.14 |
sub option ($@) {
|
792 |
wakaba |
1.1 |
my $self = shift;
|
793 |
wakaba |
1.14 |
if (@_ == 1) {
|
794 |
|
|
return $self->{option}->{ shift (@_) };
|
795 |
|
|
}
|
796 |
|
|
while (my ($name, $value) = splice (@_, 0, 2)) {
|
797 |
wakaba |
1.12 |
$self->{option}->{$name} = $value;
|
798 |
|
|
if ($name eq 'format') {
|
799 |
|
|
for my $f (@{$self->{field}}) {
|
800 |
wakaba |
1.14 |
if (ref $f->{body}) {
|
801 |
|
|
$f->{body}->option (-format => $value);
|
802 |
wakaba |
1.12 |
}
|
803 |
|
|
}
|
804 |
|
|
}
|
805 |
|
|
}
|
806 |
wakaba |
1.1 |
}
|
807 |
|
|
|
808 |
wakaba |
1.20 |
sub field_type ($@) {shift->SUPER::value_type (@_)}
|
809 |
|
|
|
810 |
|
|
## $self->_fold ($string, %option = (-max, -initial_length(for field-name)) )
|
811 |
|
|
sub _fold ($$;%) {
|
812 |
wakaba |
1.4 |
my $self = shift;
|
813 |
wakaba |
1.20 |
my $string = shift;
|
814 |
|
|
my %option = @_;
|
815 |
|
|
my $max = $self->{option}->{line_length_max};
|
816 |
|
|
$max = 20 if $max < 20;
|
817 |
|
|
|
818 |
|
|
my $l = $option{-initial_length} || 0;
|
819 |
|
|
$string =~ s{([\x09\x20][^\x09\x20]+)}{
|
820 |
|
|
my $s = $1;
|
821 |
|
|
if ($l + length $s > $max) {
|
822 |
|
|
$s = "\x0D\x0A\x20" . $s;
|
823 |
|
|
$l = length ($s) - 2;
|
824 |
|
|
} else { $l += length $s }
|
825 |
|
|
$s;
|
826 |
|
|
}gex;
|
827 |
|
|
$string;
|
828 |
wakaba |
1.4 |
}
|
829 |
|
|
|
830 |
wakaba |
1.20 |
sub _ns_load_ph ($$) {
|
831 |
wakaba |
1.1 |
my $self = shift;
|
832 |
wakaba |
1.20 |
my $name = shift; ## normalized prefix (without HYPHEN-MINUS)
|
833 |
|
|
return if $self->{ns}->{phname2uri}->{$name};
|
834 |
|
|
$self->{ns}->{phname2uri}->{$name} = $NS_phname2uri{$name};
|
835 |
|
|
return unless $self->{ns}->{phname2uri}->{$name};
|
836 |
|
|
$self->{ns}->{uri2phname}->{$self->{ns}->{phname2uri}->{$name}} = $name;
|
837 |
wakaba |
1.1 |
}
|
838 |
|
|
|
839 |
wakaba |
1.20 |
sub _NS_uri2phpackage ($) {
|
840 |
|
|
$NS_uri2phpackage{$_[0]}
|
841 |
|
|
|| $NS_uri2phpackage{$Message::Header::Default::OPTION{namespace_uri}};
|
842 |
wakaba |
1.1 |
}
|
843 |
|
|
|
844 |
wakaba |
1.14 |
=head2 $self->clone ()
|
845 |
|
|
|
846 |
|
|
Returns a copy of Message::Header object.
|
847 |
|
|
|
848 |
|
|
=cut
|
849 |
|
|
|
850 |
wakaba |
1.20 |
## Inhreited
|
851 |
wakaba |
1.14 |
|
852 |
|
|
=head1 NOTE
|
853 |
|
|
|
854 |
|
|
=head2 C<field-name>
|
855 |
|
|
|
856 |
|
|
The header field name is not case sensitive. To make the life
|
857 |
|
|
easier for perl users who wants to avoid quoting before the => operator,
|
858 |
|
|
you can use '_' as a synonym for '-' in header field names
|
859 |
|
|
(this behaviour can be suppressed by setting
|
860 |
|
|
C<translate_underscore> option to C<0> value).
|
861 |
|
|
|
862 |
wakaba |
1.1 |
=head1 EXAMPLE
|
863 |
|
|
|
864 |
|
|
## Print field list
|
865 |
|
|
|
866 |
|
|
use Message::Header;
|
867 |
|
|
my $header = Message::Header->parse ($header);
|
868 |
|
|
|
869 |
wakaba |
1.2 |
for my $i (0..$#$header) {
|
870 |
|
|
print $header->field_name ($i), "\t=> ", $header->field_body ($i), "\n";
|
871 |
wakaba |
1.1 |
}
|
872 |
|
|
|
873 |
|
|
|
874 |
|
|
## Make simple header
|
875 |
|
|
|
876 |
wakaba |
1.2 |
use Message::Header;
|
877 |
wakaba |
1.1 |
use Message::Field::Address;
|
878 |
|
|
my $header = new Message::Header;
|
879 |
|
|
|
880 |
|
|
my $from = new Message::Field::Address;
|
881 |
|
|
$from->add ('foo@foo.example', name => 'F. Foo');
|
882 |
|
|
my $to = new Message::Field::Address;
|
883 |
|
|
$to->add ('bar@bar.example', name => 'Mr. Bar');
|
884 |
|
|
$to->add ('hoge@foo.example', name => 'Hoge-san');
|
885 |
|
|
$header->add ('From' => $from);
|
886 |
|
|
$header->add ('To' => $to);
|
887 |
|
|
$header->add ('Subject' => 'Re: Meeting');
|
888 |
|
|
$header->add ('References' => '<hoge.msgid%foo@foo.example>');
|
889 |
|
|
print $header;
|
890 |
|
|
|
891 |
wakaba |
1.14 |
=head1 ACKNOWLEDGEMENTS
|
892 |
|
|
|
893 |
|
|
Some of codes are taken from other modules such as
|
894 |
|
|
HTTP::Header, Mail::Header.
|
895 |
|
|
|
896 |
wakaba |
1.1 |
=head1 LICENSE
|
897 |
|
|
|
898 |
|
|
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>.
|
899 |
|
|
|
900 |
|
|
This program is free software; you can redistribute it and/or modify
|
901 |
|
|
it under the terms of the GNU General Public License as published by
|
902 |
|
|
the Free Software Foundation; either version 2 of the License, or
|
903 |
|
|
(at your option) any later version.
|
904 |
|
|
|
905 |
|
|
This program is distributed in the hope that it will be useful,
|
906 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
907 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
908 |
|
|
GNU General Public License for more details.
|
909 |
|
|
|
910 |
|
|
You should have received a copy of the GNU General Public License
|
911 |
|
|
along with this program; see the file COPYING. If not, write to
|
912 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
913 |
|
|
Boston, MA 02111-1307, USA.
|
914 |
|
|
|
915 |
|
|
=head1 CHANGE
|
916 |
|
|
|
917 |
|
|
See F<ChangeLog>.
|
918 |
wakaba |
1.20 |
$Date: 2002/05/15 07:31:28 $
|
919 |
wakaba |
1.1 |
|
920 |
|
|
=cut
|
921 |
|
|
|
922 |
|
|
1;
|