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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24