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 |
|
|
$VERSION=do{my @r=(q$Revision: 1.5 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
13 |
|
|
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 |
|
|
} else { |
79 |
|
|
$self->{option}->{parameter_value_unsafe_rule}->{'*value'} |
80 |
|
|
= 'NON_http_token_wsp'; |
81 |
|
|
} |
82 |
|
|
} |
83 |
wakaba |
1.1 |
|
84 |
|
|
## Initialization for new () method. |
85 |
|
|
sub _initialize_new ($;%) { |
86 |
|
|
my $self = shift; |
87 |
wakaba |
1.6 |
$self->{value} = $self->{option}->{value_default}; |
88 |
wakaba |
1.1 |
} |
89 |
|
|
|
90 |
|
|
## Initialization for parse () method. |
91 |
wakaba |
1.6 |
#sub _initialize_parse ($;%) { |
92 |
|
|
## Inherited |
93 |
|
|
#} |
94 |
|
|
|
95 |
|
|
=item $vp = Message::Field::ValueParams->new ([%options]) |
96 |
|
|
|
97 |
|
|
Constructs a new object. You might pass some options as parameters |
98 |
|
|
to the constructor. |
99 |
|
|
|
100 |
|
|
=cut |
101 |
|
|
|
102 |
|
|
## Inherited |
103 |
wakaba |
1.1 |
|
104 |
wakaba |
1.6 |
=item $vp = Message::Field::ValueParams->parse ($field-body, [%options]) |
105 |
wakaba |
1.1 |
|
106 |
wakaba |
1.6 |
Constructs a new object with given field body. You might pass |
107 |
|
|
some options as parameters to the constructor. |
108 |
wakaba |
1.1 |
|
109 |
|
|
=cut |
110 |
|
|
|
111 |
|
|
## Inherited |
112 |
|
|
|
113 |
|
|
sub _save_param ($@) { |
114 |
|
|
my $self = shift; |
115 |
|
|
my @p = @_; |
116 |
wakaba |
1.6 |
$self->{value} = $self->{option}->{value_default}; |
117 |
wakaba |
1.1 |
if ($p[0]->[1]->{is_parameter} == 0) { |
118 |
|
|
my $type = shift (@p)->[0]; |
119 |
|
|
if ($type && $type !~ /$self->{option}->{value_no_regex}/) { |
120 |
|
|
$self->{value} = $type; |
121 |
|
|
} elsif ($type) { |
122 |
|
|
push @p, ['x-invalid-value' => {value => $type, is_parameter => 1}]; |
123 |
|
|
} |
124 |
|
|
} |
125 |
wakaba |
1.5 |
#$self->{param} = \@p; |
126 |
|
|
$self->SUPER::_save_param (@p); |
127 |
wakaba |
1.1 |
$self; |
128 |
|
|
} |
129 |
|
|
|
130 |
wakaba |
1.6 |
=back |
131 |
|
|
|
132 |
|
|
=head1 METHODS |
133 |
|
|
|
134 |
|
|
=over 4 |
135 |
|
|
|
136 |
|
|
=item $vp->add ($name => [$value], [$name => $value,...]) |
137 |
wakaba |
1.1 |
|
138 |
|
|
Sets new parameter C<value> of $name. |
139 |
|
|
|
140 |
|
|
Example: |
141 |
wakaba |
1.6 |
$vp->add (title => 'foo of bar'); ## title="foo of bar" |
142 |
|
|
$vp->add (subject => 'hogehoge, foo'); ## subject*=''hogehoge%2C%20foo |
143 |
|
|
$vp->add (foo => 'bar', language => 'en') ## foo*='en'bar |
144 |
wakaba |
1.1 |
|
145 |
|
|
This method returns array reference of (name, {value => value, attribute...}). |
146 |
|
|
C<value> is same as returned value of C<$self-E<gt>parameter>. |
147 |
|
|
|
148 |
|
|
Available options: charset (charset name), language (language tag), |
149 |
|
|
value (1/0, see example above). |
150 |
|
|
|
151 |
wakaba |
1.6 |
=item $vp->replace ($name => [$value], [$name => $value,...]) |
152 |
wakaba |
1.1 |
|
153 |
wakaba |
1.6 |
=item $count = $vp->count |
154 |
wakaba |
1.1 |
|
155 |
wakaba |
1.6 |
Returns the number of C<parameter>s. |
156 |
|
|
|
157 |
|
|
=item $param-value = $vp->parameter ($name, [$new_value]) |
158 |
wakaba |
1.1 |
|
159 |
|
|
Returns given C<name>'ed C<parameter>'s C<value>. |
160 |
|
|
|
161 |
|
|
Note that when $self->{option}->{value_type}->{$name} |
162 |
|
|
is defined (and it is class name), returned value |
163 |
|
|
is a reference to the object. |
164 |
|
|
|
165 |
wakaba |
1.6 |
=item $param-name = $vp->parameter_name ($index, [$new_name]) |
166 |
wakaba |
1.1 |
|
167 |
|
|
Returns (and set) C<$index>'th C<parameter>'s name. |
168 |
|
|
|
169 |
wakaba |
1.6 |
=item $param-value = $vp->parameter_value ($index, [$new_value]) |
170 |
wakaba |
1.1 |
|
171 |
|
|
Returns (and set) C<$index>'th C<parameter>'s value. |
172 |
|
|
|
173 |
|
|
Note that when $self->{option}->{value_type}->{$name} |
174 |
|
|
is defined (and it is class name), returned value |
175 |
|
|
is a reference to the object. |
176 |
|
|
|
177 |
|
|
=cut |
178 |
|
|
|
179 |
wakaba |
1.6 |
## add, replace, count, parameter, parameter_name, parameter_value: Inherited. |
180 |
wakaba |
1.1 |
|
181 |
|
|
## Hook called before returning C<value>. |
182 |
|
|
## $self->_param_value ($name, $value); |
183 |
wakaba |
1.4 |
## -- Inherited. |
184 |
wakaba |
1.1 |
|
185 |
wakaba |
1.6 |
=item $field-body = $vp->stringify () |
186 |
wakaba |
1.1 |
|
187 |
wakaba |
1.6 |
Returns C<field-body> as a string. |
188 |
wakaba |
1.1 |
|
189 |
|
|
=cut |
190 |
|
|
|
191 |
|
|
sub stringify ($;%) { |
192 |
|
|
my $self = shift; |
193 |
|
|
my $param = $self->SUPER::stringify (@_); |
194 |
wakaba |
1.6 |
$self->value_as_string (@_).(defined $param? '; '.$param: ''); |
195 |
|
|
} |
196 |
|
|
*as_string = \&stringify; |
197 |
|
|
|
198 |
|
|
## This method is intended to be used by child classes |
199 |
|
|
sub stringify_params ($;%) { |
200 |
|
|
shift->SUPER::stringify (@_); |
201 |
wakaba |
1.1 |
} |
202 |
|
|
|
203 |
wakaba |
1.6 |
=item $value = $vp->value ([$new_value]) |
204 |
wakaba |
1.1 |
|
205 |
|
|
Returns or set value. |
206 |
|
|
|
207 |
|
|
=cut |
208 |
|
|
|
209 |
wakaba |
1.2 |
sub value ($;$%) { |
210 |
wakaba |
1.1 |
my $self = shift; |
211 |
|
|
my $new_value = shift; |
212 |
wakaba |
1.2 |
my %option = @_; |
213 |
wakaba |
1.6 |
if (defined $new_value && $new_value !~ m#$self->{option}->{value_no_regex}#) { |
214 |
wakaba |
1.1 |
$self->{value} = $new_value; |
215 |
|
|
} |
216 |
wakaba |
1.6 |
$self->{value} = $self->_param_value ('*value' => $self->{value}); |
217 |
|
|
$self->{option}->{value_case_sensible}? $self->{value}: lc $self->{value}; |
218 |
wakaba |
1.2 |
} |
219 |
|
|
|
220 |
wakaba |
1.6 |
=item $value = $vp->value_as_string ([%options]) |
221 |
wakaba |
1.2 |
|
222 |
|
|
Returns value. If necessary, quoted and encoded in |
223 |
|
|
message format. Same as C<stringify> except that |
224 |
|
|
only first "value" is outputed. |
225 |
|
|
|
226 |
|
|
=cut |
227 |
|
|
|
228 |
|
|
sub value_as_string ($;%) { |
229 |
|
|
my $self = shift; |
230 |
|
|
my (%e) = &{$self->{option}->{hook_encode_string}} ($self, |
231 |
|
|
$self->{value}, type => 'phrase'); |
232 |
wakaba |
1.6 |
my $unsafe_rule = $self->{option}->{parameter_value_unsafe_rule}->{'*value'}; |
233 |
|
|
Message::Util::quote_unsafe_string ($e{value}, unsafe => $unsafe_rule); |
234 |
wakaba |
1.1 |
} |
235 |
|
|
|
236 |
|
|
|
237 |
wakaba |
1.6 |
=item $option-value = $vp->option ($option-name) |
238 |
|
|
|
239 |
|
|
Gets option value. |
240 |
wakaba |
1.1 |
|
241 |
wakaba |
1.6 |
=item $vp->option ($option-name, $option-value, ...) |
242 |
|
|
|
243 |
|
|
Set option value(s). You can pass multiple option name-value pair |
244 |
|
|
as parameter when setting. |
245 |
wakaba |
1.1 |
|
246 |
|
|
=cut |
247 |
|
|
|
248 |
|
|
## Inherited. |
249 |
|
|
|
250 |
wakaba |
1.6 |
=item $clone = $ua->clone () |
251 |
|
|
|
252 |
|
|
Returns a copy of the object. |
253 |
|
|
|
254 |
|
|
=cut |
255 |
|
|
|
256 |
|
|
## Inherited |
257 |
|
|
|
258 |
|
|
=head1 EXAMPLE |
259 |
|
|
|
260 |
|
|
use Message::Field::ValueParams; |
261 |
|
|
my $cd = new Message::Field::ValueParams (-field_name => 'Content-Disposition'); |
262 |
|
|
$cd->type ('attachment'); |
263 |
|
|
$cd->parameter ('filename' => 'foobar'); |
264 |
|
|
$cd->parameter ('creation-date' => '')->unix_time (0); |
265 |
|
|
print $cd; ## attachment; filename=foobar; |
266 |
|
|
## creation-date="Thr, 01 Jan 1970 00:00:00 +0000" |
267 |
|
|
|
268 |
|
|
use Message::Field::ValueParams; |
269 |
|
|
my $b = q{attachment; filename*=iso-2022-jp''%1B%24B%25U%25%21%25%24%25k%1B%28B}; |
270 |
|
|
my $cd = Message::Field::ValueParams->parse ($b, |
271 |
|
|
-field_name => 'Content-Disposition'); |
272 |
|
|
my $filename = $cd->parameter ('FileName'); |
273 |
|
|
if (!$filename || $filename =~ /[^A-Za-z0-9.,_~=+-]/ || -e $filename) { |
274 |
|
|
## $filename can be unsafe, see RFC 2183. |
275 |
|
|
$filename = 'default'; |
276 |
|
|
} |
277 |
|
|
open MSG, "> $filename"; |
278 |
|
|
print $something; |
279 |
|
|
close MSG; |
280 |
wakaba |
1.1 |
|
281 |
|
|
=head1 LICENSE |
282 |
|
|
|
283 |
|
|
Copyright 2002 wakaba E<lt>w@suika.fam.cxE<gt>. |
284 |
|
|
|
285 |
|
|
This program is free software; you can redistribute it and/or modify |
286 |
|
|
it under the terms of the GNU General Public License as published by |
287 |
|
|
the Free Software Foundation; either version 2 of the License, or |
288 |
|
|
(at your option) any later version. |
289 |
|
|
|
290 |
|
|
This program is distributed in the hope that it will be useful, |
291 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
292 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
293 |
|
|
GNU General Public License for more details. |
294 |
|
|
|
295 |
|
|
You should have received a copy of the GNU General Public License |
296 |
|
|
along with this program; see the file COPYING. If not, write to |
297 |
|
|
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
298 |
|
|
Boston, MA 02111-1307, USA. |
299 |
|
|
|
300 |
|
|
=head1 CHANGE |
301 |
|
|
|
302 |
|
|
See F<ChangeLog>. |
303 |
wakaba |
1.6 |
$Date: 2002/04/01 05:32:15 $ |
304 |
wakaba |
1.1 |
|
305 |
|
|
=cut |
306 |
|
|
|
307 |
|
|
1; |