1 |
wakaba |
1.1 |
|
2 |
|
|
=head1 NAME |
3 |
|
|
|
4 |
wakaba |
1.6 |
Message::Field::ValueParams --- Perl module for "word; parameter(s)" style |
5 |
|
|
Internet message field bodies |
6 |
wakaba |
1.1 |
|
7 |
|
|
=cut |
8 |
|
|
|
9 |
|
|
package Message::Field::ValueParams; |
10 |
|
|
use strict; |
11 |
wakaba |
1.6 |
use vars qw(@ISA %REG $VERSION); |
12 |
wakaba |
1.7 |
$VERSION=do{my @r=(q$Revision: 1.6 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
13 |
wakaba |
1.6 |
require Message::Field::Params; |
14 |
|
|
push @ISA, qw(Message::Field::Params); |
15 |
|
|
|
16 |
|
|
use overload '+=' => sub { $_[0]->{value} = $_[0]->{value} + $_[1]; $_[0] }, |
17 |
|
|
'-=' => sub { $_[0]->{value} = $_[0]->{value} - $_[1]; $_[0] }, |
18 |
|
|
'*=' => sub { $_[0]->{value} = $_[0]->{value} * $_[1]; $_[0] }, |
19 |
|
|
'**=' => sub { $_[0]->{value} = $_[0]->{value} ** $_[1]; $_[0] }, |
20 |
|
|
'/=' => sub { $_[0]->{value} = $_[0]->{value} / $_[1]; $_[0] }, |
21 |
|
|
'%=' => sub { $_[0]->{value} = $_[0]->{value} % $_[1]; $_[0] }, |
22 |
|
|
fallback => 1; |
23 |
|
|
|
24 |
|
|
*REG = \%Message::Field::Params::REG; |
25 |
|
|
## Inherited: comment, quoted_string, domain_literal, angle_quoted |
26 |
|
|
## WSP, FWS, atext, atext_dot, token, attribute_char |
27 |
|
|
## S_encoded_word |
28 |
|
|
## M_quoted_string |
29 |
|
|
## param, parameter |
30 |
|
|
## M_parameter, M_parameter_name, M_parameter_extended_value |
31 |
|
|
|
32 |
|
|
=head1 CONSTRUCTORS |
33 |
wakaba |
1.4 |
|
34 |
wakaba |
1.6 |
The following methods construct new objects: |
35 |
wakaba |
1.1 |
|
36 |
wakaba |
1.6 |
=over 4 |
37 |
wakaba |
1.1 |
|
38 |
|
|
=cut |
39 |
|
|
|
40 |
wakaba |
1.6 |
## Initialize of this class -- called by constructors |
41 |
|
|
sub _init ($;%) { |
42 |
|
|
my $self = shift; |
43 |
|
|
my %options = @_; |
44 |
|
|
my %DEFAULT = ( |
45 |
|
|
#delete_fws ## Inheritted |
46 |
|
|
#encoding_after_encode ## Inherited |
47 |
|
|
#encoding_before_decode ## Inherited |
48 |
|
|
#format ## Inherited |
49 |
|
|
#hook_encode_string ## Inherited |
50 |
|
|
#hook_decode_string ## Inherited |
51 |
|
|
#parameter_name_case_sensible ## Inherited |
52 |
|
|
#parameter_value_max_length ## Inherited |
53 |
|
|
#parse_all ## Inherited |
54 |
|
|
#use_parameter_extension ## Inherited |
55 |
|
|
-value_case_sensible => 1, |
56 |
|
|
-value_default => '', |
57 |
|
|
-value_no_regex => qr/(?!)/, ## default = (none) |
58 |
|
|
-value_regex => qr/[\x00-\xFF]+/, |
59 |
|
|
#value_type ## Inherited |
60 |
|
|
); |
61 |
|
|
$self->SUPER::_init (%DEFAULT, %options); |
62 |
|
|
|
63 |
|
|
my $fname = $self->_n11n_field_name ($self->{option}->{field_name}); |
64 |
|
|
if ($fname eq 'content-disposition') { |
65 |
|
|
$self->{option}->{value_case_sensible} = 0; |
66 |
|
|
$self->{option}->{value_default} = 'inline'; |
67 |
|
|
$self->{option}->{value_no_regex} = $REG{NON_token}; |
68 |
|
|
unless ($self->{option}->{format} =~ /^http/) { |
69 |
|
|
$self->{option}->{value_no_regex} = $REG{NON_http_token}; |
70 |
|
|
$self->{option}->{use_parameter_extension} = 1; |
71 |
|
|
} |
72 |
|
|
$self->{option}->{value_type}->{'creation-date'} = ['Message::Field::Date']; |
73 |
|
|
$self->{option}->{value_type}->{'modification-date'} = ['Message::Field::Date']; |
74 |
|
|
$self->{option}->{value_type}->{'read-date'} = ['Message::Field::Date']; |
75 |
|
|
} elsif ($fname eq 'link') { |
76 |
|
|
$self->{option}->{parameter_value_unsafe_rule}->{'*value'} = 'MATCH_NONE'; |
77 |
|
|
$self->{option}->{value_type}->{'*value'} = ['Message::Field::URI']; |
78 |
wakaba |
1.7 |
} elsif ($fname eq 'auto-submitted') { |
79 |
|
|
$self->{option}->{parameter_value_unsafe_rule}->{'*value'} = 'NON_token'; |
80 |
|
|
$self->{option}->{value_type}->{increment} = ['Message::Field::Numval']; |
81 |
wakaba |
1.6 |
} else { |
82 |
|
|
$self->{option}->{parameter_value_unsafe_rule}->{'*value'} |
83 |
|
|
= 'NON_http_token_wsp'; |
84 |
|
|
} |
85 |
|
|
} |
86 |
wakaba |
1.1 |
|
87 |
|
|
## Initialization for new () method. |
88 |
|
|
sub _initialize_new ($;%) { |
89 |
|
|
my $self = shift; |
90 |
wakaba |
1.6 |
$self->{value} = $self->{option}->{value_default}; |
91 |
wakaba |
1.1 |
} |
92 |
|
|
|
93 |
|
|
## Initialization for parse () method. |
94 |
wakaba |
1.6 |
#sub _initialize_parse ($;%) { |
95 |
|
|
## Inherited |
96 |
|
|
#} |
97 |
|
|
|
98 |
|
|
=item $vp = Message::Field::ValueParams->new ([%options]) |
99 |
|
|
|
100 |
|
|
Constructs a new object. You might pass some options as parameters |
101 |
|
|
to the constructor. |
102 |
|
|
|
103 |
|
|
=cut |
104 |
|
|
|
105 |
|
|
## Inherited |
106 |
wakaba |
1.1 |
|
107 |
wakaba |
1.6 |
=item $vp = Message::Field::ValueParams->parse ($field-body, [%options]) |
108 |
wakaba |
1.1 |
|
109 |
wakaba |
1.6 |
Constructs a new object with given field body. You might pass |
110 |
|
|
some options as parameters to the constructor. |
111 |
wakaba |
1.1 |
|
112 |
|
|
=cut |
113 |
|
|
|
114 |
|
|
## Inherited |
115 |
|
|
|
116 |
|
|
sub _save_param ($@) { |
117 |
|
|
my $self = shift; |
118 |
|
|
my @p = @_; |
119 |
wakaba |
1.6 |
$self->{value} = $self->{option}->{value_default}; |
120 |
wakaba |
1.1 |
if ($p[0]->[1]->{is_parameter} == 0) { |
121 |
|
|
my $type = shift (@p)->[0]; |
122 |
|
|
if ($type && $type !~ /$self->{option}->{value_no_regex}/) { |
123 |
|
|
$self->{value} = $type; |
124 |
|
|
} elsif ($type) { |
125 |
|
|
push @p, ['x-invalid-value' => {value => $type, is_parameter => 1}]; |
126 |
|
|
} |
127 |
|
|
} |
128 |
wakaba |
1.5 |
#$self->{param} = \@p; |
129 |
|
|
$self->SUPER::_save_param (@p); |
130 |
wakaba |
1.1 |
$self; |
131 |
|
|
} |
132 |
|
|
|
133 |
wakaba |
1.6 |
=back |
134 |
|
|
|
135 |
|
|
=head1 METHODS |
136 |
|
|
|
137 |
|
|
=over 4 |
138 |
|
|
|
139 |
|
|
=item $vp->add ($name => [$value], [$name => $value,...]) |
140 |
wakaba |
1.1 |
|
141 |
|
|
Sets new parameter C<value> of $name. |
142 |
|
|
|
143 |
|
|
Example: |
144 |
wakaba |
1.6 |
$vp->add (title => 'foo of bar'); ## title="foo of bar" |
145 |
|
|
$vp->add (subject => 'hogehoge, foo'); ## subject*=''hogehoge%2C%20foo |
146 |
|
|
$vp->add (foo => 'bar', language => 'en') ## foo*='en'bar |
147 |
wakaba |
1.1 |
|
148 |
|
|
This method returns array reference of (name, {value => value, attribute...}). |
149 |
|
|
C<value> is same as returned value of C<$self-E<gt>parameter>. |
150 |
|
|
|
151 |
|
|
Available options: charset (charset name), language (language tag), |
152 |
|
|
value (1/0, see example above). |
153 |
|
|
|
154 |
wakaba |
1.6 |
=item $vp->replace ($name => [$value], [$name => $value,...]) |
155 |
wakaba |
1.1 |
|
156 |
wakaba |
1.6 |
=item $count = $vp->count |
157 |
wakaba |
1.1 |
|
158 |
wakaba |
1.6 |
Returns the number of C<parameter>s. |
159 |
|
|
|
160 |
|
|
=item $param-value = $vp->parameter ($name, [$new_value]) |
161 |
wakaba |
1.1 |
|
162 |
|
|
Returns given C<name>'ed C<parameter>'s C<value>. |
163 |
|
|
|
164 |
|
|
Note that when $self->{option}->{value_type}->{$name} |
165 |
|
|
is defined (and it is class name), returned value |
166 |
|
|
is a reference to the object. |
167 |
|
|
|
168 |
wakaba |
1.6 |
=item $param-name = $vp->parameter_name ($index, [$new_name]) |
169 |
wakaba |
1.1 |
|
170 |
|
|
Returns (and set) C<$index>'th C<parameter>'s name. |
171 |
|
|
|
172 |
wakaba |
1.6 |
=item $param-value = $vp->parameter_value ($index, [$new_value]) |
173 |
wakaba |
1.1 |
|
174 |
|
|
Returns (and set) C<$index>'th C<parameter>'s value. |
175 |
|
|
|
176 |
|
|
Note that when $self->{option}->{value_type}->{$name} |
177 |
|
|
is defined (and it is class name), returned value |
178 |
|
|
is a reference to the object. |
179 |
|
|
|
180 |
|
|
=cut |
181 |
|
|
|
182 |
wakaba |
1.6 |
## add, replace, count, parameter, parameter_name, parameter_value: Inherited. |
183 |
wakaba |
1.1 |
|
184 |
|
|
## Hook called before returning C<value>. |
185 |
|
|
## $self->_param_value ($name, $value); |
186 |
wakaba |
1.4 |
## -- Inherited. |
187 |
wakaba |
1.1 |
|
188 |
wakaba |
1.6 |
=item $field-body = $vp->stringify () |
189 |
wakaba |
1.1 |
|
190 |
wakaba |
1.6 |
Returns C<field-body> as a string. |
191 |
wakaba |
1.1 |
|
192 |
|
|
=cut |
193 |
|
|
|
194 |
|
|
sub stringify ($;%) { |
195 |
|
|
my $self = shift; |
196 |
|
|
my $param = $self->SUPER::stringify (@_); |
197 |
wakaba |
1.7 |
$self->value_as_string (@_).(length $param? '; '.$param: ''); |
198 |
wakaba |
1.6 |
} |
199 |
|
|
*as_string = \&stringify; |
200 |
|
|
|
201 |
|
|
## This method is intended to be used by child classes |
202 |
|
|
sub stringify_params ($;%) { |
203 |
|
|
shift->SUPER::stringify (@_); |
204 |
wakaba |
1.1 |
} |
205 |
|
|
|
206 |
wakaba |
1.6 |
=item $value = $vp->value ([$new_value]) |
207 |
wakaba |
1.1 |
|
208 |
|
|
Returns or set value. |
209 |
|
|
|
210 |
|
|
=cut |
211 |
|
|
|
212 |
wakaba |
1.2 |
sub value ($;$%) { |
213 |
wakaba |
1.1 |
my $self = shift; |
214 |
|
|
my $new_value = shift; |
215 |
wakaba |
1.2 |
my %option = @_; |
216 |
wakaba |
1.6 |
if (defined $new_value && $new_value !~ m#$self->{option}->{value_no_regex}#) { |
217 |
wakaba |
1.1 |
$self->{value} = $new_value; |
218 |
|
|
} |
219 |
wakaba |
1.6 |
$self->{value} = $self->_param_value ('*value' => $self->{value}); |
220 |
|
|
$self->{option}->{value_case_sensible}? $self->{value}: lc $self->{value}; |
221 |
wakaba |
1.2 |
} |
222 |
|
|
|
223 |
wakaba |
1.6 |
=item $value = $vp->value_as_string ([%options]) |
224 |
wakaba |
1.2 |
|
225 |
|
|
Returns value. If necessary, quoted and encoded in |
226 |
|
|
message format. Same as C<stringify> except that |
227 |
|
|
only first "value" is outputed. |
228 |
|
|
|
229 |
|
|
=cut |
230 |
|
|
|
231 |
|
|
sub value_as_string ($;%) { |
232 |
|
|
my $self = shift; |
233 |
|
|
my (%e) = &{$self->{option}->{hook_encode_string}} ($self, |
234 |
|
|
$self->{value}, type => 'phrase'); |
235 |
wakaba |
1.6 |
my $unsafe_rule = $self->{option}->{parameter_value_unsafe_rule}->{'*value'}; |
236 |
|
|
Message::Util::quote_unsafe_string ($e{value}, unsafe => $unsafe_rule); |
237 |
wakaba |
1.1 |
} |
238 |
|
|
|
239 |
|
|
|
240 |
wakaba |
1.6 |
=item $option-value = $vp->option ($option-name) |
241 |
|
|
|
242 |
|
|
Gets option value. |
243 |
wakaba |
1.1 |
|
244 |
wakaba |
1.6 |
=item $vp->option ($option-name, $option-value, ...) |
245 |
|
|
|
246 |
|
|
Set option value(s). You can pass multiple option name-value pair |
247 |
|
|
as parameter when setting. |
248 |
wakaba |
1.1 |
|
249 |
|
|
=cut |
250 |
|
|
|
251 |
|
|
## Inherited. |
252 |
|
|
|
253 |
wakaba |
1.6 |
=item $clone = $ua->clone () |
254 |
|
|
|
255 |
|
|
Returns a copy of the object. |
256 |
|
|
|
257 |
|
|
=cut |
258 |
|
|
|
259 |
|
|
## Inherited |
260 |
|
|
|
261 |
|
|
=head1 EXAMPLE |
262 |
|
|
|
263 |
|
|
use Message::Field::ValueParams; |
264 |
|
|
my $cd = new Message::Field::ValueParams (-field_name => 'Content-Disposition'); |
265 |
|
|
$cd->type ('attachment'); |
266 |
|
|
$cd->parameter ('filename' => 'foobar'); |
267 |
|
|
$cd->parameter ('creation-date' => '')->unix_time (0); |
268 |
|
|
print $cd; ## attachment; filename=foobar; |
269 |
|
|
## creation-date="Thr, 01 Jan 1970 00:00:00 +0000" |
270 |
|
|
|
271 |
|
|
use Message::Field::ValueParams; |
272 |
|
|
my $b = q{attachment; filename*=iso-2022-jp''%1B%24B%25U%25%21%25%24%25k%1B%28B}; |
273 |
|
|
my $cd = Message::Field::ValueParams->parse ($b, |
274 |
|
|
-field_name => 'Content-Disposition'); |
275 |
|
|
my $filename = $cd->parameter ('FileName'); |
276 |
|
|
if (!$filename || $filename =~ /[^A-Za-z0-9.,_~=+-]/ || -e $filename) { |
277 |
|
|
## $filename can be unsafe, see RFC 2183. |
278 |
|
|
$filename = 'default'; |
279 |
|
|
} |
280 |
|
|
open MSG, "> $filename"; |
281 |
|
|
print $something; |
282 |
|
|
close MSG; |
283 |
wakaba |
1.1 |
|
284 |
|
|
=head1 LICENSE |
285 |
|
|
|
286 |
|
|
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>. |
287 |
|
|
|
288 |
|
|
This program is free software; you can redistribute it and/or modify |
289 |
|
|
it under the terms of the GNU General Public License as published by |
290 |
|
|
the Free Software Foundation; either version 2 of the License, or |
291 |
|
|
(at your option) any later version. |
292 |
|
|
|
293 |
|
|
This program is distributed in the hope that it will be useful, |
294 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
295 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
296 |
|
|
GNU General Public License for more details. |
297 |
|
|
|
298 |
|
|
You should have received a copy of the GNU General Public License |
299 |
|
|
along with this program; see the file COPYING. If not, write to |
300 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
301 |
|
|
Boston, MA 02111-1307, USA. |
302 |
|
|
|
303 |
|
|
=head1 CHANGE |
304 |
|
|
|
305 |
|
|
See F<ChangeLog>. |
306 |
wakaba |
1.7 |
$Date: 2002/04/21 04:27:42 $ |
307 |
wakaba |
1.1 |
|
308 |
|
|
=cut |
309 |
|
|
|
310 |
|
|
1; |