/[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.11 - (hide annotations) (download)
Sat Jul 6 10:30:43 2002 UTC (22 years, 4 months ago) by wakaba
Branch: MAIN
Changes since 1.10: +4 -2 lines
2002-06-29  Wakaba <w@suika.fam.cx>

	* ContentType.pm, Params.pm, ValueParams.pm,
	XMoe.pm: Rewritten.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24