/[suikacvs]/messaging/manakai/lib/Message/Field/ValueParams.pm
Suika

Contents of /messaging/manakai/lib/Message/Field/ValueParams.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations) (download)
Sat Aug 3 04:57:59 2002 UTC (22 years, 3 months ago) by wakaba
Branch: MAIN
CVS Tags: before-dis2-200411, manakai-release-0-3-2, manakai-release-0-3-1, manakai-release-0-4-0, manakai-200612, msg-0-1, HEAD
Branch point for: branch-suikawiki-1, experimental-xml-parser-200401, stable
Changes since 1.11: +4 -2 lines
Error occurred while calculating annotation data.
2002-08-03  Wakaba <w@suika.fam.cx>

	* Status.pm: New module.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24